Skip to content

Commit c4940e7

Browse files
committed
test(timeline): pin a refused directory fsync to the saved segment
Windows answers EPERM for `fsync` on a directory handle (EINVAL on a filesystem without directory flushes), and it is the last step of `writeWhole`, after the temporary file was synced and renamed. Since #3300 `flushDirectory` tolerates that refusal, so a segment that is already in place still counts as saved and is not rewritten on every flush (#3298, #3301). A Linux directory fsync is a no-op success, so the three cases pin the `fsyncSync` binding `store.js` imports (through node:module's `syncBuiltinESMExports()`) and refuse only directory handles: EPERM still saves the segment (flush true, `failing` false, not rewritten by the next flush, records read back after a reopen), EINVAL is tolerated the same way, and a rotation still prunes to the cap while the directory fsync is refused. Emptying the tolerated set turns all three red. Docs-not-needed: tests only; no product behaviour and no documented write order changes. Refs #3301
1 parent 664f48f commit c4940e7

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

‎plugins/timeline/test/store.test.js‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import assert from 'node:assert/strict'
2+
import fs from 'node:fs'
23
import { chmodSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs'
4+
import { syncBuiltinESMExports } from 'node:module'
35
import { tmpdir } from 'node:os'
46
import { join } from 'node:path'
57
import { test } from 'node:test'
@@ -277,3 +279,71 @@ test('a plain temporary file a crashed writer left behind (one name) is overwrit
277279
assert.equal(JSON.parse(readFileSync(join(dir, 'trace-000001.json'), 'utf8')).records.length, 1)
278280
trace.close()
279281
})
282+
283+
// Windows refuses `fsync` on a directory handle (EPERM from FlushFileBuffers; EINVAL on a filesystem
284+
// without directory flushes). It is the last step of `writeWhole`, after the temporary file was
285+
// synced and renamed, so the segment's bytes are already durable when it throws: the write must
286+
// count as saved, or the published segment is rewritten on every flush and the cap and the lock
287+
// hand-off never settle (#3298). A Linux directory fsync is a no-op success, so the case replaces
288+
// the `fsyncSync` binding `store.js` imports (node:module keeps that binding live) for as long as it
289+
// runs, and refuses only directory handles: the file sync stays real, and a failing *file* sync
290+
// would still reach the caller instead of being swallowed.
291+
const refuseDirectorySync = (t, code) => {
292+
const real = fs.fsyncSync
293+
const seen = { files: 0 }
294+
fs.fsyncSync = (fd) => {
295+
if (fs.fstatSync(fd).isDirectory()) throw Object.assign(new Error(`${code}: fsync failed`), { code })
296+
seen.files += 1
297+
return real(fd)
298+
}
299+
syncBuiltinESMExports()
300+
t.after(() => {
301+
fs.fsyncSync = real
302+
syncBuiltinESMExports()
303+
})
304+
return seen
305+
}
306+
307+
test('a directory fsync the platform refuses still saves the segment (Windows EPERM)', (t) => {
308+
const dir = tmp()
309+
t.after(() => rmSync(dir, { recursive: true, force: true }))
310+
const seen = refuseDirectorySync(t, 'EPERM')
311+
const trace = openTrace({ dir, now: () => 10_000 })
312+
trace.append(rec(9_000))
313+
assert.equal(trace.flush(), true, 'the bytes are durable once the file was synced and renamed')
314+
assert.equal(trace.stats().failing, false, 'a refused directory sync is not a failed write')
315+
assert.equal(seen.files, 1, 'the segment itself was synced before the directory was')
316+
assert.equal(trace.flush(), true, 'a flush with nothing new succeeds too')
317+
assert.equal(seen.files, 1, 'a saved segment is not rewritten by the next flush')
318+
trace.close()
319+
const reopened = openTrace({ dir, now: () => 10_000 })
320+
assert.deepEqual(reopened.records().map(r => r.t), [9_000])
321+
reopened.close()
322+
})
323+
324+
test('a directory fsync refused with EINVAL is tolerated the same way', (t) => {
325+
const dir = tmp()
326+
t.after(() => rmSync(dir, { recursive: true, force: true }))
327+
const seen = refuseDirectorySync(t, 'EINVAL')
328+
const trace = openTrace({ dir, now: () => 10_000 })
329+
trace.append(rec(9_000))
330+
assert.equal(trace.flush(), true)
331+
assert.equal(trace.stats().failing, false)
332+
assert.equal(seen.files, 1)
333+
trace.close()
334+
const reopened = openTrace({ dir, now: () => 10_000 })
335+
assert.deepEqual(reopened.records().map(r => r.t), [9_000])
336+
reopened.close()
337+
})
338+
339+
test('a rotation still prunes to the cap when the directory fsync is refused', (t) => {
340+
const dir = tmp()
341+
t.after(() => rmSync(dir, { recursive: true, force: true }))
342+
refuseDirectorySync(t, 'EPERM')
343+
const trace = openTrace({ dir, segmentRecords: 2, segments: 2, flushMs: 60_000, now: () => 1_000_000 })
344+
for (const at of [1, 2, 3, 4, 5, 6]) trace.append(rec(at))
345+
assert.equal(trace.flush(), true)
346+
assert.deepEqual(files(dir).filter(name => /^trace-\d{6}\.json$/.test(name)), ['trace-000002.json', 'trace-000003.json'])
347+
assert.deepEqual(trace.records().map(r => r.t), [3, 4, 5, 6])
348+
trace.close()
349+
})

0 commit comments

Comments
 (0)