Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert swingstore from LMDB to Sqlite, phase 1 #6561

Merged
merged 11 commits into from
Dec 23, 2022
5 changes: 3 additions & 2 deletions packages/SwingSet/misc-tools/db-delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
import 'lmdb';
import '@endo/init';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store';
Expand Down Expand Up @@ -47,7 +46,9 @@ async function run() {
const stateDBDir = argv.shift();
const key = argv.shift();

const { kvStore, commit } = openSwingStore(stateDBDir);
const { kernelStorage, hostStorage } = openSwingStore(stateDBDir);
const { kvStore } = kernelStorage;
const { commit } = hostStorage;

if (range) {
for (const k of kvStore.getKeys(`${key}.`, `${key}/`)) {
Expand Down
3 changes: 1 addition & 2 deletions packages/SwingSet/misc-tools/db-dump.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable */
import 'lmdb';
import '@endo/init';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store';
Expand Down Expand Up @@ -41,7 +40,7 @@ function run() {
}
const stateDBDir = argv.shift();

const { kvStore } = openSwingStore(stateDBDir);
const { kvStore } = openSwingStore(stateDBDir).kernelStorage;

// we know all keys start with letters, so '@' is before all keys, and '{'
// is after all keys
Expand Down
3 changes: 1 addition & 2 deletions packages/SwingSet/misc-tools/db-get.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
import 'lmdb';
import '@endo/init';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store';
Expand Down Expand Up @@ -58,7 +57,7 @@ function run() {
const stateDBDir = argv.shift();
const key = argv.shift();

const { kvStore } = openSwingStore(stateDBDir);
const { kvStore } = openSwingStore(stateDBDir).kernelStorage;

function pkv(k) {
const value = kvStore.get(k);
Expand Down
7 changes: 3 additions & 4 deletions packages/SwingSet/misc-tools/db-set.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
import 'lmdb';
import '@endo/init';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store';
Expand Down Expand Up @@ -33,10 +32,10 @@ async function run() {
const key = argv.shift();
const value = argv.shift();

const { kvStore, commit } = openSwingStore(stateDBDir);
const { kernelStorage, hostStorage } = openSwingStore(stateDBDir);

kvStore.set(key, value);
await commit();
kernelStorage.kvStore.set(key, value);
await hostStorage.commit();
}

run().then(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'lmdb';
import '@endo/init';
import process from 'process';
import fs from 'fs';
Expand All @@ -19,7 +18,7 @@ if (!dirPath) {
if (!isSwingStore(dirPath)) {
throw Error(`${dirPath} does not appear to be a swingstore (no ./data.mdb)`);
}
const { kvStore, streamStore } = openSwingStore(dirPath);
const { kvStore, streamStore } = openSwingStore(dirPath).kernelStorage;
function get(key) {
return kvStore.get(key);
}
Expand Down
75 changes: 0 additions & 75 deletions packages/SwingSet/misc-tools/rekernelize.js

This file was deleted.

5 changes: 3 additions & 2 deletions packages/SwingSet/misc-tools/replace-bundle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node

import 'lmdb';
import '@endo/init';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store';
Expand Down Expand Up @@ -36,7 +35,9 @@ async function run() {
const stateDBDir = argv.shift();
const srcPath = argv.shift();

const { kvStore, commit } = openSwingStore(stateDBDir);
const { kernelStorage, hostStorage } = openSwingStore(stateDBDir);
const { kvStore } = kernelStorage;
const { commit } = hostStorage;
log(`will use ${srcPath} in ${stateDBDir} for ${bundleName}`);

if (bundleName === 'kernel') {
Expand Down
3 changes: 1 addition & 2 deletions packages/SwingSet/misc-tools/vat-stats.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable */
import 'lmdb';
import '@endo/init';
import process from 'process';
import { openSwingStore } from '@agoric/swing-store';
Expand Down Expand Up @@ -36,7 +35,7 @@ function fail(message, printUsage) {
function run() {
const argv = process.argv.slice(2);
const stateDBDir = argv.shift();
const { kvStore: kv } = openSwingStore(stateDBDir);
const { kvStore: kv } = openSwingStore(stateDBDir).kernelStorage;
const vatIDs = [];
const vats = {};
for (const name of JSON.parse(kv.get('vat.names'))) {
Expand Down
1 change: 0 additions & 1 deletion packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@endo/zip": "^0.2.28",
"anylogger": "^0.21.0",
"import-meta-resolve": "^1.1.1",
"lmdb": "^2.4.5",
"microtime": "^3.1.0",
"semver": "^6.3.0"
},
Expand Down
37 changes: 21 additions & 16 deletions packages/SwingSet/src/controller/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ import microtime from 'microtime';
import { assert, Fail } from '@agoric/assert';
import { importBundle } from '@endo/import-bundle';
import { xsnap, recordXSnap } from '@agoric/xsnap';
import { initSwingStore } from '@agoric/swing-store';

import { checkBundle } from '@endo/check-bundle/lite.js';
import { createSHA256 } from '../lib-nodejs/hasher.js';
import engineGC from '../lib-nodejs/engine-gc.js';
import { startSubprocessWorker } from '../lib-nodejs/spawnSubprocessWorker.js';
import { waitUntilQuiescent } from '../lib-nodejs/waitUntilQuiescent.js';
import { makeGcAndFinalize } from '../lib-nodejs/gc-and-finalize.js';
import { kslot } from '../lib/kmarshal.js';
import { insistStorageAPI } from '../lib/storageAPI.js';
import { provideHostStorage } from './hostStorage.js';
import {
buildKernelBundle,
swingsetIsInitialized,
Expand Down Expand Up @@ -159,7 +158,7 @@ export function makeStartXSnap(bundles, { snapStore, env, spawn }) {

/**
*
* @param {HostStore} hostStorage
* @param {SwingStoreKernelStorage} kernelStorage
* @param {Record<string, unknown>} deviceEndowments
* @param {{
* verbose?: boolean,
Expand All @@ -175,11 +174,11 @@ export function makeStartXSnap(bundles, { snapStore, env, spawn }) {
* }} runtimeOptions
*/
export async function makeSwingsetController(
hostStorage = provideHostStorage(),
kernelStorage = initSwingStore().kernelStorage,
deviceEndowments = {},
runtimeOptions = {},
) {
const kvStore = hostStorage.kvStore;
const kvStore = kernelStorage.kvStore;
insistStorageAPI(kvStore);

// Use ambient process.env only if caller did not specify.
Expand Down Expand Up @@ -293,14 +292,14 @@ export async function makeSwingsetController(
JSON.parse(kvStore.get('supervisorBundle')),
];
const startXSnap = makeStartXSnap(bundles, {
snapStore: hostStorage.snapStore,
snapStore: kernelStorage.snapStore,
env,
spawn,
});

const kernelEndowments = {
waitUntilQuiescent,
hostStorage,
kernelStorage,
debugPrefix,
vatEndowments,
makeConsole,
Expand All @@ -312,7 +311,6 @@ export async function makeSwingsetController(
WeakRef,
FinalizationRegistry,
gcAndFinalize: makeGcAndFinalize(engineGC),
createSHA256,
};

const kernelRuntimeOptions = {
Expand Down Expand Up @@ -420,7 +418,7 @@ export async function makeSwingsetController(
},

getActivityhash() {
return kernel.getActivityhash();
return kernelStorage.getActivityhash();
},

// everything beyond here is for tests, and everything should be migrated
Expand All @@ -434,6 +432,7 @@ export async function makeSwingsetController(
const vatID = kernel.vatNameToID(vatName);
const kref = kernel.getRootObject(vatID);
kernel.pinObject(kref);
kernelStorage.emitCrankHashes();
FUDCo marked this conversation as resolved.
Show resolved Hide resolved
return kref;
},

Expand All @@ -442,7 +441,10 @@ export async function makeSwingsetController(
},

kpResolution(kpid) {
return kernel.kpResolution(kpid);
const result = kernel.kpResolution(kpid);
// kpResolution does DB write (changes refcounts) so we need emitCrankHashes here
kernelStorage.emitCrankHashes();
FUDCo marked this conversation as resolved.
Show resolved Hide resolved
return result;
},
vatNameToID(vatName) {
return kernel.vatNameToID(vatName);
Expand All @@ -465,6 +467,7 @@ export async function makeSwingsetController(
if (kpid) {
kernel.kpRegisterInterest(kpid);
}
kernelStorage.emitCrankHashes();
return kpid;
},

Expand All @@ -477,12 +480,14 @@ export async function makeSwingsetController(
if (!options.upgradeMessage) {
options.upgradeMessage = `vat ${vatName} upgraded`;
}
return controller.queueToVatRoot(
const result = controller.queueToVatRoot(
FUDCo marked this conversation as resolved.
Show resolved Hide resolved
'vatAdmin',
'upgradeStaticVat',
[vatID, pauseTarget, bundleID, options],
'ignore',
);
// no emitCrankHashes here because queueToVatRoot did that
return result;
},
});

Expand All @@ -507,7 +512,7 @@ export async function makeSwingsetController(
* @param {SwingSetConfig} config
* @param {string[]} argv
* @param {{
* hostStorage?: HostStore;
* kernelStorage?: SwingStoreKernelStorage;
* env?: Record<string, string>;
* verbose?: boolean;
* kernelBundles?: Record<string, Bundle>;
Expand All @@ -525,7 +530,7 @@ export async function buildVatController(
runtimeOptions = {},
) {
const {
hostStorage = provideHostStorage(),
kernelStorage = initSwingStore().kernelStorage,
env,
verbose,
kernelBundles: kernelAndOtherBundles = {},
Expand All @@ -548,18 +553,18 @@ export async function buildVatController(
};
const initializationOptions = { verbose, kernelBundles };
let bootstrapResult;
if (!swingsetIsInitialized(hostStorage)) {
if (!swingsetIsInitialized(kernelStorage)) {
// eslint-disable-next-line @jessie.js/no-nested-await
bootstrapResult = await initializeSwingset(
config,
argv,
hostStorage,
kernelStorage,
initializationOptions,
runtimeOptions,
);
}
const controller = await makeSwingsetController(
hostStorage,
kernelStorage,
{},
actualRuntimeOptions,
);
Expand Down
23 changes: 0 additions & 23 deletions packages/SwingSet/src/controller/hostStorage.js

This file was deleted.

Loading