Skip to content

Commit edcbf08

Browse files
nathanielcdav1do
authored andcommitted
wip: add concluder todos
1 parent 6b6c414 commit edcbf08

7 files changed

Lines changed: 61 additions & 64 deletions

File tree

event-svc/src/event/service.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ impl EventService {
396396
init,
397397
previous: vec![*time_event.prev()],
398398
order: delivered as u64,
399+
before: todo!(),
400+
chain_id: todo!(),
401+
tx_hash: todo!(),
402+
tx_type: todo!(),
403+
root: todo!(),
399404
}))
400405
}
401406
ceramic_event::unvalidated::Event::Signed(signed_event) => {

pipeline/src/concluder/event.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use anyhow::{anyhow, Result};
44
use arrow::datatypes::{DataType, Field, Int32Type};
5+
use ceramic_core::ssi::caip2;
56
use ceramic_core::METAMODEL_STREAM_ID;
67
use ceramic_event::{unvalidated, StreamId, StreamIdType};
78
use cid::Cid;
@@ -110,7 +111,34 @@ pub struct ConclusionTime {
110111
pub init: ConclusionInit,
111112
/// Ordered list of previous events this event references.
112113
pub previous: Vec<Cid>,
113-
//TODO Add temporal conclusions, i.e the block timestamp of this event
114+
115+
// TODO figure out how to populate these values.
116+
// Once populated we need to update the conclusion_events table schema to include them
117+
// Then we need to preserve them in the event_states table
118+
// Finally in the resolver we can use this information to determine a canonical tip for a
119+
// stream.
120+
//
121+
// Challenges: The anchor proof information is gather when validating the event but then
122+
// forgotten. It likely needs to be persisteted into a new sqlite table and then the conclusion
123+
// feed call needs to join against that table to produce these fields.
124+
//
125+
// How do we populate that table for existing data?
126+
// Likely need some kind of explicit migration command that can be run to populate the table.
127+
//
128+
/// It is known that the time event existed before this time as a Unix timestamp in seconds.
129+
pub before: u64,
130+
131+
// Only before should be needed for conflict resolution but it may be nice to preserve these
132+
// other values as well?
133+
//
134+
/// Chain ID where this time event was anchored.
135+
pub chain_id: String,
136+
/// Transaction hash that anchored this time event.
137+
pub tx_hash: String,
138+
/// Transaction type (TODO: not sure what this is).
139+
pub tx_type: String,
140+
/// Root cid of the proof.
141+
pub root: Cid,
114142
}
115143

116144
impl<'a> TryFrom<&'a unvalidated::Event<Ipld>> for ConclusionInit {

pipeline/src/concluder/metrics.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ceramic_metrics::Recorder;
33

44
use crate::metrics::{MessageLabels, Metrics};
55

6-
use super::{ConcluderRecorder, EventsSinceMsg, NewEventsMsg};
6+
use super::{ConcluderRecorder, NewEventsMsg};
77

88
impl Recorder<MessageEvent<NewEventsMsg>> for Metrics {
99
fn record(&self, event: &MessageEvent<NewEventsMsg>) {
@@ -12,11 +12,4 @@ impl Recorder<MessageEvent<NewEventsMsg>> for Metrics {
1212
.inc();
1313
}
1414
}
15-
impl Recorder<MessageEvent<EventsSinceMsg>> for Metrics {
16-
fn record(&self, event: &MessageEvent<EventsSinceMsg>) {
17-
self.message_count
18-
.get_or_create(&MessageLabels::from(event))
19-
.inc();
20-
}
21-
}
2215
impl ConcluderRecorder for Metrics {}

pipeline/src/concluder/mock.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use prometheus_client::registry::Registry;
77
use crate::metrics::Metrics;
88

99
use super::{
10-
Concluder, ConcluderActor, ConcluderEnvelope, ConcluderHandle, EventsSinceMsg, NewEventsMsg,
11-
SubscribeSinceMsg,
10+
Concluder, ConcluderActor, ConcluderEnvelope, ConcluderHandle, NewEventsMsg, SubscribeSinceMsg,
1211
};
1312

1413
mock! {
@@ -28,11 +27,6 @@ mock! {
2827
&mut self,
2928
message: SubscribeSinceMsg,
3029
) -> <SubscribeSinceMsg as Message>::Result;
31-
#[allow(missing_docs)]
32-
pub fn handle_events_since(
33-
&mut self,
34-
message: EventsSinceMsg,
35-
) -> <EventsSinceMsg as Message>::Result;
3630
}
3731
}
3832

@@ -53,13 +47,6 @@ impl Handler<SubscribeSinceMsg> for MockConcluder {
5347
}
5448
}
5549

56-
#[async_trait]
57-
impl Handler<EventsSinceMsg> for MockConcluder {
58-
async fn handle(&mut self, message: EventsSinceMsg) -> <EventsSinceMsg as Message>::Result {
59-
self.handle_events_since(message)
60-
}
61-
}
62-
6350
impl Actor for MockConcluder {
6451
type Envelope = ConcluderEnvelope;
6552
}

pipeline/src/concluder/mod.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ actor_envelope! {
169169
ConcluderRecorder,
170170
NewEvents => NewEventsMsg,
171171
SubscribeSince => SubscribeSinceMsg,
172-
EventsSince => EventsSinceMsg,
173172
}
174173

175174
/// Notify actor of new events
@@ -329,23 +328,6 @@ async fn events_since(
329328
Ok(conclusion_events.execute_stream().await?)
330329
}
331330

332-
/// Request the events since a highwater mark
333-
#[derive(Debug)]
334-
pub struct EventsSinceMsg {
335-
/// Optional filters to apply to the query
336-
pub filters: Vec<Expr>,
337-
}
338-
impl Message for EventsSinceMsg {
339-
type Result = anyhow::Result<SendableRecordBatchStream>;
340-
}
341-
342-
#[async_trait]
343-
impl Handler<EventsSinceMsg> for Concluder {
344-
async fn handle(&mut self, message: EventsSinceMsg) -> <EventsSinceMsg as Message>::Result {
345-
events_since(&self.ctx, Some(message.filters), None).await
346-
}
347-
}
348-
349331
#[async_trait]
350332
impl FeedTableSource for ConcluderHandle {
351333
fn schema(&self) -> SchemaRef {

pipeline/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ mod cache_table;
1414
pub mod cid_part;
1515
pub mod cid_string;
1616
pub mod concluder;
17-
pub mod resolver;
1817
mod config;
1918
pub mod dimension_extract;
2019
mod metrics;
20+
pub mod resolver;
2121
pub mod schemas;
2222
mod since;
2323
pub mod stream_id_string;

pipeline/src/resolver/mod.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use datafusion::{
3535
functions_aggregate::expr_fn::last_value,
3636
logical_expr::{col, lit, ExprFunctionExt as _},
3737
physical_plan::stream::RecordBatchStreamAdapter,
38-
prelude::{wildcard, SessionContext},
38+
prelude::{wildcard, Expr, SessionContext},
3939
};
4040
use futures::TryStreamExt as _;
4141
use shutdown::{Shutdown, ShutdownSignal};
@@ -47,7 +47,7 @@ use crate::{
4747
cache_table::CacheTable,
4848
metrics::Metrics,
4949
schemas,
50-
since::{rows_since, FeedTable, FeedTableSource},
50+
since::{gt_expression, rows_since, FeedTable, FeedTableSource, RowsSinceInput},
5151
PipelineContext, Result, SessionContextRef,
5252
};
5353
// Use the SubscribeSinceMsg so its clear its a message for this actor
@@ -310,7 +310,7 @@ async fn aggregator_subscription(
310310
let mut rx = aggregator
311311
.send(SubscribeSinceMsg {
312312
projection: None,
313-
offset,
313+
filters: offset.map(|o| vec![gt_expression("event_state_order", o)]),
314314
limit: None,
315315
})
316316
.await??;
@@ -354,40 +354,42 @@ impl Handler<SubscribeSinceMsg> for Resolver {
354354
) -> <SubscribeSinceMsg as Message>::Result {
355355
let subscription = self.broadcast_tx.subscribe();
356356
let ctx = self.ctx.clone();
357-
rows_since(
358-
schemas::stream_states(),
359-
"stream_state_order",
360-
message.projection,
361-
message.offset,
362-
message.limit,
363-
Box::pin(RecordBatchStreamAdapter::new(
357+
rows_since(RowsSinceInput {
358+
session_context: &ctx,
359+
schema: schemas::stream_states(),
360+
order_col: "stream_state_order",
361+
projection: message.projection,
362+
filters: message.filters.clone(),
363+
limit: message.limit,
364+
subscription: Box::pin(RecordBatchStreamAdapter::new(
364365
schemas::stream_states(),
365366
tokio_stream::wrappers::BroadcastStream::new(subscription)
366367
.map_err(|err| exec_datafusion_err!("{err}")),
367368
)),
368369
// Future Optimization can be to send the projection and limit into the events_since call.
369-
stream_states_since(&ctx, message.offset).await?,
370-
)
370+
since: stream_states_since(&ctx, message.filters).await?,
371+
})
371372
}
372373
}
373374

374375
async fn stream_states_since(
375376
ctx: &SessionContext,
376-
offset: Option<u64>,
377+
filters: Option<Vec<Expr>>,
377378
) -> Result<SendableRecordBatchStream> {
378379
let mut stream_states = ctx
379380
.table(STREAM_STATES_TABLE)
380381
.await?
381382
.select(vec![wildcard()])?
382383
// Do not return the partition columns
383-
.drop_columns(&["stream_cid_partition"])?;
384-
if let Some(offset) = offset {
385-
stream_states = stream_states.filter(col("stream_state_order").gt(lit(offset)))?;
384+
.drop_columns(&["stream_cid_partition"])?
385+
.sort(vec![col("stream_state_order").sort(true, true)])?;
386+
387+
if let Some(filters) = filters {
388+
for filter in filters {
389+
stream_states = stream_states.filter(filter)?;
390+
}
386391
}
387-
Ok(stream_states
388-
.sort(vec![col("stream_state_order").sort(true, true)])?
389-
.execute_stream()
390-
.await?)
392+
Ok(stream_states.execute_stream().await?)
391393
}
392394

393395
/// Inform the resolver about new event states.
@@ -581,13 +583,13 @@ impl FeedTableSource for ResolverHandle {
581583
async fn subscribe_since(
582584
&self,
583585
projection: Option<Vec<usize>>,
584-
offset: Option<u64>,
586+
filters: Option<Vec<Expr>>,
585587
limit: Option<usize>,
586588
) -> anyhow::Result<SendableRecordBatchStream> {
587589
Ok(self
588590
.send(SubscribeSinceMsg {
589591
projection,
590-
offset,
592+
filters,
591593
limit,
592594
})
593595
.await??)

0 commit comments

Comments
 (0)