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
61 changes: 60 additions & 1 deletion js/net/src/fill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ test("fill: a refusal under an old budget is asked afresh on each widening, neve
const fill = await requests.requestedFill();
expect([fill?.start, fill?.end]).toEqual([0n, 5n]);
if (!fill) throw new Error("no fill");
rejectGroups(fill, 0n, 5n, old, budget);
rejectGroups(fill, 0n, 5n, old, { budget, everyone: false });
fill.close();
reader.update(terms(budget * 2));
}
Expand Down Expand Up @@ -1261,3 +1261,62 @@ test("fill: a lite-07 ranged subscription reads history the publisher never cach
peer.close();
}
});

// A fill's OLD answer under the publisher's max age binds every reader, so the track holds it as
// a stale verdict under that age rather than a loss for good: the readers waiting now are refused,
// a later reader no wider is settled without asking, and once new terms forget the verdict a
// reader asks again. Mirrors the Rust `a_fills_old_answer_under_the_max_age_is_held_until_forgotten`.
test("fill: an OLD answer under the max age is held as stale until forgotten", async () => {
const broadcast = new BroadcastProducer();
const track = broadcast.createTrack("video", { maxAge: Duration.millis(10_000) });
const live = track.createGroup(3n);
live.writeFrame({ payload: enc.encode("live 3") });
live.finish();
const requests = track.requests();
const terms: Subscription = {
ranges: [{ start: { absolute: 0n } }],
delay: { max: Duration.millis(60_000) },
order: "ascending",
};
const asked = async () => {
const fills: bigint[][] = [];
for (;;) {
const fill = await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)]);
if (!fill) return fills;
fills.push([fill.start, fill.end]);
fill.close();
}
};
const reader = track.subscribe(terms);
expect(reader.tryRecvGroup()?.sequence).toBe(3n);
reader.tryRecvGroup();
// A reader of the run alone, which the verdict does not settle, waits on the same fill.
const bounded = track.subscribe({ ...terms, ranges: [{ start: { absolute: 0n }, end: { absolute: 3n } }] });
bounded.tryRecvGroup();
const fill = await requests.requestedFill();
if (!fill) throw new Error("no fill");
expect([fill.start, fill.end]).toEqual([0n, 3n]);
const old = new StreamError(StreamCode.Old, { message: "old" });
rejectGroups(fill, 0n, 3n, old, { budget: 10_000, everyone: true });
expect(hooks.tryDrop(bounded)).toMatchObject({ kind: "drop", start: 0n, end: 3n, error: old });
fill.close();

const later = track.subscribe(terms);
expect(later.tryRecvGroup()?.sequence).toBe(3n);
later.tryRecvGroup();
expect(hooks.tryDrop(later)).toMatchObject({ kind: "drop", start: 0n, end: 3n, stale: 10_000 });
expect(await asked()).toEqual([]);

expect(hooks.supersedeStale(track, undefined)).toEqual([{ start: 0n, end: 3n }]);
const again = track.subscribe(terms);
expect(again.tryRecvGroup()?.sequence).toBe(3n);
again.tryRecvGroup();
expect(await asked()).toEqual([[0n, 3n]]);

reader.close();
bounded.close();
later.close();
again.close();
requests.close();
broadcast.close();
});
52 changes: 36 additions & 16 deletions js/net/src/fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export interface FillTrack {
readonly name: string;
/** Insert group `sequence` into the cache, committing `info` first if the track has none. */
accept(sequence: bigint, info: Info | undefined): GroupProducer;
/** Record `[start, end)` as never to be delivered. */
drop(start: bigint, end: bigint, error: Error): void;
/**
* Record `[start, end)` as never to be delivered, with `error`. With `stale`, judged too old
* under that budget in milliseconds: a verdict only on readers no more tolerant.
*/
drop(start: bigint, end: bigint, hole: { error: Error; stale?: number }): void;
/** Whether the track still caches group `sequence`, not failed. */
holds(sequence: bigint): boolean;
/** Whether the track closed, so nothing more can be served. */
Expand Down Expand Up @@ -67,7 +70,7 @@ export interface Asker {
// The fill operations a handle runs against the state it came from; assigned in its static block.
let fillOps: {
accept(state: FillState, pending: Pending, sequence: bigint, info: Info | undefined): GroupProducer;
drop(state: FillState, pending: Pending, start: bigint, end: bigint, error?: Error): void;
drop(state: FillState, pending: Pending, start: bigint, end: bigint, hole?: { error: Error; stale?: number }): void;
refuse(state: FillState, pending: Pending, start: bigint, end: bigint, error: Error, stale?: number): void;
settle(state: FillState, pending: Pending): void;
name(state: FillState): string;
Expand Down Expand Up @@ -283,7 +286,8 @@ export class FillState {

// Answer what `pending` still owes as dropped, then forget it.
#settle(pending: Pending): void {
for (const run of pending.owed()) this.#track.drop(run.start, run.end, new NotFound(`group ${run.start}`));
for (const run of pending.owed())
this.#track.drop(run.start, run.end, { error: new NotFound(`group ${run.start}`) });
this.#finish(pending);
}

Expand Down Expand Up @@ -315,11 +319,15 @@ export class FillState {
// Landing in the cache answers it, as `arrived` records.
return state.#track.accept(sequence, info);
},
drop: (state, pending, start, end, error) => {
drop: (state, pending, start, end, hole) => {
for (const run of pending.owed(start, end)) {
state.#track.drop(run.start, run.end, error ?? new NotFound(`group ${run.start}`));
state.#track.drop(run.start, run.end, hole ?? { error: new NotFound(`group ${run.start}`) });
}
state.#resolve(pending, start, end);
// A stale verdict settles only readers that read every group above it, so the readers
// waiting now are refused as well, as the Rust `Inner::drop_groups` does. It binds every
// reader, so the refusal carries no budget a wider one could see past.
if (hole?.stale !== undefined) fillOps.refuse(state, pending, start, end, hole.error);
else state.#resolve(pending, start, end);
},
refuse: (state, pending, start, end, error, stale) => {
for (const gap of pending.unresolved(start, end)) {
Expand Down Expand Up @@ -348,16 +356,28 @@ function subtract(gap: Run, run: Run): Run[] {
export let wantedRuns: (fill: Fill) => Run[];
/** Wait for a change to the groups or readers of a fill. @internal */
export let fillChanged: (fill: Fill) => Promise<unknown>;
/**
* The budget in milliseconds an OLD answer was judged under, and whether that is the publisher's
* max age, which binds every reader, rather than the request's own budget.
*
* @internal
*/
export interface Judged {
budget: number;
everyone: boolean;
}

/**
* Answer `[start, end)` of `fill` with its source's refusal of a request for them. An OLD one
* says every older group of the fill is stale too, since a timed track's starts never rewind:
* with `stale`, the budget in milliseconds the request carried, it is refused to the readers
* waiting now, a verdict only on those no more tolerant, so a wider reader asks again; without,
* the publisher's max age judged it, which binds every reader, so the track records it dropped.
* Any other refusal answers only `[start, end)`, for now. Mirrors the Rust `Fill::reject_groups`.
* judged under the request's own budget, or a max age not known here (`judged` undefined), it is
* refused to the readers waiting now, a verdict only on those no more tolerant, so a wider reader
* asks again; under the publisher's max age it binds every reader, so the track records it as a
* stale verdict under that age, which new terms or the drop set's expiry window forget. Any other
* refusal answers only `[start, end)`, for now. Mirrors the Rust `Fill::reject_groups`.
* @internal
*/
export let rejectGroups: (fill: Fill, start: bigint, end: bigint, error: Error, stale: number | undefined) => void;
export let rejectGroups: (fill: Fill, start: bigint, end: bigint, error: Error, judged: Judged | undefined) => void;

// Constructs the public handles from within this module; assigned in their static blocks.
let makeFill: (state: FillState, pending: Pending) => Fill;
Expand Down Expand Up @@ -465,13 +485,13 @@ export class Fill {
makeFill = (state, pending) => new Fill(state, pending);
wantedRuns = (fill) => fill.#pending.owed();
fillChanged = (fill) => Promise.resolve(fill.#pending.revision.changed());
rejectGroups = (fill, start, end, error, stale) => {
rejectGroups = (fill, start, end, error, judged) => {
if (toStreamCode(error) !== StreamCode.Old) {
fill.refuseGroups(start, end, error);
} else if (stale === undefined) {
fillOps.drop(fill.#state, fill.#pending, fill.start, end, error);
} else if (judged?.everyone) {
fillOps.drop(fill.#state, fill.#pending, fill.start, end, { error, stale: judged.budget });
} else {
fillOps.refuse(fill.#state, fill.#pending, fill.start, end, error, stale);
fillOps.refuse(fill.#state, fill.#pending, fill.start, end, error, judged?.budget);
}
};
}
Expand Down
19 changes: 10 additions & 9 deletions js/net/src/lite/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
toStreamCode,
unauthorized,
} from "../error.ts";
import { fillChanged, rejectGroups, wantedRuns } from "../fill.ts";
import { fillChanged, type Judged, rejectGroups, wantedRuns } from "../fill.ts";
import * as netGroup from "../group.ts";
import { Cost, type Hop, MAX_HOPS, routesEqual, UNKNOWN_HOP, WireRoute } from "../hop.ts";
import * as Instance from "../instance.ts";
Expand Down Expand Up @@ -2542,10 +2542,10 @@ export class Subscriber {
const { start, end } = resp.drop;
entry.tail.account(start, end);
const cause = resp.drop.cause(this.version);
// Only a group its publisher never had is lost for every reader, and asked with no
// budget, an OLD one is past the publisher's max age, which binds every reader too.
// Only a group its publisher never had is lost for every reader; anything else,
// OLD included, is refused for now, as the Rust `Refusal::answer` does.
if (toStreamCode(cause) === StreamCode.NotFound) fill.dropGroups(start, end);
else rejectGroups(fill, start, end, cause, undefined);
else fill.refuseGroups(start, end, cause);
} else if ("skip" in resp) {
// Never produced: final, as the subscription feeding the copy records it.
const { start, end } = resp.skip;
Expand All @@ -2559,12 +2559,13 @@ export class Subscriber {
}
}

// The budget an OLD answer to a request carrying `asked` judged, or undefined when it
// reached the publisher's max age, which binds every reader, as the Rust
// `Inner::old_binds_everyone` judges. No budget leaves only the max age.
#stale(framing: Framing, asked: number | undefined): number | undefined {
// The budget an OLD answer to a request carrying `asked` judged: the publisher's max age once
// it reached it, which binds every reader, as the Rust `Inner::binding_max_age` judges. No
// budget leaves only the max age; undefined when neither is known.
#stale(framing: Framing, asked: number | undefined): Judged | undefined {
const bound = framing.maxAge?.asMillis();
return asked === undefined || (bound !== undefined && asked >= bound) ? undefined : asked;
if (asked !== undefined && (bound === undefined || asked < bound)) return { budget: asked, everyone: false };
return bound === undefined ? undefined : { budget: bound, everyone: true };
}

// Settle `[start, end)` of what `producer`'s subscription re-offers: a stream or a verdict
Expand Down
4 changes: 2 additions & 2 deletions js/net/src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,8 +1135,8 @@ export class Producer {
if (info !== undefined && this.#state.info.peek() === undefined) this.accept(info);
return this.#createGroup(sequence, 0, true);
},
drop: (start, end, error) => {
this.#drop(start, end, { error });
drop: (start, end, hole) => {
this.#drop(start, end, hole);
this.#chargeDebt();
},
holds: (sequence) => cachedHolds(this, sequence),
Expand Down
83 changes: 0 additions & 83 deletions quest/m1/group-error-cache.md

This file was deleted.

61 changes: 61 additions & 0 deletions rs/moq-net/src/model/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23929,6 +23929,67 @@ mod test {
assert!(matches!(dynamic.poll_requested_fill(&waiter), Poll::Ready(Ok(_))));
}

/// A fill's OLD answer under the publisher's max age binds every reader, so the track holds
/// it as a stale verdict under that age rather than a loss for good: the readers waiting now
/// are refused, a later reader no wider is settled without asking, and once the verdict is forgotten, as new terms or the
/// drop set's expiry window forget one, a reader asks again. Mirrors JS.
#[test]
#[cfg(feature = "async")]
#[expect(clippy::single_range_in_vec_init, reason = "the forgotten runs are a set, here one")]
fn a_fills_old_answer_under_the_max_age_is_held_until_forgotten() {
let max_age = Duration::from_secs(10);
let mut producer = track_producer("test", Info::timed().with_max_age(max_age));
let dynamic = producer.requests();
producer.start_at(3).unwrap();
write_group(&producer, 3);
// No subscription feeds the copy, so nothing below the start arrives but what a fill does.
producer.set_unfed(true);
let terms = || {
Subscription::default()
.with_delay(Delay::default().with_max(Duration::from_secs(60)))
.with_ranges([Range::groups(0..)])
};
let mut subscriber = producer.subscribe(terms());
let waiter = kio::Waiter::noop();
assert_eq!(drain_groups(&mut subscriber), [3]);
// A reader of the run alone, which the verdict does not settle, waits on the same fill.
let mut bounded = producer.subscribe(terms().with_ranges([Range::groups(0..3)]));
assert!(drain_groups(&mut bounded).is_empty());
let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else {
panic!("a fill is asked below the start");
};
fill.reject_groups(0..3, Error::Old);
drop(fill);
assert_eq!(drained_drops(&mut subscriber), [(0, 3)]);
assert_eq!(
drained_drops(&mut bounded),
[(0, 3)],
"the readers waiting now are refused"
);
assert!(dynamic.poll_requested_fill(&waiter).is_pending());
assert_eq!(
producer.state.read().drops.get(&0),
Some(&Hole::stale(max_age)),
"held under the max age, which a widening reverses"
);

let mut later = producer.subscribe(terms());
assert_eq!(drain_groups(&mut later), [3]);
assert_eq!(drained_drops(&mut later), [(0, 3)]);
assert!(
dynamic.poll_requested_fill(&waiter).is_pending(),
"the verdict is not asked again"
);

assert_eq!(producer.supersede_stale(None), RangeSet::from_iter([0..3]));
let mut again = producer.subscribe(terms());
assert_eq!(drain_groups(&mut again), [3]);
assert!(
matches!(dynamic.poll_requested_fill(&waiter), Poll::Ready(Ok(_))),
"a forgotten verdict is asked again"
);
}

/// Every group a set of ranges names, as a received copy's upstream terms judge them.
#[cfg(feature = "async")]
fn judged(runs: impl IntoIterator<Item = (u64, u64)>) -> Option<RangeSet<u64>> {
Expand Down
Loading