Skip to content

Commit 31fb262

Browse files
committed
fix(metrics-pipeline): widen order_key packing factor to 1e6
Packs the stream sequence with a 1e6 factor (was 1e5) so up to 1M entries per millisecond per shard fit before a seq could spill into the next millisecond's range, far above what a single Redis stream can produce. ms*1e6 stays within UInt64. Also fixes the webapp mapping test that still expected a numeric order_key after the switch to a BigInt-derived string.
1 parent 45e978d commit 31fb262

4 files changed

Lines changed: 10 additions & 9 deletions

File tree

apps/webapp/test/queueMetricsMapping.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe("mapEntryToRow", () => {
112112
op: "started",
113113
wait_ms: 48,
114114
cumulative: 512,
115-
order_key: 1700000000000 * 100000,
115+
order_key: (1700000000000n * 1000000n).toString(),
116116
})
117117
);
118118
expect(mapEntryToRow({ id: "1-0", fields: { op: "ack", q, cum: "9" } })).toEqual(

internal-packages/clickhouse/schema/035_create_queue_metrics_v1.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CREATE TABLE IF NOT EXISTS trigger_dev.queue_metrics_raw_v1
1313
environment_id String CODEC(ZSTD(1)),
1414
queue_name String CODEC(ZSTD(1)),
1515
event_time DateTime CODEC(Delta(4), ZSTD(1)),
16-
order_key UInt64 DEFAULT 0, -- stream-id composite (ms*1e5+seq), deltaSumTimestamp ordering key
16+
order_key UInt64 DEFAULT 0, -- stream-id composite (ms*1e6+seq), deltaSumTimestamp ordering key
1717
op LowCardinality(String), -- gauge | enqueue | started | ack | nack | dlq
1818
running UInt32 DEFAULT 0,
1919
queued UInt32 DEFAULT 0,

internal-packages/metrics-pipeline/src/pipeline.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ describe("stream keys", () => {
3535
});
3636

3737
it("entryOrderKey stays exact and strictly monotonic at real epoch magnitudes", () => {
38-
const ms = 1783000000000; // ~2026: ms*1e5 is past JS safe-integer range, so a number key
38+
const ms = 1783000000000; // ~2026: ms*1e6 is past JS safe-integer range, so a number key
3939
const k = (seq: number) => BigInt(entryOrderKey(`${ms}-${seq}`));
4040
// adjacent seq within one ms must not collapse to the same key (the float bug)
41-
expect(k(0)).toBe(BigInt(ms) * 100000n);
41+
expect(k(0)).toBe(BigInt(ms) * 1000000n);
4242
expect(k(1) - k(0)).toBe(1n);
4343
expect(k(2) - k(1)).toBe(1n);
44-
// a later ms always outranks any seq of an earlier ms
45-
expect(BigInt(entryOrderKey(`${ms + 1}-0`))).toBeGreaterThan(k(99999));
44+
// a later ms always outranks any seq of an earlier ms (up to the 1M/ms factor)
45+
expect(BigInt(entryOrderKey(`${ms + 1}-0`))).toBeGreaterThan(k(999999));
4646
});
4747
});
4848

internal-packages/metrics-pipeline/src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ export function entryTimeMs(id: string): number | null {
3232
return Number.isFinite(ms) ? ms : null;
3333
}
3434

35-
// Ordering key from a stream id (`<ms>-<seq>`) = ms*1e5+seq, for deltaSumTimestamp. BigInt +
36-
// string because ms*1e5 exceeds JS safe-integer range at real epoch magnitudes (a number would
35+
// Ordering key from a stream id (`<ms>-<seq>`) = ms*1e6+seq, for deltaSumTimestamp. BigInt +
36+
// string because ms*1e6 exceeds JS safe-integer range at real epoch magnitudes (a number would
3737
// collapse nearby seq values); the ClickHouse order_key column is UInt64 and takes the string.
38+
// The 1e6 factor (1M entries/ms/shard, far above any single Redis stream) stays within UInt64.
3839
export function entryOrderKey(id: string): string {
3940
const [ms, seq] = id.split("-");
40-
return (BigInt(Number(ms) || 0) * 100000n + BigInt(Number(seq) || 0)).toString();
41+
return (BigInt(Number(ms) || 0) * 1000000n + BigInt(Number(seq) || 0)).toString();
4142
}

0 commit comments

Comments
 (0)