@fluidframework/container-runtime 2.0.0-internal.3.0.1 → 2.0.0-internal.3.1.0

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