-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprepare.mjs
More file actions
361 lines (315 loc) · 10.9 KB
/
Copy pathprepare.mjs
File metadata and controls
361 lines (315 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* `backup-helper prepare` — local pieceCID enrichment and CAR renaming.
*
* Uses tracking.db as the source of truth for completed shards, then validates
* local `.car` file presence before renaming each completed shard to its
* `<pieceCid>.car` filename. Missing piece CIDs are computed from the local
* CAR bytes and persisted back into tracking.db.
*/
import fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { Worker } from 'node:worker_threads'
import pMap from 'p-map'
import { pathExists, renderProgressLine } from '../../utils.js'
import { pieceCarPath, shardCarPath } from '../lib/layout.mjs'
import { openTrackingDb } from '../lib/tracking-db.mjs'
const DEFAULT_PREPARE_CONCURRENCY = 16
const PREPARE_BATCH_SIZE = 1_000
/**
* @typedef {object} PrepareWorkItem
* @property {string} shardCid
* @property {string | null} pieceCid
* @property {string} carPath
*/
/**
* Pool of worker threads that each compute a piece CID from a CAR file, so the
* CPU-bound (pure-JS, single-threaded) CommP hashing runs in parallel across
* cores. Each worker calls the same `@filoz/synapse-core/piece` hash as before,
* so output piece CIDs are identical — only throughput changes. DB writes and
* file renames stay on the main thread (sqlite is not shared with workers).
*/
class PieceCidWorkerPool {
/** @param {number} size */
constructor(size) {
this.workerPath = fileURLToPath(new URL('../lib/piece-cid-worker.mjs', import.meta.url))
/** @type {import('node:worker_threads').Worker[]} */
this.workers = []
/** @type {import('node:worker_threads').Worker[]} */
this.idle = []
/** @type {Array<{carPath: string, resolve: (v: string) => void, reject: (e: Error) => void}>} */
this.queue = []
/** @type {Map<import('node:worker_threads').Worker, {resolve: (v: string) => void, reject: (e: Error) => void}>} */
this.busy = new Map()
/** @type {Error | null} */
this.fatalError = null
this.closing = false
/** @type {Set<import('node:worker_threads').Worker>} */
this.failedWorkers = new Set()
for (let i = 0; i < Math.max(1, size); i++) {
const worker = new Worker(this.workerPath)
worker.on('message', (msg) => this._onMessage(worker, msg))
worker.on('error', (err) => this._onError(worker, err))
worker.on('exit', (code) => this._onExit(worker, code))
this.workers.push(worker)
this.idle.push(worker)
}
}
/**
* @param {string} carPath
* @returns {Promise<string>}
*/
compute(carPath) {
return new Promise((resolve, reject) => {
if (this.fatalError) {
reject(this.fatalError)
return
}
const worker = this.idle.pop()
if (worker) this._assign(worker, { carPath, resolve, reject })
else this.queue.push({ carPath, resolve, reject })
})
}
/**
* @param {import('node:worker_threads').Worker} worker
* @param {{carPath: string, resolve: (v: string) => void, reject: (e: Error) => void}} job
*/
_assign(worker, job) {
this.busy.set(worker, { resolve: job.resolve, reject: job.reject })
worker.postMessage({ carPath: job.carPath })
}
/**
* @param {import('node:worker_threads').Worker} worker
* @param {{pieceCid?: string, error?: string}} msg
*/
_onMessage(worker, msg) {
const job = this.busy.get(worker)
this.busy.delete(worker)
if (job) {
if (msg.error) job.reject(new Error(msg.error))
else job.resolve(/** @type {string} */ (msg.pieceCid))
}
const next = this.queue.shift()
if (next) this._assign(worker, next)
else this.idle.push(worker)
}
/**
* @param {import('node:worker_threads').Worker} worker
* @param {Error} err
*/
_onError(worker, err) {
this._handleWorkerFailure(worker, err, `prepare: piece CID worker error: ${err?.message || err}`)
}
/**
* @param {import('node:worker_threads').Worker} worker
* @param {number} code
*/
_onExit(worker, code) {
if (this.closing) return
if (code !== 0) {
const err = new Error(`piece CID worker exited with code ${code}`)
this._handleWorkerFailure(worker, err, `prepare: ${err.message}`)
return
}
if (this.busy.has(worker)) {
const err = new Error('piece CID worker exited unexpectedly while processing a job')
this._handleWorkerFailure(worker, err, `prepare: ${err.message}`)
}
}
/**
* @param {import('node:worker_threads').Worker} worker
*/
_removeWorker(worker) {
this.workers = this.workers.filter((candidate) => candidate !== worker)
this.idle = this.idle.filter((candidate) => candidate !== worker)
}
/**
* @param {import('node:worker_threads').Worker} worker
* @param {Error} err
* @param {string} logMessage
*/
_handleWorkerFailure(worker, err, logMessage) {
if (this.failedWorkers.has(worker)) return
this.failedWorkers.add(worker)
const job = this.busy.get(worker)
this.busy.delete(worker)
if (job) job.reject(err)
this._removeWorker(worker)
console.error(logMessage)
this._drainOrFailQueue()
}
_drainOrFailQueue() {
while (this.idle.length > 0 && this.queue.length > 0) {
const worker = this.idle.pop()
const next = this.queue.shift()
if (!worker || !next) break
this._assign(worker, next)
}
if (this.workers.length > 0) return
this.fatalError = new Error('all piece CID workers failed')
console.error(`prepare: ${this.fatalError.message}`)
while (this.queue.length > 0) {
const next = this.queue.shift()
if (!next) break
next.reject(this.fatalError)
}
}
async close() {
this.closing = true
await Promise.all(this.workers.map((worker) => worker.terminate()))
}
}
/**
* @param {string} carPath
*/
function aria2ControlPath(carPath) {
return `${carPath}.aria2`
}
/**
* Resolve the local CAR path for prepare, accepting either shard- or piece-named files.
*
* @param {string} dir
* @param {string} shardCid
* @param {string | null} pieceCid
*/
async function resolvePrepareCarPath(dir, shardCid, pieceCid) {
const shardPath = shardCarPath(dir, shardCid)
const hasShardPath = await pathExists(shardPath)
if (!pieceCid) {
return hasShardPath ? shardPath : null
}
const piecePath = pieceCarPath(dir, pieceCid)
const hasPiecePath = await pathExists(piecePath)
if (hasShardPath && hasPiecePath) {
throw new Error(`prepare: both shard and piece CAR files exist for ${shardCid}`)
}
if (hasPiecePath) return piecePath
if (hasShardPath) return shardPath
return null
}
/**
* Rename a completed shard CAR from `<shardCid>.car` to `<pieceCid>.car`, carrying any `.aria2` sidecar with it.
*
* @param {string} dir
* @param {string} shardCid
* @param {string} pieceCid
* @param {string} carPath
*/
async function renameCarToPieceCid(dir, shardCid, pieceCid, carPath) {
const targetPath = pieceCarPath(dir, pieceCid)
if (carPath === targetPath) return targetPath
if (await pathExists(targetPath)) {
throw new Error(`prepare: target piece CAR already exists for ${shardCid}`)
}
await fs.rename(carPath, targetPath)
const aria2SourceControlPath = aria2ControlPath(carPath)
if (!(await pathExists(aria2SourceControlPath))) return targetPath
try {
await fs.rename(aria2SourceControlPath, aria2ControlPath(targetPath))
return targetPath
} catch (err) {
try {
// if renaming the control file fails, roll the CAR rename back
await fs.rename(targetPath, carPath)
} catch {}
throw err
}
}
/**
* @param {object} summary
* @param {number} summary.total
* @param {number} summary.done
* @param {number} summary.computed
* @param {number} summary.failed
*/
function renderPrepareProgress(summary) {
renderProgressLine(
`prepare: total=${summary.total} done=${summary.done}/${summary.total} computed=${summary.computed} failed=${summary.failed}`,
)
}
/**
* @param {object} args
* @param {string} args.dir
* @param {number | undefined} args.concurrency
*/
export async function runPrepare({ dir, concurrency }) {
const tracking = openTrackingDb(dir)
const workerConcurrency = concurrency ?? DEFAULT_PREPARE_CONCURRENCY
/** @type {PieceCidWorkerPool | null} */
let pool = null
try {
const total = tracking.getDownloadStats().complete
if (total === 0) {
console.error('prepare: nothing to do')
return
}
pool = new PieceCidWorkerPool(workerConcurrency)
const summary = {
total,
done: 0,
computed: 0,
failed: 0,
}
renderPrepareProgress(summary)
let afterShardCid = ''
while (true) {
const candidates = tracking.listPrepareCandidates(PREPARE_BATCH_SIZE, afterShardCid)
if (candidates.length === 0) break
const results = await pMap(
candidates,
async (candidate) => {
/** @type {PrepareWorkItem | null} */
let workItem = null
let computedPieceCid = false
try {
const carPath = await resolvePrepareCarPath(dir, candidate.shardCid, candidate.pieceCid)
if (!carPath) {
throw new Error(`prepare: missing shard file for ${candidate.shardCid}`)
}
workItem = {
shardCid: candidate.shardCid,
pieceCid: candidate.pieceCid,
carPath,
}
let pieceCid = candidate.pieceCid
if (!pieceCid) {
if (!pool) throw new Error('prepare: piece CID worker pool was not initialized')
pieceCid = await pool.compute(workItem.carPath)
tracking.setPieceCid(workItem.shardCid, pieceCid)
computedPieceCid = true
} else {
tracking.setRootShardsPieceCid(workItem.shardCid, pieceCid)
}
await renameCarToPieceCid(dir, workItem.shardCid, pieceCid, workItem.carPath)
tracking.clearPrepareFailure(workItem.shardCid)
return { computedPieceCid, failed: false }
} catch (err) {
const shardCid = workItem?.shardCid || candidate.shardCid
const error = String(err?.message || err)
tracking.markPrepareFailure({
shardCid,
error,
retryable: false,
})
console.error(`prepare: failure shard=${shardCid} error=${error}`)
return { computedPieceCid: false, failed: true }
}
},
{ concurrency: workerConcurrency },
)
for (const result of results) {
if (result.computedPieceCid) summary.computed++
if (result.failed) summary.failed++
summary.done++
renderPrepareProgress(summary)
}
afterShardCid = candidates[candidates.length - 1].shardCid
}
if (process.stdout.isTTY) process.stdout.write('\n')
console.error(
`prepare: done. total=${summary.total} done=${summary.done}/${summary.total} computed=${summary.computed} failed=${summary.failed}`,
)
} finally {
if (pool) await pool.close()
tracking.close()
}
}