use std::sync::Arc;
use serde_json::{Value, json};
use stateknot_core::{
BoundedJson, BoxFuture, CancellationSignal, ChildRunJoinBinding, ChildRunJoinHead,
ChildRunJoinRequest, ChildRunSlot, Digest, EventId, JournalAppend, JournalEventIntent,
JournalEventKind, JournalExpectation, JournalPayload, RunLifecycle, SchemaReference, TenantId,
Version,
};
use stateknot_store_postgres::{
ChildJoinCommitOutcome, ChildJoinRecord, PostgresStore, StoreError,
};
use crate::{
ChildReconcilerBuildError, DurableChildReconcilerOptions, ExecutableGraphRegistry,
JsonSchemaRegistry, JsonSchemaRegistryBuilder,
};
pub const STANDARD_CHILD_JOIN_EVENT_SCHEMA_ID: &str =
"https://stknot.com/schemas/runtime/child-join-event/1.0.0";
pub fn standard_child_join_event_schema()
-> Result<(SchemaReference, Value), ChildReconcilerBuildError> {
let document: Value =
serde_json::from_str(include_str!("../schemas/child-join-event-1.0.0.json"))
.map_err(|_| ChildReconcilerBuildError::SchemaDefinition)?;
let canonical = serde_json_canonicalizer::to_vec(&document)
.map_err(|_| ChildReconcilerBuildError::SchemaDefinition)?;
Ok((
SchemaReference::new(
STANDARD_CHILD_JOIN_EVENT_SCHEMA_ID
.parse()
.map_err(|_| ChildReconcilerBuildError::SchemaDefinition)?,
Version::new(1, 0, 0),
Digest::sha256(canonical),
),
document,
))
}
pub fn register_standard_child_join_event_schema(
builder: &mut JsonSchemaRegistryBuilder,
) -> Result<SchemaReference, ChildReconcilerBuildError> {
let (reference, document) = standard_child_join_event_schema()?;
builder.register(reference.clone(), document)?;
Ok(reference)
}
pub(crate) fn schema_available(schemas: &JsonSchemaRegistry) -> bool {
standard_child_join_event_schema().is_ok_and(|(schema, _)| schemas.contains(&schema))
}
pub(crate) fn payload(
schemas: &JsonSchemaRegistry,
request: &ChildRunJoinRequest,
publication: bool,
) -> Result<JournalPayload, StoreError> {
let (schema, _) =
standard_child_join_event_schema().map_err(|_| StoreError::ChildJoinRejected)?;
let (operation, kind) = if publication {
("child_join_published", "child-join-published")
} else {
("child_join_registered", "child-join-registered")
};
let digest = request.digest().to_string();
let digest = digest
.strip_prefix("sha256:")
.ok_or(StoreError::ChildJoinRejected)?;
let data =
BoundedJson::try_from_value(json!({"operation": operation, "request_digest": digest}))
.map_err(|_| StoreError::ChildJoinRejected)?;
schemas
.validate_bounded(&schema, &data)
.map_err(|_| StoreError::ChildJoinRejected)?;
JournalPayload::new(
schema,
JournalEventKind::new(kind).map_err(|_| StoreError::ChildJoinRejected)?,
data,
)
.map_err(|_| StoreError::ChildJoinRejected)
}
#[derive(Clone, Debug)]
pub struct GraphChildJoin {
store: PostgresStore,
registry: ExecutableGraphRegistry,
binding: ChildRunJoinBinding,
head: ChildRunJoinHead,
}
impl GraphChildJoin {
pub(crate) async fn prepare(
store: PostgresStore,
registry: ExecutableGraphRegistry,
record: &ChildJoinRecord,
) -> Result<Arc<Self>, StoreError> {
if record.consumed().is_some() {
return Err(StoreError::ChildJoinRejected);
}
let value = Arc::new(Self {
store,
registry,
binding: record
.binding()
.cloned()
.ok_or(StoreError::ChildJoinRejected)?,
head: record
.head()
.cloned()
.ok_or(StoreError::ChildJoinRejected)?,
});
for key in value.binding.request().keys() {
value.load_child(key.slot()).await?;
}
Ok(value)
}
#[must_use]
pub const fn binding(&self) -> &ChildRunJoinBinding {
&self.binding
}
#[must_use]
pub const fn head(&self) -> &ChildRunJoinHead {
&self.head
}
pub async fn load_child(&self, slot: &ChildRunSlot) -> Result<RunLifecycle, StoreError> {
let index = self
.binding
.request()
.keys()
.iter()
.position(|key| key.slot() == slot)
.ok_or(StoreError::ChildJoinRejected)?;
let record = self
.store
.load_child_run(&self.binding.request().keys()[index])
.await?;
if record.child().run().is_quarantined() {
return Err(StoreError::RunQuarantined);
}
if record.settlement() != Some(&self.binding.terminals()[index]) {
return Err(StoreError::ChildJoinRejected);
}
let graph = self
.registry
.resolve(record.intent().child().graph())
.ok_or(StoreError::ChildJoinRejected)?;
if graph.graph() != record.intent().child_graph() {
return Err(StoreError::ChildJoinRejected);
}
let lifecycle = record.child().run().lifecycle();
if let Some(result) = lifecycle.result() {
if result.output_schema() != graph.graph().output_schema()
|| result.output_schema() != record.intent().child().descriptor().output_schema()
{
return Err(StoreError::ChildJoinRejected);
}
self.registry
.schemas()
.validate_bounded(result.output_schema(), result.output())
.map_err(|_| StoreError::ChildJoinRejected)?;
}
Ok(lifecycle.clone())
}
}
#[derive(Clone, Debug)]
pub struct ChildJoinPublicationCursor {
tenant: TenantId,
after: Option<ChildRunJoinRequest>,
}
#[derive(Debug)]
pub struct ChildJoinPublicationItem {
request: ChildRunJoinRequest,
result: Result<ChildJoinCommitOutcome, StoreError>,
}
impl ChildJoinPublicationItem {
#[must_use]
pub const fn request(&self) -> &ChildRunJoinRequest {
&self.request
}
pub fn result(&self) -> Result<&ChildJoinCommitOutcome, &StoreError> {
self.result.as_ref()
}
}
#[derive(Debug)]
pub struct ChildJoinPublicationTick {
cursor: ChildJoinPublicationCursor,
items: Vec<ChildJoinPublicationItem>,
cancelled: bool,
}
impl ChildJoinPublicationTick {
#[must_use]
pub const fn cursor(&self) -> &ChildJoinPublicationCursor {
&self.cursor
}
#[must_use]
pub fn items(&self) -> &[ChildJoinPublicationItem] {
&self.items
}
#[must_use]
pub const fn is_cancelled(&self) -> bool {
self.cancelled
}
}
#[derive(Clone, Debug)]
pub struct DurableChildJoinPublisher {
store: PostgresStore,
schemas: JsonSchemaRegistry,
options: DurableChildReconcilerOptions,
}
impl DurableChildJoinPublisher {
pub fn new(
store: PostgresStore,
schemas: JsonSchemaRegistry,
options: DurableChildReconcilerOptions,
) -> Result<Self, ChildReconcilerBuildError> {
if !schema_available(&schemas) {
return Err(ChildReconcilerBuildError::SchemaUnavailable);
}
Ok(Self {
store,
schemas,
options,
})
}
pub fn tick(
&self,
tenant: TenantId,
after: Option<ChildJoinPublicationCursor>,
shutdown: CancellationSignal,
) -> BoxFuture<'_, Result<ChildJoinPublicationTick, StoreError>> {
Box::pin(async move {
if after.as_ref().is_some_and(|cursor| cursor.tenant != tenant) {
return Err(StoreError::ChildJoinRejected);
}
let mut tick = ChildJoinPublicationTick {
cursor: after.unwrap_or(ChildJoinPublicationCursor {
tenant: tenant.clone(),
after: None,
}),
items: Vec::with_capacity(16),
cancelled: false,
};
let requests = tokio::select! {
biased;
() = shutdown.cancelled() => { tick.cancelled = true; return Ok(tick); }
requests = self.store.pending_child_joins_after(&tenant, tick.cursor.after.as_ref()) => requests?,
};
let exhausted = requests.len() < 16;
for request in requests {
let result = tokio::select! {
biased;
() = shutdown.cancelled() => { tick.cancelled = true; break; }
result = self.publish_with_retry(&request) => result,
};
tick.cursor.after = Some(request.clone());
tick.items
.push(ChildJoinPublicationItem { request, result });
}
if exhausted && !tick.cancelled {
tick.cursor.after = None;
}
Ok(tick)
})
}
async fn publish_with_retry(
&self,
request: &ChildRunJoinRequest,
) -> Result<ChildJoinCommitOutcome, StoreError> {
let id = EventId::generate();
for attempt in 1..=self.options.maximum_mutation_attempts() {
let result = self.publish(request, id).await;
match result {
Err(error)
if attempt < self.options.maximum_mutation_attempts()
&& (error.is_retryable()
|| matches!(error, StoreError::StaleJournalHead)) =>
{
tokio::time::sleep(self.options.retry_delay(attempt)).await;
}
other => return other,
}
}
unreachable!("validated positive attempt count")
}
async fn publish(
&self,
request: &ChildRunJoinRequest,
id: EventId,
) -> Result<ChildJoinCommitOutcome, StoreError> {
let activation = request.activation();
let run = self
.store
.load_run(activation.tenant_id(), activation.run_id())
.await?;
let append = JournalAppend::new(
JournalExpectation::exact(
run.journal_head()
.cloned()
.ok_or(StoreError::ChildJoinRejected)?,
),
JournalEventIntent::control_plane(
activation.tenant_id().clone(),
activation.run_id(),
id,
payload(&self.schemas, request, true)?,
)
.map_err(|_| StoreError::ChildJoinRejected)?,
)
.map_err(|_| StoreError::ChildJoinRejected)?;
self.store.publish_child_join(request, append).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn join_schema_is_offline_closed_and_independent_of_reconciliation_v1() {
let mut builder = JsonSchemaRegistryBuilder::default();
let schema = register_standard_child_join_event_schema(&mut builder).unwrap();
assert_eq!(
schema.digest().to_string(),
"sha256:d63a62eead850debf4a4ed79aae622bed6a89e20fe60ce5e146bb93f3c86a21e"
);
crate::register_standard_child_reconciliation_event_schema(&mut builder).unwrap();
let registry = builder.build().unwrap();
assert!(schema_available(®istry));
for operation in ["child_join_registered", "child_join_published"] {
let mut data = json!({"operation": operation, "request_digest": "a".repeat(64)});
registry
.validate_bounded(&schema, &BoundedJson::try_from_value(data.clone()).unwrap())
.unwrap();
data["child_output"] = json!({"private": true});
assert!(
registry
.validate_bounded(&schema, &BoundedJson::try_from_value(data).unwrap())
.is_err()
);
}
for data in [
json!({"operation":"child_run_settled", "request_digest":"a".repeat(64)}),
json!({"operation":"child_join_published", "request_digest":"not-a-digest"}),
] {
assert!(
registry
.validate_bounded(&schema, &BoundedJson::try_from_value(data).unwrap())
.is_err()
);
}
}
}