Skip to content
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
8 changes: 4 additions & 4 deletions js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type {
} from "./track.ts";
import { max, min } from "./util/key.ts";
import * as Varint from "./varint.ts";
import type { Holdings, Resolved, Window } from "./window.ts";
import type { Holdings, Resolved } from "./window.ts";
import type { Advertised, Advertisements } from "./wire.ts";

/** A null {@link Delay.max} in milliseconds, which the model holds as the largest value every peer can read. */
Expand Down Expand Up @@ -496,11 +496,11 @@ export interface ResumeSource {
*/
tryDrop(delivered: Delivered, passing?: bigint): (Recv & { kind: "drop" }) | undefined;
/**
* Whether the reader's bounded `window` has nothing left to deliver, judged by the serving
* copy against what it and the logical reader (`delivered`) handed out. A copy that may still
* Whether the reader's bounded window has nothing left to deliver, judged by the serving copy
* against what it and the logical reader (`delivered`) handed out. A copy that may still
* deliver keeps it open. Mirrors the Rust `Reader::exhausted`.
*/
exhausted(delivered: Delivered, window: Window): boolean;
exhausted(delivered: Delivered): boolean;
/** Wake once a read may go differently; fires at most once. */
changed(fn: () => void): Dispose;
/** Forget the gaps in `delivered` below each group past the budget, judged on the newest copy. */
Expand Down
138 changes: 137 additions & 1 deletion js/net/src/resume.model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ class Model {
if (!instance.ended && reader.ranges === undefined) {
throw fail(index, "the track ended cleanly though no route declared an end");
}
const covered = (group: number) => (instance.ended ? true : within(reader.ranges, group));
const covered = (group: number) => (instance.ended ? covers(reader, group) : within(reader.ranges, group));
reader.carried = new Set();
for (let group = 0; group < GROUPS; group++) {
if (covered(group) && instance.live().every((copy) => complete(copy, group))) reader.carried.add(group);
Expand Down Expand Up @@ -1190,6 +1190,142 @@ test("regressions", async () => {
],
finish: false,
},
// An ordered reader reads a cached group its local floor kept out once the floor is lowered:
// the read stays open while the floor keeps it out.
{
ops: [
{ kind: "join", reader: 3, spec: spec(true, "hour", [0, 1], false) },
{ kind: "bounds", reader: 3, range: [1, 2] },
{ kind: "write", instance: 0, slot: 0, group: undefined, frames: 1 },
{ kind: "bounds", reader: 3, range: undefined },
{ kind: "read", reader: 0 },
],
finish: false,
},
// A bounded read ends by the window its copies placed, so a group under the resume point that
// the local floor keeps out stays owed.
{
ops: [
{ kind: "join", reader: 2, spec: spec(false, "hour", [1, 4], false) },
{ kind: "write", instance: 0, slot: 0, group: 2, frames: 1 },
{ kind: "bounds", reader: 2, range: [2, 4] },
{ kind: "up", instance: 0, slot: 1 },
{ kind: "join", reader: 0, spec: spec(false, "short", undefined, false) },
],
finish: false,
},
// A route's give-up is no arrival, so the gap below it does not age from then: a group landing
// there later still reaches the backfill asked for it.
{
ops: [
{ kind: "up", instance: 1, slot: 1 },
{ kind: "giveUp", instance: 1, slot: 1, group: 2 },
{ kind: "expire" },
{ kind: "up", instance: 1, slot: 2 },
{ kind: "join", reader: 0, spec: spec(false, "hour", undefined, false) },
{ kind: "write", instance: 1, slot: 1, group: undefined, frames: 1 },
{ kind: "write", instance: 1, slot: 2, group: 2, frames: 1 },
{ kind: "down", instance: 1, slot: 2 },
{ kind: "leave", reader: 1 },
],
finish: false,
},
// What a local track's subscribers ask for is no feed floor, so a drop it forgets under that is
// still accounted and the read ends with the track.
{
ops: [
{ kind: "up", instance: 0, slot: 1 },
{ kind: "join", reader: 0, spec: spec(false, "hour", [9, 12], false) },
{ kind: "up", instance: 0, slot: 2 },
{ kind: "giveUp", instance: 0, slot: 1, group: 1 },
{ kind: "expire" },
{ kind: "write", instance: 0, slot: 0, group: undefined, frames: 1 },
],
finish: true,
},
// A local floor holds a read open only for a group the cache holds under it, so a range whose
// groups never arrived ends.
{
ops: [
{ kind: "join", reader: 1, spec: spec(false, "hour", [1, 2], false) },
{ kind: "bounds", reader: 1, range: [6, 12] },
{ kind: "write", instance: 0, slot: 0, group: 11, frames: 3 },
{ kind: "end", instance: 0, slot: 0 },
{ kind: "ranges", reader: 1, range: undefined },
{ kind: "write", instance: 0, slot: 0, group: undefined, frames: 1 },
],
finish: false,
},
// A forgotten drop reaching a local track's first arrival stays accounted once a lower group
// lands, so no replaced copy is waited on for it.
{
ops: [
{ kind: "join", reader: 0, spec: spec(false, "hour", [10, 12], false) },
{ kind: "giveUp", instance: 0, slot: 0, group: 1 },
{ kind: "expire" },
{ kind: "write", instance: 0, slot: 0, group: 2, frames: 1 },
{ kind: "up", instance: 0, slot: 1 },
],
finish: true,
},
// A replaced copy is waited on only for groups the reader's window names.
{
ops: [
{ kind: "join", reader: 2, spec: spec(false, "hour", undefined, false) },
{ kind: "write", instance: 0, slot: 0, group: 9, frames: 1 },
{ kind: "up", instance: 0, slot: 1 },
{ kind: "down", instance: 0, slot: 0 },
{ kind: "ranges", reader: 2, range: [1, 2] },
{ kind: "write", instance: 0, slot: 0, group: 0, frames: 1 },
],
finish: false,
},
// A read that ends with the track owed the groups its cursor named then, as the Rust model
// judges it.
{
ops: [
{ kind: "up", instance: 1, slot: 1 },
{ kind: "up", instance: 1, slot: 0 },
{ kind: "join", reader: 0, spec: spec(false, "hour", [1, 3], false) },
{ kind: "write", instance: 1, slot: 0, group: undefined, frames: 1 },
{ kind: "write", instance: 1, slot: 1, group: 1, frames: 1 },
{ kind: "write", instance: 1, slot: 0, group: 11, frames: 1 },
{ kind: "down", instance: 1, slot: 1 },
{ kind: "end", instance: 1, slot: 0 },
{ kind: "ranges", reader: 0, range: undefined },
{ kind: "advance", ms: 1 },
],
finish: false,
},
// A time start under the serving copy's resume point is placed where it names, not at the floor
// that raised it, so the read waits on a group the local floor keeps out. Mirrors the Rust case.
{
ops: [
{ kind: "join", reader: 2, spec: spec(false, "hour", undefined, false) },
{ kind: "write", instance: 0, slot: 0, group: 8, frames: 3 },
{ kind: "up", instance: 0, slot: 1 },
{ kind: "down", instance: 0, slot: 0 },
{ kind: "bounds", reader: 2, range: [8, 9] },
{ kind: "ranges", reader: 2, range: [7, 9] },
{ kind: "read", reader: 0 },
],
finish: false,
},
// Ranges widened under the resume point of a copy whose failover cap cut their end short start
// where they name and keep their own end. Mirrors the Rust case.
{
ops: [
{ kind: "up", instance: 1, slot: 0 },
{ kind: "join", reader: 1, spec: spec(false, "hour", [3, 7], false) },
{ kind: "up", instance: 1, slot: 1 },
{ kind: "write", instance: 1, slot: 1, group: 3, frames: 1 },
{ kind: "write", instance: 1, slot: 1, group: 5, frames: 3 },
{ kind: "down", instance: 1, slot: 1 },
{ kind: "ranges", reader: 1, range: [1, 7] },
{ kind: "join", reader: 0, spec: spec(false, "hour", undefined, false) },
],
finish: false,
},
];
const failures: string[] = [];
for (const [i, c] of regressions.entries()) {
Expand Down
52 changes: 40 additions & 12 deletions js/net/src/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ interface Copy {
/**
* One past the highest group the reader's window dropped from it, which it no longer offers
* though a widened window may name it again: a resume point, as {@link Copy.floor} is.
*
* The Rust reader has no counterpart, since it never reaches this shape. Its front keeps a track
* being read on its source until that copy ends, so a copy it replaced has stopped placing the
* window by the time the next one subscribes. That copy subscribes with the window's resolution,
* so its own ranges hold back what the window leaves out, still cached for a widening to offer.
* This front moves a track being read as soon as a better route of its instance answers. A copy
* it replaced, still delivering, can then place the window after the serving copy subscribed with
* nothing resolved, and that copy offers a group the window leaves out: the reader takes it.
*/
dropped?: bigint;
}
Expand Down Expand Up @@ -404,6 +412,19 @@ function resumePoint(copy: Pick<Copy, "floor" | "dropped">): bigint | undefined
return copy.dropped === undefined ? copy.floor : max(copy.floor ?? 0n, copy.dropped);
}

/**
* Where `range` resolved, as `at`, on a copy whose resume point is `floor`. One above the range's
* time start asked the copy from the floor, so its resolution says nothing of which group the
* range starts at, nor of an end under the floor too. The range is placed at its own anchors,
* which names the groups under the floor that only a backfill reads. Mirrors the Rust `unraised`.
*/
function unraised(range: track.Range | undefined, floor: bigint | undefined, at: Resolved): Resolved {
const start = range?.start?.time;
if (floor === undefined || start === undefined || start >= floor) return at;
const end = range?.end?.time;
return { ...at, start, end: end !== undefined && end < floor ? end + 1n : at.end, floored: true };
Comment thread
kixelated marked this conversation as resolved.
Resolved
}

/** The first group past where `copy` was replaced: 0 for one replaced before any group. */
function copyEnd(copy: Pick<Copy, "until">): bigint | undefined {
if (copy.until === undefined) return undefined;
Expand Down Expand Up @@ -694,8 +715,10 @@ class Reader implements ResumeSource {
// A failover cap at or below where a range starts empties the copy's range, which then
// says nothing of where the reader's own range starts. One at or below where it ends cut
// that end short, so it says nothing of where the reader's range ends.
const resolved = own.map((at) => {
if (!at || (cap !== undefined && at.end !== undefined && at.end <= at.start)) return undefined;
const resolved = own.map((placed, index) => {
if (!placed) return undefined;
const at = unraised(ranges[index], copy.floor, placed);
if (cap !== undefined && at.end !== undefined && at.end <= at.start) return undefined;
return cap === undefined || (at.end !== undefined && at.end < cap) ? at : { ...at, end: undefined };
});
this.#resolve(resolved);
Expand Down Expand Up @@ -1076,25 +1099,28 @@ class Reader implements ResumeSource {
}

/**
* Whether the reader's bounded `window` has nothing left to deliver, once no replaced copy may
* still deliver a group under its bound. The serving copy reads every group of the window from
* its resume point, so it judges those, against what it and the reader (`delivered`) handed
* out. What lies under its resume point was the copies' it replaced, each let go once it had
* nothing more, though one there that the local floor or cap keeps out stays owed, as on one
* Whether the reader's bounded {@link #window} has nothing left to deliver, once no replaced
* copy may still deliver a group under its bound. The serving copy reads every group of the
* window from its resume point, so it judges those, against what it and the reader (`delivered`)
* handed out. What lies under its resume point was the copies' it replaced, each let go once it
* had nothing more, though one there that the local floor or cap keeps out stays owed, as on one
* cursor. A backfill still to ask is not judged yet, and a route between copies has nothing to
* judge. Mirrors the Rust `Reader::exhausted`.
*/
exhausted(delivered: Delivered, window: Window): boolean {
exhausted(delivered: Delivered): boolean {
this.sync();
if (this.#closed || this.#ended || this.#resumed) return false;
const serving = this.#copies.at(-1);
if (!serving || serving.replaced || serving.done !== undefined || serving.generation !== this.#generation) {
return false;
}
if (this.#letGo(delivered)) return false;
// The reader's own window, as its copies placed it: the subscriber's leaves its anchors to them.
const window = this.#window();
const end = window.end();
if (end === undefined || window.pending) return false;
const resumed = resumePoint(serving);
if (resumed !== undefined && end !== undefined) {
if (resumed !== undefined) {
const cursor = this.#shell.cursor.peek();
for (const run of window.runs(window.floor() ?? 0n, min(resumed, end))) {
for (const gap of delivered.gaps(run.start, run.end)) {
Expand Down Expand Up @@ -1417,9 +1443,11 @@ class Reader implements ResumeSource {
// From where it was asked to start, or else from what it first got.
const start = max(cursor.start, copy.floor ?? 0n);
const floor = copy.floor !== undefined;
// A group still to land is waited for once something superseded it, as a gap is.
const land = { start, end, floor, grace: wait, delivered };
if (copy.since !== undefined && hooks.mayLand(copy.sub, land)) return "wait";
// A group still to land in the reader's window is waited for once something superseded
// it, as a gap is.
const lands = (run: { start: bigint; end: bigint }) =>
hooks.mayLand(copy.sub, { ...run, floor, grace: wait, delivered });
if (copy.since !== undefined && this.#window().runs(start, end).some(lands)) return "wait";
if (hooks.hasUnreadGroup(copy.sub, cursor.start, end, delivered)) return "keep";
}
if (copy.lease > 0) return "keep";
Expand Down
Loading