@fluidframework/container-runtime 2.0.0-internal.3.0.5 → 2.0.0-internal.3.1.1
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/.eslintrc.js +19 -19
- package/.mocharc.js +2 -2
- package/api-extractor.json +2 -2
- package/dist/batchTracker.d.ts.map +1 -1
- package/dist/batchTracker.js +2 -1
- package/dist/batchTracker.js.map +1 -1
- package/dist/blobManager.d.ts +9 -2
- package/dist/blobManager.d.ts.map +1 -1
- package/dist/blobManager.js +80 -33
- package/dist/blobManager.js.map +1 -1
- package/dist/connectionTelemetry.d.ts.map +1 -1
- package/dist/connectionTelemetry.js +11 -9
- package/dist/connectionTelemetry.js.map +1 -1
- package/dist/containerHandleContext.d.ts.map +1 -1
- package/dist/containerHandleContext.js +3 -1
- package/dist/containerHandleContext.js.map +1 -1
- package/dist/containerRuntime.d.ts +11 -1
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +116 -72
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStore.d.ts.map +1 -1
- package/dist/dataStore.js +11 -9
- package/dist/dataStore.js.map +1 -1
- package/dist/dataStoreContext.d.ts +18 -13
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +68 -55
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/dataStoreContexts.d.ts.map +1 -1
- package/dist/dataStoreContexts.js +7 -3
- package/dist/dataStoreContexts.js.map +1 -1
- package/dist/dataStoreRegistry.d.ts.map +1 -1
- package/dist/dataStoreRegistry.js +3 -1
- package/dist/dataStoreRegistry.js.map +1 -1
- package/dist/dataStores.d.ts +26 -1
- package/dist/dataStores.d.ts.map +1 -1
- package/dist/dataStores.js +103 -18
- package/dist/dataStores.js.map +1 -1
- package/dist/deltaScheduler.d.ts.map +1 -1
- package/dist/deltaScheduler.js +8 -3
- package/dist/deltaScheduler.js.map +1 -1
- package/dist/garbageCollection.d.ts +33 -14
- package/dist/garbageCollection.d.ts.map +1 -1
- package/dist/garbageCollection.js +178 -92
- package/dist/garbageCollection.js.map +1 -1
- package/dist/garbageCollectionConstants.d.ts +1 -0
- package/dist/garbageCollectionConstants.d.ts.map +1 -1
- package/dist/garbageCollectionConstants.js +4 -1
- package/dist/garbageCollectionConstants.js.map +1 -1
- package/dist/garbageCollectionHelpers.d.ts +26 -0
- package/dist/garbageCollectionHelpers.d.ts.map +1 -0
- package/dist/garbageCollectionHelpers.js +45 -0
- package/dist/garbageCollectionHelpers.js.map +1 -0
- package/dist/gcSweepReadyUsageDetection.d.ts +5 -5
- package/dist/gcSweepReadyUsageDetection.d.ts.map +1 -1
- package/dist/gcSweepReadyUsageDetection.js +14 -10
- package/dist/gcSweepReadyUsageDetection.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/opLifecycle/batchManager.d.ts +5 -5
- package/dist/opLifecycle/batchManager.d.ts.map +1 -1
- package/dist/opLifecycle/batchManager.js +19 -12
- package/dist/opLifecycle/batchManager.js.map +1 -1
- package/dist/opLifecycle/definitions.d.ts.map +1 -1
- package/dist/opLifecycle/definitions.js.map +1 -1
- package/dist/opLifecycle/index.d.ts.map +1 -1
- package/dist/opLifecycle/index.js.map +1 -1
- package/dist/opLifecycle/opCompressor.d.ts.map +1 -1
- package/dist/opLifecycle/opCompressor.js.map +1 -1
- package/dist/opLifecycle/opDecompressor.d.ts +0 -4
- package/dist/opLifecycle/opDecompressor.d.ts.map +1 -1
- package/dist/opLifecycle/opDecompressor.js +7 -43
- package/dist/opLifecycle/opDecompressor.js.map +1 -1
- package/dist/opLifecycle/opSplitter.d.ts.map +1 -1
- package/dist/opLifecycle/opSplitter.js +4 -1
- package/dist/opLifecycle/opSplitter.js.map +1 -1
- package/dist/opLifecycle/outbox.d.ts.map +1 -1
- package/dist/opLifecycle/outbox.js +20 -19
- package/dist/opLifecycle/outbox.js.map +1 -1
- package/dist/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
- package/dist/opLifecycle/remoteMessageProcessor.js.map +1 -1
- package/dist/opProperties.d.ts.map +1 -1
- package/dist/opProperties.js +1 -3
- package/dist/opProperties.js.map +1 -1
- package/dist/orderedClientElection.d.ts.map +1 -1
- package/dist/orderedClientElection.js +10 -4
- package/dist/orderedClientElection.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 +7 -0
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +7 -4
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/runWhileConnectedCoordinator.d.ts.map +1 -1
- package/dist/runWhileConnectedCoordinator.js.map +1 -1
- package/dist/runningSummarizer.d.ts.map +1 -1
- package/dist/runningSummarizer.js +34 -21
- package/dist/runningSummarizer.js.map +1 -1
- package/dist/scheduleManager.d.ts.map +1 -1
- package/dist/scheduleManager.js +3 -2
- package/dist/scheduleManager.js.map +1 -1
- package/dist/serializedSnapshotStorage.d.ts +2 -2
- package/dist/serializedSnapshotStorage.d.ts.map +1 -1
- package/dist/serializedSnapshotStorage.js +5 -3
- package/dist/serializedSnapshotStorage.js.map +1 -1
- package/dist/summarizer.d.ts +2 -2
- package/dist/summarizer.d.ts.map +1 -1
- package/dist/summarizer.js +37 -17
- package/dist/summarizer.js.map +1 -1
- package/dist/summarizerClientElection.d.ts.map +1 -1
- package/dist/summarizerClientElection.js.map +1 -1
- package/dist/summarizerHandle.d.ts.map +1 -1
- package/dist/summarizerHandle.js.map +1 -1
- package/dist/summarizerHeuristics.d.ts.map +1 -1
- package/dist/summarizerHeuristics.js +6 -9
- package/dist/summarizerHeuristics.js.map +1 -1
- package/dist/summarizerTypes.d.ts +21 -21
- package/dist/summarizerTypes.d.ts.map +1 -1
- package/dist/summarizerTypes.js.map +1 -1
- package/dist/summaryCollection.d.ts.map +1 -1
- package/dist/summaryCollection.js +18 -8
- package/dist/summaryCollection.js.map +1 -1
- package/dist/summaryFormat.d.ts +5 -2
- package/dist/summaryFormat.d.ts.map +1 -1
- package/dist/summaryFormat.js +18 -10
- package/dist/summaryFormat.js.map +1 -1
- package/dist/summaryGenerator.d.ts.map +1 -1
- package/dist/summaryGenerator.js +35 -16
- package/dist/summaryGenerator.js.map +1 -1
- package/dist/summaryManager.d.ts.map +1 -1
- package/dist/summaryManager.js +21 -9
- package/dist/summaryManager.js.map +1 -1
- package/dist/throttler.d.ts +2 -2
- package/dist/throttler.d.ts.map +1 -1
- package/dist/throttler.js +4 -4
- package/dist/throttler.js.map +1 -1
- package/garbageCollection.md +15 -2
- package/lib/batchTracker.d.ts.map +1 -1
- package/lib/batchTracker.js +2 -1
- package/lib/batchTracker.js.map +1 -1
- package/lib/blobManager.d.ts +9 -2
- package/lib/blobManager.d.ts.map +1 -1
- package/lib/blobManager.js +82 -35
- package/lib/blobManager.js.map +1 -1
- package/lib/connectionTelemetry.d.ts.map +1 -1
- package/lib/connectionTelemetry.js +11 -9
- package/lib/connectionTelemetry.js.map +1 -1
- package/lib/containerHandleContext.d.ts.map +1 -1
- package/lib/containerHandleContext.js +3 -1
- package/lib/containerHandleContext.js.map +1 -1
- package/lib/containerRuntime.d.ts +11 -1
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +122 -78
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStore.d.ts.map +1 -1
- package/lib/dataStore.js +11 -9
- package/lib/dataStore.js.map +1 -1
- package/lib/dataStoreContext.d.ts +18 -13
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +71 -58
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/dataStoreContexts.d.ts.map +1 -1
- package/lib/dataStoreContexts.js +7 -3
- package/lib/dataStoreContexts.js.map +1 -1
- package/lib/dataStoreRegistry.d.ts.map +1 -1
- package/lib/dataStoreRegistry.js +3 -1
- package/lib/dataStoreRegistry.js.map +1 -1
- package/lib/dataStores.d.ts +26 -1
- package/lib/dataStores.d.ts.map +1 -1
- package/lib/dataStores.js +109 -24
- package/lib/dataStores.js.map +1 -1
- package/lib/deltaScheduler.d.ts.map +1 -1
- package/lib/deltaScheduler.js +9 -4
- package/lib/deltaScheduler.js.map +1 -1
- package/lib/garbageCollection.d.ts +33 -14
- package/lib/garbageCollection.d.ts.map +1 -1
- package/lib/garbageCollection.js +180 -94
- package/lib/garbageCollection.js.map +1 -1
- package/lib/garbageCollectionConstants.d.ts +1 -0
- package/lib/garbageCollectionConstants.d.ts.map +1 -1
- package/lib/garbageCollectionConstants.js +3 -0
- package/lib/garbageCollectionConstants.js.map +1 -1
- package/lib/garbageCollectionHelpers.d.ts +26 -0
- package/lib/garbageCollectionHelpers.d.ts.map +1 -0
- package/lib/garbageCollectionHelpers.js +40 -0
- package/lib/garbageCollectionHelpers.js.map +1 -0
- package/lib/gcSweepReadyUsageDetection.d.ts +5 -5
- package/lib/gcSweepReadyUsageDetection.d.ts.map +1 -1
- package/lib/gcSweepReadyUsageDetection.js +14 -10
- package/lib/gcSweepReadyUsageDetection.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/opLifecycle/batchManager.d.ts +5 -5
- package/lib/opLifecycle/batchManager.d.ts.map +1 -1
- package/lib/opLifecycle/batchManager.js +19 -12
- package/lib/opLifecycle/batchManager.js.map +1 -1
- package/lib/opLifecycle/definitions.d.ts.map +1 -1
- package/lib/opLifecycle/definitions.js.map +1 -1
- package/lib/opLifecycle/index.d.ts.map +1 -1
- package/lib/opLifecycle/index.js.map +1 -1
- package/lib/opLifecycle/opCompressor.d.ts.map +1 -1
- package/lib/opLifecycle/opCompressor.js.map +1 -1
- package/lib/opLifecycle/opDecompressor.d.ts +0 -4
- package/lib/opLifecycle/opDecompressor.d.ts.map +1 -1
- package/lib/opLifecycle/opDecompressor.js +7 -43
- package/lib/opLifecycle/opDecompressor.js.map +1 -1
- package/lib/opLifecycle/opSplitter.d.ts.map +1 -1
- package/lib/opLifecycle/opSplitter.js +5 -2
- package/lib/opLifecycle/opSplitter.js.map +1 -1
- package/lib/opLifecycle/outbox.d.ts.map +1 -1
- package/lib/opLifecycle/outbox.js +20 -19
- package/lib/opLifecycle/outbox.js.map +1 -1
- package/lib/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
- package/lib/opLifecycle/remoteMessageProcessor.js.map +1 -1
- package/lib/opProperties.d.ts.map +1 -1
- package/lib/opProperties.js +1 -3
- package/lib/opProperties.js.map +1 -1
- package/lib/orderedClientElection.d.ts.map +1 -1
- package/lib/orderedClientElection.js +10 -4
- package/lib/orderedClientElection.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 +7 -0
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +7 -4
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/runWhileConnectedCoordinator.d.ts.map +1 -1
- package/lib/runWhileConnectedCoordinator.js.map +1 -1
- package/lib/runningSummarizer.d.ts.map +1 -1
- package/lib/runningSummarizer.js +35 -22
- package/lib/runningSummarizer.js.map +1 -1
- package/lib/scheduleManager.d.ts.map +1 -1
- package/lib/scheduleManager.js +3 -2
- package/lib/scheduleManager.js.map +1 -1
- package/lib/serializedSnapshotStorage.d.ts +2 -2
- package/lib/serializedSnapshotStorage.d.ts.map +1 -1
- package/lib/serializedSnapshotStorage.js +5 -3
- package/lib/serializedSnapshotStorage.js.map +1 -1
- package/lib/summarizer.d.ts +2 -2
- package/lib/summarizer.d.ts.map +1 -1
- package/lib/summarizer.js +37 -17
- package/lib/summarizer.js.map +1 -1
- package/lib/summarizerClientElection.d.ts.map +1 -1
- package/lib/summarizerClientElection.js.map +1 -1
- package/lib/summarizerHandle.d.ts.map +1 -1
- package/lib/summarizerHandle.js.map +1 -1
- package/lib/summarizerHeuristics.d.ts.map +1 -1
- package/lib/summarizerHeuristics.js +6 -9
- package/lib/summarizerHeuristics.js.map +1 -1
- package/lib/summarizerTypes.d.ts +21 -21
- package/lib/summarizerTypes.d.ts.map +1 -1
- package/lib/summarizerTypes.js.map +1 -1
- package/lib/summaryCollection.d.ts.map +1 -1
- package/lib/summaryCollection.js +18 -8
- package/lib/summaryCollection.js.map +1 -1
- package/lib/summaryFormat.d.ts +5 -2
- package/lib/summaryFormat.d.ts.map +1 -1
- package/lib/summaryFormat.js +20 -12
- package/lib/summaryFormat.js.map +1 -1
- package/lib/summaryGenerator.d.ts.map +1 -1
- package/lib/summaryGenerator.js +35 -16
- package/lib/summaryGenerator.js.map +1 -1
- package/lib/summaryManager.d.ts.map +1 -1
- package/lib/summaryManager.js +21 -9
- package/lib/summaryManager.js.map +1 -1
- package/lib/throttler.d.ts +2 -2
- package/lib/throttler.d.ts.map +1 -1
- package/lib/throttler.js +4 -4
- package/lib/throttler.js.map +1 -1
- package/package.json +121 -115
- package/prettier.config.cjs +1 -1
- package/src/batchTracker.ts +54 -49
- package/src/blobManager.ts +793 -672
- package/src/connectionTelemetry.ts +280 -249
- package/src/containerHandleContext.ts +27 -29
- package/src/containerRuntime.ts +3168 -2988
- package/src/dataStore.ts +172 -159
- package/src/dataStoreContext.ts +1098 -1055
- package/src/dataStoreContexts.ts +178 -161
- package/src/dataStoreRegistry.ts +25 -20
- package/src/dataStores.ts +884 -728
- package/src/deltaScheduler.ts +158 -150
- package/src/garbageCollection.ts +1860 -1688
- package/src/garbageCollectionConstants.ts +3 -0
- package/src/garbageCollectionHelpers.ts +61 -0
- package/src/gcSweepReadyUsageDetection.ts +89 -83
- package/src/index.ts +67 -66
- package/src/opLifecycle/README.md +152 -0
- package/src/opLifecycle/batchManager.ts +145 -141
- package/src/opLifecycle/definitions.ts +29 -29
- package/src/opLifecycle/index.ts +5 -5
- package/src/opLifecycle/opCompressor.ts +54 -53
- package/src/opLifecycle/opDecompressor.ts +100 -128
- package/src/opLifecycle/opSplitter.ts +214 -188
- package/src/opLifecycle/outbox.ts +204 -195
- package/src/opLifecycle/remoteMessageProcessor.ts +62 -62
- package/src/opProperties.ts +11 -9
- package/src/orderedClientElection.ts +489 -457
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +384 -338
- package/src/runWhileConnectedCoordinator.ts +78 -71
- package/src/runningSummarizer.ts +619 -581
- package/src/scheduleManager.ts +299 -269
- package/src/serializedSnapshotStorage.ts +126 -112
- package/src/summarizer.ts +417 -381
- package/src/summarizerClientElection.ts +107 -100
- package/src/summarizerHandle.ts +11 -9
- package/src/summarizerHeuristics.ts +183 -186
- package/src/summarizerTypes.ts +344 -330
- package/src/summaryCollection.ts +378 -349
- package/src/summaryFormat.ts +165 -143
- package/src/summaryGenerator.ts +465 -410
- package/src/summaryManager.ts +377 -348
- package/src/throttler.ts +131 -122
- package/tsconfig.esnext.json +6 -6
- package/tsconfig.json +9 -13
- package/dist/garbageCollectionTombstoneUtils.d.ts +0 -14
- package/dist/garbageCollectionTombstoneUtils.d.ts.map +0 -1
- package/dist/garbageCollectionTombstoneUtils.js +0 -23
- package/dist/garbageCollectionTombstoneUtils.js.map +0 -1
- package/lib/garbageCollectionTombstoneUtils.d.ts +0 -14
- package/lib/garbageCollectionTombstoneUtils.d.ts.map +0 -1
- package/lib/garbageCollectionTombstoneUtils.js +0 -19
- package/lib/garbageCollectionTombstoneUtils.js.map +0 -1
- package/src/garbageCollectionTombstoneUtils.ts +0 -28
|
@@ -15,37 +15,37 @@ import { summarizerClientType } from "./summarizerClientElection";
|
|
|
15
15
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
16
16
|
export type ImmutablePrimitives = undefined | null | boolean | string | number | Function;
|
|
17
17
|
export type Immutable<T> = T extends ImmutablePrimitives
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
? T
|
|
19
|
+
: T extends (infer A)[]
|
|
20
|
+
? readonly Immutable<A>[]
|
|
21
|
+
: T extends Map<infer K, infer V>
|
|
22
|
+
? ReadonlyMap<Immutable<K>, Immutable<V>>
|
|
23
|
+
: T extends Set<infer V>
|
|
24
|
+
? ReadonlySet<Immutable<V>>
|
|
25
|
+
: { readonly [K in keyof T]: Immutable<T[K]> };
|
|
26
26
|
|
|
27
27
|
/** Minimum information for a client tracked for election consideration. */
|
|
28
28
|
export interface ITrackedClient {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
readonly clientId: string;
|
|
30
|
+
readonly sequenceNumber: number;
|
|
31
|
+
readonly client: Immutable<IClient>;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/** Common contract for link nodes within an OrderedClientCollection. */
|
|
35
35
|
export interface ILinkNode {
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
readonly sequenceNumber: number;
|
|
37
|
+
youngerClient: ILinkedClient | undefined;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/** Placeholder root node within an OrderedClientCollection; does not represent a client. */
|
|
41
41
|
export interface IRootLinkNode extends ILinkNode {
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
readonly sequenceNumber: -1;
|
|
43
|
+
readonly olderClient: undefined;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/** Additional information required to keep track of the client within the doubly-linked list. */
|
|
47
47
|
export interface ILinkedClient extends ILinkNode, ITrackedClient {
|
|
48
|
-
|
|
48
|
+
olderClient: LinkNode;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/** Any link node within OrderedClientCollection including the placeholder root node. */
|
|
@@ -53,18 +53,21 @@ export type LinkNode = IRootLinkNode | ILinkedClient;
|
|
|
53
53
|
|
|
54
54
|
/** Events raised by an OrderedClientCollection. */
|
|
55
55
|
export interface IOrderedClientCollectionEvents extends IEvent {
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
/** Event fires when client is being added. */
|
|
57
|
+
(
|
|
58
|
+
event: "addClient" | "removeClient",
|
|
59
|
+
listener: (client: ILinkedClient, sequenceNumber: number) => void,
|
|
60
|
+
);
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
/** Contract for a sorted collection of all clients in the quorum. */
|
|
61
64
|
export interface IOrderedClientCollection extends IEventProvider<IOrderedClientCollectionEvents> {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
/** Count of clients in the collection. */
|
|
66
|
+
readonly count: number;
|
|
67
|
+
/** Pointer to the oldest client in the collection. */
|
|
68
|
+
readonly oldestClient: ILinkedClient | undefined;
|
|
69
|
+
/** Returns a sorted array of all the clients in the collection. */
|
|
70
|
+
getAllClients(): ILinkedClient[];
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
/**
|
|
@@ -75,182 +78,196 @@ export interface IOrderedClientCollection extends IEventProvider<IOrderedClientC
|
|
|
75
78
|
* The first (root) node is a placeholder to simplify logic and reduce null checking.
|
|
76
79
|
*/
|
|
77
80
|
export class OrderedClientCollection
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
81
|
+
extends TypedEventEmitter<IOrderedClientCollectionEvents>
|
|
82
|
+
implements IOrderedClientCollection
|
|
83
|
+
{
|
|
84
|
+
/** Collection of ALL clients currently in the quorum, with client ids as keys. */
|
|
85
|
+
private readonly clientMap = new Map<string, ILinkedClient>();
|
|
86
|
+
/** Placeholder head node of linked list, for simplified null checking. */
|
|
87
|
+
private readonly rootNode: IRootLinkNode = {
|
|
88
|
+
sequenceNumber: -1,
|
|
89
|
+
olderClient: undefined,
|
|
90
|
+
youngerClient: undefined,
|
|
91
|
+
};
|
|
92
|
+
/** Pointer to end of linked list, for optimized client adds. */
|
|
93
|
+
private _youngestClient: LinkNode = this.rootNode;
|
|
94
|
+
private readonly logger: ITelemetryLogger;
|
|
95
|
+
|
|
96
|
+
public get count() {
|
|
97
|
+
return this.clientMap.size;
|
|
98
|
+
}
|
|
99
|
+
public get oldestClient() {
|
|
100
|
+
return this.rootNode.youngerClient;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
constructor(
|
|
104
|
+
logger: ITelemetryLogger,
|
|
105
|
+
deltaManager: Pick<IDeltaManager<unknown, unknown>, "lastSequenceNumber">,
|
|
106
|
+
quorum: Pick<IQuorumClients, "getMembers" | "on">,
|
|
107
|
+
) {
|
|
108
|
+
super();
|
|
109
|
+
this.logger = ChildLogger.create(logger, "OrderedClientCollection");
|
|
110
|
+
const members = quorum.getMembers();
|
|
111
|
+
for (const [clientId, client] of members) {
|
|
112
|
+
this.addClient(clientId, client);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
quorum.on("addMember", (clientId, client) => {
|
|
116
|
+
const newClient = this.addClient(clientId, client);
|
|
117
|
+
this.emit("addClient", newClient, deltaManager.lastSequenceNumber);
|
|
118
|
+
});
|
|
119
|
+
quorum.on("removeMember", (clientId) => {
|
|
120
|
+
const sequenceNumber = deltaManager.lastSequenceNumber;
|
|
121
|
+
const removeClient = this.removeClient(clientId);
|
|
122
|
+
if (removeClient === undefined) {
|
|
123
|
+
this.logger.sendErrorEvent({
|
|
124
|
+
eventName: "ClientNotFound",
|
|
125
|
+
clientId,
|
|
126
|
+
sequenceNumber,
|
|
127
|
+
});
|
|
128
|
+
} else {
|
|
129
|
+
this.emit("removeClient", removeClient, sequenceNumber);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private addClient(clientId: string, client: ISequencedClient): ITrackedClient {
|
|
135
|
+
// Normal case is adding the latest client, which will bypass loop.
|
|
136
|
+
// Find where it belongs otherwise (maybe possible during initial load?).
|
|
137
|
+
assert(
|
|
138
|
+
client.sequenceNumber > -1,
|
|
139
|
+
0x1f6 /* "Negative client sequence number not allowed" */,
|
|
140
|
+
);
|
|
141
|
+
let currClient = this._youngestClient;
|
|
142
|
+
while (currClient.sequenceNumber > client.sequenceNumber) {
|
|
143
|
+
assert(
|
|
144
|
+
currClient.olderClient !== undefined,
|
|
145
|
+
0x1f7 /* "Previous client should always be defined" */,
|
|
146
|
+
);
|
|
147
|
+
// Note: If adding a client older than the elected client, it will not be automatically elected.
|
|
148
|
+
currClient = currClient.olderClient;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Now currClient is the node right before where the new client node should be.
|
|
152
|
+
const newClient: ILinkedClient = {
|
|
153
|
+
clientId,
|
|
154
|
+
sequenceNumber: client.sequenceNumber,
|
|
155
|
+
client: { ...client.client }, // shallow clone
|
|
156
|
+
olderClient: currClient,
|
|
157
|
+
youngerClient: currClient.youngerClient,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// Update prev node to point to this new node.
|
|
161
|
+
newClient.olderClient.youngerClient = newClient;
|
|
162
|
+
|
|
163
|
+
if (newClient.youngerClient === undefined) {
|
|
164
|
+
// Update linked list end pointer to youngest client.
|
|
165
|
+
this._youngestClient = newClient;
|
|
166
|
+
} else {
|
|
167
|
+
// Update next node to point back to this new node.
|
|
168
|
+
newClient.youngerClient.olderClient = newClient;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.clientMap.set(clientId, newClient);
|
|
172
|
+
return newClient;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private removeClient(clientId: string): ITrackedClient | undefined {
|
|
176
|
+
const removeClient = this.clientMap.get(clientId);
|
|
177
|
+
if (removeClient === undefined) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Update prev node to point to next node.
|
|
182
|
+
removeClient.olderClient.youngerClient = removeClient.youngerClient;
|
|
183
|
+
|
|
184
|
+
if (removeClient.youngerClient === undefined) {
|
|
185
|
+
// Update linked list end pointer to youngest client.
|
|
186
|
+
this._youngestClient = removeClient.olderClient;
|
|
187
|
+
} else {
|
|
188
|
+
// Update next node to point back to previous node.
|
|
189
|
+
removeClient.youngerClient.olderClient = removeClient.olderClient;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
this.clientMap.delete(clientId);
|
|
193
|
+
return removeClient;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Returns an array of all clients being tracked in order from oldest to newest. */
|
|
197
|
+
public getAllClients(): ILinkedClient[] {
|
|
198
|
+
const result: ILinkedClient[] = [];
|
|
199
|
+
let currClient: LinkNode = this.rootNode;
|
|
200
|
+
while (currClient.youngerClient !== undefined) {
|
|
201
|
+
result.push(currClient.youngerClient);
|
|
202
|
+
currClient = currClient.youngerClient;
|
|
203
|
+
}
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
192
206
|
}
|
|
193
207
|
|
|
194
208
|
/** Events raised by an OrderedClientElection. */
|
|
195
209
|
export interface IOrderedClientElectionEvents extends IEvent {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
210
|
+
/** Event fires when the currently elected client changes. */
|
|
211
|
+
(
|
|
212
|
+
event: "election",
|
|
213
|
+
listener: (
|
|
214
|
+
/** Newly elected client. */
|
|
215
|
+
client: ITrackedClient | undefined,
|
|
216
|
+
/** Sequence number where election took place. */
|
|
217
|
+
sequenceNumber: number,
|
|
218
|
+
/** Previously elected client. */
|
|
219
|
+
prevClient: ITrackedClient | undefined,
|
|
220
|
+
) => void,
|
|
221
|
+
);
|
|
205
222
|
}
|
|
206
223
|
|
|
207
224
|
/** Serialized state of IOrderedClientElection. */
|
|
208
225
|
export interface ISerializedElection {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
226
|
+
/** Sequence number at the time of the latest election. */
|
|
227
|
+
readonly electionSequenceNumber: number;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Most recently elected client id. This is either:
|
|
231
|
+
*
|
|
232
|
+
* 1. the interactive elected parent client, in which case electedClientId === electedParentId,
|
|
233
|
+
* and the SummaryManager on the elected client will spawn a summarizer client, or
|
|
234
|
+
*
|
|
235
|
+
* 2. the non-interactive summarizer client itself.
|
|
236
|
+
*/
|
|
237
|
+
readonly electedClientId: string | undefined;
|
|
238
|
+
|
|
239
|
+
/** Most recently elected parent client id. This is always an interactive client. */
|
|
240
|
+
readonly electedParentId: string | undefined;
|
|
224
241
|
}
|
|
225
242
|
|
|
226
243
|
/** Contract for maintaining a deterministic client election based on eligibility. */
|
|
227
244
|
export interface IOrderedClientElection extends IEventProvider<IOrderedClientElectionEvents> {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
245
|
+
/** Count of eligible clients in the collection. */
|
|
246
|
+
readonly eligibleCount: number;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Currently elected client. This is either:
|
|
250
|
+
*
|
|
251
|
+
* 1. the interactive elected parent client, in which case electedClientId === electedParentId,
|
|
252
|
+
* and the SummaryManager on the elected client will spawn a summarizer client, or
|
|
253
|
+
*
|
|
254
|
+
* 2. the non-interactive summarizer client itself.
|
|
255
|
+
*/
|
|
256
|
+
readonly electedClient: ITrackedClient | undefined;
|
|
257
|
+
/** Currently elected parent client. This is always an interactive client. */
|
|
258
|
+
readonly electedParent: ITrackedClient | undefined;
|
|
259
|
+
/** Sequence number of most recent election. */
|
|
260
|
+
readonly electionSequenceNumber: number;
|
|
261
|
+
/** Marks the currently elected client as invalid, and elects the next eligible client. */
|
|
262
|
+
incrementElectedClient(sequenceNumber: number): void;
|
|
263
|
+
/** Resets the currently elected client back to the oldest eligible client. */
|
|
264
|
+
resetElectedClient(sequenceNumber: number): void;
|
|
265
|
+
/** Peeks at what the next elected client would be if incrementElectedClient were called. */
|
|
266
|
+
peekNextElectedClient(): ITrackedClient | undefined;
|
|
267
|
+
/** Returns a sorted array of all the eligible clients in the collection. */
|
|
268
|
+
getAllEligibleClients(): ITrackedClient[];
|
|
269
|
+
/** Serialize election data */
|
|
270
|
+
serialize(): ISerializedElection;
|
|
254
271
|
}
|
|
255
272
|
|
|
256
273
|
/**
|
|
@@ -260,273 +277,288 @@ export interface IOrderedClientElection extends IEventProvider<IOrderedClientEle
|
|
|
260
277
|
* are called under the same conditions for all clients.
|
|
261
278
|
*/
|
|
262
279
|
export class OrderedClientElection
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
280
|
+
extends TypedEventEmitter<IOrderedClientElectionEvents>
|
|
281
|
+
implements IOrderedClientElection
|
|
282
|
+
{
|
|
283
|
+
private _eligibleCount: number = 0;
|
|
284
|
+
private _electedClient: ILinkedClient | undefined;
|
|
285
|
+
private _electedParent: ILinkedClient | undefined;
|
|
286
|
+
private _electionSequenceNumber: number;
|
|
287
|
+
|
|
288
|
+
public get eligibleCount() {
|
|
289
|
+
return this._eligibleCount;
|
|
290
|
+
}
|
|
291
|
+
public get electionSequenceNumber() {
|
|
292
|
+
return this._electionSequenceNumber;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* OrderedClientCollection tracks electedClient and electedParent separately. This allows us to handle the case
|
|
297
|
+
* where a new interactive parent client has been elected, but the summarizer is still doing work, so
|
|
298
|
+
* a new summarizer should not yet be spawned. In this case, changing electedParent will cause SummaryManager
|
|
299
|
+
* to stop the current summarizer, but a new summarizer will not be spawned until the old summarizer client has
|
|
300
|
+
* left the quorum.
|
|
301
|
+
*
|
|
302
|
+
* Details:
|
|
303
|
+
*
|
|
304
|
+
* electedParent is the interactive client that has been elected to spawn a summarizer. It is typically the oldest
|
|
305
|
+
* eligible interactive client in the quorum. Only the electedParent is permitted to spawn a summarizer.
|
|
306
|
+
* Once elected, this client will remain the electedParent until it leaves the quorum or the summarizer that
|
|
307
|
+
* it spawned stops producing summaries, at which point a new electedParent will be chosen.
|
|
308
|
+
*
|
|
309
|
+
* electedClient is the non-interactive summarizer client if one exists. If not, then electedClient is equal to
|
|
310
|
+
* electedParent. If electedParent === electedClient, this is the signal for electedParent to spawn a new
|
|
311
|
+
* electedClient. Once a summarizer client becomes electedClient, a new summarizer will not be spawned until
|
|
312
|
+
* electedClient leaves the quorum.
|
|
313
|
+
*
|
|
314
|
+
* A typical sequence looks like this:
|
|
315
|
+
*
|
|
316
|
+
* i. Begin by electing A. electedParent === A, electedClient === A.
|
|
317
|
+
*
|
|
318
|
+
* ii. SummaryManager running on A spawns a summarizer client, A'. electedParent === A, electedClient === A'
|
|
319
|
+
*
|
|
320
|
+
* iii. A' stops producing summaries. A new parent client, B, is elected. electedParent === B, electedClient === A'
|
|
321
|
+
*
|
|
322
|
+
* iv. SummaryManager running on A detects the change to electedParent and tells the summarizer to stop, but A'
|
|
323
|
+
* is in mid-summarization. No new summarizer is spawned, as electedParent !== electedClient.
|
|
324
|
+
*
|
|
325
|
+
* v. A' completes its summary, and the summarizer and backing client are torn down.
|
|
326
|
+
*
|
|
327
|
+
* vi. A' leaves the quorum, and B takes its place as electedClient. electedParent === B, electedClient === B
|
|
328
|
+
*
|
|
329
|
+
* vii. SummaryManager running on B spawns a summarizer client, B'. electedParent === B, electedClient === B'
|
|
330
|
+
*/
|
|
331
|
+
public get electedClient() {
|
|
332
|
+
return this._electedClient;
|
|
333
|
+
}
|
|
334
|
+
public get electedParent() {
|
|
335
|
+
return this._electedParent;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
constructor(
|
|
339
|
+
logger: ITelemetryLogger,
|
|
340
|
+
private readonly orderedClientCollection: IOrderedClientCollection,
|
|
341
|
+
/** Serialized state from summary or current sequence number at time of load if new. */
|
|
342
|
+
initialState: ISerializedElection | number,
|
|
343
|
+
private readonly isEligibleFn: (c: ITrackedClient) => boolean,
|
|
344
|
+
) {
|
|
345
|
+
super();
|
|
346
|
+
let initialClient: ILinkedClient | undefined;
|
|
347
|
+
let initialParent: ILinkedClient | undefined;
|
|
348
|
+
for (const client of orderedClientCollection.getAllClients()) {
|
|
349
|
+
this.addClient(client, 0);
|
|
350
|
+
if (typeof initialState !== "number") {
|
|
351
|
+
if (client.clientId === initialState.electedClientId) {
|
|
352
|
+
initialClient = client;
|
|
353
|
+
if (
|
|
354
|
+
initialState.electedParentId === undefined &&
|
|
355
|
+
client.client.details.type !== summarizerClientType
|
|
356
|
+
) {
|
|
357
|
+
// If there was no elected parent in the serialized data, use this one.
|
|
358
|
+
initialParent = client;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (client.clientId === initialState.electedParentId) {
|
|
362
|
+
initialParent = client;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
orderedClientCollection.on("addClient", (client, seq) => this.addClient(client, seq));
|
|
367
|
+
orderedClientCollection.on("removeClient", (client, seq) => this.removeClient(client, seq));
|
|
368
|
+
|
|
369
|
+
if (typeof initialState === "number") {
|
|
370
|
+
this._electionSequenceNumber = initialState;
|
|
371
|
+
} else {
|
|
372
|
+
// Override the initially elected client with the initial state.
|
|
373
|
+
if (initialClient?.clientId !== initialState.electedClientId) {
|
|
374
|
+
// Cannot find initially elected client, so elect undefined.
|
|
375
|
+
logger.sendErrorEvent({
|
|
376
|
+
eventName: "InitialElectedClientNotFound",
|
|
377
|
+
electionSequenceNumber: initialState.electionSequenceNumber,
|
|
378
|
+
expectedClientId: initialState.electedClientId,
|
|
379
|
+
electedClientId: initialClient?.clientId,
|
|
380
|
+
clientCount: orderedClientCollection.count,
|
|
381
|
+
});
|
|
382
|
+
} else if (initialClient !== undefined && !isEligibleFn(initialClient)) {
|
|
383
|
+
// Initially elected client is ineligible, so elect next eligible client.
|
|
384
|
+
initialClient = initialParent = this.findFirstEligibleParent(initialParent);
|
|
385
|
+
logger.sendErrorEvent({
|
|
386
|
+
eventName: "InitialElectedClientIneligible",
|
|
387
|
+
electionSequenceNumber: initialState.electionSequenceNumber,
|
|
388
|
+
expectedClientId: initialState.electedClientId,
|
|
389
|
+
electedClientId: initialClient?.clientId,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
this._electedParent = initialParent;
|
|
393
|
+
this._electedClient = initialClient;
|
|
394
|
+
this._electionSequenceNumber = initialState.electionSequenceNumber;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Tries changing the elected client, raising an event if it is different.
|
|
400
|
+
* Note that this function does no eligibility or suitability checks. If we get here, then
|
|
401
|
+
* we will set _electedClient, and we will set _electedParent if this is an interactive client.
|
|
402
|
+
*/
|
|
403
|
+
private tryElectingClient(client: ILinkedClient | undefined, sequenceNumber: number): void {
|
|
404
|
+
let change = false;
|
|
405
|
+
const isSummarizerClient = client?.client.details.type === summarizerClientType;
|
|
406
|
+
const prevClient = this._electedClient;
|
|
407
|
+
if (this._electedClient !== client) {
|
|
408
|
+
// Changing the elected client. Record the sequence number and note that we have to fire an event.
|
|
409
|
+
this._electionSequenceNumber = sequenceNumber;
|
|
410
|
+
this._electedClient = client;
|
|
411
|
+
change = true;
|
|
412
|
+
}
|
|
413
|
+
if (this._electedParent !== client && !isSummarizerClient) {
|
|
414
|
+
// Changing the elected parent as well.
|
|
415
|
+
this._electedParent = client;
|
|
416
|
+
change = true;
|
|
417
|
+
}
|
|
418
|
+
if (change) {
|
|
419
|
+
this.emit("election", client, sequenceNumber, prevClient);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
private tryElectingParent(client: ILinkedClient | undefined, sequenceNumber: number): void {
|
|
424
|
+
if (this._electedParent !== client) {
|
|
425
|
+
this._electedParent = client;
|
|
426
|
+
this.emit("election", this._electedClient, sequenceNumber, this._electedClient);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Helper function to find the first eligible parent client starting with the passed in client,
|
|
432
|
+
* or undefined if none are eligible.
|
|
433
|
+
* @param client - client to start checking
|
|
434
|
+
* @returns oldest eligible client starting with passed in client or undefined if none.
|
|
435
|
+
*/
|
|
436
|
+
private findFirstEligibleParent(client: ILinkedClient | undefined): ILinkedClient | undefined {
|
|
437
|
+
let candidateClient = client;
|
|
438
|
+
while (
|
|
439
|
+
candidateClient !== undefined &&
|
|
440
|
+
(!this.isEligibleFn(candidateClient) ||
|
|
441
|
+
candidateClient.client.details.type === summarizerClientType)
|
|
442
|
+
) {
|
|
443
|
+
candidateClient = candidateClient.youngerClient;
|
|
444
|
+
}
|
|
445
|
+
return candidateClient;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Updates tracking for when a new client is added to the collection.
|
|
450
|
+
* Will automatically elect that new client if none is elected currently.
|
|
451
|
+
* @param client - client added to the collection
|
|
452
|
+
* @param sequenceNumber - sequence number when client was added
|
|
453
|
+
*/
|
|
454
|
+
private addClient(client: ILinkedClient, sequenceNumber: number): void {
|
|
455
|
+
if (this.isEligibleFn(client)) {
|
|
456
|
+
this._eligibleCount++;
|
|
457
|
+
const newClientIsSummarizer = client.client.details.type === summarizerClientType;
|
|
458
|
+
const electedClientIsSummarizer =
|
|
459
|
+
this._electedClient?.client.details.type === summarizerClientType;
|
|
460
|
+
// Note that we allow a summarizer client to supercede an interactive client as elected client.
|
|
461
|
+
if (
|
|
462
|
+
this._electedClient === undefined ||
|
|
463
|
+
(!electedClientIsSummarizer && newClientIsSummarizer)
|
|
464
|
+
) {
|
|
465
|
+
this.tryElectingClient(client, sequenceNumber);
|
|
466
|
+
} else if (this._electedParent === undefined && !newClientIsSummarizer) {
|
|
467
|
+
// This is an odd case. If the _electedClient is set, the _electedParent should be as well.
|
|
468
|
+
this.tryElectingParent(client, sequenceNumber);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Updates tracking for when an existing client is removed from the collection.
|
|
475
|
+
* Will automatically elect next oldest client if currently elected is removed.
|
|
476
|
+
* @param client - client removed from the collection
|
|
477
|
+
* @param sequenceNumber - sequence number when client was removed
|
|
478
|
+
*/
|
|
479
|
+
private removeClient(client: ILinkedClient, sequenceNumber: number): void {
|
|
480
|
+
if (this.isEligibleFn(client)) {
|
|
481
|
+
this._eligibleCount--;
|
|
482
|
+
if (this._electedClient === client) {
|
|
483
|
+
// Removing the _electedClient. There are 2 possible cases:
|
|
484
|
+
if (this._electedParent !== client) {
|
|
485
|
+
// 1. The _electedClient is a summarizer that we've been allowing to finish its work.
|
|
486
|
+
// Let the _electedParent become the _electedClient so that it can start its own summarizer.
|
|
487
|
+
if (this._electedClient.client.details.type !== summarizerClientType) {
|
|
488
|
+
throw new UsageError("Elected client should be a summarizer client 1");
|
|
489
|
+
}
|
|
490
|
+
this.tryElectingClient(this._electedParent, sequenceNumber);
|
|
491
|
+
} else {
|
|
492
|
+
// 2. The _electedClient is an interactive client that has left the quorum.
|
|
493
|
+
// Automatically shift to next oldest client.
|
|
494
|
+
const nextClient =
|
|
495
|
+
this.findFirstEligibleParent(this._electedParent?.youngerClient) ??
|
|
496
|
+
this.findFirstEligibleParent(this.orderedClientCollection.oldestClient);
|
|
497
|
+
this.tryElectingClient(nextClient, sequenceNumber);
|
|
498
|
+
}
|
|
499
|
+
} else if (this._electedParent === client) {
|
|
500
|
+
// Removing the _electedParent (but not _electedClient).
|
|
501
|
+
// Shift to the next oldest parent, but do not replace the _electedClient,
|
|
502
|
+
// which is a summarizer that is still doing work.
|
|
503
|
+
if (this._electedClient?.client.details.type !== summarizerClientType) {
|
|
504
|
+
throw new UsageError("Elected client should be a summarizer client 2");
|
|
505
|
+
}
|
|
506
|
+
const nextParent =
|
|
507
|
+
this.findFirstEligibleParent(this._electedParent?.youngerClient) ??
|
|
508
|
+
this.findFirstEligibleParent(this.orderedClientCollection.oldestClient);
|
|
509
|
+
this.tryElectingParent(nextParent, sequenceNumber);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
public getAllEligibleClients(): ITrackedClient[] {
|
|
515
|
+
return this.orderedClientCollection.getAllClients().filter(this.isEligibleFn);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Advance election to the next-oldest client. This is called if the current parent is leaving the quorum,
|
|
520
|
+
* or if the current summarizer is not responsive and we want to stop it and spawn a new one.
|
|
521
|
+
*/
|
|
522
|
+
public incrementElectedClient(sequenceNumber: number): void {
|
|
523
|
+
const nextClient =
|
|
524
|
+
this.findFirstEligibleParent(this._electedParent?.youngerClient) ??
|
|
525
|
+
this.findFirstEligibleParent(this.orderedClientCollection.oldestClient);
|
|
526
|
+
if (this._electedClient === undefined || this._electedClient === this._electedParent) {
|
|
527
|
+
this.tryElectingClient(nextClient, sequenceNumber);
|
|
528
|
+
} else {
|
|
529
|
+
// The _electedClient is a summarizer and should not be replaced until it leaves the quorum.
|
|
530
|
+
// Changing the _electedParent will stop the summarizer.
|
|
531
|
+
this.tryElectingParent(nextClient, sequenceNumber);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* (Re-)start election with the oldest client in the quorum. This is called if we need to summarize
|
|
537
|
+
* and no client has been elected.
|
|
538
|
+
*/
|
|
539
|
+
public resetElectedClient(sequenceNumber: number): void {
|
|
540
|
+
const firstClient = this.findFirstEligibleParent(this.orderedClientCollection.oldestClient);
|
|
541
|
+
if (this._electedClient === undefined || this._electedClient === this._electedParent) {
|
|
542
|
+
this.tryElectingClient(firstClient, sequenceNumber);
|
|
543
|
+
} else {
|
|
544
|
+
// The _electedClient is a summarizer and should not be replaced until it leaves the quorum.
|
|
545
|
+
// Changing the _electedParent will stop the summarizer.
|
|
546
|
+
this.tryElectingParent(firstClient, sequenceNumber);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
public peekNextElectedClient(): ITrackedClient | undefined {
|
|
551
|
+
return (
|
|
552
|
+
this.findFirstEligibleParent(this._electedParent?.youngerClient) ??
|
|
553
|
+
this.findFirstEligibleParent(this.orderedClientCollection.oldestClient)
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
public serialize(): ISerializedElection {
|
|
558
|
+
return {
|
|
559
|
+
electionSequenceNumber: this.electionSequenceNumber,
|
|
560
|
+
electedClientId: this.electedClient?.clientId,
|
|
561
|
+
electedParentId: this.electedParent?.clientId,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
532
564
|
}
|