Bootstrap LiveStore mirror from persisted catch-up history
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
93686a84b3
commit
2dd87351ac
5 files changed
+192
No files matched your search
@@ -108,6 +108,7 @@ The server supports feature-flagged sync-engine modes while migrating from the l
|
||||
- `T3CODE_SYNC_ENGINE_MODE=livestore-read-pilot` (default)
|
||||
- Uses the LiveStore mirror for `state.bootstrap`, `state.catchUp`, and `state.listMessages` reads when available.
|
||||
- Automatically falls back to legacy reads if the mirror is unavailable or errors.
|
||||
- Mirror state is pre-bootstrapped from persisted catch-up history during server startup.
|
||||
- `T3CODE_SYNC_ENGINE_MODE=livestore`
|
||||
- Uses the LiveStore-backed read path and disables delegate fallback (strict mirror-read mode).
|
||||
- `T3CODE_SYNC_ENGINE_MODE=legacy`
|
||||
|
||||
@@ -11,6 +11,10 @@ Server mode is controlled by `T3CODE_SYNC_ENGINE_MODE`:
|
||||
- `legacy`: legacy persistence service is canonical for reads/writes.
|
||||
- `shadow`: legacy remains canonical, and `state.event` writes are mirrored into LiveStore.
|
||||
|
||||
Startup behavior:
|
||||
|
||||
- when running `shadow`, `livestore-read-pilot`, or `livestore`, the mirror is bootstrapped from persisted catch-up history before serving requests.
|
||||
|
||||
Optional safety gate:
|
||||
|
||||
- `T3CODE_LIVESTORE_ENFORCE_MODE=1`
|
||||
|
||||
@@ -6,6 +6,7 @@ import { fileURLToPath } from "node:url";
|
||||
|
||||
import { fixPath } from "./fixPath";
|
||||
import { createLogger } from "./logger";
|
||||
import { bootstrapMirrorFromCatchUp } from "./livestore/mirrorBootstrap";
|
||||
import { LiveStoreStateMirror } from "./livestore/liveStoreEngine";
|
||||
import { PersistenceService } from "./persistenceService";
|
||||
import { LegacyStateSyncEngine } from "./stateSyncEngineLegacy";
|
||||
@@ -144,6 +145,20 @@ async function main() {
|
||||
storeId: `t3-shadow-${mode}`,
|
||||
});
|
||||
const isReadPilotMode = syncEngineMode === "livestore-read-pilot" || syncEngineMode === "livestore";
|
||||
if (syncEngineMode === "shadow" || isReadPilotMode) {
|
||||
const bootstrapResult = bootstrapMirrorFromCatchUp({
|
||||
source: legacyStateSyncEngine,
|
||||
mirror: liveStoreMirror,
|
||||
logger,
|
||||
failOnError: syncEngineMode === "livestore",
|
||||
});
|
||||
logger.info("bootstrapped livestore mirror from persisted catch-up history", {
|
||||
mirroredCount: bootstrapResult.mirroredCount,
|
||||
lastStateSeq: bootstrapResult.lastStateSeq,
|
||||
complete: bootstrapResult.complete,
|
||||
syncEngineMode,
|
||||
});
|
||||
}
|
||||
const stateSyncEngine =
|
||||
syncEngineMode === "shadow"
|
||||
? new ShadowStateSyncEngine({
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createLogger } from "../logger";
|
||||
import { bootstrapMirrorFromCatchUp } from "./mirrorBootstrap";
|
||||
|
||||
describe("bootstrapMirrorFromCatchUp", () => {
|
||||
it("mirrors catch-up history into the mirror sink", () => {
|
||||
const mirrorStateEvent = vi.fn();
|
||||
const source = {
|
||||
catchUp: vi.fn().mockReturnValue({
|
||||
events: [
|
||||
{
|
||||
seq: 1,
|
||||
entityId: "project-1",
|
||||
eventType: "project.upsert",
|
||||
payload: { id: "project-1", name: "Demo", path: "/demo" },
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
seq: 2,
|
||||
entityId: "thread-1",
|
||||
eventType: "thread.upsert",
|
||||
payload: { id: "thread-1", title: "Thread" },
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
lastStateSeq: 2,
|
||||
}),
|
||||
};
|
||||
|
||||
const result = bootstrapMirrorFromCatchUp({
|
||||
source,
|
||||
mirror: { mirrorStateEvent },
|
||||
logger: createLogger("mirror-bootstrap-test"),
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
mirroredCount: 2,
|
||||
lastStateSeq: 2,
|
||||
complete: true,
|
||||
});
|
||||
expect(mirrorStateEvent).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("returns partial progress when non-fatal mirroring errors occur", () => {
|
||||
const mirrorStateEvent = vi
|
||||
.fn()
|
||||
.mockImplementationOnce(() => {})
|
||||
.mockImplementationOnce(() => {
|
||||
throw new Error("mirror write failed");
|
||||
});
|
||||
const source = {
|
||||
catchUp: vi.fn().mockReturnValue({
|
||||
events: [
|
||||
{
|
||||
seq: 1,
|
||||
entityId: "project-1",
|
||||
eventType: "project.upsert",
|
||||
payload: { id: "project-1", name: "Demo", path: "/demo" },
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
seq: 2,
|
||||
entityId: "thread-1",
|
||||
eventType: "thread.upsert",
|
||||
payload: { id: "thread-1", title: "Thread" },
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
lastStateSeq: 2,
|
||||
}),
|
||||
};
|
||||
|
||||
const result = bootstrapMirrorFromCatchUp({
|
||||
source,
|
||||
mirror: { mirrorStateEvent },
|
||||
logger: createLogger("mirror-bootstrap-test"),
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
mirroredCount: 1,
|
||||
lastStateSeq: 2,
|
||||
complete: false,
|
||||
});
|
||||
expect(mirrorStateEvent).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("throws when failOnError is enabled", () => {
|
||||
const mirrorStateEvent = vi.fn().mockImplementation(() => {
|
||||
throw new Error("mirror write failed");
|
||||
});
|
||||
const source = {
|
||||
catchUp: vi.fn().mockReturnValue({
|
||||
events: [
|
||||
{
|
||||
seq: 1,
|
||||
entityId: "project-1",
|
||||
eventType: "project.upsert",
|
||||
payload: { id: "project-1", name: "Demo", path: "/demo" },
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
lastStateSeq: 1,
|
||||
}),
|
||||
};
|
||||
|
||||
expect(() =>
|
||||
bootstrapMirrorFromCatchUp({
|
||||
source,
|
||||
mirror: { mirrorStateEvent },
|
||||
logger: createLogger("mirror-bootstrap-test"),
|
||||
failOnError: true,
|
||||
}),
|
||||
).toThrow(/failed to bootstrap livestore mirror/i);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import type { StateCatchUpResult, StateEvent } from "@t3tools/contracts";
|
||||
|
||||
interface CatchUpSource {
|
||||
catchUp(input: { afterSeq: number }): StateCatchUpResult;
|
||||
}
|
||||
|
||||
interface MirrorSink {
|
||||
mirrorStateEvent(event: StateEvent): void;
|
||||
}
|
||||
|
||||
interface LoggerLike {
|
||||
warn(message: string, context?: Record<string, unknown>): void;
|
||||
}
|
||||
|
||||
export interface MirrorBootstrapResult {
|
||||
mirroredCount: number;
|
||||
lastStateSeq: number;
|
||||
complete: boolean;
|
||||
}
|
||||
|
||||
export function bootstrapMirrorFromCatchUp(options: {
|
||||
source: CatchUpSource;
|
||||
mirror: MirrorSink;
|
||||
logger: LoggerLike;
|
||||
failOnError?: boolean;
|
||||
}): MirrorBootstrapResult {
|
||||
const { source, mirror, logger, failOnError = false } = options;
|
||||
const catchUpResult = source.catchUp({ afterSeq: 0 });
|
||||
let mirroredCount = 0;
|
||||
for (const event of catchUpResult.events) {
|
||||
try {
|
||||
mirror.mirrorStateEvent(event);
|
||||
mirroredCount += 1;
|
||||
} catch (error) {
|
||||
if (failOnError) {
|
||||
throw new Error("failed to bootstrap livestore mirror from catch-up history", {
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
logger.warn("failed to bootstrap livestore mirror event; continuing with partial mirror state", {
|
||||
error,
|
||||
eventSeq: event.seq,
|
||||
mirroredCount,
|
||||
});
|
||||
return {
|
||||
mirroredCount,
|
||||
lastStateSeq: catchUpResult.lastStateSeq,
|
||||
complete: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
return {
|
||||
mirroredCount,
|
||||
lastStateSeq: catchUpResult.lastStateSeq,
|
||||
complete: true,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user