eventsourced-projection 0.6.0

Projections for EventSourced.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use error_ext::StdErrorExt;
use eventsourced::{binarize, event_log::EventLog};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres, Row, Transaction};
use std::{
    error::Error as StdError,
    fmt::Debug,
    num::{NonZeroU64, TryFromIntError},
    pin::pin,
    sync::Arc,
    time::Duration,
};
use thiserror::Error;
use tokio::{
    sync::{mpsc, oneshot, RwLock},
    task,
    time::sleep,
};
use tracing::{debug, error, info};

/// A projection of events of an event sourced entity to a Postgres database.
#[derive(Debug, Clone)]
pub struct Projection {
    name: String,
    command_in: mpsc::Sender<(Command, oneshot::Sender<State>)>,
}

impl Projection {
    pub async fn new<E, L, H>(
        type_name: &'static str,
        name: String,
        event_log: L,
        event_handler: H,
        error_strategy: ErrorStrategy,
        pool: Pool<Postgres>,
    ) -> Result<Self, Error>
    where
        E: for<'de> Deserialize<'de> + Send + 'static,
        L: EventLog + Sync,
        H: EventHandler<E> + Clone + Send + Sync + 'static,
    {
        sqlx::query(include_str!("create_projection.sql"))
            .execute(&pool)
            .await
            .expect("create projection table");

        let seq_no = load_seq_no(&name, &pool).await?;

        let state = Arc::new(RwLock::new(State {
            seq_no,
            running: false,
            error: None,
        }));

        let (command_in, mut command_out) = mpsc::channel::<(Command, oneshot::Sender<State>)>(1);

        task::spawn({
            let name = name.clone();
            let state = state.clone();

            async move {
                while let Some((command, reply_in)) = command_out.recv().await {
                    match command {
                        Command::Run => {
                            // Do not remove braces, dead-lock is waiting for you!
                            let running = { state.read().await.running };
                            if running {
                                info!(type_name, name, "projection already running");
                            } else {
                                info!(type_name, name, "running projection");

                                // Do not remove braces, dead-lock is waiting for you!
                                {
                                    let mut state = state.write().await;
                                    state.running = true;
                                    state.error = None;
                                }

                                run_projection_loop(
                                    type_name,
                                    name.clone(),
                                    state.clone(),
                                    event_log.clone(),
                                    event_handler.clone(),
                                    pool.clone(),
                                    error_strategy,
                                )
                                .await;
                            }

                            if reply_in.send(state.read().await.clone()).is_err() {
                                error!(type_name, name, "cannot send state");
                            }
                        }

                        Command::Stop => {
                            // Do not remove braces, dead-lock is waiting for you!
                            let running = { state.read().await.running };
                            if running {
                                info!(type_name, name, "stopping projection");
                                let mut state = state.write().await;
                                state.running = false;
                            } else {
                                info!(type_name, name, "projection already stopped");
                            }

                            if reply_in.send(state.read().await.clone()).is_err() {
                                error!(type_name, name, "cannot send state");
                            }
                        }

                        Command::GetState => {
                            if reply_in.send(state.read().await.clone()).is_err() {
                                error!(type_name, name, "cannot send state");
                            }
                        }
                    }
                }
            }
        });

        Ok(Projection { name, command_in })
    }

    pub async fn run(&self) -> Result<State, CommandError> {
        self.dispatch_command(Command::Run).await
    }

    pub async fn stop(&self) -> Result<State, CommandError> {
        self.dispatch_command(Command::Stop).await
    }

    pub async fn get_state(&self) -> Result<State, CommandError> {
        self.dispatch_command(Command::GetState).await
    }

    async fn dispatch_command(&self, command: Command) -> Result<State, CommandError> {
        let (reply_in, reply_out) = oneshot::channel();
        self.command_in
            .send((command, reply_in))
            .await
            .map_err(|_| CommandError::SendCommand(command, self.name.clone()))?;
        let state = reply_out
            .await
            .map_err(|_| CommandError::ReceiveResponse(command, self.name.clone()))?;
        Ok(state)
    }
}

#[trait_variant::make(Send)]
pub trait EventHandler<E> {
    type Error: StdError + Send + Sync + 'static;

    async fn handle_event(
        &self,
        event: E,
        tx: &mut Transaction<'static, Postgres>,
    ) -> Result<(), Self::Error>;
}

#[derive(Debug, Error)]
pub enum Error {
    #[error("cannot create Projection, b/c cannot load state from database")]
    Sqlx(#[from] sqlx::Error),

    #[error("cannot create Projection, b/c cannot convert loaded seq_no into non zero value")]
    TryFromInt(#[from] TryFromIntError),
}

#[derive(Debug, Error, Serialize, Deserialize)]
pub enum CommandError {
    /// The command cannot be sent from this [Projection] to its projection.
    #[error("cannot send command {0:?} to projection {1}")]
    SendCommand(Command, String),

    /// A response for the command cannot be received from this [Projection]'s projection.
    #[error("cannot receive reply for command {0:?} from projection {1}")]
    ReceiveResponse(Command, String),
}

#[derive(Debug, Clone, Copy)]
pub enum ErrorStrategy {
    Retry(Duration),
    Stop,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct State {
    pub seq_no: Option<NonZeroU64>,
    pub running: bool,
    pub error: Option<String>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum Command {
    Run,
    Stop,
    GetState,
}

#[derive(Debug, Error)]
enum IntenalRunError<E, H> {
    #[error(transparent)]
    Events(E),

    #[error(transparent)]
    Handler(H),

    #[error(transparent)]
    Sqlx(#[from] sqlx::Error),

    #[error(transparent)]
    LoadStateError(#[from] Error),
}

async fn load_seq_no(name: &str, pool: &Pool<Postgres>) -> Result<Option<NonZeroU64>, Error> {
    let seq_no = sqlx::query("SELECT seq_no FROM projection WHERE name=$1")
        .bind(name)
        .fetch_optional(pool)
        .await?
        .map(|row| row.try_get::<i64, _>(0))
        .transpose()?
        .map(|seq_no| (seq_no as u64).try_into())
        .transpose()?;
    Ok(seq_no)
}

async fn run_projection_loop<E, L, H>(
    type_name: &'static str,
    name: String,
    state: Arc<RwLock<State>>,
    event_log: L,
    event_handler: H,
    pool: Pool<Postgres>,
    error_strategy: ErrorStrategy,
) where
    E: for<'de> Deserialize<'de> + Send + 'static,
    L: EventLog + Sync,
    H: EventHandler<E> + Sync + 'static,
{
    task::spawn({
        async move {
            loop {
                let result =
                    run_projection(type_name, &name, &event_log, &event_handler, &pool, &state)
                        .await;
                match result {
                    Ok(_) => {
                        info!(type_name, name, "projection stopped");
                        {
                            let mut state = state.write().await;
                            state.running = false;
                        }
                        break;
                    }

                    Err(error) => {
                        error!(
                            error = error.as_chain(),
                            type_name, name, "projection error"
                        );

                        match error_strategy {
                            ErrorStrategy::Retry(delay) => {
                                info!(type_name, name, ?delay, "projection retrying after error");
                                {
                                    let mut state = state.write().await;
                                    state.error = Some(error.to_string());
                                }
                                sleep(delay).await
                            }

                            ErrorStrategy::Stop => {
                                info!(type_name, name, "projection stopped after error");
                                {
                                    let mut state = state.write().await;
                                    state.running = false;
                                    state.error = Some(error.to_string());
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    });
}

async fn run_projection<E, L, H>(
    type_name: &'static str,
    name: &str,
    event_log: &L,
    handler: &H,
    pool: &Pool<Postgres>,
    state: &Arc<RwLock<State>>,
) -> Result<(), IntenalRunError<L::Error, H::Error>>
where
    E: for<'de> Deserialize<'de> + Send + 'static,
    L: EventLog,
    H: EventHandler<E>,
{
    let seq_no = load_seq_no(name, pool)
        .await?
        .map(|n| n.saturating_add(1))
        .unwrap_or(NonZeroU64::MIN);
    let events = event_log
        .events_by_type::<E, _, _>(type_name, seq_no, binarize::serde_json::from_bytes)
        .await
        .map_err(IntenalRunError::Events)?;
    let mut events = pin!(events);

    while let Some(event) = events.next().await {
        if !state.read().await.running {
            break;
        };

        let (seq_no, event) = event.map_err(IntenalRunError::Events)?;

        let mut tx = pool.begin().await?;
        handler
            .handle_event(event, &mut tx)
            .await
            .map_err(IntenalRunError::Handler)?;
        debug!(type_name, name, seq_no, "projection handled event");
        save_seq_no(seq_no, name, &mut tx).await?;
        tx.commit().await?;

        state.write().await.seq_no = Some(seq_no);
    }

    Ok(())
}

async fn save_seq_no(
    seq_no: NonZeroU64,
    name: &str,
    tx: &mut Transaction<'_, Postgres>,
) -> Result<(), sqlx::Error> {
    let query = r#"INSERT INTO projection (name, seq_no)
                   VALUES ($1, $2)
                   ON CONFLICT (name) DO UPDATE SET seq_no = $2"#;
    sqlx::query(query)
        .bind(name)
        .bind(seq_no.get() as i64)
        .execute(&mut **tx)
        .await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use crate::postgres::{ErrorStrategy, EventHandler, Projection};
    use error_ext::BoxError;
    use eventsourced::{
        binarize::serde_json::to_bytes,
        event_log::{test::TestEventLog, EventLog},
    };
    use sqlx::{
        postgres::{PgConnectOptions, PgPoolOptions},
        Postgres, QueryBuilder, Row, Transaction,
    };
    use std::{iter::once, time::Duration};
    use testcontainers::{clients::Cli, RunnableImage};
    use testcontainers_modules::postgres::Postgres as TCPostgres;
    use tokio::time::sleep;

    #[derive(Clone)]
    struct TestHandler;

    impl EventHandler<i32> for TestHandler {
        type Error = sqlx::Error;

        async fn handle_event(
            &self,
            event: i32,
            tx: &mut Transaction<'static, Postgres>,
        ) -> Result<(), Self::Error> {
            QueryBuilder::new("INSERT INTO test (n) ")
                .push_values(once(event), |mut q, event| {
                    q.push_bind(event);
                })
                .build()
                .execute(&mut **tx)
                .await?;
            Ok(())
        }
    }

    #[tokio::test]
    async fn test() -> Result<(), BoxError> {
        let containers = Cli::default();

        let container =
            containers.run(RunnableImage::from(TCPostgres::default()).with_tag("16-alpine"));
        let port = container.get_host_port_ipv4(5432);

        let cnn_url = format!("postgresql://postgres:postgres@localhost:{port}");
        let cnn_options = cnn_url.parse::<PgConnectOptions>()?;
        let pool = PgPoolOptions::new().connect_with(cnn_options).await?;

        let mut event_log = TestEventLog::<u64>::default();
        for n in 1..=100 {
            event_log.persist("test", &0, None, &n, &to_bytes).await?;
        }

        sqlx::query("CREATE TABLE test (n bigint);")
            .execute(&pool)
            .await?;

        let projection = Projection::new(
            "test",
            "test-projection".to_string(),
            event_log.clone(),
            TestHandler,
            ErrorStrategy::Stop,
            pool.clone(),
        )
        .await?;

        QueryBuilder::new("INSERT INTO projection ")
            .push_values(once(("test-projection", 10)), |mut q, (name, seq_no)| {
                q.push_bind(name).push_bind(seq_no);
            })
            .build()
            .execute(&pool)
            .await?;

        projection.run().await?;

        let mut state = projection.get_state().await?;
        let max = Some(100.try_into()?);
        while state.seq_no < max {
            sleep(Duration::from_millis(100)).await;
            state = projection.get_state().await?;
        }
        assert_eq!(state.seq_no, max);

        let sum = sqlx::query("SELECT * FROM test;")
            .fetch_all(&pool)
            .await?
            .into_iter()
            .map(|row| row.try_get::<i64, _>(0))
            .try_fold(0i64, |acc, n| n.map(|n| acc + n))?;
        assert_eq!(sum, 4_995); // sum(1..100) - sum(1..10)

        projection.stop().await?;
        sleep(Duration::from_millis(100)).await;
        let state = projection.get_state().await?;
        sleep(Duration::from_millis(100)).await;
        let state_2 = projection.get_state().await?;
        assert_eq!(state.seq_no, state_2.seq_no);

        Ok(())
    }
}