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: 7 additions & 1 deletion packages/sdk/src/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export type BlockPrefetchOpts = {
prefetch: number;
/** Total reserved ranges (in flight + buffered). Default 2× prefetch. */
buffer?: number;
/** Aggregate byte reservation. Defaults to the 256 MiB transport cap. */
maxReservedBytes?: number;
fetch: (start: number, end: number, signal: AbortSignal) => Promise<Uint8Array>;
signal?: AbortSignal;
cancelled?: () => boolean;
Expand All @@ -42,6 +44,10 @@ export function createBlockPrefetch(opts: BlockPrefetchOpts): BlockPrefetch {
}
const prefetch = Math.max(1, Math.floor(opts.prefetch));
const buffer = Math.max(prefetch, Math.floor(opts.buffer ?? prefetch * 2));
const maxReservedBytes = opts.maxReservedBytes ?? COMPACT_PREFETCH_MAX_BYTES;
if (!Number.isSafeInteger(maxReservedBytes) || maxReservedBytes < COMPACT_RANGE_WORKING_BYTES) {
throw new Error(`invalid prefetch maxReservedBytes: ${opts.maxReservedBytes}`);
}
const cancelled = opts.cancelled ?? (() => false);
let nextFetch = opts.start;
let nextApply = opts.start;
Expand Down Expand Up @@ -95,7 +101,7 @@ export function createBlockPrefetch(opts: BlockPrefetchOpts): BlockPrefetch {
if (cancelled() || opts.signal?.aborted) dispose();
if (!live || failed) return;
while (inFlight < prefetch && ready.size + inFlight < buffer && nextFetch <= opts.tip &&
reservedBytes() + COMPACT_RANGE_WORKING_BYTES <= COMPACT_PREFETCH_MAX_BYTES) {
reservedBytes() + COMPACT_RANGE_WORKING_BYTES <= maxReservedBytes) {
const start = nextFetch;
const end = Math.min(opts.tip, start + opts.batch - 1);
nextFetch = end + 1;
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/src/transport-bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
export const COMPACT_RANGE_MAX_BYTES = 32 * 1024 * 1024;
export const COMPACT_RANGE_WORKING_BYTES = 2 * COMPACT_RANGE_MAX_BYTES;
export const COMPACT_PREFETCH_MAX_BYTES = 256 * 1024 * 1024;
// Eight Private Cloud batches. Each reserves one working range, and each batch
// is two 500-block posts, which fills the gateway's 16 block permits.
export const COMPACT_PREFETCH_PRIVATE_CLOUD_MAX_BYTES = 8 * COMPACT_RANGE_WORKING_BYTES;
10 changes: 7 additions & 3 deletions packages/sdk/src/wasm-client-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { createLightServerRecovery, LightServerUnavailableError, type LightServerRecovery } from "./light-server-recovery";
import { assertCompactBlockHeight, isTransientLightServerError } from "./lwd";
import { createBlockPrefetch } from "./prefetch";
import { COMPACT_PREFETCH_PRIVATE_CLOUD_MAX_BYTES } from "./transport-bounds";
import { supportsTransparentCompact, syncPublicData } from "./public-data";
import {
isTreeConflictError,
Expand Down Expand Up @@ -333,9 +334,11 @@ export function createSyncController(ctx: Pick<WasmClientContext, "onProgress" |
}
}
const tuned = syncTuning(ctx.localLight, ctx.syncOpts);
// Private Cloud can send two 500-block reads for each batch. Allow up to
// eight batches while the prefetch byte budget limits memory in flight.
const shieldedPrefetch = ctx.transport.kind === "private-cloud"
// Private Cloud sends two 500-block posts for each batch. Eight batches
// fill the gateway's 16 block permits when the reservation cap allows
// every batch to stay in flight together.
const privateCloud = ctx.transport.kind === "private-cloud";
const shieldedPrefetch = privateCloud
? 8
: ctx.grpcWeb
? grpcWebPrefetch(scanWorkerRuntime()?.threads, runtime.radioConstrained())
Expand Down Expand Up @@ -507,6 +510,7 @@ export function createSyncController(ctx: Pick<WasmClientContext, "onProgress" |
batch,
prefetch,
buffer: prefetchBuffer(prefetch),
maxReservedBytes: privateCloud && prefetch === 8 ? COMPACT_PREFETCH_PRIVATE_CLOUD_MAX_BYTES : undefined,
fetch: (h, end, signal) => {
if (combinedTransparent) {
assertCombinedTransparentCurrent();
Expand Down
26 changes: 26 additions & 0 deletions packages/sdk/test/prefetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from "node:assert/strict";
import { test } from "node:test";
import { WalletError } from "@z-stack/core";
import { createBlockPrefetch } from "../src/prefetch.ts";
import { COMPACT_PREFETCH_PRIVATE_CLOUD_MAX_BYTES, COMPACT_RANGE_WORKING_BYTES } from "../src/transport-bounds.ts";
import { createLightServerRecovery, LightServerUnavailableError } from "../src/light-server-recovery.ts";
import { isTransientLightServerError, TransientLightServerError } from "../src/lwd.ts";
import { setImmediate as turn } from "node:timers/promises";
Expand Down Expand Up @@ -471,6 +472,31 @@ test("prefetch 8 starts four HTTP ranges within the working byte budget before a
assert.equal(started.length, 4);
});

test("private cloud reservation starts eight ranges before any apply", async () => {
assert.equal(COMPACT_PREFETCH_PRIVATE_CLOUD_MAX_BYTES, 8 * COMPACT_RANGE_WORKING_BYTES);
let inFlight = 0;
let max = 0;
const started: number[] = [];
const prefetch = createBlockPrefetch({
start: 1,
tip: 16_000,
batch: 1_000,
prefetch: 8,
buffer: 16,
maxReservedBytes: COMPACT_PREFETCH_PRIVATE_CLOUD_MAX_BYTES,
fetch: (start) => {
started.push(start);
inFlight += 1;
max = Math.max(max, inFlight);
return new Promise(() => {});
},
});
await delay(20);
prefetch.dispose();
assert.equal(max, 8, `expected eight reserved in-flight ranges, max ${max}`);
assert.deepEqual(started, [1, 1001, 2001, 3001, 4001, 5001, 6001, 7001]);
});

test("consumeDelimitedStream pages a chunked body", async () => {
const { consumeDelimitedStream } = await import("../src/lwd.ts");
const frame = (n: number) => {
Expand Down
Loading