Skip to content

Commit 974b9fd

Browse files
authored
refactor(auth): delete ordered deliveries and the event consumer cursor, which nothing used (#491)
Auth carried two pieces of machinery nothing in production used, but that were maintained, tested and documented anyway. Ordered deliveries were one: no channel ever set an ordering key, yet `delivery_orderings` grew, claims checked ordinals, and cleanup abandoned stuck keys. The event consumer cursor was the other: `event_consumers`, `read_after` and `acknowledge` had callers only in tests. Both are gone, about 540 lines of code and tests. Every delivery now retries and cancels the same way, as every real delivery already did. An operator may retry any failed or cancelled delivery, and cleanup deletes either after 30 days. The CLI's `ORDER` column, the dashboard's "ordered" line and the wording about deliveries "ordered after" one are gone. `EventCursor` stays, as the audit chain's place for an event, and `in_safe_order!` stays as the sealer's reading rule. **Schema.** Migration 0044 drops `event_consumers`, which no release reads or writes. `deliveries.ordering_key`, `deliveries.ordinal` and `delivery_orderings` stay under a `PENDING CONTRACT`. The release before this one still names them when it inserts, claims and lists deliveries, and the [expand-then-contract rule](https://github.com/sourceorigin-dev/sourceorigin/blob/main/docs/dev/architecture/database.md) keeps it working during a deploy. A contract migration drops them once this release is in production. The back-channel bug #252 names shipped on its own in #490. #83's alerts will add the smallest cursor they need when they read the stream. Closes #252. **Proof:** `just db test` passes on a scratch cluster: 468 tests, including the inventory, runtime-role and provisioning tests that list every table. `operators_retry_and_cancel_deliveries` replaces the three ordering tests and keeps what they covered without ordering: retry, cancel, a discarded outcome after cancelling mid-attempt, and a retry waiting for that attempt's lease. Clippy, `just db prepare-check`, `just docs reference` and `just docs` pass. Reviewed with review-loop: 1 round with Claude Opus 5.5 over #491 and #492; all GPT 6.1 Sol reviewers failed on a Codex usage limit. Its one finding is fixed: the CleanupFailing alert no longer says stuck deliveries hold others back. So are its nits on the cleanup metric's description and the migration's stale note; renaming the migration was declined, since it also records the deliveries' pending contract. > [!NOTE] > Written by Claude Opus 5.5 in Claude Code (via T3 Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 5337eb6 commit 974b9fd

49 files changed

Lines changed: 258 additions & 932 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/auth-service/src/admin/service/events.rs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ impl AdminService {
155155
.await?)
156156
}
157157

158-
/// Attempts a failed delivery, or a cancelled unordered one, again now, for
159-
/// `reason`.
158+
/// Attempts a failed or cancelled delivery again now, for `reason`.
160159
pub(crate) async fn retry_delivery(
161160
&self,
162161
operator: &Operator,
@@ -167,13 +166,12 @@ impl AdminService {
167166
tx.deliveries()
168167
.retry(id)
169168
.await
170-
.map_err(AdminError::delivery(id, "failed or cancelled unordered"))?;
169+
.map_err(AdminError::delivery(id, "failed or cancelled"))?;
171170
let retried = DomainEvent::DeliveryRetried { delivery_id: id };
172171
Self::finish(tx, operator, &reason, None, vec![retried]).await
173172
}
174173

175-
/// Cancels a pending or failed delivery, for `reason`, which unblocks the
176-
/// deliveries ordered after it.
174+
/// Cancels a pending or failed delivery, for `reason`.
177175
pub(crate) async fn cancel_delivery(
178176
&self,
179177
operator: &Operator,

‎apps/auth-service/src/admin/tests/deliveries.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ async fn delivery_changes_check_and_record_their_administrator() {
4545
destination: "https://receiver.example".into(),
4646
recipient: (),
4747
payload: (),
48-
ordering_key: None,
4948
};
5049
harness
5150
.database

‎apps/auth-service/src/cli/deliveries.rs‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ pub(super) enum DeliveriesCommand {
2424
#[arg(long, default_value_t = 100)]
2525
limit: u32,
2626
},
27-
/// Attempt a failed delivery, or a cancelled unordered one, again now.
27+
/// Attempt a failed or cancelled delivery again now.
2828
Retry {
2929
/// The delivery's ID.
3030
id: DeliveryId,
3131
/// Why, recorded with the change: 1 to 500 characters.
3232
#[arg(long)]
3333
reason: Reason,
3434
},
35-
/// Cancel a pending or failed delivery, which unblocks the deliveries ordered after it.
35+
/// Cancel a pending or failed delivery.
3636
Cancel {
3737
/// The delivery's ID.
3838
id: DeliveryId,
@@ -82,7 +82,6 @@ report!([DeliverySummary], self, out, {
8282
"CHANNEL",
8383
"STATUS",
8484
"ATTEMPTS",
85-
"ORDER",
8685
"DUE OR FINISHED",
8786
"LAST ERROR",
8887
"DESTINATION",
@@ -94,11 +93,6 @@ report!([DeliverySummary], self, out, {
9493
Cell::new(&delivery.channel),
9594
delivery.status.into(),
9695
delivery.attempts.into(),
97-
delivery
98-
.ordering
99-
.as_ref()
100-
.map(|(key, ordinal)| Cell::new(format_args!("{key}#{ordinal}")))
101-
.into(),
10296
delivery
10397
.finished_at
10498
.unwrap_or(delivery.next_attempt_at)

‎apps/auth-service/src/deliveries/tests.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ async fn queue<C: Channel<Recipient = Behavior, Payload = ()>>(
376376
destination: "https://receiver.example".into(),
377377
recipient: behavior,
378378
payload: (),
379-
ordering_key: None,
380379
};
381380
database
382381
.deliveries()

‎apps/auth-service/src/jobs/mod.rs‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl Jobs {
256256
static CLEANUP_CHANGED: Counter = Counter::new(
257257
"sourceorigin.auth.cleanup.changed",
258258
"{row}",
259-
"Rows cleanup deleted or changed: expired state, personal data past retention and stuck deliveries.",
259+
"Rows cleanup deleted or changed: expired state, personal data past retention and finished deliveries.",
260260
);
261261
/// How long each cleanup pass took.
262262
static CLEANUP_DURATION: Histogram = Histogram::new(
@@ -273,9 +273,8 @@ static CLEANUP_OVERRUNS: Counter = Counter::new(
273273
);
274274

275275
/// Drains expired authentication state, event personal data and finished
276-
/// deliveries past their retention, and abandons delivery ordering keys stuck
277-
/// behind an old failure, in bounded batches until a pass finds nothing or a
278-
/// one-minute budget runs out.
276+
/// deliveries past their retention, in bounded batches until a pass finds nothing
277+
/// or a one-minute budget runs out.
279278
/// Short pauses between batches keep cleanup from monopolizing the database under
280279
/// heavy churn.
281280
struct Cleanup {
@@ -394,17 +393,12 @@ impl Cleanup {
394393
Ok(())
395394
}
396395

397-
/// Runs one batch and returns how many rows it changed. Abandoning stuck keys
398-
/// needs a transaction of its own, so its locks last until it decides.
396+
/// Runs one batch and returns how many rows it deleted.
399397
async fn batch(connection: &mut ExclusiveConnection) -> Result<u64, DatabaseError> {
400-
let mut tx = connection.begin().await?;
401-
let abandoned = tx.abandon_stuck_deliveries(Self::BATCH_SIZE).await?;
402-
tx.commit().await?;
403-
let purged = connection
398+
connection
404399
.maintenance()
405400
.purge_expired_auth_state(Self::BATCH_SIZE)
406-
.await?;
407-
Ok(abandoned + purged)
401+
.await
408402
}
409403
}
410404

‎apps/auth-service/src/web/admin/deliveries.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,13 @@ pub(super) async fn list(cx: &Cx) -> Result<Response> {
7979
App::render(cx, StatusCode::OK, "Deliveries", view).await
8080
}
8181

82-
/// `/deliveries/{delivery}/retry`: attempts a failed delivery, or a cancelled
83-
/// unordered one, again now.
82+
/// `/deliveries/{delivery}/retry`: attempts a failed or cancelled delivery again now.
8483
#[route([GET, POST] "/deliveries/{delivery}/retry")]
8584
pub(super) async fn retry(cx: &Cx, form: Option<Form<ReasonForm>>) -> Result<Response> {
8685
App::delivery_change(cx, form, Change::RetryDelivery).await
8786
}
8887

89-
/// `/deliveries/{delivery}/cancel`: cancels a pending or failed delivery, which
90-
/// unblocks the deliveries ordered after it.
88+
/// `/deliveries/{delivery}/cancel`: cancels a pending or failed delivery.
9189
#[route([GET, POST] "/deliveries/{delivery}/cancel")]
9290
pub(super) async fn cancel(cx: &Cx, form: Option<Form<ReasonForm>>) -> Result<Response> {
9391
App::delivery_change(cx, form, Change::CancelDelivery).await

‎apps/auth-service/src/web/admin/tests.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,6 @@ async fn deliveries_are_cancelled_and_retried_from_the_dashboard() {
17151715
destination: "https://receiver.example".into(),
17161716
recipient: (),
17171717
payload: (),
1718-
ordering_key: None,
17191718
};
17201719
let id = harness
17211720
.database

‎crates/common/src/admin.rs‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,6 @@ pub struct DeliverySummary {
942942
pub status: DeliveryStatus,
943943
/// Attempts so far.
944944
pub attempts: u64,
945-
/// Its ordering key and its place in that order, if it is ordered.
946-
pub ordering: Option<(String, i64)>,
947945
/// When the next attempt is due, while pending.
948946
pub next_attempt_at: DateTime<Utc>,
949947
/// The latest attempt's error.
@@ -955,15 +953,13 @@ pub struct DeliverySummary {
955953
}
956954

957955
impl DeliverySummary {
958-
/// Whether an operator may attempt it again: a failed delivery, or a
959-
/// cancelled one no other delivery is ordered after.
956+
/// Whether an operator may attempt it again: once it failed or was cancelled.
960957
#[must_use]
961958
pub const fn retryable(&self) -> bool {
962-
match self.status {
963-
DeliveryStatus::Failed => true,
964-
DeliveryStatus::Cancelled => self.ordering.is_none(),
965-
DeliveryStatus::Pending | DeliveryStatus::Succeeded => false,
966-
}
959+
matches!(
960+
self.status,
961+
DeliveryStatus::Failed | DeliveryStatus::Cancelled
962+
)
967963
}
968964

969965
/// Whether an operator may cancel it: while pending or failed.

‎crates/common/src/alerts.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ impl Alert {
157157
.to_owned(),
158158
Self::CleanupFailing {} => "Cleanup passes keep failing, so expired sign-in state, \
159159
personal data past its retention and finished deliveries stay in the \
160-
database, and stuck deliveries hold back the ones behind them. The logs of \
161-
the cleanup job say why. It clears once a pass works."
160+
database. The logs of the cleanup job say why. It clears once a pass works."
162161
.to_owned(),
163162
}
164163
}

‎crates/common/src/events/mod.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ pub enum DomainEvent {
360360
/// The recovery.
361361
recovery_id: RecoveryId,
362362
},
363-
/// An operator made a failed delivery, or a cancelled unordered one, due again.
363+
/// An operator made a failed or cancelled delivery due again.
364364
DeliveryRetried {
365365
/// The delivery.
366366
delivery_id: DeliveryId,
367367
},
368368
/// An operator cancelled a pending or failed delivery, such as a notice to a
369-
/// user, which unblocks the deliveries ordered after it.
369+
/// user.
370370
DeliveryCancelled {
371371
/// The delivery.
372372
delivery_id: DeliveryId,

0 commit comments

Comments
 (0)