Skip to content

Commit

Permalink
refactor(swingset): move crankbuffer/commit/abort inside kernelKeeper
Browse files Browse the repository at this point in the history
The host application (which uses swingset as a library) is obligated to
provide a "hostStorage" object, which is a trio of kvStore, streamStore, and
snapStore. The host typically uses openLMDBSwingStore() to create this, and
holds onto the "commit block" facet.

Previously the kernelKeeper was built on top of the streamStore, the
snapStore, and an "enhancedCrankBuffer", which was the host-provided
key-value store plus an abortable/committable "crank buffer", plus some extra
convenience methods. The kernel held the abort/commit facet. The kernel was
responsible for extracting the raw kvStore from `hostStorage`, wrapping it,
and passing the enhanced version into `makeKernelKeeper` along with the other
two values.

Instead, we now give the entire "hostStorage" object to `makeKernelKeeper`,
and give the kernel keeper responsibility for doing the wrapping. When the
kernel wants to commit or abort, it calls `kernelKeeper.abortCrank()` or
`commitCrank()`.

This simplifies the `makeKernelKeeper` construction and will provide a place
for `commitCrank` to store a hash of kernel activity.

refs #3442
  • Loading branch information
warner committed Aug 12, 2021
1 parent a277ae6 commit 3b860fa
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 118 deletions.
12 changes: 4 additions & 8 deletions packages/SwingSet/src/kernel/initializeKernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { assertKnownOptions } from '../assertOptions.js';
import { insistVatID } from './id.js';
import { makeVatSlot } from '../parseVatSlots.js';
import { insistStorageAPI } from '../storageAPI.js';
import { wrapStorage } from './state/storageWrapper.js';
import makeKernelKeeper from './state/kernelKeeper.js';
import { exportRootObject, doQueueToKref } from './kernel.js';

Expand All @@ -16,12 +15,9 @@ function makeVatRootObjectSlot() {

export function initializeKernel(config, hostStorage, verbose = false) {
const logStartup = verbose ? console.debug : () => 0;
insistStorageAPI(hostStorage.kvStore);

const { kvStore, streamStore } = hostStorage;
insistStorageAPI(kvStore);
const { enhancedCrankBuffer, commitCrank } = wrapStorage(kvStore);

const kernelKeeper = makeKernelKeeper(enhancedCrankBuffer, streamStore);
const kernelKeeper = makeKernelKeeper(hostStorage);

const wasInitialized = kernelKeeper.getInitialized();
assert(!wasInitialized);
Expand Down Expand Up @@ -86,7 +82,7 @@ export function initializeKernel(config, hostStorage, verbose = false) {
const kref = exportRootObject(kernelKeeper, vatID);
// Pin, to prevent it being GC'd when only the kvStore points to it
kernelKeeper.pinObject(kref);
kvStore.set('vatAdminRootKref', kref);
kernelKeeper.kvStore.set('vatAdminRootKref', kref);
gotVatAdminRootKref = true;
}
}
Expand Down Expand Up @@ -135,7 +131,7 @@ export function initializeKernel(config, hostStorage, verbose = false) {
}
kernelKeeper.setInitialized();
kernelKeeper.saveStats();
commitCrank(); // commit initialized kernel state as crank #0
kernelKeeper.commitCrank(); // commit initialized kernel state as crank #0
return bootstrapResultKpid;

// ----------------------------------------------------------------------
Expand Down
23 changes: 4 additions & 19 deletions packages/SwingSet/src/kernel/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import { foreverPolicy } from '../runPolicies.js';
import { makeVatManagerFactory } from './vatManager/factory.js';
import { makeVatWarehouse } from './vatManager/vat-warehouse.js';
import makeDeviceManager from './deviceManager.js';
import { wrapStorage } from './state/storageWrapper.js';
import makeKernelKeeper from './state/kernelKeeper.js';
import { kdebug, kdebugEnable, legibilizeMessageArgs } from './kdebug.js';
import { insistKernelType, parseKernelSlot } from './parseKernelSlots.js';
import { parseVatSlot } from '../parseVatSlots.js';
import { insistStorageAPI } from '../storageAPI.js';
import { insistCapData } from '../capdata.js';
import { insistMessage, insistVatDeliveryResult } from '../message.js';
import { insistDeviceID, insistVatID } from './id.js';
Expand Down Expand Up @@ -138,25 +136,13 @@ export default function buildKernel(
} = kernelOptions;
const logStartup = verbose ? console.debug : () => 0;

const {
kvStore,
streamStore,
snapStore,
} = /** @type { HostStore } */ (hostStorage);
insistStorageAPI(kvStore);
const { enhancedCrankBuffer, abortCrank, commitCrank } = wrapStorage(kvStore);
const vatAdminRootKref = kvStore.get('vatAdminRootKref');
const vatAdminRootKref = hostStorage.kvStore.get('vatAdminRootKref');

const kernelSlog = writeSlogObject
? makeSlogger(slogCallbacks, writeSlogObject)
: makeDummySlogger(slogCallbacks, makeConsole);

const kernelKeeper = makeKernelKeeper(
enhancedCrankBuffer,
streamStore,
kernelSlog,
snapStore,
);
const kernelKeeper = makeKernelKeeper(hostStorage, kernelSlog);

let started = false;

Expand Down Expand Up @@ -261,7 +247,6 @@ export default function buildKernel(

const kernelSyscallHandler = makeKernelSyscallHandler({
kernelKeeper,
kvStore: enhancedCrankBuffer,
ephemeral,
// eslint-disable-next-line no-use-before-define
notify,
Expand Down Expand Up @@ -792,7 +777,7 @@ export default function buildKernel(
const { vatID, shouldReject, info } = terminationTrigger;
if (terminationTrigger.shouldAbortCrank) {
// errors unwind any changes the vat made
abortCrank();
kernelKeeper.abortCrank();
didAbort = true;
// but metering deductions and underflow notifications must survive
const { meterDeductions } = postAbortActions;
Expand All @@ -813,7 +798,7 @@ export default function buildKernel(
}
kernelKeeper.processRefcounts();
kernelKeeper.saveStats();
commitCrank();
kernelKeeper.commitCrank();
kernelKeeper.incrementCrankNumber();
} finally {
processQueueRunning = undefined;
Expand Down
3 changes: 2 additions & 1 deletion packages/SwingSet/src/kernel/kernelSyscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ export function doSend(kernelKeeper, target, msg) {
export function makeKernelSyscallHandler(tools) {
const {
kernelKeeper,
kvStore,
ephemeral,
notify,
doResolve,
setTerminationTrigger,
} = tools;

const { kvStore } = kernelKeeper;

function send(target, msg) {
return doSend(kernelKeeper, target, msg);
}
Expand Down
29 changes: 19 additions & 10 deletions packages/SwingSet/src/kernel/state/kernelKeeper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// @ts-check
import { Nat } from '@agoric/nat';
import { assert, details as X } from '@agoric/assert';
import { wrapStorage } from './storageWrapper.js';
import { initializeVatState, makeVatKeeper } from './vatKeeper.js';
import { initializeDeviceState, makeDeviceKeeper } from './deviceKeeper.js';
import { parseReachableAndVatSlot } from './reachable.js';
import { insistEnhancedStorageAPI } from '../../storageAPI.js';
import {
insistStorageAPI,
insistEnhancedStorageAPI,
} from '../../storageAPI.js';
import {
insistKernelType,
makeKernelSlot,
Expand Down Expand Up @@ -125,18 +129,19 @@ const FIRST_CRANK_NUMBER = 0n;
const FIRST_METER_ID = 1n;

/**
* @param {KVStorePlus} kvStore
* @param {StreamStore} streamStore
* @param {HostStore} hostStorage
* @param {KernelSlog} kernelSlog
* @param {SnapStore=} snapStore
*/
export default function makeKernelKeeper(
kvStore,
streamStore,
kernelSlog,
snapStore = undefined,
) {
export default function makeKernelKeeper(hostStorage, kernelSlog) {
// the kernelKeeper wraps the host's raw key-value store in a crank buffer
const rawKVStore = hostStorage.kvStore;
insistStorageAPI(rawKVStore);

const { abortCrank, commitCrank, enhancedCrankBuffer: kvStore } = wrapStorage(
rawKVStore,
);
insistEnhancedStorageAPI(kvStore);
const { streamStore, snapStore } = hostStorage;

/**
* @param {string} key
Expand Down Expand Up @@ -1329,6 +1334,10 @@ export default function makeKernelKeeper(
allocateDeviceIDForNameIfNeeded,
allocateDeviceKeeperIfNeeded,

kvStore,
abortCrank,
commitCrank,

dump,
});
}
9 changes: 3 additions & 6 deletions packages/SwingSet/test/test-clist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { test } from '../tools/prepare-test-env-ava.js';
import { initSimpleSwingStore } from '@agoric/swing-store-simple';
import { makeDummySlogger } from '../src/kernel/slogger.js';
import makeKernelKeeper from '../src/kernel/state/kernelKeeper.js';
import { wrapStorage } from '../src/kernel/state/storageWrapper.js';

test(`clist reachability`, async t => {
const slog = makeDummySlogger({});
const hostStorage = initSimpleSwingStore();
const { enhancedCrankBuffer: s } = wrapStorage(hostStorage.kvStore);

const kk = makeKernelKeeper(s, hostStorage.streamStore, slog);
const kk = makeKernelKeeper(hostStorage, slog);
const s = kk.kvStore;
kk.createStartingKernelState('local');
const vatID = kk.allocateUnusedVatID();
const vk = kk.provideVatKeeper(vatID);
Expand Down Expand Up @@ -95,9 +93,8 @@ test(`clist reachability`, async t => {
test('getImporters', async t => {
const slog = makeDummySlogger({});
const hostStorage = initSimpleSwingStore();
const { enhancedCrankBuffer: s } = wrapStorage(hostStorage.kvStore);
const kk = makeKernelKeeper(hostStorage, slog);

const kk = makeKernelKeeper(s, hostStorage.streamStore, slog);
kk.createStartingKernelState('local');
const vatID1 = kk.allocateUnusedVatID();
kk.addDynamicVatID(vatID1);
Expand Down
Loading

0 comments on commit 3b860fa

Please sign in to comment.