Public: anyone can read this; the crew can change it.

Sentinel: memory-growth was degraded 14 minutes today: The forge's own memory is 4774 MiB, over twice its first-hour 1779 MiB and past the 2176 MiB more its database caches m… #17860

opened this

Merged 30h 22m ago, live in production

mason/forge:mason/17851-sentinel-memory-growth-was-degraded-14-m → main 0 commits fork

  • Merged. mason/17851-sentinel-memory-growth-was-degraded-14-m was merged into main as 8d72195.

Review

Sign in to review this change.
3 files changed +49 −16
docs/features/memory-growth.md +2
@@ -39,6 +39,8 @@ The sentinel's memory-growth issue quotes the watch's evidence: each VM's heap,
3939
4040 **A smaller pile between collections (#17760).** The finding after #17558 had `snapshot-stream` open across the trip again, the main heap at 465 MiB in the first hour, 1393 MiB at the trip and 604 MiB now (down from 2655 MiB), and the memory outside every heap 845 MiB to 2417 MiB and back to 891 MiB: native buffers a pass cut and let go, held until a collection. The pass now asks for a collection each 64 MiB of plain bytes sent (was 256) and `settler` runs a full one 128 MiB above the last (was 512), so the pile a pass can leave is a quarter of what it was. Not measured on production's data: the next finding's `memory outside every heap` line at the trip says whether it was the whole. Test: `test/snapshot-settle.test.ts`.
4141
42 **One copy of the bucket's keys between forges (#17851).** The finding after #17760, on a release that had it, had `snapshot-stream` open across the trip again, the main heap at 431 MiB in the first hour and 1685 MiB at the trip, the memory outside every heap 1221 to 1899 MiB, and every held count flat. Each forge's check before its manifest made a `Set` of a fresh listing of the whole bucket while the pass's view, a `Set` of the same keys, was still held: two copies of every key, plus the listing's own array, with nothing sent to call a collection. `missingChunks` now walks the listing against the few keys the manifest names and makes no `Set` of it, and the view handed on is the one the forge kept (what it started from and every chunk it sent); the check is still a fresh listing, and a view that claims a chunk the store lost fails that check, writes no manifest and is dropped, so the next forge lists afresh. Measured off production with a listing of a million keys and four snapshots in a row: the main heap's peak 281 MiB against 323 to 575 MiB before, 146 MiB held after either. Not measured on production's data: the next finding's `memory heap main` line at the trip says whether it was the whole. Test: `test/snapshot-stream.test.ts`.
43
4244 A restart between the growth and the filing (a release restarts the forge several times a day) leaves a watch that never saw it: the sentence is the stored one of the process that grew, and its own evidence would say `trip none`. So each minute the need is open, the sampler writes the watch's evidence to `memory-kept.json` under the data directory (`perf/memory_kept.ts`, #16872); a finding filed by a later process that has not tripped quotes those lines, kept that day, under `memory kept by the process started …, at its last grown minute …; the forge restarted since`, and ends with one `memory now (process started …)` line for the process reading it.
4345
4446 The need clears by itself once RSS falls under twice the baseline. A process that slept through its first hour has no baseline and raises none.
src/snapshot/snapshot.ts +18 −16
@@ -169,16 +169,16 @@ export function chunksNamed(m: Manifest): string[] {
169169 export const storedChunk = async (store: Store, id: string): Promise<string> => (await store.storedKey?.(chunkKey(id))) ?? chunkKey(id);
170170
171171 /**
172 * The chunks among `ids` the store does not list, by one fresh listing. `listed` is handed that
173 * listing, for a caller that would otherwise list again.
172 * The chunks among `ids` the store does not list, by one fresh listing, in the order of `ids`. The
173 * listing is walked against the few keys asked for, never made a `Set` of its own: on a bucket of
174 * a million keys that Set was a second copy of the whole bucket on the heap (#17851).
174175 */
175 export async function missingChunks(store: Store, ids: readonly string[], listed?: (there: Set<string>) => void): Promise<string[]> {
176 export async function missingChunks(store: Store, ids: readonly string[]): Promise<string[]> {
176177 if (ids.length === 0) return [];
177 const there = new Set(await store.list(CHUNK_PREFIX));
178 listed?.(there);
179 const missing: string[] = [];
180 for (const id of ids) if (!there.has(await storedChunk(store, id))) missing.push(id);
181 return missing;
178 const wanted = new Map<string, string>();
179 for (const id of ids) wanted.set(await storedChunk(store, id), id);
180 for (const key of await store.list(CHUNK_PREFIX)) wanted.delete(key);
181 return [...wanted.values()];
182182 }
183183
184184 /** How far a running snapshot is: chunks sent and their bytes as stored. */
@@ -354,9 +354,10 @@ export interface Snapshotter {
354354 readonly stream?: string | undefined;
355355 /**
356356 * The store's chunks as one stream pass last listed them, shared by its forges one after
357 * another (#17205): a forge starts from it in place of a listing of its own, and leaves it the
358 * fresh listing its check before the manifest made. A forge that throws leaves it empty, so the
359 * next one lists afresh.
357 * another (#17205): a forge starts from it in place of a listing of its own, and leaves it what
358 * it started from and the chunks it sent, once its check before the manifest found every chunk it
359 * names in a fresh listing (#17851). A forge that throws leaves it empty, so the next one lists
360 * afresh.
360361 */
361362 readonly chunks?: ChunkView | undefined;
362363 /**
@@ -375,8 +376,8 @@ export interface Snapshotter {
375376 const SETTLE_BYTES = 64 * 1024 * 1024;
376377
377378 /**
378 * What a run of snapshots knows of the store's chunks between them: `keys` as `list` gave them,
379 * or undefined when the next must list.
379 * What a run of snapshots knows of the store's chunks between them: `keys` in the form `list` gives
380 * them, or undefined when the next must list.
380381 */
381382 export interface ChunkView {
382383 keys?: Set<string> | undefined;
@@ -876,15 +877,16 @@ export async function takeSnapshot(
876877 if (refused !== undefined) throw new Error(`the stream of ${refused.name} was not written: ${refused.why}`);
877878 // Announced only when whole: every chunk it names, seen in the store now.
878879 const named = chunksNamed(manifest);
879 let fresh: Set<string> | undefined;
880 const missing = await missingChunks(store, named, (listed) => void (fresh = listed));
880 const missing = await missingChunks(store, named);
881881 if (missing.length > 0)
882882 throw new Error(`${missing.length} of the ${named.length} chunks this snapshot names are not in ${store.where} (first ${missing[0]}); no manifest written`);
883883 await store.put(
884884 forge.stream === undefined ? `${prefix}manifest.json` : streamManifestKey(forge.stream),
885885 JSON.stringify(manifest, null, 2),
886886 );
887 if (forge.chunks !== undefined) forge.chunks.keys = fresh;
887 // The view goes on as this forge kept it: what it started from and every chunk it sent, each
888 // seen landed. One Set of the bucket's keys, not that and a second of the check's listing.
889 if (forge.chunks !== undefined) forge.chunks.keys = there;
888890 log.info("snapshot taken", {
889891 name,
890892 where: store.where,
test/snapshot-stream.test.ts +29
@@ -266,6 +266,35 @@ describe("the stream of hosted forges", () => {
266266 expect((await restoreHostedInto(inner, "stream", "kid", join(root, "after-view"), { passphrase: PASS })).repos).toBe(1);
267267 });
268268
269 test("a forge hands on the view it kept, not a second Set of its check's listing (#17851)", async () => {
270 const store = dirStore(join(root, `store-${n++}`));
271 await takeSnapshot({ ...shape(), stream: "kid" }, store);
272 const started = new Set(await store.list(CHUNK_PREFIX));
273 const chunks: ChunkView = { keys: started };
274 await takeSnapshot({ ...shape(), stream: "kid", chunks }, store);
275 // The same Set, grown by what was sent: the bucket's keys are held once between forges.
276 expect(chunks.keys).toBe(started);
277 expect([...chunks.keys!].sort()).toEqual((await store.list(CHUNK_PREFIX)).sort());
278 });
279
280 test("a view that claims a chunk the store lost writes no manifest, and the next forge lists afresh (#17851)", async () => {
281 const store = dirStore(join(root, `store-${n++}`));
282 await takeSnapshot({ ...shape(), stream: "kid" }, store);
283 const before = await store.get(streamManifestKey("kid"));
284 const keys = await store.list(CHUNK_PREFIX);
285 const gone = keys[0]!;
286 const body = (await store.get(gone))!;
287 await store.delete(gone);
288 // The view still names it, so the forge does not send it again: the fresh check finds it gone.
289 const chunks: ChunkView = { keys: new Set(keys) };
290 await expect(takeSnapshot({ ...shape(), stream: "kid", chunks }, store)).rejects.toThrow("no manifest written");
291 expect(new TextDecoder().decode((await store.get(streamManifestKey("kid")))!)).toBe(new TextDecoder().decode(before!));
292 expect(chunks.keys).toBeUndefined();
293 await takeSnapshot({ ...shape(), stream: "kid", chunks }, store);
294 expect(await store.get(gone)).toEqual(body);
295 expect((await restoreHostedInto(store, "stream", "kid", join(root, "after-lost"), { passphrase: PASS })).repos).toBe(1);
296 });
297
269298 test("the MCP system tool's stream read gives an admin each forge's lag and refuses anyone else", async () => {
270299 const store = dirStore(join(root, `store-${n++}`));
271300 await pass(store, new Date());

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs