@fluidframework/container-runtime 2.0.0-dev.2.2.0.111723 → 2.0.0-dev.3.1.0.125672

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 (365) hide show
  1. package/.eslintrc.js +21 -10
  2. package/.mocharc.js +2 -2
  3. package/api-extractor.json +2 -2
  4. package/dist/batchTracker.d.ts +1 -2
  5. package/dist/batchTracker.d.ts.map +1 -1
  6. package/dist/batchTracker.js +2 -1
  7. package/dist/batchTracker.js.map +1 -1
  8. package/dist/blobManager.d.ts +62 -28
  9. package/dist/blobManager.d.ts.map +1 -1
  10. package/dist/blobManager.js +256 -102
  11. package/dist/blobManager.js.map +1 -1
  12. package/dist/connectionTelemetry.d.ts.map +1 -1
  13. package/dist/connectionTelemetry.js +11 -9
  14. package/dist/connectionTelemetry.js.map +1 -1
  15. package/dist/containerHandleContext.d.ts.map +1 -1
  16. package/dist/containerHandleContext.js +3 -1
  17. package/dist/containerHandleContext.js.map +1 -1
  18. package/dist/containerRuntime.d.ts +110 -78
  19. package/dist/containerRuntime.d.ts.map +1 -1
  20. package/dist/containerRuntime.js +336 -331
  21. package/dist/containerRuntime.js.map +1 -1
  22. package/dist/dataStore.d.ts.map +1 -1
  23. package/dist/dataStore.js +11 -9
  24. package/dist/dataStore.js.map +1 -1
  25. package/dist/dataStoreContext.d.ts +2 -1
  26. package/dist/dataStoreContext.d.ts.map +1 -1
  27. package/dist/dataStoreContext.js +40 -23
  28. package/dist/dataStoreContext.js.map +1 -1
  29. package/dist/dataStoreContexts.d.ts.map +1 -1
  30. package/dist/dataStoreContexts.js +7 -3
  31. package/dist/dataStoreContexts.js.map +1 -1
  32. package/dist/dataStoreRegistry.d.ts.map +1 -1
  33. package/dist/dataStoreRegistry.js +3 -1
  34. package/dist/dataStoreRegistry.js.map +1 -1
  35. package/dist/dataStores.d.ts +12 -9
  36. package/dist/dataStores.d.ts.map +1 -1
  37. package/dist/dataStores.js +69 -46
  38. package/dist/dataStores.js.map +1 -1
  39. package/dist/deltaScheduler.d.ts.map +1 -1
  40. package/dist/deltaScheduler.js +8 -3
  41. package/dist/deltaScheduler.js.map +1 -1
  42. package/dist/garbageCollection.d.ts +57 -42
  43. package/dist/garbageCollection.d.ts.map +1 -1
  44. package/dist/garbageCollection.js +371 -239
  45. package/dist/garbageCollection.js.map +1 -1
  46. package/dist/garbageCollectionConstants.d.ts +23 -0
  47. package/dist/garbageCollectionConstants.d.ts.map +1 -0
  48. package/dist/garbageCollectionConstants.js +36 -0
  49. package/dist/garbageCollectionConstants.js.map +1 -0
  50. package/dist/garbageCollectionHelpers.d.ts +15 -0
  51. package/dist/garbageCollectionHelpers.d.ts.map +1 -0
  52. package/dist/garbageCollectionHelpers.js +27 -0
  53. package/dist/garbageCollectionHelpers.js.map +1 -0
  54. package/dist/gcSweepReadyUsageDetection.d.ts +5 -5
  55. package/dist/gcSweepReadyUsageDetection.d.ts.map +1 -1
  56. package/dist/gcSweepReadyUsageDetection.js +15 -11
  57. package/dist/gcSweepReadyUsageDetection.js.map +1 -1
  58. package/dist/index.d.ts +4 -3
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +5 -6
  61. package/dist/index.js.map +1 -1
  62. package/dist/opLifecycle/batchManager.d.ts +42 -0
  63. package/dist/opLifecycle/batchManager.d.ts.map +1 -0
  64. package/dist/opLifecycle/batchManager.js +124 -0
  65. package/dist/opLifecycle/batchManager.js.map +1 -0
  66. package/dist/opLifecycle/definitions.d.ts +64 -0
  67. package/dist/opLifecycle/definitions.d.ts.map +1 -0
  68. package/dist/opLifecycle/definitions.js +7 -0
  69. package/dist/opLifecycle/definitions.js.map +1 -0
  70. package/dist/opLifecycle/index.d.ts +12 -0
  71. package/dist/opLifecycle/index.d.ts.map +1 -0
  72. package/dist/opLifecycle/index.js +22 -0
  73. package/dist/opLifecycle/index.js.map +1 -0
  74. package/dist/{opCompressor.d.ts → opLifecycle/opCompressor.d.ts} +3 -3
  75. package/dist/opLifecycle/opCompressor.d.ts.map +1 -0
  76. package/dist/opLifecycle/opCompressor.js +67 -0
  77. package/dist/opLifecycle/opCompressor.js.map +1 -0
  78. package/dist/{opDecompressor.d.ts → opLifecycle/opDecompressor.d.ts} +2 -1
  79. package/dist/opLifecycle/opDecompressor.d.ts.map +1 -0
  80. package/dist/{opDecompressor.js → opLifecycle/opDecompressor.js} +37 -21
  81. package/dist/opLifecycle/opDecompressor.js.map +1 -0
  82. package/dist/opLifecycle/opSplitter.d.ts +49 -0
  83. package/dist/opLifecycle/opSplitter.d.ts.map +1 -0
  84. package/dist/opLifecycle/opSplitter.js +173 -0
  85. package/dist/opLifecycle/opSplitter.js.map +1 -0
  86. package/dist/opLifecycle/outbox.d.ts +52 -0
  87. package/dist/opLifecycle/outbox.d.ts.map +1 -0
  88. package/dist/opLifecycle/outbox.js +164 -0
  89. package/dist/opLifecycle/outbox.js.map +1 -0
  90. package/dist/opLifecycle/remoteMessageProcessor.d.ts +26 -0
  91. package/dist/opLifecycle/remoteMessageProcessor.d.ts.map +1 -0
  92. package/dist/opLifecycle/remoteMessageProcessor.js +96 -0
  93. package/dist/opLifecycle/remoteMessageProcessor.js.map +1 -0
  94. package/dist/opProperties.d.ts.map +1 -1
  95. package/dist/opProperties.js +1 -3
  96. package/dist/opProperties.js.map +1 -1
  97. package/dist/orderedClientElection.d.ts.map +1 -1
  98. package/dist/orderedClientElection.js +10 -4
  99. package/dist/orderedClientElection.js.map +1 -1
  100. package/dist/packageVersion.d.ts +1 -1
  101. package/dist/packageVersion.js +1 -1
  102. package/dist/packageVersion.js.map +1 -1
  103. package/dist/pendingStateManager.d.ts +4 -13
  104. package/dist/pendingStateManager.d.ts.map +1 -1
  105. package/dist/pendingStateManager.js +134 -161
  106. package/dist/pendingStateManager.js.map +1 -1
  107. package/dist/runWhileConnectedCoordinator.d.ts.map +1 -1
  108. package/dist/runWhileConnectedCoordinator.js.map +1 -1
  109. package/dist/runningSummarizer.d.ts.map +1 -1
  110. package/dist/runningSummarizer.js +34 -22
  111. package/dist/runningSummarizer.js.map +1 -1
  112. package/dist/scheduleManager.d.ts +0 -1
  113. package/dist/scheduleManager.d.ts.map +1 -1
  114. package/dist/scheduleManager.js +11 -21
  115. package/dist/scheduleManager.js.map +1 -1
  116. package/dist/serializedSnapshotStorage.d.ts.map +1 -1
  117. package/dist/serializedSnapshotStorage.js +3 -1
  118. package/dist/serializedSnapshotStorage.js.map +1 -1
  119. package/dist/summarizer.d.ts +2 -3
  120. package/dist/summarizer.d.ts.map +1 -1
  121. package/dist/summarizer.js +39 -18
  122. package/dist/summarizer.js.map +1 -1
  123. package/dist/summarizerClientElection.d.ts +1 -2
  124. package/dist/summarizerClientElection.d.ts.map +1 -1
  125. package/dist/summarizerClientElection.js +3 -30
  126. package/dist/summarizerClientElection.js.map +1 -1
  127. package/dist/summarizerHandle.d.ts.map +1 -1
  128. package/dist/summarizerHandle.js.map +1 -1
  129. package/dist/summarizerHeuristics.d.ts.map +1 -1
  130. package/dist/summarizerHeuristics.js +6 -9
  131. package/dist/summarizerHeuristics.js.map +1 -1
  132. package/dist/summarizerTypes.d.ts +22 -25
  133. package/dist/summarizerTypes.d.ts.map +1 -1
  134. package/dist/summarizerTypes.js.map +1 -1
  135. package/dist/summaryCollection.d.ts.map +1 -1
  136. package/dist/summaryCollection.js +18 -8
  137. package/dist/summaryCollection.js.map +1 -1
  138. package/dist/summaryFormat.d.ts.map +1 -1
  139. package/dist/summaryFormat.js +18 -11
  140. package/dist/summaryFormat.js.map +1 -1
  141. package/dist/summaryGenerator.d.ts.map +1 -1
  142. package/dist/summaryGenerator.js +32 -14
  143. package/dist/summaryGenerator.js.map +1 -1
  144. package/dist/summaryManager.d.ts.map +1 -1
  145. package/dist/summaryManager.js +21 -9
  146. package/dist/summaryManager.js.map +1 -1
  147. package/dist/throttler.d.ts +2 -2
  148. package/dist/throttler.d.ts.map +1 -1
  149. package/dist/throttler.js +4 -4
  150. package/dist/throttler.js.map +1 -1
  151. package/garbageCollection.md +15 -2
  152. package/lib/batchTracker.d.ts +1 -2
  153. package/lib/batchTracker.d.ts.map +1 -1
  154. package/lib/batchTracker.js +2 -1
  155. package/lib/batchTracker.js.map +1 -1
  156. package/lib/blobManager.d.ts +62 -28
  157. package/lib/blobManager.d.ts.map +1 -1
  158. package/lib/blobManager.js +259 -105
  159. package/lib/blobManager.js.map +1 -1
  160. package/lib/connectionTelemetry.d.ts.map +1 -1
  161. package/lib/connectionTelemetry.js +11 -9
  162. package/lib/connectionTelemetry.js.map +1 -1
  163. package/lib/containerHandleContext.d.ts.map +1 -1
  164. package/lib/containerHandleContext.js +3 -1
  165. package/lib/containerHandleContext.js.map +1 -1
  166. package/lib/containerRuntime.d.ts +110 -78
  167. package/lib/containerRuntime.d.ts.map +1 -1
  168. package/lib/containerRuntime.js +340 -334
  169. package/lib/containerRuntime.js.map +1 -1
  170. package/lib/dataStore.d.ts.map +1 -1
  171. package/lib/dataStore.js +11 -9
  172. package/lib/dataStore.js.map +1 -1
  173. package/lib/dataStoreContext.d.ts +2 -1
  174. package/lib/dataStoreContext.d.ts.map +1 -1
  175. package/lib/dataStoreContext.js +41 -24
  176. package/lib/dataStoreContext.js.map +1 -1
  177. package/lib/dataStoreContexts.d.ts.map +1 -1
  178. package/lib/dataStoreContexts.js +7 -3
  179. package/lib/dataStoreContexts.js.map +1 -1
  180. package/lib/dataStoreRegistry.d.ts.map +1 -1
  181. package/lib/dataStoreRegistry.js +3 -1
  182. package/lib/dataStoreRegistry.js.map +1 -1
  183. package/lib/dataStores.d.ts +12 -9
  184. package/lib/dataStores.d.ts.map +1 -1
  185. package/lib/dataStores.js +75 -52
  186. package/lib/dataStores.js.map +1 -1
  187. package/lib/deltaScheduler.d.ts.map +1 -1
  188. package/lib/deltaScheduler.js +9 -4
  189. package/lib/deltaScheduler.js.map +1 -1
  190. package/lib/garbageCollection.d.ts +57 -42
  191. package/lib/garbageCollection.d.ts.map +1 -1
  192. package/lib/garbageCollection.js +364 -232
  193. package/lib/garbageCollection.js.map +1 -1
  194. package/lib/garbageCollectionConstants.d.ts +23 -0
  195. package/lib/garbageCollectionConstants.d.ts.map +1 -0
  196. package/lib/garbageCollectionConstants.js +33 -0
  197. package/lib/garbageCollectionConstants.js.map +1 -0
  198. package/lib/garbageCollectionHelpers.d.ts +15 -0
  199. package/lib/garbageCollectionHelpers.d.ts.map +1 -0
  200. package/lib/garbageCollectionHelpers.js +23 -0
  201. package/lib/garbageCollectionHelpers.js.map +1 -0
  202. package/lib/gcSweepReadyUsageDetection.d.ts +5 -5
  203. package/lib/gcSweepReadyUsageDetection.d.ts.map +1 -1
  204. package/lib/gcSweepReadyUsageDetection.js +15 -11
  205. package/lib/gcSweepReadyUsageDetection.js.map +1 -1
  206. package/lib/index.d.ts +4 -3
  207. package/lib/index.d.ts.map +1 -1
  208. package/lib/index.js +3 -3
  209. package/lib/index.js.map +1 -1
  210. package/lib/opLifecycle/batchManager.d.ts +42 -0
  211. package/lib/opLifecycle/batchManager.d.ts.map +1 -0
  212. package/lib/opLifecycle/batchManager.js +120 -0
  213. package/lib/opLifecycle/batchManager.js.map +1 -0
  214. package/lib/opLifecycle/definitions.d.ts +64 -0
  215. package/lib/opLifecycle/definitions.d.ts.map +1 -0
  216. package/lib/opLifecycle/definitions.js +6 -0
  217. package/lib/opLifecycle/definitions.js.map +1 -0
  218. package/lib/opLifecycle/index.d.ts +12 -0
  219. package/lib/opLifecycle/index.d.ts.map +1 -0
  220. package/lib/opLifecycle/index.js +11 -0
  221. package/lib/opLifecycle/index.js.map +1 -0
  222. package/lib/{opCompressor.d.ts → opLifecycle/opCompressor.d.ts} +3 -3
  223. package/lib/opLifecycle/opCompressor.d.ts.map +1 -0
  224. package/lib/opLifecycle/opCompressor.js +63 -0
  225. package/lib/opLifecycle/opCompressor.js.map +1 -0
  226. package/lib/{opDecompressor.d.ts → opLifecycle/opDecompressor.d.ts} +2 -1
  227. package/lib/opLifecycle/opDecompressor.d.ts.map +1 -0
  228. package/lib/{opDecompressor.js → opLifecycle/opDecompressor.js} +37 -21
  229. package/lib/opLifecycle/opDecompressor.js.map +1 -0
  230. package/lib/opLifecycle/opSplitter.d.ts +49 -0
  231. package/lib/opLifecycle/opSplitter.d.ts.map +1 -0
  232. package/lib/opLifecycle/opSplitter.js +168 -0
  233. package/lib/opLifecycle/opSplitter.js.map +1 -0
  234. package/lib/opLifecycle/outbox.d.ts +52 -0
  235. package/lib/opLifecycle/outbox.d.ts.map +1 -0
  236. package/lib/opLifecycle/outbox.js +160 -0
  237. package/lib/opLifecycle/outbox.js.map +1 -0
  238. package/lib/opLifecycle/remoteMessageProcessor.d.ts +26 -0
  239. package/lib/opLifecycle/remoteMessageProcessor.d.ts.map +1 -0
  240. package/lib/opLifecycle/remoteMessageProcessor.js +91 -0
  241. package/lib/opLifecycle/remoteMessageProcessor.js.map +1 -0
  242. package/lib/opProperties.d.ts.map +1 -1
  243. package/lib/opProperties.js +1 -3
  244. package/lib/opProperties.js.map +1 -1
  245. package/lib/orderedClientElection.d.ts.map +1 -1
  246. package/lib/orderedClientElection.js +10 -4
  247. package/lib/orderedClientElection.js.map +1 -1
  248. package/lib/packageVersion.d.ts +1 -1
  249. package/lib/packageVersion.js +1 -1
  250. package/lib/packageVersion.js.map +1 -1
  251. package/lib/pendingStateManager.d.ts +4 -13
  252. package/lib/pendingStateManager.d.ts.map +1 -1
  253. package/lib/pendingStateManager.js +134 -161
  254. package/lib/pendingStateManager.js.map +1 -1
  255. package/lib/runWhileConnectedCoordinator.d.ts.map +1 -1
  256. package/lib/runWhileConnectedCoordinator.js.map +1 -1
  257. package/lib/runningSummarizer.d.ts.map +1 -1
  258. package/lib/runningSummarizer.js +35 -23
  259. package/lib/runningSummarizer.js.map +1 -1
  260. package/lib/scheduleManager.d.ts +0 -1
  261. package/lib/scheduleManager.d.ts.map +1 -1
  262. package/lib/scheduleManager.js +11 -21
  263. package/lib/scheduleManager.js.map +1 -1
  264. package/lib/serializedSnapshotStorage.d.ts.map +1 -1
  265. package/lib/serializedSnapshotStorage.js +3 -1
  266. package/lib/serializedSnapshotStorage.js.map +1 -1
  267. package/lib/summarizer.d.ts +2 -3
  268. package/lib/summarizer.d.ts.map +1 -1
  269. package/lib/summarizer.js +39 -18
  270. package/lib/summarizer.js.map +1 -1
  271. package/lib/summarizerClientElection.d.ts +1 -2
  272. package/lib/summarizerClientElection.d.ts.map +1 -1
  273. package/lib/summarizerClientElection.js +3 -30
  274. package/lib/summarizerClientElection.js.map +1 -1
  275. package/lib/summarizerHandle.d.ts.map +1 -1
  276. package/lib/summarizerHandle.js.map +1 -1
  277. package/lib/summarizerHeuristics.d.ts.map +1 -1
  278. package/lib/summarizerHeuristics.js +6 -9
  279. package/lib/summarizerHeuristics.js.map +1 -1
  280. package/lib/summarizerTypes.d.ts +22 -25
  281. package/lib/summarizerTypes.d.ts.map +1 -1
  282. package/lib/summarizerTypes.js.map +1 -1
  283. package/lib/summaryCollection.d.ts.map +1 -1
  284. package/lib/summaryCollection.js +18 -8
  285. package/lib/summaryCollection.js.map +1 -1
  286. package/lib/summaryFormat.d.ts.map +1 -1
  287. package/lib/summaryFormat.js +20 -13
  288. package/lib/summaryFormat.js.map +1 -1
  289. package/lib/summaryGenerator.d.ts.map +1 -1
  290. package/lib/summaryGenerator.js +32 -14
  291. package/lib/summaryGenerator.js.map +1 -1
  292. package/lib/summaryManager.d.ts.map +1 -1
  293. package/lib/summaryManager.js +21 -9
  294. package/lib/summaryManager.js.map +1 -1
  295. package/lib/throttler.d.ts +2 -2
  296. package/lib/throttler.d.ts.map +1 -1
  297. package/lib/throttler.js +4 -4
  298. package/lib/throttler.js.map +1 -1
  299. package/package.json +28 -38
  300. package/prettier.config.cjs +1 -1
  301. package/src/batchTracker.ts +55 -50
  302. package/src/blobManager.ts +802 -541
  303. package/src/connectionTelemetry.ts +280 -249
  304. package/src/containerHandleContext.ts +27 -29
  305. package/src/containerRuntime.ts +3125 -2982
  306. package/src/dataStore.ts +172 -159
  307. package/src/dataStoreContext.ts +1049 -992
  308. package/src/dataStoreContexts.ts +178 -161
  309. package/src/dataStoreRegistry.ts +25 -20
  310. package/src/dataStores.ts +785 -711
  311. package/src/deltaScheduler.ts +158 -150
  312. package/src/garbageCollection.ts +1797 -1558
  313. package/src/garbageCollectionConstants.ts +38 -0
  314. package/src/garbageCollectionHelpers.ts +37 -0
  315. package/src/gcSweepReadyUsageDetection.ts +90 -84
  316. package/src/index.ts +68 -69
  317. package/src/opLifecycle/batchManager.ts +167 -0
  318. package/src/opLifecycle/definitions.ts +70 -0
  319. package/src/opLifecycle/index.ts +18 -0
  320. package/src/opLifecycle/opCompressor.ts +82 -0
  321. package/src/opLifecycle/opDecompressor.ts +124 -0
  322. package/src/opLifecycle/opSplitter.ts +238 -0
  323. package/src/opLifecycle/outbox.ts +228 -0
  324. package/src/opLifecycle/remoteMessageProcessor.ts +106 -0
  325. package/src/opProperties.ts +11 -9
  326. package/src/orderedClientElection.ts +489 -457
  327. package/src/packageVersion.ts +1 -1
  328. package/src/pendingStateManager.ts +379 -381
  329. package/src/runWhileConnectedCoordinator.ts +78 -71
  330. package/src/runningSummarizer.ts +619 -582
  331. package/src/scheduleManager.ts +299 -280
  332. package/src/serializedSnapshotStorage.ts +116 -111
  333. package/src/summarizer.ts +417 -381
  334. package/src/summarizerClientElection.ts +107 -129
  335. package/src/summarizerHandle.ts +11 -9
  336. package/src/summarizerHeuristics.ts +183 -186
  337. package/src/summarizerTypes.ts +344 -333
  338. package/src/summaryCollection.ts +378 -349
  339. package/src/summaryFormat.ts +146 -127
  340. package/src/summaryGenerator.ts +464 -406
  341. package/src/summaryManager.ts +377 -348
  342. package/src/throttler.ts +131 -122
  343. package/tsconfig.esnext.json +6 -6
  344. package/tsconfig.json +9 -13
  345. package/dist/batchManager.d.ts +0 -42
  346. package/dist/batchManager.d.ts.map +0 -1
  347. package/dist/batchManager.js +0 -83
  348. package/dist/batchManager.js.map +0 -1
  349. package/dist/opCompressor.d.ts.map +0 -1
  350. package/dist/opCompressor.js +0 -50
  351. package/dist/opCompressor.js.map +0 -1
  352. package/dist/opDecompressor.d.ts.map +0 -1
  353. package/dist/opDecompressor.js.map +0 -1
  354. package/lib/batchManager.d.ts +0 -42
  355. package/lib/batchManager.d.ts.map +0 -1
  356. package/lib/batchManager.js +0 -79
  357. package/lib/batchManager.js.map +0 -1
  358. package/lib/opCompressor.d.ts.map +0 -1
  359. package/lib/opCompressor.js +0 -46
  360. package/lib/opCompressor.js.map +0 -1
  361. package/lib/opDecompressor.d.ts.map +0 -1
  362. package/lib/opDecompressor.js.map +0 -1
  363. package/src/batchManager.ts +0 -108
  364. package/src/opCompressor.ts +0 -59
  365. package/src/opDecompressor.ts +0 -82
@@ -7,9 +7,7 @@ import { IDisposable } from "@fluidframework/common-definitions";
7
7
  import { assert, Lazy } from "@fluidframework/common-utils";
8
8
  import { ICriticalContainerError } from "@fluidframework/container-definitions";
9
9
  import { DataProcessingError } from "@fluidframework/container-utils";
10
- import {
11
- ISequencedDocumentMessage,
12
- } from "@fluidframework/protocol-definitions";
10
+ import { ISequencedDocumentMessage } from "@fluidframework/protocol-definitions";
13
11
  import Deque from "double-ended-queue";
14
12
  import { ContainerMessageType } from "./containerRuntime";
15
13
  import { pkgVersion } from "./packageVersion";
@@ -19,48 +17,49 @@ import { pkgVersion } from "./packageVersion";
19
17
  * ContainerRuntime. This message has either not been ack'd by the server or has not been submitted to the server yet.
20
18
  */
21
19
  export interface IPendingMessage {
22
- type: "message";
23
- messageType: ContainerMessageType;
24
- clientSequenceNumber: number;
25
- referenceSequenceNumber: number;
26
- content: any;
27
- localOpMetadata: unknown;
28
- opMetadata: Record<string, unknown> | undefined;
20
+ type: "message";
21
+ messageType: ContainerMessageType;
22
+ clientSequenceNumber: number;
23
+ referenceSequenceNumber: number;
24
+ content: any;
25
+ localOpMetadata: unknown;
26
+ opMetadata: Record<string, unknown> | undefined;
29
27
  }
30
28
 
31
29
  /**
32
30
  * This represents an explicit flush call and is added to the pending queue when flush is called on the ContainerRuntime
33
31
  * to flush pending messages.
32
+ * @deprecated Use batch metadata on IPendingMessage instead. To be removed in 2.0.0-internal.4.0.0 (AB#2496)
34
33
  */
35
34
  export interface IPendingFlush {
36
- type: "flush";
35
+ type: "flush";
37
36
  }
38
37
 
39
38
  export type IPendingState = IPendingMessage | IPendingFlush;
40
39
 
41
40
  export interface IPendingLocalState {
42
- /**
43
- * list of pending states, including ops and batch information
44
- */
45
- pendingStates: IPendingState[];
41
+ /**
42
+ * list of pending states, including ops and batch information
43
+ */
44
+ pendingStates: IPendingState[];
46
45
  }
47
46
 
48
- export interface IRuntimeStateHandler{
49
- connected(): boolean;
50
- clientId(): string | undefined;
51
- close(error?: ICriticalContainerError): void;
52
- applyStashedOp: (type: ContainerMessageType, content: ISequencedDocumentMessage) => Promise<unknown>;
53
- flush(): void;
54
- reSubmit(
55
- type: ContainerMessageType,
56
- content: any,
57
- localOpMetadata: unknown,
58
- opMetadata: Record<string, unknown> | undefined): void;
59
- rollback(
60
- type: ContainerMessageType,
61
- content: any,
62
- localOpMetadata: unknown): void;
63
- orderSequentially(callback: () => void): void;
47
+ export interface IRuntimeStateHandler {
48
+ connected(): boolean;
49
+ clientId(): string | undefined;
50
+ close(error?: ICriticalContainerError): void;
51
+ applyStashedOp: (
52
+ type: ContainerMessageType,
53
+ content: ISequencedDocumentMessage,
54
+ ) => Promise<unknown>;
55
+ reSubmit(
56
+ type: ContainerMessageType,
57
+ content: any,
58
+ localOpMetadata: unknown,
59
+ opMetadata: Record<string, unknown> | undefined,
60
+ ): void;
61
+ rollback(type: ContainerMessageType, content: any, localOpMetadata: unknown): void;
62
+ orderSequentially(callback: () => void): void;
64
63
  }
65
64
 
66
65
  /**
@@ -73,354 +72,353 @@ export interface IRuntimeStateHandler{
73
72
  * It verifies that all the ops are acked, are received in the right order and batch information is correct.
74
73
  */
75
74
  export class PendingStateManager implements IDisposable {
76
- private readonly pendingStates = new Deque<IPendingState>();
77
- private readonly initialStates: Deque<IPendingState>;
78
- private readonly disposeOnce = new Lazy<void>(() => {
79
- this.initialStates.clear();
80
- this.pendingStates.clear();
81
- });
82
-
83
- // Maintains the count of messages that are currently unacked.
84
- private _pendingMessagesCount: number = 0;
85
- public get pendingMessagesCount(): number {
86
- return this._pendingMessagesCount;
87
- }
88
-
89
- // Indicates whether we are processing a batch.
90
- private isProcessingBatch: boolean = false;
91
-
92
- // This stores the first message in the batch that we are processing. This is used to verify that we get
93
- // the correct batch metadata.
94
- private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;
95
-
96
- private clientId: string | undefined;
97
-
98
- /**
99
- * Called to check if there are any pending messages in the pending state queue.
100
- * @returns A boolean indicating whether there are messages or not.
101
- */
102
- public hasPendingMessages(): boolean {
103
- return this._pendingMessagesCount !== 0 || !this.initialStates.isEmpty();
104
- }
105
-
106
- public getLocalState(): IPendingLocalState | undefined {
107
- assert(this.initialStates.isEmpty(), 0x2e9 /* "Must call getLocalState() after applying initial states" */);
108
- if (this.hasPendingMessages()) {
109
- return {
110
- pendingStates: this.pendingStates.toArray().map(
111
- // delete localOpMetadata since it may not be serializable
112
- // and will be regenerated by applyStashedOp()
113
- (state) => state.type === "message" ? { ...state, localOpMetadata: undefined } : state),
114
- };
115
- }
116
- }
117
-
118
- constructor(
119
- private readonly stateHandler: IRuntimeStateHandler,
120
- initialLocalState: IPendingLocalState | undefined,
121
- ) {
122
- this.initialStates = new Deque<IPendingState>(initialLocalState?.pendingStates ?? []);
123
- }
124
-
125
- public get disposed() { return this.disposeOnce.evaluated; }
126
- public readonly dispose = () => this.disposeOnce.value;
127
-
128
- /**
129
- * Called when a message is submitted locally. Adds the message and the associated details to the pending state
130
- * queue.
131
- * @param type - The container message type.
132
- * @param clientSequenceNumber - The clientSequenceNumber associated with the message.
133
- * @param content - The message content.
134
- * @param localOpMetadata - The local metadata associated with the message.
135
- */
136
- public onSubmitMessage(
137
- type: ContainerMessageType,
138
- clientSequenceNumber: number,
139
- referenceSequenceNumber: number,
140
- content: any,
141
- localOpMetadata: unknown,
142
- opMetadata: Record<string, unknown> | undefined,
143
- ) {
144
- const pendingMessage: IPendingMessage = {
145
- type: "message",
146
- messageType: type,
147
- clientSequenceNumber,
148
- referenceSequenceNumber,
149
- content,
150
- localOpMetadata,
151
- opMetadata,
152
- };
153
-
154
- this.pendingStates.push(pendingMessage);
155
-
156
- this._pendingMessagesCount++;
157
- }
158
-
159
- /**
160
- * Called when flush() is called on the ContainerRuntime to manually flush messages.
161
- */
162
- public onFlush() {
163
- // If the previous state is not a message, flush is a no-op.
164
- const previousState = this.pendingStates.peekBack();
165
- if (previousState?.type !== "message") {
166
- return;
167
- }
168
-
169
- // An explicit flush is interesting and is tracked only if there are messages sent in TurnBased mode.
170
- this.pendingStates.push({ type: "flush" });
171
- }
172
-
173
- /**
174
- * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted
175
- * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.
176
- */
177
- public async applyStashedOpsAt(seqNum?: number) {
178
- // apply stashed ops at sequence number
179
- while (!this.initialStates.isEmpty()) {
180
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
181
- const nextState = this.initialStates.peekFront()!;
182
- if (nextState.type === "message") {
183
- if (seqNum !== undefined) {
184
- if (nextState.referenceSequenceNumber > seqNum) {
185
- break; // nothing left to do at this sequence number
186
- } else if (nextState.referenceSequenceNumber < seqNum) {
187
- throw new Error("loaded from snapshot too recent to apply stashed ops");
188
- }
189
- }
190
-
191
- // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it
192
- const localOpMetadata =
193
- await this.stateHandler.applyStashedOp(nextState.messageType, nextState.content);
194
- nextState.localOpMetadata = localOpMetadata;
195
- }
196
-
197
- // then we push onto pendingStates which will cause PendingStateManager to resubmit when we connect
198
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
199
- const firstPendingState = this.initialStates.shift()!;
200
- this.pendingStates.push(firstPendingState);
201
- if (firstPendingState.type === "message") {
202
- this._pendingMessagesCount++;
203
- }
204
- }
205
- }
206
-
207
- /**
208
- * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that
209
- * the batch information was preserved for batch messages.
210
- * @param message - The message that got ack'd and needs to be processed.
211
- */
212
- public processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {
213
- // Pre-processing part - This may be the start of a batch.
214
- this.maybeProcessBatchBegin(message);
215
-
216
- // Get the next state from the pending queue and verify that it is of type "message".
217
- const pendingState = this.peekNextPendingState();
218
- assert(pendingState.type === "message", 0x169 /* "No pending message found for this remote message" */);
219
- this.pendingStates.shift();
220
-
221
- // Processing part - Verify that there has been no data corruption.
222
- // The clientSequenceNumber of the incoming message must match that of the pending message.
223
- if (pendingState.clientSequenceNumber !== message.clientSequenceNumber) {
224
- // Close the container because this could indicate data corruption.
225
- const error = DataProcessingError.create(
226
- "pending local message clientSequenceNumber mismatch",
227
- "unexpectedAckReceived",
228
- message,
229
- { expectedClientSequenceNumber: pendingState.clientSequenceNumber },
230
- );
231
-
232
- this.stateHandler.close(error);
233
- return;
234
- }
235
-
236
- this._pendingMessagesCount--;
237
- assert(this._pendingMessagesCount >= 0, 0x3d6 /* positive */);
238
-
239
- // Post-processing part - If we are processing a batch then this could be the last message in the batch.
240
- this.maybeProcessBatchEnd(message);
241
-
242
- return pendingState.localOpMetadata;
243
- }
244
-
245
- /**
246
- * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.
247
- * @param message - The message that is being processed.
248
- */
249
- private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {
250
- /**
251
- * We are checking if the next message is the start of a batch. It can happen in the following scenarios:
252
- *
253
- * 1. The FlushMode was set to TurnBased before this message was sent.
254
- *
255
- * 2. The FlushMode was already TurnBased and a flush was called before this message was sent. This essentially
256
- * means that the flush marked the end of a previous batch and beginning of a new batch.
257
- *
258
- * Keep reading pending states from the queue until we encounter a message. It's possible that the FlushMode was
259
- * updated a bunch of times without sending any messages.
260
- */
261
- while (this.peekNextPendingState().type !== "message") {
262
- this.pendingStates.shift();
263
- }
264
-
265
- // This message is the first in a batch if the "batch" property on the metadata is set to true
266
- if (message.metadata?.batch) {
267
- // We should not already be processing a batch and there should be no pending batch begin message.
268
- assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,
269
- 0x16b /* "The pending batch state indicates we are already processing a batch" */);
270
-
271
- // Set the pending batch state indicating we have started processing a batch.
272
- this.pendingBatchBeginMessage = message;
273
- this.isProcessingBatch = true;
274
- }
275
- }
276
-
277
- /**
278
- * This message could be the last message in batch. If so, clear batch state since the batch is complete.
279
- * @param message - The message that is being processed.
280
- */
281
- private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {
282
- if (!this.isProcessingBatch) {
283
- return;
284
- }
285
-
286
- const nextPendingState = this.peekNextPendingState();
287
- if (nextPendingState.type === "message") {
288
- return;
289
- }
290
-
291
- // There should be a pending batch begin message.
292
- assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* "There is no pending batch begin message" */);
293
-
294
- // Get the batch begin metadata from the first message in the batch.
295
- const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;
296
-
297
- // There could be just a single message in the batch. If so, it should not have any batch metadata. If there
298
- // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.
299
- if (this.pendingBatchBeginMessage === message) {
300
- assert(batchBeginMetadata === undefined,
301
- 0x16e /* "Batch with single message should not have batch metadata" */);
302
- } else {
303
- // Get the batch metadata from the last message in the batch.
304
- const batchEndMetadata = message.metadata?.batch;
305
- if (batchBeginMetadata !== true || batchEndMetadata !== false) {
306
- this.stateHandler.close(DataProcessingError.create(
307
- "Pending batch inconsistency", // Formerly known as asserts 0x16f and 0x170
308
- "processPendingLocalMessage",
309
- message,
310
- {
311
- runtimeVersion: pkgVersion,
312
- batchClientId: this.pendingBatchBeginMessage.clientId,
313
- clientId: this.stateHandler.clientId(),
314
- hasBatchStart: batchBeginMetadata === true,
315
- hasBatchEnd: batchEndMetadata === false,
316
- messageType: message.type,
317
- batchStartSequenceNumber: this.pendingBatchBeginMessage.clientSequenceNumber,
318
- pendingMessagesCount: this.pendingMessagesCount,
319
- nextPendingState: nextPendingState.type,
320
- }));
321
- }
322
- }
323
-
324
- // Clear the pending batch state now that we have processed the entire batch.
325
- this.pendingBatchBeginMessage = undefined;
326
- this.isProcessingBatch = false;
327
- }
328
-
329
- /**
330
- * Returns the next pending state from the pending state queue.
331
- */
332
- private peekNextPendingState(): IPendingState {
333
- const nextPendingState = this.pendingStates.peekFront();
334
- assert(!!nextPendingState, 0x171 /* "No pending state found for the remote message" */);
335
- return nextPendingState;
336
- }
337
-
338
- /**
339
- * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending
340
- * states in its queue. This includes triggering resubmission of unacked ops.
341
- */
342
- public replayPendingStates() {
343
- assert(this.stateHandler.connected(), 0x172 /* "The connection state is not consistent with the runtime" */);
344
-
345
- // This assert suggests we are about to send same ops twice, which will result in data loss.
346
- assert(this.clientId !== this.stateHandler.clientId(),
347
- 0x173 /* "replayPendingStates called twice for same clientId!" */);
348
- this.clientId = this.stateHandler.clientId();
349
-
350
- assert(this.initialStates.isEmpty(), 0x174 /* "initial states should be empty before replaying pending" */);
351
-
352
- let pendingStatesCount = this.pendingStates.length;
353
- if (pendingStatesCount === 0) {
354
- return;
355
- }
356
-
357
- // Reset the pending message count because all these messages will be removed from the queue.
358
- this._pendingMessagesCount = 0;
359
-
360
- const messageBatchQueue = new Deque<IPendingMessage>();
361
-
362
- // Process exactly `pendingStatesCount` items in the queue as it represents the number of states that were
363
- // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue
364
- // which must not be replayed.
365
- while (pendingStatesCount > 0) {
366
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
367
- const pendingState = this.pendingStates.shift()!;
368
- switch (pendingState.type) {
369
- case "message":
370
- assert(pendingState.opMetadata?.batch !== false || messageBatchQueue.length > 0,
371
- 0x41b /* We cannot process batches in chunks */);
372
- /**
373
- * We want to ensure grouped messages get processed in a batch.
374
- * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will
375
- * either receive the whole batch ack or nothing at all.
376
- */
377
- if (messageBatchQueue.length > 0 || pendingState.opMetadata?.batch) {
378
- messageBatchQueue.enqueue(pendingState);
379
- } else {
380
- this.stateHandler.reSubmit(
381
- pendingState.messageType,
382
- pendingState.content,
383
- pendingState.localOpMetadata,
384
- pendingState.opMetadata);
385
- }
386
- break;
387
- case "flush":
388
- /**
389
- * A "flush" call can indicate the end of a batch.
390
- * We can't rely on the "batch" property in the message metadata as it gets
391
- * updated elsewhere and it is not the same object instance that gets updated.
392
- */
393
- if (messageBatchQueue.length > 0) {
394
- this.stateHandler.orderSequentially(() => {
395
- while (messageBatchQueue.length > 0) {
396
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
397
- const message = messageBatchQueue.dequeue()!;
398
- this.stateHandler.reSubmit(
399
- message.messageType,
400
- message.content,
401
- message.localOpMetadata,
402
- message.opMetadata);
403
- }
404
- });
405
- }
406
- assert(messageBatchQueue.length === 0, 0x41c /* cannot flush in the middle of a batch */);
407
- this.stateHandler.flush();
408
- break;
409
- default:
410
- break;
411
- }
412
- pendingStatesCount--;
413
- }
414
-
415
- // There are some cases where ops are stashed but not flushed. We need to ensure they are resubmitted
416
- while (messageBatchQueue.length > 0) {
417
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
418
- const message = messageBatchQueue.dequeue()!;
419
- this.stateHandler.reSubmit(
420
- message.messageType,
421
- message.content,
422
- message.localOpMetadata,
423
- message.opMetadata);
424
- }
425
- }
75
+ private readonly pendingMessages = new Deque<IPendingMessage>();
76
+ private readonly initialMessages = new Deque<IPendingMessage>();
77
+ private readonly disposeOnce = new Lazy<void>(() => {
78
+ this.initialMessages.clear();
79
+ this.pendingMessages.clear();
80
+ });
81
+
82
+ public get pendingMessagesCount(): number {
83
+ return this.pendingMessages.length;
84
+ }
85
+
86
+ // Indicates whether we are processing a batch.
87
+ private isProcessingBatch: boolean = false;
88
+
89
+ // This stores the first message in the batch that we are processing. This is used to verify that we get
90
+ // the correct batch metadata.
91
+ private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;
92
+
93
+ private clientId: string | undefined;
94
+
95
+ /**
96
+ * Called to check if there are any pending messages in the pending message queue.
97
+ * @returns A boolean indicating whether there are messages or not.
98
+ */
99
+ public hasPendingMessages(): boolean {
100
+ return !this.pendingMessages.isEmpty() || !this.initialMessages.isEmpty();
101
+ }
102
+
103
+ public getLocalState(): IPendingLocalState | undefined {
104
+ assert(
105
+ this.initialMessages.isEmpty(),
106
+ 0x2e9 /* "Must call getLocalState() after applying initial states" */,
107
+ );
108
+ if (!this.pendingMessages.isEmpty()) {
109
+ return {
110
+ pendingStates: this.pendingMessages.toArray().reduce((arr, message) => {
111
+ // delete localOpMetadata since it may not be serializable
112
+ // and will be regenerated by applyStashedOp()
113
+ arr.push({ ...message, localOpMetadata: undefined });
114
+
115
+ // TODO: Remove in 2.0.0-internal.4.0.0 (AB#2496)
116
+ if (message.opMetadata?.batch === false) {
117
+ arr.push({ type: "flush" });
118
+ }
119
+ return arr;
120
+ }, new Array<IPendingState>()),
121
+ };
122
+ }
123
+ }
124
+
125
+ constructor(
126
+ private readonly stateHandler: IRuntimeStateHandler,
127
+ initialLocalState: IPendingLocalState | undefined,
128
+ ) {
129
+ /**
130
+ * Convert old local state format to the new format
131
+ * The old format contained "flush" messages as the indicator of batch ends
132
+ * The new format instead uses batch metadata on the last message to indicate batch ends
133
+ * ! TODO: Remove this conversion in "2.0.0-internal.4.0.0" as rollback from future version will be new format
134
+ * AB#2496 tracks removal
135
+ */
136
+ if (initialLocalState?.pendingStates) {
137
+ const pendingStates = initialLocalState?.pendingStates;
138
+ let currentlyBatching = false;
139
+ for (let i = 0; i < pendingStates.length; i++) {
140
+ const initialState = pendingStates[i];
141
+
142
+ // Skip over "flush" messages
143
+ if (initialState.type === "message") {
144
+ if (initialState.opMetadata?.batch) {
145
+ currentlyBatching = true;
146
+ } else if (initialState.opMetadata?.batch === false) {
147
+ currentlyBatching = false;
148
+ } else if (
149
+ // End of batch if we are currently batching and this is last message or next message is flush
150
+ currentlyBatching &&
151
+ (i === pendingStates.length - 1 || pendingStates[i + 1].type === "flush")
152
+ ) {
153
+ currentlyBatching = false;
154
+ initialState.opMetadata = { ...initialState.opMetadata, batch: false };
155
+ }
156
+ this.initialMessages.push(initialState);
157
+ }
158
+ }
159
+ }
160
+ }
161
+
162
+ public get disposed() {
163
+ return this.disposeOnce.evaluated;
164
+ }
165
+ public readonly dispose = () => this.disposeOnce.value;
166
+
167
+ /**
168
+ * Called when a message is submitted locally. Adds the message and the associated details to the pending state
169
+ * queue.
170
+ * @param type - The container message type.
171
+ * @param clientSequenceNumber - The clientSequenceNumber associated with the message.
172
+ * @param content - The message content.
173
+ * @param localOpMetadata - The local metadata associated with the message.
174
+ */
175
+ public onSubmitMessage(
176
+ type: ContainerMessageType,
177
+ clientSequenceNumber: number,
178
+ referenceSequenceNumber: number,
179
+ content: any,
180
+ localOpMetadata: unknown,
181
+ opMetadata: Record<string, unknown> | undefined,
182
+ ) {
183
+ const pendingMessage: IPendingMessage = {
184
+ type: "message",
185
+ messageType: type,
186
+ clientSequenceNumber,
187
+ referenceSequenceNumber,
188
+ content,
189
+ localOpMetadata,
190
+ opMetadata,
191
+ };
192
+
193
+ this.pendingMessages.push(pendingMessage);
194
+ }
195
+
196
+ /**
197
+ * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted
198
+ * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.
199
+ */
200
+ public async applyStashedOpsAt(seqNum?: number) {
201
+ // apply stashed ops at sequence number
202
+ while (!this.initialMessages.isEmpty()) {
203
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
204
+ const nextMessage = this.initialMessages.peekFront()!;
205
+ if (seqNum !== undefined) {
206
+ if (nextMessage.referenceSequenceNumber > seqNum) {
207
+ break; // nothing left to do at this sequence number
208
+ }
209
+ if (nextMessage.referenceSequenceNumber < seqNum) {
210
+ throw new Error("loaded from snapshot too recent to apply stashed ops");
211
+ }
212
+ }
213
+
214
+ // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it
215
+ const localOpMetadata = await this.stateHandler.applyStashedOp(
216
+ nextMessage.messageType,
217
+ nextMessage.content,
218
+ );
219
+ nextMessage.localOpMetadata = localOpMetadata;
220
+
221
+ // then we push onto pendingMessages which will cause PendingStateManager to resubmit when we connect
222
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
223
+ this.pendingMessages.push(this.initialMessages.shift()!);
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that
229
+ * the batch information was preserved for batch messages.
230
+ * @param message - The message that got ack'd and needs to be processed.
231
+ */
232
+ public processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {
233
+ // Pre-processing part - This may be the start of a batch.
234
+ this.maybeProcessBatchBegin(message);
235
+
236
+ // Get the next message from the pending queue. Verify a message exists.
237
+ const pendingMessage = this.pendingMessages.peekFront();
238
+ assert(
239
+ pendingMessage !== undefined,
240
+ 0x169 /* "No pending message found for this remote message" */,
241
+ );
242
+ this.pendingMessages.shift();
243
+
244
+ // Processing part - Verify that there has been no data corruption.
245
+ // The clientSequenceNumber of the incoming message must match that of the pending message.
246
+ if (pendingMessage.clientSequenceNumber !== message.clientSequenceNumber) {
247
+ // Close the container because this could indicate data corruption.
248
+ const error = DataProcessingError.create(
249
+ "pending local message clientSequenceNumber mismatch",
250
+ "unexpectedAckReceived",
251
+ message,
252
+ { expectedClientSequenceNumber: pendingMessage.clientSequenceNumber },
253
+ );
254
+
255
+ this.stateHandler.close(error);
256
+ return;
257
+ }
258
+
259
+ // Post-processing part - If we are processing a batch then this could be the last message in the batch.
260
+ this.maybeProcessBatchEnd(message);
261
+
262
+ return pendingMessage.localOpMetadata;
263
+ }
264
+
265
+ /**
266
+ * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.
267
+ * @param message - The message that is being processed.
268
+ */
269
+ private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {
270
+ // This message is the first in a batch if the "batch" property on the metadata is set to true
271
+ if (message.metadata?.batch) {
272
+ // We should not already be processing a batch and there should be no pending batch begin message.
273
+ assert(
274
+ !this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,
275
+ 0x16b /* "The pending batch state indicates we are already processing a batch" */,
276
+ );
277
+
278
+ // Set the pending batch state indicating we have started processing a batch.
279
+ this.pendingBatchBeginMessage = message;
280
+ this.isProcessingBatch = true;
281
+ }
282
+ }
283
+
284
+ /**
285
+ * This message could be the last message in batch. If so, clear batch state since the batch is complete.
286
+ * @param message - The message that is being processed.
287
+ */
288
+ private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {
289
+ if (!this.isProcessingBatch) {
290
+ return;
291
+ }
292
+
293
+ // There should be a pending batch begin message.
294
+ assert(
295
+ this.pendingBatchBeginMessage !== undefined,
296
+ 0x16d /* "There is no pending batch begin message" */,
297
+ );
298
+
299
+ const batchEndMetadata = message.metadata?.batch;
300
+ if (this.pendingMessages.isEmpty() || batchEndMetadata === false) {
301
+ // Get the batch begin metadata from the first message in the batch.
302
+ const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;
303
+
304
+ // There could be just a single message in the batch. If so, it should not have any batch metadata. If there
305
+ // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.
306
+ if (this.pendingBatchBeginMessage === message) {
307
+ assert(
308
+ batchBeginMetadata === undefined,
309
+ 0x16e /* "Batch with single message should not have batch metadata" */,
310
+ );
311
+ } else {
312
+ if (batchBeginMetadata !== true || batchEndMetadata !== false) {
313
+ this.stateHandler.close(
314
+ DataProcessingError.create(
315
+ "Pending batch inconsistency", // Formerly known as asserts 0x16f and 0x170
316
+ "processPendingLocalMessage",
317
+ message,
318
+ {
319
+ runtimeVersion: pkgVersion,
320
+ batchClientId: this.pendingBatchBeginMessage.clientId,
321
+ clientId: this.stateHandler.clientId(),
322
+ hasBatchStart: batchBeginMetadata === true,
323
+ hasBatchEnd: batchEndMetadata === false,
324
+ messageType: message.type,
325
+ batchStartSequenceNumber:
326
+ this.pendingBatchBeginMessage.clientSequenceNumber,
327
+ pendingMessagesCount: this.pendingMessagesCount,
328
+ },
329
+ ),
330
+ );
331
+ }
332
+ }
333
+
334
+ // Clear the pending batch state now that we have processed the entire batch.
335
+ this.pendingBatchBeginMessage = undefined;
336
+ this.isProcessingBatch = false;
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending
342
+ * states in its queue. This includes triggering resubmission of unacked ops.
343
+ */
344
+ public replayPendingStates() {
345
+ assert(
346
+ this.stateHandler.connected(),
347
+ 0x172 /* "The connection state is not consistent with the runtime" */,
348
+ );
349
+
350
+ // This assert suggests we are about to send same ops twice, which will result in data loss.
351
+ assert(
352
+ this.clientId !== this.stateHandler.clientId(),
353
+ 0x173 /* "replayPendingStates called twice for same clientId!" */,
354
+ );
355
+ this.clientId = this.stateHandler.clientId();
356
+
357
+ assert(
358
+ this.initialMessages.isEmpty(),
359
+ 0x174 /* "initial states should be empty before replaying pending" */,
360
+ );
361
+
362
+ let pendingMessagesCount = this.pendingMessages.length;
363
+ if (pendingMessagesCount === 0) {
364
+ return;
365
+ }
366
+
367
+ // Process exactly `pendingMessagesCount` items in the queue as it represents the number of messages that were
368
+ // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue
369
+ // which must not be replayed.
370
+ while (pendingMessagesCount > 0) {
371
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
372
+ let pendingMessage = this.pendingMessages.shift()!;
373
+ pendingMessagesCount--;
374
+ assert(
375
+ pendingMessage.opMetadata?.batch !== false,
376
+ 0x41b /* We cannot process batches in chunks */,
377
+ );
378
+
379
+ /**
380
+ * We want to ensure grouped messages get processed in a batch.
381
+ * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will
382
+ * either receive the whole batch ack or nothing at all.
383
+ */
384
+ if (pendingMessage.opMetadata?.batch) {
385
+ assert(
386
+ pendingMessagesCount > 0,
387
+ 0x554 /* Last pending message cannot be a batch begin */,
388
+ );
389
+
390
+ this.stateHandler.orderSequentially(() => {
391
+ while (pendingMessagesCount >= 0) {
392
+ // check is >= because batch end may be last pending message
393
+ this.stateHandler.reSubmit(
394
+ pendingMessage.messageType,
395
+ pendingMessage.content,
396
+ pendingMessage.localOpMetadata,
397
+ pendingMessage.opMetadata,
398
+ );
399
+
400
+ if (pendingMessage.opMetadata?.batch === false) {
401
+ break;
402
+ }
403
+ assert(pendingMessagesCount > 0, 0x555 /* No batch end found */);
404
+
405
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
406
+ pendingMessage = this.pendingMessages.shift()!;
407
+ pendingMessagesCount--;
408
+ assert(
409
+ pendingMessage.opMetadata?.batch !== true,
410
+ 0x556 /* Batch start needs a corresponding batch end */,
411
+ );
412
+ }
413
+ });
414
+ } else {
415
+ this.stateHandler.reSubmit(
416
+ pendingMessage.messageType,
417
+ pendingMessage.content,
418
+ pendingMessage.localOpMetadata,
419
+ pendingMessage.opMetadata,
420
+ );
421
+ }
422
+ }
423
+ }
426
424
  }