From 8ebc44251254c76b97f9c287ff47444ca1eddc48 Mon Sep 17 00:00:00 2001 From: Studio Date: Wed, 2 Dec 2026 00:52:12 -0500 Subject: [PATCH] =?UTF-8?q?T-154:=20M2.3l=20=E2=80=94=20`libs/data/questdb?= =?UTF-8?q?`:=20pure=20TypeScript=20ILP=20v2=20batch=20writer=20for=20`bar?= =?UTF-8?q?s=5F1m`=20/=20`fx=5F1m`=20(`ilp-v2-writer.ts`,=20binary=20doubl?= =?UTF-8?q?es=20+=20microsecond=20timestamps=20over=20an=20injected=20sink?= =?UTF-8?q?,=20byte-parity=20with=20`tools/curate-etl/src/ilp.rs`;=20no=20?= =?UTF-8?q?new=20deps,=20no=20live=20QuestDB)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/data/questdb/src/ilp-v2-writer.ts | 33 ++++++- libs/data/questdb/test/ilp-v2-writer.test.ts | 91 ++++++++++++++++++-- 2 files changed, 116 insertions(+), 8 deletions(-) diff --git a/libs/data/questdb/src/ilp-v2-writer.ts b/libs/data/questdb/src/ilp-v2-writer.ts index 2d64a7c8..6917f5b7 100644 --- a/libs/data/questdb/src/ilp-v2-writer.ts +++ b/libs/data/questdb/src/ilp-v2-writer.ts @@ -481,19 +481,48 @@ export function createIlpV2BatchWriter(options: IlpV2BatchWriterOptions): IlpV2B } } + function snapshotBar1mRow(row: Bar1mIlpV2Row): Bar1mIlpV2Row { + return { + listing_id: row.listing_id, + ts: row.ts, + known_at: row.known_at, + open: row.open, + high: row.high, + low: row.low, + close: row.close, + volume: row.volume, + session: row.session, + source: row.source, + }; + } + + function snapshotFx1mRow(row: Fx1mIlpV2Row): Fx1mIlpV2Row { + return { + pair: row.pair, + source: row.source, + open: row.open, + high: row.high, + low: row.low, + close: row.close, + ts: row.ts, + }; + } + return { writeBar1m(row: Bar1mIlpV2Row): Promise { + const snapshot = snapshotBar1mRow(row); return enqueue(async () => { assertHealthyForMutation('writeBar1m'); - const encoded = encodeBar1mIlpV2(row); + const encoded = encodeBar1mIlpV2(snapshot); await enqueueEncodedRow(encoded); }); }, writeFx1m(row: Fx1mIlpV2Row): Promise { + const snapshot = snapshotFx1mRow(row); return enqueue(async () => { assertHealthyForMutation('writeFx1m'); - const encoded = encodeFx1mIlpV2(row); + const encoded = encodeFx1mIlpV2(snapshot); await enqueueEncodedRow(encoded); }); }, diff --git a/libs/data/questdb/test/ilp-v2-writer.test.ts b/libs/data/questdb/test/ilp-v2-writer.test.ts index b271a920..c7d4c6ca 100644 --- a/libs/data/questdb/test/ilp-v2-writer.test.ts +++ b/libs/data/questdb/test/ilp-v2-writer.test.ts @@ -506,7 +506,7 @@ describe('createIlpV2BatchWriter batching, boundaries, and option validation', ( expect(batches.every(b => b[b.byteLength - 1] === 0x0a)).toBe(true); } - // 2. Exact byte limit (2 * rowLen): flushes after every 2 rows. + // 2. Exact byte limit (2 * rowLen): flushes immediately when the 2nd and 4th rows hit the limit. { const { sink, batches } = createMemorySink(); const writer = createIlpV2BatchWriter({ @@ -514,14 +514,21 @@ describe('createIlpV2BatchWriter batching, boundaries, and option validation', ( maxBatchBytes: 2 * rowLen, maxBatchRows: 100, }); - for (let i = 0; i < 5; i++) { - await writer.writeBar1m(row); - } + await writer.writeBar1m(row); + expect(batches).toHaveLength(0); + await writer.writeBar1m(row); + expect(batches).toHaveLength(1); + expect(batches[0]).toEqual(concatBytes([oneLine, oneLine])); + await writer.writeBar1m(row); + expect(batches).toHaveLength(1); + await writer.writeBar1m(row); + expect(batches).toHaveLength(2); + expect(batches[1]).toEqual(concatBytes([oneLine, oneLine])); + await writer.writeBar1m(row); + expect(batches).toHaveLength(2); const stats = await writer.finish(); expect(stats).toEqual({ rows: 5, batches: 3, bytes: 5 * rowLen }); expect(batches).toHaveLength(3); - expect(batches[0]).toEqual(concatBytes([oneLine, oneLine])); - expect(batches[1]).toEqual(concatBytes([oneLine, oneLine])); expect(batches[2]).toEqual(oneLine); } @@ -684,6 +691,78 @@ describe('createIlpV2BatchWriter serialization and fail-closed sink errors', () ]); }); + it('snapshots row field values at submission when a caller reuses mutable row objects across overlapping writes', async () => { + const { sink, batches } = createMemorySink(); + const writer = createIlpV2BatchWriter({ sink, maxBatchRows: 1, maxBatchBytes: 100_000 }); + + const mutableBar = sampleBar({ + listing_id: 10, + ts: 1_700_000_000_000_000n, + known_at: 1_700_000_060_000_000n, + open: 100.0, + high: 101.0, + low: 99.5, + close: 100.5, + volume: 1_000, + session: 'PRE', + source: 'bar_one', + }); + const expectedBar1 = encodeBar1mIlpV2(mutableBar); + + const mutableFx = sampleFx({ + pair: 'USD.CAD', + source: 'fx_one', + open: 1.35, + high: 1.36, + low: 1.34, + close: 1.355, + ts: 1_700_000_000_000_000n, + }); + const expectedFx1 = encodeFx1mIlpV2(mutableFx); + + // Submit first bar and FX row without awaiting, then mutate both objects in place. + const pBar1 = writer.writeBar1m(mutableBar); + const pFx1 = writer.writeFx1m(mutableFx); + + mutableBar.listing_id = 11; + mutableBar.ts = 1_700_000_060_000_000n; + mutableBar.known_at = 1_700_000_120_000_000n; + mutableBar.open = 200.0; + mutableBar.high = 202.0; + mutableBar.low = 199.0; + mutableBar.close = 201.5; + mutableBar.volume = 2_000; + mutableBar.session = 'RTH'; + mutableBar.source = 'bar_two'; + const expectedBar2 = encodeBar1mIlpV2(mutableBar); + + mutableFx.pair = 'EUR.USD'; + mutableFx.source = 'fx_two'; + mutableFx.open = 1.08; + mutableFx.high = 1.09; + mutableFx.low = 1.07; + mutableFx.close = 1.085; + mutableFx.ts = 1_700_000_060_000_000n; + const expectedFx2 = encodeFx1mIlpV2(mutableFx); + + const pBar2 = writer.writeBar1m(mutableBar); + const pFx2 = writer.writeFx1m(mutableFx); + + await Promise.all([pBar1, pFx1, pBar2, pFx2]); + const stats = await writer.finish(); + + expect(stats).toEqual({ + rows: 4, + batches: 4, + bytes: + expectedBar1.byteLength + + expectedFx1.byteLength + + expectedBar2.byteLength + + expectedFx2.byteLength, + }); + expect(batches).toEqual([expectedBar1, expectedFx1, expectedBar2, expectedFx2]); + }); + it('propagates sink rejection and fails closed for queued and subsequent calls with zero extra sink writes', async () => { const boom = new Error('simulated sink EPIPE'); let sinkCalls = 0;