mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-10 05:12:14 +00:00
An account of 1.25 million measurements could not export its data or be restored from any backup (#1031). Measured in a 1 GB container: - The full export and the passphrase export built the whole payload and its JSON string in memory; the app ran out of heap and restarted. - The weekly backup's JSON is 662 MB, longer than any string V8 can hold, so `unpackBackupBlob` threw on every read. Restore, download and the restore preview all answered "could not be decrypted". - Upload accepted 10 MB, a fraction of a large export. Every reader and writer of a backup file now streams it: - `scanBackupJson` reads a backup's bytes once, keeps every section but the named bulk arrays, and hands their elements out one at a time. `readStreamedBackup` uses it twice: a first pass that validates each measurement against the element schema and keeps the rest of the file, and a second pass that hands the measurements to the writer in batches. Stored copies and off-host objects are authenticated whole on their compressed ciphertext before any plaintext is read; only the decompression is streamed. - Restore writes the measurements with one `INSERT … FROM unnest` per batch. Prisma caches a query plan per `createMany` row count, and one 1 000-row plan kept about 145 MB alive, which alone put the restore past the heap limit. The transaction budget grows with the measurement count. The awards and ECG sections are handed the ids they reference that were written, not every written id. - The export routes write the file into the response as it is produced. The passphrase export spools its ciphertext to a temporary file because the HLX1 format puts the tag first; the format is unchanged, so existing archives open as before. - Upload accepts the raw body, JSON or gzip, up to the 512 MB request ceiling, checks it as it arrives and stores it in the same pass; the admin page compresses the file in the browser before sending it. Multipart stays for small files. - Download streams the stored JSON; the preview counts measurements from the stream. The monthly restore drill and `scripts/restore-backup.ts` read the off-host object the same way; the script can write `.json.gz`. On the seeded account in a 1 GB container: export 25 s at 389 MB peak, passphrase export 25 s, both uploads 19 s, restore of the weekly backup 94 s at 554 MB peak, and export, upload and restore round-trip to exactly the same rows. The encrypted-column guard now also recognises the raw SQL statement that writes `DataBackup.data`.
186 lines
5.4 KiB
TypeScript
186 lines
5.4 KiB
TypeScript
/**
|
|
* `scanBackupJson` must read a backup exactly as `JSON.parse` of the whole
|
|
* document would, with the streamed members handed out one element at a time
|
|
* (issue #1031: a 662 MB backup cannot exist as one string). These tests hold
|
|
* it to that over awkward values and arbitrary chunk boundaries, including
|
|
* boundaries inside strings, escapes and multi-byte characters.
|
|
*/
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { BackupJsonError, scanBackupJson } from "../backup-json-scan";
|
|
|
|
async function* chunked(text: string, sizes: number[]) {
|
|
const bytes = Buffer.from(text, "utf8");
|
|
let at = 0;
|
|
let i = 0;
|
|
while (at < bytes.length) {
|
|
const size = sizes[i++ % sizes.length]!;
|
|
yield bytes.subarray(at, at + size);
|
|
at += size;
|
|
}
|
|
}
|
|
|
|
function rng(seed: number) {
|
|
let x = seed;
|
|
return () => {
|
|
x = (x * 1103515245 + 12345) % 2 ** 31;
|
|
return x / 2 ** 31;
|
|
};
|
|
}
|
|
|
|
const AWKWARD = [
|
|
'quote " inside',
|
|
"back\\slash and \\n",
|
|
"brackets ] } [ { , : in a string",
|
|
"umlaut ä ö ü and emoji 🫀",
|
|
"",
|
|
"\u0000\u001f control",
|
|
];
|
|
|
|
function randomValue(rand: () => number, depth: number): unknown {
|
|
const r = rand();
|
|
if (depth > 2 || r < 0.35) {
|
|
const p = rand();
|
|
if (p < 0.2) return null;
|
|
if (p < 0.3) return rand() < 0.5;
|
|
if (p < 0.55) return Math.round(rand() * 1e6) / 100 - 500;
|
|
return (
|
|
AWKWARD[Math.floor(rand() * AWKWARD.length)]! + Math.floor(rand() * 9)
|
|
);
|
|
}
|
|
if (r < 0.65) {
|
|
return Array.from({ length: Math.floor(rand() * 4) }, () =>
|
|
randomValue(rand, depth + 1),
|
|
);
|
|
}
|
|
const obj: Record<string, unknown> = {};
|
|
for (let i = 0; i < Math.floor(rand() * 4); i++) {
|
|
obj[`${AWKWARD[i % AWKWARD.length]}-${i}`] = randomValue(rand, depth + 1);
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
function randomDocument(seed: number) {
|
|
const rand = rng(seed);
|
|
return {
|
|
schemaVersion: "2",
|
|
userId: "user-ä",
|
|
measurements: Array.from({ length: Math.floor(rand() * 30) }, (_, i) =>
|
|
i % 5 === 4
|
|
? randomValue(rand, 1)
|
|
: { id: `m${i}`, notes: AWKWARD[i % 6] },
|
|
),
|
|
medications: [randomValue(rand, 0), randomValue(rand, 0)],
|
|
moodEntries: [],
|
|
count: 42,
|
|
flag: false,
|
|
nothing: null,
|
|
text: AWKWARD[seed % AWKWARD.length],
|
|
nested: randomValue(rand, 0),
|
|
};
|
|
}
|
|
|
|
async function scan(text: string, sizes: number[], streamKeys: string[]) {
|
|
const elements: Record<string, unknown[]> = {};
|
|
const result = await scanBackupJson(chunked(text, sizes), {
|
|
streamKeys: new Set(streamKeys),
|
|
onElement: (key, element, index) => {
|
|
(elements[key] ??= [])[index] = element;
|
|
},
|
|
});
|
|
return { ...result, elements };
|
|
}
|
|
|
|
describe("scanBackupJson", () => {
|
|
it("equals JSON.parse over random documents, formats and chunk splits", async () => {
|
|
for (let seed = 1; seed <= 200; seed++) {
|
|
const doc = randomDocument(seed);
|
|
const text =
|
|
seed % 3 === 0
|
|
? JSON.stringify(doc, null, seed % 2 ? 2 : "\t")
|
|
: JSON.stringify(doc);
|
|
const sizes = [1 + (seed % 7), 2, 13, 1, 64].slice(seed % 3);
|
|
const { document, streamedCounts, elements } = await scan(text, sizes, [
|
|
"measurements",
|
|
"moodEntries",
|
|
]);
|
|
const expected = JSON.parse(text) as Record<string, unknown>;
|
|
|
|
expect(elements.measurements ?? [], `seed ${seed}`).toEqual(
|
|
expected.measurements,
|
|
);
|
|
expect(streamedCounts).toEqual({
|
|
measurements: (expected.measurements as unknown[]).length,
|
|
moodEntries: 0,
|
|
});
|
|
expect(document, `seed ${seed}`).toEqual({
|
|
...expected,
|
|
measurements: [],
|
|
moodEntries: [],
|
|
});
|
|
}
|
|
});
|
|
|
|
it("hands out streamed elements before it has read the rest of the document", async () => {
|
|
const text = JSON.stringify({
|
|
a: 1,
|
|
measurements: [{ id: "x" }, { id: "y" }],
|
|
tail: "z".repeat(100),
|
|
});
|
|
const bytes = Buffer.from(text);
|
|
let delivered = 0;
|
|
const seenAt: number[] = [];
|
|
async function* source() {
|
|
for (let i = 0; i < bytes.length; i += 4) {
|
|
delivered = i + 4;
|
|
yield bytes.subarray(i, i + 4);
|
|
}
|
|
}
|
|
await scanBackupJson(source(), {
|
|
streamKeys: new Set(["measurements"]),
|
|
onElement: () => {
|
|
seenAt.push(delivered);
|
|
},
|
|
});
|
|
expect(seenAt).toHaveLength(2);
|
|
expect(seenAt[1]!).toBeLessThan(bytes.length - 100);
|
|
});
|
|
|
|
it("skips a member it was told to skip without keeping it", async () => {
|
|
const { document } = await scanBackupJson(
|
|
chunked('{"a":[1,{"b":"]"}],"big":{"x":[1,2,3]},"c":"d"}', [3]),
|
|
{ skipKeys: new Set(["big"]) },
|
|
);
|
|
expect(document).toEqual({ a: [1, { b: "]" }], c: "d" });
|
|
});
|
|
|
|
it("refuses a truncated or malformed document", async () => {
|
|
for (const bad of [
|
|
'{"a":1',
|
|
'{"a":[1,2}',
|
|
"[1,2]",
|
|
'{"measurements":{"x":1}}',
|
|
'{"a":1}{',
|
|
'{"measurements":[{"id":1} {"id":2}]}',
|
|
]) {
|
|
await expect(
|
|
scanBackupJson(chunked(bad, [2]), {
|
|
streamKeys: new Set(["measurements"]),
|
|
}),
|
|
bad,
|
|
).rejects.toBeInstanceOf(BackupJsonError);
|
|
}
|
|
});
|
|
|
|
it("propagates a failure from the element consumer", async () => {
|
|
await expect(
|
|
scanBackupJson(chunked('{"measurements":[1,2,3]}', [5]), {
|
|
streamKeys: new Set(["measurements"]),
|
|
onElement: (_key, _element, index) => {
|
|
if (index === 1) throw new Error("consumer failed");
|
|
},
|
|
}),
|
|
).rejects.toThrow("consumer failed");
|
|
});
|
|
});
|