Skip to content

Commit

Permalink
introduce AutobaseHostCalls abstraction (#216)
Browse files Browse the repository at this point in the history
* introduce HostCalls abstraction

* remove test on private method

* _removeWriter is sync

* expose removeable from base

* increase blocks in simple ff test

* applyState is source of truth

* rename to AutobaseHostCalls
  • Loading branch information
chm-diederichs authored Feb 13, 2025
1 parent 742ca9a commit a3b190c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
28 changes: 8 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ module.exports = class Autobase extends ReadyResource {
: { key: null, indexedLength: 0, indexersUpdated: false, fastForwarding: false, heads: null }
}

interrupt (reason) {
_interrupt (reason) {
assert(this._applyState.applying, 'Interrupt is only allowed in apply')
this._interrupting = true
if (reason) this.interrupted = reason
Expand Down Expand Up @@ -1371,13 +1371,10 @@ module.exports = class Autobase extends ReadyResource {
}

// triggered from apply
async addWriter (key, { indexer = true, isIndexer = indexer } = {}) { // just compat for old version
async _addWriter (key, sys) { // just compat for old version
assert(this._applyState.applying, 'System changes are only allowed in apply')

const sys = this._applyState.system
await sys.add(key, { isIndexer })

const writer = (await this._getWriterByKey(key, -1, 0, false, true, null)) || this._makeWriter(key, 0, true, false)
const writer = (await this._getWriterByKey(key, -1, 0, false, true, sys)) || this._makeWriter(key, 0, true, false)
await writer.ready()

if (!this.activeWriters.has(key)) {
Expand All @@ -1390,27 +1387,18 @@ module.exports = class Autobase extends ReadyResource {
this._queueBump()
}

removeable (key, sys = this.system) {
if (sys.indexers.length !== 1) return true
return !b4a.equals(sys.indexers[0].key, key)
}

// triggered from apply
async removeWriter (key) { // just compat for old version
assert(this._applyState.applying, 'System changes are only allowed in apply')

if (!this.removeable(key, this._applyState.system)) {
throw new Error('Not allowed to remove the last indexer')
}

await this._applyState.system.remove(key)

_removeWriter (key) { // just compat for old version
const w = this.activeWriters.get(key)
if (w) w.isRemoved = true

this._queueBump()
}

removeable (key) {
return this._applyState ? this._applyState.removeable(key) : false
}

_updateAckThreshold () {
if (this._ackThreshold === 0) return
if (this._ackTimer) this._ackTimer.bau()
Expand Down
35 changes: 34 additions & 1 deletion lib/apply-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ const safetyCatch = require('safety-catch')
// must not be set too low (stretched with num of indexers)
const SHOULD_WRITE_THRESHOLD = 16

class AutobaseHostCalls {
constructor (state) {
this.state = state
this.base = state.base
this.system = state.system
}

async addWriter (key, { indexer = true, isIndexer = indexer } = {}) { // just compat for old version
await this.system.add(key, { isIndexer })
await this.base._addWriter(key, this.system)
}

async removeWriter (key) { // just compat for old version
if (!this.state.removeable(key)) {
throw new Error('Not allowed to remove the last indexer')
}

await this.system.remove(key)
this.base._removeWriter(key)
}

interrupt (reason) {
this.base._interrupt(reason)
}
}

class CheckpointCore {
constructor (view, core, signer) {
this.view = view
Expand Down Expand Up @@ -186,6 +212,7 @@ module.exports = class ApplyState extends ReadyResource {
this.view = null
this.views = []
this.systemRef = null
this.hostCalls = null

this.updates = []

Expand Down Expand Up @@ -255,6 +282,11 @@ module.exports = class ApplyState extends ReadyResource {
return false
}

removeable (key) {
if (this.system.indexers.length !== 1) return true
return !b4a.equals(this.system.indexers[0].key, key)
}

isLocalIndexer () {
return !!this.localCheckpoint
}
Expand Down Expand Up @@ -297,6 +329,7 @@ module.exports = class ApplyState extends ReadyResource {
this.indexersUpdated = boot.indexersUpdated
this.stretchedThreshold = this.system.indexers.length * SHOULD_WRITE_THRESHOLD
this.quorum = sysCore.manifest ? sysCore.manifest.quorum : 0
this.hostCalls = new AutobaseHostCalls(this)

const added = new Set()

Expand Down Expand Up @@ -769,7 +802,7 @@ module.exports = class ApplyState extends ReadyResource {

if (applyBatch.length && this.base._hasApply === true) {
this.applying = true
await this.base._handlers.apply(applyBatch, this.view, this.base)
await this.base._handlers.apply(applyBatch, this.view, this.hostCalls)
this.applying = false
}

Expand Down
1 change: 0 additions & 1 deletion test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ test('basic - throws', async t => {

await t.exception(b.append('not writable'))
await t.exception(a.view.append('append outside apply'))
await t.exception(() => a.addWriter(b.local.key))
})

test('basic - add 5 writers', async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/fast-forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('fast-forward - simple', async t => {

const [a, b] = bases

for (let i = 0; i < 200; i++) {
for (let i = 0; i < 1000; i++) {
await a.append('a' + i)
}

Expand Down

0 comments on commit a3b190c

Please sign in to comment.