Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(sync): warmup document on wakeup (#3154)

teocddc30d1b7ae
diff --git a/rust/sync-service/src/durable_object.rs b/rust/sync-service/src/durable_object.rs
index 3a6331008f..c673d2ec11 100644
--- a/rust/sync-service/src/durable_object.rs
+++ b/rust/sync-service/src/durable_object.rs
@@ -307,7 +307,7 @@ impl DocumentSyncSession {
.peer_handler(document_id, matched.params.get("peer_id"))
.await;
}
- path::WAKEUP => self.wakeup(),
+ path::WAKEUP => return self.wakeup(document_id).await,
// These need auth
rest => {
let claims = or_unauth!(decode_jwt(&req, &self.env, TokenFrom::Headers).ok());
@@ -489,11 +489,25 @@ impl DocumentSyncSession {
ResponseBuilder::new().from_json(&PeerResponse { peer_id, user_id })
}
- fn wakeup(&self) -> Result<Response> {
+ async fn wakeup(&self, document_id: &str) -> Result<Response> {
+ let _ = self.warmup(document_id).await.inspect_err(
+ |error| warn!(document_id = document_id, error = ?error, "failed to warm up document"),
+ );
+
let out = keepalive(DEFAULT_TIME_TO_LIVE);
ResponseBuilder::new().from_json(&out)
}
+ async fn warmup(&self, document_id: &str) -> Result<()> {
+ if !self.exists(document_id).await? {
+ return Ok(());
+ }
+
+ self.session_storage().await?;
+ self.document_state().await?;
+ Ok(())
+ }
+
async fn metadata_handler(&self, document_id: &str) -> Result<Response> {
if !self.exists(document_id).await? {
return Ok(response(status_codes::NOT_FOUND));
diff --git a/rust/sync-service/tests/copy_endpoint.test.ts b/rust/sync-service/tests/copy_endpoint.test.ts
index f52777aa72..39c429e21c 100644
--- a/rust/sync-service/tests/copy_endpoint.test.ts
+++ b/rust/sync-service/tests/copy_endpoint.test.ts
@@ -75,6 +75,7 @@ describe("document copy endpoint tests", () => {
headers: {
"Content-Type": "application/json",
},
+ credentials: "omit",
body: JSON.stringify({ target_document_id: "unauthorized-copy" }),
}
);
diff --git a/rust/sync-service/tests/debug_endpoint.test.ts b/rust/sync-service/tests/debug_endpoint.test.ts
index 07bd1c0e15..bc7f41296b 100644
--- a/rust/sync-service/tests/debug_endpoint.test.ts
+++ b/rust/sync-service/tests/debug_endpoint.test.ts
@@ -1,6 +1,7 @@
import { Miniflare } from "miniflare";
+import { LoroDoc } from "loro-crdt";
import { expect, test, describe, beforeEach } from "vitest";
-import { log, createTestUser, getTokenForDocument, INTERNAL_API_SECRET, setupMiniflare, sleep } from "./utils";
+import { createTestUser, INTERNAL_API_SECRET, setupMiniflare, sleep } from "./utils";
let mf: Miniflare;
@@ -24,6 +25,16 @@ const callWakeupEndpoint = (docId: string, headers = {...admin_headers}) => {
return callEndpoint('wakeup', docId, headers);
};
+const putSnapshotInKv = async (docId: string, content: string) => {
+ const doc = new LoroDoc();
+ doc.getText('content').push(content);
+ doc.commit();
+
+ const snapshot = doc.export({ mode: 'snapshot' });
+ const kv = await mf.getKVNamespace('SNAPSHOT_STORE_KV');
+ await kv.put(`${docId}/${docId}.snapshot`, snapshot);
+};
+
const expectValidDebugResponse = async (response: Response) => {
expect(response.status).toBe(200);
const data = await response.json();
@@ -102,5 +113,16 @@ describe("wakeup endpoint tests", async () => {
expect(first).toBeNull();
let [_tid, duration] = await(await callWakeupEndpoint("empty-doc")).json();
expect(duration).toBe(60 * 1000);
- })
+ });
+
+ test("wakeup initializes storage from an existing snapshot", async () => {
+ const documentId = "kv-wakeup-doc";
+ await putSnapshotInKv(documentId, "Warm me");
+
+ const wakeupResponse = await callWakeupEndpoint(documentId);
+ expect(wakeupResponse.status).toBe(200);
+
+ const response = await callEndpoint("debug_do_kv_list/o", documentId, admin_headers);
+ expect(response.status).toBe(200);
+ });
});
diff --git a/rust/sync-service/tests/utils.ts b/rust/sync-service/tests/utils.ts
index 28e9de6380..bf7715554c 100644
--- a/rust/sync-service/tests/utils.ts
+++ b/rust/sync-service/tests/utils.ts
@@ -35,6 +35,7 @@ export async function setupMiniflare() {
scriptPath: "./build/worker/shim.mjs",
modules: true,
modulesRules: [
+ { type: "ESModule", include: ["**/build/index.js"] },
{ type: "CompiledWasm", include: ["**/*.wasm"], fallthrough: true },
],
durableObjects: {