Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(sync): raise pull response cap to 8 MB
Elian Doran89cc6f89eb94
diff --git a/packages/trilium-core/src/routes/api/sync.spec.ts b/packages/trilium-core/src/routes/api/sync.spec.tsindex 917ee4cfd2..054aa2d6af 100644--- a/packages/trilium-core/src/routes/api/sync.spec.ts+++ b/packages/trilium-core/src/routes/api/sync.spec.ts@@ -1,3 +1,4 @@+import type { EntityChange } from "@triliumnext/commons";import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";import consistencyChecksService from "../../services/consistency_checks";@@ -197,6 +198,30 @@ describe("Sync API (core)", () => {sql.execute("DELETE FROM entity_changes WHERE id IN (?, ?)", [maxId + 1, maxId + 2]);sql.execute("DELETE FROM options WHERE name = 'otst_opt'");});++ it("getEntityChangeRecords stops accumulating once the response byte cap is reached", () => {+ const sql = getSql();+ const maxId = sql.getValue<number>("SELECT COALESCE(MAX(id), 0) FROM entity_changes");+ const changes: EntityChange[] = [];+ for (let i = 0; i < 3; i++) {+ sql.execute("INSERT INTO options (name, value, isSynced, utcDateModified) VALUES (?, 'v', 1, '2020-01-01T00:00:00.000Z')", [`cap_opt_${i}`]);+ sql.execute(+ `INSERT INTO entity_changes (id, entityName, entityId, hash, isErased, changeId, componentId, instanceId, isSynced, utcDateChanged)+ VALUES (?, 'options', ?, 'h', 0, ?, 'cmp', 'OTHER', 1, '2020-01-01T00:00:00.000Z')`,+ [maxId + 1 + i, `cap_opt_${i}`, `cap_chg_${i}`]+ );+ changes.push(sql.getRow<EntityChange>("SELECT * FROM entity_changes WHERE id = ?", [maxId + 1 + i]));+ }++ // Each option record estimates ~300 bytes, so a 400-byte cap stops right after the second+ // record crosses it (the crossing record is still included).+ expect(syncService.getEntityChangeRecords(changes, 400)).toHaveLength(2);+ // With the (large) default cap all three are returned.+ expect(syncService.getEntityChangeRecords(changes)).toHaveLength(3);++ sql.execute("DELETE FROM entity_changes WHERE id BETWEEN ? AND ?", [maxId + 1, maxId + 3]);+ for (let i = 0; i < 3; i++) sql.execute("DELETE FROM options WHERE name = ?", [`cap_opt_${i}`]);+ });});describe("update (PUT /api/sync/update)", () => {diff --git a/packages/trilium-core/src/services/sync.spec.ts b/packages/trilium-core/src/services/sync.spec.tsindex 7aba8f0578..e49447625f 100644--- a/packages/trilium-core/src/services/sync.spec.ts+++ b/packages/trilium-core/src/services/sync.spec.ts@@ -254,8 +254,8 @@ describe("sync service", () => {const records = syncService.getEntityChangeRecords([ec({ entityName: "blobs", entityId: "sync_big_blob" }),ec({ entityName: "blobs", entityId: "sync_small_blob" })- ]);- // The big blob fills the budget so the small one is not included.+ ], 1_000_000);+ // The big blob alone exceeds the (explicit) cap, so the small one is not included.expect(records).toHaveLength(1);expect(records[0]?.entityChange.entityId).toBe("sync_big_blob");});diff --git a/packages/trilium-core/src/services/sync.ts b/packages/trilium-core/src/services/sync.tsindex dea7277882..d94e55d5f7 100644--- a/packages/trilium-core/src/services/sync.ts+++ b/packages/trilium-core/src/services/sync.ts@@ -449,7 +449,18 @@ function getEntityChangeRow(entityChange: EntityChange) {}-function getEntityChangeRecords(entityChanges: EntityChange[]) {+/**+ * Soft upper bound on the (estimated) size of a single pull response. Larger responses mean fewer+ * HTTP round-trips, which is the dominant cost of an initial sync over a high-latency link; on a 2 GB+ * benchmark, going from 1 MB to 8 MB cut the request count ~3x (1030 -> 333). It stays well under the+ * tens-of-MB responses a single large blob already produces (so it is within what existing reverse+ * proxies tolerate), and below the client's pull-batch memory budget, so it does not raise the+ * receiver's peak memory. It is a soft cap: a response is at least one record, and the record that+ * crosses the threshold is still included.+ */+const MAX_PULL_RESPONSE_BYTES = 8 * 1024 * 1024;++function getEntityChangeRecords(entityChanges: EntityChange[], maxResponseBytes = MAX_PULL_RESPONSE_BYTES) {const records: EntityChangeRecord[] = [];let length = 0;@@ -471,8 +482,7 @@ function getEntityChangeRecords(entityChanges: EntityChange[]) {length += estimateEntityChangeRecordSize(record);- if (length > 1_000_000) {- // each sync request/response should have at most ~1 MB.+ if (length > maxResponseBytes) {break;}}