Skip to content

Commit

Permalink
rocksdb: only create single read batch in bg call (#578)
Browse files Browse the repository at this point in the history
* only create single batch per bg call

* iterate over blocks as they were read

* catch error in case uncaught

* skip read if we do not have block
  • Loading branch information
chm-diederichs authored Oct 7, 2024
1 parent 0ddab83 commit f4047e5
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1830,26 +1830,19 @@ module.exports = class Replicator {
async _resolveBlocksLocally () {
// TODO: check if fork compat etc. Requires that we pass down truncation info

let clear = null
const clear = []
const blocks = []

const reader = this.core.storage.createReadBatch()
for (const b of this._blocks) {
if (this.core.bitfield.get(b.index) === false) continue

const reader = this.core.storage.createReadBatch()

try {
const block = this.core.blocks.get(reader, b.index)
reader.tryFlush()
b.resolve(await block)
} catch (err) {
b.reject(err)
}

if (clear === null) clear = []
clear.push(b)
blocks.push(this._resolveLocalBlock(b, reader, clear))
}
reader.tryFlush()

await Promise.all(blocks)

if (clear === null) return
if (!clear.length) return

// Currently the block tracker does not support deletes during iteration, so we make
// sure to clear them afterwards.
Expand All @@ -1858,6 +1851,17 @@ module.exports = class Replicator {
}
}

async _resolveLocalBlock (b, reader, resolved) {
try {
b.resolve(await this.core.blocks.get(reader, b.index))
} catch (err) {
b.reject(err)
return
}

resolved.push(b)
}

_resolveBlockRequest (tracker, index, value, req) {
const b = tracker.remove(index)
if (b === null) return false
Expand Down

0 comments on commit f4047e5

Please sign in to comment.