use std::collections::HashSet;
use happenstance_core::{Event, EventId, RecordedAt, StoreId};
use happenstance_sync::{
Classify, FailureClass, IngestError, IngestGroup, Ingested, SendIngestStore, Watermark,
};
use sqlx::{PgPool, Postgres, Transaction};
use super::{
NewRow, Origin, PostgresEventStore, StoredOrigin, allocate_positions, exceeded,
insert_statement, now, position_from_row,
};
use crate::error::PostgresEventStoreError;
const CONFLICT_CLAUSE: &str = " ON CONFLICT (origin_store, origin_position) \
WHERE origin_store IS NOT NULL AND origin_position IS NOT NULL DO NOTHING \
RETURNING origin_store, origin_position";
const BEGIN_SNAPSHOT_SQL: &str = "SELECT pg_current_snapshot()::text";
const HIDDEN_CLAIMS_SQL: &str = "SELECT origin_position FROM event \
WHERE origin_store = $1 AND origin_position = ANY($2) \
AND NOT pg_visible_in_snapshot(xact_id, $3::text::pg_snapshot)";
const WATERMARK_SQL: &str = "SELECT origin_store, max(origin_position) FROM event \
WHERE origin_store IS NOT NULL AND origin_position IS NOT NULL GROUP BY origin_store";
const ROW_PARAMETERS: usize = 8;
const MAX_BIND_PARAMETERS: usize = 65_535;
const _: () =
assert!(PostgresEventStore::MAX_EVENTS_PER_BATCH * ROW_PARAMETERS <= MAX_BIND_PARAMETERS);
impl PostgresEventStore {
async fn ingest_groups(
&self,
groups: &[IngestGroup<'_>],
) -> Result<Ingested, IngestError<PostgresEventStoreError>> {
if groups.iter().all(|group| group.events.is_empty()) {
return Ok(Ingested::default());
}
let len = groups
.iter()
.map(|group| group.events.len() + group.compensation.len())
.sum();
let events = groups.iter().flat_map(|group| {
group
.events
.iter()
.map(|replicated| &replicated.event)
.chain(group.compensation)
});
if let Some((limit, len)) = exceeded(len, events) {
return Err(IngestError::exceeds_store_limit(limit, len));
}
let owned = groups
.iter()
.map(OwnedGroup::try_from_group)
.collect::<Result<Vec<_>, _>>()
.map_err(IngestError::Store)?;
let own = Self::store_id(self).await.map_err(IngestError::Store)?;
let recorded_at = now();
let pool = self.pool.clone();
let outcome = self
.on_runtime(ingest_in_transaction(pool, own, owned, recorded_at))
.await
.map_err(IngestError::Store)?;
match outcome {
Outcome::Ingested(ingested) => Ok(ingested),
Outcome::UnheldOwnIdentity(id) => Err(IngestError::unheld_own_identity(id)),
}
}
async fn origin_watermark(&self) -> Result<Watermark, PostgresEventStoreError> {
let pool = self.pool.clone();
let rows: Vec<(Vec<u8>, i64)> = self
.on_runtime(async move {
sqlx::query_as(WATERMARK_SQL)
.fetch_all(&pool)
.await
.map_err(PostgresEventStoreError::from)
})
.await?;
let mut watermark = Watermark::new();
for (store, highest) in rows {
watermark.advance(stored_store_id(&store)?, position_from_row(highest)?);
}
Ok(watermark)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
impl SendIngestStore for PostgresEventStore {
type Error = PostgresEventStoreError;
async fn store_id(&self) -> Result<StoreId, Self::Error> {
Self::store_id(self).await
}
async fn ingest(
&self,
groups: &[IngestGroup<'_>],
) -> Result<Ingested, IngestError<Self::Error>> {
self.ingest_groups(groups).await
}
async fn watermark(&self) -> Result<Watermark, Self::Error> {
self.origin_watermark().await
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
impl Classify for PostgresEventStoreError {
fn class(&self) -> FailureClass {
match self {
Self::Driver(error) if transient_driver_failure(error) => FailureClass::Transient,
Self::Worker(error) if error.is_cancelled() => FailureClass::Transient,
Self::OriginPositionOutOfRange { .. } => FailureClass::Park,
Self::Driver(_)
| Self::Worker(_)
| Self::PositionOutOfRange { .. }
| Self::EventType(_)
| Self::Tag(_)
| Self::MissingIdentity
| Self::MalformedIdentity { .. }
| Self::UnstampedEvent { .. }
| Self::NoRuntime
| Self::UnreadableTransactionId => FailureClass::Stop,
}
}
}
fn transient_driver_failure(error: &sqlx::Error) -> bool {
match error {
sqlx::Error::Database(database) => {
database.code().as_deref().is_some_and(transient_sqlstate)
}
sqlx::Error::Io(_) | sqlx::Error::PoolTimedOut => true,
_ => false,
}
}
fn transient_sqlstate(code: &str) -> bool {
code.starts_with("08")
|| code.starts_with("53")
|| matches!(code, "40001" | "40P01" | "57P01" | "57P02" | "57P03")
}
struct OwnedGroup {
events: Vec<ForeignEvent>,
compensation: Vec<Event>,
}
impl OwnedGroup {
fn try_from_group(group: &IngestGroup<'_>) -> Result<Self, PostgresEventStoreError> {
let events = group
.events
.iter()
.map(|replicated| {
Ok(ForeignEvent {
id: replicated.id,
origin: StoredOrigin::try_from(replicated.id)?,
recorded_at: replicated.recorded_at,
event: replicated.event.clone(),
})
})
.collect::<Result<_, PostgresEventStoreError>>()?;
Ok(Self {
events,
compensation: group.compensation.to_vec(),
})
}
}
struct ForeignEvent {
id: EventId,
origin: StoredOrigin,
recorded_at: RecordedAt,
event: Event,
}
impl ForeignEvent {
fn row(&self) -> NewRow<'_> {
NewRow {
event: &self.event,
origin: Origin::Foreign(self.origin),
recorded_at: self.recorded_at,
}
}
}
enum Outcome {
Ingested(Ingested),
UnheldOwnIdentity(EventId),
}
async fn ingest_in_transaction(
pool: PgPool,
own: StoreId,
groups: Vec<OwnedGroup>,
recorded_at: RecordedAt,
) -> Result<Outcome, PostgresEventStoreError> {
let mut transaction = pool.begin().await?;
let claims = groups
.iter()
.flat_map(|group| &group.events)
.any(|foreign| foreign.id.store() == own);
let began: Option<String> = if claims {
Some(
sqlx::query_scalar(BEGIN_SNAPSHOT_SQL)
.fetch_one(&mut *transaction)
.await?,
)
} else {
None
};
let mut ingested = Ingested::default();
for group in &groups {
let landed = insert_foreign(&mut transaction, own, &group.events).await?;
if let Some(began) = &began
&& let Some(id) =
unheld_claim(&mut transaction, own, &group.events, &landed, began).await?
{
transaction.rollback().await?;
return Ok(Outcome::UnheldOwnIdentity(id));
}
ingested.appended += landed.len();
ingested.skipped += group.events.len().saturating_sub(landed.len());
if !landed.is_empty() {
insert_compensation(&mut transaction, own, &group.compensation, recorded_at).await?;
ingested.compensated += group.compensation.len();
}
}
transaction.commit().await?;
Ok(Outcome::Ingested(ingested))
}
async fn insert_foreign(
transaction: &mut Transaction<'static, Postgres>,
own: StoreId,
events: &[ForeignEvent],
) -> Result<HashSet<EventId>, PostgresEventStoreError> {
if events.is_empty() {
return Ok(HashSet::new());
}
let positions = allocate_positions(transaction, events.len()).await?;
let returned: Vec<(Vec<u8>, i64)> = foreign_statement(own, events, &positions)
.build_query_as()
.fetch_all(&mut **transaction)
.await?;
returned
.into_iter()
.map(|(store, position)| {
Ok(EventId::new(
stored_store_id(&store)?,
position_from_row(position)?,
))
})
.collect()
}
async fn insert_compensation(
transaction: &mut Transaction<'static, Postgres>,
own: StoreId,
compensation: &[Event],
recorded_at: RecordedAt,
) -> Result<(), PostgresEventStoreError> {
if compensation.is_empty() {
return Ok(());
}
let positions = allocate_positions(transaction, compensation.len()).await?;
let rows = compensation.iter().map(|event| NewRow {
event,
origin: Origin::Local,
recorded_at,
});
insert_statement(own, rows, &positions)
.build()
.execute(&mut **transaction)
.await?;
Ok(())
}
fn foreign_statement(
own: StoreId,
events: &[ForeignEvent],
positions: &[i64],
) -> sqlx::QueryBuilder<'static, Postgres> {
let mut statement = insert_statement(own, events.iter().map(ForeignEvent::row), positions);
statement.push(CONFLICT_CLAUSE);
statement
}
async fn unheld_claim(
transaction: &mut Transaction<'static, Postgres>,
own: StoreId,
events: &[ForeignEvent],
landed: &HashSet<EventId>,
began: &str,
) -> Result<Option<EventId>, PostgresEventStoreError> {
let skipped: Vec<i64> = events
.iter()
.filter(|foreign| foreign.id.store() == own && !landed.contains(&foreign.id))
.map(|foreign| foreign.origin.position)
.collect();
let hidden: HashSet<i64> = if skipped.is_empty() {
HashSet::new()
} else {
sqlx::query_scalar::<Postgres, i64>(HIDDEN_CLAIMS_SQL)
.bind(own.to_bytes().to_vec())
.bind(skipped)
.bind(began)
.fetch_all(&mut **transaction)
.await?
.into_iter()
.collect()
};
Ok(events
.iter()
.filter(|foreign| foreign.id.store() == own)
.find(|foreign| landed.contains(&foreign.id) || hidden.contains(&foreign.origin.position))
.map(|foreign| foreign.id))
}
fn stored_store_id(stored: &[u8]) -> Result<StoreId, PostgresEventStoreError> {
let bytes: [u8; 16] = stored
.try_into()
.map_err(|_| PostgresEventStoreError::MalformedIdentity { len: stored.len() })?;
Ok(StoreId::from_bytes(bytes))
}
#[cfg(test)]
mod tests;