@fluidframework/container-runtime 2.0.0-internal.1.4.1 → 2.0.0-internal.2.0.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/dist/batchManager.d.ts +2 -3
- package/dist/batchManager.d.ts.map +1 -1
- package/dist/batchManager.js +3 -8
- package/dist/batchManager.js.map +1 -1
- package/dist/containerRuntime.d.ts +43 -16
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +107 -83
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStoreContext.d.ts +4 -20
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +17 -47
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/dataStores.d.ts +2 -5
- package/dist/dataStores.d.ts.map +1 -1
- package/dist/dataStores.js +3 -11
- package/dist/dataStores.js.map +1 -1
- package/dist/garbageCollection.d.ts +1 -10
- package/dist/garbageCollection.d.ts.map +1 -1
- package/dist/garbageCollection.js +43 -51
- package/dist/garbageCollection.js.map +1 -1
- package/dist/gcSweepReadyUsageDetection.d.ts.map +1 -1
- package/dist/gcSweepReadyUsageDetection.js +3 -12
- package/dist/gcSweepReadyUsageDetection.js.map +1 -1
- package/dist/index.d.ts +3 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -5
- package/dist/index.js.map +1 -1
- 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 +6 -26
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +42 -62
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/scheduleManager.js.map +1 -1
- package/dist/summarizer.js +7 -2
- package/dist/summarizer.js.map +1 -1
- package/dist/summarizerTypes.d.ts +19 -2
- package/dist/summarizerTypes.d.ts.map +1 -1
- package/dist/summarizerTypes.js.map +1 -1
- package/dist/summaryFormat.d.ts +4 -2
- package/dist/summaryFormat.d.ts.map +1 -1
- package/dist/summaryFormat.js.map +1 -1
- package/dist/summaryManager.d.ts.map +1 -1
- package/dist/summaryManager.js +10 -6
- package/dist/summaryManager.js.map +1 -1
- package/lib/batchManager.d.ts +2 -3
- package/lib/batchManager.d.ts.map +1 -1
- package/lib/batchManager.js +3 -8
- package/lib/batchManager.js.map +1 -1
- package/lib/containerRuntime.d.ts +43 -16
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +108 -84
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStoreContext.d.ts +4 -20
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +18 -48
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/dataStores.d.ts +2 -5
- package/lib/dataStores.d.ts.map +1 -1
- package/lib/dataStores.js +3 -11
- package/lib/dataStores.js.map +1 -1
- package/lib/garbageCollection.d.ts +1 -10
- package/lib/garbageCollection.d.ts.map +1 -1
- package/lib/garbageCollection.js +42 -50
- package/lib/garbageCollection.js.map +1 -1
- package/lib/gcSweepReadyUsageDetection.d.ts.map +1 -1
- package/lib/gcSweepReadyUsageDetection.js +3 -12
- package/lib/gcSweepReadyUsageDetection.js.map +1 -1
- package/lib/index.d.ts +3 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +0 -2
- package/lib/index.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 +6 -26
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +42 -62
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/scheduleManager.js.map +1 -1
- package/lib/summarizer.js +7 -2
- package/lib/summarizer.js.map +1 -1
- package/lib/summarizerTypes.d.ts +19 -2
- package/lib/summarizerTypes.d.ts.map +1 -1
- package/lib/summarizerTypes.js.map +1 -1
- package/lib/summaryFormat.d.ts +4 -2
- package/lib/summaryFormat.d.ts.map +1 -1
- package/lib/summaryFormat.js.map +1 -1
- package/lib/summaryManager.d.ts.map +1 -1
- package/lib/summaryManager.js +10 -6
- package/lib/summaryManager.js.map +1 -1
- package/package.json +40 -38
- package/src/batchManager.ts +7 -11
- package/src/containerRuntime.ts +149 -102
- package/src/dataStoreContext.ts +20 -62
- package/src/dataStores.ts +2 -10
- package/src/garbageCollection.ts +45 -55
- package/src/gcSweepReadyUsageDetection.ts +2 -10
- package/src/index.ts +2 -3
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +57 -96
- package/src/scheduleManager.ts +1 -0
- package/src/summarizer.ts +6 -6
- package/src/summarizerTypes.ts +20 -7
- package/src/summaryFormat.ts +4 -2
- package/src/summaryManager.ts +18 -7
package/lib/index.d.ts
CHANGED
|
@@ -2,14 +2,12 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
-
export { ContainerMessageType, IChunkedOp, ContainerRuntimeMessage, IGCRuntimeOptions, ISummaryRuntimeOptions, ISummaryBaseConfiguration, ISummaryConfigurationHeuristics, ISummaryConfigurationDisableSummarizer, ISummaryConfigurationDisableHeuristics, IContainerRuntimeOptions, IRootSummaryTreeWithStats, isRuntimeMessage, RuntimeMessage, unpackRuntimeMessage, agentSchedulerId, ContainerRuntime, RuntimeHeaders, ISummaryConfiguration, DefaultSummaryConfiguration, } from "./containerRuntime";
|
|
6
|
-
export { DeltaScheduler } from "./deltaScheduler";
|
|
5
|
+
export { ContainerMessageType, IChunkedOp, ContainerRuntimeMessage, IGCRuntimeOptions, ISummaryRuntimeOptions, ISummaryBaseConfiguration, ISummaryConfigurationHeuristics, ISummaryConfigurationDisableSummarizer, ISummaryConfigurationDisableHeuristics, IContainerRuntimeOptions, IRootSummaryTreeWithStats, isRuntimeMessage, RuntimeMessage, unpackRuntimeMessage, agentSchedulerId, ContainerRuntime, RuntimeHeaders, ISummaryConfiguration, DefaultSummaryConfiguration, ICompressionRuntimeOptions, } from "./containerRuntime";
|
|
7
6
|
export { FluidDataStoreRegistry } from "./dataStoreRegistry";
|
|
8
7
|
export { gcBlobPrefix, gcTreeKey, IGarbageCollectionRuntime, IGCStats, } from "./garbageCollection";
|
|
9
|
-
export { IPendingFlush,
|
|
10
|
-
export { ScheduleManager } from "./scheduleManager";
|
|
8
|
+
export { IPendingFlush, IPendingLocalState, IPendingMessage, IPendingState, } from "./pendingStateManager";
|
|
11
9
|
export { Summarizer } from "./summarizer";
|
|
12
|
-
export { EnqueueSummarizeResult, IAckSummaryResult, IBaseSummarizeResult, IBroadcastSummaryResult, ICancellationToken, IConnectableRuntime, IEnqueueSummarizeOptions, IGenerateSummaryTreeResult, IGeneratedSummaryStats, INackSummaryResult, IOnDemandSummarizeOptions, IProvideSummarizer, ISubmitSummaryOpResult, ISubmitSummaryOptions, ISummarizeOptions, ISummarizeResults, ISummarizer, ISummarizerEvents, ISummarizerInternalsProvider, ISummarizerRuntime, ISummarizingWarning, ISummaryCancellationToken, IUploadSummaryResult, SubmitSummaryResult, SummarizeResultPart, SummarizerStopReason, } from "./summarizerTypes";
|
|
10
|
+
export { EnqueueSummarizeResult, IAckSummaryResult, IBaseSummarizeResult, IBroadcastSummaryResult, ICancellationToken, IConnectableRuntime, IEnqueueSummarizeOptions, IGenerateSummaryTreeResult, IGeneratedSummaryStats, INackSummaryResult, IOnDemandSummarizeOptions, IProvideSummarizer, IRefreshSummaryAckOptions, ISubmitSummaryOpResult, ISubmitSummaryOptions, ISummarizeOptions, ISummarizeResults, ISummarizer, ISummarizerEvents, ISummarizerInternalsProvider, ISummarizerRuntime, ISummarizingWarning, ISummaryCancellationToken, IUploadSummaryResult, SubmitSummaryResult, SummarizeResultPart, SummarizerStopReason, } from "./summarizerTypes";
|
|
13
11
|
export { IAckedSummary, IClientSummaryWatcher, ISummary, ISummaryCollectionOpEvents, ISummaryAckMessage, ISummaryNackMessage, ISummaryOpMessage, OpActionEventListener, OpActionEventName, SummaryCollection, } from "./summaryCollection";
|
|
14
12
|
export { ICancellableSummarizerController, neverCancelledSummaryToken } from "./runWhileConnectedCoordinator";
|
|
15
13
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACH,oBAAoB,EACpB,UAAU,EACV,uBAAuB,EACvB,iBAAiB,EACjB,sBAAsB,EACtB,yBAAyB,EACzB,+BAA+B,EAC/B,sCAAsC,EACtC,sCAAsC,EACtC,wBAAwB,EACxB,yBAAyB,EACzB,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,2BAA2B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACH,oBAAoB,EACpB,UAAU,EACV,uBAAuB,EACvB,iBAAiB,EACjB,sBAAsB,EACtB,yBAAyB,EACzB,+BAA+B,EAC/B,sCAAsC,EACtC,sCAAsC,EACtC,wBAAwB,EACxB,yBAAyB,EACzB,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,qBAAqB,EACrB,2BAA2B,EAC3B,0BAA0B,GAC7B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EACH,YAAY,EACZ,SAAS,EACT,yBAAyB,EACzB,QAAQ,GACX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACH,aAAa,EACb,kBAAkB,EAClB,eAAe,EACf,aAAa,GAChB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EACH,sBAAsB,EACtB,iBAAiB,EACjB,oBAAoB,EACpB,uBAAuB,EACvB,kBAAkB,EAClB,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAC1B,sBAAsB,EACtB,kBAAkB,EAClB,yBAAyB,EACzB,kBAAkB,EAClB,yBAAyB,EACzB,sBAAsB,EACtB,qBAAqB,EACrB,iBAAiB,EACjB,iBAAiB,EACjB,WAAW,EACX,iBAAiB,EACjB,4BAA4B,EAC5B,kBAAkB,EAClB,mBAAmB,EACnB,yBAAyB,EACzB,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,GACvB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACH,aAAa,EACb,qBAAqB,EACrB,QAAQ,EACR,0BAA0B,EAC1B,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,EACjB,qBAAqB,EACrB,iBAAiB,EACjB,iBAAiB,GACpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,gCAAgC,EAAE,0BAA0B,EAAE,MAAM,gCAAgC,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -3,10 +3,8 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
export { ContainerMessageType, isRuntimeMessage, RuntimeMessage, unpackRuntimeMessage, agentSchedulerId, ContainerRuntime, RuntimeHeaders, DefaultSummaryConfiguration, } from "./containerRuntime";
|
|
6
|
-
export { DeltaScheduler } from "./deltaScheduler";
|
|
7
6
|
export { FluidDataStoreRegistry } from "./dataStoreRegistry";
|
|
8
7
|
export { gcBlobPrefix, gcTreeKey, } from "./garbageCollection";
|
|
9
|
-
export { ScheduleManager } from "./scheduleManager";
|
|
10
8
|
export { Summarizer } from "./summarizer";
|
|
11
9
|
export { ISummarizer, } from "./summarizerTypes";
|
|
12
10
|
export { SummaryCollection, } from "./summaryCollection";
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACH,oBAAoB,EAWpB,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EAEd,2BAA2B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACH,oBAAoB,EAWpB,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EAEd,2BAA2B,GAE9B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EACH,YAAY,EACZ,SAAS,GAGZ,MAAM,qBAAqB,CAAC;AAO7B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAkBH,WAAW,GAUd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAUH,iBAAiB,GACpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAoC,0BAA0B,EAAE,MAAM,gCAAgC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport {\n ContainerMessageType,\n IChunkedOp,\n ContainerRuntimeMessage,\n IGCRuntimeOptions,\n ISummaryRuntimeOptions,\n ISummaryBaseConfiguration,\n ISummaryConfigurationHeuristics,\n ISummaryConfigurationDisableSummarizer,\n ISummaryConfigurationDisableHeuristics,\n IContainerRuntimeOptions,\n IRootSummaryTreeWithStats,\n isRuntimeMessage,\n RuntimeMessage,\n unpackRuntimeMessage,\n agentSchedulerId,\n ContainerRuntime,\n RuntimeHeaders,\n ISummaryConfiguration,\n DefaultSummaryConfiguration,\n ICompressionRuntimeOptions,\n} from \"./containerRuntime\";\nexport { FluidDataStoreRegistry } from \"./dataStoreRegistry\";\nexport {\n gcBlobPrefix,\n gcTreeKey,\n IGarbageCollectionRuntime,\n IGCStats,\n} from \"./garbageCollection\";\nexport {\n IPendingFlush,\n IPendingLocalState,\n IPendingMessage,\n IPendingState,\n} from \"./pendingStateManager\";\nexport { Summarizer } from \"./summarizer\";\nexport {\n EnqueueSummarizeResult,\n IAckSummaryResult,\n IBaseSummarizeResult,\n IBroadcastSummaryResult,\n ICancellationToken,\n IConnectableRuntime,\n IEnqueueSummarizeOptions,\n IGenerateSummaryTreeResult,\n IGeneratedSummaryStats,\n INackSummaryResult,\n IOnDemandSummarizeOptions,\n IProvideSummarizer,\n IRefreshSummaryAckOptions,\n ISubmitSummaryOpResult,\n ISubmitSummaryOptions,\n ISummarizeOptions,\n ISummarizeResults,\n ISummarizer,\n ISummarizerEvents,\n ISummarizerInternalsProvider,\n ISummarizerRuntime,\n ISummarizingWarning,\n ISummaryCancellationToken,\n IUploadSummaryResult,\n SubmitSummaryResult,\n SummarizeResultPart,\n SummarizerStopReason,\n} from \"./summarizerTypes\";\nexport {\n IAckedSummary,\n IClientSummaryWatcher,\n ISummary,\n ISummaryCollectionOpEvents,\n ISummaryAckMessage,\n ISummaryNackMessage,\n ISummaryOpMessage,\n OpActionEventListener,\n OpActionEventName,\n SummaryCollection,\n} from \"./summaryCollection\";\nexport { ICancellableSummarizerController, neverCancelledSummaryToken } from \"./runWhileConnectedCoordinator\";\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 = "2.0.0-internal.
|
|
8
|
+
export declare const pkgVersion = "2.0.0-internal.2.0.0";
|
|
9
9
|
//# sourceMappingURL=packageVersion.d.ts.map
|
package/lib/packageVersion.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
|
|
6
6
|
*/
|
|
7
7
|
export const pkgName = "@fluidframework/container-runtime";
|
|
8
|
-
export const pkgVersion = "2.0.0-internal.
|
|
8
|
+
export const pkgVersion = "2.0.0-internal.2.0.0";
|
|
9
9
|
//# sourceMappingURL=packageVersion.js.map
|
|
@@ -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,sBAAsB,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 = \"2.0.0-internal.
|
|
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,sBAAsB,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 = \"2.0.0-internal.2.0.0\";\n"]}
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
import { IDisposable } from "@fluidframework/common-definitions";
|
|
6
6
|
import { ICriticalContainerError } from "@fluidframework/container-definitions";
|
|
7
7
|
import { ISequencedDocumentMessage } from "@fluidframework/protocol-definitions";
|
|
8
|
-
import { FlushMode } from "@fluidframework/runtime-definitions";
|
|
9
8
|
import { ContainerMessageType } from "./containerRuntime";
|
|
10
9
|
/**
|
|
11
10
|
* This represents a message that has been submitted and is added to the pending queue when `submit` is called on the
|
|
@@ -20,14 +19,6 @@ export interface IPendingMessage {
|
|
|
20
19
|
localOpMetadata: unknown;
|
|
21
20
|
opMetadata: Record<string, unknown> | undefined;
|
|
22
21
|
}
|
|
23
|
-
/**
|
|
24
|
-
* This represents a FlushMode update and is added to the pending queue when `setFlushMode` is called on the
|
|
25
|
-
* ContainerRuntime and the FlushMode changes.
|
|
26
|
-
*/
|
|
27
|
-
export interface IPendingFlushMode {
|
|
28
|
-
type: "flushMode";
|
|
29
|
-
flushMode: FlushMode;
|
|
30
|
-
}
|
|
31
22
|
/**
|
|
32
23
|
* This represents an explicit flush call and is added to the pending queue when flush is called on the ContainerRuntime
|
|
33
24
|
* to flush pending messages.
|
|
@@ -35,7 +26,7 @@ export interface IPendingFlushMode {
|
|
|
35
26
|
export interface IPendingFlush {
|
|
36
27
|
type: "flush";
|
|
37
28
|
}
|
|
38
|
-
export declare type IPendingState = IPendingMessage |
|
|
29
|
+
export declare type IPendingState = IPendingMessage | IPendingFlush;
|
|
39
30
|
export interface IPendingLocalState {
|
|
40
31
|
/**
|
|
41
32
|
* list of pending states, including ops and batch information
|
|
@@ -45,18 +36,18 @@ export interface IPendingLocalState {
|
|
|
45
36
|
export interface IRuntimeStateHandler {
|
|
46
37
|
connected(): boolean;
|
|
47
38
|
clientId(): string | undefined;
|
|
48
|
-
flushMode(): FlushMode;
|
|
49
|
-
setFlushMode(mode: FlushMode): void;
|
|
50
39
|
close(error?: ICriticalContainerError): void;
|
|
51
40
|
applyStashedOp: (type: ContainerMessageType, content: ISequencedDocumentMessage) => Promise<unknown>;
|
|
52
41
|
flush(): void;
|
|
53
42
|
reSubmit(type: ContainerMessageType, content: any, localOpMetadata: unknown, opMetadata: Record<string, unknown> | undefined): void;
|
|
43
|
+
rollback(type: ContainerMessageType, content: any, localOpMetadata: unknown): void;
|
|
44
|
+
orderSequentially(callback: () => void): void;
|
|
54
45
|
}
|
|
55
46
|
/**
|
|
56
47
|
* PendingStateManager is responsible for maintaining the messages that have not been sent or have not yet been
|
|
57
48
|
* acknowledged by the server. It also maintains the batch information for both automatically and manually flushed
|
|
58
49
|
* batches along with the messages.
|
|
59
|
-
* When the Container reconnects, it replays the pending states, which includes
|
|
50
|
+
* When the Container reconnects, it replays the pending states, which includes manual flushing
|
|
60
51
|
* of messages and triggering resubmission of unacked ops.
|
|
61
52
|
*
|
|
62
53
|
* It verifies that all the ops are acked, are received in the right order and batch information is correct.
|
|
@@ -70,12 +61,6 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
70
61
|
get pendingMessagesCount(): number;
|
|
71
62
|
private isProcessingBatch;
|
|
72
63
|
private pendingBatchBeginMessage;
|
|
73
|
-
/**
|
|
74
|
-
* This tracks the flush mode for the next message in the pending state queue. When replaying messages, we need to
|
|
75
|
-
* first set the flush mode to this value and then send ops. It is important to do this info because the flush
|
|
76
|
-
* mode could have been updated.
|
|
77
|
-
*/
|
|
78
|
-
private flushModeForNextMessage;
|
|
79
64
|
private clientId;
|
|
80
65
|
/**
|
|
81
66
|
* Called to check if there are any pending messages in the pending state queue.
|
|
@@ -83,7 +68,7 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
83
68
|
*/
|
|
84
69
|
hasPendingMessages(): boolean;
|
|
85
70
|
getLocalState(): IPendingLocalState | undefined;
|
|
86
|
-
constructor(stateHandler: IRuntimeStateHandler,
|
|
71
|
+
constructor(stateHandler: IRuntimeStateHandler, initialLocalState: IPendingLocalState | undefined);
|
|
87
72
|
get disposed(): boolean;
|
|
88
73
|
readonly dispose: () => void;
|
|
89
74
|
/**
|
|
@@ -95,11 +80,6 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
95
80
|
* @param localOpMetadata - The local metadata associated with the message.
|
|
96
81
|
*/
|
|
97
82
|
onSubmitMessage(type: ContainerMessageType, clientSequenceNumber: number, referenceSequenceNumber: number, content: any, localOpMetadata: unknown, opMetadata: Record<string, unknown> | undefined): void;
|
|
98
|
-
/**
|
|
99
|
-
* Called when the FlushMode is updated. Adds the FlushMode to the pending state queue.
|
|
100
|
-
* @param flushMode - The flushMode that was updated.
|
|
101
|
-
*/
|
|
102
|
-
onFlushModeUpdated(flushMode: FlushMode): void;
|
|
103
83
|
/**
|
|
104
84
|
* Called when flush() is called on the ContainerRuntime to manually flush messages.
|
|
105
85
|
*/
|
|
@@ -131,7 +111,7 @@ export declare class PendingStateManager implements IDisposable {
|
|
|
131
111
|
private peekNextPendingState;
|
|
132
112
|
/**
|
|
133
113
|
* Called when the Container's connection state changes. If the Container gets connected, it replays all the pending
|
|
134
|
-
* states in its queue. This includes
|
|
114
|
+
* states in its queue. This includes triggering resubmission of unacked ops.
|
|
135
115
|
*/
|
|
136
116
|
replayPendingStates(): void;
|
|
137
117
|
}
|
|
@@ -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;AAEjE,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAEhF,OAAO,EACH,yBAAyB,EAC5B,MAAM,sCAAsC,CAAC;
|
|
1
|
+
{"version":3,"file":"pendingStateManager.d.ts","sourceRoot":"","sources":["../src/pendingStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAC;AAEjE,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAEhF,OAAO,EACH,yBAAyB,EAC5B,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAG1D;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC5B,IA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CAAC,aAAa,CAA8B;IAC5D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAuB;IACrD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAGzB;IAGH,OAAO,CAAC,qBAAqB,CAAa;IAC1C,IAAW,oBAAoB,IAAI,MAAM,CAExC;IAGD,OAAO,CAAC,iBAAiB,CAAkB;IAI3C,OAAO,CAAC,wBAAwB,CAAwC;IAExE,OAAO,CAAC,QAAQ,CAAqB;IAErC;;;OAGG;IACI,kBAAkB,IAAI,OAAO;IAI7B,aAAa,IAAI,kBAAkB,GAAG,SAAS;gBAajC,YAAY,EAAE,oBAAoB,EACnD,iBAAiB,EAAE,kBAAkB,GAAG,SAAS;IAKrD,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;;OAEG;IACI,OAAO;IAWd;;;OAGG;IACU,iBAAiB,CAAC,MAAM,CAAC,EAAE,MAAM;IA8B9C;;;;OAIG;IACI,0BAA0B,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO;IAiC9E;;;OAGG;IACH,OAAO,CAAC,sBAAsB;IA4B9B;;;OAGG;IACH,OAAO,CAAC,oBAAoB;IAgD5B;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAM5B;;;OAGG;IACI,mBAAmB;CAoF7B"}
|
|
@@ -4,20 +4,19 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { assert, Lazy } from "@fluidframework/common-utils";
|
|
6
6
|
import { DataProcessingError } from "@fluidframework/container-utils";
|
|
7
|
-
import { FlushMode } from "@fluidframework/runtime-definitions";
|
|
8
7
|
import Deque from "double-ended-queue";
|
|
9
8
|
import { pkgVersion } from "./packageVersion";
|
|
10
9
|
/**
|
|
11
10
|
* PendingStateManager is responsible for maintaining the messages that have not been sent or have not yet been
|
|
12
11
|
* acknowledged by the server. It also maintains the batch information for both automatically and manually flushed
|
|
13
12
|
* batches along with the messages.
|
|
14
|
-
* When the Container reconnects, it replays the pending states, which includes
|
|
13
|
+
* When the Container reconnects, it replays the pending states, which includes manual flushing
|
|
15
14
|
* of messages and triggering resubmission of unacked ops.
|
|
16
15
|
*
|
|
17
16
|
* It verifies that all the ops are acked, are received in the right order and batch information is correct.
|
|
18
17
|
*/
|
|
19
18
|
export class PendingStateManager {
|
|
20
|
-
constructor(stateHandler,
|
|
19
|
+
constructor(stateHandler, initialLocalState) {
|
|
21
20
|
var _a;
|
|
22
21
|
this.stateHandler = stateHandler;
|
|
23
22
|
this.pendingStates = new Deque();
|
|
@@ -31,8 +30,6 @@ export class PendingStateManager {
|
|
|
31
30
|
this.isProcessingBatch = false;
|
|
32
31
|
this.dispose = () => this.disposeOnce.value;
|
|
33
32
|
this.initialStates = new Deque((_a = initialLocalState === null || initialLocalState === void 0 ? void 0 : initialLocalState.pendingStates) !== null && _a !== void 0 ? _a : []);
|
|
34
|
-
this.flushModeForNextMessage = initialFlushMode;
|
|
35
|
-
this.onFlushModeUpdated(initialFlushMode);
|
|
36
33
|
}
|
|
37
34
|
get pendingMessagesCount() {
|
|
38
35
|
return this._pendingMessagesCount;
|
|
@@ -77,22 +74,10 @@ export class PendingStateManager {
|
|
|
77
74
|
this.pendingStates.push(pendingMessage);
|
|
78
75
|
this._pendingMessagesCount++;
|
|
79
76
|
}
|
|
80
|
-
/**
|
|
81
|
-
* Called when the FlushMode is updated. Adds the FlushMode to the pending state queue.
|
|
82
|
-
* @param flushMode - The flushMode that was updated.
|
|
83
|
-
*/
|
|
84
|
-
onFlushModeUpdated(flushMode) {
|
|
85
|
-
this.pendingStates.push({ type: "flushMode", flushMode });
|
|
86
|
-
}
|
|
87
77
|
/**
|
|
88
78
|
* Called when flush() is called on the ContainerRuntime to manually flush messages.
|
|
89
79
|
*/
|
|
90
80
|
onFlush() {
|
|
91
|
-
// If the FlushMode is Immediate, we don't need to track an explicit flush call because every message is
|
|
92
|
-
// automatically flushed. So, flush is a no-op.
|
|
93
|
-
if (this.stateHandler.flushMode() === FlushMode.Immediate) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
81
|
// If the previous state is not a message, flush is a no-op.
|
|
97
82
|
const previousState = this.pendingStates.peekBack();
|
|
98
83
|
if ((previousState === null || previousState === void 0 ? void 0 : previousState.type) !== "message") {
|
|
@@ -163,10 +148,7 @@ export class PendingStateManager {
|
|
|
163
148
|
* @param message - The message that is being processed.
|
|
164
149
|
*/
|
|
165
150
|
maybeProcessBatchBegin(message) {
|
|
166
|
-
|
|
167
|
-
let pendingFlushMode;
|
|
168
|
-
// Tracks whether a flush was called before this message was sent.
|
|
169
|
-
let pendingFlush = false;
|
|
151
|
+
var _a;
|
|
170
152
|
/**
|
|
171
153
|
* We are checking if the next message is the start of a batch. It can happen in the following scenarios:
|
|
172
154
|
*
|
|
@@ -178,31 +160,11 @@ export class PendingStateManager {
|
|
|
178
160
|
* Keep reading pending states from the queue until we encounter a message. It's possible that the FlushMode was
|
|
179
161
|
* updated a bunch of times without sending any messages.
|
|
180
162
|
*/
|
|
181
|
-
|
|
182
|
-
while (nextPendingState.type !== "message") {
|
|
183
|
-
if (nextPendingState.type === "flushMode") {
|
|
184
|
-
pendingFlushMode = nextPendingState.flushMode;
|
|
185
|
-
}
|
|
186
|
-
if (nextPendingState.type === "flush") {
|
|
187
|
-
pendingFlush = true;
|
|
188
|
-
}
|
|
163
|
+
while (this.peekNextPendingState().type !== "message") {
|
|
189
164
|
this.pendingStates.shift();
|
|
190
|
-
nextPendingState = this.peekNextPendingState();
|
|
191
|
-
}
|
|
192
|
-
if (pendingFlushMode !== undefined) {
|
|
193
|
-
this.flushModeForNextMessage = pendingFlushMode;
|
|
194
|
-
}
|
|
195
|
-
// If the FlushMode was set to Immediate before this message was sent, this message won't be a batch message
|
|
196
|
-
// because in Immediate mode, every message is flushed individually.
|
|
197
|
-
if (pendingFlushMode === FlushMode.Immediate) {
|
|
198
|
-
return;
|
|
199
165
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
* was an explicit flush call. Note that a flush call is tracked only in TurnBased mode and it indicates the end
|
|
203
|
-
* of one batch and beginning of another.
|
|
204
|
-
*/
|
|
205
|
-
if (pendingFlushMode === FlushMode.TurnBased || pendingFlush) {
|
|
166
|
+
// This message is the first in a batch if the "batch" property on the metadata is set to true
|
|
167
|
+
if ((_a = message.metadata) === null || _a === void 0 ? void 0 : _a.batch) {
|
|
206
168
|
// We should not already be processing a batch and there should be no pending batch begin message.
|
|
207
169
|
assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined, 0x16b /* "The pending batch state indicates we are already processing a batch" */);
|
|
208
170
|
// Set the pending batch state indicating we have started processing a batch.
|
|
@@ -223,12 +185,6 @@ export class PendingStateManager {
|
|
|
223
185
|
if (nextPendingState.type === "message") {
|
|
224
186
|
return;
|
|
225
187
|
}
|
|
226
|
-
/**
|
|
227
|
-
* We are in the middle of processing a batch. The batch ends when we see an explicit flush. We should never see
|
|
228
|
-
* a FlushMode before flush. This is true because we track batches only when FlushMode is TurnBased and in this
|
|
229
|
-
* mode, a batch ends either by calling flush or by changing the mode to Immediate which also triggers a flush.
|
|
230
|
-
*/
|
|
231
|
-
assert(nextPendingState.type !== "flushMode", 0x2bd /* "We should not see a pending FlushMode until we see a flush when processing a batch" */);
|
|
232
188
|
// There should be a pending batch begin message.
|
|
233
189
|
assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* "There is no pending batch begin message" */);
|
|
234
190
|
// Get the batch begin metadata from the first message in the batch.
|
|
@@ -270,9 +226,10 @@ export class PendingStateManager {
|
|
|
270
226
|
}
|
|
271
227
|
/**
|
|
272
228
|
* Called when the Container's connection state changes. If the Container gets connected, it replays all the pending
|
|
273
|
-
* states in its queue. This includes
|
|
229
|
+
* states in its queue. This includes triggering resubmission of unacked ops.
|
|
274
230
|
*/
|
|
275
231
|
replayPendingStates() {
|
|
232
|
+
var _a, _b;
|
|
276
233
|
assert(this.stateHandler.connected(), 0x172 /* "The connection state is not consistent with the runtime" */);
|
|
277
234
|
// This assert suggests we are about to send same ops twice, which will result in data loss.
|
|
278
235
|
assert(this.clientId !== this.stateHandler.clientId(), 0x173 /* "replayPendingStates called twice for same clientId!" */);
|
|
@@ -284,11 +241,7 @@ export class PendingStateManager {
|
|
|
284
241
|
}
|
|
285
242
|
// Reset the pending message count because all these messages will be removed from the queue.
|
|
286
243
|
this._pendingMessagesCount = 0;
|
|
287
|
-
|
|
288
|
-
const savedFlushMode = this.stateHandler.flushMode();
|
|
289
|
-
// Set the flush mode for the next message. This step is important because the flush mode may have been changed
|
|
290
|
-
// after the next pending message was sent.
|
|
291
|
-
this.stateHandler.setFlushMode(this.flushModeForNextMessage);
|
|
244
|
+
const messageBatchQueue = new Deque();
|
|
292
245
|
// Process exactly `pendingStatesCount` items in the queue as it represents the number of states that were
|
|
293
246
|
// pending when we connected. This is important because the `reSubmitFn` might add more items in the queue
|
|
294
247
|
// which must not be replayed.
|
|
@@ -297,12 +250,35 @@ export class PendingStateManager {
|
|
|
297
250
|
const pendingState = this.pendingStates.shift();
|
|
298
251
|
switch (pendingState.type) {
|
|
299
252
|
case "message":
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
253
|
+
assert(((_a = pendingState.opMetadata) === null || _a === void 0 ? void 0 : _a.batch) !== false || messageBatchQueue.length > 0, 0x41b /* We cannot process batches in chunks */);
|
|
254
|
+
/**
|
|
255
|
+
* We want to ensure grouped messages get processed in a batch.
|
|
256
|
+
* Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will
|
|
257
|
+
* either receive the whole batch ack or nothing at all.
|
|
258
|
+
*/
|
|
259
|
+
if (messageBatchQueue.length > 0 || ((_b = pendingState.opMetadata) === null || _b === void 0 ? void 0 : _b.batch)) {
|
|
260
|
+
messageBatchQueue.enqueue(pendingState);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
this.stateHandler.reSubmit(pendingState.messageType, pendingState.content, pendingState.localOpMetadata, pendingState.opMetadata);
|
|
264
|
+
}
|
|
304
265
|
break;
|
|
305
266
|
case "flush":
|
|
267
|
+
/**
|
|
268
|
+
* A "flush" call can indicate the end of a batch.
|
|
269
|
+
* We can't rely on the "batch" property in the message metadata as it gets
|
|
270
|
+
* updated elsewhere and it is not the same object instance that gets updated.
|
|
271
|
+
*/
|
|
272
|
+
if (messageBatchQueue.length > 0) {
|
|
273
|
+
this.stateHandler.orderSequentially(() => {
|
|
274
|
+
while (messageBatchQueue.length > 0) {
|
|
275
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
276
|
+
const message = messageBatchQueue.dequeue();
|
|
277
|
+
this.stateHandler.reSubmit(message.messageType, message.content, message.localOpMetadata, message.opMetadata);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
assert(messageBatchQueue.length === 0, 0x41c /* cannot flush in the middle of a batch */);
|
|
306
282
|
this.stateHandler.flush();
|
|
307
283
|
break;
|
|
308
284
|
default:
|
|
@@ -310,8 +286,12 @@ export class PendingStateManager {
|
|
|
310
286
|
}
|
|
311
287
|
pendingStatesCount--;
|
|
312
288
|
}
|
|
313
|
-
//
|
|
314
|
-
|
|
289
|
+
// There are some cases where ops are stashed but not flushed. We need to ensure they are resubmitted
|
|
290
|
+
while (messageBatchQueue.length > 0) {
|
|
291
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
292
|
+
const message = messageBatchQueue.dequeue();
|
|
293
|
+
this.stateHandler.reSubmit(message.messageType, message.content, message.localOpMetadata, message.opMetadata);
|
|
294
|
+
}
|
|
315
295
|
}
|
|
316
296
|
}
|
|
317
297
|
//# sourceMappingURL=pendingStateManager.js.map
|
|
@@ -1 +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B,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","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 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}\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 const firstPendingState = this.initialStates.shift()!;\n this.pendingStates.push(firstPendingState);\n if (firstPendingState.type === \"message\") {\n this._pendingMessagesCount++;\n }\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 assert(this._pendingMessagesCount >= 0, 0x3d6 /* positive */);\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 *\n * 1. The FlushMode was set to TurnBased before this message was sent.\n *\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 * 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.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,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAC;AAE5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAItE,OAAO,KAAK,MAAM,oBAAoB,CAAC;AAEvC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAmD9C;;;;;;;;GAQG;AACH,MAAM,OAAO,mBAAmB;IA2C5B,YACqB,YAAkC,EACnD,iBAAiD;;QADhC,iBAAY,GAAZ,YAAY,CAAsB;QA3CtC,kBAAa,GAAG,IAAI,KAAK,EAAiB,CAAC;QAE3C,gBAAW,GAAG,IAAI,IAAI,CAAO,GAAG,EAAE;YAC/C,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,CAAC;YAC3B,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,8DAA8D;QACtD,0BAAqB,GAAW,CAAC,CAAC;QAK1C,+CAA+C;QACvC,sBAAiB,GAAY,KAAK,CAAC;QAoC3B,YAAO,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC;QAJnD,IAAI,CAAC,aAAa,GAAG,IAAI,KAAK,CAAgB,MAAA,iBAAiB,aAAjB,iBAAiB,uBAAjB,iBAAiB,CAAE,aAAa,mCAAI,EAAE,CAAC,CAAC;IAC1F,CA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AAC,KAAK,EAAE,CAAC;oBAC1B,MAAM;gBACV;oBACI,MAAM;aACb;YACD,kBAAkB,EAAE,CAAC;SACxB;QAED,qGAAqG;QACrG,OAAO,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;YACjC,oEAAoE;YACpE,MAAM,OAAO,GAAG,iBAAiB,CAAC,OAAO,EAAG,CAAC;YAC7C,IAAI,CAAC,YAAY,CAAC,QAAQ,CACtB,OAAO,CAAC,WAAW,EACnB,OAAO,CAAC,OAAO,EACf,OAAO,CAAC,eAAe,EACvB,OAAO,CAAC,UAAU,CAAC,CAAC;SAC3B;IACL,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 { ICriticalContainerError } from \"@fluidframework/container-definitions\";\nimport { DataProcessingError } from \"@fluidframework/container-utils\";\nimport {\n ISequencedDocumentMessage,\n} from \"@fluidframework/protocol-definitions\";\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 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 | 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 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 orderSequentially(callback: () => void): 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 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 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 initialLocalState: IPendingLocalState | undefined,\n ) {\n this.initialStates = new Deque<IPendingState>(initialLocalState?.pendingStates ?? []);\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 flush() is called on the ContainerRuntime to manually flush messages.\n */\n public onFlush() {\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 const firstPendingState = this.initialStates.shift()!;\n this.pendingStates.push(firstPendingState);\n if (firstPendingState.type === \"message\") {\n this._pendingMessagesCount++;\n }\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 assert(this._pendingMessagesCount >= 0, 0x3d6 /* positive */);\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 /**\n * We are checking if the next message is the start of a batch. It can happen in the following scenarios:\n *\n * 1. The FlushMode was set to TurnBased before this message was sent.\n *\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 while (this.peekNextPendingState().type !== \"message\") {\n this.pendingStates.shift();\n }\n\n // This message is the first in a batch if the \"batch\" property on the metadata is set to true\n if (message.metadata?.batch) {\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 // 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 * 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 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 const messageBatchQueue = new Deque<IPendingMessage>();\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 assert(pendingState.opMetadata?.batch !== false || messageBatchQueue.length > 0,\n 0x41b /* We cannot process batches in chunks */);\n /**\n * We want to ensure grouped messages get processed in a batch.\n * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will\n * either receive the whole batch ack or nothing at all.\n */\n if (messageBatchQueue.length > 0 || pendingState.opMetadata?.batch) {\n messageBatchQueue.enqueue(pendingState);\n } else {\n this.stateHandler.reSubmit(\n pendingState.messageType,\n pendingState.content,\n pendingState.localOpMetadata,\n pendingState.opMetadata);\n }\n break;\n case \"flush\":\n /**\n * A \"flush\" call can indicate the end of a batch.\n * We can't rely on the \"batch\" property in the message metadata as it gets\n * updated elsewhere and it is not the same object instance that gets updated.\n */\n if (messageBatchQueue.length > 0) {\n this.stateHandler.orderSequentially(() => {\n while (messageBatchQueue.length > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const message = messageBatchQueue.dequeue()!;\n this.stateHandler.reSubmit(\n message.messageType,\n message.content,\n message.localOpMetadata,\n message.opMetadata);\n }\n });\n }\n assert(messageBatchQueue.length === 0, 0x41c /* cannot flush in the middle of a batch */);\n this.stateHandler.flush();\n break;\n default:\n break;\n }\n pendingStatesCount--;\n }\n\n // There are some cases where ops are stashed but not flushed. We need to ensure they are resubmitted\n while (messageBatchQueue.length > 0) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const message = messageBatchQueue.dequeue()!;\n this.stateHandler.reSubmit(\n message.messageType,\n message.content,\n message.localOpMetadata,\n message.opMetadata);\n }\n }\n}\n"]}
|