Skip to content

Commit

Permalink
chore: last minute cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Mar 10, 2023
1 parent 052b00f commit 45df6b2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/SwingSet/src/kernel/state/kernelKeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ export default function makeKernelKeeper(kernelStorage, kernelSlog) {
const promisePrefix = `${vatID}.c.p`;
const kernelPromisesToReject = [];

vatKeeper.removeSnapshotAndTranscript();
vatKeeper.deleteSnapshotsAndTranscript();

// Note: ASCII order is "+,-./", and we rely upon this to split the
// keyspace into the various o+NN/o-NN/etc spaces. If we were using a
Expand Down
17 changes: 7 additions & 10 deletions packages/SwingSet/src/kernel/state/vatKeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,19 +542,17 @@ export function makeVatKeeper(
return true;
}

function deleteSnapshots() {
function deleteSnapshotsAndTranscript() {
if (snapStore) {
snapStore.deleteVatSnapshots(vatID);
}
}

function removeSnapshotAndTranscript() {
deleteSnapshots();
transcriptStore.deleteVatTranscripts(vatID);
}

function removeSnapshotAndResetTranscript() {
deleteSnapshots();
function dropSnapshotAndResetTranscript() {
if (snapStore) {
snapStore.stopUsingLastSnapshot(vatID);
}
transcriptStore.rolloverSpan(vatID);
}

Expand Down Expand Up @@ -627,9 +625,8 @@ export function makeVatKeeper(
vatStats,
dumpState,
saveSnapshot,
deleteSnapshots,
getSnapshotInfo,
removeSnapshotAndTranscript,
removeSnapshotAndResetTranscript,
deleteSnapshotsAndTranscript,
dropSnapshotAndResetTranscript,
});
}
2 changes: 1 addition & 1 deletion packages/SwingSet/src/kernel/vat-warehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export function makeVatWarehouse(kernelKeeper, vatLoader, policyOptions) {
async function resetWorker(vatID) {
await evict(vatID);
const vatKeeper = kernelKeeper.provideVatKeeper(vatID);
vatKeeper.removeSnapshotAndResetTranscript();
vatKeeper.dropSnapshotAndResetTranscript();
}

/**
Expand Down
42 changes: 23 additions & 19 deletions packages/swing-store/src/snapStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { fsStreamReady } from '@agoric/internal/src/fs-stream.js';
* saveSnapshot: (vatID: string, endPos: number, saveRaw: (filePath: string) => Promise<void>) => Promise<SnapshotResult>,
* deleteAllUnusedSnapshots: () => void,
* deleteVatSnapshots: (vatID: string) => void,
* stopUsingLastSnapshot: (vatID: string) => void,
* getSnapshotInfo: (vatID: string) => SnapshotInfo,
* }} SnapStore
*
Expand Down Expand Up @@ -179,9 +180,22 @@ export function makeSnapStore(
WHERE inUse = 1 AND vatID = ?
`);

function stopUsingLastSnapshot(vatID) {
const oldInfo = sqlGetPriorSnapshotInfo.get(vatID);
if (oldInfo) {
const rec = snapshotRec(vatID, oldInfo.endPos, oldInfo.hash, 0);
noteExport(snapshotMetadataKey(rec), JSON.stringify(rec));
if (keepSnapshots) {
sqlStopUsingLastSnapshot.run(vatID);
} else {
sqlClearLastSnapshot.run(vatID);
}
}
}

const sqlSaveSnapshot = db.prepare(`
INSERT OR REPLACE INTO snapshots
(vatID, endPos, hash, uncompressedSize, compressedSize, compressedSnapshot, inUse)
(vatID, endPos, inUse, hash, uncompressedSize, compressedSize, compressedSnapshot)
VALUES (?, ?, ?, ?, ?, ?, ?)
`);

Expand Down Expand Up @@ -229,25 +243,16 @@ export function makeSnapStore(
await finished(snapReader);

const h = hashStream.digest('hex');
const oldInfo = sqlGetPriorSnapshotInfo.get(vatID);
if (oldInfo) {
const rec = snapshotRec(vatID, oldInfo.endPos, oldInfo.hash, 0);
noteExport(snapshotMetadataKey(rec), JSON.stringify(rec));
if (keepSnapshots) {
sqlStopUsingLastSnapshot.run(vatID);
} else {
sqlClearLastSnapshot.run(vatID);
}
}
stopUsingLastSnapshot(vatID);
compressedSize = compressedSnapshot.length;
sqlSaveSnapshot.run(
vatID,
endPos,
1,
h,
uncompressedSize,
compressedSize,
compressedSnapshot,
1,
);
const rec = snapshotRec(vatID, endPos, h, 1);
const exportKey = snapshotMetadataKey(rec);
Expand Down Expand Up @@ -318,9 +323,7 @@ export function makeSnapStore(
const sqlLoadSnapshot = db.prepare(`
SELECT hash, compressedSnapshot
FROM snapshots
WHERE vatID = ?
ORDER BY endPos DESC
LIMIT 1
WHERE vatID = ? AND inUse = 1
`);

/**
Expand All @@ -337,6 +340,7 @@ export function makeSnapStore(
const loadInfo = sqlLoadSnapshot.get(vatID);
loadInfo || Fail`no snapshot available for vat ${q(vatID)}`;
const { hash, compressedSnapshot } = loadInfo;
compressedSnapshot || Fail`no snapshot available for vat ${q(vatID)}`;
const gzReader = Readable.from(compressedSnapshot);
cleanup.push(() => gzReader.destroy());
const snapReader = gzReader.pipe(createGunzip());
Expand Down Expand Up @@ -383,6 +387,7 @@ export function makeSnapStore(
SELECT endPos
FROM snapshots
WHERE vatID = ?
ORDER BY endPos
`);

/**
Expand All @@ -402,9 +407,7 @@ export function makeSnapStore(
const sqlGetSnapshotInfo = db.prepare(`
SELECT endPos, hash, uncompressedSize, compressedSize
FROM snapshots
WHERE vatID = ?
ORDER BY endPos DESC
LIMIT 1
WHERE vatID = ? AND inUse = 1
`);

/**
Expand Down Expand Up @@ -549,11 +552,11 @@ export function makeSnapStore(
sqlSaveSnapshot.run(
vatID,
endPos,
info.inUse,
info.hash,
size,
compressedArtifact.length,
compressedArtifact,
info.inUse,
);
}

Expand Down Expand Up @@ -595,6 +598,7 @@ export function makeSnapStore(
loadSnapshot,
deleteAllUnusedSnapshots,
deleteVatSnapshots,
stopUsingLastSnapshot,
getSnapshotInfo,
getExportRecords,
getArtifactNames,
Expand Down
1 change: 1 addition & 0 deletions packages/swing-store/src/swingStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ function makeSwingStore(dirPath, forceReset, options = {}) {
saveSnapshot: snapStore.saveSnapshot,
deleteAllUnusedSnapshots: snapStore.deleteAllUnusedSnapshots,
deleteVatSnapshots: snapStore.deleteVatSnapshots,
stopUsingLastSnapshot: snapStore.stopUsingLastSnapshot,
getSnapshotInfo: snapStore.getSnapshotInfo,
};

Expand Down
1 change: 1 addition & 0 deletions packages/swing-store/src/transcriptStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export function makeTranscriptStore(
SELECT startPos
FROM transcriptSpans
WHERE vatID = ?
ORDER BY startPos
`);
sqlGetVatSpans.pluck(true);

Expand Down

0 comments on commit 45df6b2

Please sign in to comment.