mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-11-29 23:37:41 +00:00
* perf(embeddings): batch resume verification and skip a no-op journal fsync completedNodes verified each complete receipt with its own native query, so resuming a 64-node unit cost 65 statements. verify() already fills bounded primary-key reads across groups; pass it the whole unit. acknowledge removed a failed-unit record with rm --force and then synced the job directory every time. A manifest unit has no such record, so the sync covered an unchanged directory. Unlink instead, and sync only when a record was removed. Errors other than ENOENT still fail the acknowledgement. Refs #3870 * fix(embeddings): sync before acknowledging a recovered unit The no-op fsync skip relied on ENOENT meaning nothing was removed. After an acknowledgement whose unlink landed but whose directory fsync failed, the reopened journal's retry also sees ENOENT and wrote job.json with no barrier, so a second interruption could leave failed-unit accounting inconsistent. Keep the fast path only for a unit begun by this journal instance. A unit found active by recover() always syncs the directory before job.json is written, and a reopen test pins that order. * test(embeddings): clarify when acknowledgements sync the journal --------- Co-authored-by: Abhigyan Patwari <126312502+abhigyanpatwari@users.noreply.github.com>
140 lines
5.9 KiB
TypeScript
140 lines
5.9 KiB
TypeScript
/**
|
|
* `acknowledge` syncs the job directory after removing a failed-unit record and
|
|
* for every recovered active unit, since an earlier unlink may still need a sync.
|
|
* These counts exclude directory syncs performed by record writes (#3870).
|
|
*/
|
|
import fs from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const { syncEmbeddingJobDirectory, writeEmbeddingJobRecord, events } = vi.hoisted(() => ({
|
|
syncEmbeddingJobDirectory: vi.fn(),
|
|
writeEmbeddingJobRecord: vi.fn(),
|
|
events: [] as string[],
|
|
}));
|
|
|
|
vi.mock('../../src/storage/embedding-job.js', async (importOriginal) => {
|
|
const original = await importOriginal<typeof import('../../src/storage/embedding-job.js')>();
|
|
syncEmbeddingJobDirectory.mockImplementation(async (directory: string) => {
|
|
events.push('sync');
|
|
return original.syncEmbeddingJobDirectory(directory);
|
|
});
|
|
writeEmbeddingJobRecord.mockImplementation(
|
|
async (directory: string, name: string, payload: unknown) => {
|
|
events.push(`write ${name}`);
|
|
return original.writeEmbeddingJobRecord(directory, name, payload);
|
|
},
|
|
);
|
|
return { ...original, syncEmbeddingJobDirectory, writeEmbeddingJobRecord };
|
|
});
|
|
|
|
import {
|
|
EmbeddingJobJournal,
|
|
type EmbeddingUnitIntent,
|
|
} from '../../src/storage/embedding-job-journal.js';
|
|
import type { EmbeddingManifestReceipt } from '../../src/core/embeddings/embedding-work-manifest.js';
|
|
|
|
const identity = 'a'.repeat(64);
|
|
const manifest: EmbeddingManifestReceipt = {
|
|
version: 1,
|
|
digest: 'b'.repeat(64),
|
|
sourceIdentity: 'c'.repeat(64),
|
|
bytes: 30,
|
|
totalNodes: 6,
|
|
totalUnits: 3,
|
|
maximumUnitBytes: 100,
|
|
};
|
|
const unit = (n: number, origin: 'manifest' | 'failed' = 'manifest'): EmbeddingUnitIntent => ({
|
|
start: { unit: n, offset: n * 10 },
|
|
next: { unit: n + 1, offset: (n + 1) * 10 },
|
|
nodes: 2,
|
|
origin,
|
|
});
|
|
const record = 'failed-0000000000000000.json';
|
|
const directories: string[] = [];
|
|
|
|
async function job() {
|
|
const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'gn-embedding-ack-'));
|
|
directories.push(directory);
|
|
return EmbeddingJobJournal.create(directory, identity, manifest);
|
|
}
|
|
const exists = (file: string) =>
|
|
fs.access(file).then(
|
|
() => true,
|
|
() => false,
|
|
);
|
|
|
|
afterEach(async () => {
|
|
for (const directory of directories.splice(0))
|
|
await fs.rm(directory, { force: true, recursive: true });
|
|
});
|
|
|
|
describe('acknowledge directory sync', () => {
|
|
it('skips the directory sync for a manifest unit with no failed record', async () => {
|
|
const journal = await job();
|
|
await journal.begin(unit(0));
|
|
syncEmbeddingJobDirectory.mockClear();
|
|
await journal.acknowledge({ reusedNodes: 0, chunks: 2 });
|
|
expect(syncEmbeddingJobDirectory).not.toHaveBeenCalled();
|
|
expect(journal.snapshot()).toMatchObject({ completedUnits: 1, cursor: unit(0).next });
|
|
});
|
|
|
|
it('removes a retried unit record and syncs the directory once', async () => {
|
|
let journal = await job();
|
|
await journal.begin(unit(0));
|
|
await journal.fail('provider');
|
|
journal = await EmbeddingJobJournal.recover(journal.directory, identity, manifest);
|
|
await journal.begin(unit(0, 'failed'));
|
|
syncEmbeddingJobDirectory.mockClear();
|
|
await journal.acknowledge({ reusedNodes: 0, chunks: 2 });
|
|
expect(syncEmbeddingJobDirectory).toHaveBeenCalledTimes(1);
|
|
expect(await exists(path.join(journal.directory, record))).toBe(false);
|
|
});
|
|
|
|
it('removes and syncs a record left by a crash inside fail() beside the active manifest unit', async () => {
|
|
// fail() wrote the record but died before job.json moved the unit out of
|
|
// `active`, so the intent still has origin `manifest`.
|
|
const crashed = await job();
|
|
await crashed.begin(unit(0));
|
|
const state = await fs.readFile(path.join(crashed.directory, 'job.json'));
|
|
await crashed.fail('provider');
|
|
await fs.writeFile(path.join(crashed.directory, 'job.json'), state);
|
|
const journal = await EmbeddingJobJournal.recover(crashed.directory, identity, manifest);
|
|
expect(journal.snapshot().active).toMatchObject({ origin: 'manifest' });
|
|
expect(await exists(path.join(journal.directory, record))).toBe(true);
|
|
syncEmbeddingJobDirectory.mockClear();
|
|
await journal.acknowledge({ reusedNodes: 0, chunks: 2 });
|
|
expect(syncEmbeddingJobDirectory).toHaveBeenCalledTimes(1);
|
|
expect(await exists(path.join(journal.directory, record))).toBe(false);
|
|
});
|
|
|
|
it('syncs before publishing a recovered acknowledgement whose earlier unlink was never synced', async () => {
|
|
let journal = await job();
|
|
await journal.begin(unit(0));
|
|
await journal.fail('provider');
|
|
journal = await EmbeddingJobJournal.recover(journal.directory, identity, manifest);
|
|
await journal.begin(unit(0, 'failed'));
|
|
// The unlink lands, then the directory fsync fails.
|
|
syncEmbeddingJobDirectory.mockRejectedValueOnce(new Error('EIO'));
|
|
await expect(journal.acknowledge({ reusedNodes: 0, chunks: 2 })).rejects.toThrow('EIO');
|
|
expect(await exists(path.join(journal.directory, record))).toBe(false);
|
|
|
|
journal = await EmbeddingJobJournal.recover(journal.directory, identity, manifest);
|
|
expect(journal.snapshot().active).toEqual(unit(0, 'failed'));
|
|
events.length = 0;
|
|
await journal.acknowledge({ reusedNodes: 0, chunks: 2 });
|
|
// The retry's unlink finds nothing, but the earlier one still needs its barrier.
|
|
expect(events.slice(0, 2)).toEqual(['sync', 'write job.json']);
|
|
expect(journal.snapshot()).toMatchObject({ active: null, failedUnits: 0 });
|
|
});
|
|
|
|
it('still fails the acknowledgement when the record cannot be removed', async () => {
|
|
const journal = await job();
|
|
await journal.begin(unit(0));
|
|
// A directory in the record's place makes unlink fail with something other than ENOENT.
|
|
await fs.mkdir(path.join(journal.directory, record));
|
|
await expect(journal.acknowledge({ reusedNodes: 0, chunks: 2 })).rejects.toThrow();
|
|
expect(journal.snapshot().active).toEqual(unit(0));
|
|
});
|
|
});
|