Skip to content

Build your personal knowledge base with Trilium Notes

core/chunked uploader: improve resiliency

Adorian Doran568f27e69d9b
diff --git a/apps/server/src/services/chunked_upload.spec.ts b/apps/server/src/services/chunked_upload.spec.ts
index 778f545d8e..19acbeab39 100644
--- a/apps/server/src/services/chunked_upload.spec.ts
+++ b/apps/server/src/services/chunked_upload.spec.ts
@@ -281,10 +281,72 @@ describe("chunked upload: starting and ending a session", () => {
await expect(upload.begin(beginRequest(8))).resolves.toBeTruthy();
});
- it("answers for an upload it has never heard of", async () => {
+ it("answers for an upload id it never issued, which is no upload of its that has ended", async () => {
const { upload } = uploadService();
- await expect(upload.status(sessionRequest("nosuchupload"))).rejects.toThrow(/No such upload/);
+ await expect(upload.status(sessionRequest("nosuchupload"))).rejects.toMatchObject({ statusCode: 410 });
+ });
+});
+
+describe("chunked upload: an upload that is no longer there", () => {
+ it("lets a new upload take the place of one that is in the way, where it is configured to", async () => {
+ const { upload } = uploadService({ whenAtCapacity: "supersede" });
+ const abandoned = await upload.begin(beginRequest(8));
+ await upload.chunk(chunkRequest(abandoned.uploadId, 0, Buffer.alloc(4)));
+
+ const started = await upload.begin(beginRequest(8));
+
+ expect(started.uploadId).not.toBe(abandoned.uploadId);
+ expect(upload.activeSessions()).toBe(1);
+ expect(fs.existsSync(partialPath(tempRoot, abandoned.uploadId))).toBe(false);
+ });
+
+ it("tells the upload that was replaced what became of it, rather than letting it read a timeout into it", async () => {
+ const { upload } = uploadService({ whenAtCapacity: "supersede" });
+ const replaced = await upload.begin(beginRequest(8));
+ await upload.begin(beginRequest(8));
+
+ await expect(upload.chunk(chunkRequest(replaced.uploadId, 0, Buffer.alloc(4)))).rejects.toMatchObject({
+ message: expect.stringContaining("Another upload took over"),
+ statusCode: 410
+ });
+ });
+
+ it("keeps a running upload when the request that would have replaced it is malformed", async () => {
+ const { upload } = uploadService({ whenAtCapacity: "supersede", maxTotalBytes: 100 });
+ const running = await upload.begin(beginRequest(8));
+
+ await expect(upload.begin(beginRequest(101))).rejects.toThrow(/above the limit/);
+ await expect(upload.begin(beginRequest(undefined))).rejects.toThrow(/positive integer/);
+
+ await expect(upload.chunk(chunkRequest(running.uploadId, 0, Buffer.alloc(4)))).resolves.toBeTruthy();
+ });
+
+ it("recognises an upload id from the process that ran before it, which has no session to match it", async () => {
+ // Two services stand in for two runs of the server: sessions live in memory, so an id from
+ // one is meaningless to the other, and the client holding it should be told so rather than
+ // told its upload expired.
+ const before = uploadService().upload;
+ const after = uploadService().upload;
+ const { uploadId } = await before.begin(beginRequest(8));
+
+ await expect(after.status(sessionRequest(uploadId))).rejects.toMatchObject({
+ message: expect.stringContaining("The server restarted"),
+ statusCode: 410
+ });
+ });
+
+ it("answers a `finish` that is asked again with what it answered the first time", async () => {
+ // The file is complete and the answer to it was lost on the way back. Sending the whole
+ // upload a second time is the only other way out of that, and it is hours long.
+ const { upload, completed } = uploadService();
+ const { uploadId } = await upload.begin(beginRequest(4));
+ await upload.chunk(chunkRequest(uploadId, 0, Buffer.alloc(4)));
+
+ const assembledPath = await upload.finish(sessionRequest(uploadId));
+
+ await expect(upload.finish(sessionRequest(uploadId))).resolves.toBe(assembledPath);
+ expect(completed).toHaveLength(1);
});
});
diff --git a/apps/server/src/services/chunked_upload.ts b/apps/server/src/services/chunked_upload.ts
index d62676dd2b..c843613e18 100644
--- a/apps/server/src/services/chunked_upload.ts
+++ b/apps/server/src/services/chunked_upload.ts
@@ -1,4 +1,4 @@
-import { ConflictError, NotFoundError, utils as coreUtils, ValidationError } from "@triliumnext/core";
+import { ConflictError, GoneError, type HttpError, NotFoundError, utils as coreUtils, ValidationError } from "@triliumnext/core";
import { Mutex } from "async-mutex";
import type { Request } from "express";
import { createWriteStream } from "fs";
@@ -17,6 +17,11 @@ import { pipeline } from "stream/promises";
* one session all fail that check, and a client that lost a response only has to ask where the file
* got to and carry on from there.
*
+ * Sessions live in memory while their files do not, so an upload cannot outlive the process that is
+ * receiving it. What it can do is say so: every upload id is stamped with the process that issued
+ * it, and one presented to a later process is answered as gone rather than as expired, which is the
+ * difference between a client waiting out a retry and one starting again.
+ *
* Nothing here knows what the file is for. A consumer supplies {@link ChunkedUploadConfig.onComplete},
* which is handed the assembled file and takes ownership of it.
*
@@ -40,6 +45,8 @@ export const DEFAULT_CHUNK_SIZE = 32 * 1024 * 1024;
const DEFAULT_SESSION_TTL_MS = 60 * 60 * 1000;
const SWEEP_INTERVAL_MS = 5 * 60 * 1000;
+/** How many endings are kept, which only has to cover the clients still talking about them. */
+const REMEMBERED_ENDINGS = 16;
/** The assembled file, handed to the consumer once every byte is in. */
export interface CompletedUpload {
@@ -69,6 +76,16 @@ export interface ChunkedUploadConfig<T> {
sessionTtlMs?: number;
/** Sessions that may be open at once. Defaults to one. */
maxConcurrentSessions?: number;
+ /**
+ * What one more session than {@link maxConcurrentSessions} allows does: `refuse` turns the new
+ * one away, `supersede` ends the oldest to make room for it. Defaults to refusing.
+ *
+ * Superseding is for an upload that is the only way into what it feeds. A client whose
+ * connection is gone cannot tell the server to forget its session, so that session stands in the
+ * way for the whole of {@link sessionTtlMs} — and the user, who knows only that their upload
+ * failed, is refused every attempt to start it again for the next hour.
+ */
+ whenAtCapacity?: "refuse" | "supersede";
/**
* Checks the filesystem has room for the whole file before accepting it, so an upload that
* cannot possibly fit fails at the start rather than after hours.
@@ -130,8 +147,19 @@ export function createChunkedUpload<T>(config: ChunkedUploadConfig<T>): ChunkedU
const maxConcurrentSessions = config.maxConcurrentSessions ?? 1;
const directory = path.resolve(config.directory, config.name);
const sessions = new Map<string, Session>();
+ /** What became of the sessions that ended recently, for a client that comes back to one. */
+ const endings = new Map<string, Ending<T>>();
/** Lets one caller at a time open a session, so the limit on how many are open holds. */
const admission = new Mutex();
+ /**
+ * Stamped onto every upload id this process issues, so one issued by the process before it is
+ * recognised as such. A restart ends every session while leaving the client mid-upload none the
+ * wiser, and "the server restarted" is something it can act on where "no such upload" is not.
+ *
+ * Minted when it is first wanted rather than here: an upload service is built as its module
+ * loads, which is before there is any crypto to draw a random string from.
+ */
+ let instanceId: string | null = null;
// Unreferenced so neither a test nor a shutdown waits on it.
const timer = setInterval(() => void sweep(), SWEEP_INTERVAL_MS);
@@ -156,9 +184,9 @@ export function createChunkedUpload<T>(config: ChunkedUploadConfig<T>): ChunkedU
// Before the count is checked, so an abandoned session never blocks the next one.
await sweep();
- if (sessions.size >= maxConcurrentSessions) {
- throw new ConflictError("Another upload is already in progress.");
- }
+ // Everything the request can be turned away for comes first: where a new session takes the
+ // place of an old one, a malformed request must not cost a healthy upload its session on its
+ // way to being refused.
if (typeof totalBytes !== "number" || !Number.isSafeInteger(totalBytes) || totalBytes <= 0) {
throw new ValidationError("The size of the upload must be stated as a positive integer.");
}
@@ -167,13 +195,29 @@ export function createChunkedUpload<T>(config: ChunkedUploadConfig<T>): ChunkedU
`The upload is ${totalBytes} bytes, above the limit of ${config.maxTotalBytes}.`
);
}
+
+ while (sessions.size >= maxConcurrentSessions) {
+ if (config.whenAtCapacity !== "supersede") {
+ throw new ConflictError("Another upload is already in progress.");
+ }
+
+ // Oldest first, the map keeping them in the order they were opened. Remembered as
+ // superseded, so the client it belonged to is told what became of it rather than left to
+ // conclude that its own upload timed out.
+ const [ oldest ] = sessions.values();
+ remember(oldest.uploadId, { kind: "superseded" });
+ await drop(oldest);
+ }
+
+ // After the room an ended session gave back, which is the difference between fitting and not
+ // when the upload being replaced is another copy of the same database.
if (config.requireFreeSpace) {
await requireRoomFor(directory, totalBytes);
}
await fsp.mkdir(directory, { recursive: true });
- const uploadId = coreUtils.randomString(20);
+ const uploadId = `${stamp()}-${coreUtils.randomString(20)}`;
const session: Session = {
uploadId,
// A base name, so a crafted name cannot reach outside the upload directory. Only ever
@@ -248,6 +292,14 @@ export function createChunkedUpload<T>(config: ChunkedUploadConfig<T>): ChunkedU
}
async function finish(req: Request): Promise<T> {
+ // A `finish` whose answer never arrived is asked again, and by then the session is gone and
+ // the file with it. Without the answer being kept, the only way back for that client would
+ // be to send the whole upload a second time.
+ const completed = endings.get(String(req.params.uploadId));
+ if (completed?.kind === "completed") {
+ return completed.result;
+ }
+
const session = sessionFor(req);
return session.writing.runExclusive(async () => {
@@ -262,12 +314,15 @@ export function createChunkedUpload<T>(config: ChunkedUploadConfig<T>): ChunkedU
sessions.delete(session.uploadId);
try {
- return await config.onComplete({
+ const result = await config.onComplete({
path: session.path,
fileName: session.fileName,
totalBytes: session.totalBytes,
metadata: session.metadata
});
+ remember(session.uploadId, { kind: "completed", result });
+
+ return result;
} catch (e) {
// The consumer may have taken the file or may have failed before touching it; either
// way nothing else will come back for it.
@@ -312,13 +367,48 @@ export function createChunkedUpload<T>(config: ChunkedUploadConfig<T>): ChunkedU
}
function sessionFor(req: Request): Session {
- const session = sessions.get(String(req.params.uploadId));
+ const uploadId = String(req.params.uploadId);
+ const session = sessions.get(uploadId);
if (!session) {
- throw new NotFoundError("No such upload. It may have expired.");
+ throw endingOf(uploadId);
}
return session;
}
+ /**
+ * Why an upload is no longer here, in the terms a client can act on.
+ *
+ * The two that are worth telling apart from an expiry are the ones the client did not cause and
+ * cannot wait out: an upload that another took the place of, and one issued by the process that
+ * was running before this one. Both are final, where an expiry at least reads as something that
+ * took too long.
+ */
+ function endingOf(uploadId: string): HttpError {
+ if (endings.get(uploadId)?.kind === "superseded") {
+ return new GoneError("Another upload took over. Only one upload runs at a time.");
+ }
+ if (!uploadId.startsWith(`${stamp()}-`)) {
+ return new GoneError("The server restarted, so the upload it was holding is gone.");
+ }
+
+ return new NotFoundError("No such upload. It may have expired.");
+ }
+
+ /** This process's mark, made the first time anything needs it. */
+ function stamp(): string {
+ return instanceId ??= coreUtils.randomString(8);
+ }
+
+ /** Keeps the last few endings, so the map cannot grow with every upload the process ever saw. */
+ function remember(uploadId: string, ending: Ending<T>): void {
+ endings.set(uploadId, ending);
+
+ while (endings.size > REMEMBERED_ENDINGS) {
+ const [ oldest ] = endings.keys();
+ endings.delete(oldest);
+ }
+ }
+
/**
* Ends a session that produced nothing: expired, aborted, or contradicted what it declared.
*
@@ -377,6 +467,16 @@ interface Session {
writing: Mutex;
}
+/**
+ * What became of a session that is no longer open, for as long as a client may still ask after it.
+ *
+ * Only the endings a client has something to do about are kept. An expiry is not one of them: it is
+ * what an unknown upload id means already.
+ */
+type Ending<T> =
+ | { kind: "superseded" }
+ | { kind: "completed"; result: T };
+
interface BeginBody {
fileName?: string;
totalBytes?: number;
diff --git a/apps/server/src/services/restore_session.spec.ts b/apps/server/src/services/restore_session.spec.ts
index 912deda96e..a7223a7a92 100644
--- a/apps/server/src/services/restore_session.spec.ts
+++ b/apps/server/src/services/restore_session.spec.ts
@@ -81,15 +81,17 @@ describe("reserving setup for a backup that is coming", () => {
expect(getRunningSetupOperation()).toBe(null);
});
- it("keeps the reservation when a second upload is refused in favour of the first", async () => {
- const { uploadId } = await beginBackupUpload(beginRequest(8));
+ it("keeps the reservation when a second upload takes the place of the first", async () => {
+ const replaced = await beginBackupUpload(beginRequest(8));
- // Only one at a time, so this one is turned away — and must leave the running one's
- // reservation exactly where it found it.
- await expect(beginBackupUpload(beginRequest(8))).rejects.toThrow(/already in progress/);
+ // The newest attempt wins, since a user who is being refused because of an upload of their
+ // own that is already lost has nothing else they can do on this screen.
+ const { uploadId } = await beginBackupUpload(beginRequest(8));
+ // The reservation the replaced upload was standing on must not go with it: it is the same
+ // reservation the one replacing it now stands on.
expect(getRunningSetupOperation()).toBe("restore-backup");
- // Still the one that was accepted, and still receiving.
+ await expect(backupUpload.chunk(chunkRequest(replaced.uploadId, 0, Buffer.alloc(4)))).rejects.toThrow(/took over/);
await expect(backupUpload.chunk(chunkRequest(uploadId, 0, Buffer.alloc(4)))).resolves.toBeTruthy();
});
diff --git a/apps/server/src/services/restore_session.ts b/apps/server/src/services/restore_session.ts
index 8a0d08bffe..da94390da4 100644
--- a/apps/server/src/services/restore_session.ts
+++ b/apps/server/src/services/restore_session.ts
@@ -93,6 +93,10 @@ export const backupUpload = createChunkedUpload<PendingBackup>({
directory: path.join(dataDir.TMP_DIR, "uploads"),
maxTotalBytes: MAX_BACKUP_BYTES,
requireFreeSpace: true,
+ // The newest attempt wins. An upload whose connection is gone cannot say so, and until it aged
+ // out it left the user refused every time they tried again — on the one screen where trying
+ // again is all they can do, since a failed upload is what brought them back to it.
+ whenAtCapacity: "supersede",
// An upload that ends with nothing to show for it gives setup back. Expiry is the case that
// needs saying: it happens on a timer, with no request to fail and nobody to tell, and without
// this the instance would refuse every other way of setting itself up until it was restarted.
diff --git a/packages/trilium-core/src/errors.ts b/packages/trilium-core/src/errors.ts
index 68dc39a51f..f6e0ea18c0 100644
--- a/packages/trilium-core/src/errors.ts
+++ b/packages/trilium-core/src/errors.ts
@@ -38,6 +38,22 @@ export class ConflictError extends HttpError {
}
+/**
+ * What the request names is gone for good, so asking again will not bring it back.
+ *
+ * Distinct from a 404 in the one way that matters to a client holding a reference: a 404 may be a
+ * mistyped reference to something that never existed, while this is the thing it meant, and the
+ * answer is to start over rather than to retry.
+ */
+export class GoneError extends HttpError {
+
+ constructor(message: string) {
+ super(message, 410);
+ this.name = "GoneError";
+ }
+
+}
+
export class OpenIdError {
message: string;