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
30 changes: 30 additions & 0 deletions bench/tree-op-model/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
WRITE_TICK_FOLD_ENTRIES_PER_PASS,
WRITE_TICK_GC_MAX_SWEEPS,
} from "@baerly/protocol";
import { TREE_GC_TICK_RESERVED_CALLS } from "../../packages/server/src/tree/constants.ts";
import { treeGcCandidateCharge } from "../../packages/server/src/tree/gc.ts";
import { loadRequestCountSnapshot, parseCell } from "../../scripts/request-counts.ts";
import {
ALL_ATTEMPTS,
Expand All @@ -22,6 +24,9 @@ import {
findLogTailGets,
FOLD_RECORD_CREATES,
foldTickCalls,
treeGcCandidateCalls,
treeGcClassAPerMinute,
treeGcTickBound,
GC_WORST_STATE,
gcTickCalls,
type GcTickState,
Expand Down Expand Up @@ -424,3 +429,28 @@ describe("Paid ledger", () => {
},
);
});

describe("tree GC tick", () => {
test("the static bound at 64 checks, depth 0 to 4, and at 512 checks on depth 2", () => {
expect([0, 1, 2, 3, 4].map((d) => treeGcTickBound(64, d, false) - 3)).toEqual([
235, 235, 619, 811, 1003,
]);
expect(treeGcTickBound(64, 1, true)).toBe(304);
expect(treeGcTickBound(512, 2, true)).toBe(4720);
expect(treeGcTickBound(64, 0, true)).toBeLessThanOrEqual(900);
});

test("the model agrees with the tick's own sizing", () => {
for (const depth of [0, 1, 2, 3]) {
expect(treeGcCandidateCalls(depth)).toBe(treeGcCandidateCharge(depth));
}
expect(treeGcTickBound(0, 0, true)).toBe(TREE_GC_TICK_RESERVED_CALLS);
});

test("tree GC's Class A rate: a record per fold and one probe tick a minute", () => {
// One fold a minute: its record, then the probe's two state writes and two LISTs.
expect(treeGcClassAPerMinute(1)).toBe(5);
// No folds: the probe alone, however many writes tick.
expect(treeGcClassAPerMinute(0)).toBe(4);
});
});
41 changes: 41 additions & 0 deletions bench/tree-op-model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,47 @@ export const gcTickCalls = (input: GcTickInput): Calls => {
);
};

// ── Tree GC tick ─────────────────────────────────────────────────────────

/**
* Charged calls of one tree GC candidate check at root level `depth`:
* its DELETE plus its membership GETs, each charged at every attempt.
* At depth 0 or 1 membership is decided from the key alone; above, a
* check GETs the candidate and at most `depth − 1` path nodes
* (`packages/server/src/tree/gc-member.ts`).
*/
export const treeGcCandidateCalls = (depth: number): number =>
depth <= 1 ? STORAGE_RETRY_MAX_ATTEMPTS : STORAGE_RETRY_MAX_ATTEMPTS * (depth + 1);

/**
* The static bound on one tree GC tick's charged calls with `candidates`
* checks at root level `depth` (`packages/server/src/tree/gc.ts`): the
* state GET, the sweep and record LISTs, the head and root GETs, 4
* record GETs and 4 record DELETEs (each charged at every attempt), one
* state create (retried) and one state CAS (sent once), the checks, and
* log retirement (its gate GET alone, or 69 when it finds work).
*/
export const treeGcTickBound = (
candidates: number,
depth: number,
retirementFindsWork: boolean,
): number => {
const a = STORAGE_RETRY_MAX_ATTEMPTS;
const fixed = 5 * a + 4 * a + 4 * a + a + 1;
const retire = retirementFindsWork ? 3 * a + 20 * a : a;
return fixed + candidates * treeGcCandidateCalls(depth) + retire;
};

/**
* Class A operations per minute that tree GC adds at `foldsPerMinute`
* folds a minute: each fold attempt's record create, and per probe tick
* (at most one a minute, the only ticks that collect) two state writes,
* the sweep's LIST page and the fold records' LIST page. Ticks between
* probes make only Class B calls, so the rate does not grow with the
* write rate.
*/
export const treeGcClassAPerMinute = (foldsPerMinute: number): number => foldsPerMinute + 4;

// ── Reads ────────────────────────────────────────────────────────────────

export interface ReadInput {
Expand Down
46 changes: 46 additions & 0 deletions packages/server/src/tree/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ export const TREE_GC_SWEEP_AGE_MINUTES = 60;
*/
export const TREE_GC_CLOCK_PROBE_MINUTES = 1;

/**
* How far, in local-clock minutes, a tick's local time must read before
* the previous probe's local time for the tick to probe early. Writers
* on different instances share one state, and their local clocks
* differ: a smaller backward reading is skew between instances, so the
* tick waits for the probe interval as usual instead of probing on every
* tick. Only a jump this large, a real clock reset, re-probes early.
* Local time is only ever compared with local time; it never decides a
* deletion.
*/
export const TREE_GC_CLOCK_RESET_MINUTES = 60;

/**
* Candidate checks one GC tick makes, the sweep page's listed keys
* included: the floor a tick uses wherever its ledger does not admit
Expand Down Expand Up @@ -262,3 +274,37 @@ export const TREE_GC_MAX_WORK_ENTRIES = 256;
* key left unmarked is examined again on the next sweep pass.
*/
export const TREE_GC_MAX_SWEEP_MARKS = 256;

/**
* Most candidate checks one GC tick makes when its request ledger has
* room: on the Workers Paid ledger a tick of this many checks at depth 2
* costs about 4,700 calls with log retirement, about half the ledger,
* and drains a whole-tree rewrite's record in a few ticks instead of
* dozens. A ledger with less room gets {@link TREE_GC_TICK_CANDIDATES}.
*/
export const TREE_GC_TICK_MAX_CANDIDATES = 512;

/**
* Calls a GC tick may make besides its candidate checks, each charged
* at every attempt: the state GET, two LISTs, the head and root GETs,
* {@link TREE_GC_TICK_RECORD_CALLS} record GETs and as many record
* DELETEs (3 each), one state create (3) and one state CAS (1), plus 69
* for a log-retirement pass that finds work (its gate GET, CAS GET and
* PUT, and 20 DELETEs). The tick sizes its candidate count from what
* the ledger has left after these.
*/
export const TREE_GC_TICK_RESERVED_CALLS = 43 + 69;

/** The `format` tag of the tree GC state. */
export const TREE_GC_STATE_FORMAT = "baerly-tree-gc-state-proto";

/** The `schema_version` of the tree GC state. */
export const TREE_GC_STATE_SCHEMA_VERSION = 1;

/**
* Decoder ceiling on the tree GC state body, in bytes. A state at its
* caps ({@link TREE_GC_MAX_WORK_ENTRIES} work entries and
* {@link TREE_GC_MAX_SWEEP_MARKS} marks, a few hundred bytes each) is
* well under it.
*/
export const TREE_GC_STATE_MAX_BODY_BYTES = 524_288;
Loading