|
1 | 1 | import assert from 'node:assert/strict' |
| 2 | +import fs from 'node:fs' |
2 | 3 | import { chmodSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs' |
| 4 | +import { syncBuiltinESMExports } from 'node:module' |
3 | 5 | import { tmpdir } from 'node:os' |
4 | 6 | import { join } from 'node:path' |
5 | 7 | import { test } from 'node:test' |
@@ -277,3 +279,71 @@ test('a plain temporary file a crashed writer left behind (one name) is overwrit |
277 | 279 | assert.equal(JSON.parse(readFileSync(join(dir, 'trace-000001.json'), 'utf8')).records.length, 1) |
278 | 280 | trace.close() |
279 | 281 | }) |
| 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