@fluidframework/container-runtime 2.0.0-internal.1.0.0.84253 → 2.0.0-internal.1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.js +12 -0
- package/dist/blobManager.d.ts +7 -1
- package/dist/blobManager.d.ts.map +1 -1
- package/dist/blobManager.js +18 -1
- package/dist/blobManager.js.map +1 -1
- package/dist/containerRuntime.d.ts +2 -66
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +37 -295
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStoreContext.d.ts +3 -5
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +13 -23
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/dataStores.d.ts.map +1 -1
- package/dist/dataStores.js +1 -6
- package/dist/dataStores.js.map +1 -1
- package/dist/garbageCollection.d.ts +37 -6
- package/dist/garbageCollection.d.ts.map +1 -1
- package/dist/garbageCollection.js +61 -65
- package/dist/garbageCollection.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +15 -2
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/scheduleManager.d.ts +28 -0
- package/dist/scheduleManager.d.ts.map +1 -0
- package/dist/scheduleManager.js +235 -0
- package/dist/scheduleManager.js.map +1 -0
- package/dist/summaryCollection.js +1 -1
- package/dist/summaryCollection.js.map +1 -1
- package/dist/summaryManager.d.ts.map +1 -1
- package/dist/summaryManager.js +20 -5
- package/dist/summaryManager.js.map +1 -1
- package/lib/blobManager.d.ts +7 -1
- package/lib/blobManager.d.ts.map +1 -1
- package/lib/blobManager.js +19 -2
- package/lib/blobManager.js.map +1 -1
- package/lib/containerRuntime.d.ts +2 -66
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +38 -295
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStoreContext.d.ts +3 -5
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +13 -23
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/dataStores.d.ts.map +1 -1
- package/lib/dataStores.js +1 -6
- package/lib/dataStores.js.map +1 -1
- package/lib/garbageCollection.d.ts +37 -6
- package/lib/garbageCollection.d.ts.map +1 -1
- package/lib/garbageCollection.js +47 -52
- package/lib/garbageCollection.js.map +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.d.ts.map +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +15 -2
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/scheduleManager.d.ts +28 -0
- package/lib/scheduleManager.d.ts.map +1 -0
- package/lib/scheduleManager.js +231 -0
- package/lib/scheduleManager.js.map +1 -0
- package/lib/summaryCollection.js +1 -1
- package/lib/summaryCollection.js.map +1 -1
- package/lib/summaryManager.d.ts.map +1 -1
- package/lib/summaryManager.js +20 -5
- package/lib/summaryManager.js.map +1 -1
- package/package.json +19 -15
- package/src/blobManager.ts +23 -1
- package/src/containerRuntime.ts +42 -392
- package/src/dataStoreContext.ts +10 -25
- package/src/dataStores.ts +0 -6
- package/src/garbageCollection.ts +64 -69
- package/src/index.ts +1 -2
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +18 -2
- package/src/scheduleManager.ts +294 -0
- package/src/summaryCollection.ts +1 -1
- package/src/summaryManager.ts +20 -5
|
@@ -13,6 +13,7 @@ const container_utils_1 = require("@fluidframework/container-utils");
|
|
|
13
13
|
const runtime_definitions_1 = require("@fluidframework/runtime-definitions");
|
|
14
14
|
const telemetry_utils_1 = require("@fluidframework/telemetry-utils");
|
|
15
15
|
const double_ended_queue_1 = __importDefault(require("double-ended-queue"));
|
|
16
|
+
const packageVersion_1 = require("./packageVersion");
|
|
16
17
|
/**
|
|
17
18
|
* PendingStateManager is responsible for maintaining the messages that have not been sent or have not yet been
|
|
18
19
|
* acknowledged by the server. It also maintains the batch information for both automatically and manually flushed
|
|
@@ -240,8 +241,20 @@ class PendingStateManager {
|
|
|
240
241
|
else {
|
|
241
242
|
// Get the batch metadata from the last message in the batch.
|
|
242
243
|
const batchEndMetadata = (_b = message.metadata) === null || _b === void 0 ? void 0 : _b.batch;
|
|
243
|
-
|
|
244
|
-
|
|
244
|
+
if (batchBeginMetadata !== true || batchEndMetadata !== false) {
|
|
245
|
+
this.stateHandler.close(container_utils_1.DataProcessingError.create("Pending batch inconsistency", // Formerly known as asserts 0x16f and 0x170
|
|
246
|
+
"processPendingLocalMessage", message, {
|
|
247
|
+
runtimeVersion: packageVersion_1.pkgVersion,
|
|
248
|
+
batchClientId: this.pendingBatchBeginMessage.clientId,
|
|
249
|
+
clientId: this.stateHandler.clientId(),
|
|
250
|
+
hasBatchStart: batchBeginMetadata === true,
|
|
251
|
+
hasBatchEnd: batchEndMetadata === false,
|
|
252
|
+
messageType: message.type,
|
|
253
|
+
batchStartSequenceNumber: this.pendingBatchBeginMessage.clientSequenceNumber,
|
|
254
|
+
pendingMessagesCount: this.pendingMessagesCount,
|
|
255
|
+
nextPendingState: nextPendingState.type,
|
|
256
|
+
}));
|
|
257
|
+
}
|
|
245
258
|
}
|
|
246
259
|
// Clear the pending batch state now that we have processed the entire batch.
|
|
247
260
|
this.pendingBatchBeginMessage = undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names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D,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,IAAI,CAAC,YAAY,CAAC,QAAQ,CACtB,YAAY,CAAC,WAAW,EACxB,YAAY,CAAC,OAAO,EACpB,YAAY,CAAC,eAAe,EAC5B,YAAY,CAAC,UAAU,CAAC,CAAC;oBAC7B,MAAM;gBACV,KAAK,WAAW;oBACZ,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;oBACvD,MAAM;gBACV,KAAK,OAAO;oBACR,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;oBAC1B,MAAM;gBACV;oBACI,MAAM;aACb;YACD,kBAAkB,EAAE,CAAC;SACxB;QAED,wBAAwB;QACxB,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IACnD,CAAC;CACJ;AA3ZD,kDA2ZC","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 { ICriticalContainerError } from \"@fluidframework/container-definitions\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\nimport { FlushMode } from \"@fluidframework/runtime-definitions\";\nimport { wrapError } from \"@fluidframework/telemetry-utils\";\nimport Deque from \"double-ended-queue\";\nimport { ContainerMessageType } 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 an explicit flush call and is added to the pending queue when flush is called on the ContainerRuntime\n * to flush pending messages.\n */\nexport interface IPendingFlush {\n type: \"flush\";\n}\n\nexport type IPendingState = IPendingMessage | IPendingFlushMode | IPendingFlush;\n\nexport interface IPendingLocalState {\n /**\n * list of pending states, including ops and batch information\n */\n pendingStates: IPendingState[];\n}\n\nexport interface IRuntimeStateHandler{\n connected(): boolean;\n clientId(): string | undefined;\n flushMode(): FlushMode;\n setFlushMode(mode: FlushMode): void;\n close(error?: ICriticalContainerError): void;\n applyStashedOp: (type: ContainerMessageType, content: ISequencedDocumentMessage) => Promise<unknown>;\n flush(): void;\n reSubmit(\n type: ContainerMessageType,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined): void;\n rollback(\n type: ContainerMessageType,\n content: any,\n localOpMetadata: unknown): void;\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 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 public get pendingMessagesCount(): number {\n return this._pendingMessagesCount;\n }\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 /**\n * This tracks the flush mode for the next message in the pending state queue. When replaying messages, we need to\n * first set the flush mode to this value and then send ops. It is important to do this info because the flush\n * mode could have been updated.\n */\n private flushModeForNextMessage: FlushMode;\n\n private clientId: string | undefined;\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 || !this.initialStates.isEmpty();\n }\n\n public getLocalState(): IPendingLocalState | undefined {\n assert(this.initialStates.isEmpty(), 0x2e9 /* \"Must call getLocalState() after applying initial states\" */);\n if (this.hasPendingMessages()) {\n return {\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 stateHandler: IRuntimeStateHandler,\n initialFlushMode: FlushMode,\n initialLocalState: IPendingLocalState | undefined,\n ) {\n this.initialStates = new Deque<IPendingState>(initialLocalState?.pendingStates ?? []);\n\n this.flushModeForNextMessage = initialFlushMode;\n this.onFlushModeUpdated(initialFlushMode);\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 this.pendingStates.push({ type: \"flushMode\", flushMode });\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 don't need to track an explicit flush call because every message is\n // automatically flushed. So, flush is a no-op.\n if (this.stateHandler.flushMode() === FlushMode.Immediate) {\n return;\n }\n\n // If the previous state is not a message, flush is a no-op.\n const previousState = this.pendingStates.peekBack();\n if (previousState?.type !== \"message\") {\n return;\n }\n\n // An explicit flush is interesting and is tracked only if there are messages sent in TurnBased mode.\n this.pendingStates.push({ type: \"flush\" });\n }\n\n /**\n * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.\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 (seqNum !== undefined) {\n if (nextState.referenceSequenceNumber > seqNum) {\n break; // nothing left to do at this sequence number\n } else if (nextState.referenceSequenceNumber < seqNum) {\n throw new Error(\"loaded from snapshot too recent to apply stashed ops\");\n }\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 =\n await this.stateHandler.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 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 message that got ack'd and needs to be processed.\n */\n public 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 = DataProcessingError.create(\n \"pending local message clientSequenceNumber mismatch\",\n \"unexpectedAckReceived\",\n message,\n { expectedClientSequenceNumber: pendingState.clientSequenceNumber },\n );\n\n this.stateHandler.close(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 this.maybeProcessBatchEnd(message);\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 // Tracks the last FlushMode that was set before this message was sent.\n let pendingFlushMode: FlushMode | undefined;\n // Tracks whether a flush was called before this message was sent.\n let pendingFlush: boolean = false;\n\n /**\n * We are checking if the next message is the start of a batch. It can happen in the following scenarios:\n * 1. The FlushMode was set to TurnBased before this message was sent.\n * 2. The FlushMode was already TurnBased and a flush was called before this message was sent. This essentially\n * means that the flush marked the end of a previous batch and beginning of a new batch.\n *\n * Keep reading pending states from the queue until we encounter a message. It's possible that the FlushMode was\n * updated a bunch of times without sending any messages.\n */\n let nextPendingState = this.peekNextPendingState();\n while (nextPendingState.type !== \"message\") {\n if (nextPendingState.type === \"flushMode\") {\n pendingFlushMode = nextPendingState.flushMode;\n }\n if (nextPendingState.type === \"flush\") {\n pendingFlush = true;\n }\n this.pendingStates.shift();\n nextPendingState = this.peekNextPendingState();\n }\n\n if (pendingFlushMode !== undefined) {\n this.flushModeForNextMessage = pendingFlushMode;\n }\n\n // If the FlushMode was set to Immediate before this message was sent, this message won't be a batch message\n // because in Immediate mode, every message is flushed individually.\n if (pendingFlushMode === FlushMode.Immediate) {\n return;\n }\n\n /**\n * This message is the first in a batch if before it was sent either the FlushMode was set to TurnBased or there\n * was an explicit flush call. Note that a flush call is tracked only in TurnBased mode and it indicates the end\n * of one batch and beginning of another.\n */\n if (pendingFlushMode === FlushMode.TurnBased || pendingFlush) {\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 }\n\n /**\n * This message could be the last message in batch. If so, clear batch state since the batch is complete.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n if (!this.isProcessingBatch) {\n return;\n }\n\n const nextPendingState = this.peekNextPendingState();\n if (nextPendingState.type === \"message\") {\n return;\n }\n\n /**\n * We are in the middle of processing a batch. The batch ends when we see an explicit flush. We should never see\n * a FlushMode before flush. This is true because we track batches only when FlushMode is TurnBased and in this\n * mode, a batch ends either by calling flush or by changing the mode to Immediate which also triggers a flush.\n */\n assert(\n nextPendingState.type !== \"flushMode\",\n 0x2bd /* \"We should not see a pending FlushMode until we see a flush when processing a batch\" */,\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 * Capture the pending state at this point\n */\n public checkpoint() {\n const checkpointHead = this.pendingStates.peekBack();\n return {\n rollback: () => {\n try {\n while (this.pendingStates.peekBack() !== checkpointHead) {\n this.rollbackNextPendingState();\n }\n } catch (err) {\n const error = wrapError(err, (message) => {\n return DataProcessingError.create(\n `RollbackError: ${message}`,\n \"checkpointRollback\",\n undefined) as DataProcessingError;\n });\n this.stateHandler.close(error);\n throw error;\n }\n },\n };\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 * Undo the last pending state\n */\n private rollbackNextPendingState() {\n const pendingStatesCount = this.pendingStates.length;\n if (pendingStatesCount === 0) {\n return;\n }\n\n this._pendingMessagesCount--;\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const pendingState = this.pendingStates.pop()!;\n switch (pendingState.type) {\n case \"message\":\n this.stateHandler.rollback(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata);\n break;\n default:\n throw new Error(`Can't rollback state ${pendingState.type}`);\n }\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.stateHandler.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.stateHandler.clientId(),\n 0x173 /* \"replayPendingStates called twice for same clientId!\" */);\n this.clientId = this.stateHandler.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.stateHandler.flushMode();\n\n // Set the flush mode for the next message. This step is important because the flush mode may have been changed\n // after the next pending message was sent.\n this.stateHandler.setFlushMode(this.flushModeForNextMessage);\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 this.stateHandler.reSubmit(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata,\n pendingState.opMetadata);\n break;\n case \"flushMode\":\n this.stateHandler.setFlushMode(pendingState.flushMode);\n break;\n case \"flush\":\n this.stateHandler.flush();\n break;\n default:\n break;\n }\n pendingStatesCount--;\n }\n\n // Revert the FlushMode.\n this.stateHandler.setFlushMode(savedFlushMode);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"pendingStateManager.js","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAGH,+DAA4D;AAE5D,qEAAsE;AAItE,6EAAgE;AAChE,qEAA4D;AAC5D,4EAAuC;AAEvC,qDAA8C;AA6D9C;;;;;;;;GAQG;AACH,MAAa,mBAAmB;IAkD5B,YACqB,YAAkC,EACnD,gBAA2B,EAC3B,iBAAiD;;QAFhC,iBAAY,GAAZ,YAAY,CAAsB;QAlDtC,kBAAa,GAAG,IAAI,4BAAK,EAAiB,CAAC;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,MAAM,cAAc,GAAG,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,CAAC;QAErD,+GAA+G;QAC/G,2CAA2C;QAC3C,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC;QAE7D,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,IAAI,CAAC,YAAY,CAAC,QAAQ,CACtB,YAAY,CAAC,WAAW,EACxB,YAAY,CAAC,OAAO,EACpB,YAAY,CAAC,eAAe,EAC5B,YAAY,CAAC,UAAU,CAAC,CAAC;oBAC7B,MAAM;gBACV,KAAK,WAAW;oBACZ,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;oBACvD,MAAM;gBACV,KAAK,OAAO;oBACR,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;oBAC1B,MAAM;gBACV;oBACI,MAAM;aACb;YACD,kBAAkB,EAAE,CAAC;SACxB;QAED,wBAAwB;QACxB,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IACnD,CAAC;CACJ;AA1aD,kDA0aC","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 { ICriticalContainerError } from \"@fluidframework/container-definitions\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\nimport { FlushMode } from \"@fluidframework/runtime-definitions\";\nimport { wrapError } from \"@fluidframework/telemetry-utils\";\nimport Deque from \"double-ended-queue\";\nimport { ContainerMessageType } from \"./containerRuntime\";\nimport { pkgVersion } from \"./packageVersion\";\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 an explicit flush call and is added to the pending queue when flush is called on the ContainerRuntime\n * to flush pending messages.\n */\nexport interface IPendingFlush {\n type: \"flush\";\n}\n\nexport type IPendingState = IPendingMessage | IPendingFlushMode | IPendingFlush;\n\nexport interface IPendingLocalState {\n /**\n * list of pending states, including ops and batch information\n */\n pendingStates: IPendingState[];\n}\n\nexport interface IRuntimeStateHandler{\n connected(): boolean;\n clientId(): string | undefined;\n flushMode(): FlushMode;\n setFlushMode(mode: FlushMode): void;\n close(error?: ICriticalContainerError): void;\n applyStashedOp: (type: ContainerMessageType, content: ISequencedDocumentMessage) => Promise<unknown>;\n flush(): void;\n reSubmit(\n type: ContainerMessageType,\n content: any,\n localOpMetadata: unknown,\n opMetadata: Record<string, unknown> | undefined): void;\n rollback(\n type: ContainerMessageType,\n content: any,\n localOpMetadata: unknown): void;\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 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 public get pendingMessagesCount(): number {\n return this._pendingMessagesCount;\n }\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 /**\n * This tracks the flush mode for the next message in the pending state queue. When replaying messages, we need to\n * first set the flush mode to this value and then send ops. It is important to do this info because the flush\n * mode could have been updated.\n */\n private flushModeForNextMessage: FlushMode;\n\n private clientId: string | undefined;\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 || !this.initialStates.isEmpty();\n }\n\n public getLocalState(): IPendingLocalState | undefined {\n assert(this.initialStates.isEmpty(), 0x2e9 /* \"Must call getLocalState() after applying initial states\" */);\n if (this.hasPendingMessages()) {\n return {\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 stateHandler: IRuntimeStateHandler,\n initialFlushMode: FlushMode,\n initialLocalState: IPendingLocalState | undefined,\n ) {\n this.initialStates = new Deque<IPendingState>(initialLocalState?.pendingStates ?? []);\n\n this.flushModeForNextMessage = initialFlushMode;\n this.onFlushModeUpdated(initialFlushMode);\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 this.pendingStates.push({ type: \"flushMode\", flushMode });\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 don't need to track an explicit flush call because every message is\n // automatically flushed. So, flush is a no-op.\n if (this.stateHandler.flushMode() === FlushMode.Immediate) {\n return;\n }\n\n // If the previous state is not a message, flush is a no-op.\n const previousState = this.pendingStates.peekBack();\n if (previousState?.type !== \"message\") {\n return;\n }\n\n // An explicit flush is interesting and is tracked only if there are messages sent in TurnBased mode.\n this.pendingStates.push({ type: \"flush\" });\n }\n\n /**\n * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted\n * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.\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 (seqNum !== undefined) {\n if (nextState.referenceSequenceNumber > seqNum) {\n break; // nothing left to do at this sequence number\n } else if (nextState.referenceSequenceNumber < seqNum) {\n throw new Error(\"loaded from snapshot too recent to apply stashed ops\");\n }\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 =\n await this.stateHandler.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 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 message that got ack'd and needs to be processed.\n */\n public 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 = DataProcessingError.create(\n \"pending local message clientSequenceNumber mismatch\",\n \"unexpectedAckReceived\",\n message,\n { expectedClientSequenceNumber: pendingState.clientSequenceNumber },\n );\n\n this.stateHandler.close(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 this.maybeProcessBatchEnd(message);\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 // Tracks the last FlushMode that was set before this message was sent.\n let pendingFlushMode: FlushMode | undefined;\n // Tracks whether a flush was called before this message was sent.\n let pendingFlush: boolean = false;\n\n /**\n * We are checking if the next message is the start of a batch. It can happen in the following scenarios:\n * 1. The FlushMode was set to TurnBased before this message was sent.\n * 2. The FlushMode was already TurnBased and a flush was called before this message was sent. This essentially\n * means that the flush marked the end of a previous batch and beginning of a new batch.\n *\n * Keep reading pending states from the queue until we encounter a message. It's possible that the FlushMode was\n * updated a bunch of times without sending any messages.\n */\n let nextPendingState = this.peekNextPendingState();\n while (nextPendingState.type !== \"message\") {\n if (nextPendingState.type === \"flushMode\") {\n pendingFlushMode = nextPendingState.flushMode;\n }\n if (nextPendingState.type === \"flush\") {\n pendingFlush = true;\n }\n this.pendingStates.shift();\n nextPendingState = this.peekNextPendingState();\n }\n\n if (pendingFlushMode !== undefined) {\n this.flushModeForNextMessage = pendingFlushMode;\n }\n\n // If the FlushMode was set to Immediate before this message was sent, this message won't be a batch message\n // because in Immediate mode, every message is flushed individually.\n if (pendingFlushMode === FlushMode.Immediate) {\n return;\n }\n\n /**\n * This message is the first in a batch if before it was sent either the FlushMode was set to TurnBased or there\n * was an explicit flush call. Note that a flush call is tracked only in TurnBased mode and it indicates the end\n * of one batch and beginning of another.\n */\n if (pendingFlushMode === FlushMode.TurnBased || pendingFlush) {\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 }\n\n /**\n * This message could be the last message in batch. If so, clear batch state since the batch is complete.\n * @param message - The message that is being processed.\n */\n private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {\n if (!this.isProcessingBatch) {\n return;\n }\n\n const nextPendingState = this.peekNextPendingState();\n if (nextPendingState.type === \"message\") {\n return;\n }\n\n /**\n * We are in the middle of processing a batch. The batch ends when we see an explicit flush. We should never see\n * a FlushMode before flush. This is true because we track batches only when FlushMode is TurnBased and in this\n * mode, a batch ends either by calling flush or by changing the mode to Immediate which also triggers a flush.\n */\n assert(\n nextPendingState.type !== \"flushMode\",\n 0x2bd /* \"We should not see a pending FlushMode until we see a flush when processing a batch\" */,\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 if (batchBeginMetadata !== true || batchEndMetadata !== false) {\n this.stateHandler.close(DataProcessingError.create(\n \"Pending batch inconsistency\", // Formerly known as asserts 0x16f and 0x170\n \"processPendingLocalMessage\",\n message,\n {\n runtimeVersion: pkgVersion,\n batchClientId: this.pendingBatchBeginMessage.clientId,\n clientId: this.stateHandler.clientId(),\n hasBatchStart: batchBeginMetadata === true,\n hasBatchEnd: batchEndMetadata === false,\n messageType: message.type,\n batchStartSequenceNumber: this.pendingBatchBeginMessage.clientSequenceNumber,\n pendingMessagesCount: this.pendingMessagesCount,\n nextPendingState: nextPendingState.type,\n }));\n }\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 * Capture the pending state at this point\n */\n public checkpoint() {\n const checkpointHead = this.pendingStates.peekBack();\n return {\n rollback: () => {\n try {\n while (this.pendingStates.peekBack() !== checkpointHead) {\n this.rollbackNextPendingState();\n }\n } catch (err) {\n const error = wrapError(err, (message) => {\n return DataProcessingError.create(\n `RollbackError: ${message}`,\n \"checkpointRollback\",\n undefined) as DataProcessingError;\n });\n this.stateHandler.close(error);\n throw error;\n }\n },\n };\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 * Undo the last pending state\n */\n private rollbackNextPendingState() {\n const pendingStatesCount = this.pendingStates.length;\n if (pendingStatesCount === 0) {\n return;\n }\n\n this._pendingMessagesCount--;\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const pendingState = this.pendingStates.pop()!;\n switch (pendingState.type) {\n case \"message\":\n this.stateHandler.rollback(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata);\n break;\n default:\n throw new Error(`Can't rollback state ${pendingState.type}`);\n }\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.stateHandler.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.stateHandler.clientId(),\n 0x173 /* \"replayPendingStates called twice for same clientId!\" */);\n this.clientId = this.stateHandler.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.stateHandler.flushMode();\n\n // Set the flush mode for the next message. This step is important because the flush mode may have been changed\n // after the next pending message was sent.\n this.stateHandler.setFlushMode(this.flushModeForNextMessage);\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 this.stateHandler.reSubmit(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata,\n pendingState.opMetadata);\n break;\n case \"flushMode\":\n this.stateHandler.setFlushMode(pendingState.flushMode);\n break;\n case \"flush\":\n this.stateHandler.flush();\n break;\n default:\n break;\n }\n pendingStatesCount--;\n }\n\n // Revert the FlushMode.\n this.stateHandler.setFlushMode(savedFlushMode);\n }\n}\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/*!
|
|
3
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
4
|
+
* Licensed under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
import { EventEmitter } from "events";
|
|
7
|
+
import { IDeltaManager } from "@fluidframework/container-definitions";
|
|
8
|
+
import { IDocumentMessage, ISequencedDocumentMessage } from "@fluidframework/protocol-definitions";
|
|
9
|
+
import { ITelemetryLogger } from "@fluidframework/common-definitions";
|
|
10
|
+
/**
|
|
11
|
+
* This class has the following responsibilities:
|
|
12
|
+
* 1. It tracks batches as we process ops and raises "batchBegin" and "batchEnd" events.
|
|
13
|
+
* As part of it, it validates batch correctness (i.e. no system ops in the middle of batch)
|
|
14
|
+
* 2. It creates instance of ScheduleManagerCore that ensures we never start processing ops from batch
|
|
15
|
+
* unless all ops of the batch are in.
|
|
16
|
+
*/
|
|
17
|
+
export declare class ScheduleManager {
|
|
18
|
+
private readonly deltaManager;
|
|
19
|
+
private readonly emitter;
|
|
20
|
+
private readonly logger;
|
|
21
|
+
private readonly deltaScheduler;
|
|
22
|
+
private batchClientId;
|
|
23
|
+
private hitError;
|
|
24
|
+
constructor(deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>, emitter: EventEmitter, logger: ITelemetryLogger);
|
|
25
|
+
beforeOpProcessing(message: ISequencedDocumentMessage): void;
|
|
26
|
+
afterOpProcessing(error: any | undefined, message: ISequencedDocumentMessage): void;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=scheduleManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduleManager.d.ts","sourceRoot":"","sources":["../src/scheduleManager.ts"],"names":[],"mappings":";AAAA;;;GAGG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,sCAAsC,CAAC;AACnG,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAatE;;;;;;GAMG;AACH,qBAAa,eAAe;IAMpB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAP3B,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAiB;IAChD,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,QAAQ,CAAS;gBAGJ,YAAY,EAAE,aAAa,CAAC,yBAAyB,EAAE,gBAAgB,CAAC,EACxE,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,gBAAgB;IAStC,kBAAkB,CAAC,OAAO,EAAE,yBAAyB;IAkBrD,iBAAiB,CAAC,KAAK,EAAE,GAAG,GAAG,SAAS,EAAE,OAAO,EAAE,yBAAyB;CAwBtF"}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ScheduleManager = void 0;
|
|
4
|
+
const telemetry_utils_1 = require("@fluidframework/telemetry-utils");
|
|
5
|
+
const common_utils_1 = require("@fluidframework/common-utils");
|
|
6
|
+
const driver_utils_1 = require("@fluidframework/driver-utils");
|
|
7
|
+
const container_utils_1 = require("@fluidframework/container-utils");
|
|
8
|
+
const deltaScheduler_1 = require("./deltaScheduler");
|
|
9
|
+
const packageVersion_1 = require("./packageVersion");
|
|
10
|
+
const connectionTelemetry_1 = require("./connectionTelemetry");
|
|
11
|
+
/**
|
|
12
|
+
* This class has the following responsibilities:
|
|
13
|
+
* 1. It tracks batches as we process ops and raises "batchBegin" and "batchEnd" events.
|
|
14
|
+
* As part of it, it validates batch correctness (i.e. no system ops in the middle of batch)
|
|
15
|
+
* 2. It creates instance of ScheduleManagerCore that ensures we never start processing ops from batch
|
|
16
|
+
* unless all ops of the batch are in.
|
|
17
|
+
*/
|
|
18
|
+
class ScheduleManager {
|
|
19
|
+
constructor(deltaManager, emitter, logger) {
|
|
20
|
+
this.deltaManager = deltaManager;
|
|
21
|
+
this.emitter = emitter;
|
|
22
|
+
this.logger = logger;
|
|
23
|
+
this.hitError = false;
|
|
24
|
+
this.deltaScheduler = new deltaScheduler_1.DeltaScheduler(this.deltaManager, telemetry_utils_1.ChildLogger.create(this.logger, "DeltaScheduler"));
|
|
25
|
+
void new ScheduleManagerCore(deltaManager, logger);
|
|
26
|
+
}
|
|
27
|
+
beforeOpProcessing(message) {
|
|
28
|
+
var _a;
|
|
29
|
+
if (this.batchClientId !== message.clientId) {
|
|
30
|
+
(0, common_utils_1.assert)(this.batchClientId === undefined, 0x2a2 /* "Batch is interrupted by other client op. Should be caught by trackPending()" */);
|
|
31
|
+
// This could be the beginning of a new batch or an individual message.
|
|
32
|
+
this.emitter.emit("batchBegin", message);
|
|
33
|
+
this.deltaScheduler.batchBegin(message);
|
|
34
|
+
const batch = (_a = message === null || message === void 0 ? void 0 : message.metadata) === null || _a === void 0 ? void 0 : _a.batch;
|
|
35
|
+
if (batch) {
|
|
36
|
+
this.batchClientId = message.clientId;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.batchClientId = undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
afterOpProcessing(error, message) {
|
|
44
|
+
var _a;
|
|
45
|
+
// If this is no longer true, we need to revisit what we do where we set this.hitError.
|
|
46
|
+
(0, common_utils_1.assert)(!this.hitError, 0x2a3 /* "container should be closed on any error" */);
|
|
47
|
+
if (error) {
|
|
48
|
+
// We assume here that loader will close container and stop processing all future ops.
|
|
49
|
+
// This is implicit dependency. If this flow changes, this code might no longer be correct.
|
|
50
|
+
this.hitError = true;
|
|
51
|
+
this.batchClientId = undefined;
|
|
52
|
+
this.emitter.emit("batchEnd", error, message);
|
|
53
|
+
this.deltaScheduler.batchEnd(message);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const batch = (_a = message === null || message === void 0 ? void 0 : message.metadata) === null || _a === void 0 ? void 0 : _a.batch;
|
|
57
|
+
// If no batchClientId has been set then we're in an individual batch. Else, if we get
|
|
58
|
+
// batch end metadata, this is end of the current batch.
|
|
59
|
+
if (this.batchClientId === undefined || batch === false) {
|
|
60
|
+
this.batchClientId = undefined;
|
|
61
|
+
this.emitter.emit("batchEnd", undefined, message);
|
|
62
|
+
this.deltaScheduler.batchEnd(message);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.ScheduleManager = ScheduleManager;
|
|
68
|
+
/**
|
|
69
|
+
* This class controls pausing and resuming of inbound queue to ensure that we never
|
|
70
|
+
* start processing ops in a batch IF we do not have all ops in the batch.
|
|
71
|
+
*/
|
|
72
|
+
class ScheduleManagerCore {
|
|
73
|
+
constructor(deltaManager, logger) {
|
|
74
|
+
this.deltaManager = deltaManager;
|
|
75
|
+
this.logger = logger;
|
|
76
|
+
this.localPaused = false;
|
|
77
|
+
this.timePaused = 0;
|
|
78
|
+
this.batchCount = 0;
|
|
79
|
+
// Listen for delta manager sends and add batch metadata to messages
|
|
80
|
+
this.deltaManager.on("prepareSend", (messages) => {
|
|
81
|
+
if (messages.length === 0) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
// First message will have the batch flag set to true if doing a batched send
|
|
85
|
+
const firstMessageMetadata = messages[0].metadata;
|
|
86
|
+
if (!(firstMessageMetadata === null || firstMessageMetadata === void 0 ? void 0 : firstMessageMetadata.batch)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
// If the batch contains only a single op, clear the batch flag.
|
|
90
|
+
if (messages.length === 1) {
|
|
91
|
+
delete firstMessageMetadata.batch;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// Set the batch flag to false on the last message to indicate the end of the send batch
|
|
95
|
+
const lastMessage = messages[messages.length - 1];
|
|
96
|
+
lastMessage.metadata = Object.assign(Object.assign({}, lastMessage.metadata), { batch: false });
|
|
97
|
+
});
|
|
98
|
+
// Listen for updates and peek at the inbound
|
|
99
|
+
this.deltaManager.inbound.on("push", (message) => {
|
|
100
|
+
this.trackPending(message);
|
|
101
|
+
});
|
|
102
|
+
// Start with baseline - empty inbound queue.
|
|
103
|
+
(0, common_utils_1.assert)(!this.localPaused, 0x293 /* "initial state" */);
|
|
104
|
+
const allPending = this.deltaManager.inbound.toArray();
|
|
105
|
+
for (const pending of allPending) {
|
|
106
|
+
this.trackPending(pending);
|
|
107
|
+
}
|
|
108
|
+
// We are intentionally directly listening to the "op" to inspect system ops as well.
|
|
109
|
+
// If we do not observe system ops, we are likely to hit 0x296 assert when system ops
|
|
110
|
+
// precedes start of incomplete batch.
|
|
111
|
+
this.deltaManager.on("op", (message) => this.afterOpProcessing(message.sequenceNumber));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The only public function in this class - called when we processed an op,
|
|
115
|
+
* to make decision if op processing should be paused or not after that.
|
|
116
|
+
*/
|
|
117
|
+
afterOpProcessing(sequenceNumber) {
|
|
118
|
+
(0, common_utils_1.assert)(!this.localPaused, 0x294 /* "can't have op processing paused if we are processing an op" */);
|
|
119
|
+
// If the inbound queue is ever empty, nothing to do!
|
|
120
|
+
if (this.deltaManager.inbound.length === 0) {
|
|
121
|
+
(0, common_utils_1.assert)(this.pauseSequenceNumber === undefined, 0x295 /* "there should be no pending batch if we have no ops" */);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
// The queue is
|
|
125
|
+
// 1. paused only when the next message to be processed is the beginning of a batch. Done in two places:
|
|
126
|
+
// - here (processing ops until reaching start of incomplete batch)
|
|
127
|
+
// - in trackPending(), when queue was empty and start of batch showed up.
|
|
128
|
+
// 2. resumed when batch end comes in (in trackPending())
|
|
129
|
+
// do we have incomplete batch to worry about?
|
|
130
|
+
if (this.pauseSequenceNumber !== undefined) {
|
|
131
|
+
(0, common_utils_1.assert)(sequenceNumber < this.pauseSequenceNumber, 0x296 /* "we should never start processing incomplete batch!" */);
|
|
132
|
+
// If the next op is the start of incomplete batch, then we can't process it until it's fully in - pause!
|
|
133
|
+
if (sequenceNumber + 1 === this.pauseSequenceNumber) {
|
|
134
|
+
this.pauseQueue();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
pauseQueue() {
|
|
139
|
+
(0, common_utils_1.assert)(!this.localPaused, 0x297 /* "always called from resumed state" */);
|
|
140
|
+
this.localPaused = true;
|
|
141
|
+
this.timePaused = common_utils_1.performance.now();
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
143
|
+
this.deltaManager.inbound.pause();
|
|
144
|
+
}
|
|
145
|
+
resumeQueue(startBatch, messageEndBatch) {
|
|
146
|
+
const endBatch = messageEndBatch.sequenceNumber;
|
|
147
|
+
const duration = this.localPaused ? (common_utils_1.performance.now() - this.timePaused) : undefined;
|
|
148
|
+
this.batchCount++;
|
|
149
|
+
if (this.batchCount % 1000 === 1) {
|
|
150
|
+
this.logger.sendTelemetryEvent({
|
|
151
|
+
eventName: "BatchStats",
|
|
152
|
+
sequenceNumber: endBatch,
|
|
153
|
+
length: endBatch - startBatch + 1,
|
|
154
|
+
msnDistance: endBatch - messageEndBatch.minimumSequenceNumber,
|
|
155
|
+
duration,
|
|
156
|
+
batchCount: this.batchCount,
|
|
157
|
+
interrupted: this.localPaused,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
// Return early if no change in value
|
|
161
|
+
if (!this.localPaused) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
this.localPaused = false;
|
|
165
|
+
// Random round number - we want to know when batch waiting paused op processing.
|
|
166
|
+
if (duration !== undefined && duration > connectionTelemetry_1.latencyThreshold) {
|
|
167
|
+
this.logger.sendErrorEvent({
|
|
168
|
+
eventName: "MaxBatchWaitTimeExceeded",
|
|
169
|
+
duration,
|
|
170
|
+
sequenceNumber: endBatch,
|
|
171
|
+
length: endBatch - startBatch,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
this.deltaManager.inbound.resume();
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Called for each incoming op (i.e. inbound "push" notification)
|
|
178
|
+
*/
|
|
179
|
+
trackPending(message) {
|
|
180
|
+
(0, common_utils_1.assert)(this.deltaManager.inbound.length !== 0, 0x298 /* "we have something in the queue that generates this event" */);
|
|
181
|
+
(0, common_utils_1.assert)((this.currentBatchClientId === undefined) === (this.pauseSequenceNumber === undefined), 0x299 /* "non-synchronized state" */);
|
|
182
|
+
const metadata = message.metadata;
|
|
183
|
+
const batchMetadata = metadata === null || metadata === void 0 ? void 0 : metadata.batch;
|
|
184
|
+
// Protocol messages are never part of a runtime batch of messages
|
|
185
|
+
if (!(0, driver_utils_1.isUnpackedRuntimeMessage)(message)) {
|
|
186
|
+
// Protocol messages should never show up in the middle of the batch!
|
|
187
|
+
(0, common_utils_1.assert)(this.currentBatchClientId === undefined, 0x29a /* "System message in the middle of batch!" */);
|
|
188
|
+
(0, common_utils_1.assert)(batchMetadata === undefined, 0x29b /* "system op in a batch?" */);
|
|
189
|
+
(0, common_utils_1.assert)(!this.localPaused, 0x29c /* "we should be processing ops when there is no active batch" */);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (this.currentBatchClientId === undefined && batchMetadata === undefined) {
|
|
193
|
+
(0, common_utils_1.assert)(!this.localPaused, 0x29d /* "we should be processing ops when there is no active batch" */);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
// If the client ID changes then we can move the pause point. If it stayed the same then we need to check.
|
|
197
|
+
// If batchMetadata is not undefined then if it's true we've begun a new batch - if false we've ended
|
|
198
|
+
// the previous one
|
|
199
|
+
if (this.currentBatchClientId !== undefined || batchMetadata === false) {
|
|
200
|
+
if (this.currentBatchClientId !== message.clientId) {
|
|
201
|
+
// "Batch not closed, yet message from another client!"
|
|
202
|
+
throw new container_utils_1.DataCorruptionError("OpBatchIncomplete", Object.assign({ runtimeVersion: packageVersion_1.pkgVersion, batchClientId: this.currentBatchClientId }, (0, container_utils_1.extractSafePropertiesFromMessage)(message)));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// The queue is
|
|
206
|
+
// 1. paused only when the next message to be processed is the beginning of a batch. Done in two places:
|
|
207
|
+
// - in afterOpProcessing() - processing ops until reaching start of incomplete batch
|
|
208
|
+
// - here (batchMetadata == false below), when queue was empty and start of batch showed up.
|
|
209
|
+
// 2. resumed when batch end comes in (batchMetadata === true case below)
|
|
210
|
+
if (batchMetadata) {
|
|
211
|
+
(0, common_utils_1.assert)(this.currentBatchClientId === undefined, 0x29e /* "there can't be active batch" */);
|
|
212
|
+
(0, common_utils_1.assert)(!this.localPaused, 0x29f /* "we should be processing ops when there is no active batch" */);
|
|
213
|
+
this.pauseSequenceNumber = message.sequenceNumber;
|
|
214
|
+
this.currentBatchClientId = message.clientId;
|
|
215
|
+
// Start of the batch
|
|
216
|
+
// Only pause processing if queue has no other ops!
|
|
217
|
+
// If there are any other ops in the queue, processing will be stopped when they are processed!
|
|
218
|
+
if (this.deltaManager.inbound.length === 1) {
|
|
219
|
+
this.pauseQueue();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else if (batchMetadata === false) {
|
|
223
|
+
(0, common_utils_1.assert)(this.pauseSequenceNumber !== undefined, 0x2a0 /* "batch presence was validated above" */);
|
|
224
|
+
// Batch is complete, we can process it!
|
|
225
|
+
this.resumeQueue(this.pauseSequenceNumber, message);
|
|
226
|
+
this.pauseSequenceNumber = undefined;
|
|
227
|
+
this.currentBatchClientId = undefined;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
// Continuation of current batch. Do nothing
|
|
231
|
+
(0, common_utils_1.assert)(this.currentBatchClientId !== undefined, 0x2a1 /* "logic error" */);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
//# sourceMappingURL=scheduleManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduleManager.js","sourceRoot":"","sources":["../src/scheduleManager.ts"],"names":[],"mappings":";;;AAQA,qEAA8D;AAC9D,+DAAmE;AACnE,+DAAwE;AACxE,qEAAwG;AACxG,qDAAkD;AAClD,qDAA8C;AAC9C,+DAAyD;AAMzD;;;;;;GAMG;AACH,MAAa,eAAe;IAKxB,YACqB,YAAwE,EACxE,OAAqB,EACrB,MAAwB;QAFxB,iBAAY,GAAZ,YAAY,CAA4D;QACxE,YAAO,GAAP,OAAO,CAAc;QACrB,WAAM,GAAN,MAAM,CAAkB;QALrC,aAAQ,GAAG,KAAK,CAAC;QAOrB,IAAI,CAAC,cAAc,GAAG,IAAI,+BAAc,CACpC,IAAI,CAAC,YAAY,EACjB,6BAAW,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,gBAAgB,CAAC,CACpD,CAAC;QACF,KAAK,IAAI,mBAAmB,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IACvD,CAAC;IAEM,kBAAkB,CAAC,OAAkC;;QACxD,IAAI,IAAI,CAAC,aAAa,KAAK,OAAO,CAAC,QAAQ,EAAE;YACzC,IAAA,qBAAM,EAAC,IAAI,CAAC,aAAa,KAAK,SAAS,EACnC,KAAK,CAAC,mFAAmF,CAAC,CAAC;YAE/F,uEAAuE;YACvE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;YACzC,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;YAExC,MAAM,KAAK,GAAG,MAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAoC,0CAAE,KAAK,CAAC;YACp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,IAAI,CAAC,oBAAoB,KAAK,SAAS,EAAE,KAAK,CAAC,mCAAmC,CAAC,CAAC;YAC3F,IAAA,qBAAM,EAAC,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,iEAAiE,CAAC,CAAC;YACnG,IAAI,CAAC,mBAAmB,GAAG,OAAO,CAAC,cAAc,CAAC;YAClD,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC,QAAQ,CAAC;YAC7C,qBAAqB;YACrB,mDAAmD;YACnD,+FAA+F;YAC/F,IAAI,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE;gBACxC,IAAI,CAAC,UAAU,EAAE,CAAC;aACrB;SACJ;aAAM,IAAI,aAAa,KAAK,KAAK,EAAE;YAChC,IAAA,qBAAM,EAAC,IAAI,CAAC,mBAAmB,KAAK,SAAS,EAAE,KAAK,CAAC,0CAA0C,CAAC,CAAC;YACjG,wCAAwC;YACxC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;YACpD,IAAI,CAAC,mBAAmB,GAAG,SAAS,CAAC;YACrC,IAAI,CAAC,oBAAoB,GAAG,SAAS,CAAC;SACzC;aAAM;YACH,4CAA4C;YAC5C,IAAA,qBAAM,EAAC,IAAI,CAAC,oBAAoB,KAAK,SAAS,EAAE,KAAK,CAAC,mBAAmB,CAAC,CAAC;SAC9E;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\nimport { EventEmitter } from \"events\";\nimport { IDeltaManager } from \"@fluidframework/container-definitions\";\nimport { IDocumentMessage, ISequencedDocumentMessage } from \"@fluidframework/protocol-definitions\";\nimport { ITelemetryLogger } from \"@fluidframework/common-definitions\";\nimport { ChildLogger } from \"@fluidframework/telemetry-utils\";\nimport { assert, performance } from \"@fluidframework/common-utils\";\nimport { isUnpackedRuntimeMessage } from \"@fluidframework/driver-utils\";\nimport { DataCorruptionError, extractSafePropertiesFromMessage } from \"@fluidframework/container-utils\";\nimport { DeltaScheduler } from \"./deltaScheduler\";\nimport { pkgVersion } from \"./packageVersion\";\nimport { latencyThreshold } from \"./connectionTelemetry\";\n\ntype IRuntimeMessageMetadata = undefined | {\n batch?: boolean;\n};\n\n/**\n * This class has the following responsibilities:\n * 1. It tracks batches as we process ops and raises \"batchBegin\" and \"batchEnd\" events.\n * As part of it, it validates batch correctness (i.e. no system ops in the middle of batch)\n * 2. It creates instance of ScheduleManagerCore that ensures we never start processing ops from batch\n * unless all ops of the batch are in.\n */\nexport class ScheduleManager {\n private readonly deltaScheduler: DeltaScheduler;\n private batchClientId: string | undefined;\n private hitError = false;\n\n constructor(\n private readonly deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,\n private readonly emitter: EventEmitter,\n private readonly logger: ITelemetryLogger,\n ) {\n this.deltaScheduler = new DeltaScheduler(\n this.deltaManager,\n ChildLogger.create(this.logger, \"DeltaScheduler\"),\n );\n void new ScheduleManagerCore(deltaManager, logger);\n }\n\n public beforeOpProcessing(message: ISequencedDocumentMessage) {\n if (this.batchClientId !== message.clientId) {\n assert(this.batchClientId === undefined,\n 0x2a2 /* \"Batch is interrupted by other client op. Should be caught by trackPending()\" */);\n\n // This could be the beginning of a new batch or an individual message.\n this.emitter.emit(\"batchBegin\", message);\n this.deltaScheduler.batchBegin(message);\n\n const batch = (message?.metadata as IRuntimeMessageMetadata)?.batch;\n if (batch) {\n this.batchClientId = message.clientId;\n } else {\n this.batchClientId = undefined;\n }\n }\n }\n\n public afterOpProcessing(error: any | undefined, message: ISequencedDocumentMessage) {\n // If this is no longer true, we need to revisit what we do where we set this.hitError.\n assert(!this.hitError, 0x2a3 /* \"container should be closed on any error\" */);\n\n if (error) {\n // We assume here that loader will close container and stop processing all future ops.\n // This is implicit dependency. If this flow changes, this code might no longer be correct.\n this.hitError = true;\n this.batchClientId = undefined;\n this.emitter.emit(\"batchEnd\", error, message);\n this.deltaScheduler.batchEnd(message);\n return;\n }\n\n const batch = (message?.metadata as IRuntimeMessageMetadata)?.batch;\n // If no batchClientId has been set then we're in an individual batch. Else, if we get\n // batch end metadata, this is end of the current batch.\n if (this.batchClientId === undefined || batch === false) {\n this.batchClientId = undefined;\n this.emitter.emit(\"batchEnd\", undefined, message);\n this.deltaScheduler.batchEnd(message);\n return;\n }\n }\n}\n\n/**\n * This class controls pausing and resuming of inbound queue to ensure that we never\n * start processing ops in a batch IF we do not have all ops in the batch.\n */\nclass ScheduleManagerCore {\n private pauseSequenceNumber: number | undefined;\n private currentBatchClientId: string | undefined;\n private localPaused = false;\n private timePaused = 0;\n private batchCount = 0;\n\n constructor(\n private readonly deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,\n private readonly logger: ITelemetryLogger,\n ) {\n // Listen for delta manager sends and add batch metadata to messages\n this.deltaManager.on(\"prepareSend\", (messages: IDocumentMessage[]) => {\n if (messages.length === 0) {\n return;\n }\n\n // First message will have the batch flag set to true if doing a batched send\n const firstMessageMetadata = messages[0].metadata as IRuntimeMessageMetadata;\n if (!firstMessageMetadata?.batch) {\n return;\n }\n\n // If the batch contains only a single op, clear the batch flag.\n if (messages.length === 1) {\n delete firstMessageMetadata.batch;\n return;\n }\n\n // Set the batch flag to false on the last message to indicate the end of the send batch\n const lastMessage = messages[messages.length - 1];\n lastMessage.metadata = { ...lastMessage.metadata, batch: false };\n });\n\n // Listen for updates and peek at the inbound\n this.deltaManager.inbound.on(\n \"push\",\n (message: ISequencedDocumentMessage) => {\n this.trackPending(message);\n });\n\n // Start with baseline - empty inbound queue.\n assert(!this.localPaused, 0x293 /* \"initial state\" */);\n\n const allPending = this.deltaManager.inbound.toArray();\n for (const pending of allPending) {\n this.trackPending(pending);\n }\n\n // We are intentionally directly listening to the \"op\" to inspect system ops as well.\n // If we do not observe system ops, we are likely to hit 0x296 assert when system ops\n // precedes start of incomplete batch.\n this.deltaManager.on(\"op\", (message) => this.afterOpProcessing(message.sequenceNumber));\n }\n\n /**\n * The only public function in this class - called when we processed an op,\n * to make decision if op processing should be paused or not after that.\n */\n public afterOpProcessing(sequenceNumber: number) {\n assert(!this.localPaused, 0x294 /* \"can't have op processing paused if we are processing an op\" */);\n\n // If the inbound queue is ever empty, nothing to do!\n if (this.deltaManager.inbound.length === 0) {\n assert(this.pauseSequenceNumber === undefined,\n 0x295 /* \"there should be no pending batch if we have no ops\" */);\n return;\n }\n\n // The queue is\n // 1. paused only when the next message to be processed is the beginning of a batch. Done in two places:\n // - here (processing ops until reaching start of incomplete batch)\n // - in trackPending(), when queue was empty and start of batch showed up.\n // 2. resumed when batch end comes in (in trackPending())\n\n // do we have incomplete batch to worry about?\n if (this.pauseSequenceNumber !== undefined) {\n assert(sequenceNumber < this.pauseSequenceNumber,\n 0x296 /* \"we should never start processing incomplete batch!\" */);\n // If the next op is the start of incomplete batch, then we can't process it until it's fully in - pause!\n if (sequenceNumber + 1 === this.pauseSequenceNumber) {\n this.pauseQueue();\n }\n }\n }\n\n private pauseQueue() {\n assert(!this.localPaused, 0x297 /* \"always called from resumed state\" */);\n this.localPaused = true;\n this.timePaused = performance.now();\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.deltaManager.inbound.pause();\n }\n\n private resumeQueue(startBatch: number, messageEndBatch: ISequencedDocumentMessage) {\n const endBatch = messageEndBatch.sequenceNumber;\n const duration = this.localPaused ? (performance.now() - this.timePaused) : undefined;\n\n this.batchCount++;\n if (this.batchCount % 1000 === 1) {\n this.logger.sendTelemetryEvent({\n eventName: \"BatchStats\",\n sequenceNumber: endBatch,\n length: endBatch - startBatch + 1,\n msnDistance: endBatch - messageEndBatch.minimumSequenceNumber,\n duration,\n batchCount: this.batchCount,\n interrupted: this.localPaused,\n });\n }\n\n // Return early if no change in value\n if (!this.localPaused) {\n return;\n }\n\n this.localPaused = false;\n\n // Random round number - we want to know when batch waiting paused op processing.\n if (duration !== undefined && duration > latencyThreshold) {\n this.logger.sendErrorEvent({\n eventName: \"MaxBatchWaitTimeExceeded\",\n duration,\n sequenceNumber: endBatch,\n length: endBatch - startBatch,\n });\n }\n this.deltaManager.inbound.resume();\n }\n\n /**\n * Called for each incoming op (i.e. inbound \"push\" notification)\n */\n private trackPending(message: ISequencedDocumentMessage) {\n assert(this.deltaManager.inbound.length !== 0,\n 0x298 /* \"we have something in the queue that generates this event\" */);\n\n assert((this.currentBatchClientId === undefined) === (this.pauseSequenceNumber === undefined),\n 0x299 /* \"non-synchronized state\" */);\n\n const metadata = message.metadata as IRuntimeMessageMetadata;\n const batchMetadata = metadata?.batch;\n\n // Protocol messages are never part of a runtime batch of messages\n if (!isUnpackedRuntimeMessage(message)) {\n // Protocol messages should never show up in the middle of the batch!\n assert(this.currentBatchClientId === undefined, 0x29a /* \"System message in the middle of batch!\" */);\n assert(batchMetadata === undefined, 0x29b /* \"system op in a batch?\" */);\n assert(!this.localPaused, 0x29c /* \"we should be processing ops when there is no active batch\" */);\n return;\n }\n\n if (this.currentBatchClientId === undefined && batchMetadata === undefined) {\n assert(!this.localPaused, 0x29d /* \"we should be processing ops when there is no active batch\" */);\n return;\n }\n\n // If the client ID changes then we can move the pause point. If it stayed the same then we need to check.\n // If batchMetadata is not undefined then if it's true we've begun a new batch - if false we've ended\n // the previous one\n if (this.currentBatchClientId !== undefined || batchMetadata === false) {\n if (this.currentBatchClientId !== message.clientId) {\n // \"Batch not closed, yet message from another client!\"\n throw new DataCorruptionError(\n \"OpBatchIncomplete\",\n {\n runtimeVersion: pkgVersion,\n batchClientId: this.currentBatchClientId,\n ...extractSafePropertiesFromMessage(message),\n });\n }\n }\n\n // The queue is\n // 1. paused only when the next message to be processed is the beginning of a batch. Done in two places:\n // - in afterOpProcessing() - processing ops until reaching start of incomplete batch\n // - here (batchMetadata == false below), when queue was empty and start of batch showed up.\n // 2. resumed when batch end comes in (batchMetadata === true case below)\n\n if (batchMetadata) {\n assert(this.currentBatchClientId === undefined, 0x29e /* \"there can't be active batch\" */);\n assert(!this.localPaused, 0x29f /* \"we should be processing ops when there is no active batch\" */);\n this.pauseSequenceNumber = message.sequenceNumber;\n this.currentBatchClientId = message.clientId;\n // Start of the batch\n // Only pause processing if queue has no other ops!\n // If there are any other ops in the queue, processing will be stopped when they are processed!\n if (this.deltaManager.inbound.length === 1) {\n this.pauseQueue();\n }\n } else if (batchMetadata === false) {\n assert(this.pauseSequenceNumber !== undefined, 0x2a0 /* \"batch presence was validated above\" */);\n // Batch is complete, we can process it!\n this.resumeQueue(this.pauseSequenceNumber, message);\n this.pauseSequenceNumber = undefined;\n this.currentBatchClientId = undefined;\n } else {\n // Continuation of current batch. Do nothing\n assert(this.currentBatchClientId !== undefined, 0x2a1 /* \"logic error\" */);\n }\n }\n}\n"]}
|
|
@@ -255,7 +255,7 @@ class SummaryCollection extends common_utils_1.TypedEventEmitter {
|
|
|
255
255
|
// from. i.e. initialSequenceNumber > summarySequenceNumber.
|
|
256
256
|
// We really don't care about it for now, since it is older than
|
|
257
257
|
// the one we loaded from.
|
|
258
|
-
if (seq
|
|
258
|
+
if (seq > this.deltaManager.initialSequenceNumber) {
|
|
259
259
|
// Potential causes for it to be later than our initialSequenceNumber
|
|
260
260
|
// are that the summaryOp was nacked then acked, double-acked, or
|
|
261
261
|
// the summarySequenceNumber is incorrect.
|