@fluidframework/container-runtime 0.51.2 → 0.51.3
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/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/pendingStateManager.d.ts +0 -1
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +0 -36
- package/dist/pendingStateManager.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/pendingStateManager.d.ts +0 -1
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +0 -36
- package/lib/pendingStateManager.js.map +1 -1
- package/package.json +11 -11
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +0 -43
package/dist/packageVersion.d.ts
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export declare const pkgName = "@fluidframework/container-runtime";
|
|
8
|
-
export declare const pkgVersion = "0.51.
|
|
8
|
+
export declare const pkgVersion = "0.51.3";
|
|
9
9
|
//# sourceMappingURL=packageVersion.d.ts.map
|
package/dist/packageVersion.js
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.pkgVersion = exports.pkgName = void 0;
|
|
10
10
|
exports.pkgName = "@fluidframework/container-runtime";
|
|
11
|
-
exports.pkgVersion = "0.51.
|
|
11
|
+
exports.pkgVersion = "0.51.3";
|
|
12
12
|
//# sourceMappingURL=packageVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,mCAAmC,CAAC;AAC9C,QAAA,UAAU,GAAG,QAAQ,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/container-runtime\";\nexport const pkgVersion = \"0.51.
|
|
1
|
+
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEU,QAAA,OAAO,GAAG,mCAAmC,CAAC;AAC9C,QAAA,UAAU,GAAG,QAAQ,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/container-runtime\";\nexport const pkgVersion = \"0.51.3\";\n"]}
|
|
@@ -61,7 +61,6 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
61
61
|
private readonly initialStates;
|
|
62
62
|
private readonly previousClientIds;
|
|
63
63
|
private readonly firstStashedCSN;
|
|
64
|
-
private stashedCount;
|
|
65
64
|
private readonly disposeOnce;
|
|
66
65
|
private pendingMessagesCount;
|
|
67
66
|
private isProcessingBatch;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingStateManager.d.ts","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAC;AAGjE,OAAO,EACH,yBAAyB,EAC5B,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAoB,MAAM,oBAAoB,CAAC;AAE9F;;;GAGG;AACH,MAAM,WA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AS;IAehD,IAAW,QAAQ,YAAyC;IAC5D,SAAgB,OAAO,aAAgC;IAEvD;;;;;;;OAOG;IACI,eAAe,CAClB,IAAI,EAAE,oBAAoB,EAC1B,oBAAoB,EAAE,MAAM,EAC5B,uBAAuB,EAAE,MAAM,EAC/B,OAAO,EAAE,GAAG,EACZ,eAAe,EAAE,OAAO,EACxB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS;IAiBnD;;;OAGG;IACI,kBAAkB,CAAC,SAAS,EAAE,SAAS;IA0B9C;;OAEG;IACI,OAAO;IAsBd;;OAEG;IACU,iBAAiB,CAAC,MAAM,EAAE,MAAM;IAuB7C;;;;;OAKG;IACI,cAAc,CAAC,OAAO,EAAE,yBAAyB,EAAE,KAAK,EAAE,OAAO;;;;IAaxE;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAiC5B,OAAO,CAAC,aAAa;IAqBrB;;;;OAIG;IACH,OAAO,CAAC,0BAA0B;IAsClC;;;OAGG;IACH,OAAO,CAAC,sBAAsB;IAyB9B,OAAO,CAAC,oBAAoB;IAuC5B;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAM5B;;;OAGG;IACI,mBAAmB;CAwD7B"}
|
|
@@ -30,7 +30,6 @@ class PendingStateManager {
|
|
|
30
30
|
this.pendingStates = new double_ended_queue_1.default();
|
|
31
31
|
this.previousClientIds = new Set();
|
|
32
32
|
this.firstStashedCSN = -1;
|
|
33
|
-
this.stashedCount = 0;
|
|
34
33
|
this.disposeOnce = new common_utils_1.Lazy(() => {
|
|
35
34
|
this.initialStates.clear();
|
|
36
35
|
this.pendingStates.clear();
|
|
@@ -48,7 +47,6 @@ class PendingStateManager {
|
|
|
48
47
|
// get stashed op count and client sequence number of first op
|
|
49
48
|
const messages = initialState.pendingStates
|
|
50
49
|
.filter((state) => state.type === "message");
|
|
51
|
-
this.stashedCount = messages.length;
|
|
52
50
|
this.firstStashedCSN = messages[0].clientSequenceNumber;
|
|
53
51
|
}
|
|
54
52
|
}
|
|
@@ -208,7 +206,6 @@ class PendingStateManager {
|
|
|
208
206
|
// if it's not a message just drop it and keep looking
|
|
209
207
|
if (nextState.type === "message") {
|
|
210
208
|
this.assertOpMatch(nextState, message, isOriginalClientId);
|
|
211
|
-
--this.stashedCount;
|
|
212
209
|
return { localAck: true, localOpMetadata: nextState.localOpMetadata };
|
|
213
210
|
}
|
|
214
211
|
}
|
|
@@ -340,45 +337,12 @@ class PendingStateManager {
|
|
|
340
337
|
common_utils_1.assert(this.connected, 0x172 /* "The connection state is not consistent with the runtime" */);
|
|
341
338
|
// This assert suggests we are about to send same ops twice, which will result in data loss.
|
|
342
339
|
common_utils_1.assert(this.clientId !== this.containerRuntime.clientId, 0x173 /* "replayPendingStates called twice for same clientId!" */);
|
|
343
|
-
const prevClientId = this.clientId;
|
|
344
340
|
this.clientId = this.containerRuntime.clientId;
|
|
345
341
|
common_utils_1.assert(this.initialStates.isEmpty(), 0x174 /* "initial states should be empty before replaying pending" */);
|
|
346
342
|
let pendingStatesCount = this.pendingStates.length;
|
|
347
343
|
if (pendingStatesCount === 0) {
|
|
348
344
|
return;
|
|
349
345
|
}
|
|
350
|
-
if (!prevClientId && this.stashedCount > 0) {
|
|
351
|
-
// this is first connect, verify we are about to "resubmit" only stashed ops
|
|
352
|
-
common_utils_1.assert(this.pendingStates.toArray().filter((s) => s.type === "message").length === this.stashedCount, 0x290 /* "unexpected message queued before first connect" */);
|
|
353
|
-
Array.from(this.previousClientIds).map((id) => common_utils_1.assert(this.containerRuntime.getQuorum().getMember(id) === undefined, 0x291 /* "client with stashed ops already connected" */));
|
|
354
|
-
// send rejoin op with stashed client ID if we have it
|
|
355
|
-
if (this.previousClientIds.size > 0) {
|
|
356
|
-
const clientId = Array.from(this.previousClientIds)[0];
|
|
357
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
358
|
-
this.pendingStates.unshift({
|
|
359
|
-
type: "message",
|
|
360
|
-
messageType: containerRuntime_1.ContainerMessageType.Rejoin,
|
|
361
|
-
content: { clientId },
|
|
362
|
-
});
|
|
363
|
-
++pendingStatesCount;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
if (prevClientId) {
|
|
367
|
-
// add a rejoin op so future clients provided with our stashed pending ops can recognize them
|
|
368
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
369
|
-
const firstState = this.pendingStates.peekFront();
|
|
370
|
-
if (firstState.type !== "message" || firstState.messageType !== containerRuntime_1.ContainerMessageType.Rejoin) {
|
|
371
|
-
// if there is already a rejoin op in the queue, just resubmit same op under new client ID
|
|
372
|
-
// otherwise, add one to the queue
|
|
373
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
374
|
-
this.pendingStates.unshift({
|
|
375
|
-
type: "message",
|
|
376
|
-
messageType: containerRuntime_1.ContainerMessageType.Rejoin,
|
|
377
|
-
content: { clientId: prevClientId },
|
|
378
|
-
});
|
|
379
|
-
++pendingStatesCount;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
346
|
// Reset the pending message count because all these messages will be removed from the queue.
|
|
383
347
|
this.pendingMessagesCount = 0;
|
|
384
348
|
// Save the current FlushMode so that we can revert it back after replaying the states.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAGH,+DAA4D;AAC5D,qEAAsE;AAItE,6EAAgE;AAChE,4EAAuC;AACvC,yDAA8F;AA8C9F;;;;;;;;GAQG;AACH,MAAa,mBAAmB;IA+C5B,YACqB,gBAAkC,EAClC,cAAmD,EACpE,YAA4C;;QAF3B,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,mBAAc,GAAd,cAAc,CAAqC;QAhDvD,kBAAa,GAAG,IAAI,4BAAK,EAAiB,CAAC;QAE3C,sBAAiB,GAAG,IAAI,GAAG,EAAU,CAAC;QACtC,oBAA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M;gBACV,KAAK,WAAW;oBACZ;wBACI,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;qBAC9D;oBACD,MAAM;gBACV,KAAK,OAAO;oBACR;wBACI,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;qBACjC;oBACD,MAAM;gBACV;oBACI,MAAM;aACb;YACD,kBAAkB,EAAE,CAAC;SACxB;QAED,wBAAwB;QACxB,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IACvD,CAAC;CACJ;AA9dD,kDA8dC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IDisposable } from \"@fluidframework/common-definitions\";\nimport { assert, Lazy } from \"@fluidframework/common-utils\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\nimport { FlushMode } from \"@fluidframework/runtime-definitions\";\nimport Deque from \"double-ended-queue\";\nimport { ContainerRuntime, ContainerMessageType, isRuntimeMessage } from \"./containerRuntime\";\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 type: \"message\";\n messageType: ContainerMessageType;\n clientSequenceNumber: number;\n referenceSequenceNumber: number;\n content: any;\n localOpMetadata: unknown;\n opMetadata: Record<string, unknown> | undefined;\n}\n\n/**\n * This represents a FlushMode update and is added to the pending queue when `setFlushMode` is called on the\n * ContainerRuntime and the FlushMode changes.\n */\nexport interface IPendingFlushMode {\n type: \"flushMode\";\n flushMode: FlushMode;\n}\n\n/**\n * This represents a manual flush and is added to the pending queue when `flush` is called on the ContainerRuntime to\n * flush any pending messages. This is applicable only when the FlushMode is Manual.\n */\nexport interface IPendingFlush {\n type: \"flush\";\n}\n\nexport type IPendingState = IPendingMessage | IPendingFlushMode | IPendingFlush;\n\nexport interface IPendingLocalState {\n /**\n * client ID we most recently connected with, or undefined if we never connected\n */\n clientId?: string;\n /**\n * list of pending states, including ops and batch information\n */\n pendingStates: IPendingState[];\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 setting the FlushMode, 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 private readonly pendingStates = new Deque<IPendingState>();\n private readonly initialStates: Deque<IPendingState>;\n private readonly previousClientIds = new Set<string>();\n private readonly firstStashedCSN: number = -1;\n private stashedCount = 0;\n private readonly disposeOnce = new Lazy<void>(() => {\n this.initialStates.clear();\n this.pendingStates.clear();\n });\n\n // Maintains the count of messages that are currently unacked.\n private pendingMessagesCount: number = 0;\n\n // Indicates whether we are processing a batch.\n private isProcessingBatch: boolean = false;\n\n // This stores the first message in the batch that we are processing. This is used to verify that we get\n // the correct batch metadata.\n private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;\n\n private clientId: string | undefined;\n\n private get connected(): boolean {\n return this.containerRuntime.connected;\n }\n\n /**\n * Called to check if there are any pending messages in the pending state queue.\n * @returns A boolean indicating whether there are messages or not.\n */\n public hasPendingMessages(): boolean {\n return this.pendingMessagesCount !== 0;\n }\n\n public getLocalState(): IPendingLocalState | undefined {\n if (this.hasPendingMessages()) {\n return {\n clientId: this.clientId,\n pendingStates: this.pendingStates.toArray().map(\n // delete localOpMetadata since it may not be serializable\n // and will be regenerated by applyStashedOp()\n (state) => state.type === \"message\" ? {...state, localOpMetadata: undefined } : state),\n };\n }\n }\n\n constructor(\n private readonly containerRuntime: ContainerRuntime,\n private readonly applyStashedOp: (type, content) => Promise<unknown>,\n initialState: IPendingLocalState | undefined,\n ) {\n this.initialStates = new Deque<IPendingState>(initialState?.pendingStates ?? []);\n\n if (initialState) {\n if (initialState?.clientId) {\n this.previousClientIds.add(initialState.clientId);\n }\n // get stashed op count and client sequence number of first op\n const messages = initialState.pendingStates\n .filter((state) => state.type === \"message\") as IPendingMessage[];\n this.stashedCount = messages.length;\n this.firstStashedCSN = messages[0].clientSequenceNumber;\n }\n }\n\n public get disposed() { return this.disposeOnce.evaluated; }\n public readonly dispose = () => this.disposeOnce.value;\n\n /**\n * Called when a message is submitted locally. Adds the message and the associated details to the pending state\n * queue.\n * @param type - The container message type.\n * @param clientSequenceNumber - The clientSequenceNumber associated with the message.\n * @param content - The message content.\n * @param localOpMetadata - The local metadata associated with the message.\n */\n public onSubmitMessage(\n type: ContainerMessageType,\n clientSequenceNumber: number,\n referenceSequenceNumber: number,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined,\n ) {\n const pendingMessage: IPendingMessage = {\n type: \"message\",\n messageType: type,\n clientSequenceNumber,\n referenceSequenceNumber,\n content,\n localOpMetadata,\n opMetadata,\n };\n\n this.pendingStates.push(pendingMessage);\n\n this.pendingMessagesCount++;\n }\n\n /**\n * Called when the FlushMode is updated. Adds the FlushMode to the pending state queue.\n * @param flushMode - The flushMode that was updated.\n */\n public onFlushModeUpdated(flushMode: FlushMode) {\n if (flushMode === FlushMode.Immediate) {\n const previousState = this.pendingStates.peekBack();\n\n // We don't have to track a previous \"flush\" state because FlushMode.Immediate flushes the messages. So,\n // just tracking this FlushMode.Immediate is enough.\n if (previousState?.type === \"flush\") {\n this.pendingStates.removeBack();\n }\n\n // If no messages were sent between FlushMode.TurnBased and FlushMode.Immediate,\n // then we do not have to track both these states.\n // Remove FlushMode.TurnBased from the pending queue and return.\n if (previousState?.type === \"flushMode\" && previousState.flushMode === FlushMode.TurnBased) {\n this.pendingStates.removeBack();\n return;\n }\n }\n\n const pendingFlushMode: IPendingFlushMode = {\n type: \"flushMode\",\n flushMode,\n };\n this.pendingStates.push(pendingFlushMode);\n }\n\n /**\n * Called when flush() is called on the ContainerRuntime to manually flush messages.\n */\n public onFlush() {\n // If the FlushMode is Immediate, we should not track this flush call as it is only applicable when FlushMode\n // is TurnBased.\n if (this.containerRuntime.flushMode === FlushMode.Immediate) {\n return;\n }\n\n // If the previous state is not a message, we don't have to track this flush call as there is nothing to flush.\n const previousState = this.pendingStates.peekBack();\n if (previousState?.type !== \"message\") {\n return;\n }\n\n // Note that because of the checks above and the checks in onFlushModeUpdated(), we can be sure that a \"flush\"\n // state always has a \"message\" before and after it. So, it marks the end of a batch and the beginning of a\n // new one.\n const pendingFlush: IPendingFlush = {\n type: \"flush\",\n };\n this.pendingStates.push(pendingFlush);\n }\n\n /**\n * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n */\n public async applyStashedOpsAt(seqNum: number) {\n // apply stashed ops at sequence number\n while (!this.initialStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.initialStates.peekFront()!;\n if (nextState.type === \"message\") {\n if (nextState.referenceSequenceNumber > seqNum) {\n break; // nothing left to do at this sequence number\n } else if (nextState.referenceSequenceNumber > 0 && nextState.referenceSequenceNumber < seqNum) {\n throw new Error(\"loaded from snapshot too recent to apply stashed ops\");\n }\n\n // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it\n const localOpMetadata = await this.applyStashedOp(nextState.messageType, nextState.content);\n nextState.localOpMetadata = localOpMetadata;\n }\n\n // then we push onto pendingStates which will cause PendingStateManager to resubmit when we connect\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.pendingStates.push(this.initialStates.shift()!);\n }\n }\n\n /**\n * Processes a local message once it's ack'd by the server to verify that there was no data corruption and that\n * the batch information was preserved for batch messages. Also process remote messages that might have been\n * sent from a previous container.\n * @param message - The messsage that got ack'd and needs to be processed.\n */\n public processMessage(message: ISequencedDocumentMessage, local: boolean) {\n // Do not process chunked ops until all pieces are available.\n if (message.type === ContainerMessageType.ChunkedOp) {\n return { localAck: false, localOpMetadata: undefined };\n }\n\n if (local) {\n return { localAck: false, localOpMetadata: this.processPendingLocalMessage(message) };\n } else {\n return this.processRemoteMessage(message);\n }\n }\n\n /**\n * Listens for ACKs of stashed ops\n */\n private processRemoteMessage(message: ISequencedDocumentMessage) {\n if (!isRuntimeMessage(message)) {\n return { localAck: false, localOpMetadata: undefined };\n }\n\n // this message was a pending op that was actually sent successfully\n const isOriginalClientId = message.clientId === Array.from(this.previousClientIds)[0] &&\n message.clientSequenceNumber >= this.firstStashedCSN;\n // this message is a pending or stashed op that was resubmitted\n const isNewClientId = Array.from(this.previousClientIds).indexOf(message.clientId) > 0;\n\n // if this is an ack for a stashed op, dequeue one message.\n // we should have seen its ref seq num by now and the DDS should be ready for it to be ACKed\n if (isOriginalClientId || isNewClientId) {\n assert(this.clientId === undefined, 0x28b /* \"multiple clients connected with stashed ops\" */);\n while (!this.pendingStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.pendingStates.shift()!;\n // if it's not a message just drop it and keep looking\n if (nextState.type === \"message\") {\n this.assertOpMatch(nextState, message, isOriginalClientId);\n --this.stashedCount;\n return { localAck: true, localOpMetadata: nextState.localOpMetadata };\n }\n }\n }\n\n if (message.type === ContainerMessageType.Rejoin && this.previousClientIds.has(message.contents?.clientId)) {\n this.previousClientIds.add(message.clientId);\n }\n\n return { localAck: false, localOpMetadata: undefined };\n }\n\n private assertOpMatch(state: IPendingMessage, message: ISequencedDocumentMessage, isOriginalClientId: boolean) {\n assert(message.type === state.messageType, 0x28c /* \"different message type\" */);\n assert(message.clientSequenceNumber === state.clientSequenceNumber || !isOriginalClientId,\n 0x28d /* \"client sequence number doesn't match\" */);\n switch(message.type) {\n case ContainerMessageType.Attach:\n assert(message.contents.id === state.content.id, 0x28e /* \"datastore ID doesn't match\" */);\n break;\n case ContainerMessageType.FluidDataStoreOp:\n assert(message.contents.address === state.content.address, 0x28f /* \"address doesn't match\" */);\n break;\n case ContainerMessageType.BlobAttach:\n // todo: assert we have blob storage, assert blob IDs match, remove blob from blob storage since it made\n // it through successfully\n break;\n case ContainerMessageType.Rejoin:\n default:\n throw new Error(`${message.type} not expected`);\n }\n }\n\n /**\n * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that\n * the batch information was preserved for batch messages.\n * @param message - The messsage that got ack'd and needs to be processed.\n */\n private processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {\n // Pre-processing part - This may be the start of a batch.\n this.maybeProcessBatchBegin(message);\n\n // Get the next state from the pending queue and verify that it is of type \"message\".\n const pendingState = this.peekNextPendingState();\n assert(pendingState.type === \"message\", 0x169 /* \"No pending message found for this remote message\" */);\n this.pendingStates.shift();\n\n // Processing part - Verify that there has been no data corruption.\n // The clientSequenceNumber of the incoming message must match that of the pending message.\n if (pendingState.clientSequenceNumber !== message.clientSequenceNumber) {\n // Close the container because this could indicate data corruption.\n const error = new DataProcessingError(\n \"unexpectedAckReceived\",\n \"unexpectedAckReceived\",\n {\n clientId: message.clientId,\n sequenceNumber: message.sequenceNumber,\n clientSequenceNumber: message.clientSequenceNumber,\n expectedClientSequenceNumber: pendingState.clientSequenceNumber,\n },\n );\n\n this.containerRuntime.closeFn(error);\n return;\n }\n\n this.pendingMessagesCount--;\n\n // Post-processing part - If we are processing a batch then this could be the last message in the batch.\n if (this.isProcessingBatch) {\n this.maybeProcessBatchEnd(message);\n }\n\n return pendingState.localOpMetadata;\n }\n\n /**\n * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {\n const pendingState = this.peekNextPendingState();\n if (pendingState.type !== \"flush\" && pendingState.type !== \"flushMode\") {\n return;\n }\n\n // If the pending state is of type \"flushMode\", it must be Manual since Automatic flush mode is processed\n // after a message is processed and not before.\n if (pendingState.type === \"flushMode\") {\n assert(pendingState.flushMode === FlushMode.TurnBased,\n 0x16a /* \"Flush mode should be manual when processing batch begin\" */);\n }\n\n // We should not already be processing a batch and there should be no pending batch begin message.\n assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,\n 0x16b /* \"The pending batch state indicates we are already processing a batch\" */);\n\n // Set the pending batch state indicating we have started processing a batch.\n this.pendingBatchBeginMessage = message;\n this.isProcessingBatch = true;\n\n // Remove this pending state from the queue as we have processed it.\n this.pendingStates.shift();\n }\n\n private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n const nextPendingState = this.peekNextPendingState();\n if (nextPendingState.type !== \"flush\" && nextPendingState.type !== \"flushMode\") {\n return;\n }\n\n // If the next pending state is of type \"flushMode\", it must be Immediate and if so, we need to remove it from\n // the queue.\n // Note that we do not remove the type \"flush\" from the queue because it indicates the end of one batch and the\n // beginning of a new one. So, it will removed when the next batch begin is processed.\n if (nextPendingState.type === \"flushMode\") {\n assert(nextPendingState.flushMode === FlushMode.Immediate,\n 0x16c /* \"Flush mode is set to TurnBased in the middle of processing a batch\" */);\n this.pendingStates.shift();\n }\n\n // There should be a pending batch begin message.\n assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* \"There is no pending batch begin message\" */);\n\n // Get the batch begin metadata from the first message in the batch.\n const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;\n\n // There could be just a single message in the batch. If so, it should not have any batch metadata. If there\n // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.\n if (this.pendingBatchBeginMessage === message) {\n assert(batchBeginMetadata === undefined,\n 0x16e /* \"Batch with single message should not have batch metadata\" */);\n } else {\n // Get the batch metadata from the last message in the batch.\n const batchEndMetadata = message.metadata?.batch;\n assert(batchBeginMetadata === true, 0x16f /* \"Did not receive batch begin metadata\" */);\n assert(batchEndMetadata === false, 0x170 /* \"Did not receive batch end metadata\" */);\n }\n\n // Clear the pending batch state now that we have processed the entire batch.\n this.pendingBatchBeginMessage = undefined;\n this.isProcessingBatch = false;\n }\n\n /**\n * Returns the next pending state from the pending state queue.\n */\n private peekNextPendingState(): IPendingState {\n const nextPendingState = this.pendingStates.peekFront();\n assert(!!nextPendingState, 0x171 /* \"No pending state found for the remote message\" */);\n return nextPendingState;\n }\n\n /**\n * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending\n * states in its queue. This includes setting the FlushMode and triggering resubmission of unacked ops.\n */\n public replayPendingStates() {\n assert(this.connected, 0x172 /* \"The connection state is not consistent with the runtime\" */);\n\n // This assert suggests we are about to send same ops twice, which will result in data loss.\n assert(this.clientId !== this.containerRuntime.clientId,\n 0x173 /* \"replayPendingStates called twice for same clientId!\" */);\n const prevClientId = this.clientId;\n this.clientId = this.containerRuntime.clientId;\n\n assert(this.initialStates.isEmpty(), 0x174 /* \"initial states should be empty before replaying pending\" */);\n\n let pendingStatesCount = this.pendingStates.length;\n if (pendingStatesCount === 0) {\n return;\n }\n\n if (!prevClientId && this.stashedCount > 0) {\n // this is first connect, verify we are about to \"resubmit\" only stashed ops\n assert(this.pendingStates.toArray().filter((s) => s.type === \"message\").length === this.stashedCount,\n 0x290 /* \"unexpected message queued before first connect\" */);\n\n Array.from(this.previousClientIds).map((id) =>\n assert(this.containerRuntime.getQuorum().getMember(id) === undefined,\n 0x291 /* \"client with stashed ops already connected\" */));\n\n // send rejoin op with stashed client ID if we have it\n if (this.previousClientIds.size > 0) {\n const clientId = Array.from(this.previousClientIds)[0];\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n this.pendingStates.unshift({\n type: \"message\",\n messageType: ContainerMessageType.Rejoin,\n content: { clientId },\n } as IPendingMessage);\n ++pendingStatesCount;\n }\n }\n\n if (prevClientId) {\n // add a rejoin op so future clients provided with our stashed pending ops can recognize them\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const firstState = this.pendingStates.peekFront()!;\n if (firstState.type !== \"message\" || firstState.messageType !== ContainerMessageType.Rejoin) {\n // if there is already a rejoin op in the queue, just resubmit same op under new client ID\n // otherwise, add one to the queue\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n this.pendingStates.unshift({\n type: \"message\",\n messageType: ContainerMessageType.Rejoin,\n content: { clientId: prevClientId },\n } as IPendingMessage);\n ++pendingStatesCount;\n }\n }\n\n // Reset the pending message count because all these messages will be removed from the queue.\n this.pendingMessagesCount = 0;\n\n // Save the current FlushMode so that we can revert it back after replaying the states.\n const savedFlushMode = this.containerRuntime.flushMode;\n\n // Process exactly `pendingStatesCount` items in the queue as it represents the number of states that were\n // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue\n // which must not be replayed.\n while (pendingStatesCount > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const pendingState = this.pendingStates.shift()!;\n switch (pendingState.type) {\n case \"message\":\n {\n this.containerRuntime.reSubmitFn(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata,\n pendingState.opMetadata);\n }\n break;\n case \"flushMode\":\n {\n this.containerRuntime.setFlushMode(pendingState.flushMode);\n }\n break;\n case \"flush\":\n {\n this.containerRuntime.flush();\n }\n break;\n default:\n break;\n }\n pendingStatesCount--;\n }\n\n // Revert the FlushMode.\n this.containerRuntime.setFlushMode(savedFlushMode);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAGH,+DAA4D;AAC5D,qEAAsE;AAItE,6EAAgE;AAChE,4EAAuC;AACvC,yDAA8F;AA8C9F;;;;;;;;GAQG;AACH,MAAa,mBAAmB;IA8C5B,YACqB,gBAAkC,EAClC,cAAmD,EACpE,YAA4C;;QAF3B,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,mBAAc,GAAd,cAAc,CAAqC;QA/CvD,kBAAa,GAAG,IAAI,4BAAK,EAAiB,CAAC;QAE3C,sBAAiB,GAAG,IAAI,GAAG,EAAU,CAAC;QACtC,oBAAe,GAAW,CAAC,CAAC,CAAC;QAC7B,gBAAW,GAAG,IAAI,mBAAI,CAAO,GAAG,EAAE;YAC/C,IAAI,CAAC,aAAa,CAAC,KAAK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AAQ,CAAC;QAE/C,qBAAM,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,EAAE,KAAK,CAAC,+DAA+D,CAAC,CAAC;QAE5G,IAAI,kBAAkB,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;QACnD,IAAI,kBAAkB,KAAK,CAAC,EAAE;YAC1B,OAAO;SACV;QAED,6FAA6F;QAC7F,IAAI,CAAC,oBAAoB,GAAG,CAAC,CAAC;QAE9B,uFAAuF;QACvF,MAAM,cAAc,GAAG,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC;QAEvD,0GAA0G;QAC1G,0GAA0G;QAC1G,8BAA8B;QAC9B,OAAO,kBAAkB,GAAG,CAAC,EAAE;YAC3B,oEAAoE;YACpE,MAAM,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC,KAAK,EAAG,CAAC;YACjD,QAAQ,YAAY,CAAC,IAAI,EAAE;gBACvB,KAAK,SAAS;oBACV;wBACI,IAAI,CAAC,gBAAgB,CAAC,UAAU,CAC5B,YAAY,CAAC,WAAW,EACxB,YAAY,CAAC,OAAO,EACpB,YAAY,CAAC,eAAe,EAC5B,YAAY,CAAC,UAAU,CAAC,CAAC;qBAChC;oBACD,MAAM;gBACV,KAAK,WAAW;oBACZ;wBACI,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;qBAC9D;oBACD,MAAM;gBACV,KAAK,OAAO;oBACR;wBACI,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;qBACjC;oBACD,MAAM;gBACV;oBACI,MAAM;aACb;YACD,kBAAkB,EAAE,CAAC;SACxB;QAED,wBAAwB;QACxB,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IACvD,CAAC;CACJ;AAnbD,kDAmbC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IDisposable } from \"@fluidframework/common-definitions\";\nimport { assert, Lazy } from \"@fluidframework/common-utils\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\nimport { FlushMode } from \"@fluidframework/runtime-definitions\";\nimport Deque from \"double-ended-queue\";\nimport { ContainerRuntime, ContainerMessageType, isRuntimeMessage } from \"./containerRuntime\";\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 type: \"message\";\n messageType: ContainerMessageType;\n clientSequenceNumber: number;\n referenceSequenceNumber: number;\n content: any;\n localOpMetadata: unknown;\n opMetadata: Record<string, unknown> | undefined;\n}\n\n/**\n * This represents a FlushMode update and is added to the pending queue when `setFlushMode` is called on the\n * ContainerRuntime and the FlushMode changes.\n */\nexport interface IPendingFlushMode {\n type: \"flushMode\";\n flushMode: FlushMode;\n}\n\n/**\n * This represents a manual flush and is added to the pending queue when `flush` is called on the ContainerRuntime to\n * flush any pending messages. This is applicable only when the FlushMode is Manual.\n */\nexport interface IPendingFlush {\n type: \"flush\";\n}\n\nexport type IPendingState = IPendingMessage | IPendingFlushMode | IPendingFlush;\n\nexport interface IPendingLocalState {\n /**\n * client ID we most recently connected with, or undefined if we never connected\n */\n clientId?: string;\n /**\n * list of pending states, including ops and batch information\n */\n pendingStates: IPendingState[];\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 setting the FlushMode, 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 private readonly pendingStates = new Deque<IPendingState>();\n private readonly initialStates: Deque<IPendingState>;\n private readonly previousClientIds = new Set<string>();\n private readonly firstStashedCSN: number = -1;\n private readonly disposeOnce = new Lazy<void>(() => {\n this.initialStates.clear();\n this.pendingStates.clear();\n });\n\n // Maintains the count of messages that are currently unacked.\n private pendingMessagesCount: number = 0;\n\n // Indicates whether we are processing a batch.\n private isProcessingBatch: boolean = false;\n\n // This stores the first message in the batch that we are processing. This is used to verify that we get\n // the correct batch metadata.\n private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;\n\n private clientId: string | undefined;\n\n private get connected(): boolean {\n return this.containerRuntime.connected;\n }\n\n /**\n * Called to check if there are any pending messages in the pending state queue.\n * @returns A boolean indicating whether there are messages or not.\n */\n public hasPendingMessages(): boolean {\n return this.pendingMessagesCount !== 0;\n }\n\n public getLocalState(): IPendingLocalState | undefined {\n if (this.hasPendingMessages()) {\n return {\n clientId: this.clientId,\n pendingStates: this.pendingStates.toArray().map(\n // delete localOpMetadata since it may not be serializable\n // and will be regenerated by applyStashedOp()\n (state) => state.type === \"message\" ? {...state, localOpMetadata: undefined } : state),\n };\n }\n }\n\n constructor(\n private readonly containerRuntime: ContainerRuntime,\n private readonly applyStashedOp: (type, content) => Promise<unknown>,\n initialState: IPendingLocalState | undefined,\n ) {\n this.initialStates = new Deque<IPendingState>(initialState?.pendingStates ?? []);\n\n if (initialState) {\n if (initialState?.clientId) {\n this.previousClientIds.add(initialState.clientId);\n }\n // get stashed op count and client sequence number of first op\n const messages = initialState.pendingStates\n .filter((state) => state.type === \"message\") as IPendingMessage[];\n this.firstStashedCSN = messages[0].clientSequenceNumber;\n }\n }\n\n public get disposed() { return this.disposeOnce.evaluated; }\n public readonly dispose = () => this.disposeOnce.value;\n\n /**\n * Called when a message is submitted locally. Adds the message and the associated details to the pending state\n * queue.\n * @param type - The container message type.\n * @param clientSequenceNumber - The clientSequenceNumber associated with the message.\n * @param content - The message content.\n * @param localOpMetadata - The local metadata associated with the message.\n */\n public onSubmitMessage(\n type: ContainerMessageType,\n clientSequenceNumber: number,\n referenceSequenceNumber: number,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined,\n ) {\n const pendingMessage: IPendingMessage = {\n type: \"message\",\n messageType: type,\n clientSequenceNumber,\n referenceSequenceNumber,\n content,\n localOpMetadata,\n opMetadata,\n };\n\n this.pendingStates.push(pendingMessage);\n\n this.pendingMessagesCount++;\n }\n\n /**\n * Called when the FlushMode is updated. Adds the FlushMode to the pending state queue.\n * @param flushMode - The flushMode that was updated.\n */\n public onFlushModeUpdated(flushMode: FlushMode) {\n if (flushMode === FlushMode.Immediate) {\n const previousState = this.pendingStates.peekBack();\n\n // We don't have to track a previous \"flush\" state because FlushMode.Immediate flushes the messages. So,\n // just tracking this FlushMode.Immediate is enough.\n if (previousState?.type === \"flush\") {\n this.pendingStates.removeBack();\n }\n\n // If no messages were sent between FlushMode.TurnBased and FlushMode.Immediate,\n // then we do not have to track both these states.\n // Remove FlushMode.TurnBased from the pending queue and return.\n if (previousState?.type === \"flushMode\" && previousState.flushMode === FlushMode.TurnBased) {\n this.pendingStates.removeBack();\n return;\n }\n }\n\n const pendingFlushMode: IPendingFlushMode = {\n type: \"flushMode\",\n flushMode,\n };\n this.pendingStates.push(pendingFlushMode);\n }\n\n /**\n * Called when flush() is called on the ContainerRuntime to manually flush messages.\n */\n public onFlush() {\n // If the FlushMode is Immediate, we should not track this flush call as it is only applicable when FlushMode\n // is TurnBased.\n if (this.containerRuntime.flushMode === FlushMode.Immediate) {\n return;\n }\n\n // If the previous state is not a message, we don't have to track this flush call as there is nothing to flush.\n const previousState = this.pendingStates.peekBack();\n if (previousState?.type !== \"message\") {\n return;\n }\n\n // Note that because of the checks above and the checks in onFlushModeUpdated(), we can be sure that a \"flush\"\n // state always has a \"message\" before and after it. So, it marks the end of a batch and the beginning of a\n // new one.\n const pendingFlush: IPendingFlush = {\n type: \"flush\",\n };\n this.pendingStates.push(pendingFlush);\n }\n\n /**\n * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n */\n public async applyStashedOpsAt(seqNum: number) {\n // apply stashed ops at sequence number\n while (!this.initialStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.initialStates.peekFront()!;\n if (nextState.type === \"message\") {\n if (nextState.referenceSequenceNumber > seqNum) {\n break; // nothing left to do at this sequence number\n } else if (nextState.referenceSequenceNumber > 0 && nextState.referenceSequenceNumber < seqNum) {\n throw new Error(\"loaded from snapshot too recent to apply stashed ops\");\n }\n\n // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it\n const localOpMetadata = await this.applyStashedOp(nextState.messageType, nextState.content);\n nextState.localOpMetadata = localOpMetadata;\n }\n\n // then we push onto pendingStates which will cause PendingStateManager to resubmit when we connect\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.pendingStates.push(this.initialStates.shift()!);\n }\n }\n\n /**\n * Processes a local message once it's ack'd by the server to verify that there was no data corruption and that\n * the batch information was preserved for batch messages. Also process remote messages that might have been\n * sent from a previous container.\n * @param message - The messsage that got ack'd and needs to be processed.\n */\n public processMessage(message: ISequencedDocumentMessage, local: boolean) {\n // Do not process chunked ops until all pieces are available.\n if (message.type === ContainerMessageType.ChunkedOp) {\n return { localAck: false, localOpMetadata: undefined };\n }\n\n if (local) {\n return { localAck: false, localOpMetadata: this.processPendingLocalMessage(message) };\n } else {\n return this.processRemoteMessage(message);\n }\n }\n\n /**\n * Listens for ACKs of stashed ops\n */\n private processRemoteMessage(message: ISequencedDocumentMessage) {\n if (!isRuntimeMessage(message)) {\n return { localAck: false, localOpMetadata: undefined };\n }\n\n // this message was a pending op that was actually sent successfully\n const isOriginalClientId = message.clientId === Array.from(this.previousClientIds)[0] &&\n message.clientSequenceNumber >= this.firstStashedCSN;\n // this message is a pending or stashed op that was resubmitted\n const isNewClientId = Array.from(this.previousClientIds).indexOf(message.clientId) > 0;\n\n // if this is an ack for a stashed op, dequeue one message.\n // we should have seen its ref seq num by now and the DDS should be ready for it to be ACKed\n if (isOriginalClientId || isNewClientId) {\n assert(this.clientId === undefined, 0x28b /* \"multiple clients connected with stashed ops\" */);\n while (!this.pendingStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.pendingStates.shift()!;\n // if it's not a message just drop it and keep looking\n if (nextState.type === \"message\") {\n this.assertOpMatch(nextState, message, isOriginalClientId);\n return { localAck: true, localOpMetadata: nextState.localOpMetadata };\n }\n }\n }\n\n if (message.type === ContainerMessageType.Rejoin && this.previousClientIds.has(message.contents?.clientId)) {\n this.previousClientIds.add(message.clientId);\n }\n\n return { localAck: false, localOpMetadata: undefined };\n }\n\n private assertOpMatch(state: IPendingMessage, message: ISequencedDocumentMessage, isOriginalClientId: boolean) {\n assert(message.type === state.messageType, 0x28c /* \"different message type\" */);\n assert(message.clientSequenceNumber === state.clientSequenceNumber || !isOriginalClientId,\n 0x28d /* \"client sequence number doesn't match\" */);\n switch(message.type) {\n case ContainerMessageType.Attach:\n assert(message.contents.id === state.content.id, 0x28e /* \"datastore ID doesn't match\" */);\n break;\n case ContainerMessageType.FluidDataStoreOp:\n assert(message.contents.address === state.content.address, 0x28f /* \"address doesn't match\" */);\n break;\n case ContainerMessageType.BlobAttach:\n // todo: assert we have blob storage, assert blob IDs match, remove blob from blob storage since it made\n // it through successfully\n break;\n case ContainerMessageType.Rejoin:\n default:\n throw new Error(`${message.type} not expected`);\n }\n }\n\n /**\n * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that\n * the batch information was preserved for batch messages.\n * @param message - The messsage that got ack'd and needs to be processed.\n */\n private processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {\n // Pre-processing part - This may be the start of a batch.\n this.maybeProcessBatchBegin(message);\n\n // Get the next state from the pending queue and verify that it is of type \"message\".\n const pendingState = this.peekNextPendingState();\n assert(pendingState.type === \"message\", 0x169 /* \"No pending message found for this remote message\" */);\n this.pendingStates.shift();\n\n // Processing part - Verify that there has been no data corruption.\n // The clientSequenceNumber of the incoming message must match that of the pending message.\n if (pendingState.clientSequenceNumber !== message.clientSequenceNumber) {\n // Close the container because this could indicate data corruption.\n const error = new DataProcessingError(\n \"unexpectedAckReceived\",\n \"unexpectedAckReceived\",\n {\n clientId: message.clientId,\n sequenceNumber: message.sequenceNumber,\n clientSequenceNumber: message.clientSequenceNumber,\n expectedClientSequenceNumber: pendingState.clientSequenceNumber,\n },\n );\n\n this.containerRuntime.closeFn(error);\n return;\n }\n\n this.pendingMessagesCount--;\n\n // Post-processing part - If we are processing a batch then this could be the last message in the batch.\n if (this.isProcessingBatch) {\n this.maybeProcessBatchEnd(message);\n }\n\n return pendingState.localOpMetadata;\n }\n\n /**\n * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {\n const pendingState = this.peekNextPendingState();\n if (pendingState.type !== \"flush\" && pendingState.type !== \"flushMode\") {\n return;\n }\n\n // If the pending state is of type \"flushMode\", it must be Manual since Automatic flush mode is processed\n // after a message is processed and not before.\n if (pendingState.type === \"flushMode\") {\n assert(pendingState.flushMode === FlushMode.TurnBased,\n 0x16a /* \"Flush mode should be manual when processing batch begin\" */);\n }\n\n // We should not already be processing a batch and there should be no pending batch begin message.\n assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,\n 0x16b /* \"The pending batch state indicates we are already processing a batch\" */);\n\n // Set the pending batch state indicating we have started processing a batch.\n this.pendingBatchBeginMessage = message;\n this.isProcessingBatch = true;\n\n // Remove this pending state from the queue as we have processed it.\n this.pendingStates.shift();\n }\n\n private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n const nextPendingState = this.peekNextPendingState();\n if (nextPendingState.type !== \"flush\" && nextPendingState.type !== \"flushMode\") {\n return;\n }\n\n // If the next pending state is of type \"flushMode\", it must be Immediate and if so, we need to remove it from\n // the queue.\n // Note that we do not remove the type \"flush\" from the queue because it indicates the end of one batch and the\n // beginning of a new one. So, it will removed when the next batch begin is processed.\n if (nextPendingState.type === \"flushMode\") {\n assert(nextPendingState.flushMode === FlushMode.Immediate,\n 0x16c /* \"Flush mode is set to TurnBased in the middle of processing a batch\" */);\n this.pendingStates.shift();\n }\n\n // There should be a pending batch begin message.\n assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* \"There is no pending batch begin message\" */);\n\n // Get the batch begin metadata from the first message in the batch.\n const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;\n\n // There could be just a single message in the batch. If so, it should not have any batch metadata. If there\n // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.\n if (this.pendingBatchBeginMessage === message) {\n assert(batchBeginMetadata === undefined,\n 0x16e /* \"Batch with single message should not have batch metadata\" */);\n } else {\n // Get the batch metadata from the last message in the batch.\n const batchEndMetadata = message.metadata?.batch;\n assert(batchBeginMetadata === true, 0x16f /* \"Did not receive batch begin metadata\" */);\n assert(batchEndMetadata === false, 0x170 /* \"Did not receive batch end metadata\" */);\n }\n\n // Clear the pending batch state now that we have processed the entire batch.\n this.pendingBatchBeginMessage = undefined;\n this.isProcessingBatch = false;\n }\n\n /**\n * Returns the next pending state from the pending state queue.\n */\n private peekNextPendingState(): IPendingState {\n const nextPendingState = this.pendingStates.peekFront();\n assert(!!nextPendingState, 0x171 /* \"No pending state found for the remote message\" */);\n return nextPendingState;\n }\n\n /**\n * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending\n * states in its queue. This includes setting the FlushMode and triggering resubmission of unacked ops.\n */\n public replayPendingStates() {\n assert(this.connected, 0x172 /* \"The connection state is not consistent with the runtime\" */);\n\n // This assert suggests we are about to send same ops twice, which will result in data loss.\n assert(this.clientId !== this.containerRuntime.clientId,\n 0x173 /* \"replayPendingStates called twice for same clientId!\" */);\n this.clientId = this.containerRuntime.clientId;\n\n assert(this.initialStates.isEmpty(), 0x174 /* \"initial states should be empty before replaying pending\" */);\n\n let pendingStatesCount = this.pendingStates.length;\n if (pendingStatesCount === 0) {\n return;\n }\n\n // Reset the pending message count because all these messages will be removed from the queue.\n this.pendingMessagesCount = 0;\n\n // Save the current FlushMode so that we can revert it back after replaying the states.\n const savedFlushMode = this.containerRuntime.flushMode;\n\n // Process exactly `pendingStatesCount` items in the queue as it represents the number of states that were\n // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue\n // which must not be replayed.\n while (pendingStatesCount > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const pendingState = this.pendingStates.shift()!;\n switch (pendingState.type) {\n case \"message\":\n {\n this.containerRuntime.reSubmitFn(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata,\n pendingState.opMetadata);\n }\n break;\n case \"flushMode\":\n {\n this.containerRuntime.setFlushMode(pendingState.flushMode);\n }\n break;\n case \"flush\":\n {\n this.containerRuntime.flush();\n }\n break;\n default:\n break;\n }\n pendingStatesCount--;\n }\n\n // Revert the FlushMode.\n this.containerRuntime.setFlushMode(savedFlushMode);\n }\n}\n"]}
|
package/lib/packageVersion.d.ts
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export declare const pkgName = "@fluidframework/container-runtime";
|
|
8
|
-
export declare const pkgVersion = "0.51.
|
|
8
|
+
export declare const pkgVersion = "0.51.3";
|
|
9
9
|
//# sourceMappingURL=packageVersion.d.ts.map
|
package/lib/packageVersion.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,mCAAmC,CAAC;AAC3D,MAAM,CAAC,MAAM,UAAU,GAAG,QAAQ,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/container-runtime\";\nexport const pkgVersion = \"0.51.
|
|
1
|
+
{"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../src/packageVersion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,OAAO,GAAG,mCAAmC,CAAC;AAC3D,MAAM,CAAC,MAAM,UAAU,GAAG,QAAQ,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n *\n * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY\n */\n\nexport const pkgName = \"@fluidframework/container-runtime\";\nexport const pkgVersion = \"0.51.3\";\n"]}
|
|
@@ -61,7 +61,6 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
61
61
|
private readonly initialStates;
|
|
62
62
|
private readonly previousClientIds;
|
|
63
63
|
private readonly firstStashedCSN;
|
|
64
|
-
private stashedCount;
|
|
65
64
|
private readonly disposeOnce;
|
|
66
65
|
private pendingMessagesCount;
|
|
67
66
|
private isProcessingBatch;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingStateManager.d.ts","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAC;AAGjE,OAAO,EACH,yBAAyB,EAC5B,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAoB,MAAM,oBAAoB,CAAC;AAE9F;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC5B,IAAI,EAAE,SAAS,CAAC;IAChB,WAAW,EAAE,oBAAoB,CAAC;IAClC,oBAAoB,EAAE,MAAM,CAAC;IAC7B,uBAAuB,EAAE,MAAM,CAAC;IAChC,OAAO,EAAE,GAAG,CAAC;IACb,eAAe,EAAE,OAAO,CAAC;IACzB,UAAU,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AE,OAAO,EACxB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS;IAiBnD;;;OAGG;IACI,kBAAkB,CAAC,SAAS,EAAE,SAAS;IA0B9C;;OAEG;IACI,OAAO;IAsBd;;OAEG;IACU,iBAAiB,CAAC,MAAM,EAAE,MAAM;IAuB7C;;;;;OAKG;IACI,cAAc,CAAC,OAAO,EAAE,yBAAyB,EAAE,KAAK,EAAE,OAAO;;;;IAaxE;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAiC5B,OAAO,CAAC,aAAa;IAqBrB;;;;OAIG;IACH,OAAO,CAAC,0BAA0B;IAsClC;;;OAGG;IACH,OAAO,CAAC,sBAAsB;IAyB9B,OAAO,CAAC,oBAAoB;IAuC5B;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAM5B;;;OAGG;IACI,mBAAmB;CAwD7B"}
|
|
@@ -24,7 +24,6 @@ export class PendingStateManager {
|
|
|
24
24
|
this.pendingStates = new Deque();
|
|
25
25
|
this.previousClientIds = new Set();
|
|
26
26
|
this.firstStashedCSN = -1;
|
|
27
|
-
this.stashedCount = 0;
|
|
28
27
|
this.disposeOnce = new Lazy(() => {
|
|
29
28
|
this.initialStates.clear();
|
|
30
29
|
this.pendingStates.clear();
|
|
@@ -42,7 +41,6 @@ export class PendingStateManager {
|
|
|
42
41
|
// get stashed op count and client sequence number of first op
|
|
43
42
|
const messages = initialState.pendingStates
|
|
44
43
|
.filter((state) => state.type === "message");
|
|
45
|
-
this.stashedCount = messages.length;
|
|
46
44
|
this.firstStashedCSN = messages[0].clientSequenceNumber;
|
|
47
45
|
}
|
|
48
46
|
}
|
|
@@ -202,7 +200,6 @@ export class PendingStateManager {
|
|
|
202
200
|
// if it's not a message just drop it and keep looking
|
|
203
201
|
if (nextState.type === "message") {
|
|
204
202
|
this.assertOpMatch(nextState, message, isOriginalClientId);
|
|
205
|
-
--this.stashedCount;
|
|
206
203
|
return { localAck: true, localOpMetadata: nextState.localOpMetadata };
|
|
207
204
|
}
|
|
208
205
|
}
|
|
@@ -334,45 +331,12 @@ export class PendingStateManager {
|
|
|
334
331
|
assert(this.connected, 0x172 /* "The connection state is not consistent with the runtime" */);
|
|
335
332
|
// This assert suggests we are about to send same ops twice, which will result in data loss.
|
|
336
333
|
assert(this.clientId !== this.containerRuntime.clientId, 0x173 /* "replayPendingStates called twice for same clientId!" */);
|
|
337
|
-
const prevClientId = this.clientId;
|
|
338
334
|
this.clientId = this.containerRuntime.clientId;
|
|
339
335
|
assert(this.initialStates.isEmpty(), 0x174 /* "initial states should be empty before replaying pending" */);
|
|
340
336
|
let pendingStatesCount = this.pendingStates.length;
|
|
341
337
|
if (pendingStatesCount === 0) {
|
|
342
338
|
return;
|
|
343
339
|
}
|
|
344
|
-
if (!prevClientId && this.stashedCount > 0) {
|
|
345
|
-
// this is first connect, verify we are about to "resubmit" only stashed ops
|
|
346
|
-
assert(this.pendingStates.toArray().filter((s) => s.type === "message").length === this.stashedCount, 0x290 /* "unexpected message queued before first connect" */);
|
|
347
|
-
Array.from(this.previousClientIds).map((id) => assert(this.containerRuntime.getQuorum().getMember(id) === undefined, 0x291 /* "client with stashed ops already connected" */));
|
|
348
|
-
// send rejoin op with stashed client ID if we have it
|
|
349
|
-
if (this.previousClientIds.size > 0) {
|
|
350
|
-
const clientId = Array.from(this.previousClientIds)[0];
|
|
351
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
352
|
-
this.pendingStates.unshift({
|
|
353
|
-
type: "message",
|
|
354
|
-
messageType: ContainerMessageType.Rejoin,
|
|
355
|
-
content: { clientId },
|
|
356
|
-
});
|
|
357
|
-
++pendingStatesCount;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
if (prevClientId) {
|
|
361
|
-
// add a rejoin op so future clients provided with our stashed pending ops can recognize them
|
|
362
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
363
|
-
const firstState = this.pendingStates.peekFront();
|
|
364
|
-
if (firstState.type !== "message" || firstState.messageType !== ContainerMessageType.Rejoin) {
|
|
365
|
-
// if there is already a rejoin op in the queue, just resubmit same op under new client ID
|
|
366
|
-
// otherwise, add one to the queue
|
|
367
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
368
|
-
this.pendingStates.unshift({
|
|
369
|
-
type: "message",
|
|
370
|
-
messageType: ContainerMessageType.Rejoin,
|
|
371
|
-
content: { clientId: prevClientId },
|
|
372
|
-
});
|
|
373
|
-
++pendingStatesCount;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
340
|
// Reset the pending message count because all these messages will be removed from the queue.
|
|
377
341
|
this.pendingMessagesCount = 0;
|
|
378
342
|
// Save the current FlushMode so that we can revert it back after replaying the states.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAItE,OAAO,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAC;AAChE,OAAO,KAAK,MAAM,oBAAoB,CAAC;AACvC,OAAO,EAAoB,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AA8C9F;;;;;;;;GAQG;AACH,MAAM,OAAO,mBAAmB;IA+C5B,YACqB,gBAAkC,EAClC,cAAmD,EACpE,YAA4C;;QAF3B,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,mBAAc,GAAd,cAAc,CAAqC;QAhDvD,kBAAa,GAAG,IAAI,KAAK,EAAiB,CAAC;QAE3C,sBAAiB,GAAG,IAAI,GAAG,EAAU,CAAC;QACtC,oBAAe,GAAW,CAAC,CAAC,CAAC;QACtC,iBAAY,GAAG,CAAC,CAAC;QACR,gBAAW,GAAG,IAAI,IAAI,CAAO,GAAG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AAK,OAAO;oBACR;wBACI,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;qBACjC;oBACD,MAAM;gBACV;oBACI,MAAM;aACb;YACD,kBAAkB,EAAE,CAAC;SACxB;QAED,wBAAwB;QACxB,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IACvD,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IDisposable } from \"@fluidframework/common-definitions\";\nimport { assert, Lazy } from \"@fluidframework/common-utils\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\nimport { FlushMode } from \"@fluidframework/runtime-definitions\";\nimport Deque from \"double-ended-queue\";\nimport { ContainerRuntime, ContainerMessageType, isRuntimeMessage } from \"./containerRuntime\";\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 type: \"message\";\n messageType: ContainerMessageType;\n clientSequenceNumber: number;\n referenceSequenceNumber: number;\n content: any;\n localOpMetadata: unknown;\n opMetadata: Record<string, unknown> | undefined;\n}\n\n/**\n * This represents a FlushMode update and is added to the pending queue when `setFlushMode` is called on the\n * ContainerRuntime and the FlushMode changes.\n */\nexport interface IPendingFlushMode {\n type: \"flushMode\";\n flushMode: FlushMode;\n}\n\n/**\n * This represents a manual flush and is added to the pending queue when `flush` is called on the ContainerRuntime to\n * flush any pending messages. This is applicable only when the FlushMode is Manual.\n */\nexport interface IPendingFlush {\n type: \"flush\";\n}\n\nexport type IPendingState = IPendingMessage | IPendingFlushMode | IPendingFlush;\n\nexport interface IPendingLocalState {\n /**\n * client ID we most recently connected with, or undefined if we never connected\n */\n clientId?: string;\n /**\n * list of pending states, including ops and batch information\n */\n pendingStates: IPendingState[];\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 setting the FlushMode, 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 private readonly pendingStates = new Deque<IPendingState>();\n private readonly initialStates: Deque<IPendingState>;\n private readonly previousClientIds = new Set<string>();\n private readonly firstStashedCSN: number = -1;\n private stashedCount = 0;\n private readonly disposeOnce = new Lazy<void>(() => {\n this.initialStates.clear();\n this.pendingStates.clear();\n });\n\n // Maintains the count of messages that are currently unacked.\n private pendingMessagesCount: number = 0;\n\n // Indicates whether we are processing a batch.\n private isProcessingBatch: boolean = false;\n\n // This stores the first message in the batch that we are processing. This is used to verify that we get\n // the correct batch metadata.\n private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;\n\n private clientId: string | undefined;\n\n private get connected(): boolean {\n return this.containerRuntime.connected;\n }\n\n /**\n * Called to check if there are any pending messages in the pending state queue.\n * @returns A boolean indicating whether there are messages or not.\n */\n public hasPendingMessages(): boolean {\n return this.pendingMessagesCount !== 0;\n }\n\n public getLocalState(): IPendingLocalState | undefined {\n if (this.hasPendingMessages()) {\n return {\n clientId: this.clientId,\n pendingStates: this.pendingStates.toArray().map(\n // delete localOpMetadata since it may not be serializable\n // and will be regenerated by applyStashedOp()\n (state) => state.type === \"message\" ? {...state, localOpMetadata: undefined } : state),\n };\n }\n }\n\n constructor(\n private readonly containerRuntime: ContainerRuntime,\n private readonly applyStashedOp: (type, content) => Promise<unknown>,\n initialState: IPendingLocalState | undefined,\n ) {\n this.initialStates = new Deque<IPendingState>(initialState?.pendingStates ?? []);\n\n if (initialState) {\n if (initialState?.clientId) {\n this.previousClientIds.add(initialState.clientId);\n }\n // get stashed op count and client sequence number of first op\n const messages = initialState.pendingStates\n .filter((state) => state.type === \"message\") as IPendingMessage[];\n this.stashedCount = messages.length;\n this.firstStashedCSN = messages[0].clientSequenceNumber;\n }\n }\n\n public get disposed() { return this.disposeOnce.evaluated; }\n public readonly dispose = () => this.disposeOnce.value;\n\n /**\n * Called when a message is submitted locally. Adds the message and the associated details to the pending state\n * queue.\n * @param type - The container message type.\n * @param clientSequenceNumber - The clientSequenceNumber associated with the message.\n * @param content - The message content.\n * @param localOpMetadata - The local metadata associated with the message.\n */\n public onSubmitMessage(\n type: ContainerMessageType,\n clientSequenceNumber: number,\n referenceSequenceNumber: number,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined,\n ) {\n const pendingMessage: IPendingMessage = {\n type: \"message\",\n messageType: type,\n clientSequenceNumber,\n referenceSequenceNumber,\n content,\n localOpMetadata,\n opMetadata,\n };\n\n this.pendingStates.push(pendingMessage);\n\n this.pendingMessagesCount++;\n }\n\n /**\n * Called when the FlushMode is updated. Adds the FlushMode to the pending state queue.\n * @param flushMode - The flushMode that was updated.\n */\n public onFlushModeUpdated(flushMode: FlushMode) {\n if (flushMode === FlushMode.Immediate) {\n const previousState = this.pendingStates.peekBack();\n\n // We don't have to track a previous \"flush\" state because FlushMode.Immediate flushes the messages. So,\n // just tracking this FlushMode.Immediate is enough.\n if (previousState?.type === \"flush\") {\n this.pendingStates.removeBack();\n }\n\n // If no messages were sent between FlushMode.TurnBased and FlushMode.Immediate,\n // then we do not have to track both these states.\n // Remove FlushMode.TurnBased from the pending queue and return.\n if (previousState?.type === \"flushMode\" && previousState.flushMode === FlushMode.TurnBased) {\n this.pendingStates.removeBack();\n return;\n }\n }\n\n const pendingFlushMode: IPendingFlushMode = {\n type: \"flushMode\",\n flushMode,\n };\n this.pendingStates.push(pendingFlushMode);\n }\n\n /**\n * Called when flush() is called on the ContainerRuntime to manually flush messages.\n */\n public onFlush() {\n // If the FlushMode is Immediate, we should not track this flush call as it is only applicable when FlushMode\n // is TurnBased.\n if (this.containerRuntime.flushMode === FlushMode.Immediate) {\n return;\n }\n\n // If the previous state is not a message, we don't have to track this flush call as there is nothing to flush.\n const previousState = this.pendingStates.peekBack();\n if (previousState?.type !== \"message\") {\n return;\n }\n\n // Note that because of the checks above and the checks in onFlushModeUpdated(), we can be sure that a \"flush\"\n // state always has a \"message\" before and after it. So, it marks the end of a batch and the beginning of a\n // new one.\n const pendingFlush: IPendingFlush = {\n type: \"flush\",\n };\n this.pendingStates.push(pendingFlush);\n }\n\n /**\n * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n */\n public async applyStashedOpsAt(seqNum: number) {\n // apply stashed ops at sequence number\n while (!this.initialStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.initialStates.peekFront()!;\n if (nextState.type === \"message\") {\n if (nextState.referenceSequenceNumber > seqNum) {\n break; // nothing left to do at this sequence number\n } else if (nextState.referenceSequenceNumber > 0 && nextState.referenceSequenceNumber < seqNum) {\n throw new Error(\"loaded from snapshot too recent to apply stashed ops\");\n }\n\n // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it\n const localOpMetadata = await this.applyStashedOp(nextState.messageType, nextState.content);\n nextState.localOpMetadata = localOpMetadata;\n }\n\n // then we push onto pendingStates which will cause PendingStateManager to resubmit when we connect\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.pendingStates.push(this.initialStates.shift()!);\n }\n }\n\n /**\n * Processes a local message once it's ack'd by the server to verify that there was no data corruption and that\n * the batch information was preserved for batch messages. Also process remote messages that might have been\n * sent from a previous container.\n * @param message - The messsage that got ack'd and needs to be processed.\n */\n public processMessage(message: ISequencedDocumentMessage, local: boolean) {\n // Do not process chunked ops until all pieces are available.\n if (message.type === ContainerMessageType.ChunkedOp) {\n return { localAck: false, localOpMetadata: undefined };\n }\n\n if (local) {\n return { localAck: false, localOpMetadata: this.processPendingLocalMessage(message) };\n } else {\n return this.processRemoteMessage(message);\n }\n }\n\n /**\n * Listens for ACKs of stashed ops\n */\n private processRemoteMessage(message: ISequencedDocumentMessage) {\n if (!isRuntimeMessage(message)) {\n return { localAck: false, localOpMetadata: undefined };\n }\n\n // this message was a pending op that was actually sent successfully\n const isOriginalClientId = message.clientId === Array.from(this.previousClientIds)[0] &&\n message.clientSequenceNumber >= this.firstStashedCSN;\n // this message is a pending or stashed op that was resubmitted\n const isNewClientId = Array.from(this.previousClientIds).indexOf(message.clientId) > 0;\n\n // if this is an ack for a stashed op, dequeue one message.\n // we should have seen its ref seq num by now and the DDS should be ready for it to be ACKed\n if (isOriginalClientId || isNewClientId) {\n assert(this.clientId === undefined, 0x28b /* \"multiple clients connected with stashed ops\" */);\n while (!this.pendingStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.pendingStates.shift()!;\n // if it's not a message just drop it and keep looking\n if (nextState.type === \"message\") {\n this.assertOpMatch(nextState, message, isOriginalClientId);\n --this.stashedCount;\n return { localAck: true, localOpMetadata: nextState.localOpMetadata };\n }\n }\n }\n\n if (message.type === ContainerMessageType.Rejoin && this.previousClientIds.has(message.contents?.clientId)) {\n this.previousClientIds.add(message.clientId);\n }\n\n return { localAck: false, localOpMetadata: undefined };\n }\n\n private assertOpMatch(state: IPendingMessage, message: ISequencedDocumentMessage, isOriginalClientId: boolean) {\n assert(message.type === state.messageType, 0x28c /* \"different message type\" */);\n assert(message.clientSequenceNumber === state.clientSequenceNumber || !isOriginalClientId,\n 0x28d /* \"client sequence number doesn't match\" */);\n switch(message.type) {\n case ContainerMessageType.Attach:\n assert(message.contents.id === state.content.id, 0x28e /* \"datastore ID doesn't match\" */);\n break;\n case ContainerMessageType.FluidDataStoreOp:\n assert(message.contents.address === state.content.address, 0x28f /* \"address doesn't match\" */);\n break;\n case ContainerMessageType.BlobAttach:\n // todo: assert we have blob storage, assert blob IDs match, remove blob from blob storage since it made\n // it through successfully\n break;\n case ContainerMessageType.Rejoin:\n default:\n throw new Error(`${message.type} not expected`);\n }\n }\n\n /**\n * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that\n * the batch information was preserved for batch messages.\n * @param message - The messsage that got ack'd and needs to be processed.\n */\n private processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {\n // Pre-processing part - This may be the start of a batch.\n this.maybeProcessBatchBegin(message);\n\n // Get the next state from the pending queue and verify that it is of type \"message\".\n const pendingState = this.peekNextPendingState();\n assert(pendingState.type === \"message\", 0x169 /* \"No pending message found for this remote message\" */);\n this.pendingStates.shift();\n\n // Processing part - Verify that there has been no data corruption.\n // The clientSequenceNumber of the incoming message must match that of the pending message.\n if (pendingState.clientSequenceNumber !== message.clientSequenceNumber) {\n // Close the container because this could indicate data corruption.\n const error = new DataProcessingError(\n \"unexpectedAckReceived\",\n \"unexpectedAckReceived\",\n {\n clientId: message.clientId,\n sequenceNumber: message.sequenceNumber,\n clientSequenceNumber: message.clientSequenceNumber,\n expectedClientSequenceNumber: pendingState.clientSequenceNumber,\n },\n );\n\n this.containerRuntime.closeFn(error);\n return;\n }\n\n this.pendingMessagesCount--;\n\n // Post-processing part - If we are processing a batch then this could be the last message in the batch.\n if (this.isProcessingBatch) {\n this.maybeProcessBatchEnd(message);\n }\n\n return pendingState.localOpMetadata;\n }\n\n /**\n * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {\n const pendingState = this.peekNextPendingState();\n if (pendingState.type !== \"flush\" && pendingState.type !== \"flushMode\") {\n return;\n }\n\n // If the pending state is of type \"flushMode\", it must be Manual since Automatic flush mode is processed\n // after a message is processed and not before.\n if (pendingState.type === \"flushMode\") {\n assert(pendingState.flushMode === FlushMode.TurnBased,\n 0x16a /* \"Flush mode should be manual when processing batch begin\" */);\n }\n\n // We should not already be processing a batch and there should be no pending batch begin message.\n assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,\n 0x16b /* \"The pending batch state indicates we are already processing a batch\" */);\n\n // Set the pending batch state indicating we have started processing a batch.\n this.pendingBatchBeginMessage = message;\n this.isProcessingBatch = true;\n\n // Remove this pending state from the queue as we have processed it.\n this.pendingStates.shift();\n }\n\n private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n const nextPendingState = this.peekNextPendingState();\n if (nextPendingState.type !== \"flush\" && nextPendingState.type !== \"flushMode\") {\n return;\n }\n\n // If the next pending state is of type \"flushMode\", it must be Immediate and if so, we need to remove it from\n // the queue.\n // Note that we do not remove the type \"flush\" from the queue because it indicates the end of one batch and the\n // beginning of a new one. So, it will removed when the next batch begin is processed.\n if (nextPendingState.type === \"flushMode\") {\n assert(nextPendingState.flushMode === FlushMode.Immediate,\n 0x16c /* \"Flush mode is set to TurnBased in the middle of processing a batch\" */);\n this.pendingStates.shift();\n }\n\n // There should be a pending batch begin message.\n assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* \"There is no pending batch begin message\" */);\n\n // Get the batch begin metadata from the first message in the batch.\n const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;\n\n // There could be just a single message in the batch. If so, it should not have any batch metadata. If there\n // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.\n if (this.pendingBatchBeginMessage === message) {\n assert(batchBeginMetadata === undefined,\n 0x16e /* \"Batch with single message should not have batch metadata\" */);\n } else {\n // Get the batch metadata from the last message in the batch.\n const batchEndMetadata = message.metadata?.batch;\n assert(batchBeginMetadata === true, 0x16f /* \"Did not receive batch begin metadata\" */);\n assert(batchEndMetadata === false, 0x170 /* \"Did not receive batch end metadata\" */);\n }\n\n // Clear the pending batch state now that we have processed the entire batch.\n this.pendingBatchBeginMessage = undefined;\n this.isProcessingBatch = false;\n }\n\n /**\n * Returns the next pending state from the pending state queue.\n */\n private peekNextPendingState(): IPendingState {\n const nextPendingState = this.pendingStates.peekFront();\n assert(!!nextPendingState, 0x171 /* \"No pending state found for the remote message\" */);\n return nextPendingState;\n }\n\n /**\n * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending\n * states in its queue. This includes setting the FlushMode and triggering resubmission of unacked ops.\n */\n public replayPendingStates() {\n assert(this.connected, 0x172 /* \"The connection state is not consistent with the runtime\" */);\n\n // This assert suggests we are about to send same ops twice, which will result in data loss.\n assert(this.clientId !== this.containerRuntime.clientId,\n 0x173 /* \"replayPendingStates called twice for same clientId!\" */);\n const prevClientId = this.clientId;\n this.clientId = this.containerRuntime.clientId;\n\n assert(this.initialStates.isEmpty(), 0x174 /* \"initial states should be empty before replaying pending\" */);\n\n let pendingStatesCount = this.pendingStates.length;\n if (pendingStatesCount === 0) {\n return;\n }\n\n if (!prevClientId && this.stashedCount > 0) {\n // this is first connect, verify we are about to \"resubmit\" only stashed ops\n assert(this.pendingStates.toArray().filter((s) => s.type === \"message\").length === this.stashedCount,\n 0x290 /* \"unexpected message queued before first connect\" */);\n\n Array.from(this.previousClientIds).map((id) =>\n assert(this.containerRuntime.getQuorum().getMember(id) === undefined,\n 0x291 /* \"client with stashed ops already connected\" */));\n\n // send rejoin op with stashed client ID if we have it\n if (this.previousClientIds.size > 0) {\n const clientId = Array.from(this.previousClientIds)[0];\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n this.pendingStates.unshift({\n type: \"message\",\n messageType: ContainerMessageType.Rejoin,\n content: { clientId },\n } as IPendingMessage);\n ++pendingStatesCount;\n }\n }\n\n if (prevClientId) {\n // add a rejoin op so future clients provided with our stashed pending ops can recognize them\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const firstState = this.pendingStates.peekFront()!;\n if (firstState.type !== \"message\" || firstState.messageType !== ContainerMessageType.Rejoin) {\n // if there is already a rejoin op in the queue, just resubmit same op under new client ID\n // otherwise, add one to the queue\n // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n this.pendingStates.unshift({\n type: \"message\",\n messageType: ContainerMessageType.Rejoin,\n content: { clientId: prevClientId },\n } as IPendingMessage);\n ++pendingStatesCount;\n }\n }\n\n // Reset the pending message count because all these messages will be removed from the queue.\n this.pendingMessagesCount = 0;\n\n // Save the current FlushMode so that we can revert it back after replaying the states.\n const savedFlushMode = this.containerRuntime.flushMode;\n\n // Process exactly `pendingStatesCount` items in the queue as it represents the number of states that were\n // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue\n // which must not be replayed.\n while (pendingStatesCount > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const pendingState = this.pendingStates.shift()!;\n switch (pendingState.type) {\n case \"message\":\n {\n this.containerRuntime.reSubmitFn(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata,\n pendingState.opMetadata);\n }\n break;\n case \"flushMode\":\n {\n this.containerRuntime.setFlushMode(pendingState.flushMode);\n }\n break;\n case \"flush\":\n {\n this.containerRuntime.flush();\n }\n break;\n default:\n break;\n }\n pendingStatesCount--;\n }\n\n // Revert the FlushMode.\n this.containerRuntime.setFlushMode(savedFlushMode);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAItE,OAAO,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAC;AAChE,OAAO,KAAK,MAAM,oBAAoB,CAAC;AACvC,OAAO,EAAoB,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AA8C9F;;;;;;;;GAQG;AACH,MAAM,OAAO,mBAAmB;IA8C5B,YACqB,gBAAkC,EAClC,cAAmD,EACpE,YAA4C;;QAF3B,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,mBAAc,GAAd,cAAc,CAAqC;QA/CvD,kBAAa,GAAG,IAAI,KAAK,EAAiB,CAAC;QAE3C,sBAAiB,GAAG,IAAI,GAAG,EAAU,CAAC;QACtC,oBAAe,GAAW,CAAC,CAAC,CAAC;QAC7B,gBAAW,GAAG,IAAI,IAAI,C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AAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC;QAE/C,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,EAAE,KAAK,CAAC,+DAA+D,CAAC,CAAC;QAE5G,IAAI,kBAAkB,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;QACnD,IAAI,kBAAkB,KAAK,CAAC,EAAE;YAC1B,OAAO;SACV;QAED,6FAA6F;QAC7F,IAAI,CAAC,oBAAoB,GAAG,CAAC,CAAC;QAE9B,uFAAuF;QACvF,MAAM,cAAc,GAAG,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC;QAEvD,0GAA0G;QAC1G,0GAA0G;QAC1G,8BAA8B;QAC9B,OAAO,kBAAkB,GAAG,CAAC,EAAE;YAC3B,oEAAoE;YACpE,MAAM,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC,KAAK,EAAG,CAAC;YACjD,QAAQ,YAAY,CAAC,IAAI,EAAE;gBACvB,KAAK,SAAS;oBACV;wBACI,IAAI,CAAC,gBAAgB,CAAC,UAAU,CAC5B,YAAY,CAAC,WAAW,EACxB,YAAY,CAAC,OAAO,EACpB,YAAY,CAAC,eAAe,EAC5B,YAAY,CAAC,UAAU,CAAC,CAAC;qBAChC;oBACD,MAAM;gBACV,KAAK,WAAW;oBACZ;wBACI,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;qBAC9D;oBACD,MAAM;gBACV,KAAK,OAAO;oBACR;wBACI,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;qBACjC;oBACD,MAAM;gBACV;oBACI,MAAM;aACb;YACD,kBAAkB,EAAE,CAAC;SACxB;QAED,wBAAwB;QACxB,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IACvD,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IDisposable } from \"@fluidframework/common-definitions\";\nimport { assert, Lazy } from \"@fluidframework/common-utils\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\nimport { FlushMode } from \"@fluidframework/runtime-definitions\";\nimport Deque from \"double-ended-queue\";\nimport { ContainerRuntime, ContainerMessageType, isRuntimeMessage } from \"./containerRuntime\";\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 type: \"message\";\n messageType: ContainerMessageType;\n clientSequenceNumber: number;\n referenceSequenceNumber: number;\n content: any;\n localOpMetadata: unknown;\n opMetadata: Record<string, unknown> | undefined;\n}\n\n/**\n * This represents a FlushMode update and is added to the pending queue when `setFlushMode` is called on the\n * ContainerRuntime and the FlushMode changes.\n */\nexport interface IPendingFlushMode {\n type: \"flushMode\";\n flushMode: FlushMode;\n}\n\n/**\n * This represents a manual flush and is added to the pending queue when `flush` is called on the ContainerRuntime to\n * flush any pending messages. This is applicable only when the FlushMode is Manual.\n */\nexport interface IPendingFlush {\n type: \"flush\";\n}\n\nexport type IPendingState = IPendingMessage | IPendingFlushMode | IPendingFlush;\n\nexport interface IPendingLocalState {\n /**\n * client ID we most recently connected with, or undefined if we never connected\n */\n clientId?: string;\n /**\n * list of pending states, including ops and batch information\n */\n pendingStates: IPendingState[];\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 setting the FlushMode, 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 private readonly pendingStates = new Deque<IPendingState>();\n private readonly initialStates: Deque<IPendingState>;\n private readonly previousClientIds = new Set<string>();\n private readonly firstStashedCSN: number = -1;\n private readonly disposeOnce = new Lazy<void>(() => {\n this.initialStates.clear();\n this.pendingStates.clear();\n });\n\n // Maintains the count of messages that are currently unacked.\n private pendingMessagesCount: number = 0;\n\n // Indicates whether we are processing a batch.\n private isProcessingBatch: boolean = false;\n\n // This stores the first message in the batch that we are processing. This is used to verify that we get\n // the correct batch metadata.\n private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;\n\n private clientId: string | undefined;\n\n private get connected(): boolean {\n return this.containerRuntime.connected;\n }\n\n /**\n * Called to check if there are any pending messages in the pending state queue.\n * @returns A boolean indicating whether there are messages or not.\n */\n public hasPendingMessages(): boolean {\n return this.pendingMessagesCount !== 0;\n }\n\n public getLocalState(): IPendingLocalState | undefined {\n if (this.hasPendingMessages()) {\n return {\n clientId: this.clientId,\n pendingStates: this.pendingStates.toArray().map(\n // delete localOpMetadata since it may not be serializable\n // and will be regenerated by applyStashedOp()\n (state) => state.type === \"message\" ? {...state, localOpMetadata: undefined } : state),\n };\n }\n }\n\n constructor(\n private readonly containerRuntime: ContainerRuntime,\n private readonly applyStashedOp: (type, content) => Promise<unknown>,\n initialState: IPendingLocalState | undefined,\n ) {\n this.initialStates = new Deque<IPendingState>(initialState?.pendingStates ?? []);\n\n if (initialState) {\n if (initialState?.clientId) {\n this.previousClientIds.add(initialState.clientId);\n }\n // get stashed op count and client sequence number of first op\n const messages = initialState.pendingStates\n .filter((state) => state.type === \"message\") as IPendingMessage[];\n this.firstStashedCSN = messages[0].clientSequenceNumber;\n }\n }\n\n public get disposed() { return this.disposeOnce.evaluated; }\n public readonly dispose = () => this.disposeOnce.value;\n\n /**\n * Called when a message is submitted locally. Adds the message and the associated details to the pending state\n * queue.\n * @param type - The container message type.\n * @param clientSequenceNumber - The clientSequenceNumber associated with the message.\n * @param content - The message content.\n * @param localOpMetadata - The local metadata associated with the message.\n */\n public onSubmitMessage(\n type: ContainerMessageType,\n clientSequenceNumber: number,\n referenceSequenceNumber: number,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined,\n ) {\n const pendingMessage: IPendingMessage = {\n type: \"message\",\n messageType: type,\n clientSequenceNumber,\n referenceSequenceNumber,\n content,\n localOpMetadata,\n opMetadata,\n };\n\n this.pendingStates.push(pendingMessage);\n\n this.pendingMessagesCount++;\n }\n\n /**\n * Called when the FlushMode is updated. Adds the FlushMode to the pending state queue.\n * @param flushMode - The flushMode that was updated.\n */\n public onFlushModeUpdated(flushMode: FlushMode) {\n if (flushMode === FlushMode.Immediate) {\n const previousState = this.pendingStates.peekBack();\n\n // We don't have to track a previous \"flush\" state because FlushMode.Immediate flushes the messages. So,\n // just tracking this FlushMode.Immediate is enough.\n if (previousState?.type === \"flush\") {\n this.pendingStates.removeBack();\n }\n\n // If no messages were sent between FlushMode.TurnBased and FlushMode.Immediate,\n // then we do not have to track both these states.\n // Remove FlushMode.TurnBased from the pending queue and return.\n if (previousState?.type === \"flushMode\" && previousState.flushMode === FlushMode.TurnBased) {\n this.pendingStates.removeBack();\n return;\n }\n }\n\n const pendingFlushMode: IPendingFlushMode = {\n type: \"flushMode\",\n flushMode,\n };\n this.pendingStates.push(pendingFlushMode);\n }\n\n /**\n * Called when flush() is called on the ContainerRuntime to manually flush messages.\n */\n public onFlush() {\n // If the FlushMode is Immediate, we should not track this flush call as it is only applicable when FlushMode\n // is TurnBased.\n if (this.containerRuntime.flushMode === FlushMode.Immediate) {\n return;\n }\n\n // If the previous state is not a message, we don't have to track this flush call as there is nothing to flush.\n const previousState = this.pendingStates.peekBack();\n if (previousState?.type !== \"message\") {\n return;\n }\n\n // Note that because of the checks above and the checks in onFlushModeUpdated(), we can be sure that a \"flush\"\n // state always has a \"message\" before and after it. So, it marks the end of a batch and the beginning of a\n // new one.\n const pendingFlush: IPendingFlush = {\n type: \"flush\",\n };\n this.pendingStates.push(pendingFlush);\n }\n\n /**\n * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n */\n public async applyStashedOpsAt(seqNum: number) {\n // apply stashed ops at sequence number\n while (!this.initialStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.initialStates.peekFront()!;\n if (nextState.type === \"message\") {\n if (nextState.referenceSequenceNumber > seqNum) {\n break; // nothing left to do at this sequence number\n } else if (nextState.referenceSequenceNumber > 0 && nextState.referenceSequenceNumber < seqNum) {\n throw new Error(\"loaded from snapshot too recent to apply stashed ops\");\n }\n\n // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it\n const localOpMetadata = await this.applyStashedOp(nextState.messageType, nextState.content);\n nextState.localOpMetadata = localOpMetadata;\n }\n\n // then we push onto pendingStates which will cause PendingStateManager to resubmit when we connect\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n this.pendingStates.push(this.initialStates.shift()!);\n }\n }\n\n /**\n * Processes a local message once it's ack'd by the server to verify that there was no data corruption and that\n * the batch information was preserved for batch messages. Also process remote messages that might have been\n * sent from a previous container.\n * @param message - The messsage that got ack'd and needs to be processed.\n */\n public processMessage(message: ISequencedDocumentMessage, local: boolean) {\n // Do not process chunked ops until all pieces are available.\n if (message.type === ContainerMessageType.ChunkedOp) {\n return { localAck: false, localOpMetadata: undefined };\n }\n\n if (local) {\n return { localAck: false, localOpMetadata: this.processPendingLocalMessage(message) };\n } else {\n return this.processRemoteMessage(message);\n }\n }\n\n /**\n * Listens for ACKs of stashed ops\n */\n private processRemoteMessage(message: ISequencedDocumentMessage) {\n if (!isRuntimeMessage(message)) {\n return { localAck: false, localOpMetadata: undefined };\n }\n\n // this message was a pending op that was actually sent successfully\n const isOriginalClientId = message.clientId === Array.from(this.previousClientIds)[0] &&\n message.clientSequenceNumber >= this.firstStashedCSN;\n // this message is a pending or stashed op that was resubmitted\n const isNewClientId = Array.from(this.previousClientIds).indexOf(message.clientId) > 0;\n\n // if this is an ack for a stashed op, dequeue one message.\n // we should have seen its ref seq num by now and the DDS should be ready for it to be ACKed\n if (isOriginalClientId || isNewClientId) {\n assert(this.clientId === undefined, 0x28b /* \"multiple clients connected with stashed ops\" */);\n while (!this.pendingStates.isEmpty()) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const nextState = this.pendingStates.shift()!;\n // if it's not a message just drop it and keep looking\n if (nextState.type === \"message\") {\n this.assertOpMatch(nextState, message, isOriginalClientId);\n return { localAck: true, localOpMetadata: nextState.localOpMetadata };\n }\n }\n }\n\n if (message.type === ContainerMessageType.Rejoin && this.previousClientIds.has(message.contents?.clientId)) {\n this.previousClientIds.add(message.clientId);\n }\n\n return { localAck: false, localOpMetadata: undefined };\n }\n\n private assertOpMatch(state: IPendingMessage, message: ISequencedDocumentMessage, isOriginalClientId: boolean) {\n assert(message.type === state.messageType, 0x28c /* \"different message type\" */);\n assert(message.clientSequenceNumber === state.clientSequenceNumber || !isOriginalClientId,\n 0x28d /* \"client sequence number doesn't match\" */);\n switch(message.type) {\n case ContainerMessageType.Attach:\n assert(message.contents.id === state.content.id, 0x28e /* \"datastore ID doesn't match\" */);\n break;\n case ContainerMessageType.FluidDataStoreOp:\n assert(message.contents.address === state.content.address, 0x28f /* \"address doesn't match\" */);\n break;\n case ContainerMessageType.BlobAttach:\n // todo: assert we have blob storage, assert blob IDs match, remove blob from blob storage since it made\n // it through successfully\n break;\n case ContainerMessageType.Rejoin:\n default:\n throw new Error(`${message.type} not expected`);\n }\n }\n\n /**\n * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that\n * the batch information was preserved for batch messages.\n * @param message - The messsage that got ack'd and needs to be processed.\n */\n private processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {\n // Pre-processing part - This may be the start of a batch.\n this.maybeProcessBatchBegin(message);\n\n // Get the next state from the pending queue and verify that it is of type \"message\".\n const pendingState = this.peekNextPendingState();\n assert(pendingState.type === \"message\", 0x169 /* \"No pending message found for this remote message\" */);\n this.pendingStates.shift();\n\n // Processing part - Verify that there has been no data corruption.\n // The clientSequenceNumber of the incoming message must match that of the pending message.\n if (pendingState.clientSequenceNumber !== message.clientSequenceNumber) {\n // Close the container because this could indicate data corruption.\n const error = new DataProcessingError(\n \"unexpectedAckReceived\",\n \"unexpectedAckReceived\",\n {\n clientId: message.clientId,\n sequenceNumber: message.sequenceNumber,\n clientSequenceNumber: message.clientSequenceNumber,\n expectedClientSequenceNumber: pendingState.clientSequenceNumber,\n },\n );\n\n this.containerRuntime.closeFn(error);\n return;\n }\n\n this.pendingMessagesCount--;\n\n // Post-processing part - If we are processing a batch then this could be the last message in the batch.\n if (this.isProcessingBatch) {\n this.maybeProcessBatchEnd(message);\n }\n\n return pendingState.localOpMetadata;\n }\n\n /**\n * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {\n const pendingState = this.peekNextPendingState();\n if (pendingState.type !== \"flush\" && pendingState.type !== \"flushMode\") {\n return;\n }\n\n // If the pending state is of type \"flushMode\", it must be Manual since Automatic flush mode is processed\n // after a message is processed and not before.\n if (pendingState.type === \"flushMode\") {\n assert(pendingState.flushMode === FlushMode.TurnBased,\n 0x16a /* \"Flush mode should be manual when processing batch begin\" */);\n }\n\n // We should not already be processing a batch and there should be no pending batch begin message.\n assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,\n 0x16b /* \"The pending batch state indicates we are already processing a batch\" */);\n\n // Set the pending batch state indicating we have started processing a batch.\n this.pendingBatchBeginMessage = message;\n this.isProcessingBatch = true;\n\n // Remove this pending state from the queue as we have processed it.\n this.pendingStates.shift();\n }\n\n private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n const nextPendingState = this.peekNextPendingState();\n if (nextPendingState.type !== \"flush\" && nextPendingState.type !== \"flushMode\") {\n return;\n }\n\n // If the next pending state is of type \"flushMode\", it must be Immediate and if so, we need to remove it from\n // the queue.\n // Note that we do not remove the type \"flush\" from the queue because it indicates the end of one batch and the\n // beginning of a new one. So, it will removed when the next batch begin is processed.\n if (nextPendingState.type === \"flushMode\") {\n assert(nextPendingState.flushMode === FlushMode.Immediate,\n 0x16c /* \"Flush mode is set to TurnBased in the middle of processing a batch\" */);\n this.pendingStates.shift();\n }\n\n // There should be a pending batch begin message.\n assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* \"There is no pending batch begin message\" */);\n\n // Get the batch begin metadata from the first message in the batch.\n const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;\n\n // There could be just a single message in the batch. If so, it should not have any batch metadata. If there\n // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.\n if (this.pendingBatchBeginMessage === message) {\n assert(batchBeginMetadata === undefined,\n 0x16e /* \"Batch with single message should not have batch metadata\" */);\n } else {\n // Get the batch metadata from the last message in the batch.\n const batchEndMetadata = message.metadata?.batch;\n assert(batchBeginMetadata === true, 0x16f /* \"Did not receive batch begin metadata\" */);\n assert(batchEndMetadata === false, 0x170 /* \"Did not receive batch end metadata\" */);\n }\n\n // Clear the pending batch state now that we have processed the entire batch.\n this.pendingBatchBeginMessage = undefined;\n this.isProcessingBatch = false;\n }\n\n /**\n * Returns the next pending state from the pending state queue.\n */\n private peekNextPendingState(): IPendingState {\n const nextPendingState = this.pendingStates.peekFront();\n assert(!!nextPendingState, 0x171 /* \"No pending state found for the remote message\" */);\n return nextPendingState;\n }\n\n /**\n * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending\n * states in its queue. This includes setting the FlushMode and triggering resubmission of unacked ops.\n */\n public replayPendingStates() {\n assert(this.connected, 0x172 /* \"The connection state is not consistent with the runtime\" */);\n\n // This assert suggests we are about to send same ops twice, which will result in data loss.\n assert(this.clientId !== this.containerRuntime.clientId,\n 0x173 /* \"replayPendingStates called twice for same clientId!\" */);\n this.clientId = this.containerRuntime.clientId;\n\n assert(this.initialStates.isEmpty(), 0x174 /* \"initial states should be empty before replaying pending\" */);\n\n let pendingStatesCount = this.pendingStates.length;\n if (pendingStatesCount === 0) {\n return;\n }\n\n // Reset the pending message count because all these messages will be removed from the queue.\n this.pendingMessagesCount = 0;\n\n // Save the current FlushMode so that we can revert it back after replaying the states.\n const savedFlushMode = this.containerRuntime.flushMode;\n\n // Process exactly `pendingStatesCount` items in the queue as it represents the number of states that were\n // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue\n // which must not be replayed.\n while (pendingStatesCount > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const pendingState = this.pendingStates.shift()!;\n switch (pendingState.type) {\n case \"message\":\n {\n this.containerRuntime.reSubmitFn(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata,\n pendingState.opMetadata);\n }\n break;\n case \"flushMode\":\n {\n this.containerRuntime.setFlushMode(pendingState.flushMode);\n }\n break;\n case \"flush\":\n {\n this.containerRuntime.flush();\n }\n break;\n default:\n break;\n }\n pendingStatesCount--;\n }\n\n // Revert the FlushMode.\n this.containerRuntime.setFlushMode(savedFlushMode);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidframework/container-runtime",
|
|
3
|
-
"version": "0.51.
|
|
3
|
+
"version": "0.51.3",
|
|
4
4
|
"description": "Fluid container runtime",
|
|
5
5
|
"homepage": "https://fluidframework.com",
|
|
6
6
|
"repository": "https://github.com/microsoft/FluidFramework",
|
|
@@ -59,26 +59,26 @@
|
|
|
59
59
|
"@fluidframework/common-definitions": "^0.20.1",
|
|
60
60
|
"@fluidframework/common-utils": "^0.32.1",
|
|
61
61
|
"@fluidframework/container-definitions": "^0.41.0",
|
|
62
|
-
"@fluidframework/container-runtime-definitions": "^0.51.
|
|
63
|
-
"@fluidframework/container-utils": "^0.51.
|
|
62
|
+
"@fluidframework/container-runtime-definitions": "^0.51.3",
|
|
63
|
+
"@fluidframework/container-utils": "^0.51.3",
|
|
64
64
|
"@fluidframework/core-interfaces": "^0.40.0",
|
|
65
|
-
"@fluidframework/datastore": "^0.51.
|
|
65
|
+
"@fluidframework/datastore": "^0.51.3",
|
|
66
66
|
"@fluidframework/driver-definitions": "^0.41.0",
|
|
67
|
-
"@fluidframework/driver-utils": "^0.51.
|
|
68
|
-
"@fluidframework/garbage-collector": "^0.51.
|
|
67
|
+
"@fluidframework/driver-utils": "^0.51.3",
|
|
68
|
+
"@fluidframework/garbage-collector": "^0.51.3",
|
|
69
69
|
"@fluidframework/protocol-base": "^0.1033.0",
|
|
70
70
|
"@fluidframework/protocol-definitions": "^0.1025.0",
|
|
71
|
-
"@fluidframework/runtime-definitions": "^0.51.
|
|
72
|
-
"@fluidframework/runtime-utils": "^0.51.
|
|
73
|
-
"@fluidframework/telemetry-utils": "^0.51.
|
|
71
|
+
"@fluidframework/runtime-definitions": "^0.51.3",
|
|
72
|
+
"@fluidframework/runtime-utils": "^0.51.3",
|
|
73
|
+
"@fluidframework/telemetry-utils": "^0.51.3",
|
|
74
74
|
"double-ended-queue": "^2.1.0-0",
|
|
75
75
|
"uuid": "^8.3.1"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"@fluidframework/build-common": "^0.23.0",
|
|
79
79
|
"@fluidframework/eslint-config-fluid": "^0.24.0",
|
|
80
|
-
"@fluidframework/mocha-test-setup": "^0.51.
|
|
81
|
-
"@fluidframework/test-runtime-utils": "^0.51.
|
|
80
|
+
"@fluidframework/mocha-test-setup": "^0.51.3",
|
|
81
|
+
"@fluidframework/test-runtime-utils": "^0.51.3",
|
|
82
82
|
"@microsoft/api-extractor": "^7.16.1",
|
|
83
83
|
"@types/double-ended-queue": "^2.1.0",
|
|
84
84
|
"@types/mocha": "^8.2.2",
|
package/src/packageVersion.ts
CHANGED
|
@@ -71,7 +71,6 @@ export class PendingStateManager implements IDisposable {
|
|
|
71
71
|
private readonly initialStates: Deque<IPendingState>;
|
|
72
72
|
private readonly previousClientIds = new Set<string>();
|
|
73
73
|
private readonly firstStashedCSN: number = -1;
|
|
74
|
-
private stashedCount = 0;
|
|
75
74
|
private readonly disposeOnce = new Lazy<void>(() => {
|
|
76
75
|
this.initialStates.clear();
|
|
77
76
|
this.pendingStates.clear();
|
|
@@ -127,7 +126,6 @@ export class PendingStateManager implements IDisposable {
|
|
|
127
126
|
// get stashed op count and client sequence number of first op
|
|
128
127
|
const messages = initialState.pendingStates
|
|
129
128
|
.filter((state) => state.type === "message") as IPendingMessage[];
|
|
130
|
-
this.stashedCount = messages.length;
|
|
131
129
|
this.firstStashedCSN = messages[0].clientSequenceNumber;
|
|
132
130
|
}
|
|
133
131
|
}
|
|
@@ -290,7 +288,6 @@ export class PendingStateManager implements IDisposable {
|
|
|
290
288
|
// if it's not a message just drop it and keep looking
|
|
291
289
|
if (nextState.type === "message") {
|
|
292
290
|
this.assertOpMatch(nextState, message, isOriginalClientId);
|
|
293
|
-
--this.stashedCount;
|
|
294
291
|
return { localAck: true, localOpMetadata: nextState.localOpMetadata };
|
|
295
292
|
}
|
|
296
293
|
}
|
|
@@ -454,7 +451,6 @@ export class PendingStateManager implements IDisposable {
|
|
|
454
451
|
// This assert suggests we are about to send same ops twice, which will result in data loss.
|
|
455
452
|
assert(this.clientId !== this.containerRuntime.clientId,
|
|
456
453
|
0x173 /* "replayPendingStates called twice for same clientId!" */);
|
|
457
|
-
const prevClientId = this.clientId;
|
|
458
454
|
this.clientId = this.containerRuntime.clientId;
|
|
459
455
|
|
|
460
456
|
assert(this.initialStates.isEmpty(), 0x174 /* "initial states should be empty before replaying pending" */);
|
|
@@ -464,45 +460,6 @@ export class PendingStateManager implements IDisposable {
|
|
|
464
460
|
return;
|
|
465
461
|
}
|
|
466
462
|
|
|
467
|
-
if (!prevClientId && this.stashedCount > 0) {
|
|
468
|
-
// this is first connect, verify we are about to "resubmit" only stashed ops
|
|
469
|
-
assert(this.pendingStates.toArray().filter((s) => s.type === "message").length === this.stashedCount,
|
|
470
|
-
0x290 /* "unexpected message queued before first connect" */);
|
|
471
|
-
|
|
472
|
-
Array.from(this.previousClientIds).map((id) =>
|
|
473
|
-
assert(this.containerRuntime.getQuorum().getMember(id) === undefined,
|
|
474
|
-
0x291 /* "client with stashed ops already connected" */));
|
|
475
|
-
|
|
476
|
-
// send rejoin op with stashed client ID if we have it
|
|
477
|
-
if (this.previousClientIds.size > 0) {
|
|
478
|
-
const clientId = Array.from(this.previousClientIds)[0];
|
|
479
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
480
|
-
this.pendingStates.unshift({
|
|
481
|
-
type: "message",
|
|
482
|
-
messageType: ContainerMessageType.Rejoin,
|
|
483
|
-
content: { clientId },
|
|
484
|
-
} as IPendingMessage);
|
|
485
|
-
++pendingStatesCount;
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
if (prevClientId) {
|
|
490
|
-
// add a rejoin op so future clients provided with our stashed pending ops can recognize them
|
|
491
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
492
|
-
const firstState = this.pendingStates.peekFront()!;
|
|
493
|
-
if (firstState.type !== "message" || firstState.messageType !== ContainerMessageType.Rejoin) {
|
|
494
|
-
// if there is already a rejoin op in the queue, just resubmit same op under new client ID
|
|
495
|
-
// otherwise, add one to the queue
|
|
496
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
497
|
-
this.pendingStates.unshift({
|
|
498
|
-
type: "message",
|
|
499
|
-
messageType: ContainerMessageType.Rejoin,
|
|
500
|
-
content: { clientId: prevClientId },
|
|
501
|
-
} as IPendingMessage);
|
|
502
|
-
++pendingStatesCount;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
463
|
// Reset the pending message count because all these messages will be removed from the queue.
|
|
507
464
|
this.pendingMessagesCount = 0;
|
|
508
465
|
|