Add source-labelled read-pilot logging and recovery test
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
fc14fdb569
commit
cbfe1e3a57
2 files changed
+125
-16
No files matched your search
@@ -305,4 +305,74 @@ describe("LiveStoreReadPilotStateSyncEngine", () => {
|
||||
engine.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("returns to mirror reads after transient fallback errors", () => {
|
||||
const delegate = new MockDelegateStateSyncEngine();
|
||||
const mirror = makeMirrorStub({
|
||||
snapshot: {
|
||||
projects: [],
|
||||
threads: [],
|
||||
lastStateSeq: 5,
|
||||
},
|
||||
catchUp: {
|
||||
events: [],
|
||||
lastStateSeq: 5,
|
||||
},
|
||||
listMessages: {
|
||||
messages: [],
|
||||
total: 0,
|
||||
nextOffset: null,
|
||||
},
|
||||
});
|
||||
vi.mocked(mirror.debugCatchUp)
|
||||
.mockImplementationOnce(() => {
|
||||
throw new Error("mirror catch-up failure");
|
||||
})
|
||||
.mockImplementation(() => ({
|
||||
events: [],
|
||||
lastStateSeq: 5,
|
||||
}));
|
||||
vi.mocked(mirror.debugListMessages)
|
||||
.mockImplementationOnce(() => {
|
||||
throw new Error("mirror list failure");
|
||||
})
|
||||
.mockImplementation(() => ({
|
||||
messages: [],
|
||||
total: 0,
|
||||
nextOffset: null,
|
||||
}));
|
||||
|
||||
const engine = new LiveStoreReadPilotStateSyncEngine({
|
||||
delegate,
|
||||
mirror,
|
||||
});
|
||||
|
||||
try {
|
||||
expect(engine.catchUp({ afterSeq: 0 })).toEqual({
|
||||
events: [],
|
||||
lastStateSeq: 1,
|
||||
});
|
||||
expect(engine.listMessages({ threadId: "thread-1", offset: 0, limit: 10 })).toEqual({
|
||||
messages: [],
|
||||
total: 0,
|
||||
nextOffset: null,
|
||||
});
|
||||
expect(delegate.catchUpMock).toHaveBeenCalledTimes(1);
|
||||
expect(delegate.listMessagesMock).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(engine.catchUp({ afterSeq: 0 })).toEqual({
|
||||
events: [],
|
||||
lastStateSeq: 5,
|
||||
});
|
||||
expect(engine.listMessages({ threadId: "thread-1", offset: 0, limit: 10 })).toEqual({
|
||||
messages: [],
|
||||
total: 0,
|
||||
nextOffset: null,
|
||||
});
|
||||
expect(delegate.catchUpMock).toHaveBeenCalledTimes(1);
|
||||
expect(delegate.listMessagesMock).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
engine.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -32,6 +32,8 @@ interface LiveStoreReadPilotEvents {
|
||||
stateEvent: [event: StateEvent];
|
||||
}
|
||||
|
||||
type LiveStoreReadSource = "delegate" | "livestore";
|
||||
|
||||
export interface LiveStoreReadPilotStateSyncEngineOptions {
|
||||
delegate: StateSyncEngine;
|
||||
mirror: LiveStoreStateMirror;
|
||||
@@ -48,7 +50,9 @@ export class LiveStoreReadPilotStateSyncEngine
|
||||
private readonly unsubscribeDelegate: () => void;
|
||||
private readonly enableBootstrapParityCheck: boolean;
|
||||
private closed = false;
|
||||
private bootstrapSource: "delegate" | "livestore" = "delegate";
|
||||
private bootstrapSource: LiveStoreReadSource = "delegate";
|
||||
private catchUpSource: LiveStoreReadSource = "delegate";
|
||||
private listMessagesSource: LiveStoreReadSource = "delegate";
|
||||
private bootstrapParityState: "unknown" | "in-parity" | "drift" = "unknown";
|
||||
|
||||
constructor(options: LiveStoreReadPilotStateSyncEngineOptions) {
|
||||
@@ -81,22 +85,21 @@ export class LiveStoreReadPilotStateSyncEngine
|
||||
if (this.enableBootstrapParityCheck) {
|
||||
this.checkBootstrapParity(snapshot);
|
||||
}
|
||||
if (this.bootstrapSource !== "livestore") {
|
||||
this.bootstrapSource = "livestore";
|
||||
this.logger.info("serving state.bootstrap from livestore mirror", {
|
||||
lastStateSeq: snapshot.lastStateSeq,
|
||||
});
|
||||
}
|
||||
this.logReadSourceChange("state.bootstrap", this.bootstrapSource, "livestore", {
|
||||
lastStateSeq: snapshot.lastStateSeq,
|
||||
});
|
||||
this.bootstrapSource = "livestore";
|
||||
return snapshot;
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.warn("failed to read bootstrap from livestore mirror; using delegate", { error });
|
||||
}
|
||||
if (this.bootstrapSource !== "delegate") {
|
||||
this.bootstrapSource = "delegate";
|
||||
this.logger.info("serving state.bootstrap from delegate fallback");
|
||||
}
|
||||
return this.delegate.loadSnapshot();
|
||||
const snapshot = this.delegate.loadSnapshot();
|
||||
this.logReadSourceChange("state.bootstrap", this.bootstrapSource, "delegate", {
|
||||
lastStateSeq: snapshot.lastStateSeq,
|
||||
});
|
||||
this.bootstrapSource = "delegate";
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
private checkBootstrapParity(mirrorSnapshot: StateBootstrapResult): void {
|
||||
@@ -130,29 +133,65 @@ export class LiveStoreReadPilotStateSyncEngine
|
||||
|
||||
listMessages(raw: StateListMessagesInput): StateListMessagesResult {
|
||||
try {
|
||||
return this.mirror.debugListMessages(raw);
|
||||
const result = this.mirror.debugListMessages(raw);
|
||||
this.logReadSourceChange("state.listMessages", this.listMessagesSource, "livestore", {
|
||||
threadId: raw.threadId,
|
||||
});
|
||||
this.listMessagesSource = "livestore";
|
||||
return result;
|
||||
} catch (error) {
|
||||
this.logger.warn("failed to list messages from livestore mirror; using delegate", {
|
||||
error,
|
||||
threadId: raw.threadId,
|
||||
});
|
||||
return this.delegate.listMessages(raw);
|
||||
const result = this.delegate.listMessages(raw);
|
||||
this.logReadSourceChange("state.listMessages", this.listMessagesSource, "delegate", {
|
||||
threadId: raw.threadId,
|
||||
});
|
||||
this.listMessagesSource = "delegate";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
catchUp(raw: StateCatchUpInput): StateCatchUpResult {
|
||||
const afterSeq = raw.afterSeq ?? 0;
|
||||
try {
|
||||
return this.mirror.debugCatchUp(afterSeq);
|
||||
const result = this.mirror.debugCatchUp(afterSeq);
|
||||
this.logReadSourceChange("state.catchUp", this.catchUpSource, "livestore", {
|
||||
afterSeq,
|
||||
});
|
||||
this.catchUpSource = "livestore";
|
||||
return result;
|
||||
} catch (error) {
|
||||
this.logger.warn("failed to catch up from livestore mirror; using delegate", {
|
||||
error,
|
||||
afterSeq,
|
||||
});
|
||||
return this.delegate.catchUp(raw);
|
||||
const result = this.delegate.catchUp(raw);
|
||||
this.logReadSourceChange("state.catchUp", this.catchUpSource, "delegate", {
|
||||
afterSeq,
|
||||
});
|
||||
this.catchUpSource = "delegate";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private logReadSourceChange(
|
||||
route: "state.bootstrap" | "state.catchUp" | "state.listMessages",
|
||||
previous: LiveStoreReadSource,
|
||||
next: LiveStoreReadSource,
|
||||
metadata: Record<string, unknown>,
|
||||
): void {
|
||||
if (previous === next) {
|
||||
return;
|
||||
}
|
||||
this.logger.info(`serving ${route} from ${next}`, {
|
||||
source: next,
|
||||
previousSource: previous,
|
||||
...metadata,
|
||||
});
|
||||
}
|
||||
|
||||
getAppSettings(): AppSettings {
|
||||
return this.delegate.getAppSettings();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user