@nodii/outbox-dispatcher 0.15.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -0
- package/dist/generated/package-version.d.ts +1 -1
- package/dist/generated/package-version.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/prune.d.ts +286 -0
- package/dist/prune.d.ts.map +1 -0
- package/dist/prune.js +753 -0
- package/dist/prune.js.map +1 -0
- package/dist/sql-builders.d.ts +44 -0
- package/dist/sql-builders.d.ts.map +1 -1
- package/dist/sql-builders.js +81 -0
- package/dist/sql-builders.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -344,6 +344,136 @@ await sweepUnpublishedOutbox({ pool: pgPool, dispatcher, telemetry });
|
|
|
344
344
|
This is distinct from the subscriber-side `replication_dlq` (`09 § 8.1`), which
|
|
345
345
|
handles consume-side poison applies and is unaffected.
|
|
346
346
|
|
|
347
|
+
## Retention: prune published rows, bound streams by age (RETENTION-FIX-4, LIBS-72)
|
|
348
|
+
|
|
349
|
+
Outbox payloads can hold personal data, and nothing else ever deletes a
|
|
350
|
+
published row. Wire the prune job **once** in your worker process and run its
|
|
351
|
+
handler daily:
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
import { makeOutboxPruneJob } from "@nodii/outbox-dispatcher";
|
|
355
|
+
|
|
356
|
+
const dispatcher = defineDispatcher({ serviceName: "tenant", streamFor /* , orderPerAggregate */ });
|
|
357
|
+
worker.schedule(makeOutboxPruneJob({ dispatcher, pool: ownerSql, redis }));
|
|
358
|
+
// { name: "tenant.outbox-prune", intervalMs: 86_400_000, pool: "owner", handler }
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Passing the `dispatcher` gives the job its `serviceName`, `outboxTable` and
|
|
362
|
+
`orderPerAggregate`; each can also be given on its own.
|
|
363
|
+
Python: `make_outbox_prune_job(dispatcher=dispatcher, pool=owner_pool, redis=redis)`.
|
|
364
|
+
Go: `outbox.MakeOutboxPruneJob(outbox.OutboxPruneJobConfig{Dispatcher: &d, Pool: ownerPool, Redis: redisClient})`.
|
|
365
|
+
|
|
366
|
+
Each run:
|
|
367
|
+
|
|
368
|
+
- **Deletes published rows** whose `dispatched_at` is older than `retentionMs`
|
|
369
|
+
(default **30 days**, operator ruling of 20 Nov 2026), `batchSize` (1,000)
|
|
370
|
+
rows per `DELETE`, until a `DELETE` removes nothing (a short batch is not
|
|
371
|
+
the end: `SKIP LOCKED` may have passed over rows a drain holds), at most
|
|
372
|
+
`maxBatchesPerRun` (1,000) statements.
|
|
373
|
+
- **With `orderPerAggregate`, keeps each aggregate's latest published row.** The
|
|
374
|
+
ordered claim reads the aggregate's previous row to detect a version gap; if
|
|
375
|
+
the prune removed the aggregate's whole history, a v7 committing before v6
|
|
376
|
+
would publish at once. So a row is deleted only while a newer published row
|
|
377
|
+
of its aggregate exists, and the kept latest row, once past the retention,
|
|
378
|
+
is scrubbed (`payload = '{}'`, trace, telemetry and error columns NULL):
|
|
379
|
+
its version survives, its personal data does not
|
|
380
|
+
(`outbox.prune.scrubbed_rows`). Without `orderPerAggregate` every aged
|
|
381
|
+
published row goes, so one-shot aggregates do not pile up. As a fail-safe, a
|
|
382
|
+
table that carries the aggregate-order index (`idx_aggregate_order_<table>`,
|
|
383
|
+
from `buildOrderPerAggregateIndexSql`) is treated as ordered even when the
|
|
384
|
+
job was built from `serviceName` rather than the `dispatcher`.
|
|
385
|
+
- **Never deletes an unpublished row** (`dispatched_at IS NULL`), however old
|
|
386
|
+
and however often its publish failed. D401 keeps it as the source of truth;
|
|
387
|
+
once repaired it publishes, and it is deleted 30 days after that.
|
|
388
|
+
- **Trims the streams you list**, and only those, to `XTRIM <stream> MINID ~
|
|
389
|
+
<now - 14 days>` (per-stream `retentionMs`, default **14 days**). Trimming
|
|
390
|
+
is **opt-in per stream**: a new consumer group, or one whose cursor points
|
|
391
|
+
at a trimmed entry, starts mid-stream, so list a stream only when every
|
|
392
|
+
consumer group on it can hydrate from a snapshot on cold start (or starts
|
|
393
|
+
at `$` by design). Streams whose consumers replay from `0` with no snapshot
|
|
394
|
+
stay unlisted. The trim is approximate (`~`): Redis removes whole macro
|
|
395
|
+
nodes only, so a few entries a little older than the bound can remain until
|
|
396
|
+
a later run.
|
|
397
|
+
- **Never trims past an entry a consumer group still needs**: the trim point is
|
|
398
|
+
held at each group's lowest pending id, else its last-delivered id. A group
|
|
399
|
+
that stopped reading holds its stream back; the job logs the group and sets
|
|
400
|
+
`outbox.stream_trim.held_back{service,stream}` to 1. Pass
|
|
401
|
+
`respectConsumerGroups: false` only for a stream whose groups are disposable.
|
|
402
|
+
|
|
403
|
+
```ts
|
|
404
|
+
makeOutboxPruneJob({
|
|
405
|
+
serviceName: "tenant",
|
|
406
|
+
pool: ownerSql,
|
|
407
|
+
redis,
|
|
408
|
+
streams: [
|
|
409
|
+
"tenant.custom_domain", // 14 days
|
|
410
|
+
{ stream: "tenant.role", retentionMs: 30 * 86_400_000 },
|
|
411
|
+
],
|
|
412
|
+
});
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
One replica runs at a time, under the fenced lease
|
|
416
|
+
`nodii:lease:<service>:outbox-prune` (`LIB-EVT-20`). The fence is checked before
|
|
417
|
+
every `DELETE` batch and every `XTRIM`; a run whose lease is taken stops and
|
|
418
|
+
returns `leaseLost: true`. A replica that finds the lease held returns
|
|
419
|
+
`ran: false`. A failed run (a lease-backend outage at acquire, a database
|
|
420
|
+
error) is counted as `outcome=error` with the rows it already deleted, then
|
|
421
|
+
thrown, so nothing is deleted blind. A run is `complete` only when the DELETE
|
|
422
|
+
loop ended on an empty batch and every listed stream's trim finished within
|
|
423
|
+
its call budget. The `DELETE` bypasses tenant RLS, so pass the **owner** pool.
|
|
424
|
+
If row level security still applies to that role on the outbox (the table has
|
|
425
|
+
`FORCE ROW LEVEL SECURITY`, or the role is not the owner) and no permissive
|
|
426
|
+
`USING (true)` policy opens the table to it, the `DELETE` would see no rows;
|
|
427
|
+
the run fails with `OutboxPruneRowSecurityError` (Go
|
|
428
|
+
`ErrOutboxPruneRowSecurity`) instead of reporting itself complete. Run it as
|
|
429
|
+
the owner on a table without `FORCE`, as a `BYPASSRLS` role, or with a policy
|
|
430
|
+
such as db-rls's `FOR ALL TO nodii_owner USING (true)`. The check asks for
|
|
431
|
+
`USING (true)` on `SELECT`, `UPDATE` and `DELETE` on every run, ordered or not,
|
|
432
|
+
so separate `FOR SELECT` and `FOR DELETE` policies with no `FOR UPDATE` one are
|
|
433
|
+
refused.
|
|
434
|
+
|
|
435
|
+
**The event-id dedupe window becomes the outbox retention.**
|
|
436
|
+
`writeOutboxEventOnce` skips a write whose `event_id` conflicts with a row
|
|
437
|
+
still in the table. Once the prune deletes the published row (30 days after
|
|
438
|
+
publish by default), a retried command with the same `event_id` inserts again
|
|
439
|
+
and is published again. A caller that needs a longer dedupe window must keep
|
|
440
|
+
its own idempotency record (for example `@nodii/idempotency`) or raise
|
|
441
|
+
`retentionMs` to cover it. The same goes for consumers: a consumer that dedupes
|
|
442
|
+
on outbox event ids (`consumed_events`) should keep those records at least as
|
|
443
|
+
long as the outbox retention, or a re-published event is processed again.
|
|
444
|
+
|
|
445
|
+
The job writes no audit event (`AUDT-49`). It reports through logs and metrics:
|
|
446
|
+
`outbox.prune.runs{service,outcome}` (`complete` / `partial` / `lease_lost` /
|
|
447
|
+
`skipped` / `error`), `outbox.prune.deleted_rows{service,table}`,
|
|
448
|
+
`outbox.prune.scrubbed_rows{service,table}`,
|
|
449
|
+
`outbox.stream_trim.entries{service,stream}`,
|
|
450
|
+
`outbox.stream_trim.held_back{service,stream}` and
|
|
451
|
+
`outbox.stream_trim.errors{service,stream}`.
|
|
452
|
+
|
|
453
|
+
The library owns only your outbox table. `saga_outbox` belongs to
|
|
454
|
+
`@nodii/saga` and `replication_dlq` to `@nodii/replica-consumer`; neither is
|
|
455
|
+
pruned here. For a large outbox, add the prune's index once, outside a transaction
|
|
456
|
+
(it is built `CONCURRENTLY`, so producers' INSERTs are never blocked); the
|
|
457
|
+
prune works without it, scanning the table per batch, and the job logs a
|
|
458
|
+
warning naming the index when it is missing:
|
|
459
|
+
|
|
460
|
+
```ts
|
|
461
|
+
await sql.unsafe(buildPruneIndexSql("outbox"));
|
|
462
|
+
// CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_prune_outbox" ON "outbox" (dispatched_at) WHERE dispatched_at IS NOT NULL
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
Python `build_prune_index_sql`, Go `BuildPruneIndexSQL`. It is not in the
|
|
466
|
+
table migration, because a plain `CREATE INDEX` there would lock an existing
|
|
467
|
+
outbox against writes while it builds. If a concurrent build fails (cancelled, lock
|
|
468
|
+
timeout), Postgres leaves an INVALID `idx_prune_<table>` that `IF NOT EXISTS`
|
|
469
|
+
then skips: check `SELECT indisvalid FROM pg_index WHERE indexrelid =
|
|
470
|
+
'idx_prune_outbox'::regclass`, and if it is false run `DROP INDEX CONCURRENTLY
|
|
471
|
+
IF EXISTS "idx_prune_outbox"` and build it again.
|
|
472
|
+
|
|
473
|
+
A stream whose trim fails (a non-stream key under the name, a Redis error) is
|
|
474
|
+
returned with `error`, counted in `outbox.stream_trim.errors{service,stream}`,
|
|
475
|
+
and does not stop the next stream or the run's report.
|
|
476
|
+
|
|
347
477
|
## Metrics (§ 5.6)
|
|
348
478
|
|
|
349
479
|
`outbox.depth{topic}` · `outbox.lag_seconds{topic}` · `outbox.dispatch_rate{topic}`
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const PACKAGE_VERSION = "0.
|
|
1
|
+
export declare const PACKAGE_VERSION = "0.17.0";
|
|
2
2
|
//# sourceMappingURL=package-version.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,8 @@ export { buildEnvelope, DEFAULT_STREAM_MAXLEN, serializeEnvelope, xaddEnvelope,
|
|
|
7
7
|
export { compareStreamIds, HwmTracker } from "./hwm.js";
|
|
8
8
|
export { type LeaseHandle, type LeaseOpts, tryAcquireLease } from "./lease.js";
|
|
9
9
|
export { applyOutboxDispatcherMigrations, getOutboxDispatcherMigrationSQL, } from "./migrations/index.js";
|
|
10
|
-
export { buildClaimBatchSql, type ClaimSqlOptions, DEFAULT_AGGREGATE_GAP_WAIT_MS, MAX_AGGREGATE_GAP_WAIT_MS, MAX_ORDERED_TABLE_NAME_LENGTH, buildDepthByTopicSql, buildInsertOutboxSql, buildMarkDispatchedSql, buildOrderPerAggregateIndexSql, buildPendingTenantIndexSql, buildRecordFailureSql, buildTenantColumnCheckSql, buildUnpublishedSweepSql, quoteIdent, } from "./sql-builders.js";
|
|
10
|
+
export { buildClaimBatchSql, type ClaimSqlOptions, DEFAULT_AGGREGATE_GAP_WAIT_MS, MAX_AGGREGATE_GAP_WAIT_MS, MAX_ORDERED_TABLE_NAME_LENGTH, buildDepthByTopicSql, buildInsertOutboxSql, buildMarkDispatchedSql, buildOrderPerAggregateIndexSql, buildPendingTenantIndexSql, buildPruneIndexSql, buildPrunePublishedSql, buildScrubPublishedSql, type PruneSqlOptions, MAX_PRUNE_INDEX_TABLE_NAME_LENGTH, buildRecordFailureSql, buildTenantColumnCheckSql, buildUnpublishedSweepSql, quoteIdent, } from "./sql-builders.js";
|
|
11
|
+
export { consumerGroupFloor, DEFAULT_OUTBOX_PRUNE_BATCH_SIZE, DEFAULT_OUTBOX_PRUNE_INTERVAL_MS, DEFAULT_OUTBOX_PRUNE_LEASE_TTL_MS, DEFAULT_OUTBOX_PRUNE_MAX_BATCHES, DEFAULT_OUTBOX_RETENTION_MS, DEFAULT_STREAM_RETENTION_MS, DEFAULT_STREAM_TRIM_MAX_CALLS, makeOutboxPruneJob, MAX_OUTBOX_PRUNE_BATCH_SIZE, OUTBOX_PRUNE_LEASE_SCOPE, type OutboxPruneJob, type OutboxPruneJobOpts, type OutboxPruneResult, OutboxPruneRowSecurityError, outboxPruneJobName, prunePublishedOutbox, type StreamRetention, type StreamTrimRedisLike, type StreamTrimResult, trimStreamByAge, } from "./prune.js";
|
|
11
12
|
export { type DispatcherHandle, startDispatcher } from "./start.js";
|
|
12
13
|
export { DEFAULT_UNPUBLISHED_SWEEP_LIMIT, DEFAULT_UNPUBLISHED_SWEEP_THRESHOLD_SECONDS, sweepUnpublishedOutbox, type UnpublishedRow, } from "./sweep.js";
|
|
13
14
|
export { DispatcherNotWired, InvalidAggregateKind, InvalidDispatcherConfig, InvalidOutboxEventId, InvalidOutboxTelemetryEnvelope, InvalidOutboxTenant, OutboxDispatcherError, OutboxTenantMismatch, UnregisteredAggregateKind, } from "./types.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAyBA,eAAO,MAAM,QAAQ,sBAAsB,CAAC;AAC5C,eAAO,MAAM,OAAO,EAAE,MAAwB,CAAC;AAE/C,OAAO,EACL,KAAK,qBAAqB,EAC1B,oBAAoB,GACrB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,8BAA8B,EAC9B,oBAAoB,EACpB,eAAe,EACf,4BAA4B,EAC5B,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,4BAA4B,EAC5B,KAAK,sBAAsB,EAC3B,WAAW,EACX,qBAAqB,GACtB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,iBAAiB,EACjB,YAAY,GACb,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,EACL,+BAA+B,EAC/B,+BAA+B,GAChC,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,KAAK,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EACL,+BAA+B,EAC/B,2CAA2C,EAC3C,sBAAsB,EACtB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,uBAAuB,EACvB,oBAAoB,EACpB,8BAA8B,EAC9B,mBAAmB,EACnB,qBAAqB,EACrB,oBAAoB,EACpB,yBAAyB,GAC1B,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,SAAS,EACT,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,WAAW,EACX,eAAe,EACf,aAAa,EACb,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACtB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -30,7 +30,8 @@ export { buildEnvelope, DEFAULT_STREAM_MAXLEN, serializeEnvelope, xaddEnvelope,
|
|
|
30
30
|
export { compareStreamIds, HwmTracker } from "./hwm.js";
|
|
31
31
|
export { tryAcquireLease } from "./lease.js";
|
|
32
32
|
export { applyOutboxDispatcherMigrations, getOutboxDispatcherMigrationSQL, } from "./migrations/index.js";
|
|
33
|
-
export { buildClaimBatchSql, DEFAULT_AGGREGATE_GAP_WAIT_MS, MAX_AGGREGATE_GAP_WAIT_MS, MAX_ORDERED_TABLE_NAME_LENGTH, buildDepthByTopicSql, buildInsertOutboxSql, buildMarkDispatchedSql, buildOrderPerAggregateIndexSql, buildPendingTenantIndexSql, buildRecordFailureSql, buildTenantColumnCheckSql, buildUnpublishedSweepSql, quoteIdent, } from "./sql-builders.js";
|
|
33
|
+
export { buildClaimBatchSql, DEFAULT_AGGREGATE_GAP_WAIT_MS, MAX_AGGREGATE_GAP_WAIT_MS, MAX_ORDERED_TABLE_NAME_LENGTH, buildDepthByTopicSql, buildInsertOutboxSql, buildMarkDispatchedSql, buildOrderPerAggregateIndexSql, buildPendingTenantIndexSql, buildPruneIndexSql, buildPrunePublishedSql, buildScrubPublishedSql, MAX_PRUNE_INDEX_TABLE_NAME_LENGTH, buildRecordFailureSql, buildTenantColumnCheckSql, buildUnpublishedSweepSql, quoteIdent, } from "./sql-builders.js";
|
|
34
|
+
export { consumerGroupFloor, DEFAULT_OUTBOX_PRUNE_BATCH_SIZE, DEFAULT_OUTBOX_PRUNE_INTERVAL_MS, DEFAULT_OUTBOX_PRUNE_LEASE_TTL_MS, DEFAULT_OUTBOX_PRUNE_MAX_BATCHES, DEFAULT_OUTBOX_RETENTION_MS, DEFAULT_STREAM_RETENTION_MS, DEFAULT_STREAM_TRIM_MAX_CALLS, makeOutboxPruneJob, MAX_OUTBOX_PRUNE_BATCH_SIZE, OUTBOX_PRUNE_LEASE_SCOPE, OutboxPruneRowSecurityError, outboxPruneJobName, prunePublishedOutbox, trimStreamByAge, } from "./prune.js";
|
|
34
35
|
export { startDispatcher } from "./start.js";
|
|
35
36
|
export { DEFAULT_UNPUBLISHED_SWEEP_LIMIT, DEFAULT_UNPUBLISHED_SWEEP_THRESHOLD_SECONDS, sweepUnpublishedOutbox, } from "./sweep.js";
|
|
36
37
|
export { DispatcherNotWired, InvalidAggregateKind, InvalidDispatcherConfig, InvalidOutboxEventId, InvalidOutboxTelemetryEnvelope, InvalidOutboxTenant, OutboxDispatcherError, OutboxTenantMismatch, UnregisteredAggregateKind, } from "./types.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,gFAAgF;AAChF,4DAA4D;AAC5D,kEAAkE;AAClE,E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LeaseRedisLike } from "@nodii/telemetry/worker";
|
|
2
|
+
import { type ResolvedDispatcher, type SqlExecutor, type TelemetryAdapter } from "./types.js";
|
|
3
|
+
/** Published outbox rows are kept 30 days after publish (operator ruling 3). */
|
|
4
|
+
export declare const DEFAULT_OUTBOX_RETENTION_MS: number;
|
|
5
|
+
/** A listed stream keeps 14 days of entries (LIBS-72, operator 10 Nov). */
|
|
6
|
+
export declare const DEFAULT_STREAM_RETENTION_MS: number;
|
|
7
|
+
/** The job runs daily. */
|
|
8
|
+
export declare const DEFAULT_OUTBOX_PRUNE_INTERVAL_MS: number;
|
|
9
|
+
/** Rows deleted per DELETE statement. */
|
|
10
|
+
export declare const DEFAULT_OUTBOX_PRUNE_BATCH_SIZE = 1000;
|
|
11
|
+
/** Upper bound on DELETE statements per run (the orderPerAggregate scrub gets as many again). */
|
|
12
|
+
export declare const DEFAULT_OUTBOX_PRUNE_MAX_BATCHES = 1000;
|
|
13
|
+
/** Largest accepted batch size. */
|
|
14
|
+
export declare const MAX_OUTBOX_PRUNE_BATCH_SIZE = 10000;
|
|
15
|
+
/** XTRIM calls per stream per run, at most. */
|
|
16
|
+
export declare const DEFAULT_STREAM_TRIM_MAX_CALLS = 1000;
|
|
17
|
+
/** Lease scope: `nodii:lease:<service>:outbox-prune`. */
|
|
18
|
+
export declare const OUTBOX_PRUNE_LEASE_SCOPE = "outbox-prune";
|
|
19
|
+
/** Lease TTL; it renews at half this for as long as the run lasts. */
|
|
20
|
+
export declare const DEFAULT_OUTBOX_PRUNE_LEASE_TTL_MS = 30000;
|
|
21
|
+
/** The job's name for a service: `<service>.outbox-prune`. */
|
|
22
|
+
export declare function outboxPruneJobName(serviceName: string): string;
|
|
23
|
+
/** The Redis calls the stream trim makes. ioredis has them, but its overloaded
|
|
24
|
+
* commands need narrowing to these signatures (cast each method, as the
|
|
25
|
+
* synthetic consumer's `asPruneRedis` does). */
|
|
26
|
+
export interface StreamTrimRedisLike {
|
|
27
|
+
xtrim(key: string, ...args: (string | number)[]): Promise<unknown>;
|
|
28
|
+
xinfo(...args: (string | number)[]): Promise<unknown>;
|
|
29
|
+
xpending(key: string, group: string): Promise<unknown>;
|
|
30
|
+
}
|
|
31
|
+
/** One stream the job trims, with its own retention (default 14 days). */
|
|
32
|
+
export interface StreamRetention {
|
|
33
|
+
stream: string;
|
|
34
|
+
/** Entries older than this are trimmed. Default `DEFAULT_STREAM_RETENTION_MS`. */
|
|
35
|
+
retentionMs?: number;
|
|
36
|
+
}
|
|
37
|
+
export interface OutboxPruneJobOpts {
|
|
38
|
+
/**
|
|
39
|
+
* The dispatcher this job prunes for. Supplies `serviceName`, `outboxTable`
|
|
40
|
+
* and `orderPerAggregate` unless they are given here. Prefer it: it keeps
|
|
41
|
+
* the prune's aggregate-order handling in step with the dispatcher's.
|
|
42
|
+
*/
|
|
43
|
+
dispatcher?: Pick<ResolvedDispatcher, "serviceName" | "outboxTable" | "orderPerAggregate">;
|
|
44
|
+
/** The producing service: the lease key's service segment and a metric label. */
|
|
45
|
+
serviceName?: string;
|
|
46
|
+
/**
|
|
47
|
+
* An OWNER-pool executor (the DELETE bypasses tenant RLS). postgres.js
|
|
48
|
+
* `sql` fits.
|
|
49
|
+
*/
|
|
50
|
+
pool: SqlExecutor;
|
|
51
|
+
/**
|
|
52
|
+
* Redis for the lease, and for the trim when `streams` is non-empty (then
|
|
53
|
+
* it must also have `xtrim`, `xinfo` and `xpending`). ioredis has them;
|
|
54
|
+
* narrow its overloaded commands to these signatures.
|
|
55
|
+
*/
|
|
56
|
+
redis: LeaseRedisLike & Partial<StreamTrimRedisLike>;
|
|
57
|
+
/** Outbox table. Default `outbox`. */
|
|
58
|
+
outboxTable?: string;
|
|
59
|
+
/**
|
|
60
|
+
* The dispatcher publishes each aggregate in version order. Keeps each
|
|
61
|
+
* aggregate's highest-version published row (scrubbed past the retention)
|
|
62
|
+
* for its gap check. Default: the dispatcher's value, else false. A
|
|
63
|
+
* dispatcher's `true` is never overridden by a `false` here. A job for an
|
|
64
|
+
* ordered dispatcher MUST be built from `dispatcher` or set this to true:
|
|
65
|
+
* the aggregate-order index the prune also looks for is optional, so
|
|
66
|
+
* without either the prune can delete an aggregate's whole published
|
|
67
|
+
* history and break the dispatcher's gap check.
|
|
68
|
+
*/
|
|
69
|
+
orderPerAggregate?: boolean;
|
|
70
|
+
/** Published rows older than this are deleted. Default 30 days. */
|
|
71
|
+
retentionMs?: number;
|
|
72
|
+
/** Rows per DELETE. Default 1000, at most 10000. */
|
|
73
|
+
batchSize?: number;
|
|
74
|
+
/** DELETE statements per run (the orderPerAggregate scrub gets as many again). Default 1000. */
|
|
75
|
+
maxBatchesPerRun?: number;
|
|
76
|
+
/**
|
|
77
|
+
* Streams to bound by age. Default none: a stream is trimmed only when
|
|
78
|
+
* listed here. A bare string uses the 14-day default.
|
|
79
|
+
*/
|
|
80
|
+
streams?: ReadonlyArray<string | StreamRetention>;
|
|
81
|
+
/**
|
|
82
|
+
* Hold each stream's trim point at the oldest entry a consumer group still
|
|
83
|
+
* needs. Default true. Turn off only for a stream whose groups are known
|
|
84
|
+
* to be disposable.
|
|
85
|
+
*/
|
|
86
|
+
respectConsumerGroups?: boolean;
|
|
87
|
+
/** Job interval. Default 24 hours. */
|
|
88
|
+
intervalMs?: number;
|
|
89
|
+
/** Lease TTL. Default 30000, at least 2000. */
|
|
90
|
+
leaseTtlMs?: number;
|
|
91
|
+
/** Logs and metrics. */
|
|
92
|
+
telemetry?: TelemetryAdapter;
|
|
93
|
+
}
|
|
94
|
+
/** What one stream's trim did. */
|
|
95
|
+
export interface StreamTrimResult {
|
|
96
|
+
stream: string;
|
|
97
|
+
retentionMs: number;
|
|
98
|
+
/** The last `MINID` passed to XTRIM, or null when the stream does not exist. */
|
|
99
|
+
minId: string | null;
|
|
100
|
+
/** Entries XTRIM removed. */
|
|
101
|
+
trimmed: number;
|
|
102
|
+
/** The group that held the last trim point below the age cutoff, if any. */
|
|
103
|
+
heldBackBy: string | null;
|
|
104
|
+
/** False when the trim stopped at `maxCalls` with entries still to remove,
|
|
105
|
+
* or failed. */
|
|
106
|
+
complete: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Set only by the job when this stream's trim failed (Redis error, a
|
|
109
|
+
* non-stream key under the name, a group removed mid-trim). The job logs
|
|
110
|
+
* it, counts `outbox.stream_trim.errors` and goes on to the next stream.
|
|
111
|
+
*/
|
|
112
|
+
error?: string;
|
|
113
|
+
}
|
|
114
|
+
/** What one run did. */
|
|
115
|
+
export interface OutboxPruneResult {
|
|
116
|
+
/** False when another replica held the lease: this run did nothing. */
|
|
117
|
+
ran: boolean;
|
|
118
|
+
deletedRows: number;
|
|
119
|
+
/** Kept latest-per-aggregate rows whose payload was scrubbed
|
|
120
|
+
* (`orderPerAggregate` only). */
|
|
121
|
+
scrubbedRows: number;
|
|
122
|
+
/** DELETE and scrub statements run. */
|
|
123
|
+
batches: number;
|
|
124
|
+
/** False when the run stopped at `maxBatchesPerRun`, a stream trim was
|
|
125
|
+
* incomplete or failed, or the lease was lost. */
|
|
126
|
+
complete: boolean;
|
|
127
|
+
/** True when the lease was lost (or could not be checked) mid-run. */
|
|
128
|
+
leaseLost: boolean;
|
|
129
|
+
streams: StreamTrimResult[];
|
|
130
|
+
ranAt: string;
|
|
131
|
+
}
|
|
132
|
+
/** The framework-agnostic job a worker schedules: `{ name, intervalMs, pool, handler }`. */
|
|
133
|
+
export interface OutboxPruneJob {
|
|
134
|
+
name: string;
|
|
135
|
+
intervalMs: number;
|
|
136
|
+
/** The DELETE runs on the owner pool. */
|
|
137
|
+
pool: "owner";
|
|
138
|
+
handler: () => Promise<OutboxPruneResult>;
|
|
139
|
+
}
|
|
140
|
+
/** Resolved prune-batch options: the one validator both entry points share. */
|
|
141
|
+
export interface ResolvedPruneBatchOpts {
|
|
142
|
+
outboxTable: string;
|
|
143
|
+
retentionMs: number;
|
|
144
|
+
batchSize: number;
|
|
145
|
+
maxBatches: number;
|
|
146
|
+
orderPerAggregate: boolean;
|
|
147
|
+
}
|
|
148
|
+
/** Resolved stream-trim options. */
|
|
149
|
+
export interface ResolvedTrimOpts {
|
|
150
|
+
retentionMs: number;
|
|
151
|
+
maxCalls: number;
|
|
152
|
+
respectConsumerGroups: boolean;
|
|
153
|
+
approximate: boolean;
|
|
154
|
+
}
|
|
155
|
+
export interface ResolvedPruneOpts {
|
|
156
|
+
serviceName: string;
|
|
157
|
+
batch: ResolvedPruneBatchOpts;
|
|
158
|
+
streams: Array<Required<StreamRetention>>;
|
|
159
|
+
respectConsumerGroups: boolean;
|
|
160
|
+
intervalMs: number;
|
|
161
|
+
leaseTtlMs: number;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Validate and default the prune-batch options. A retention or batch size of
|
|
165
|
+
* 0 or less is refused: a retention of 0 would delete rows published a moment
|
|
166
|
+
* ago, and a batch size of 0 would loop without deleting.
|
|
167
|
+
*/
|
|
168
|
+
export declare function resolvePruneBatchOpts(opts: {
|
|
169
|
+
outboxTable?: string;
|
|
170
|
+
retentionMs?: number;
|
|
171
|
+
batchSize?: number;
|
|
172
|
+
maxBatches?: number;
|
|
173
|
+
orderPerAggregate?: boolean;
|
|
174
|
+
}): ResolvedPruneBatchOpts;
|
|
175
|
+
/** Validate and default the stream-trim options (retention and maxCalls of
|
|
176
|
+
* 0 or less are refused). */
|
|
177
|
+
export declare function resolveTrimOpts(opts: {
|
|
178
|
+
retentionMs?: number;
|
|
179
|
+
maxCalls?: number;
|
|
180
|
+
respectConsumerGroups?: boolean;
|
|
181
|
+
approximate?: boolean;
|
|
182
|
+
}): ResolvedTrimOpts;
|
|
183
|
+
/** Validate and default the job options. Throws `InvalidDispatcherConfig`. */
|
|
184
|
+
export declare function resolveOutboxPruneOpts(opts: Omit<OutboxPruneJobOpts, "pool" | "redis" | "telemetry">): ResolvedPruneOpts;
|
|
185
|
+
/**
|
|
186
|
+
* Thrown when row level security applies to the prune's role on the outbox
|
|
187
|
+
* table (FORCE ROW LEVEL SECURITY, or a role that is not the owner) and no
|
|
188
|
+
* permissive `USING (true)` policy lets the role see every row: the DELETE
|
|
189
|
+
* would see no rows, and the run would report complete having pruned nothing.
|
|
190
|
+
* Run the prune as the table owner on a table without FORCE ROW LEVEL
|
|
191
|
+
* SECURITY, as a role with BYPASSRLS, or give the role such a policy.
|
|
192
|
+
*/
|
|
193
|
+
export declare class OutboxPruneRowSecurityError extends Error {
|
|
194
|
+
constructor(outboxTable: string, role: string);
|
|
195
|
+
}
|
|
196
|
+
/** Hooks a caller passes to the batch loops. */
|
|
197
|
+
interface BatchHooks {
|
|
198
|
+
/** Runs before each statement (the job passes the fence check). */
|
|
199
|
+
beforeBatch?: () => Promise<void>;
|
|
200
|
+
/** Runs after each statement with the rows it touched, so a caller keeps
|
|
201
|
+
* the count when a later `beforeBatch` throws. */
|
|
202
|
+
onBatch?: (rows: number, kind: "deleted" | "scrubbed") => void;
|
|
203
|
+
/** Runs once when the prune runs ordered on a table without the full
|
|
204
|
+
* aggregate-order index, with the statement that creates it. */
|
|
205
|
+
onMissingOrderIndex?: (createIndexSql: string) => void;
|
|
206
|
+
/** Runs once when the unordered prune runs on a table without
|
|
207
|
+
* `idx_prune_<table>`, with the statement that creates it. */
|
|
208
|
+
onMissingPruneIndex?: (createIndexSql: string) => void;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Does row level security apply to this role on the table (`active`), and if
|
|
212
|
+
* so, does a permissive policy with `USING (true)` let the role select,
|
|
213
|
+
* update and delete every row (`open`) with no restrictive policy narrowing
|
|
214
|
+
* it (`unrestricted`)? db-rls's owner policy (`FOR ALL TO nodii_owner USING
|
|
215
|
+
* (true)`) is such a policy. Byte-equal with Python and Go.
|
|
216
|
+
*/
|
|
217
|
+
export declare const PRUNE_ROW_SECURITY_SQL: string;
|
|
218
|
+
/**
|
|
219
|
+
* Delete published rows older than `retentionMs`, `batchSize` at a time, up to
|
|
220
|
+
* `maxBatches` statements, until a statement deletes nothing. Unpublished rows
|
|
221
|
+
* are never selected. With `orderPerAggregate`, each aggregate's
|
|
222
|
+
* highest-version published row is kept, and scrubbed once past the retention.
|
|
223
|
+
* Throws `OutboxPruneRowSecurityError` when row level security would hide rows
|
|
224
|
+
* from the pool's role.
|
|
225
|
+
*/
|
|
226
|
+
export declare function prunePublishedOutbox(args: {
|
|
227
|
+
pool: SqlExecutor;
|
|
228
|
+
outboxTable?: string;
|
|
229
|
+
retentionMs?: number;
|
|
230
|
+
batchSize?: number;
|
|
231
|
+
maxBatches?: number;
|
|
232
|
+
orderPerAggregate?: boolean;
|
|
233
|
+
} & BatchHooks): Promise<{
|
|
234
|
+
deletedRows: number;
|
|
235
|
+
scrubbedRows: number;
|
|
236
|
+
batches: number;
|
|
237
|
+
complete: boolean;
|
|
238
|
+
}>;
|
|
239
|
+
/**
|
|
240
|
+
* The oldest entry any consumer group of `stream` still needs: per group its
|
|
241
|
+
* lowest pending id, else its last-delivered id; the minimum over groups. A
|
|
242
|
+
* group with nothing pending that has read up to the stream's newest id
|
|
243
|
+
* (`last-generated-id`) needs nothing and does not count. Returns undefined
|
|
244
|
+
* when the stream does not exist, null when no group needs anything.
|
|
245
|
+
* The groups' XPENDING summaries are read concurrently.
|
|
246
|
+
*/
|
|
247
|
+
export declare function consumerGroupFloor(redis: StreamTrimRedisLike, stream: string): Promise<{
|
|
248
|
+
floor: string;
|
|
249
|
+
group: string;
|
|
250
|
+
} | null | undefined>;
|
|
251
|
+
/**
|
|
252
|
+
* Trim `stream` to the entries newer than `retentionMs` with
|
|
253
|
+
* `XTRIM MINID ~`, never past an entry a consumer group still needs (unless
|
|
254
|
+
* `respectConsumerGroups` is false). `beforeTrim` runs right before each XTRIM.
|
|
255
|
+
* `complete` is false when `maxCalls` ran out with entries still to remove.
|
|
256
|
+
*/
|
|
257
|
+
export declare function trimStreamByAge(args: {
|
|
258
|
+
redis: StreamTrimRedisLike;
|
|
259
|
+
stream: string;
|
|
260
|
+
retentionMs?: number;
|
|
261
|
+
respectConsumerGroups?: boolean;
|
|
262
|
+
/** `~` (default true): Redis trims whole nodes only, cheaply. */
|
|
263
|
+
approximate?: boolean;
|
|
264
|
+
nowMs?: number;
|
|
265
|
+
/** XTRIM calls at most. Default `DEFAULT_STREAM_TRIM_MAX_CALLS`. */
|
|
266
|
+
maxCalls?: number;
|
|
267
|
+
beforeTrim?: () => Promise<void>;
|
|
268
|
+
}): Promise<StreamTrimResult>;
|
|
269
|
+
/**
|
|
270
|
+
* Build the outbox retention job. A service wires it with one call in its
|
|
271
|
+
* worker process and runs `handler()` every `intervalMs` (daily):
|
|
272
|
+
*
|
|
273
|
+
* ```ts
|
|
274
|
+
* worker.schedule(makeOutboxPruneJob({ dispatcher, pool: ownerSql, redis }));
|
|
275
|
+
* ```
|
|
276
|
+
*
|
|
277
|
+
* Each run, under the lease `nodii:lease:<service>:outbox-prune`, deletes the
|
|
278
|
+
* published rows older than 30 days, then trims each listed stream to 14 days.
|
|
279
|
+
* A run that finds the lease held by another replica returns `ran: false`. A
|
|
280
|
+
* failed run (a lease-backend outage at acquire, a database error, row level
|
|
281
|
+
* security hiding rows from the prune) records `outcome=error` and throws:
|
|
282
|
+
* nothing is deleted blind.
|
|
283
|
+
*/
|
|
284
|
+
export declare function makeOutboxPruneJob(opts: OutboxPruneJobOpts): OutboxPruneJob;
|
|
285
|
+
export {};
|
|
286
|
+
//# sourceMappingURL=prune.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prune.d.ts","sourceRoot":"","sources":["../src/prune.ts"],"names":[],"mappings":"AA0FA,OAAO,EAGL,KAAK,cAAc,EAEpB,MAAM,yBAAyB,CAAC;AAYjC,OAAO,EAEL,KAAK,kBAAkB,EACvB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,YAAY,CAAC;AAIpB,gFAAgF;AAChF,eAAO,MAAM,2BAA2B,QAAc,CAAC;AACvD,2EAA2E;AAC3E,eAAO,MAAM,2BAA2B,QAAc,CAAC;AACvD,0BAA0B;AAC1B,eAAO,MAA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