use std::collections::HashSet;
use happenstance_core::{AppendCondition, Event, EventId, RecordedAt, StoreId};
use happenstance_sync::{
Classify, FailureClass, IngestError, IngestGroup, Ingested, SendIngestStore, Watermark,
};
use sqlx::{Executor, PgPool, Postgres, Row, Transaction};
use super::{
NewRow, Origin, PostgresEventStore, ROW_PARAMETERS, StoredOrigin, allocate_positions, exceeded,
identity_from_row, insert_statement, now, position_from_row, stored_store_id, write_group,
};
use crate::error::PostgresEventStoreError;
mod removal;
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, position";
const FIRST_ROUND_TRIP_SQL: &str = "LOCK TABLE event_removed IN SHARE MODE; \
SELECT (SELECT v FROM store_meta WHERE k = 'store_id'), \
pg_current_snapshot()::text, EXISTS (SELECT 1 FROM event_removed)";
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 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 recorded_at = now();
let pool = self.pool.clone();
let outcome = self
.on_runtime(ingest_in_transaction(pool, 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> {
PostgresEventStore::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
| Self::StoredGuard { .. }
| Self::GroupOrder(_) => 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" | "55P03" | "57P01" | "57P02" | "57P03"
)
}
struct OwnedGroup {
events: Vec<ForeignEvent>,
offered: usize,
compensation: Vec<Event>,
guard: Option<AppendCondition>,
}
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,
offered: group.events.len(),
compensation: group.compensation.to_vec(),
guard: group.guard.cloned(),
})
}
}
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,
mut groups: Vec<OwnedGroup>,
recorded_at: RecordedAt,
) -> Result<Outcome, PostgresEventStoreError> {
let mut transaction = pool.begin().await?;
let connection: &mut sqlx::PgConnection = &mut transaction;
let first = connection
.fetch_one(sqlx::raw_sql(FIRST_ROUND_TRIP_SQL))
.await?;
let stored: Option<Vec<u8>> = first.try_get(0)?;
let snapshot: String = first.try_get(1)?;
let any_removed: bool = first.try_get(2)?;
let own = identity_from_row(stored)?;
if any_removed {
removal::drop_removed(&mut transaction, &mut groups).await?;
}
let claims = groups
.iter()
.flat_map(|group| &group.events)
.any(|foreign| foreign.id.store() == own);
let began = claims.then_some(snapshot);
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.ids, began).await?
{
transaction.rollback().await?;
return Ok(Outcome::UnheldOwnIdentity(id));
}
ingested.appended += landed.ids.len();
ingested.skipped += group.offered.saturating_sub(landed.ids.len());
if let Some(first) = landed.positions.iter().copied().min() {
write_group::stamp(
&mut transaction,
first,
&landed.positions,
group.guard.as_ref(),
)
.await?;
insert_compensation(
&mut transaction,
own,
&group.compensation,
recorded_at,
first,
)
.await?;
ingested.compensated += group.compensation.len();
}
}
transaction.commit().await?;
Ok(Outcome::Ingested(ingested))
}
#[derive(Debug, Default)]
struct Landed {
ids: HashSet<EventId>,
positions: Vec<i64>,
}
async fn insert_foreign(
transaction: &mut Transaction<'static, Postgres>,
own: StoreId,
events: &[ForeignEvent],
) -> Result<Landed, PostgresEventStoreError> {
if events.is_empty() {
return Ok(Landed::default());
}
let positions = allocate_positions(transaction, events.len()).await?;
let returned: Vec<(Vec<u8>, i64, i64)> = foreign_statement(own, events, &positions)
.build_query_as()
.fetch_all(&mut **transaction)
.await?;
let mut landed = Landed {
ids: HashSet::with_capacity(returned.len()),
positions: Vec::with_capacity(returned.len()),
};
for (store, origin, position) in returned {
landed.ids.insert(EventId::new(
stored_store_id(&store)?,
position_from_row(origin)?,
));
landed.positions.push(position);
}
Ok(landed)
}
async fn insert_compensation(
transaction: &mut Transaction<'static, Postgres>,
own: StoreId,
compensation: &[Event],
recorded_at: RecordedAt,
first: i64,
) -> 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, Some(first))
.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, None);
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))
}
#[cfg(test)]
mod tests;