Skip to content

Commit 319fda3

Browse files
author
hzq
committed
Bind reader handoff measurements to the originating request
Capture the release evidence when the first local page starts. Ignore unrelated or late frames and pace the ceiling fixture without catch-up bursts, while retaining the absolute deadline and quota assertions.
1 parent 1776018 commit 319fda3

3 files changed

Lines changed: 99 additions & 25 deletions

File tree

‎bench/upload-reader-handoff.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ try {
7070
firstLocalRowid: string | null = null,
7171
firstLocalIndex: string | null = null,
7272
checkpointAtFirstLocal = 0,
73+
releasedAtFirstLocal = 0,
7374
exitedAtFirstLocal = 0,
7475
completedDuringNative = 0,
7576
rowsAtFirstLocal: number | null = null;
@@ -87,7 +88,8 @@ try {
8788
? 'file_owner_subject'
8889
: 'file_upload_facts';
8990
checkpointAtFirstLocal = observe.checkpointRows();
90-
exitedAtFirstLocal = observe.exited();
91+
releasedAtFirstLocal = observe.releasedRows();
92+
exitedAtFirstLocal = Number(observe.primaryExited());
9193
}
9294
localQueries++;
9395
rowsAtFirstLocal ??= observe.rows();
@@ -192,13 +194,13 @@ try {
192194
native_gate_entries: observe.entries(),
193195
progress_reports: observe.reports(),
194196
checkpoint_order: observe.checkpointOrder(),
195-
acknowledged_release_rows: observe.releasedRows(),
197+
acknowledged_release_rows: firstLocalIndex ? releasedAtFirstLocal : observe.releasedRows(),
196198
checkpoint_at_first_local: checkpointAtFirstLocal,
197199
exited_at_first_local: exitedAtFirstLocal,
198200
handed_off_rows:
199201
firstLocalRowid !== null &&
200202
firstLocalRowid ===
201-
String(observe.releasedRows() || (exitedAtFirstLocal ? checkpointAtFirstLocal : 0))
203+
String(releasedAtFirstLocal || (exitedAtFirstLocal ? checkpointAtFirstLocal : 0))
202204
? Number(firstLocalRowid)
203205
: 0,
204206
first_local_after_rowid: firstLocalRowid,

‎test/fixtures/upload-reader-probe.ts‎

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,60 @@ import { createRequire, syncBuiltinESMExports } from 'node:module';
44
import { DatabaseSync } from 'node:sqlite';
55
import { isMainThread, workerData } from 'node:worker_threads';
66
import type { WorkerOptions } from 'node:worker_threads';
7+
import type { ReadCommand, ReadMessage } from '../../src/store/upload-read-worker.js';
8+
import { transferablePrefix } from '../../src/store/upload-usage-page.js';
79

810
export type ReaderScenario = 'progress' | 'blocked' | 'ceiling' | 'exit' | 'steady';
911
type Probe = { origin: string; gate: string; state: SharedArrayBuffer; scenario: ReaderScenario };
1012
const rows = 0,
1113
entered = 1,
1214
left = 2,
1315
sleepSlot = 3;
16+
17+
/** Observe the first paced request. A late frame from a cancelled/retired read or an
18+
* unrelated subscriber must not overwrite the evidence used for the first local page. */
19+
export class PrefixWitness {
20+
private source?: object;
21+
private id?: string;
22+
private cancelled = false;
23+
private retired = false;
24+
exited = false;
25+
reports = 0;
26+
checkpoint = 0;
27+
order: string | null = null;
28+
released = 0;
29+
post(source: object, command: ReadCommand) {
30+
if ('cancel' in command) {
31+
if (this.source === source && this.id === command.cancel) this.cancelled = true;
32+
} else if (!this.source && command.owner === 'owner') {
33+
this.source = source;
34+
this.id = command.id;
35+
}
36+
}
37+
retire(source: object) {
38+
if (this.source === source && this.cancelled) this.retired = true;
39+
}
40+
exit(source: object) {
41+
if (this.source === source) this.exited = true;
42+
}
43+
frame(source: object, m: ReadMessage) {
44+
if (this.source !== source || !this.id || m.id !== this.id || this.retired || this.exited)
45+
return;
46+
if (m.kind === 'progress' && !this.cancelled) {
47+
this.reports++;
48+
if (transferablePrefix(m.prefix) && m.rows === m.prefix.rows && m.rows > this.checkpoint) {
49+
this.checkpoint = m.rows;
50+
this.order = m.prefix.next.from;
51+
}
52+
} else if (m.kind === 'released' && this.cancelled) {
53+
this.released =
54+
transferablePrefix(m.prefix) && m.prefix.rows >= this.checkpoint
55+
? m.prefix.rows
56+
: this.checkpoint;
57+
this.retired = true;
58+
}
59+
}
60+
}
1461
export function observeReader(gate: string, scenario: ReaderScenario, workerUrl?: string) {
1562
const shared = new SharedArrayBuffer(16),
1663
state = new Int32Array(shared);
@@ -20,12 +67,10 @@ export function observeReader(gate: string, scenario: ReaderScenario, workerUrl?
2067
const original = builtin.Worker;
2168
let opened = 0,
2269
exited = 0,
23-
progressReports = 0,
24-
checkpointRows = 0,
25-
checkpointOrder: string | null = null,
26-
releasedRows = 0,
2770
terminationAt: number | undefined;
71+
const witness = new PrefixWitness();
2872
class Observed extends original {
73+
private watched = false;
2974
constructor(file: string | URL, options?: WorkerOptions) {
3075
const watching = options?.workerData?.role === 'oaa.upload-usage' && file instanceof URL;
3176
super(
@@ -46,25 +91,26 @@ export function observeReader(gate: string, scenario: ReaderScenario, workerUrl?
4691
: options,
4792
);
4893
if (watching) {
94+
this.watched = true;
4995
opened++;
5096
this.on('message', (m) => {
51-
if (m.kind === 'progress') {
52-
progressReports++;
53-
if (m.prefix) {
54-
checkpointRows = m.prefix.rows;
55-
checkpointOrder = m.prefix.next.from;
56-
}
57-
if (scenario === 'exit' && progressReports === 1)
58-
setImmediate(() => void this.terminate());
59-
}
60-
if (m.kind === 'released') releasedRows = m.prefix?.rows ?? 0;
97+
const before = witness.reports;
98+
witness.frame(this, m);
99+
if (scenario === 'exit' && !before && witness.reports === 1)
100+
setImmediate(() => void this.terminate());
61101
});
62102
this.once('exit', () => {
63103
exited++;
104+
witness.exit(this);
64105
});
65106
}
66107
}
108+
override postMessage(...args: Parameters<typeof original.prototype.postMessage>) {
109+
super.postMessage(...args);
110+
if (this.watched) witness.post(this, args[0]);
111+
}
67112
override terminate() {
113+
witness.retire(this);
68114
terminationAt ??= performance.now();
69115
return super.terminate();
70116
}
@@ -78,10 +124,11 @@ export function observeReader(gate: string, scenario: ReaderScenario, workerUrl?
78124
exited: () => exited,
79125
opened: () => opened,
80126
terminationAt: () => terminationAt,
81-
reports: () => progressReports,
82-
checkpointRows: () => checkpointRows,
83-
checkpointOrder: () => checkpointOrder,
84-
releasedRows: () => releasedRows,
127+
reports: () => witness.reports,
128+
checkpointRows: () => witness.checkpoint,
129+
checkpointOrder: () => witness.order,
130+
releasedRows: () => witness.released,
131+
primaryExited: () => witness.exited,
85132
restore: () => {
86133
builtin.Worker = original;
87134
syncBuiltinESMExports();
@@ -115,8 +162,10 @@ if (!isMainThread && workerData?.probe && workerData?.role === 'oaa.upload-usage
115162
) {
116163
// Pace actual visited rows on one monotonic timeline. A late OS wake-up
117164
// must not turn the slow-progress fixture into the absolute-deadline case.
118-
const due = firstVisit + (n / 64) * (p.scenario === 'ceiling' ? 80 : 24);
119-
const remaining = due - performance.now();
165+
// The ceiling scenario must not catch up after a long deschedule and suddenly
166+
// enlarge a native page beyond the cancellation window.
167+
const remaining =
168+
p.scenario === 'ceiling' ? 80 : firstVisit + (n / 64) * 24 - performance.now();
120169
if (remaining > 0) Atomics.wait(state, sleepSlot, 0, remaining);
121170
}
122171
if (p.scenario === 'blocked' && n === 2048) {

‎test/upload-reader-handoff.test.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,35 @@ import { Store } from '../src/store/store.js';
1313
import { ColdUploadReads } from '../src/store/upload-cold-reads.js';
1414
import { checkUploadRoom } from '../src/store/upload-budget.js';
1515
import { databaseIdentity, readerProtocol } from '../src/store/upload-read-worker.js';
16-
import { observeReader } from './fixtures/upload-reader-probe.js';
16+
import { observeReader, PrefixWitness } from './fixtures/upload-reader-probe.js';
1717
import { setup, waitFor } from './helpers.js';
1818
import { readUsagePage, type UsagePrefix } from '../src/store/upload-usage-page.js';
1919

2020
const n = 5400,
2121
scope = { owner: 'o', subject: 's' };
22+
test('reader handoff evidence belongs to one request and excludes late progress and release frames', () => {
23+
const source = {},
24+
other = {},
25+
id = 'primary',
26+
prefix = (n: number) => ({
27+
rows: n,
28+
bytes: n,
29+
next: { from: 'charge-ties' as const, key: [null, 1, String(n)] as [null, number, string] },
30+
});
31+
const w = new PrefixWitness();
32+
w.post(source, { id, owner: 'owner', subject: 'one' });
33+
w.frame(source, { kind: 'progress', id, rows: 64, prefix: prefix(64) });
34+
w.frame(other, { kind: 'progress', id, rows: 128, prefix: prefix(128) });
35+
w.frame(source, { kind: 'progress', id: 'unrelated', rows: 128, prefix: prefix(128) });
36+
w.post(source, { cancel: id });
37+
w.frame(source, { kind: 'progress', id, rows: 192, prefix: prefix(192) });
38+
w.retire(source);
39+
w.frame(source, { kind: 'released', id, prefix: prefix(256) });
40+
w.exit(other);
41+
assert.deepEqual([w.checkpoint, w.released, w.exited], [64, 0, false]);
42+
w.exit(source);
43+
assert.equal(w.exited, true);
44+
});
2245
test('reader handoff: older gateways receive a terminal legacy reply, never an intermediate frame', async () => {
2346
const worker = new Worker(new URL('../src/store/upload-read-worker.js', import.meta.url), {
2447
workerData: { role: 'oaa.upload-usage' },
@@ -297,7 +320,7 @@ test(
297320
const r = JSON.parse(stdout);
298321
assert.equal(r.first.status, 200);
299322
assert.ok(r.observed_finish_ms >= 15000);
300-
assert.ok(r.handed_off_rows > 0 && r.handed_off_rows < r.history_rows);
323+
assert.ok(r.handed_off_rows > 0 && r.handed_off_rows < r.history_rows, JSON.stringify(r));
301324
assert.equal(r.first_local_after_rowid, String(r.handed_off_rows));
302325
const proven =
303326
r.acknowledged_release_rows || (r.exited_at_first_local ? r.checkpoint_at_first_local : 0);

0 commit comments

Comments
 (0)