dovecote-sqlx-sqlite 0.2.0

SQLite SQLx adapter for Dovecote
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
458
459
use super::test_support::*;

#[tokio::test]
async fn stale_token_is_fenced_and_terminal_state_is_illegal() {
    let pool = database().await;
    let adapter = SqliteDovecote::new(pool.clone());
    let mut transaction = adapter.begin_write().await.unwrap();
    adapter
        .enqueue(&mut transaction, event("one"))
        .await
        .unwrap();
    transaction.commit().await.unwrap();
    let claimed = adapter
        .claim(
            WorkerId::new("worker-a").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();
    let wrong = dovecote::ClaimToken::from_bytes([7; 16]);
    assert_all_mutation_classifications(
        &adapter,
        &pool,
        claimed.row_id(),
        &wrong,
        MutationExpectation::LostClaim,
    )
    .await;
    adapter
        .quarantine(
            claimed.row_id(),
            claimed.claim_token(),
            &QuarantineReason::new("operator decision").unwrap(),
        )
        .await
        .unwrap();
    assert_all_mutation_classifications(
        &adapter,
        &pool,
        claimed.row_id(),
        claimed.claim_token(),
        MutationExpectation::IllegalTransition(dovecote::DeliveryState::Quarantined),
    )
    .await;
}

#[tokio::test]
async fn lifecycle_mutations_persist_their_exact_fields_and_database_times() {
    let pool = database().await;
    let adapter = SqliteDovecote::new(pool.clone());
    let mut transaction = adapter.begin_write().await.unwrap();
    adapter
        .enqueue(&mut transaction, event("fields"))
        .await
        .unwrap();
    transaction.commit().await.unwrap();
    let first = adapter
        .claim(
            WorkerId::new("fields-worker").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();
    let old_expiry = first.claim_expires_at();
    adapter
        .renew(
            first.row_id(),
            first.claim_token(),
            Lease::new(Duration::from_secs(10)).unwrap(),
        )
        .await
        .unwrap();
    let renewed: String = sqlx::query_scalar(
        "SELECT claim_expires_at FROM dovecote_deliveries WHERE event_row_id = ?",
    )
    .bind(first.row_id().get())
    .fetch_one(&pool)
    .await
    .unwrap();
    let renewed =
        time::OffsetDateTime::parse(&renewed, &time::format_description::well_known::Rfc3339)
            .unwrap();
    assert!(renewed > old_expiry);
    assert_eq!(renewed.microsecond() % 1_000, 0);

    let failure = Failure::new("temporary", "retry detail").unwrap();
    adapter
        .retry(
            first.row_id(),
            first.claim_token(),
            &failure,
            Delay::new(Duration::ZERO).unwrap(),
        )
        .await
        .unwrap();
    let retried = adapter.page(None, Limit::new(1).unwrap()).await.unwrap();
    assert!(matches!(
        retried[0].delivery(),
        dovecote::DeliverySnapshot::Pending {
            last_failure: Some(stored), ..
        } if stored == &failure
    ));

    let second = adapter
        .claim(
            WorkerId::new("fields-worker").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();
    adapter
        .release(
            second.row_id(),
            second.claim_token(),
            Delay::new(Duration::ZERO).unwrap(),
        )
        .await
        .unwrap();
    let released = adapter.page(None, Limit::new(1).unwrap()).await.unwrap();
    assert!(matches!(
        released[0].delivery(),
        dovecote::DeliverySnapshot::Pending {
            last_failure: Some(stored), ..
        } if stored == &failure
    ));

    let third = adapter
        .claim(
            WorkerId::new("fields-worker").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();
    adapter
        .quarantine(
            third.row_id(),
            third.claim_token(),
            &QuarantineReason::new("manual quarantine").unwrap(),
        )
        .await
        .unwrap();
    let quarantined = adapter.page(None, Limit::new(1).unwrap()).await.unwrap();
    assert!(matches!(
        quarantined[0].delivery(),
        dovecote::DeliverySnapshot::Quarantined { reason, .. }
            if reason.as_str() == "manual quarantine"
    ));
}

#[tokio::test]
async fn crash_after_claim_commit_reclaims_and_fences_the_expired_token() {
    let pool = database().await;
    let adapter = SqliteDovecote::new(pool.clone());
    let mut transaction = adapter.begin_write().await.unwrap();
    adapter
        .enqueue(&mut transaction, event("reclaim"))
        .await
        .unwrap();
    transaction.commit().await.unwrap();
    let first = adapter
        .claim(
            WorkerId::new("reclaim-a").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();

    // Returning from claim proves that its short BEGIN IMMEDIATE transaction
    // committed. A worker crash now leaves this durable claim for recovery.
    let expired_token = first.claim_token().clone();
    sqlx::query("UPDATE dovecote_deliveries SET claim_expires_at = '1970-01-01T00:00:00.000000Z'")
        .execute(&pool)
        .await
        .unwrap();
    assert_all_mutation_classifications(
        &adapter,
        &pool,
        first.row_id(),
        &expired_token,
        MutationExpectation::LostClaim,
    )
    .await;
    let second = adapter
        .claim(
            WorkerId::new("reclaim-b").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();
    assert_eq!(second.attempts().get(), 2);
    assert_ne!(&expired_token, second.claim_token());
    assert_all_mutation_classifications(
        &adapter,
        &pool,
        second.row_id(),
        &expired_token,
        MutationExpectation::LostClaim,
    )
    .await;
    adapter
        .ack(second.row_id(), second.claim_token())
        .await
        .unwrap();
}

#[tokio::test]
async fn common_occurrence_time_endpoints_round_trip_and_reject_outside_before_sql() {
    let pool = database().await;
    let adapter = SqliteDovecote::new(pool.clone());
    let minimum = time::OffsetDateTime::UNIX_EPOCH;
    let maximum = time::OffsetDateTime::new_in_offset(
        time::Date::from_calendar_date(9999, time::Month::December, 31).unwrap(),
        time::Time::from_hms_micro(23, 59, 59, 999_999).unwrap(),
        time::UtcOffset::UTC,
    );
    let timed_event = |id: &str, at: time::OffsetDateTime| {
        NewEvent::builder(
            StreamName::new("audit").unwrap(),
            EventId::new(id).unwrap(),
            EventSource::new("https://example.test/source").unwrap(),
            EventType::new("com.example.time").unwrap(),
        )
        .time(at)
        .build()
    };

    let mut transaction = adapter.begin_write().await.unwrap();
    adapter
        .enqueue(
            &mut transaction,
            timed_event("time-minimum", minimum).unwrap(),
        )
        .await
        .unwrap();
    let maximum_outcome = adapter
        .enqueue(
            &mut transaction,
            timed_event("time-maximum", maximum).unwrap(),
        )
        .await
        .unwrap();
    transaction.commit().await.unwrap();
    let maximum_id = match maximum_outcome {
        dovecote::EnqueueOutcome::Enqueued { row_id } => row_id,
        dovecote::EnqueueOutcome::AlreadyEnqueued { .. } => unreachable!(),
        _ => unreachable!(),
    };

    let mut replay_transaction = adapter.begin_write().await.unwrap();
    let replay = adapter
        .enqueue(
            &mut replay_transaction,
            timed_event("time-maximum", maximum).unwrap(),
        )
        .await
        .unwrap();
    replay_transaction.commit().await.unwrap();
    assert_eq!(
        replay,
        dovecote::EnqueueOutcome::AlreadyEnqueued { row_id: maximum_id }
    );

    let rows = adapter.page(None, Limit::new(10).unwrap()).await.unwrap();
    assert_eq!(rows.len(), 2);
    assert_eq!(rows[0].event().time(), Some(minimum));
    assert_eq!(rows[1].event().time(), Some(maximum));

    // NewEvent validates the common portable range and precision before an
    // adapter transaction is opened, so neither invalid value can reach SQL.
    assert!(
        timed_event(
            "time-before-minimum",
            minimum - time::Duration::microseconds(1)
        )
        .is_err()
    );
    assert!(
        timed_event(
            "time-after-maximum",
            maximum + time::Duration::nanoseconds(1)
        )
        .is_err()
    );
    let event_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM dovecote_events")
        .fetch_one(&pool)
        .await
        .unwrap();
    assert_eq!(event_count, 2);
}

#[tokio::test]
async fn crash_before_claim_commit_exposes_no_claim() {
    let pool = database().await;
    let adapter = SqliteDovecote::new(pool.clone());
    let mut setup = adapter.begin_write().await.unwrap();
    let outcome = adapter
        .enqueue(&mut setup, event("crash-before-claim"))
        .await
        .unwrap();
    setup.commit().await.unwrap();
    let row_id = match outcome {
        dovecote::EnqueueOutcome::Enqueued { row_id } => row_id,
        dovecote::EnqueueOutcome::AlreadyEnqueued { .. } => unreachable!(),
        _ => unreachable!(),
    };

    // An uncommitted claim is rolled back when the worker process crashes. An
    // explicit rollback gives that crash boundary a deterministic test shape.
    let mut uncommitted = pool
        .begin_with(AssertSqlSafe("BEGIN IMMEDIATE"))
        .await
        .unwrap();
    sqlx::query(
        "UPDATE dovecote_deliveries SET state = 'claimed', attempts = 1, claim_token = ?, claimed_by = ?, claim_expires_at = ? WHERE event_row_id = ?",
    )
    .bind([0x42_u8; 16].as_slice())
    .bind("crashed-before-commit")
    .bind("9999-12-31T23:59:59.999000Z")
    .bind(row_id.get())
    .execute(&mut *uncommitted)
    .await
    .unwrap();
    uncommitted.rollback().await.unwrap();

    let stored: (String, i64, Option<Vec<u8>>, Option<String>, Option<String>) =
        sqlx::query_as(
            "SELECT state, attempts, claim_token, claimed_by, claim_expires_at FROM dovecote_deliveries WHERE event_row_id = ?",
        )
        .bind(row_id.get())
        .fetch_one(&pool)
        .await
        .unwrap();
    assert_eq!(stored, ("pending".to_owned(), 0, None, None, None));

    let recovered = adapter
        .claim(
            WorkerId::new("after-crash").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();
    assert_eq!(recovered.row_id(), row_id);
    assert_eq!(recovered.attempts().get(), 1);
    adapter.ack(row_id, recovered.claim_token()).await.unwrap();
}

#[tokio::test]
async fn transport_success_before_ack_can_produce_a_reclaimed_duplicate() {
    let pool = database().await;
    let adapter = SqliteDovecote::new(pool.clone());
    let mut setup = adapter.begin_write().await.unwrap();
    adapter
        .enqueue(&mut setup, event("transport-success-before-ack"))
        .await
        .unwrap();
    setup.commit().await.unwrap();

    let claimed = adapter
        .claim(
            WorkerId::new("transport-worker").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();
    let original_row_id = claimed.row_id();
    let original_token = claimed.claim_token().clone();
    let original_event_id = claimed.event().id().clone();

    // The fake transport accepts outside the database transaction, then the
    // worker crashes before ack. Transport success is deliberately not durable.
    let mut transport = FakeTransport::default();
    transport.accept(&claimed);
    drop(claimed);
    let stored: (String, Option<String>, Option<Vec<u8>>) = sqlx::query_as(
        "SELECT state, delivered_at, claim_token FROM dovecote_deliveries WHERE event_row_id = ?",
    )
    .bind(original_row_id.get())
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(stored.0, "claimed");
    assert!(stored.1.is_none());
    assert_eq!(stored.2, Some(original_token.as_bytes().to_vec()));

    // Recovery sees the expired claim and returns the same durable event with
    // a fresh token: this is the expected possible duplicate consequence.
    sqlx::query(
        "UPDATE dovecote_deliveries SET claim_expires_at = '1970-01-01T00:00:00.000000Z' WHERE event_row_id = ?",
    )
    .bind(original_row_id.get())
    .execute(&pool)
    .await
    .unwrap();
    let reclaimed = adapter
        .claim(
            WorkerId::new("transport-recovery").unwrap(),
            Lease::new(Duration::from_secs(5)).unwrap(),
            Limit::new(1).unwrap(),
        )
        .await
        .unwrap()
        .pop()
        .unwrap();
    assert_eq!(reclaimed.row_id(), original_row_id);
    assert_eq!(reclaimed.event().id(), &original_event_id);
    assert_eq!(reclaimed.attempts().get(), 2);
    assert_ne!(reclaimed.claim_token(), &original_token);
    transport.accept(&reclaimed);
    assert_eq!(
        transport.accepted,
        vec![
            (
                "https://example.test/source".to_owned(),
                "transport-success-before-ack".to_owned()
            ),
            (
                "https://example.test/source".to_owned(),
                "transport-success-before-ack".to_owned()
            )
        ]
    );
    assert_eq!(transport.accepted[0], transport.accepted[1]);
    let still_claimed: (String, Option<String>) = sqlx::query_as(
        "SELECT state, delivered_at FROM dovecote_deliveries WHERE event_row_id = ?",
    )
    .bind(reclaimed.row_id().get())
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(still_claimed, ("claimed".to_owned(), None));
    adapter
        .ack(reclaimed.row_id(), reclaimed.claim_token())
        .await
        .unwrap();
}