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

Add peer.remoteContiguousLength #404

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 16 additions & 1 deletion lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ class Peer {
replicator._ifAvailable++
}

get remoteContiguousLength () {
return this.remoteBitfield.findFirst(false, 0)
}

getMaxInflight () {
const stream = this.stream.rawStream
if (!stream.udx) return Math.min(this.inflightRange[1], this.inflightRange[0] * 3)
Expand Down Expand Up @@ -681,7 +685,16 @@ class Peer {
onrange ({ drop, start, length }) {
const has = drop === false

this.remoteBitfield.setRange(start, length, has)
if (length === 1) {
this.remoteBitfield.setRange(start, length, has)
} else {
const rangeStart = this.remoteBitfield.findFirst(!has, start)
const rangeLength = length - (rangeStart - start)

if (rangeLength > 0) {
this.remoteBitfield.setRange(rangeStart, rangeLength, has)
}
}

if (drop === false) this._update()
}
Expand Down Expand Up @@ -912,6 +925,8 @@ class Peer {
}

_maybeWant (start, length = 1) {
if (start + length <= this.remoteContiguousLength) return

let i = Math.floor(start / DEFAULT_SEGMENT_SIZE)
const n = Math.ceil((start + length) / DEFAULT_SEGMENT_SIZE)

Expand Down
68 changes: 68 additions & 0 deletions test/remote-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const test = require('brittle')
const { create, replicate } = require('./helpers')

test('basic peer remote contiguous length', async function (t) {
t.plan(2)

const a = await create()
const b = await create(a.key)

replicate(a, b, t)

await a.append('a')
await b.get(0)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 1)

await a.append('b')
await b.get(1)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 2)
})

test('peer remote contiguous length', async function (t) {
t.plan(3)

const a = await create()
const b = await create(a.key)

replicate(a, b, t)

await a.append('a')
await b.get(0)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 1)

await a.append(['d', 'e'])
await b.get(2)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 1)

await b.get(1)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 3)
})

test('peer truncates the remote contiguous length', async function (t) {
t.plan(2)

const a = await create()
const b = await create(a.key)

replicate(a, b, t)

await a.append(['a', 'b'])
await b.get(0)
await b.get(1)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 2)

await a.truncate(1)
await new Promise(resolve => setTimeout(resolve, 100))
t.is(getPeer(a, b).remoteContiguousLength, 1)
})

// "A" wants to know if "B" is finished syncing, so find the corresponding peer
function getPeer (a, b) {
return a.replicator.peers.find(peer => peer.remotePublicKey.equals(b.replicator.peers[0].stream.publicKey))
}