T-116: M1.2af — data-pipeline: read-only security-master → curate-etl input exporter (provider_mapping.tsv, listing_currencies.tsv, Pass 6 delistings.tsv from PostgreSQL) with tests and local benchmark

This commit is contained in:
Studio 2026-11-29 05:36:19 -05:00
parent 88b7f1f020
commit 7c55c97c44
8 changed files with 576 additions and 26 deletions

View file

@ -1,5 +1,5 @@
import { afterEach, describe, expect, test } from 'bun:test';
import { mkdtemp, readdir, readFile, rename, rm, unlink, writeFile } from 'node:fs/promises';
import { mkdtemp, open, readdir, readFile, rename, rm, unlink, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import {
@ -11,6 +11,7 @@ import {
assertExportCurateInputsSafety,
buildDelistingExportRows,
CURATE_INPUT_FILES,
CURATE_INPUT_PUBLISH_LOCK,
CurateInputExportError,
DEFAULT_EXPORT_CHUNK_SIZE,
EXPORT_CURATE_INPUTS_USAGE,
@ -1068,6 +1069,186 @@ describe('exportCurateInputs atomic publish and streaming', () => {
expect(isAllA || isAllB).toBe(true);
});
test('concurrent fresh-dir exports serialize publish/rollback so a failed export never deletes a committed export top-level links', async () => {
const snapA: InMemorySecurityMasterData = {
listings: [
{ listing_id: 10, currency: 'USD', listed_to: '2023-06-15', delist_reason: 'unknown_involuntary', terminal_return: '-0.3' },
],
continuities: [],
providerMappings: [
{ source: 'qm', source_id: 'SNAP_A', valid_from: '2020-01-01', valid_to: '2023-06-15', listing_id: 10 },
],
};
const snapB: InMemorySecurityMasterData = {
listings: [
{ listing_id: 20, currency: 'CAD', listed_to: '2024-01-15', delist_reason: 'bankruptcy_cancelled', terminal_return: '-1' },
],
continuities: [],
providerMappings: [
{ source: 'qm', source_id: 'SNAP_B', valid_from: '2021-01-01', valid_to: '2024-01-15', listing_id: 20 },
],
};
// Case 1: Export A enters publish first on a fresh dir, creates top-level links, and fails at
// commit rename while Export B is waiting for `.publish.lock`. A rolls back cleanly under the
// lock; B then acquires the lock and commits with all top-level and pinned paths readable.
{
const outDir = await makeTempDir('curate-inputs-race-a-fails-');
let aEnteredPublishResolve!: () => void;
const aEnteredPublish = new Promise<void>(resolve => {
aEnteredPublishResolve = resolve;
});
let bWaitingForLockResolve!: () => void;
const bWaitingForLock = new Promise<void>(resolve => {
bWaitingForLockResolve = resolve;
});
const promiseA = exportCurateInputs(createInMemoryQueryable(snapA), {
outDir,
fs: {
rename: async (oldPath, newPath) => {
if (String(newPath) === join(outDir, 'current')) {
aEnteredPublishResolve();
await bWaitingForLock;
throw new Error('Simulated commit EIO in Export A');
}
return rename(oldPath, newPath);
},
},
});
const promiseB = (async () => {
await aEnteredPublish;
let signaledWait = false;
return exportCurateInputs(createInMemoryQueryable(snapB), {
outDir,
fs: {
open: async (filePath, flags) => {
if (String(filePath) === join(outDir, CURATE_INPUT_PUBLISH_LOCK) && !signaledWait) {
signaledWait = true;
bWaitingForLockResolve();
}
return open(filePath, flags);
},
},
});
})();
const [settledA, settledB] = await Promise.allSettled([promiseA, promiseB]);
expect(settledA.status).toBe('rejected');
expect(settledB.status).toBe('fulfilled');
// Top-level TSV paths AND pinned snapshot must both resolve to Export B cleanly.
expect(await readFile(join(outDir, CURATE_INPUT_FILES.providerMapping), 'utf8')).toBe(
'qm\tSNAP_B\t2021-01-01\t2024-01-15\t20\n'
);
expect(await readFile(join(outDir, CURATE_INPUT_FILES.listingCurrencies), 'utf8')).toBe(
'20\tCAD\n'
);
expect(await readFile(join(outDir, CURATE_INPUT_FILES.delistings), 'utf8')).toBe(
'SNAP_B\t2024-01-15\t20\t\t-1\n'
);
const pinned = await pinCurateInputSnapshot(outDir);
expect(await readFile(pinned.delistings, 'utf8')).toBe('SNAP_B\t2024-01-15\t20\t\t-1\n');
const entries = await readdir(outDir);
expect(entries.some(e => e.includes('.tmp') || e.includes('.bak') || e === CURATE_INPUT_PUBLISH_LOCK)).toBe(false);
}
// Case 2: Export B enters publish first on a fresh dir and commits while Export A is waiting
// on `.publish.lock`; Export A then acquires the lock, sees existing top-level links, and fails
// at commit rename. A's rollback must leave B's top-level links and `current` intact.
{
const outDir = await makeTempDir('curate-inputs-race-b-commits-');
let bEnteredPublishResolve!: () => void;
const bEnteredPublish = new Promise<void>(resolve => {
bEnteredPublishResolve = resolve;
});
let aWaitingForLockResolve!: () => void;
const aWaitingForLock = new Promise<void>(resolve => {
aWaitingForLockResolve = resolve;
});
const promiseB = exportCurateInputs(createInMemoryQueryable(snapB), {
outDir,
fs: {
rename: async (oldPath, newPath) => {
if (String(newPath) === join(outDir, 'current')) {
bEnteredPublishResolve();
await aWaitingForLock;
}
return rename(oldPath, newPath);
},
},
});
const promiseA = (async () => {
await bEnteredPublish;
let signaledWait = false;
return exportCurateInputs(createInMemoryQueryable(snapA), {
outDir,
fs: {
open: async (filePath, flags) => {
if (String(filePath) === join(outDir, CURATE_INPUT_PUBLISH_LOCK) && !signaledWait) {
signaledWait = true;
aWaitingForLockResolve();
}
return open(filePath, flags);
},
rename: async (oldPath, newPath) => {
if (String(newPath) === join(outDir, 'current')) {
throw new Error('Simulated commit EIO in Export A after B committed');
}
return rename(oldPath, newPath);
},
},
});
})();
const [settledB, settledA] = await Promise.allSettled([promiseB, promiseA]);
expect(settledB.status).toBe('fulfilled');
expect(settledA.status).toBe('rejected');
// Export B's top-level TSV paths AND pinned snapshot remain 100% readable.
expect(await readFile(join(outDir, CURATE_INPUT_FILES.providerMapping), 'utf8')).toBe(
'qm\tSNAP_B\t2021-01-01\t2024-01-15\t20\n'
);
expect(await readFile(join(outDir, CURATE_INPUT_FILES.listingCurrencies), 'utf8')).toBe(
'20\tCAD\n'
);
expect(await readFile(join(outDir, CURATE_INPUT_FILES.delistings), 'utf8')).toBe(
'SNAP_B\t2024-01-15\t20\t\t-1\n'
);
const pinned = await pinCurateInputSnapshot(outDir);
expect(await readFile(pinned.providerMapping, 'utf8')).toBe(
'qm\tSNAP_B\t2021-01-01\t2024-01-15\t20\n'
);
const entries = await readdir(outDir);
expect(entries.some(e => e.includes('.tmp') || e.includes('.bak') || e === CURATE_INPUT_PUBLISH_LOCK)).toBe(false);
}
});
test('fails closed with PUBLISH_LOCK_TIMEOUT when publish lock is held past timeout without removing foreign lock', async () => {
const outDir = await makeTempDir('curate-inputs-lock-timeout-');
const lockPath = join(outDir, CURATE_INPUT_PUBLISH_LOCK);
await writeFile(lockPath, '{"pid":999999,"runTag":"foreign"}\n', 'utf8');
const client = createInMemoryQueryable({
listings: [],
continuities: [],
providerMappings: [],
});
await expectExportErrorAsync(
() => exportCurateInputs(client, { outDir, publishLockTimeoutMs: 15 }),
'PUBLISH_LOCK_TIMEOUT',
'publish',
outDir
);
// Foreign lock file must remain untouched.
expect(await readFile(lockPath, 'utf8')).toBe('{"pid":999999,"runTag":"foreign"}\n');
});
test('rejects pool-like objects and invalid chunkSize', async () => {
const outDir = await makeTempDir('curate-inputs-pool-');
const poolLike = {