-
Notifications
You must be signed in to change notification settings - Fork 212
/
snapStore.js
393 lines (353 loc) · 10.7 KB
/
snapStore.js
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// @ts-check
import { Buffer } from 'buffer';
import { createHash } from 'crypto';
import { finished as finishedCallback, Readable } from 'stream';
import { promisify } from 'util';
import { createGzip, createGunzip } from 'zlib';
import { assert, details as d } from '@agoric/assert';
import {
aggregateTryFinally,
fsStreamReady,
PromiseAllOrErrors,
} from '@agoric/internal';
/**
* @typedef {object} SnapshotResult
* @property {string} hash sha256 hash of (uncompressed) snapshot
* @property {number} uncompressedSize size of (uncompressed) snapshot
* @property {number} rawSaveSeconds time to save (uncompressed) snapshot
* @property {number} compressedSize size of (compressed) snapshot
* @property {number} compressSeconds time to compress and save snapshot
*/
/**
* @typedef {object} SnapshotInfo
* @property {number} endPos
* @property {string} hash
* @property {number} uncompressedSize
* @property {number} compressedSize
*/
/**
* @typedef {{
* hasHash: (vatID: string, hash: string) => boolean,
* loadSnapshot: <T>(vatID: string, loadRaw: (filePath: string) => Promise<T>) => Promise<T>,
* saveSnapshot: (vatID: string, endPos: number, saveRaw: (filePath: string) => Promise<void>) => Promise<SnapshotResult>,
* deleteAllUnusedSnapshots: () => void,
* deleteVatSnapshots: (vatID: string) => void,
* deleteSnapshotByHash: (vatID: string, hash: string) => void,
* getSnapshotInfo: (vatID: string) => SnapshotInfo,
* }} SnapStore
*
* @typedef {{
* dumpActiveSnapshots: () => {},
* }} SnapStoreDebug
*
*/
/**
* This is a polyfill for the `buffer` function from Node's
* 'stream/consumers' package, which unfortunately only exists in newer versions
* of Node.
*
* @param {AsyncIterable<Buffer>} inStream
*/
export const buffer = async inStream => {
const chunks = [];
for await (const chunk of inStream) {
chunks.push(chunk);
}
return Buffer.concat(chunks);
};
const finished = promisify(finishedCallback);
const noPath = /** @type {import('fs').PathLike} */ (
/** @type {unknown} */ (undefined)
);
/**
* @param {*} db
* @param {{
* createReadStream: typeof import('fs').createReadStream,
* createWriteStream: typeof import('fs').createWriteStream,
* measureSeconds: ReturnType<typeof import('@agoric/internal').makeMeasureSeconds>,
* open: typeof import('fs').promises.open,
* stat: typeof import('fs').promises.stat,
* tmpFile: typeof import('tmp').file,
* tmpName: typeof import('tmp').tmpName,
* unlink: typeof import('fs').promises.unlink,
* }} io
* @param {object} [options]
* @param {boolean | undefined} [options.keepSnapshots]
* @returns {SnapStore & SnapStoreDebug}
*/
export function makeSnapStore(
db,
{
createReadStream,
createWriteStream,
measureSeconds,
stat,
tmpFile,
tmpName,
unlink,
},
{ keepSnapshots = false } = {},
) {
db.exec(`
CREATE TABLE IF NOT EXISTS snapshots (
vatID TEXT,
endPos INTEGER,
inUse INTEGER,
hash TEXT,
uncompressedSize INTEGER,
compressedSize INTEGER,
compressedSnapshot BLOB,
PRIMARY KEY (vatID, endPos)
)
`);
/** @type {(opts: unknown) => Promise<string>} */
const ptmpName = promisify(tmpName);
// Manually promisify `tmpFile` to preserve its post-`error` callback arguments.
const ptmpFile = (options = {}) => {
return new Promise((resolve, reject) => {
tmpFile(options, (err, path, fd, cleanupCallback) => {
if (err) {
reject(err);
} else {
resolve({ path, fd, cleanup: cleanupCallback });
}
});
});
};
const sqlDeleteAllUnusedSnapshots = db.prepare(`
DELETE FROM snapshots
WHERE inUse = 0
`);
/**
* Delete all extant snapshots from the snapstore that are not currently in
* use by some vat.
*/
function deleteAllUnusedSnapshots() {
sqlDeleteAllUnusedSnapshots.run();
}
const sqlStopUsingLastSnapshot = db.prepare(`
UPDATE snapshots
SET inUse = 0
WHERE inUse = 1 AND vatID = ?
`);
const sqlSaveSnapshot = db.prepare(`
INSERT OR REPLACE INTO snapshots
(vatID, endPos, inUse, hash, uncompressedSize, compressedSize, compressedSnapshot)
VALUES (?, ?, 1, ?, ?, ?, ?)
`);
/**
* Generates a new XS heap snapshot, stores a gzipped copy of it into the
* snapshots table, and reports information about the process, including
* snapshot size and timing metrics.
*
* @param {string} vatID
* @param {number} endPos
* @param {(filePath: string) => Promise<void>} saveRaw
* @returns {Promise<SnapshotResult>}
*/
async function saveSnapshot(vatID, endPos, saveRaw) {
const cleanup = [];
return aggregateTryFinally(
async () => {
// TODO: Refactor to use tmpFile rather than tmpName.
const tmpSnapPath = await ptmpName({ template: 'save-raw-XXXXXX.xss' });
cleanup.push(() => unlink(tmpSnapPath));
const { duration: rawSaveSeconds } = await measureSeconds(async () =>
saveRaw(tmpSnapPath),
);
const { size: uncompressedSize } = await stat(tmpSnapPath);
// Perform operations that read snapshot data in parallel.
// We still serialize the stat and opening of tmpSnapPath
// and creation of tmpGzPath for readability, but we could
// parallelize those as well if the cost is significant.
const snapReader = createReadStream(tmpSnapPath);
cleanup.push(() => {
snapReader.destroy();
});
await fsStreamReady(snapReader);
const hashStream = createHash('sha256');
const gzip = createGzip();
let compressedSize = 0;
const { result: hash, duration: compressSeconds } =
await measureSeconds(async () => {
snapReader.pipe(hashStream);
const compressedSnapshot = await buffer(snapReader.pipe(gzip));
await finished(snapReader);
const h = hashStream.digest('hex');
sqlStopUsingLastSnapshot.run(vatID);
if (!keepSnapshots) {
deleteAllUnusedSnapshots();
}
compressedSize = compressedSnapshot.length;
sqlSaveSnapshot.run(
vatID,
endPos,
h,
uncompressedSize,
compressedSize,
compressedSnapshot,
);
return h;
});
return harden({
hash,
uncompressedSize,
rawSaveSeconds,
compressSeconds,
compressedSize,
});
},
async () => {
await PromiseAllOrErrors(
cleanup.reverse().map(fn => Promise.resolve().then(() => fn())),
);
},
);
}
const sqlLoadSnapshot = db.prepare(`
SELECT hash, compressedSnapshot
FROM snapshots
WHERE vatID = ?
ORDER BY endPos DESC
LIMIT 1
`);
/**
* Loads the most recent snapshot for a given vat.
*
* @param {string} vatID
* @param {(filePath: string) => Promise<T>} loadRaw
* @template T
*/
async function loadSnapshot(vatID, loadRaw) {
const cleanup = [];
return aggregateTryFinally(
async () => {
const loadInfo = sqlLoadSnapshot.get(vatID);
assert(loadInfo, `no snapshot available for vat ${vatID}`);
const { hash, compressedSnapshot } = loadInfo;
const gzReader = Readable.from(compressedSnapshot);
cleanup.push(() => gzReader.destroy());
const snapReader = gzReader.pipe(createGunzip());
const {
path,
fd,
cleanup: tmpCleanup,
} = await ptmpFile({ template: `load-${hash}-XXXXXX.xss` });
cleanup.push(tmpCleanup);
const snapWriter = createWriteStream(noPath, {
fd,
autoClose: false,
});
cleanup.push(() => snapWriter.close());
await fsStreamReady(snapWriter);
const hashStream = createHash('sha256');
snapReader.pipe(hashStream);
snapReader.pipe(snapWriter);
await Promise.all([finished(gzReader), finished(snapWriter)]);
const h = hashStream.digest('hex');
h === hash || assert.fail(d`actual hash ${h} !== expected ${hash}`);
const snapWriterClose = cleanup.pop();
snapWriterClose();
return loadRaw(path);
},
async () => {
await PromiseAllOrErrors(
cleanup.reverse().map(fn => Promise.resolve().then(() => fn())),
);
},
);
}
const sqlDeleteVatSnapshots = db.prepare(`
DELETE FROM snapshots
WHERE vatID = ?
`);
/**
* Delete all snapshots for a given vat (for use when, e.g., a vat is terminated)
*
* @param {string} vatID
*/
function deleteVatSnapshots(vatID) {
sqlDeleteVatSnapshots.run(vatID);
}
const sqlGetSnapshotInfo = db.prepare(`
SELECT endPos, hash, uncompressedSize, compressedSize
FROM snapshots
WHERE vatID = ?
ORDER BY endPos DESC
LIMIT 1
`);
/**
* Find out everything there is to know about a given vat's current snapshot
* aside from the snapshot blob itself.
*
* @param {string} vatID
*
* @returns {SnapshotInfo}
*/
function getSnapshotInfo(vatID) {
return /** @type {SnapshotInfo} */ (sqlGetSnapshotInfo.get(vatID));
}
const sqlHasHash = db.prepare(`
SELECT COUNT(*)
FROM snapshots
WHERE vatID = ? AND hash = ?
`);
sqlHasHash.pluck(true);
/**
* Test if a vat has a specific snapshot identified by its hash.
*
* Note: this is for use by testing and debugging code; normal clients of
* snapStore shouldn't call this.
*
* @param {string} vatID
* @param {string} hash
*
* @returns {boolean}
*/
function hasHash(vatID, hash) {
return !!sqlHasHash.get(vatID, hash);
}
const sqlDeleteSnapshotByHash = db.prepare(`
DELETE FROM snapshots
WHERE vatID = ? AND hash = ?
`);
/**
* Delete a specific snapshot identified by its hash.
*
* Note: this is for use by testing and debugging code; normal clients of
* snapStore shouldn't call this.
*
* @param {string} vatID
* @param {string} hash
*/
function deleteSnapshotByHash(vatID, hash) {
sqlDeleteSnapshotByHash.run(vatID, hash);
}
const sqlDumpActiveSnapshots = db.prepare(`
SELECT vatID, endPos, hash, compressedSnapshot
FROM snapshots
WHERE inUse = 1
ORDER BY vatID, endPos
`);
/**
* debug function to dump active snapshots
*/
function dumpActiveSnapshots() {
const dump = {};
for (const row of sqlDumpActiveSnapshots.iterate()) {
const { vatID, endPos, hash, compressedSnapshot } = row;
dump[vatID] = { endPos, hash, compressedSnapshot };
}
return dump;
}
return harden({
saveSnapshot,
loadSnapshot,
deleteAllUnusedSnapshots,
deleteVatSnapshots,
getSnapshotInfo,
hasHash,
deleteSnapshotByHash,
dumpActiveSnapshots,
});
}