@fluidframework/container-runtime 2.1.0-276985 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +71 -18
- package/api-extractor/api-extractor.current.json +5 -0
- package/api-extractor/api-extractor.legacy.json +1 -1
- package/api-extractor.json +1 -1
- package/api-report/container-runtime.legacy.public.api.md +9 -0
- package/container-runtime.test-files.tar +0 -0
- package/dist/blobManager/blobManager.d.ts +10 -0
- package/dist/blobManager/blobManager.d.ts.map +1 -1
- package/dist/blobManager/blobManager.js +19 -0
- package/dist/blobManager/blobManager.js.map +1 -1
- package/dist/channelCollection.d.ts +1 -1
- package/dist/channelCollection.d.ts.map +1 -1
- package/dist/channelCollection.js +40 -8
- package/dist/channelCollection.js.map +1 -1
- package/dist/containerRuntime.d.ts +14 -5
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +151 -99
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStoreContext.d.ts +4 -0
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +9 -3
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/gc/garbageCollection.d.ts +1 -1
- package/dist/gc/garbageCollection.d.ts.map +1 -1
- package/dist/gc/garbageCollection.js +14 -8
- package/dist/gc/garbageCollection.js.map +1 -1
- package/dist/gc/gcDefinitions.d.ts +4 -2
- package/dist/gc/gcDefinitions.d.ts.map +1 -1
- package/dist/gc/gcDefinitions.js.map +1 -1
- package/dist/gc/gcHelpers.d.ts.map +1 -1
- package/dist/gc/gcHelpers.js +12 -0
- package/dist/gc/gcHelpers.js.map +1 -1
- package/dist/gc/gcTelemetry.d.ts +3 -2
- package/dist/gc/gcTelemetry.d.ts.map +1 -1
- package/dist/gc/gcTelemetry.js +6 -6
- package/dist/gc/gcTelemetry.js.map +1 -1
- package/dist/legacy.d.ts +1 -1
- package/dist/metadata.d.ts +7 -1
- package/dist/metadata.d.ts.map +1 -1
- package/dist/metadata.js +6 -0
- package/dist/metadata.js.map +1 -1
- package/dist/opLifecycle/batchManager.d.ts +8 -1
- package/dist/opLifecycle/batchManager.d.ts.map +1 -1
- package/dist/opLifecycle/batchManager.js +37 -16
- package/dist/opLifecycle/batchManager.js.map +1 -1
- package/dist/opLifecycle/definitions.d.ts +1 -1
- package/dist/opLifecycle/definitions.d.ts.map +1 -1
- package/dist/opLifecycle/definitions.js.map +1 -1
- package/dist/opLifecycle/index.d.ts +2 -2
- package/dist/opLifecycle/index.d.ts.map +1 -1
- package/dist/opLifecycle/index.js +3 -1
- package/dist/opLifecycle/index.js.map +1 -1
- package/dist/opLifecycle/opCompressor.d.ts.map +1 -1
- package/dist/opLifecycle/opCompressor.js +12 -8
- package/dist/opLifecycle/opCompressor.js.map +1 -1
- package/dist/opLifecycle/opGroupingManager.d.ts.map +1 -1
- package/dist/opLifecycle/opGroupingManager.js +14 -11
- package/dist/opLifecycle/opGroupingManager.js.map +1 -1
- package/dist/opLifecycle/opSplitter.d.ts.map +1 -1
- package/dist/opLifecycle/opSplitter.js +11 -6
- package/dist/opLifecycle/opSplitter.js.map +1 -1
- package/dist/opLifecycle/outbox.d.ts +22 -6
- package/dist/opLifecycle/outbox.d.ts.map +1 -1
- package/dist/opLifecycle/outbox.js +43 -21
- package/dist/opLifecycle/outbox.js.map +1 -1
- package/dist/opLifecycle/remoteMessageProcessor.d.ts +10 -8
- package/dist/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
- package/dist/opLifecycle/remoteMessageProcessor.js +39 -15
- package/dist/opLifecycle/remoteMessageProcessor.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/pendingStateManager.d.ts +37 -13
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +95 -45
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/public.d.ts +1 -1
- package/dist/scheduleManager.js +4 -0
- package/dist/scheduleManager.js.map +1 -1
- package/dist/summary/summarizerNode/summarizerNodeUtils.d.ts.map +1 -1
- package/dist/summary/summarizerNode/summarizerNodeUtils.js +2 -0
- package/dist/summary/summarizerNode/summarizerNodeUtils.js.map +1 -1
- package/dist/summary/summaryFormat.d.ts.map +1 -1
- package/dist/summary/summaryFormat.js +4 -1
- package/dist/summary/summaryFormat.js.map +1 -1
- package/internal.d.ts +1 -1
- package/legacy.d.ts +1 -1
- package/lib/blobManager/blobManager.d.ts +10 -0
- package/lib/blobManager/blobManager.d.ts.map +1 -1
- package/lib/blobManager/blobManager.js +19 -0
- package/lib/blobManager/blobManager.js.map +1 -1
- package/lib/channelCollection.d.ts +1 -1
- package/lib/channelCollection.d.ts.map +1 -1
- package/lib/channelCollection.js +40 -8
- package/lib/channelCollection.js.map +1 -1
- package/lib/containerRuntime.d.ts +14 -5
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +152 -100
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStoreContext.d.ts +4 -0
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +10 -4
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/gc/garbageCollection.d.ts +1 -1
- package/lib/gc/garbageCollection.d.ts.map +1 -1
- package/lib/gc/garbageCollection.js +14 -8
- package/lib/gc/garbageCollection.js.map +1 -1
- package/lib/gc/gcDefinitions.d.ts +4 -2
- package/lib/gc/gcDefinitions.d.ts.map +1 -1
- package/lib/gc/gcDefinitions.js.map +1 -1
- package/lib/gc/gcHelpers.d.ts.map +1 -1
- package/lib/gc/gcHelpers.js +12 -0
- package/lib/gc/gcHelpers.js.map +1 -1
- package/lib/gc/gcTelemetry.d.ts +3 -2
- package/lib/gc/gcTelemetry.d.ts.map +1 -1
- package/lib/gc/gcTelemetry.js +6 -6
- package/lib/gc/gcTelemetry.js.map +1 -1
- package/lib/legacy.d.ts +1 -1
- package/lib/metadata.d.ts +7 -1
- package/lib/metadata.d.ts.map +1 -1
- package/lib/metadata.js +4 -1
- package/lib/metadata.js.map +1 -1
- package/lib/opLifecycle/batchManager.d.ts +8 -1
- package/lib/opLifecycle/batchManager.d.ts.map +1 -1
- package/lib/opLifecycle/batchManager.js +35 -15
- package/lib/opLifecycle/batchManager.js.map +1 -1
- package/lib/opLifecycle/definitions.d.ts +1 -1
- package/lib/opLifecycle/definitions.d.ts.map +1 -1
- package/lib/opLifecycle/definitions.js.map +1 -1
- package/lib/opLifecycle/index.d.ts +2 -2
- package/lib/opLifecycle/index.d.ts.map +1 -1
- package/lib/opLifecycle/index.js +2 -2
- package/lib/opLifecycle/index.js.map +1 -1
- package/lib/opLifecycle/opCompressor.d.ts.map +1 -1
- package/lib/opLifecycle/opCompressor.js +12 -8
- package/lib/opLifecycle/opCompressor.js.map +1 -1
- package/lib/opLifecycle/opGroupingManager.d.ts.map +1 -1
- package/lib/opLifecycle/opGroupingManager.js +14 -11
- package/lib/opLifecycle/opGroupingManager.js.map +1 -1
- package/lib/opLifecycle/opSplitter.d.ts.map +1 -1
- package/lib/opLifecycle/opSplitter.js +11 -6
- package/lib/opLifecycle/opSplitter.js.map +1 -1
- package/lib/opLifecycle/outbox.d.ts +22 -6
- package/lib/opLifecycle/outbox.d.ts.map +1 -1
- package/lib/opLifecycle/outbox.js +44 -22
- package/lib/opLifecycle/outbox.js.map +1 -1
- package/lib/opLifecycle/remoteMessageProcessor.d.ts +10 -8
- package/lib/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
- package/lib/opLifecycle/remoteMessageProcessor.js +37 -14
- package/lib/opLifecycle/remoteMessageProcessor.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.d.ts.map +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/pendingStateManager.d.ts +37 -13
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +95 -45
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/public.d.ts +1 -1
- package/lib/scheduleManager.js +4 -0
- package/lib/scheduleManager.js.map +1 -1
- package/lib/summary/summarizerNode/summarizerNodeUtils.d.ts.map +1 -1
- package/lib/summary/summarizerNode/summarizerNodeUtils.js +2 -0
- package/lib/summary/summarizerNode/summarizerNodeUtils.js.map +1 -1
- package/lib/summary/summaryFormat.d.ts.map +1 -1
- package/lib/summary/summaryFormat.js +4 -1
- package/lib/summary/summaryFormat.js.map +1 -1
- package/package.json +46 -31
- package/src/blobManager/blobManager.ts +19 -0
- package/src/channelCollection.ts +48 -11
- package/src/containerRuntime.ts +203 -133
- package/src/dataStoreContext.ts +22 -4
- package/src/gc/garbageCollection.ts +15 -10
- package/src/gc/gcDefinitions.ts +7 -2
- package/src/gc/gcHelpers.ts +18 -6
- package/src/gc/gcTelemetry.ts +20 -8
- package/src/metadata.ts +11 -1
- package/src/opLifecycle/README.md +0 -8
- package/src/opLifecycle/batchManager.ts +49 -16
- package/src/opLifecycle/definitions.ts +1 -1
- package/src/opLifecycle/index.ts +13 -2
- package/src/opLifecycle/opCompressor.ts +12 -8
- package/src/opLifecycle/opGroupingManager.ts +14 -11
- package/src/opLifecycle/opSplitter.ts +10 -6
- package/src/opLifecycle/outbox.ts +64 -26
- package/src/opLifecycle/remoteMessageProcessor.ts +56 -17
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +173 -74
- package/src/scheduleManager.ts +6 -2
- package/src/summary/README.md +81 -0
- package/src/summary/summarizerNode/summarizerNodeUtils.ts +3 -1
- package/src/summary/summaryFormat.ts +3 -1
- package/src/summary/summaryFormats.md +69 -8
- package/tsconfig.json +0 -1
- package/src/summary/images/appTree.png +0 -0
- package/src/summary/images/protocolAndAppTree.png +0 -0
- package/src/summary/images/summaryTree.png +0 -0
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
import { ICriticalContainerError } from "@fluidframework/container-definitions";
|
|
6
6
|
import { IDisposable } from "@fluidframework/core-interfaces";
|
|
7
7
|
import { ITelemetryLoggerExt } from "@fluidframework/telemetry-utils/internal";
|
|
8
|
-
import { InboundSequencedContainerRuntimeMessage } from "./messageTypes.js";
|
|
9
|
-
import
|
|
8
|
+
import { type InboundSequencedContainerRuntimeMessage } from "./messageTypes.js";
|
|
9
|
+
import { BatchId, BatchMessage } from "./opLifecycle/index.js";
|
|
10
10
|
/**
|
|
11
11
|
* This represents a message that has been submitted and is added to the pending queue when `submit` is called on the
|
|
12
12
|
* ContainerRuntime. This message has either not been ack'd by the server or has not been submitted to the server yet.
|
|
13
|
+
*
|
|
14
|
+
* @remarks This is the current serialization format for pending local state when a Container is serialized.
|
|
13
15
|
*/
|
|
14
16
|
export interface IPendingMessage {
|
|
15
17
|
type: "message";
|
|
@@ -18,7 +20,16 @@ export interface IPendingMessage {
|
|
|
18
20
|
localOpMetadata: unknown;
|
|
19
21
|
opMetadata: Record<string, unknown> | undefined;
|
|
20
22
|
sequenceNumber?: number;
|
|
21
|
-
|
|
23
|
+
/** Info needed to compute the batchId on reconnect */
|
|
24
|
+
batchIdContext: {
|
|
25
|
+
/** The Batch's original clientId, from when it was first flushed to be submitted */
|
|
26
|
+
clientId: string;
|
|
27
|
+
/**
|
|
28
|
+
* The Batch's original clientSequenceNumber, from when it was first flushed to be submitted
|
|
29
|
+
* @remarks A negative value means it was not yet submitted when queued here (e.g. disconnected right before flush fired)
|
|
30
|
+
*/
|
|
31
|
+
batchStartCsn: number;
|
|
32
|
+
};
|
|
22
33
|
}
|
|
23
34
|
export interface IPendingLocalState {
|
|
24
35
|
/**
|
|
@@ -26,18 +37,14 @@ export interface IPendingLocalState {
|
|
|
26
37
|
*/
|
|
27
38
|
pendingStates: IPendingMessage[];
|
|
28
39
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
localOpMetadata: unknown;
|
|
32
|
-
opMetadata: Record<string, unknown> | undefined;
|
|
33
|
-
}
|
|
40
|
+
/** Info needed to replay/resubmit a pending message */
|
|
41
|
+
export type PendingMessageResubmitData = Pick<IPendingMessage, "content" | "localOpMetadata" | "opMetadata">;
|
|
34
42
|
export interface IRuntimeStateHandler {
|
|
35
43
|
connected(): boolean;
|
|
36
44
|
clientId(): string | undefined;
|
|
37
45
|
close(error?: ICriticalContainerError): void;
|
|
38
46
|
applyStashedOp(content: string): Promise<unknown>;
|
|
39
|
-
|
|
40
|
-
reSubmitBatch(batch: IPendingBatchMessage[]): void;
|
|
47
|
+
reSubmitBatch(batch: PendingMessageResubmitData[], batchId: BatchId): void;
|
|
41
48
|
isActiveConnection: () => boolean;
|
|
42
49
|
isAttached: () => boolean;
|
|
43
50
|
}
|
|
@@ -53,16 +60,21 @@ export interface IRuntimeStateHandler {
|
|
|
53
60
|
export declare class PendingStateManager implements IDisposable {
|
|
54
61
|
private readonly stateHandler;
|
|
55
62
|
private readonly logger;
|
|
63
|
+
/** Messages that will need to be resubmitted if not ack'd before the next reconnection */
|
|
56
64
|
private readonly pendingMessages;
|
|
65
|
+
/** Messages stashed from a previous container, now being rehydrated. Need to be resubmitted. */
|
|
57
66
|
private readonly initialMessages;
|
|
58
67
|
/**
|
|
59
68
|
* Sequenced local ops that are saved when stashing since pending ops may depend on them
|
|
60
69
|
*/
|
|
61
70
|
private savedOps;
|
|
71
|
+
/** Used to stand in for batchStartCsn for messages that weren't submitted (so no CSN) */
|
|
72
|
+
private negativeCounter;
|
|
62
73
|
private readonly disposeOnce;
|
|
63
74
|
private isProcessingBatch;
|
|
64
75
|
private pendingBatchBeginMessage;
|
|
65
|
-
|
|
76
|
+
/** Used to ensure we don't replay ops on the same connection twice */
|
|
77
|
+
private clientIdFromLastReplay;
|
|
66
78
|
/**
|
|
67
79
|
* The pending messages count. Includes `pendingMessages` and `initialMessages` to keep in sync with
|
|
68
80
|
* 'hasPendingMessages'.
|
|
@@ -80,7 +92,7 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
80
92
|
*/
|
|
81
93
|
hasPendingMessages(): boolean;
|
|
82
94
|
getLocalState(snapshotSequenceNumber?: number): IPendingLocalState;
|
|
83
|
-
constructor(stateHandler: IRuntimeStateHandler,
|
|
95
|
+
constructor(stateHandler: IRuntimeStateHandler, stashedLocalState: IPendingLocalState | undefined, logger: ITelemetryLoggerExt);
|
|
84
96
|
get disposed(): boolean;
|
|
85
97
|
readonly dispose: () => void;
|
|
86
98
|
/**
|
|
@@ -96,6 +108,16 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
96
108
|
* @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.
|
|
97
109
|
*/
|
|
98
110
|
applyStashedOpsAt(seqNum?: number): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Processes the incoming batch from the server. It verifies that messages are received in the right order and
|
|
113
|
+
* that the batch information is correct.
|
|
114
|
+
* @param batch - The batch that is being processed.
|
|
115
|
+
* @param batchStartCsn - The clientSequenceNumber of the start of this message's batch
|
|
116
|
+
*/
|
|
117
|
+
processPendingLocalBatch(batch: InboundSequencedContainerRuntimeMessage[], batchStartCsn: number): {
|
|
118
|
+
message: InboundSequencedContainerRuntimeMessage;
|
|
119
|
+
localOpMetadata: unknown;
|
|
120
|
+
}[];
|
|
99
121
|
/**
|
|
100
122
|
* Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that
|
|
101
123
|
* the batch information was preserved for batch messages.
|
|
@@ -103,10 +125,12 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
103
125
|
* @param batchStartCsn - The clientSequenceNumber of the start of this message's batch (assigned during submit)
|
|
104
126
|
* (not to be confused with message.clientSequenceNumber - the overwritten value in case of grouped batching)
|
|
105
127
|
*/
|
|
106
|
-
processPendingLocalMessage
|
|
128
|
+
private processPendingLocalMessage;
|
|
107
129
|
/**
|
|
108
130
|
* This message could be the first message in batch. If so, set batch state marking the beginning of a batch.
|
|
109
131
|
* @param message - The message that is being processed.
|
|
132
|
+
* @param batchStartCsn - The clientSequenceNumber of the start of this message's batch (assigned during submit)
|
|
133
|
+
* @param pendingMessage - The corresponding pendingMessage.
|
|
110
134
|
*/
|
|
111
135
|
private maybeProcessBatchBegin;
|
|
112
136
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingStateManager.d.ts","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAG9D,OAAO,EACN,mBAAmB,EAInB,MAAM,0CAA0C,CAAC;
|
|
1
|
+
{"version":3,"file":"pendingStateManager.d.ts","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAG9D,OAAO,EACN,mBAAmB,EAInB,MAAM,0CAA0C,CAAC;AAIlD,OAAO,EAAE,KAAK,uCAAuC,EAAE,MAAM,mBAAmB,CAAC;AAEjF,OAAO,EAAE,OAAO,EAAE,YAAY,EAAmB,MAAM,wBAAwB,CAAC;AAGhF;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC/B,IAAI,EAAE,SAAS,CAAC;IAChB,uBAAuB,EAAE,MAAM,CAAC;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,OAAO,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IAChD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,sDAAsD;IACtD,cAAc,EAAE;QACf,oFAAoF;QACpF,QAAQ,EAAE,MAAM,CAAC;QACjB;;;WAGG;QACH,aAAa,EAAE,MAAM,CAAC;KACtB,CAAC;CACF;AAcD,MAAM,WAAW,kBAAkB;IAClC;;OAEG;IACH,aAAa,EA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sBAAsB,CAAC,EAAE,MAAM,GAAG,kBAAkB;gBAgCvD,YAAY,EAAE,oBAAoB,EACnD,iBAAiB,EAAE,kBAAkB,GAAG,SAAS,EAChC,MAAM,EAAE,mBAAmB;IAO7C,IAAW,QAAQ,YAElB;IACD,SAAgB,OAAO,aAAgC;IAEvD;;;;;;OAMG;IACI,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,EAAE,oBAAoB,EAAE,MAAM,GAAG,SAAS;IA+BnF;;;OAGG;IACU,iBAAiB,CAAC,MAAM,CAAC,EAAE,MAAM;IAkC9C;;;;;OAKG;IACI,wBAAwB,CAC9B,KAAK,EAAE,uCAAuC,EAAE,EAChD,aAAa,EAAE,MAAM,GACnB;QACF,OAAO,EAAE,uCAAuC,CAAC;QACjD,eAAe,EAAE,OAAO,CAAC;KACzB,EAAE;IAOH;;;;;;OAMG;IACH,OAAO,CAAC,0BAA0B;IA0ClC;;;;;OAKG;IACH,OAAO,CAAC,sBAAsB;IAoC9B;;;OAGG;IACH,OAAO,CAAC,oBAAoB;IAwD5B;;;;OAIG;IACI,mBAAmB;CA8G1B"}
|
|
@@ -11,6 +11,9 @@ exports.PendingStateManager = void 0;
|
|
|
11
11
|
const internal_1 = require("@fluidframework/core-utils/internal");
|
|
12
12
|
const internal_2 = require("@fluidframework/telemetry-utils/internal");
|
|
13
13
|
const double_ended_queue_1 = __importDefault(require("double-ended-queue"));
|
|
14
|
+
const uuid_1 = require("uuid");
|
|
15
|
+
const metadata_js_1 = require("./metadata.js");
|
|
16
|
+
const index_js_1 = require("./opLifecycle/index.js");
|
|
14
17
|
const packageVersion_js_1 = require("./packageVersion.js");
|
|
15
18
|
function buildPendingMessageContent(
|
|
16
19
|
// AnyComboFromUnion is needed need to gain access to compatDetails that
|
|
@@ -82,16 +85,19 @@ class PendingStateManager {
|
|
|
82
85
|
],
|
|
83
86
|
};
|
|
84
87
|
}
|
|
85
|
-
constructor(stateHandler,
|
|
88
|
+
constructor(stateHandler, stashedLocalState, logger) {
|
|
86
89
|
this.stateHandler = stateHandler;
|
|
87
90
|
this.logger = logger;
|
|
91
|
+
/** Messages that will need to be resubmitted if not ack'd before the next reconnection */
|
|
88
92
|
this.pendingMessages = new double_ended_queue_1.default();
|
|
89
|
-
|
|
93
|
+
/** Messages stashed from a previous container, now being rehydrated. Need to be resubmitted. */
|
|
90
94
|
this.initialMessages = new double_ended_queue_1.default();
|
|
91
95
|
/**
|
|
92
96
|
* Sequenced local ops that are saved when stashing since pending ops may depend on them
|
|
93
97
|
*/
|
|
94
98
|
this.savedOps = [];
|
|
99
|
+
/** Used to stand in for batchStartCsn for messages that weren't submitted (so no CSN) */
|
|
100
|
+
this.negativeCounter = -1;
|
|
95
101
|
this.disposeOnce = new internal_1.Lazy(() => {
|
|
96
102
|
this.initialMessages.clear();
|
|
97
103
|
this.pendingMessages.clear();
|
|
@@ -99,8 +105,8 @@ class PendingStateManager {
|
|
|
99
105
|
// Indicates whether we are processing a batch.
|
|
100
106
|
this.isProcessingBatch = false;
|
|
101
107
|
this.dispose = () => this.disposeOnce.value;
|
|
102
|
-
if (
|
|
103
|
-
this.initialMessages.push(...
|
|
108
|
+
if (stashedLocalState?.pendingStates) {
|
|
109
|
+
this.initialMessages.push(...stashedLocalState.pendingStates);
|
|
104
110
|
}
|
|
105
111
|
}
|
|
106
112
|
get disposed() {
|
|
@@ -114,6 +120,14 @@ class PendingStateManager {
|
|
|
114
120
|
* or undefined if the batch was not yet sent (e.g. by the time we flushed we lost the connection)
|
|
115
121
|
*/
|
|
116
122
|
onFlushBatch(batch, clientSequenceNumber) {
|
|
123
|
+
// If we're connected this is the client of the current connection,
|
|
124
|
+
// otherwise it's the clientId that just disconnected
|
|
125
|
+
// It's only undefined if we've NEVER connected. This is a tight corner case and we can
|
|
126
|
+
// simply make up a unique ID in this case.
|
|
127
|
+
const clientId = this.stateHandler.clientId() ?? (0, uuid_1.v4)();
|
|
128
|
+
// If the batch was not yet sent, we need to assign a unique batchStartCsn
|
|
129
|
+
// Use a negative number to distinguish these from real CSNs
|
|
130
|
+
const batchStartCsn = clientSequenceNumber ?? this.negativeCounter--;
|
|
117
131
|
for (const message of batch) {
|
|
118
132
|
const { contents: content = "", referenceSequenceNumber, localOpMetadata, metadata: opMetadata, } = message;
|
|
119
133
|
const pendingMessage = {
|
|
@@ -122,7 +136,8 @@ class PendingStateManager {
|
|
|
122
136
|
content,
|
|
123
137
|
localOpMetadata,
|
|
124
138
|
opMetadata,
|
|
125
|
-
|
|
139
|
+
// Note: We only need this on the first message.
|
|
140
|
+
batchIdContext: { clientId, batchStartCsn },
|
|
126
141
|
};
|
|
127
142
|
this.pendingMessages.push(pendingMessage);
|
|
128
143
|
}
|
|
@@ -157,6 +172,7 @@ class PendingStateManager {
|
|
|
157
172
|
else {
|
|
158
173
|
nextMessage.localOpMetadata = localOpMetadata;
|
|
159
174
|
// then we push onto pendingMessages which will cause PendingStateManager to resubmit when we connect
|
|
175
|
+
patchBatchIdContext(nextMessage); // Back compat
|
|
160
176
|
this.pendingMessages.push(nextMessage);
|
|
161
177
|
}
|
|
162
178
|
}
|
|
@@ -165,6 +181,18 @@ class PendingStateManager {
|
|
|
165
181
|
}
|
|
166
182
|
}
|
|
167
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Processes the incoming batch from the server. It verifies that messages are received in the right order and
|
|
186
|
+
* that the batch information is correct.
|
|
187
|
+
* @param batch - The batch that is being processed.
|
|
188
|
+
* @param batchStartCsn - The clientSequenceNumber of the start of this message's batch
|
|
189
|
+
*/
|
|
190
|
+
processPendingLocalBatch(batch, batchStartCsn) {
|
|
191
|
+
return batch.map((message) => ({
|
|
192
|
+
message,
|
|
193
|
+
localOpMetadata: this.processPendingLocalMessage(message, batchStartCsn),
|
|
194
|
+
}));
|
|
195
|
+
}
|
|
168
196
|
/**
|
|
169
197
|
* Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that
|
|
170
198
|
* the batch information was preserved for batch messages.
|
|
@@ -173,27 +201,14 @@ class PendingStateManager {
|
|
|
173
201
|
* (not to be confused with message.clientSequenceNumber - the overwritten value in case of grouped batching)
|
|
174
202
|
*/
|
|
175
203
|
processPendingLocalMessage(message, batchStartCsn) {
|
|
176
|
-
// Pre-processing part - This may be the start of a batch.
|
|
177
|
-
this.maybeProcessBatchBegin(message);
|
|
178
204
|
// Get the next message from the pending queue. Verify a message exists.
|
|
179
205
|
const pendingMessage = this.pendingMessages.peekFront();
|
|
180
206
|
(0, internal_1.assert)(pendingMessage !== undefined, 0x169 /* "No pending message found for this remote message" */);
|
|
207
|
+
// This may be the start of a batch.
|
|
208
|
+
this.maybeProcessBatchBegin(message, batchStartCsn, pendingMessage);
|
|
181
209
|
pendingMessage.sequenceNumber = message.sequenceNumber;
|
|
182
210
|
this.savedOps.push(withoutLocalOpMetadata(pendingMessage));
|
|
183
211
|
this.pendingMessages.shift();
|
|
184
|
-
if (pendingMessage.batchStartCsn !== batchStartCsn) {
|
|
185
|
-
this.logger?.sendErrorEvent({
|
|
186
|
-
eventName: "BatchClientSequenceNumberMismatch",
|
|
187
|
-
details: {
|
|
188
|
-
processingBatch: !!this.pendingBatchBeginMessage,
|
|
189
|
-
pendingBatchCsn: pendingMessage.batchStartCsn,
|
|
190
|
-
batchStartCsn,
|
|
191
|
-
messageBatchMetadata: message.metadata?.batch,
|
|
192
|
-
pendingMessageBatchMetadata: pendingMessage.opMetadata?.batch,
|
|
193
|
-
},
|
|
194
|
-
messageDetails: (0, internal_2.extractSafePropertiesFromMessage)(message),
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
212
|
const messageContent = buildPendingMessageContent(message);
|
|
198
213
|
// Stringified content should match
|
|
199
214
|
if (pendingMessage.content !== messageContent) {
|
|
@@ -209,8 +224,26 @@ class PendingStateManager {
|
|
|
209
224
|
/**
|
|
210
225
|
* This message could be the first message in batch. If so, set batch state marking the beginning of a batch.
|
|
211
226
|
* @param message - The message that is being processed.
|
|
227
|
+
* @param batchStartCsn - The clientSequenceNumber of the start of this message's batch (assigned during submit)
|
|
228
|
+
* @param pendingMessage - The corresponding pendingMessage.
|
|
212
229
|
*/
|
|
213
|
-
maybeProcessBatchBegin(message) {
|
|
230
|
+
maybeProcessBatchBegin(message, batchStartCsn, pendingMessage) {
|
|
231
|
+
if (!this.isProcessingBatch) {
|
|
232
|
+
// Expecting the start of a batch (maybe single-message).
|
|
233
|
+
if (pendingMessage.batchIdContext.batchStartCsn !== batchStartCsn) {
|
|
234
|
+
this.logger?.sendErrorEvent({
|
|
235
|
+
eventName: "BatchClientSequenceNumberMismatch",
|
|
236
|
+
details: {
|
|
237
|
+
processingBatch: !!this.pendingBatchBeginMessage,
|
|
238
|
+
pendingBatchCsn: pendingMessage.batchIdContext.batchStartCsn,
|
|
239
|
+
batchStartCsn,
|
|
240
|
+
messageBatchMetadata: message.metadata?.batch,
|
|
241
|
+
pendingMessageBatchMetadata: pendingMessage.opMetadata?.batch,
|
|
242
|
+
},
|
|
243
|
+
messageDetails: (0, internal_2.extractSafePropertiesFromMessage)(message),
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
214
247
|
// This message is the first in a batch if the "batch" property on the metadata is set to true
|
|
215
248
|
if (message.metadata?.batch) {
|
|
216
249
|
// We should not already be processing a batch and there should be no pending batch begin message.
|
|
@@ -270,8 +303,8 @@ class PendingStateManager {
|
|
|
270
303
|
replayPendingStates() {
|
|
271
304
|
(0, internal_1.assert)(this.stateHandler.connected(), 0x172 /* "The connection state is not consistent with the runtime" */);
|
|
272
305
|
// This assert suggests we are about to send same ops twice, which will result in data loss.
|
|
273
|
-
(0, internal_1.assert)(this.
|
|
274
|
-
this.
|
|
306
|
+
(0, internal_1.assert)(this.clientIdFromLastReplay !== this.stateHandler.clientId(), 0x173 /* "replayPendingStates called twice for same clientId!" */);
|
|
307
|
+
this.clientIdFromLastReplay = this.stateHandler.clientId();
|
|
275
308
|
(0, internal_1.assert)(this.initialMessages.isEmpty(), 0x174 /* "initial states should be empty before replaying pending" */);
|
|
276
309
|
const initialPendingMessagesCount = this.pendingMessages.length;
|
|
277
310
|
let remainingPendingMessagesCount = this.pendingMessages.length;
|
|
@@ -282,40 +315,49 @@ class PendingStateManager {
|
|
|
282
315
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
283
316
|
let pendingMessage = this.pendingMessages.shift();
|
|
284
317
|
remainingPendingMessagesCount--;
|
|
285
|
-
(0,
|
|
318
|
+
const batchMetadataFlag = (0, metadata_js_1.asBatchMetadata)(pendingMessage.opMetadata)?.batch;
|
|
319
|
+
(0, internal_1.assert)(batchMetadataFlag !== false, 0x41b /* We cannot process batches in chunks */);
|
|
320
|
+
// The next message starts a batch (possibly single-message), and we'll need its batchId.
|
|
321
|
+
// We'll find batchId on this message if it was previously generated.
|
|
322
|
+
// Otherwise, generate it now - this is the first time resubmitting this batch.
|
|
323
|
+
const batchId = (0, metadata_js_1.asBatchMetadata)(pendingMessage.opMetadata)?.batchId ??
|
|
324
|
+
(0, index_js_1.generateBatchId)(pendingMessage.batchIdContext.clientId, pendingMessage.batchIdContext.batchStartCsn);
|
|
286
325
|
/**
|
|
287
|
-
* We
|
|
326
|
+
* We must preserve the distinct batches on resubmit.
|
|
288
327
|
* Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will
|
|
289
|
-
* either receive the whole batch ack or nothing at all.
|
|
328
|
+
* either receive the whole batch ack or nothing at all. @see ScheduleManager for how this works.
|
|
290
329
|
*/
|
|
291
|
-
if (
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
while (remainingPendingMessagesCount >= 0) {
|
|
296
|
-
batch.push({
|
|
330
|
+
if (batchMetadataFlag === undefined) {
|
|
331
|
+
// Single-message batch
|
|
332
|
+
this.stateHandler.reSubmitBatch([
|
|
333
|
+
{
|
|
297
334
|
content: pendingMessage.content,
|
|
298
335
|
localOpMetadata: pendingMessage.localOpMetadata,
|
|
299
336
|
opMetadata: pendingMessage.opMetadata,
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
(0, internal_1.assert)(remainingPendingMessagesCount > 0, 0x555 /* No batch end found */);
|
|
305
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
306
|
-
pendingMessage = this.pendingMessages.shift();
|
|
307
|
-
remainingPendingMessagesCount--;
|
|
308
|
-
(0, internal_1.assert)(pendingMessage.opMetadata?.batch !== true, 0x556 /* Batch start needs a corresponding batch end */);
|
|
309
|
-
}
|
|
310
|
-
this.stateHandler.reSubmitBatch(batch);
|
|
337
|
+
},
|
|
338
|
+
], batchId);
|
|
339
|
+
continue;
|
|
311
340
|
}
|
|
312
|
-
else
|
|
313
|
-
|
|
341
|
+
// else: batchMetadataFlag === true (It's a typical multi-message batch)
|
|
342
|
+
(0, internal_1.assert)(remainingPendingMessagesCount > 0, 0x554 /* Last pending message cannot be a batch begin */);
|
|
343
|
+
const batch = [];
|
|
344
|
+
// check is >= because batch end may be last pending message
|
|
345
|
+
while (remainingPendingMessagesCount >= 0) {
|
|
346
|
+
batch.push({
|
|
314
347
|
content: pendingMessage.content,
|
|
315
348
|
localOpMetadata: pendingMessage.localOpMetadata,
|
|
316
349
|
opMetadata: pendingMessage.opMetadata,
|
|
317
350
|
});
|
|
351
|
+
if (pendingMessage.opMetadata?.batch === false) {
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
(0, internal_1.assert)(remainingPendingMessagesCount > 0, 0x555 /* No batch end found */);
|
|
355
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
356
|
+
pendingMessage = this.pendingMessages.shift();
|
|
357
|
+
remainingPendingMessagesCount--;
|
|
358
|
+
(0, internal_1.assert)(pendingMessage.opMetadata?.batch !== true, 0x556 /* Batch start needs a corresponding batch end */);
|
|
318
359
|
}
|
|
360
|
+
this.stateHandler.reSubmitBatch(batch, batchId);
|
|
319
361
|
}
|
|
320
362
|
// pending ops should no longer depend on previous sequenced local ops after resubmit
|
|
321
363
|
this.savedOps = [];
|
|
@@ -332,4 +374,12 @@ class PendingStateManager {
|
|
|
332
374
|
}
|
|
333
375
|
}
|
|
334
376
|
exports.PendingStateManager = PendingStateManager;
|
|
377
|
+
/** For back-compat if trying to apply stashed ops that pre-date batchIdContext */
|
|
378
|
+
function patchBatchIdContext(message) {
|
|
379
|
+
const batchIdContext = message.batchIdContext;
|
|
380
|
+
if (batchIdContext === undefined) {
|
|
381
|
+
// Using uuid guarantees uniqueness, retaining existing behavior
|
|
382
|
+
message.batchIdContext = { clientId: (0, uuid_1.v4)(), batchStartCsn: -1 };
|
|
383
|
+
}
|
|
384
|
+
}
|
|
335
385
|
//# sourceMappingURL=pendingStateManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAIH,kEAAmE;AAEnE,uEAKkD;AAClD,4EAAuC;AAKvC,2DAAiD;AAgDjD,SAAS,0BAA0B;AAClC,wEAAwE;AACxE,kCAAkC;AAClC,OAAmE;IAEnE,kFAAkF;IAClF,+BAA+B;IAC/B,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC;IAClD,sEAAsE;IACtE,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,sBAAsB,CAAC,OAAwB;IACvD,OAAO;QACN,GAAG,OAAO;QACV,eAAe,EAAE,SAAS;KAC1B,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,MAAa,mBAAmB;IAwB/B;;;OAGG;IACH,IAAW,oBAAoB;QAC9B,OAAO,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC;IAClE,CAAC;IAED;;;;OAIG;IACH,IAAW,mCAAmC;QAC7C,OAAO,IAAI,CAAC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AC,CAAC;YACJ,CAAC;QACF,CAAC;QAED,qFAAqF;QACrF,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QAEnB,+GAA+G;QAC/G,6GAA6G;QAC7G,mFAAmF;QACnF,IAAI,IAAI,CAAC,YAAY,CAAC,kBAAkB,EAAE,EAAE,CAAC;YAC5C,IAAI,CAAC,MAAM,EAAE,kBAAkB,CAAC;gBAC/B,SAAS,EAAE,uBAAuB;gBAClC,KAAK,EAAE,2BAA2B;gBAClC,QAAQ,EAAE,IAAI,CAAC,YAAY,CAAC,QAAQ,EAAE;aACtC,CAAC,CAAC;QACJ,CAAC;IACF,CAAC;CACD;AA5YD,kDA4YC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { ICriticalContainerError } from \"@fluidframework/container-definitions\";\nimport { IDisposable } from \"@fluidframework/core-interfaces\";\nimport { assert, Lazy } from \"@fluidframework/core-utils/internal\";\nimport { ISequencedDocumentMessage } from \"@fluidframework/driver-definitions/internal\";\nimport {\n\tITelemetryLoggerExt,\n\tDataProcessingError,\n\tLoggingError,\n\textractSafePropertiesFromMessage,\n} from \"@fluidframework/telemetry-utils/internal\";\nimport Deque from \"double-ended-queue\";\n\nimport { InboundSequencedContainerRuntimeMessage } from \"./messageTypes.js\";\nimport { IBatchMetadata } from \"./metadata.js\";\nimport type { BatchMessage } from \"./opLifecycle/index.js\";\nimport { pkgVersion } from \"./packageVersion.js\";\n\n/**\n * This represents a message that has been submitted and is added to the pending queue when `submit` is called on the\n * ContainerRuntime. This message has either not been ack'd by the server or has not been submitted to the server yet.\n */\nexport interface IPendingMessage {\n\ttype: \"message\";\n\treferenceSequenceNumber: number;\n\tcontent: string;\n\tlocalOpMetadata: unknown;\n\topMetadata: Record<string, unknown> | undefined;\n\tsequenceNumber?: number;\n\tbatchStartCsn?: number;\n}\n\nexport interface IPendingLocalState {\n\t/**\n\t * list of pending states, including ops and batch information\n\t */\n\tpendingStates: IPendingMessage[];\n}\n\nexport interface IPendingBatchMessage {\n\tcontent: string;\n\tlocalOpMetadata: unknown;\n\topMetadata: Record<string, unknown> | undefined;\n}\n\nexport interface IRuntimeStateHandler {\n\tconnected(): boolean;\n\tclientId(): string | undefined;\n\tclose(error?: ICriticalContainerError): void;\n\tapplyStashedOp(content: string): Promise<unknown>;\n\treSubmit(message: IPendingBatchMessage): void;\n\treSubmitBatch(batch: IPendingBatchMessage[]): void;\n\tisActiveConnection: () => boolean;\n\tisAttached: () => boolean;\n}\n\n/** Union of keys of T */\ntype KeysOfUnion<T extends object> = T extends T ? keyof T : never;\n/** *Partial* type all possible combinations of properties and values of union T.\n * This loosens typing allowing access to all possible properties without\n * narrowing.\n */\ntype AnyComboFromUnion<T extends object> = { [P in KeysOfUnion<T>]?: T[P] };\n\nfunction buildPendingMessageContent(\n\t// AnyComboFromUnion is needed need to gain access to compatDetails that\n\t// is only defined for some cases.\n\tmessage: AnyComboFromUnion<InboundSequencedContainerRuntimeMessage>,\n): string {\n\t// IMPORTANT: Order matters here, this must match the order of the properties used\n\t// when submitting the message.\n\tconst { type, contents, compatDetails } = message;\n\t// Any properties that are not defined, won't be emitted by stringify.\n\treturn JSON.stringify({ type, contents, compatDetails });\n}\n\nfunction withoutLocalOpMetadata(message: IPendingMessage): IPendingMessage {\n\treturn {\n\t\t...message,\n\t\tlocalOpMetadata: undefined,\n\t};\n}\n\n/**\n * PendingStateManager is responsible for maintaining the messages that have not been sent or have not yet been\n * acknowledged by the server. It also maintains the batch information for both automatically and manually flushed\n * batches along with the messages.\n * When the Container reconnects, it replays the pending states, which includes manual flushing\n * of messages and triggering resubmission of unacked ops.\n *\n * It verifies that all the ops are acked, are received in the right order and batch information is correct.\n */\nexport class PendingStateManager implements IDisposable {\n\tprivate readonly pendingMessages = new Deque<IPendingMessage>();\n\t// This queue represents already acked messages.\n\tprivate readonly initialMessages = new Deque<IPendingMessage>();\n\n\t/**\n\t * Sequenced local ops that are saved when stashing since pending ops may depend on them\n\t */\n\tprivate savedOps: IPendingMessage[] = [];\n\n\tprivate readonly disposeOnce = new Lazy<void>(() => {\n\t\tthis.initialMessages.clear();\n\t\tthis.pendingMessages.clear();\n\t});\n\n\t// Indicates whether we are processing a batch.\n\tprivate isProcessingBatch: boolean = false;\n\n\t// This stores the first message in the batch that we are processing. This is used to verify that we get\n\t// the correct batch metadata.\n\tprivate pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;\n\n\tprivate clientId: string | undefined;\n\n\t/**\n\t * The pending messages count. Includes `pendingMessages` and `initialMessages` to keep in sync with\n\t * 'hasPendingMessages'.\n\t */\n\tpublic get pendingMessagesCount(): number {\n\t\treturn this.pendingMessages.length + this.initialMessages.length;\n\t}\n\n\t/**\n\t * The minimumPendingMessageSequenceNumber is the minimum of the first pending message and the first initial message.\n\t *\n\t * We need this so that we can properly keep local data and maintain the correct sequence window.\n\t */\n\tpublic get minimumPendingMessageSequenceNumber(): number | undefined {\n\t\treturn this.pendingMessages.peekFront()?.referenceSequenceNumber;\n\t}\n\n\t/**\n\t * Called to check if there are any pending messages in the pending message queue.\n\t * @returns A boolean indicating whether there are messages or not.\n\t */\n\tpublic hasPendingMessages(): boolean {\n\t\treturn this.pendingMessagesCount !== 0;\n\t}\n\n\tpublic getLocalState(snapshotSequenceNumber?: number): IPendingLocalState {\n\t\tassert(\n\t\t\tthis.initialMessages.isEmpty(),\n\t\t\t0x2e9 /* \"Must call getLocalState() after applying initial states\" */,\n\t\t);\n\t\t// Using snapshot sequence number to filter ops older than our latest snapshot.\n\t\t// Such ops should not be declared in pending/stashed state. Snapshot seq num will not\n\t\t// be available when the container is not attached. Therefore, no filtering is needed.\n\t\tconst newSavedOps = [...this.savedOps].filter((message) => {\n\t\t\tassert(\n\t\t\t\tmessage.sequenceNumber !== undefined,\n\t\t\t\t0x97c /* saved op should already have a sequence number */,\n\t\t\t);\n\t\t\treturn message.sequenceNumber > (snapshotSequenceNumber ?? 0);\n\t\t});\n\t\tthis.pendingMessages.toArray().forEach((message) => {\n\t\t\tif (\n\t\t\t\tsnapshotSequenceNumber !== undefined &&\n\t\t\t\tmessage.referenceSequenceNumber < snapshotSequenceNumber\n\t\t\t) {\n\t\t\t\tthrow new LoggingError(\"trying to stash ops older than our latest snapshot\");\n\t\t\t}\n\t\t});\n\t\treturn {\n\t\t\tpendingStates: [\n\t\t\t\t...newSavedOps,\n\t\t\t\t...this.pendingMessages.toArray().map(withoutLocalOpMetadata),\n\t\t\t],\n\t\t};\n\t}\n\n\tconstructor(\n\t\tprivate readonly stateHandler: IRuntimeStateHandler,\n\t\tinitialLocalState: IPendingLocalState | undefined,\n\t\tprivate readonly logger: ITelemetryLoggerExt | undefined,\n\t) {\n\t\tif (initialLocalState?.pendingStates) {\n\t\t\tthis.initialMessages.push(...initialLocalState.pendingStates);\n\t\t}\n\t}\n\n\tpublic get disposed() {\n\t\treturn this.disposeOnce.evaluated;\n\t}\n\tpublic readonly dispose = () => this.disposeOnce.value;\n\n\t/**\n\t * The given batch has been flushed, and needs to be tracked locally until the corresponding\n\t * acks are processed, to ensure it is successfully sent.\n\t * @param batch - The batch that was flushed\n\t * @param clientSequenceNumber - The CSN of the first message in the batch,\n\t * or undefined if the batch was not yet sent (e.g. by the time we flushed we lost the connection)\n\t */\n\tpublic onFlushBatch(batch: BatchMessage[], clientSequenceNumber: number | undefined) {\n\t\tfor (const message of batch) {\n\t\t\tconst {\n\t\t\t\tcontents: content = \"\",\n\t\t\t\treferenceSequenceNumber,\n\t\t\t\tlocalOpMetadata,\n\t\t\t\tmetadata: opMetadata,\n\t\t\t} = message;\n\t\t\tconst pendingMessage: IPendingMessage = {\n\t\t\t\ttype: \"message\",\n\t\t\t\treferenceSequenceNumber,\n\t\t\t\tcontent,\n\t\t\t\tlocalOpMetadata,\n\t\t\t\topMetadata,\n\t\t\t\tbatchStartCsn: clientSequenceNumber,\n\t\t\t};\n\t\t\tthis.pendingMessages.push(pendingMessage);\n\t\t}\n\t}\n\n\t/**\n\t * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n\t * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.\n\t */\n\tpublic async applyStashedOpsAt(seqNum?: number) {\n\t\t// apply stashed ops at sequence number\n\t\twhile (!this.initialMessages.isEmpty()) {\n\t\t\tif (seqNum !== undefined) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\t\tconst peekMessage = this.initialMessages.peekFront()!;\n\t\t\t\tif (peekMessage.referenceSequenceNumber > seqNum) {\n\t\t\t\t\tbreak; // nothing left to do at this sequence number\n\t\t\t\t}\n\t\t\t\tif (peekMessage.referenceSequenceNumber < seqNum) {\n\t\t\t\t\tthrow new Error(\"loaded from snapshot too recent to apply stashed ops\");\n\t\t\t\t}\n\t\t\t}\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tconst nextMessage = this.initialMessages.shift()!;\n\t\t\ttry {\n\t\t\t\t// applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it\n\t\t\t\tconst localOpMetadata = await this.stateHandler.applyStashedOp(nextMessage.content);\n\t\t\t\tif (!this.stateHandler.isAttached()) {\n\t\t\t\t\tif (localOpMetadata !== undefined) {\n\t\t\t\t\t\tthrow new Error(\"Local Op Metadata must be undefined when not attached\");\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tnextMessage.localOpMetadata = localOpMetadata;\n\t\t\t\t\t// then we push onto pendingMessages which will cause PendingStateManager to resubmit when we connect\n\t\t\t\t\tthis.pendingMessages.push(nextMessage);\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tthrow DataProcessingError.wrapIfUnrecognized(error, \"applyStashedOp\", nextMessage);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that\n\t * the batch information was preserved for batch messages.\n\t * @param message - The message that got ack'd and needs to be processed.\n\t * @param batchStartCsn - The clientSequenceNumber of the start of this message's batch (assigned during submit)\n\t * (not to be confused with message.clientSequenceNumber - the overwritten value in case of grouped batching)\n\t */\n\tpublic processPendingLocalMessage(\n\t\tmessage: InboundSequencedContainerRuntimeMessage,\n\t\tbatchStartCsn: number,\n\t): unknown {\n\t\t// Pre-processing part - This may be the start of a batch.\n\t\tthis.maybeProcessBatchBegin(message);\n\t\t// Get the next message from the pending queue. Verify a message exists.\n\t\tconst pendingMessage = this.pendingMessages.peekFront();\n\t\tassert(\n\t\t\tpendingMessage !== undefined,\n\t\t\t0x169 /* \"No pending message found for this remote message\" */,\n\t\t);\n\t\tpendingMessage.sequenceNumber = message.sequenceNumber;\n\t\tthis.savedOps.push(withoutLocalOpMetadata(pendingMessage));\n\n\t\tthis.pendingMessages.shift();\n\n\t\tif (pendingMessage.batchStartCsn !== batchStartCsn) {\n\t\t\tthis.logger?.sendErrorEvent({\n\t\t\t\teventName: \"BatchClientSequenceNumberMismatch\",\n\t\t\t\tdetails: {\n\t\t\t\t\tprocessingBatch: !!this.pendingBatchBeginMessage,\n\t\t\t\t\tpendingBatchCsn: pendingMessage.batchStartCsn,\n\t\t\t\t\tbatchStartCsn,\n\t\t\t\t\tmessageBatchMetadata: (message.metadata as any)?.batch,\n\t\t\t\t\tpendingMessageBatchMetadata: (pendingMessage.opMetadata as any)?.batch,\n\t\t\t\t},\n\t\t\t\tmessageDetails: extractSafePropertiesFromMessage(message),\n\t\t\t});\n\t\t}\n\n\t\tconst messageContent = buildPendingMessageContent(message);\n\n\t\t// Stringified content should match\n\t\tif (pendingMessage.content !== messageContent) {\n\t\t\tthis.stateHandler.close(\n\t\t\t\tDataProcessingError.create(\n\t\t\t\t\t\"pending local message content mismatch\",\n\t\t\t\t\t\"unexpectedAckReceived\",\n\t\t\t\t\tmessage,\n\t\t\t\t\t{\n\t\t\t\t\t\texpectedMessageType: JSON.parse(pendingMessage.content).type,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\t// Post-processing part - If we are processing a batch then this could be the last message in the batch.\n\t\tthis.maybeProcessBatchEnd(message);\n\n\t\treturn pendingMessage.localOpMetadata;\n\t}\n\n\t/**\n\t * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.\n\t * @param message - The message that is being processed.\n\t */\n\tprivate maybeProcessBatchBegin(message: ISequencedDocumentMessage) {\n\t\t// This message is the first in a batch if the \"batch\" property on the metadata is set to true\n\t\tif ((message.metadata as IBatchMetadata | undefined)?.batch) {\n\t\t\t// We should not already be processing a batch and there should be no pending batch begin message.\n\t\t\tassert(\n\t\t\t\t!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,\n\t\t\t\t0x16b /* \"The pending batch state indicates we are already processing a batch\" */,\n\t\t\t);\n\n\t\t\t// Set the pending batch state indicating we have started processing a batch.\n\t\t\tthis.pendingBatchBeginMessage = message;\n\t\t\tthis.isProcessingBatch = true;\n\t\t}\n\t}\n\n\t/**\n\t * This message could be the last message in batch. If so, clear batch state since the batch is complete.\n\t * @param message - The message that is being processed.\n\t */\n\tprivate maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n\t\tif (!this.isProcessingBatch) {\n\t\t\treturn;\n\t\t}\n\n\t\t// There should be a pending batch begin message.\n\t\tassert(\n\t\t\tthis.pendingBatchBeginMessage !== undefined,\n\t\t\t0x16d /* \"There is no pending batch begin message\" */,\n\t\t);\n\n\t\tconst batchEndMetadata = (message.metadata as IBatchMetadata | undefined)?.batch;\n\t\tif (this.pendingMessages.isEmpty() || batchEndMetadata === false) {\n\t\t\t// Get the batch begin metadata from the first message in the batch.\n\t\t\tconst batchBeginMetadata = (\n\t\t\t\tthis.pendingBatchBeginMessage.metadata as IBatchMetadata | undefined\n\t\t\t)?.batch;\n\n\t\t\t// There could be just a single message in the batch. If so, it should not have any batch metadata. If there\n\t\t\t// are multiple messages in the batch, verify that we got the correct batch begin and end metadata.\n\t\t\tif (this.pendingBatchBeginMessage === message) {\n\t\t\t\tassert(\n\t\t\t\t\tbatchBeginMetadata === undefined,\n\t\t\t\t\t0x16e /* \"Batch with single message should not have batch metadata\" */,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tif (batchBeginMetadata !== true || batchEndMetadata !== false) {\n\t\t\t\t\tthis.stateHandler.close(\n\t\t\t\t\t\tDataProcessingError.create(\n\t\t\t\t\t\t\t\"Pending batch inconsistency\", // Formerly known as asserts 0x16f and 0x170\n\t\t\t\t\t\t\t\"processPendingLocalMessage\",\n\t\t\t\t\t\t\tmessage,\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\truntimeVersion: pkgVersion,\n\t\t\t\t\t\t\t\tbatchClientId:\n\t\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n\t\t\t\t\t\t\t\t\tthis.pendingBatchBeginMessage.clientId === null\n\t\t\t\t\t\t\t\t\t\t? \"null\"\n\t\t\t\t\t\t\t\t\t\t: this.pendingBatchBeginMessage.clientId,\n\t\t\t\t\t\t\t\tclientId: this.stateHandler.clientId(),\n\t\t\t\t\t\t\t\thasBatchStart: batchBeginMetadata === true,\n\t\t\t\t\t\t\t\thasBatchEnd: batchEndMetadata === false,\n\t\t\t\t\t\t\t\tmessageType: message.type,\n\t\t\t\t\t\t\t\tpendingMessagesCount: this.pendingMessagesCount,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Clear the pending batch state now that we have processed the entire batch.\n\t\t\tthis.pendingBatchBeginMessage = undefined;\n\t\t\tthis.isProcessingBatch = false;\n\t\t}\n\t}\n\n\t/**\n\t * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending\n\t * states in its queue. This includes triggering resubmission of unacked ops.\n\t * ! Note: successfully resubmitting an op that has been successfully sequenced is not possible due to checks in the ConnectionStateHandler (Loader layer)\n\t */\n\tpublic replayPendingStates() {\n\t\tassert(\n\t\t\tthis.stateHandler.connected(),\n\t\t\t0x172 /* \"The connection state is not consistent with the runtime\" */,\n\t\t);\n\n\t\t// This assert suggests we are about to send same ops twice, which will result in data loss.\n\t\tassert(\n\t\t\tthis.clientId !== this.stateHandler.clientId(),\n\t\t\t0x173 /* \"replayPendingStates called twice for same clientId!\" */,\n\t\t);\n\t\tthis.clientId = this.stateHandler.clientId();\n\n\t\tassert(\n\t\t\tthis.initialMessages.isEmpty(),\n\t\t\t0x174 /* \"initial states should be empty before replaying pending\" */,\n\t\t);\n\n\t\tconst initialPendingMessagesCount = this.pendingMessages.length;\n\t\tlet remainingPendingMessagesCount = this.pendingMessages.length;\n\n\t\t// Process exactly `pendingMessagesCount` items in the queue as it represents the number of messages that were\n\t\t// pending when we connected. This is important because the `reSubmitFn` might add more items in the queue\n\t\t// which must not be replayed.\n\t\twhile (remainingPendingMessagesCount > 0) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tlet pendingMessage = this.pendingMessages.shift()!;\n\t\t\tremainingPendingMessagesCount--;\n\t\t\tassert(\n\t\t\t\tpendingMessage.opMetadata?.batch !== false,\n\t\t\t\t0x41b /* We cannot process batches in chunks */,\n\t\t\t);\n\n\t\t\t/**\n\t\t\t * We want to ensure grouped messages get processed in a batch.\n\t\t\t * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will\n\t\t\t * either receive the whole batch ack or nothing at all.\n\t\t\t */\n\t\t\tif (pendingMessage.opMetadata?.batch) {\n\t\t\t\tassert(\n\t\t\t\t\tremainingPendingMessagesCount > 0,\n\t\t\t\t\t0x554 /* Last pending message cannot be a batch begin */,\n\t\t\t\t);\n\n\t\t\t\tconst batch: IPendingBatchMessage[] = [];\n\n\t\t\t\t// check is >= because batch end may be last pending message\n\t\t\t\twhile (remainingPendingMessagesCount >= 0) {\n\t\t\t\t\tbatch.push({\n\t\t\t\t\t\tcontent: pendingMessage.content,\n\t\t\t\t\t\tlocalOpMetadata: pendingMessage.localOpMetadata,\n\t\t\t\t\t\topMetadata: pendingMessage.opMetadata,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (pendingMessage.opMetadata?.batch === false) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tassert(remainingPendingMessagesCount > 0, 0x555 /* No batch end found */);\n\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\t\t\tpendingMessage = this.pendingMessages.shift()!;\n\t\t\t\t\tremainingPendingMessagesCount--;\n\t\t\t\t\tassert(\n\t\t\t\t\t\tpendingMessage.opMetadata?.batch !== true,\n\t\t\t\t\t\t0x556 /* Batch start needs a corresponding batch end */,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tthis.stateHandler.reSubmitBatch(batch);\n\t\t\t} else {\n\t\t\t\tthis.stateHandler.reSubmit({\n\t\t\t\t\tcontent: pendingMessage.content,\n\t\t\t\t\tlocalOpMetadata: pendingMessage.localOpMetadata,\n\t\t\t\t\topMetadata: pendingMessage.opMetadata,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\t// pending ops should no longer depend on previous sequenced local ops after resubmit\n\t\tthis.savedOps = [];\n\n\t\t// We replayPendingStates on read connections too - we expect these to get nack'd though, and to then reconnect\n\t\t// on a write connection and replay again. This filters out the replay that happens on the read connection so\n\t\t// we only see the replays on write connections (that have a chance to go through).\n\t\tif (this.stateHandler.isActiveConnection()) {\n\t\t\tthis.logger?.sendTelemetryEvent({\n\t\t\t\teventName: \"PendingStatesReplayed\",\n\t\t\t\tcount: initialPendingMessagesCount,\n\t\t\t\tclientId: this.stateHandler.clientId(),\n\t\t\t});\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAIH,kEAAmE;AAEnE,uEAKkD;AAClD,4EAAuC;AACvC,+BAAkC;AAGlC,+CAAgE;AAChE,qDAAgF;AAChF,2DAAiD;AAsEjD,SAAS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F,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { ICriticalContainerError } from \"@fluidframework/container-definitions\";\nimport { IDisposable } from \"@fluidframework/core-interfaces\";\nimport { assert, Lazy } from \"@fluidframework/core-utils/internal\";\nimport { ISequencedDocumentMessage } from \"@fluidframework/driver-definitions/internal\";\nimport {\n\tITelemetryLoggerExt,\n\tDataProcessingError,\n\tLoggingError,\n\textractSafePropertiesFromMessage,\n} from \"@fluidframework/telemetry-utils/internal\";\nimport Deque from \"double-ended-queue\";\nimport { v4 as uuid } from \"uuid\";\n\nimport { type InboundSequencedContainerRuntimeMessage } from \"./messageTypes.js\";\nimport { asBatchMetadata, IBatchMetadata } from \"./metadata.js\";\nimport { BatchId, BatchMessage, generateBatchId } from \"./opLifecycle/index.js\";\nimport { pkgVersion } from \"./packageVersion.js\";\n\n/**\n * This represents a message that has been submitted and is added to the pending queue when `submit` is called on the\n * ContainerRuntime. This message has either not been ack'd by the server or has not been submitted to the server yet.\n *\n * @remarks This is the current serialization format for pending local state when a Container is serialized.\n */\nexport interface IPendingMessage {\n\ttype: \"message\";\n\treferenceSequenceNumber: number;\n\tcontent: string;\n\tlocalOpMetadata: unknown;\n\topMetadata: Record<string, unknown> | undefined;\n\tsequenceNumber?: number;\n\t/** Info needed to compute the batchId on reconnect */\n\tbatchIdContext: {\n\t\t/** The Batch's original clientId, from when it was first flushed to be submitted */\n\t\tclientId: string;\n\t\t/**\n\t\t * The Batch's original clientSequenceNumber, from when it was first flushed to be submitted\n\t\t *\t@remarks A negative value means it was not yet submitted when queued here (e.g. disconnected right before flush fired)\n\t\t */\n\t\tbatchStartCsn: number;\n\t};\n}\n\ntype Patch<T, U> = U & Omit<T, keyof U>;\n\n/** First version of the type (pre-dates batchIdContext) */\ntype IPendingMessageV0 = Patch<IPendingMessage, { batchIdContext?: undefined }>;\n\n/**\n * Union of all supported schemas for when applying stashed ops\n *\n * @remarks When the format changes, this type should update to reflect all possible schemas.\n */\ntype IPendingMessageFromStash = IPendingMessageV0 | IPendingMessage;\n\nexport interface IPendingLocalState {\n\t/**\n\t * list of pending states, including ops and batch information\n\t */\n\tpendingStates: IPendingMessage[];\n}\n\n/** Info needed to replay/resubmit a pending message */\nexport type PendingMessageResubmitData = Pick<\n\tIPendingMessage,\n\t\"content\" | \"localOpMetadata\" | \"opMetadata\"\n>;\n\nexport interface IRuntimeStateHandler {\n\tconnected(): boolean;\n\tclientId(): string | undefined;\n\tclose(error?: ICriticalContainerError): void;\n\tapplyStashedOp(content: string): Promise<unknown>;\n\treSubmitBatch(batch: PendingMessageResubmitData[], batchId: BatchId): void;\n\tisActiveConnection: () => boolean;\n\tisAttached: () => boolean;\n}\n\n/** Union of keys of T */\ntype KeysOfUnion<T extends object> = T extends T ? keyof T : never;\n/** *Partial* type all possible combinations of properties and values of union T.\n * This loosens typing allowing access to all possible properties without\n * narrowing.\n */\ntype AnyComboFromUnion<T extends object> = { [P in KeysOfUnion<T>]?: T[P] };\n\nfunction buildPendingMessageContent(\n\t// AnyComboFromUnion is needed need to gain access to compatDetails that\n\t// is only defined for some cases.\n\tmessage: AnyComboFromUnion<InboundSequencedContainerRuntimeMessage>,\n): string {\n\t// IMPORTANT: Order matters here, this must match the order of the properties used\n\t// when submitting the message.\n\tconst { type, contents, compatDetails } = message;\n\t// Any properties that are not defined, won't be emitted by stringify.\n\treturn JSON.stringify({ type, contents, compatDetails });\n}\n\nfunction withoutLocalOpMetadata(message: IPendingMessage): IPendingMessage {\n\treturn {\n\t\t...message,\n\t\tlocalOpMetadata: undefined,\n\t};\n}\n\n/**\n * PendingStateManager is responsible for maintaining the messages that have not been sent or have not yet been\n * acknowledged by the server. It also maintains the batch information for both automatically and manually flushed\n * batches along with the messages.\n * When the Container reconnects, it replays the pending states, which includes manual flushing\n * of messages and triggering resubmission of unacked ops.\n *\n * It verifies that all the ops are acked, are received in the right order and batch information is correct.\n */\nexport class PendingStateManager implements IDisposable {\n\t/** Messages that will need to be resubmitted if not ack'd before the next reconnection */\n\tprivate readonly pendingMessages = new Deque<IPendingMessage>();\n\t/** Messages stashed from a previous container, now being rehydrated. Need to be resubmitted. */\n\tprivate readonly initialMessages = new Deque<IPendingMessageFromStash>();\n\n\t/**\n\t * Sequenced local ops that are saved when stashing since pending ops may depend on them\n\t */\n\tprivate savedOps: IPendingMessage[] = [];\n\n\t/** Used to stand in for batchStartCsn for messages that weren't submitted (so no CSN) */\n\tprivate negativeCounter: number = -1;\n\n\tprivate readonly disposeOnce = new Lazy<void>(() => {\n\t\tthis.initialMessages.clear();\n\t\tthis.pendingMessages.clear();\n\t});\n\n\t// Indicates whether we are processing a batch.\n\tprivate isProcessingBatch: boolean = false;\n\n\t// This stores the first message in the batch that we are processing. This is used to verify that we get\n\t// the correct batch metadata.\n\tprivate pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;\n\n\t/** Used to ensure we don't replay ops on the same connection twice */\n\tprivate clientIdFromLastReplay: string | undefined;\n\n\t/**\n\t * The pending messages count. Includes `pendingMessages` and `initialMessages` to keep in sync with\n\t * 'hasPendingMessages'.\n\t */\n\tpublic get pendingMessagesCount(): number {\n\t\treturn this.pendingMessages.length + this.initialMessages.length;\n\t}\n\n\t/**\n\t * The minimumPendingMessageSequenceNumber is the minimum of the first pending message and the first initial message.\n\t *\n\t * We need this so that we can properly keep local data and maintain the correct sequence window.\n\t */\n\tpublic get minimumPendingMessageSequenceNumber(): number | undefined {\n\t\treturn this.pendingMessages.peekFront()?.referenceSequenceNumber;\n\t}\n\n\t/**\n\t * Called to check if there are any pending messages in the pending message queue.\n\t * @returns A boolean indicating whether there are messages or not.\n\t */\n\tpublic hasPendingMessages(): boolean {\n\t\treturn this.pendingMessagesCount !== 0;\n\t}\n\n\tpublic getLocalState(snapshotSequenceNumber?: number): IPendingLocalState {\n\t\tassert(\n\t\t\tthis.initialMessages.isEmpty(),\n\t\t\t0x2e9 /* \"Must call getLocalState() after applying initial states\" */,\n\t\t);\n\t\t// Using snapshot sequence number to filter ops older than our latest snapshot.\n\t\t// Such ops should not be declared in pending/stashed state. Snapshot seq num will not\n\t\t// be available when the container is not attached. Therefore, no filtering is needed.\n\t\tconst newSavedOps = [...this.savedOps].filter((message) => {\n\t\t\tassert(\n\t\t\t\tmessage.sequenceNumber !== undefined,\n\t\t\t\t0x97c /* saved op should already have a sequence number */,\n\t\t\t);\n\t\t\treturn message.sequenceNumber > (snapshotSequenceNumber ?? 0);\n\t\t});\n\t\tthis.pendingMessages.toArray().forEach((message) => {\n\t\t\tif (\n\t\t\t\tsnapshotSequenceNumber !== undefined &&\n\t\t\t\tmessage.referenceSequenceNumber < snapshotSequenceNumber\n\t\t\t) {\n\t\t\t\tthrow new LoggingError(\"trying to stash ops older than our latest snapshot\");\n\t\t\t}\n\t\t});\n\t\treturn {\n\t\t\tpendingStates: [\n\t\t\t\t...newSavedOps,\n\t\t\t\t...this.pendingMessages.toArray().map(withoutLocalOpMetadata),\n\t\t\t],\n\t\t};\n\t}\n\n\tconstructor(\n\t\tprivate readonly stateHandler: IRuntimeStateHandler,\n\t\tstashedLocalState: IPendingLocalState | undefined,\n\t\tprivate readonly logger: ITelemetryLoggerExt,\n\t) {\n\t\tif (stashedLocalState?.pendingStates) {\n\t\t\tthis.initialMessages.push(...stashedLocalState.pendingStates);\n\t\t}\n\t}\n\n\tpublic get disposed() {\n\t\treturn this.disposeOnce.evaluated;\n\t}\n\tpublic readonly dispose = () => this.disposeOnce.value;\n\n\t/**\n\t * The given batch has been flushed, and needs to be tracked locally until the corresponding\n\t * acks are processed, to ensure it is successfully sent.\n\t * @param batch - The batch that was flushed\n\t * @param clientSequenceNumber - The CSN of the first message in the batch,\n\t * or undefined if the batch was not yet sent (e.g. by the time we flushed we lost the connection)\n\t */\n\tpublic onFlushBatch(batch: BatchMessage[], clientSequenceNumber: number | undefined) {\n\t\t// If we're connected this is the client of the current connection,\n\t\t// otherwise it's the clientId that just disconnected\n\t\t// It's only undefined if we've NEVER connected. This is a tight corner case and we can\n\t\t// simply make up a unique ID in this case.\n\t\tconst clientId = this.stateHandler.clientId() ?? uuid();\n\n\t\t// If the batch was not yet sent, we need to assign a unique batchStartCsn\n\t\t// Use a negative number to distinguish these from real CSNs\n\t\tconst batchStartCsn = clientSequenceNumber ?? this.negativeCounter--;\n\n\t\tfor (const message of batch) {\n\t\t\tconst {\n\t\t\t\tcontents: content = \"\",\n\t\t\t\treferenceSequenceNumber,\n\t\t\t\tlocalOpMetadata,\n\t\t\t\tmetadata: opMetadata,\n\t\t\t} = message;\n\t\t\tconst pendingMessage: IPendingMessage = {\n\t\t\t\ttype: \"message\",\n\t\t\t\treferenceSequenceNumber,\n\t\t\t\tcontent,\n\t\t\t\tlocalOpMetadata,\n\t\t\t\topMetadata,\n\t\t\t\t// Note: We only need this on the first message.\n\t\t\t\tbatchIdContext: { clientId, batchStartCsn },\n\t\t\t};\n\t\t\tthis.pendingMessages.push(pendingMessage);\n\t\t}\n\t}\n\n\t/**\n\t * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n\t * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.\n\t */\n\tpublic async applyStashedOpsAt(seqNum?: number) {\n\t\t// apply stashed ops at sequence number\n\t\twhile (!this.initialMessages.isEmpty()) {\n\t\t\tif (seqNum !== undefined) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\t\tconst peekMessage = this.initialMessages.peekFront()!;\n\t\t\t\tif (peekMessage.referenceSequenceNumber > seqNum) {\n\t\t\t\t\tbreak; // nothing left to do at this sequence number\n\t\t\t\t}\n\t\t\t\tif (peekMessage.referenceSequenceNumber < seqNum) {\n\t\t\t\t\tthrow new Error(\"loaded from snapshot too recent to apply stashed ops\");\n\t\t\t\t}\n\t\t\t}\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tconst nextMessage = this.initialMessages.shift()!;\n\t\t\ttry {\n\t\t\t\t// applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it\n\t\t\t\tconst localOpMetadata = await this.stateHandler.applyStashedOp(nextMessage.content);\n\t\t\t\tif (!this.stateHandler.isAttached()) {\n\t\t\t\t\tif (localOpMetadata !== undefined) {\n\t\t\t\t\t\tthrow new Error(\"Local Op Metadata must be undefined when not attached\");\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tnextMessage.localOpMetadata = localOpMetadata;\n\t\t\t\t\t// then we push onto pendingMessages which will cause PendingStateManager to resubmit when we connect\n\t\t\t\t\tpatchBatchIdContext(nextMessage); // Back compat\n\t\t\t\t\tthis.pendingMessages.push(nextMessage);\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tthrow DataProcessingError.wrapIfUnrecognized(error, \"applyStashedOp\", nextMessage);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Processes the incoming batch from the server. It verifies that messages are received in the right order and\n\t * that the batch information is correct.\n\t * @param batch - The batch that is being processed.\n\t * @param batchStartCsn - The clientSequenceNumber of the start of this message's batch\n\t */\n\tpublic processPendingLocalBatch(\n\t\tbatch: InboundSequencedContainerRuntimeMessage[],\n\t\tbatchStartCsn: number,\n\t): {\n\t\tmessage: InboundSequencedContainerRuntimeMessage;\n\t\tlocalOpMetadata: unknown;\n\t}[] {\n\t\treturn batch.map((message) => ({\n\t\t\tmessage,\n\t\t\tlocalOpMetadata: this.processPendingLocalMessage(message, batchStartCsn),\n\t\t}));\n\t}\n\n\t/**\n\t * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that\n\t * the batch information was preserved for batch messages.\n\t * @param message - The message that got ack'd and needs to be processed.\n\t * @param batchStartCsn - The clientSequenceNumber of the start of this message's batch (assigned during submit)\n\t * (not to be confused with message.clientSequenceNumber - the overwritten value in case of grouped batching)\n\t */\n\tprivate processPendingLocalMessage(\n\t\tmessage: InboundSequencedContainerRuntimeMessage,\n\t\tbatchStartCsn: number,\n\t): unknown {\n\t\t// Get the next message from the pending queue. Verify a message exists.\n\t\tconst pendingMessage = this.pendingMessages.peekFront();\n\t\tassert(\n\t\t\tpendingMessage !== undefined,\n\t\t\t0x169 /* \"No pending message found for this remote message\" */,\n\t\t);\n\n\t\t// This may be the start of a batch.\n\t\tthis.maybeProcessBatchBegin(message, batchStartCsn, pendingMessage);\n\n\t\tpendingMessage.sequenceNumber = message.sequenceNumber;\n\t\tthis.savedOps.push(withoutLocalOpMetadata(pendingMessage));\n\n\t\tthis.pendingMessages.shift();\n\n\t\tconst messageContent = buildPendingMessageContent(message);\n\n\t\t// Stringified content should match\n\t\tif (pendingMessage.content !== messageContent) {\n\t\t\tthis.stateHandler.close(\n\t\t\t\tDataProcessingError.create(\n\t\t\t\t\t\"pending local message content mismatch\",\n\t\t\t\t\t\"unexpectedAckReceived\",\n\t\t\t\t\tmessage,\n\t\t\t\t\t{\n\t\t\t\t\t\texpectedMessageType: JSON.parse(pendingMessage.content).type,\n\t\t\t\t\t},\n\t\t\t\t),\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\t// Post-processing part - If we are processing a batch then this could be the last message in the batch.\n\t\tthis.maybeProcessBatchEnd(message);\n\n\t\treturn pendingMessage.localOpMetadata;\n\t}\n\n\t/**\n\t * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.\n\t * @param message - The message that is being processed.\n\t * @param batchStartCsn - The clientSequenceNumber of the start of this message's batch (assigned during submit)\n\t * @param pendingMessage - The corresponding pendingMessage.\n\t */\n\tprivate maybeProcessBatchBegin(\n\t\tmessage: ISequencedDocumentMessage,\n\t\tbatchStartCsn: number,\n\t\tpendingMessage: IPendingMessage,\n\t) {\n\t\tif (!this.isProcessingBatch) {\n\t\t\t// Expecting the start of a batch (maybe single-message).\n\t\t\tif (pendingMessage.batchIdContext.batchStartCsn !== batchStartCsn) {\n\t\t\t\tthis.logger?.sendErrorEvent({\n\t\t\t\t\teventName: \"BatchClientSequenceNumberMismatch\",\n\t\t\t\t\tdetails: {\n\t\t\t\t\t\tprocessingBatch: !!this.pendingBatchBeginMessage,\n\t\t\t\t\t\tpendingBatchCsn: pendingMessage.batchIdContext.batchStartCsn,\n\t\t\t\t\t\tbatchStartCsn,\n\t\t\t\t\t\tmessageBatchMetadata: (message.metadata as any)?.batch,\n\t\t\t\t\t\tpendingMessageBatchMetadata: (pendingMessage.opMetadata as any)?.batch,\n\t\t\t\t\t},\n\t\t\t\t\tmessageDetails: extractSafePropertiesFromMessage(message),\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\t// This message is the first in a batch if the \"batch\" property on the metadata is set to true\n\t\tif ((message.metadata as IBatchMetadata | undefined)?.batch) {\n\t\t\t// We should not already be processing a batch and there should be no pending batch begin message.\n\t\t\tassert(\n\t\t\t\t!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,\n\t\t\t\t0x16b /* \"The pending batch state indicates we are already processing a batch\" */,\n\t\t\t);\n\n\t\t\t// Set the pending batch state indicating we have started processing a batch.\n\t\t\tthis.pendingBatchBeginMessage = message;\n\t\t\tthis.isProcessingBatch = true;\n\t\t}\n\t}\n\n\t/**\n\t * This message could be the last message in batch. If so, clear batch state since the batch is complete.\n\t * @param message - The message that is being processed.\n\t */\n\tprivate maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n\t\tif (!this.isProcessingBatch) {\n\t\t\treturn;\n\t\t}\n\n\t\t// There should be a pending batch begin message.\n\t\tassert(\n\t\t\tthis.pendingBatchBeginMessage !== undefined,\n\t\t\t0x16d /* \"There is no pending batch begin message\" */,\n\t\t);\n\n\t\tconst batchEndMetadata = (message.metadata as IBatchMetadata | undefined)?.batch;\n\t\tif (this.pendingMessages.isEmpty() || batchEndMetadata === false) {\n\t\t\t// Get the batch begin metadata from the first message in the batch.\n\t\t\tconst batchBeginMetadata = (\n\t\t\t\tthis.pendingBatchBeginMessage.metadata as IBatchMetadata | undefined\n\t\t\t)?.batch;\n\n\t\t\t// There could be just a single message in the batch. If so, it should not have any batch metadata. If there\n\t\t\t// are multiple messages in the batch, verify that we got the correct batch begin and end metadata.\n\t\t\tif (this.pendingBatchBeginMessage === message) {\n\t\t\t\tassert(\n\t\t\t\t\tbatchBeginMetadata === undefined,\n\t\t\t\t\t0x16e /* \"Batch with single message should not have batch metadata\" */,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tif (batchBeginMetadata !== true || batchEndMetadata !== false) {\n\t\t\t\t\tthis.stateHandler.close(\n\t\t\t\t\t\tDataProcessingError.create(\n\t\t\t\t\t\t\t\"Pending batch inconsistency\", // Formerly known as asserts 0x16f and 0x170\n\t\t\t\t\t\t\t\"processPendingLocalMessage\",\n\t\t\t\t\t\t\tmessage,\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\truntimeVersion: pkgVersion,\n\t\t\t\t\t\t\t\tbatchClientId:\n\t\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n\t\t\t\t\t\t\t\t\tthis.pendingBatchBeginMessage.clientId === null\n\t\t\t\t\t\t\t\t\t\t? \"null\"\n\t\t\t\t\t\t\t\t\t\t: this.pendingBatchBeginMessage.clientId,\n\t\t\t\t\t\t\t\tclientId: this.stateHandler.clientId(),\n\t\t\t\t\t\t\t\thasBatchStart: batchBeginMetadata === true,\n\t\t\t\t\t\t\t\thasBatchEnd: batchEndMetadata === false,\n\t\t\t\t\t\t\t\tmessageType: message.type,\n\t\t\t\t\t\t\t\tpendingMessagesCount: this.pendingMessagesCount,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Clear the pending batch state now that we have processed the entire batch.\n\t\t\tthis.pendingBatchBeginMessage = undefined;\n\t\t\tthis.isProcessingBatch = false;\n\t\t}\n\t}\n\n\t/**\n\t * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending\n\t * states in its queue. This includes triggering resubmission of unacked ops.\n\t * ! Note: successfully resubmitting an op that has been successfully sequenced is not possible due to checks in the ConnectionStateHandler (Loader layer)\n\t */\n\tpublic replayPendingStates() {\n\t\tassert(\n\t\t\tthis.stateHandler.connected(),\n\t\t\t0x172 /* \"The connection state is not consistent with the runtime\" */,\n\t\t);\n\n\t\t// This assert suggests we are about to send same ops twice, which will result in data loss.\n\t\tassert(\n\t\t\tthis.clientIdFromLastReplay !== this.stateHandler.clientId(),\n\t\t\t0x173 /* \"replayPendingStates called twice for same clientId!\" */,\n\t\t);\n\t\tthis.clientIdFromLastReplay = this.stateHandler.clientId();\n\n\t\tassert(\n\t\t\tthis.initialMessages.isEmpty(),\n\t\t\t0x174 /* \"initial states should be empty before replaying pending\" */,\n\t\t);\n\n\t\tconst initialPendingMessagesCount = this.pendingMessages.length;\n\t\tlet remainingPendingMessagesCount = this.pendingMessages.length;\n\n\t\t// Process exactly `pendingMessagesCount` items in the queue as it represents the number of messages that were\n\t\t// pending when we connected. This is important because the `reSubmitFn` might add more items in the queue\n\t\t// which must not be replayed.\n\t\twhile (remainingPendingMessagesCount > 0) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tlet pendingMessage = this.pendingMessages.shift()!;\n\t\t\tremainingPendingMessagesCount--;\n\n\t\t\tconst batchMetadataFlag = asBatchMetadata(pendingMessage.opMetadata)?.batch;\n\t\t\tassert(batchMetadataFlag !== false, 0x41b /* We cannot process batches in chunks */);\n\n\t\t\t// The next message starts a batch (possibly single-message), and we'll need its batchId.\n\t\t\t// We'll find batchId on this message if it was previously generated.\n\t\t\t// Otherwise, generate it now - this is the first time resubmitting this batch.\n\t\t\tconst batchId =\n\t\t\t\tasBatchMetadata(pendingMessage.opMetadata)?.batchId ??\n\t\t\t\tgenerateBatchId(\n\t\t\t\t\tpendingMessage.batchIdContext.clientId,\n\t\t\t\t\tpendingMessage.batchIdContext.batchStartCsn,\n\t\t\t\t);\n\n\t\t\t/**\n\t\t\t * We must preserve the distinct batches on resubmit.\n\t\t\t * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will\n\t\t\t * either receive the whole batch ack or nothing at all. @see ScheduleManager for how this works.\n\t\t\t */\n\t\t\tif (batchMetadataFlag === undefined) {\n\t\t\t\t// Single-message batch\n\n\t\t\t\tthis.stateHandler.reSubmitBatch(\n\t\t\t\t\t[\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcontent: pendingMessage.content,\n\t\t\t\t\t\t\tlocalOpMetadata: pendingMessage.localOpMetadata,\n\t\t\t\t\t\t\topMetadata: pendingMessage.opMetadata,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tbatchId,\n\t\t\t\t);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// else: batchMetadataFlag === true (It's a typical multi-message batch)\n\n\t\t\tassert(\n\t\t\t\tremainingPendingMessagesCount > 0,\n\t\t\t\t0x554 /* Last pending message cannot be a batch begin */,\n\t\t\t);\n\n\t\t\tconst batch: PendingMessageResubmitData[] = [];\n\n\t\t\t// check is >= because batch end may be last pending message\n\t\t\twhile (remainingPendingMessagesCount >= 0) {\n\t\t\t\tbatch.push({\n\t\t\t\t\tcontent: pendingMessage.content,\n\t\t\t\t\tlocalOpMetadata: pendingMessage.localOpMetadata,\n\t\t\t\t\topMetadata: pendingMessage.opMetadata,\n\t\t\t\t});\n\n\t\t\t\tif (pendingMessage.opMetadata?.batch === false) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tassert(remainingPendingMessagesCount > 0, 0x555 /* No batch end found */);\n\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\t\tpendingMessage = this.pendingMessages.shift()!;\n\t\t\t\tremainingPendingMessagesCount--;\n\t\t\t\tassert(\n\t\t\t\t\tpendingMessage.opMetadata?.batch !== true,\n\t\t\t\t\t0x556 /* Batch start needs a corresponding batch end */,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthis.stateHandler.reSubmitBatch(batch, batchId);\n\t\t}\n\n\t\t// pending ops should no longer depend on previous sequenced local ops after resubmit\n\t\tthis.savedOps = [];\n\n\t\t// We replayPendingStates on read connections too - we expect these to get nack'd though, and to then reconnect\n\t\t// on a write connection and replay again. This filters out the replay that happens on the read connection so\n\t\t// we only see the replays on write connections (that have a chance to go through).\n\t\tif (this.stateHandler.isActiveConnection()) {\n\t\t\tthis.logger?.sendTelemetryEvent({\n\t\t\t\teventName: \"PendingStatesReplayed\",\n\t\t\t\tcount: initialPendingMessagesCount,\n\t\t\t\tclientId: this.stateHandler.clientId(),\n\t\t\t});\n\t\t}\n\t}\n}\n\n/** For back-compat if trying to apply stashed ops that pre-date batchIdContext */\nfunction patchBatchIdContext(\n\tmessage: IPendingMessageFromStash,\n): asserts message is IPendingMessage {\n\tconst batchIdContext: IPendingMessageFromStash[\"batchIdContext\"] = message.batchIdContext;\n\tif (batchIdContext === undefined) {\n\t\t// Using uuid guarantees uniqueness, retaining existing behavior\n\t\tmessage.batchIdContext = { clientId: uuid(), batchStartCsn: -1 };\n\t}\n}\n"]}
|
package/dist/public.d.ts
CHANGED
package/dist/scheduleManager.js
CHANGED
|
@@ -84,6 +84,8 @@ class ScheduleManagerCore {
|
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
86
|
// First message will have the batch flag set to true if doing a batched send
|
|
87
|
+
// Non null asserting because of the length check above
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
87
89
|
const firstMessageMetadata = messages[0].metadata;
|
|
88
90
|
if (!firstMessageMetadata?.batch) {
|
|
89
91
|
return;
|
|
@@ -94,6 +96,8 @@ class ScheduleManagerCore {
|
|
|
94
96
|
return;
|
|
95
97
|
}
|
|
96
98
|
// Set the batch flag to false on the last message to indicate the end of the send batch
|
|
99
|
+
// Non null asserting here because of the length check at the start of the function
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
97
101
|
const lastMessage = messages[messages.length - 1];
|
|
98
102
|
// TODO: It's not clear if this shallow clone is required, as opposed to just setting "batch" to false.
|
|
99
103
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|