| … | … | @@ -0,0 +1,146 @@ |
| 1 | // Crash during append — SIGKILL a publish in flight, reopen, and check the tree |
| 2 | // and the pointer records still agree. The tagged-record scheme exists so the |
| 3 | // pointer commits in the same CoreTX as the tree node; this is what tests it. |
| 4 | // |
| 5 | // node test/crash-append.mjs <workDir> <bigFile> [trials] |
| 6 | // node test/crash-append.mjs --child <virtual|stock> <dir> <file> (internal) |
| 7 | import { createRequire } from 'module' |
| 8 | const require = createRequire(import.meta.url) |
| 9 | |
| 10 | const fs = require('fs') |
| 11 | const { spawn } = require('child_process') |
| 12 | const Corestore = require('corestore') |
| 13 | const Hyperdrive = require('hyperdrive') |
| 14 | const { ContentStore, build } = require('../lib/storage.cjs') |
| 15 | const { chunk } = await import('../../hyper-overlay/lib/chunker.js') |
| 16 | |
| 17 | const NAME = '/app.bin' |
| 18 | const PRIMARY = Buffer.alloc(32, 7) |
| 19 | |
| 20 | function indexContent (file) { |
| 21 | const buf = fs.readFileSync(file) |
| 22 | const content = new ContentStore() |
| 23 | const chunks = chunk(buf) |
| 24 | for (const c of chunks) content.add(c.hash, file, c.offset, c.length) |
| 25 | return { buf, chunks, content } |
| 26 | } |
| 27 | |
| 28 | function openStore (dir, mode, content) { |
| 29 | if (mode === 'stock') return new Corestore(dir, { primaryKey: PRIMARY, unsafe: true }) |
| 30 | const Storage = build(content) |
| 31 | return new Corestore(new Storage(dir), { primaryKey: PRIMARY, unsafe: true }) |
| 32 | } |
| 33 | |
| 34 | function write (ws, buf) { |
| 35 | return new Promise((resolve) => { |
| 36 | if (ws.write(buf)) return resolve() |
| 37 | ws.once('drain', resolve) |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | // ── child: publish, and get killed partway through ────────────────────────── |
| 42 | if (process.argv[2] === '--child') { |
| 43 | const [, , , mode, dir, file] = process.argv |
| 44 | const { chunks, content } = indexContent(file) |
| 45 | const store = openStore(dir, mode, content) |
| 46 | const drive = new Hyperdrive(store) |
| 47 | await drive.ready() |
| 48 | const ws = drive.createWriteStream(NAME, { dedup: true }) |
| 49 | process.stdout.write('READY\n') |
| 50 | for (const c of chunks) await write(ws, c.data) |
| 51 | await new Promise((resolve, reject) => { |
| 52 | ws.once('error', reject) |
| 53 | ws.once('close', resolve) |
| 54 | ws.end() |
| 55 | }) |
| 56 | await drive.close() |
| 57 | await store.close() |
| 58 | process.stdout.write('DONE\n') |
| 59 | process.exit(0) |
| 60 | } |
| 61 | |
| 62 | // ── parent ────────────────────────────────────────────────────────────────── |
| 63 | const ROOT = process.argv[2] |
| 64 | const FILE = process.argv[3] |
| 65 | const TRIALS = Number(process.argv[4] || 4) |
| 66 | const { buf, content } = indexContent(FILE) |
| 67 | |
| 68 | function crashRun (dir, mode, killAfterMs) { |
| 69 | return new Promise((resolve) => { |
| 70 | const child = spawn(process.execPath, [process.argv[1], '--child', mode, dir, FILE], { |
| 71 | stdio: ['ignore', 'pipe', 'ignore'] |
| 72 | }) |
| 73 | let killed = false |
| 74 | let finished = false |
| 75 | child.stdout.on('data', (d) => { |
| 76 | if (d.toString().includes('READY')) { |
| 77 | setTimeout(() => { |
| 78 | if (!finished) { |
| 79 | child.kill('SIGKILL') |
| 80 | killed = true |
| 81 | } |
| 82 | }, killAfterMs) |
| 83 | } |
| 84 | if (d.toString().includes('DONE')) finished = true |
| 85 | }) |
| 86 | child.on('exit', (code, signal) => resolve({ killed, finished, code, signal })) |
| 87 | }) |
| 88 | } |
| 89 | |
| 90 | // Reopen and look for the one state that would prove the scheme broken: |
| 91 | // the tree claims a block, but its bytes cannot be produced. |
| 92 | async function audit (dir, mode) { |
| 93 | const store = openStore(dir, mode, content) |
| 94 | const drive = new Hyperdrive(store) |
| 95 | const out = { opened: false, blobsLength: 0, claimedButMissing: 0, entry: null, bytesOk: null } |
| 96 | try { |
| 97 | await drive.ready() |
| 98 | await drive.getBlobs() |
| 99 | out.opened = true |
| 100 | const core = drive.blobs.core |
| 101 | out.blobsLength = core.length |
| 102 | |
| 103 | for (let i = 0; i < core.length; i++) { |
| 104 | if (!(await core.has(i))) continue |
| 105 | const block = await core.get(i, { wait: false }) |
| 106 | if (block === null) out.claimedButMissing++ |
| 107 | } |
| 108 | |
| 109 | const entry = await drive.entry(NAME) |
| 110 | out.entry = entry === null ? 'none' : 'present' |
| 111 | if (entry !== null) { |
| 112 | const got = await drive.get(NAME, { timeout: 5000 }).catch(() => null) |
| 113 | out.bytesOk = got !== null && got.equals(buf) |
| 114 | } |
| 115 | } catch (err) { |
| 116 | out.error = err.code || err.message |
| 117 | } |
| 118 | await drive.close().catch(() => {}) |
| 119 | await store.close().catch(() => {}) |
| 120 | return out |
| 121 | } |
| 122 | |
| 123 | const results = [] |
| 124 | for (let t = 0; t < TRIALS; t++) { |
| 125 | for (const mode of ['virtual', 'stock']) { |
| 126 | const dir = `${ROOT}/${mode}-${t}` |
| 127 | const killAfterMs = 150 + t * 400 |
| 128 | const run = await crashRun(dir, mode, killAfterMs) |
| 129 | const state = await audit(dir, mode) |
| 130 | results.push({ trial: t, mode, killAfterMs, run, state }) |
| 131 | console.log(JSON.stringify({ trial: t, mode, killAfterMs, ...run, ...state })) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | content.close() |
| 136 | |
| 137 | const broken = results.filter((r) => r.state.claimedButMissing > 0 || r.state.bytesOk === false) |
| 138 | console.log( |
| 139 | JSON.stringify({ |
| 140 | trials: results.length, |
| 141 | killedMidPublish: results.filter((r) => r.run.killed).length, |
| 142 | allReopened: results.every((r) => r.state.opened), |
| 143 | inconsistent: broken.length, |
| 144 | verdict: broken.length === 0 ? 'PASS' : 'FAIL' |
| 145 | }) |
| 146 | ) |