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
10 changes: 6 additions & 4 deletions js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,10 @@ export interface ResumeSource {
/**
* 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`.
* deliver keeps it open, and an `ordered` reader passes what the copy's own publisher
* created past. Mirrors the Rust `Reader::exhausted`.
*/
exhausted(delivered: Delivered): boolean;
exhausted(delivered: Delivered, ordered: boolean): 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 Expand Up @@ -879,9 +880,10 @@ export const hooks: {
parked: (subscriber: Subscriber) => Subscriber;
/**
* Whether a route copy's bounded window has nothing left for the logical reader that handed
* out `delivered`, counting what the copy handed out itself. Mirrors the Rust `exhausted_for`.
* out `delivered`, counting what the copy handed out itself, read in key order when
* `ordered`. Mirrors the Rust `exhausted_for`.
*/
exhaustedFor: (subscriber: Subscriber, delivered: Delivered) => boolean;
exhaustedFor: (subscriber: Subscriber, delivered: Delivered, ordered: boolean) => boolean;
/** How many runs of handed-out groups a subscriber remembers: one per gap, plus the floor. Tests only. */
deliveredRuns: (subscriber: Subscriber) => number;
/** How many runs a subscriber's record of what it reported holds. Tests only. */
Expand Down
81 changes: 79 additions & 2 deletions js/net/src/ordered.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Duration, now, sleep } from "@moq/clock";
import { Manual } from "@moq/clock/test";
import { StreamCode, StreamError } from "./error.ts";
import { hooks, staleHole } from "./internal.ts";
import { textFrame } from "./mock.ts";
import { Timestamp } from "./time.ts";
import { manual, settlesWithin, textFrame } from "./mock.ts";
import { Timescale, Timestamp } from "./time.ts";
import { type Ordered, Producer as TrackProducer } from "./track.ts";

// A session-fed copy owes holes; an in-process producer does not.
Expand Down Expand Up @@ -435,3 +435,80 @@ test("a read returning only gaps keeps its handed-out record compact", () => {
}
expect(hooks.deliveredRuns(sub)).toBe(1);
});

// Mirrors the Rust `a_time_end_between_*` tests in `track.rs`.
const between = { ranges: [{ start: { time: 1500n }, end: { time: 1999n } }], delay: { max: Duration.millis(30_000) } };

test("a time end between two groups ends an ordered read once the publisher created one past it", async () => {
using clock = manual();
const producer = new TrackProducer("test");
const ordered = producer.subscribe(between).ordered();
const arrival = producer.subscribe(between);
put(producer, 1000n, false);
expect((await ordered.nextGroup())?.start).toBe(1000n);
expect((await arrival.recvGroup())?.start).toBe(1000n);
const end = ordered.nextGroup();
expect(await settlesWithin(end, clock)).toBe(false);
// Handing out 2000 would pass every start below it; an arrival read waits, since 1500 may still come.
put(producer, 2000n, false);
expect(await end).toBeUndefined();
const waiting = arrival.recvGroup();
expect(await settlesWithin(waiting, clock)).toBe(false);
// The ended read passed every key below its end: a group created there now is late.
put(producer, 1500n, false);
expect(await ordered.nextGroup()).toBeUndefined();
expect((await waiting)?.start).toBe(1500n);

// A group created between before a read reaches the end is still read. The time start now
// resolves to 1500, which contains it.
const later = producer.subscribe(between).ordered();
expect((await later.nextGroup())?.start).toBe(1500n);
put(producer, 1600n, false);
expect((await later.nextGroup())?.start).toBe(1600n);
expect(await later.nextGroup()).toBeUndefined();
});

test("a time end between appended groups ends either read at once", async () => {
const producer = new TrackProducer("test");
for (const key of [1000n, 2000n]) {
const group = producer.appendGroup(Timestamp.unscaled(key));
group.writeFrame(textFrame("x", Timestamp.unscaled(key)));
group.finish();
}
const arrival = producer.subscribe(between);
expect((await arrival.recvGroup())?.start).toBe(1000n);
expect(await arrival.recvGroup()).toBeUndefined();
const ordered = producer.subscribe(between).ordered();
expect((await ordered.nextGroup())?.start).toBe(1000n);
expect(await ordered.nextGroup()).toBeUndefined();
});

test("a time end between received groups ends an ordered read only once the skip is proven", async () => {
using clock = manual();
const producer = receivedTrack();
put(producer, 1000n, false);
put(producer, 2000n, false);
const ordered = producer.subscribe(between).ordered();
expect((await ordered.nextGroup())?.start).toBe(1000n);
const end = ordered.nextGroup();
expect(await settlesWithin(end, clock)).toBe(false);
producer.skipGroups(1001n, 2000n);
expect(await end).toBeUndefined();
});

// Mirrors the Rust `an_ordered_read_ended_past_a_stale_group_reports_it_first`.
test("an ordered read whose window ends past a group it passed as stale reports it first", () =>
withClock((clock) => {
const producer = new TrackProducer("test").accept({ timescale: Timescale.MILLI });
const first = producer.createGroup(0n);
first.writeFrame(textFrame("x", Timestamp.fromMillis(0)));
first.finishAt(10n);
clock.jump(Duration.millis(100));
put(producer, 100n);
const ordered = producer
.subscribe({ delay: { max: Duration.millis(50) }, ranges: [{ start: { time: 0n }, end: { time: 49n } }] })
.ordered();
expect(items(ordered)).toEqual(["gap 0..1 (old)"]);
expect(ordered.tryNextItem()).toBeUndefined();
expect(ordered.tryNextGroup()).toBeUndefined();
}));
59 changes: 59 additions & 0 deletions js/net/src/resume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4831,3 +4831,62 @@ test("failed copies holding groups back are each subscribed afresh", async () =>
sub.close();
close();
});

// Mirrors the Rust `a_time_end_between_groups_ends_an_ordered_read_at_the_next` integration test,
// through a front serving the publisher's own track: its copy passes on what that publisher created.
test("a time end between two groups ends an ordered read through a front at the next", async () => {
const front = new Front();
const source = new BroadcastProducer();
const handle = source.consume();
front.serve(handle);
const track = copy(source);
for (const key of [1000n, 2000n, 3000n]) track.write(key);
const between = {
ranges: [{ start: { time: 1500n }, end: { time: 1999n } }],
delay: { max: Duration.millis(3_600_000) },
};
const ordered = front.consumer.track("seq").subscribe(between).ordered();
expect((await ordered.nextGroup())?.start).toBe(1000n);
expect(await ordered.nextGroup()).toBeUndefined();
// An arrival read waits: the publisher may still create 1500.
const arrival = front.consumer.track("seq").subscribe(between);
expect((await arrival.recvGroup())?.start).toBe(1000n);
const waiting = arrival.recvGroup();
expect(await isPending(waiting)).toBe(true);
// The ended ordered read passed its range: a group created there now is late.
track.write(1500n);
expect(await ordered.nextGroup()).toBeUndefined();
expect((await waiting)?.start).toBe(1500n);
ordered.close();
arrival.close();
front.consumer.close();
handle.close();
source.close();
});

// An ordered read through a front that ends past a group its copy gave up, as one missing its head
// with no fill to ask, returns that gap before the end.
test("an ordered read through a front returns the gap its copy gave up before ending", async () => {
const front = new Front();
const a = new BroadcastProducer();
const ta = defined(a.createTrack("seq", { maxAge: null }));
const tail = hooks.receiveGroup(ta, { start: 0n, offset: 1 });
tail.writeFrame({ payload: enc.encode("0.1"), timestamp: Timestamp.unscaled(0n) });
tail.finish();
const past = ta.createGroup(2n);
past.writeFrame({ payload: enc.encode("2.0"), timestamp: Timestamp.unscaled(2n) });
past.finish();
front.serve(a.consume());
const ordered = front.consumer
.track("seq")
.subscribe({ delay: { max: null }, ranges: [{ start: { time: 0n }, end: { time: 1n } }] })
.ordered();
try {
expect(await ordered.nextItem()).toMatchObject({ kind: "gap", start: 0n, end: 1n });
expect(await ordered.nextItem()).toBeUndefined();
} finally {
ordered.close();
front.consumer.close();
a.close();
}
});
4 changes: 2 additions & 2 deletions js/net/src/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ class Reader implements ResumeSource {
* 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): boolean {
exhausted(delivered: Delivered, ordered: boolean): boolean {
this.sync();
if (this.#closed || this.#ended || this.#resumed) return false;
const serving = this.#copies.at(-1);
Expand All @@ -1138,7 +1138,7 @@ class Reader implements ResumeSource {
}
}
}
return hooks.exhaustedFor(serving.sub, delivered);
return hooks.exhaustedFor(serving.sub, delivered, ordered);
}

tryDrop(delivered: Delivered, passing?: bigint): (Recv & { kind: "drop" }) | undefined {
Expand Down
77 changes: 62 additions & 15 deletions js/net/src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4232,16 +4232,19 @@ export class Subscriber {
// {@link #drained}. A wire publisher's reader never ends there.
#exhausted(): boolean {
if (this.#parks) return false;
if (this.#resume) return this.#resume.exhausted(this.#delivered);
return this.#drained([this.#delivered]);
const ordered = this.#mode === "ordered";
if (this.#resume) return this.#resume.exhausted(this.#delivered, ordered);
return this.#drained([this.#delivered], ordered);
}

// Whether a bounded window has nothing left to deliver: every group in its ranges was handed
// out (in any of `taken`), dropped, or never will be, as one skipped as stale, one below where
// the feed started, or a hole the feed no longer owes. An open window always has more, and so
// does one the cap keeps a group out of, since the reader may raise it again. Mirrors the
// Rust `Cursor::is_exhausted`.
#drained(taken: readonly Delivered[]): boolean {
// does one the cap keeps a group out of, since the reader may raise it again. A local
// producer may still create any group it did not skip, so only an `ordered` read, which hands
// out the group it created above a hole before any created later below, passes that hole.
// Mirrors the Rust `Cursor::is_exhausted`.
#drained(taken: readonly Delivered[], ordered: boolean): boolean {
const end = this.#window.end();
if (end === undefined) return false;
// A fill still owes a group it reads.
Expand All @@ -4250,6 +4253,7 @@ export class Subscriber {
const cursor = this.#cursor.peek();
const producer = this.#state.producer;
const received = max(this.#state.received.peek(), ((producer && fillEdge(producer)) ?? -1n) + 1n);
const local = producer !== undefined && !wireFed(producer);
// A received stale run that does not settle this reader is unknown to it (see `settles`):
// the reader still wants those groups, however the feed accounted for them.
const budget = this.#drift().budget;
Expand All @@ -4269,15 +4273,19 @@ export class Subscriber {
}));
const drops = overlapping.filter((run) => settles(run, budget, coverage));
const unknown = overlapping.filter((run) => !settles(run, budget, coverage));
for (const open of runGaps(drops, gap.start, gap.end)) {
// A miss this reader gave up on is never wanted again.
const opens = runGaps(drops, gap.start, gap.end).flatMap((run) =>
runGaps(this.#givenUp, run.start, run.end),
);
for (const open of opens) {
if (unknown.some((run) => run.start < open.end && open.start < run.end)) return false;
// Cached under the local floor, which lowering offers again, unless a key-order read
// already passed it: an ordered take let go of it, so it is not held.
const under = { start: max(open.start, this.#nextKey), end: min(open.end, cursor.start) };
if (producer && under.start < under.end && cachedIn(producer, under.start, under.end).length > 0)
return false;
// Not received yet, so it may still arrive.
if (open.end > received) return false;
// Not received yet, so it may still arrive; a local producer may still create it.
if (open.end > received || (local && !ordered)) return false;
// Held, so a read still hands it out, skips it, or waits for the cap to rise.
const index = timelineIndex(held, open.start);
if (held[index] && held[index].start < open.end) return false;
Expand Down Expand Up @@ -4327,10 +4335,10 @@ export class Subscriber {
};
// A route copy judged for the logical reader that handed out `delivered`: what the copy
// handed out itself counts too. A copy that is itself a front judges across its own copies.
hooks.exhaustedFor = (subscriber, delivered) =>
hooks.exhaustedFor = (subscriber, delivered, ordered) =>
subscriber.#resume
? subscriber.#resume.exhausted(subscriber.#delivered.union(delivered))
: subscriber.#drained([subscriber.#delivered, delivered]);
? subscriber.#resume.exhausted(subscriber.#delivered.union(delivered), ordered)
: subscriber.#drained([subscriber.#delivered, delivered], ordered);
hooks.tryDrop = (subscriber) => subscriber.#tryDrop();
// A reader following route copies leaves its ranges to the serving copy, which resolves
// their anchors against the groups it holds, then narrows them to its own local bounds.
Expand Down Expand Up @@ -5111,7 +5119,11 @@ export class Subscriber {
}
}
this.#handOut(recv.group.start, ordered);
} else if (recv.kind === "idle" && this.#exhausted()) return { kind: "done" };
} else if (recv.kind === "done" || (recv.kind === "idle" && this.#exhausted())) {
// As the Rust front's reader: whatever ended it, an ordered read passed its range.
if (ordered) this.#passEnd();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return { kind: "done" };
}
return recv;
}
}
Expand Down Expand Up @@ -5348,6 +5360,35 @@ export class Subscriber {
return undefined;
}

// Pass every key below the bounded window's end for good, as an ordered read that ran out of it:
// a group created there later is late, so later reads end at once too. Only below the cap: a read
// that ran out under a lowered cap still owes what lies above it. Mirrors the Rust
// `Cursor::pass_end`.
#passEnd(): void {
// A resumed reader's ranges resolve on the copy serving it.
const window = this.#resume ? this.#resume.servingEnd() : this.#window.end();
if (window === undefined) return;
const cap = this.#cursor.peek().end;
const end = cap === undefined ? window : min(cap, window);
// Passing the keys hands them out as far as #delivered knows, so first take what the copies
// gave up there, as a group handed out does.
if (this.#resume) {
for (
let drop = this.#resume.tryDrop(this.#delivered, end);
drop;
drop = this.#resume.tryDrop(this.#delivered, end)
) {
this.#queueResumedDrop(drop);
}
}
const floor = this.#delivered.runEnd(0n) ?? 0n;
this.#delivered.forgetBelow(end);
const forgot = this.#delivered.runEnd(0n) ?? 0n;
if (forgot > floor) this.#stoppedReading(floor, forgot);
if (!this.#resume) this.#nextKey = max(this.#nextKey, end);
this.#fold(forgot);
}

// Take `given` from the gap at `index` as an item, which the read has now moved past. What lies
// below `given`, which the bounds leave out, and from `resume` on stay gaps.
#takeGap(items: Items, index: number, given: Run, resume: bigint): Item {
Expand Down Expand Up @@ -5572,7 +5613,10 @@ export class Subscriber {
const end = this.#state.end.peek()?.at;
// A time start still looked up may yet take in a group held already.
if (this.#window.lookups().length > 0) return { kind: "idle" };
if (this.#exhausted()) return { kind: "done" };
if (this.#exhausted()) {
this.#passEnd();
return { kind: "done" };
}
if (
!this.#parked(groups) &&
!this.#starting(groups, start) &&
Expand Down Expand Up @@ -5849,8 +5893,11 @@ export class Ordered {
* group whose every frame is further behind the live edge than `delay.max` (the default
* subscription's zero keeps only what nothing newer has superseded). Honors the bounds set by
* {@link withRange}. Like {@link Subscriber.recvGroup}, a read whose ranges all end
* finishes once nothing in them is left to deliver. Later reads return `undefined`
* immediately too; widening the ranges with {@link update} reads on above the last key returned.
* finishes once nothing in them is left to deliver; on a local track that includes a key
* below a group the producer created, which this read hands out first. Later reads return `undefined`
* immediately too; widening the ranges with {@link update} reads on above the end the read
* passed (or the cap, when that was lower), so a group created below that end afterward is
* never handed out.
*/
nextGroup(): Promise<GroupConsumer | undefined> {
return ordered_.nextGroup(this.#subscriber);
Expand Down
15 changes: 0 additions & 15 deletions quest/m2/time-addressing/time-end-gap.md

This file was deleted.

Loading