Skip to content

Commit

Permalink
feat(swingset): configurable snapshotPruneInterval (=20)
Browse files Browse the repository at this point in the history
Pruning snapshots involves an O(n) query on the DB, so
doing it on every crank seems expensive.

refs #3374
  • Loading branch information
dckc committed Jul 21, 2021
1 parent c792d81 commit 44b2693
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/SwingSet/src/kernel/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ export default function buildKernel(
kernelKeeper.incrementCrankNumber();
if (snapStore) {
// eslint-disable-next-line no-use-before-define
vatWarehouse.pruneSnapshots(snapStore);
vatWarehouse.pruneSnapshots(snapStore, kernelKeeper.getCrankNumber());
}
} finally {
processQueueRunning = undefined;
Expand Down
13 changes: 11 additions & 2 deletions packages/SwingSet/src/kernel/vatManager/vat-warehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const makeLRU = max => {
* maxVatsOnline?: number,
* snapshotInitial?: number,
* snapshotInterval?: number,
* snapshotPruneInverval?: number,
* }=} policyOptions
*
* @typedef {(syscall: VatSyscallObject) => ['error', string] | ['ok', null] | ['ok', Capdata]} VatSyscallHandler
Expand All @@ -66,6 +67,7 @@ export function makeVatWarehouse(kernelKeeper, vatLoader, policyOptions) {
// Note: some measurements show 10 deliveries per sec on XS
// as of this writing.
snapshotInterval = 200,
snapshotPruneInterval = 20,
} = policyOptions || {};
// Idea: snapshot based on delivery size: after deliveries >10Kb.
// console.debug('makeVatWarehouse', { policyOptions });
Expand Down Expand Up @@ -302,16 +304,23 @@ export function makeVatWarehouse(kernelKeeper, vatLoader, policyOptions) {
return true;
}

const snapshotPruneIntervalBI = BigInt(snapshotPruneInterval);

/**
* Delete unused snapshots.
* Delete unused snapshots periodically.
*
* WARNING: caller is responsible to call this only
* when all records of snapshot consumers are durably
* stored; for example, after commitCrank().
*
* @param { SnapStore } snapStore
* @param { bigint } crankNumber
*/
function pruneSnapshots(snapStore) {
function pruneSnapshots(snapStore, crankNumber) {
if (crankNumber % snapshotPruneIntervalBI !== 0n) {
return;
}

const todo = kernelKeeper.getUnusedSnapshots();
const done = [];
for (const snapshotID of todo) {
Expand Down
6 changes: 5 additions & 1 deletion packages/SwingSet/test/vat-warehouse/test-warehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ test('snapshot after deliveries', async t => {
const hostStorage = { snapStore, ...provideHostStorage() };
const c = await makeController('xs-worker', {
hostStorage,
warehousePolicy: { maxVatsOnline, snapshotInterval: 1 },
warehousePolicy: {
maxVatsOnline,
snapshotInterval: 1,
snapshotPruneInterval: 1,
},
});
t.teardown(c.shutdown);

Expand Down

0 comments on commit 44b2693

Please sign in to comment.