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

[DONT_MERGE] Rocksdb storage #527

Open
wants to merge 84 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
daa4766
wip
mafintosh May 31, 2024
5f42215
`rocksdb` : add byte helpers and use atomic batches (#526)
chm-diederichs Jun 21, 2024
6067dd5
force latest for now
mafintosh Jun 21, 2024
45e471d
addTreeNode renamed to putTreeNode
chm-diederichs Jun 21, 2024
f2d6765
remove batch id from api (#530)
chm-diederichs Jun 26, 2024
b94ce17
rocksdb: move bitfield to db (#529)
chm-diederichs Jun 26, 2024
e4ea11a
teardown storage in test helpers
chm-diederichs Jun 26, 2024
0134961
rocksdb: remove oplog and add block store API (#531)
chm-diederichs Jun 27, 2024
8c006e4
rocksdb: update index.js (#533)
chm-diederichs Jun 28, 2024
b6f4396
teardown cores implicitly in tests
mafintosh Jun 28, 2024
822c56f
keep core.header.tree around for compat (#532)
chm-diederichs Jun 28, 2024
452e604
rocksdb: implement full API apart from batches (#537)
chm-diederichs Jul 30, 2024
2016ec0
fixup typos
chm-diederichs Jul 30, 2024
596b0db
Merge branch 'main' into rocksdb
chm-diederichs Jul 30, 2024
342c1df
rocksdb: copy prologue (#549)
chm-diederichs Aug 2, 2024
8c5feb9
rocksdb: add named sessions (#539)
chm-diederichs Sep 5, 2024
3cfc609
Get skips bitfield check (#559)
mafintosh Sep 6, 2024
cade0cd
rocksdb: add db snapshots (#562)
chm-diederichs Sep 7, 2024
16d2d26
remove the cache, we do not use anymore (#560)
mafintosh Sep 7, 2024
6c213d7
add test for reopening writable core
chm-diederichs Sep 10, 2024
5516503
`rocksdb`: merge main (#564)
chm-diederichs Sep 11, 2024
79e9236
always close state first
chm-diederichs Sep 11, 2024
6a6ae78
pass opts to default storage
chm-diederichs Sep 17, 2024
8e22446
Fix activity count (#568)
mafintosh Sep 16, 2024
0e49149
`rocksdb`: avoid duplicate block download (#569)
chm-diederichs Sep 17, 2024
32b6b13
get user data from state storage
chm-diederichs Sep 17, 2024
af0e7dc
tree may have been updated beneath us
chm-diederichs Sep 17, 2024
0e555c9
fix reorg and teardown active batches
mafintosh Sep 23, 2024
cb077a8
initial support for memory overlays (#563)
mafintosh Sep 23, 2024
d9de824
dont use storage helpers
mafintosh Sep 23, 2024
edd504c
use latest storage
mafintosh Sep 24, 2024
27e28dd
internalise snapshots
mafintosh Sep 24, 2024
d8a78e9
remove all memory view branches
mafintosh Sep 24, 2024
f946075
rocksdb: add force close option (#570)
chm-diederichs Sep 25, 2024
1f1aef8
peer checks replicator state before any db reads
chm-diederichs Sep 27, 2024
f9e95b1
revert previous commit
chm-diederichs Sep 27, 2024
e41ffbc
do not process any data after destroy
chm-diederichs Oct 1, 2024
b0b0e10
upstream fix from main
chm-diederichs Oct 1, 2024
f794da1
missing core update on upgrade
chm-diederichs Oct 2, 2024
2d68418
Merge branch 'main' into rocksdb
chm-diederichs Oct 7, 2024
0ddab83
use storage idle helper
chm-diederichs Oct 7, 2024
f4047e5
rocksdb: only create single read batch in bg call (#578)
chm-diederichs Oct 7, 2024
1dd49ed
use minimum shared length (#577)
chm-diederichs Oct 7, 2024
338cb58
rocksdb: commit acquires lock (#576)
chm-diederichs Oct 9, 2024
4612792
Tweak session (#579)
mafintosh Oct 11, 2024
f94b9c3
make sure we release lock in check conflict
chm-diederichs Oct 11, 2024
e14587c
copyPrologue should not call core onupdate
chm-diederichs Oct 11, 2024
d2756bf
tree should never be null (#584)
mafintosh Oct 16, 2024
ec2e359
rename to hypercore storage (#582)
chm-diederichs Oct 16, 2024
3441a10
Add failing test (#585)
mafintosh Oct 16, 2024
234d7f0
Rocksdb copy prologue \o/ (#586)
mafintosh Oct 18, 2024
cc0d0b7
rocksdb: check shared tree length for new sessions (#588)
chm-diederichs Oct 22, 2024
e7bb078
length is required in tree opening (#589)
mafintosh Oct 22, 2024
b30857e
rename peak to peek
chm-diederichs Oct 26, 2024
de5520e
Merge branch 'main' into rocksdb
mafintosh Nov 11, 2024
161a69d
Sync constructor (#591)
mafintosh Nov 12, 2024
280f667
rocksdb: move `MemoryOverlay` to storage module (#580)
chm-diederichs Nov 12, 2024
afe2a32
rocksdb: commit overwrites full state (#587)
chm-diederichs Nov 12, 2024
17b42d8
rocksdb: update bitfield setRange API (#593)
chm-diederichs Nov 12, 2024
c0737d0
bitfield doesnt peek anymore (#595)
mafintosh Nov 12, 2024
912bc03
fix bitfield page fill typo
chm-diederichs Nov 15, 2024
a93a5da
Merge branch 'main' into rocksdb
mafintosh Nov 18, 2024
f096630
Fix events on rocks (#596)
mafintosh Nov 18, 2024
4a9e874
Merge branch 'main' into rocksdb
mafintosh Nov 18, 2024
9250a35
missed one
mafintosh Nov 18, 2024
83a5fe8
Merge branch 'main' into rocksdb
mafintosh Nov 26, 2024
64281f8
rocksdb: decouple session state from bitfield (#601)
chm-diederichs Nov 27, 2024
0e6d47f
rocksdb: support memory views (#602)
chm-diederichs Nov 27, 2024
147478e
some missing idle checks
mafintosh Nov 27, 2024
68a24d5
add weak session support (#603)
mafintosh Nov 27, 2024
9d2129f
fix dbl idle on destroy and weak session list
mafintosh Nov 28, 2024
3804228
Expose user data stream from lib/core.js (#604)
chm-diederichs Nov 28, 2024
7c4fee8
Add snapshotFrom method (#607)
chm-diederichs Nov 29, 2024
55ca149
allow root session to define session list (#608)
mafintosh Nov 29, 2024
74323aa
add ongc hook
mafintosh Dec 2, 2024
13bc2d4
also set gc handler when making sessions
mafintosh Dec 2, 2024
6c11bab
rocksdb: use storage API for updating dependency (#610)
chm-diederichs Dec 2, 2024
4cf91e5
inherit preload promise in sessions (#606)
mafintosh Dec 3, 2024
f7d0159
inherit more from parent (#612)
mafintosh Dec 4, 2024
236e584
rocksdb: minor tweaks (#613)
chm-diederichs Dec 5, 2024
8905a0e
rocksdb: SessionState.moveTo API (#609)
chm-diederichs Dec 13, 2024
dea4c32
rename flushedLength to signedLength (#614)
chm-diederichs Dec 13, 2024
82f2a29
rocksdb: atomic operations (#615)
chm-diederichs Dec 19, 2024
ca7b9f0
fix test
mafintosh Dec 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
809 changes: 366 additions & 443 deletions index.js

Large diffs are not rendered by default.

74 changes: 33 additions & 41 deletions lib/audit.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
const hypercoreCrypto = require('hypercore-crypto')
const flat = require('flat-tree')
const c = require('compact-encoding')
const b4a = require('b4a')

const empty = b4a.alloc(32)
const BitInterlude = require('./bit-interlude')

// this is optimised for speed over mem atm
// can be tweaked in the future

module.exports = async function auditCore (core) {
module.exports = async function auditCore (core, storage) {
const corrections = {
tree: 0,
blocks: 0
}

const length = core.header.tree.length

const data = await readFullStorage(core.blocks.storage)
const tree = await readFullStorage(core.tree.storage)
const bitfield = new BitInterlude()

const data = await readAllBlocks(core.storage)
const tree = await readAllTreeNodes(core.tree.storage)

const valid = new Uint8Array(Math.ceil(tree.byteLength / 40))
const stack = []
Expand All @@ -32,8 +33,8 @@ module.exports = async function auditCore (core) {
if ((node.index & 1) === 0) continue

const [left, right] = flat.children(node.index)
const leftNode = getNode(left)
const rightNode = getNode(right)
const leftNode = tree.get(left)
const rightNode = tree.get(right)

if (!rightNode && !leftNode) continue

Expand All @@ -48,12 +49,8 @@ module.exports = async function auditCore (core) {
}
}

if (leftNode.size) clearNode(leftNode)
if (rightNode.size) clearNode(rightNode)
}

if (corrections.tree) {
core.tree.cache.clear()
if (leftNode.size) clearNode(left)
if (rightNode.size) clearNode(right)
}

let i = 0
Expand All @@ -73,57 +70,52 @@ module.exports = async function auditCore (core) {
try {
nextOffset = await core.tree.byteOffset(i * 2)
} catch {
core._setBitfield(i, false)
storage.deleteBlock(i)
bitfield.set(i, false)
corrections.blocks++
i++
continue
}
}

const node = getNode(i * 2)
const blk = data.subarray(nextOffset, nextOffset + node.size)
const node = tree.get(i * 2)
const blk = data.get(i)
const hash = hypercoreCrypto.data(blk)

nextOffset += blk.byteLength

if (!b4a.equals(hash, node.hash)) {
core._setBitfield(i, false)
storage.deleteBlock(i)
bitfield.set(i, false)
corrections.blocks++
}

i++
}

return corrections
bitfield.flush(storage, core.bitfield)

function getNode (index) {
if (index * 40 + 40 > tree.byteLength) return null
const state = { start: index * 40, end: index * 40 + 40, buffer: tree }
const size = c.uint64.decode(state)
const hash = c.fixed32.decode(state)
if (size === 0 && hash.equals(empty)) return null
return { index, size, hash }
}
return corrections

function clearNode (node) {
valid[node.index] = 0
storage.deleteTreeNode(node.index)
corrections.tree++
}
}

if (node.size) {
b4a.fill(tree, 0, node.index * 40, node.index * 40 + 40)
core.tree.unflushed.set(node.index, core.tree.blankNode(node.index))
corrections.tree++
}
async function readAllBlocks (storage) {
const data = new Map()
for await (const block of storage.createBlockStream()) {
data.set(block.index, block.value)
}
return data
}

function readFullStorage (storage) {
return new Promise((resolve, reject) => {
storage.stat((_, st) => {
if (!st) return resolve(b4a.alloc(0))
storage.read(0, st.size, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
})
async function readAllTreeNodes (storage) {
const nodes = new Map()
for await (const node of storage.createTreeNodeStream()) {
nodes.set(node.index, node)
}
return nodes
}
Loading
Loading