@fluidframework/container-runtime 2.0.0-dev.2.3.0.115467 → 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 (340) 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 +53 -34
  9. package/dist/blobManager.d.ts.map +1 -1
  10. package/dist/blobManager.js +236 -124
  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 +95 -46
  19. package/dist/containerRuntime.d.ts.map +1 -1
  20. package/dist/containerRuntime.js +288 -135
  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 +38 -21
  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 +68 -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 +50 -26
  43. package/dist/garbageCollection.d.ts.map +1 -1
  44. package/dist/garbageCollection.js +348 -196
  45. package/dist/garbageCollection.js.map +1 -1
  46. package/dist/garbageCollectionConstants.d.ts +7 -3
  47. package/dist/garbageCollectionConstants.d.ts.map +1 -1
  48. package/dist/garbageCollectionConstants.js +10 -8
  49. package/dist/garbageCollectionConstants.js.map +1 -1
  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 +14 -10
  57. package/dist/gcSweepReadyUsageDetection.js.map +1 -1
  58. package/dist/index.d.ts +3 -4
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +3 -5
  61. package/dist/index.js.map +1 -1
  62. package/dist/opLifecycle/batchManager.d.ts +13 -1
  63. package/dist/opLifecycle/batchManager.d.ts.map +1 -1
  64. package/dist/opLifecycle/batchManager.js +48 -7
  65. package/dist/opLifecycle/batchManager.js.map +1 -1
  66. package/dist/opLifecycle/definitions.d.ts +25 -1
  67. package/dist/opLifecycle/definitions.d.ts.map +1 -1
  68. package/dist/opLifecycle/definitions.js.map +1 -1
  69. package/dist/opLifecycle/index.d.ts +2 -2
  70. package/dist/opLifecycle/index.d.ts.map +1 -1
  71. package/dist/opLifecycle/index.js +2 -1
  72. package/dist/opLifecycle/index.js.map +1 -1
  73. package/dist/opLifecycle/opCompressor.d.ts +1 -1
  74. package/dist/opLifecycle/opCompressor.d.ts.map +1 -1
  75. package/dist/opLifecycle/opCompressor.js +24 -10
  76. package/dist/opLifecycle/opCompressor.js.map +1 -1
  77. package/dist/opLifecycle/opDecompressor.d.ts +2 -1
  78. package/dist/opLifecycle/opDecompressor.d.ts.map +1 -1
  79. package/dist/opLifecycle/opDecompressor.js +33 -17
  80. package/dist/opLifecycle/opDecompressor.js.map +1 -1
  81. package/dist/opLifecycle/opSplitter.d.ts +34 -2
  82. package/dist/opLifecycle/opSplitter.d.ts.map +1 -1
  83. package/dist/opLifecycle/opSplitter.js +117 -5
  84. package/dist/opLifecycle/opSplitter.js.map +1 -1
  85. package/dist/opLifecycle/outbox.d.ts +5 -0
  86. package/dist/opLifecycle/outbox.d.ts.map +1 -1
  87. package/dist/opLifecycle/outbox.js +38 -27
  88. package/dist/opLifecycle/outbox.js.map +1 -1
  89. package/dist/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
  90. package/dist/opLifecycle/remoteMessageProcessor.js +17 -2
  91. package/dist/opLifecycle/remoteMessageProcessor.js.map +1 -1
  92. package/dist/opProperties.d.ts.map +1 -1
  93. package/dist/opProperties.js +1 -3
  94. package/dist/opProperties.js.map +1 -1
  95. package/dist/orderedClientElection.d.ts.map +1 -1
  96. package/dist/orderedClientElection.js +10 -4
  97. package/dist/orderedClientElection.js.map +1 -1
  98. package/dist/packageVersion.d.ts +1 -1
  99. package/dist/packageVersion.js +1 -1
  100. package/dist/packageVersion.js.map +1 -1
  101. package/dist/pendingStateManager.d.ts +4 -13
  102. package/dist/pendingStateManager.d.ts.map +1 -1
  103. package/dist/pendingStateManager.js +134 -161
  104. package/dist/pendingStateManager.js.map +1 -1
  105. package/dist/runWhileConnectedCoordinator.d.ts.map +1 -1
  106. package/dist/runWhileConnectedCoordinator.js.map +1 -1
  107. package/dist/runningSummarizer.d.ts.map +1 -1
  108. package/dist/runningSummarizer.js +34 -22
  109. package/dist/runningSummarizer.js.map +1 -1
  110. package/dist/scheduleManager.d.ts +0 -1
  111. package/dist/scheduleManager.d.ts.map +1 -1
  112. package/dist/scheduleManager.js +11 -21
  113. package/dist/scheduleManager.js.map +1 -1
  114. package/dist/serializedSnapshotStorage.d.ts.map +1 -1
  115. package/dist/serializedSnapshotStorage.js +3 -1
  116. package/dist/serializedSnapshotStorage.js.map +1 -1
  117. package/dist/summarizer.d.ts +2 -3
  118. package/dist/summarizer.d.ts.map +1 -1
  119. package/dist/summarizer.js +39 -18
  120. package/dist/summarizer.js.map +1 -1
  121. package/dist/summarizerClientElection.d.ts +1 -2
  122. package/dist/summarizerClientElection.d.ts.map +1 -1
  123. package/dist/summarizerClientElection.js +3 -30
  124. package/dist/summarizerClientElection.js.map +1 -1
  125. package/dist/summarizerHandle.d.ts.map +1 -1
  126. package/dist/summarizerHandle.js.map +1 -1
  127. package/dist/summarizerHeuristics.d.ts.map +1 -1
  128. package/dist/summarizerHeuristics.js +6 -9
  129. package/dist/summarizerHeuristics.js.map +1 -1
  130. package/dist/summarizerTypes.d.ts +22 -25
  131. package/dist/summarizerTypes.d.ts.map +1 -1
  132. package/dist/summarizerTypes.js.map +1 -1
  133. package/dist/summaryCollection.d.ts.map +1 -1
  134. package/dist/summaryCollection.js +18 -8
  135. package/dist/summaryCollection.js.map +1 -1
  136. package/dist/summaryFormat.d.ts.map +1 -1
  137. package/dist/summaryFormat.js +18 -11
  138. package/dist/summaryFormat.js.map +1 -1
  139. package/dist/summaryGenerator.d.ts.map +1 -1
  140. package/dist/summaryGenerator.js +32 -14
  141. package/dist/summaryGenerator.js.map +1 -1
  142. package/dist/summaryManager.d.ts.map +1 -1
  143. package/dist/summaryManager.js +21 -9
  144. package/dist/summaryManager.js.map +1 -1
  145. package/dist/throttler.d.ts +2 -2
  146. package/dist/throttler.d.ts.map +1 -1
  147. package/dist/throttler.js +4 -4
  148. package/dist/throttler.js.map +1 -1
  149. package/garbageCollection.md +15 -2
  150. package/lib/batchTracker.d.ts +1 -2
  151. package/lib/batchTracker.d.ts.map +1 -1
  152. package/lib/batchTracker.js +2 -1
  153. package/lib/batchTracker.js.map +1 -1
  154. package/lib/blobManager.d.ts +53 -34
  155. package/lib/blobManager.d.ts.map +1 -1
  156. package/lib/blobManager.js +239 -127
  157. package/lib/blobManager.js.map +1 -1
  158. package/lib/connectionTelemetry.d.ts.map +1 -1
  159. package/lib/connectionTelemetry.js +11 -9
  160. package/lib/connectionTelemetry.js.map +1 -1
  161. package/lib/containerHandleContext.d.ts.map +1 -1
  162. package/lib/containerHandleContext.js +3 -1
  163. package/lib/containerHandleContext.js.map +1 -1
  164. package/lib/containerRuntime.d.ts +95 -46
  165. package/lib/containerRuntime.d.ts.map +1 -1
  166. package/lib/containerRuntime.js +291 -138
  167. package/lib/containerRuntime.js.map +1 -1
  168. package/lib/dataStore.d.ts.map +1 -1
  169. package/lib/dataStore.js +11 -9
  170. package/lib/dataStore.js.map +1 -1
  171. package/lib/dataStoreContext.d.ts +2 -1
  172. package/lib/dataStoreContext.d.ts.map +1 -1
  173. package/lib/dataStoreContext.js +40 -23
  174. package/lib/dataStoreContext.js.map +1 -1
  175. package/lib/dataStoreContexts.d.ts.map +1 -1
  176. package/lib/dataStoreContexts.js +7 -3
  177. package/lib/dataStoreContexts.js.map +1 -1
  178. package/lib/dataStoreRegistry.d.ts.map +1 -1
  179. package/lib/dataStoreRegistry.js +3 -1
  180. package/lib/dataStoreRegistry.js.map +1 -1
  181. package/lib/dataStores.d.ts +12 -9
  182. package/lib/dataStores.d.ts.map +1 -1
  183. package/lib/dataStores.js +74 -52
  184. package/lib/dataStores.js.map +1 -1
  185. package/lib/deltaScheduler.d.ts.map +1 -1
  186. package/lib/deltaScheduler.js +9 -4
  187. package/lib/deltaScheduler.js.map +1 -1
  188. package/lib/garbageCollection.d.ts +50 -26
  189. package/lib/garbageCollection.d.ts.map +1 -1
  190. package/lib/garbageCollection.js +347 -195
  191. package/lib/garbageCollection.js.map +1 -1
  192. package/lib/garbageCollectionConstants.d.ts +7 -3
  193. package/lib/garbageCollectionConstants.d.ts.map +1 -1
  194. package/lib/garbageCollectionConstants.js +9 -7
  195. package/lib/garbageCollectionConstants.js.map +1 -1
  196. package/lib/garbageCollectionHelpers.d.ts +15 -0
  197. package/lib/garbageCollectionHelpers.d.ts.map +1 -0
  198. package/lib/garbageCollectionHelpers.js +23 -0
  199. package/lib/garbageCollectionHelpers.js.map +1 -0
  200. package/lib/gcSweepReadyUsageDetection.d.ts +5 -5
  201. package/lib/gcSweepReadyUsageDetection.d.ts.map +1 -1
  202. package/lib/gcSweepReadyUsageDetection.js +14 -10
  203. package/lib/gcSweepReadyUsageDetection.js.map +1 -1
  204. package/lib/index.d.ts +3 -4
  205. package/lib/index.d.ts.map +1 -1
  206. package/lib/index.js +2 -3
  207. package/lib/index.js.map +1 -1
  208. package/lib/opLifecycle/batchManager.d.ts +13 -1
  209. package/lib/opLifecycle/batchManager.d.ts.map +1 -1
  210. package/lib/opLifecycle/batchManager.js +48 -7
  211. package/lib/opLifecycle/batchManager.js.map +1 -1
  212. package/lib/opLifecycle/definitions.d.ts +25 -1
  213. package/lib/opLifecycle/definitions.d.ts.map +1 -1
  214. package/lib/opLifecycle/definitions.js.map +1 -1
  215. package/lib/opLifecycle/index.d.ts +2 -2
  216. package/lib/opLifecycle/index.d.ts.map +1 -1
  217. package/lib/opLifecycle/index.js +1 -1
  218. package/lib/opLifecycle/index.js.map +1 -1
  219. package/lib/opLifecycle/opCompressor.d.ts +1 -1
  220. package/lib/opLifecycle/opCompressor.d.ts.map +1 -1
  221. package/lib/opLifecycle/opCompressor.js +24 -10
  222. package/lib/opLifecycle/opCompressor.js.map +1 -1
  223. package/lib/opLifecycle/opDecompressor.d.ts +2 -1
  224. package/lib/opLifecycle/opDecompressor.d.ts.map +1 -1
  225. package/lib/opLifecycle/opDecompressor.js +33 -17
  226. package/lib/opLifecycle/opDecompressor.js.map +1 -1
  227. package/lib/opLifecycle/opSplitter.d.ts +34 -2
  228. package/lib/opLifecycle/opSplitter.d.ts.map +1 -1
  229. package/lib/opLifecycle/opSplitter.js +116 -5
  230. package/lib/opLifecycle/opSplitter.js.map +1 -1
  231. package/lib/opLifecycle/outbox.d.ts +5 -0
  232. package/lib/opLifecycle/outbox.d.ts.map +1 -1
  233. package/lib/opLifecycle/outbox.js +38 -27
  234. package/lib/opLifecycle/outbox.js.map +1 -1
  235. package/lib/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
  236. package/lib/opLifecycle/remoteMessageProcessor.js +17 -2
  237. package/lib/opLifecycle/remoteMessageProcessor.js.map +1 -1
  238. package/lib/opProperties.d.ts.map +1 -1
  239. package/lib/opProperties.js +1 -3
  240. package/lib/opProperties.js.map +1 -1
  241. package/lib/orderedClientElection.d.ts.map +1 -1
  242. package/lib/orderedClientElection.js +10 -4
  243. package/lib/orderedClientElection.js.map +1 -1
  244. package/lib/packageVersion.d.ts +1 -1
  245. package/lib/packageVersion.js +1 -1
  246. package/lib/packageVersion.js.map +1 -1
  247. package/lib/pendingStateManager.d.ts +4 -13
  248. package/lib/pendingStateManager.d.ts.map +1 -1
  249. package/lib/pendingStateManager.js +134 -161
  250. package/lib/pendingStateManager.js.map +1 -1
  251. package/lib/runWhileConnectedCoordinator.d.ts.map +1 -1
  252. package/lib/runWhileConnectedCoordinator.js.map +1 -1
  253. package/lib/runningSummarizer.d.ts.map +1 -1
  254. package/lib/runningSummarizer.js +35 -23
  255. package/lib/runningSummarizer.js.map +1 -1
  256. package/lib/scheduleManager.d.ts +0 -1
  257. package/lib/scheduleManager.d.ts.map +1 -1
  258. package/lib/scheduleManager.js +11 -21
  259. package/lib/scheduleManager.js.map +1 -1
  260. package/lib/serializedSnapshotStorage.d.ts.map +1 -1
  261. package/lib/serializedSnapshotStorage.js +3 -1
  262. package/lib/serializedSnapshotStorage.js.map +1 -1
  263. package/lib/summarizer.d.ts +2 -3
  264. package/lib/summarizer.d.ts.map +1 -1
  265. package/lib/summarizer.js +39 -18
  266. package/lib/summarizer.js.map +1 -1
  267. package/lib/summarizerClientElection.d.ts +1 -2
  268. package/lib/summarizerClientElection.d.ts.map +1 -1
  269. package/lib/summarizerClientElection.js +3 -30
  270. package/lib/summarizerClientElection.js.map +1 -1
  271. package/lib/summarizerHandle.d.ts.map +1 -1
  272. package/lib/summarizerHandle.js.map +1 -1
  273. package/lib/summarizerHeuristics.d.ts.map +1 -1
  274. package/lib/summarizerHeuristics.js +6 -9
  275. package/lib/summarizerHeuristics.js.map +1 -1
  276. package/lib/summarizerTypes.d.ts +22 -25
  277. package/lib/summarizerTypes.d.ts.map +1 -1
  278. package/lib/summarizerTypes.js.map +1 -1
  279. package/lib/summaryCollection.d.ts.map +1 -1
  280. package/lib/summaryCollection.js +18 -8
  281. package/lib/summaryCollection.js.map +1 -1
  282. package/lib/summaryFormat.d.ts.map +1 -1
  283. package/lib/summaryFormat.js +20 -13
  284. package/lib/summaryFormat.js.map +1 -1
  285. package/lib/summaryGenerator.d.ts.map +1 -1
  286. package/lib/summaryGenerator.js +32 -14
  287. package/lib/summaryGenerator.js.map +1 -1
  288. package/lib/summaryManager.d.ts.map +1 -1
  289. package/lib/summaryManager.js +21 -9
  290. package/lib/summaryManager.js.map +1 -1
  291. package/lib/throttler.d.ts +2 -2
  292. package/lib/throttler.d.ts.map +1 -1
  293. package/lib/throttler.js +4 -4
  294. package/lib/throttler.js.map +1 -1
  295. package/package.json +27 -24
  296. package/prettier.config.cjs +1 -1
  297. package/src/batchTracker.ts +55 -50
  298. package/src/blobManager.ts +799 -593
  299. package/src/connectionTelemetry.ts +280 -249
  300. package/src/containerHandleContext.ts +27 -29
  301. package/src/containerRuntime.ts +3123 -2793
  302. package/src/dataStore.ts +172 -159
  303. package/src/dataStoreContext.ts +1048 -991
  304. package/src/dataStoreContexts.ts +178 -161
  305. package/src/dataStoreRegistry.ts +25 -20
  306. package/src/dataStores.ts +784 -711
  307. package/src/deltaScheduler.ts +158 -150
  308. package/src/garbageCollection.ts +1795 -1546
  309. package/src/garbageCollectionConstants.ts +10 -7
  310. package/src/garbageCollectionHelpers.ts +37 -0
  311. package/src/gcSweepReadyUsageDetection.ts +89 -83
  312. package/src/index.ts +67 -69
  313. package/src/opLifecycle/batchManager.ts +148 -86
  314. package/src/opLifecycle/definitions.ts +45 -19
  315. package/src/opLifecycle/index.ts +6 -5
  316. package/src/opLifecycle/opCompressor.ts +57 -39
  317. package/src/opLifecycle/opDecompressor.ts +104 -64
  318. package/src/opLifecycle/opSplitter.ts +226 -66
  319. package/src/opLifecycle/outbox.ts +206 -182
  320. package/src/opLifecycle/remoteMessageProcessor.ts +63 -47
  321. package/src/opProperties.ts +11 -9
  322. package/src/orderedClientElection.ts +489 -457
  323. package/src/packageVersion.ts +1 -1
  324. package/src/pendingStateManager.ts +379 -381
  325. package/src/runWhileConnectedCoordinator.ts +78 -71
  326. package/src/runningSummarizer.ts +619 -582
  327. package/src/scheduleManager.ts +299 -280
  328. package/src/serializedSnapshotStorage.ts +116 -111
  329. package/src/summarizer.ts +417 -381
  330. package/src/summarizerClientElection.ts +107 -129
  331. package/src/summarizerHandle.ts +11 -9
  332. package/src/summarizerHeuristics.ts +183 -186
  333. package/src/summarizerTypes.ts +344 -333
  334. package/src/summaryCollection.ts +378 -349
  335. package/src/summaryFormat.ts +146 -127
  336. package/src/summaryGenerator.ts +464 -406
  337. package/src/summaryManager.ts +377 -348
  338. package/src/throttler.ts +131 -122
  339. package/tsconfig.esnext.json +6 -6
  340. package/tsconfig.json +9 -13
@@ -3,228 +3,239 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { ITelemetryLogger, ITelemetryPerformanceEvent } from "@fluidframework/common-definitions";
6
+ import { ITelemetryLogger } from "@fluidframework/common-definitions";
7
7
  import { assert, LazyPromise, Timer } from "@fluidframework/common-utils";
8
8
  import { ICriticalContainerError } from "@fluidframework/container-definitions";
9
- import { ClientSessionExpiredError, DataProcessingError, UsageError } from "@fluidframework/container-utils";
9
+ import {
10
+ ClientSessionExpiredError,
11
+ DataProcessingError,
12
+ UsageError,
13
+ } from "@fluidframework/container-utils";
10
14
  import { IRequestHeader } from "@fluidframework/core-interfaces";
11
15
  import {
12
- cloneGCData,
13
- concatGarbageCollectionStates,
14
- concatGarbageCollectionData,
15
- IGCResult,
16
- runGarbageCollection,
17
- unpackChildNodesGCDetails,
16
+ cloneGCData,
17
+ concatGarbageCollectionData,
18
+ getGCDataFromSnapshot,
19
+ IGCResult,
20
+ runGarbageCollection,
21
+ trimLeadingSlashes,
18
22
  } from "@fluidframework/garbage-collector";
19
23
  import { ISnapshotTree, SummaryType } from "@fluidframework/protocol-definitions";
20
24
  import {
21
- gcBlobKey,
22
- IGarbageCollectionData,
23
- IGarbageCollectionState,
24
- IGarbageCollectionDetailsBase,
25
- ISummarizeResult,
26
- ITelemetryContext,
27
- IGarbageCollectionNodeData,
28
- ISummaryTreeWithStats,
25
+ gcTreeKey,
26
+ gcBlobPrefix,
27
+ gcTombstoneBlobKey,
28
+ IGarbageCollectionData,
29
+ IGarbageCollectionDetailsBase,
30
+ IGarbageCollectionSnapshotData,
31
+ IGarbageCollectionState,
32
+ ISummarizeResult,
33
+ ITelemetryContext,
34
+ IGarbageCollectionNodeData,
35
+ IGarbageCollectionSummaryDetailsLegacy,
36
+ ISummaryTreeWithStats,
37
+ gcDeletedBlobKey,
29
38
  } from "@fluidframework/runtime-definitions";
30
39
  import {
31
- mergeStats,
32
- packagePathToTelemetryProperty,
33
- ReadAndParseBlob,
34
- RefreshSummaryResult,
35
- SummaryTreeBuilder,
40
+ mergeStats,
41
+ packagePathToTelemetryProperty,
42
+ ReadAndParseBlob,
43
+ RefreshSummaryResult,
44
+ SummaryTreeBuilder,
36
45
  } from "@fluidframework/runtime-utils";
37
46
  import {
38
- ChildLogger,
39
- generateStack,
40
- loggerToMonitoringContext,
41
- MonitoringContext,
42
- PerformanceEvent,
43
- TelemetryDataTag,
47
+ ChildLogger,
48
+ generateStack,
49
+ loggerToMonitoringContext,
50
+ MonitoringContext,
51
+ PerformanceEvent,
52
+ TelemetryDataTag,
44
53
  } from "@fluidframework/telemetry-utils";
45
54
 
46
55
  import { IGCRuntimeOptions, RuntimeHeaders } from "./containerRuntime";
47
56
  import { getSummaryForDatastores } from "./dataStores";
48
57
  import {
49
- defaultInactiveTimeoutMs,
50
- defaultSessionExpiryDurationMs,
51
- disableSweepLogKey,
52
- disableTombstoneKey,
53
- gcBlobPrefix,
54
- gcTestModeKey,
55
- gcTombstoneBlobKey,
56
- gcTreeKey,
57
- oneDayMs,
58
- runGCKey,
59
- runSessionExpiryKey,
60
- runSweepKey,
61
- trackGCStateKey
58
+ currentGCVersion,
59
+ defaultInactiveTimeoutMs,
60
+ defaultSessionExpiryDurationMs,
61
+ disableSweepLogKey,
62
+ disableTombstoneKey,
63
+ gcVersionUpgradeToV2Key,
64
+ gcTestModeKey,
65
+ oneDayMs,
66
+ runGCKey,
67
+ runSessionExpiryKey,
68
+ runSweepKey,
69
+ stableGCVersion,
70
+ trackGCStateKey,
62
71
  } from "./garbageCollectionConstants";
72
+ import { sendGCUnexpectedUsageEvent } from "./garbageCollectionHelpers";
63
73
  import { SweepReadyUsageDetectionHandler } from "./gcSweepReadyUsageDetection";
64
74
  import {
65
- getGCVersion,
66
- GCVersion,
67
- IContainerRuntimeMetadata,
68
- metadataBlobName,
69
- ReadFluidDataStoreAttributes,
70
- dataStoreAttributesBlobName,
71
- IGCMetadata,
72
- ICreateContainerMetadata,
75
+ getGCVersion,
76
+ GCVersion,
77
+ IContainerRuntimeMetadata,
78
+ metadataBlobName,
79
+ ReadFluidDataStoreAttributes,
80
+ dataStoreAttributesBlobName,
81
+ IGCMetadata,
82
+ ICreateContainerMetadata,
73
83
  } from "./summaryFormat";
74
84
 
75
- /** This is the current version of garbage collection. */
76
- const GCVersion = 1;
77
-
78
85
  /** The statistics of the system state after a garbage collection run. */
79
86
  export interface IGCStats {
80
- /** The number of nodes in the container. */
81
- nodeCount: number;
82
- /** The number of data stores in the container. */
83
- dataStoreCount: number;
84
- /** The number of attachment blobs in the container. */
85
- attachmentBlobCount: number;
86
- /** The number of unreferenced nodes in the container. */
87
- unrefNodeCount: number;
88
- /** The number of unreferenced data stores in the container. */
89
- unrefDataStoreCount: number;
90
- /** The number of unreferenced attachment blobs in the container. */
91
- unrefAttachmentBlobCount: number;
92
- /** The number of nodes whose reference state updated since last GC run. */
93
- updatedNodeCount: number;
94
- /** The number of data stores whose reference state updated since last GC run. */
95
- updatedDataStoreCount: number;
96
- /** The number of attachment blobs whose reference state updated since last GC run. */
97
- updatedAttachmentBlobCount: number;
87
+ /** The number of nodes in the container. */
88
+ nodeCount: number;
89
+ /** The number of data stores in the container. */
90
+ dataStoreCount: number;
91
+ /** The number of attachment blobs in the container. */
92
+ attachmentBlobCount: number;
93
+ /** The number of unreferenced nodes in the container. */
94
+ unrefNodeCount: number;
95
+ /** The number of unreferenced data stores in the container. */
96
+ unrefDataStoreCount: number;
97
+ /** The number of unreferenced attachment blobs in the container. */
98
+ unrefAttachmentBlobCount: number;
99
+ /** The number of nodes whose reference state updated since last GC run. */
100
+ updatedNodeCount: number;
101
+ /** The number of data stores whose reference state updated since last GC run. */
102
+ updatedDataStoreCount: number;
103
+ /** The number of attachment blobs whose reference state updated since last GC run. */
104
+ updatedAttachmentBlobCount: number;
98
105
  }
99
106
 
100
107
  /** The types of GC nodes in the GC reference graph. */
101
108
  export const GCNodeType = {
102
- // Nodes that are for data stores.
103
- DataStore: "DataStore",
104
- // Nodes that are within a data store. For example, DDS nodes.
105
- SubDataStore: "SubDataStore",
106
- // Nodes that are for attachment blobs, i.e., blobs uploaded via BlobManager.
107
- Blob: "Blob",
108
- // Nodes that are neither of the above. For example, root node.
109
- Other: "Other",
109
+ // Nodes that are for data stores.
110
+ DataStore: "DataStore",
111
+ // Nodes that are within a data store. For example, DDS nodes.
112
+ SubDataStore: "SubDataStore",
113
+ // Nodes that are for attachment blobs, i.e., blobs uploaded via BlobManager.
114
+ Blob: "Blob",
115
+ // Nodes that are neither of the above. For example, root node.
116
+ Other: "Other",
110
117
  };
111
118
  export type GCNodeType = typeof GCNodeType[keyof typeof GCNodeType];
112
119
 
113
120
  /** Defines the APIs for the runtime object to be passed to the garbage collector. */
114
121
  export interface IGarbageCollectionRuntime {
115
- /** Before GC runs, called to notify the runtime to update any pending GC state. */
116
- updateStateBeforeGC(): Promise<void>;
117
- /** Returns the garbage collection data of the runtime. */
118
- getGCData(fullGC?: boolean): Promise<IGarbageCollectionData>;
119
- /** After GC has run, called to notify the runtime of routes that are used in it. */
120
- updateUsedRoutes(usedRoutes: string[]): void;
121
- /** After GC has run, called to notify the runtime of routes that are unused in it. */
122
- updateUnusedRoutes(unusedRoutes: string[], tombstone: boolean): void;
123
- /** Returns a referenced timestamp to be used to track unreferenced nodes. */
124
- getCurrentReferenceTimestampMs(): number | undefined;
125
- /** Returns the type of the GC node. */
126
- getNodeType(nodePath: string): GCNodeType;
127
- /** Called when the runtime should close because of an error. */
128
- closeFn: (error?: ICriticalContainerError) => void;
122
+ /** Before GC runs, called to notify the runtime to update any pending GC state. */
123
+ updateStateBeforeGC(): Promise<void>;
124
+ /** Returns the garbage collection data of the runtime. */
125
+ getGCData(fullGC?: boolean): Promise<IGarbageCollectionData>;
126
+ /** After GC has run, called to notify the runtime of routes that are used in it. */
127
+ updateUsedRoutes(usedRoutes: string[]): void;
128
+ /** After GC has run, called to notify the runtime of routes that are unused in it. */
129
+ updateUnusedRoutes(unusedRoutes: string[]): void;
130
+ /** Called to notify the runtime of routes that are tombstones. */
131
+ updateTombstonedRoutes(tombstoneRoutes: string[]): void;
132
+ /** Returns a referenced timestamp to be used to track unreferenced nodes. */
133
+ getCurrentReferenceTimestampMs(): number | undefined;
134
+ /** Returns the type of the GC node. */
135
+ getNodeType(nodePath: string): GCNodeType;
136
+ /** Called when the runtime should close because of an error. */
137
+ closeFn: (error?: ICriticalContainerError) => void;
129
138
  }
130
139
 
131
140
  /** Defines the contract for the garbage collector. */
132
141
  export interface IGarbageCollector {
133
- /** Tells whether GC should run or not. */
134
- readonly shouldRunGC: boolean;
135
- /** Tells whether the GC state in summary needs to be reset in the next summary. */
136
- readonly summaryStateNeedsReset: boolean;
137
- readonly trackGCState: boolean;
138
- /** Initialize the state from the base snapshot after its creation. */
139
- initializeBaseState(): Promise<void>;
140
- /** Run garbage collection and update the reference / used state of the system. */
141
- collectGarbage(
142
- options: { logger?: ITelemetryLogger; runSweep?: boolean; fullGC?: boolean; },
143
- ): Promise<IGCStats | undefined>;
144
- /** Summarizes the GC data and returns it as a summary tree. */
145
- summarize(
146
- fullTree: boolean,
147
- trackState: boolean,
148
- telemetryContext?: ITelemetryContext,
149
- ): ISummarizeResult | undefined;
150
- /** Returns the garbage collector specific metadata to be written into the summary. */
151
- getMetadata(): IGCMetadata;
152
- /** Returns a map of each node id to its base GC details in the base summary. */
153
- getBaseGCDetails(): Promise<Map<string, IGarbageCollectionDetailsBase>>;
154
- /** Called when the latest summary of the system has been refreshed. */
155
- latestSummaryStateRefreshed(result: RefreshSummaryResult, readAndParseBlob: ReadAndParseBlob): Promise<void>;
156
- /** Called when a node is updated. Used to detect and log when an inactive node is changed or loaded. */
157
- nodeUpdated(
158
- nodePath: string,
159
- reason: "Loaded" | "Changed",
160
- timestampMs?: number,
161
- packagePath?: readonly string[],
162
- requestHeaders?: IRequestHeader,
163
- ): void;
164
- /** Called when a reference is added to a node. Used to identify nodes that were referenced between summaries. */
165
- addedOutboundReference(fromNodePath: string, toNodePath: string): void;
166
- setConnectionState(connected: boolean, clientId?: string): void;
167
- dispose(): void;
142
+ /** Tells whether GC should run or not. */
143
+ readonly shouldRunGC: boolean;
144
+ /** Tells whether the GC state in summary needs to be reset in the next summary. */
145
+ readonly summaryStateNeedsReset: boolean;
146
+ readonly trackGCState: boolean;
147
+ /** Initialize the state from the base snapshot after its creation. */
148
+ initializeBaseState(): Promise<void>;
149
+ /** Run garbage collection and update the reference / used state of the system. */
150
+ collectGarbage(options: {
151
+ logger?: ITelemetryLogger;
152
+ runSweep?: boolean;
153
+ fullGC?: boolean;
154
+ }): Promise<IGCStats | undefined>;
155
+ /** Summarizes the GC data and returns it as a summary tree. */
156
+ summarize(
157
+ fullTree: boolean,
158
+ trackState: boolean,
159
+ telemetryContext?: ITelemetryContext,
160
+ ): ISummarizeResult | undefined;
161
+ /** Returns the garbage collector specific metadata to be written into the summary. */
162
+ getMetadata(): IGCMetadata;
163
+ /** Returns the GC details generated from the base snapshot. */
164
+ getBaseGCDetails(): Promise<IGarbageCollectionDetailsBase>;
165
+ /** Called when the latest summary of the system has been refreshed. */
166
+ refreshLatestSummary(
167
+ result: RefreshSummaryResult,
168
+ proposalHandle: string | undefined,
169
+ summaryRefSeq: number,
170
+ readAndParseBlob: ReadAndParseBlob,
171
+ ): Promise<void>;
172
+ /** Called when a node is updated. Used to detect and log when an inactive node is changed or loaded. */
173
+ nodeUpdated(
174
+ nodePath: string,
175
+ reason: "Loaded" | "Changed",
176
+ timestampMs?: number,
177
+ packagePath?: readonly string[],
178
+ requestHeaders?: IRequestHeader,
179
+ ): void;
180
+ /** Called when a reference is added to a node. Used to identify nodes that were referenced between summaries. */
181
+ addedOutboundReference(fromNodePath: string, toNodePath: string): void;
182
+ /** Returns true if this node has been deleted by GC during sweep phase. */
183
+ isNodeDeleted(nodePath: string): boolean;
184
+ setConnectionState(connected: boolean, clientId?: string): void;
185
+ dispose(): void;
168
186
  }
169
187
 
170
188
  /** Parameters necessary for creating a GarbageCollector. */
171
189
  export interface IGarbageCollectorCreateParams {
172
- readonly runtime: IGarbageCollectionRuntime;
173
- readonly gcOptions: IGCRuntimeOptions;
174
- readonly baseLogger: ITelemetryLogger;
175
- readonly existing: boolean;
176
- readonly metadata: IContainerRuntimeMetadata | undefined;
177
- readonly createContainerMetadata: ICreateContainerMetadata;
178
- readonly baseSnapshot: ISnapshotTree | undefined;
179
- readonly isSummarizerClient: boolean;
180
- readonly getNodePackagePath: (nodePath: string) => Promise<readonly string[] | undefined>;
181
- readonly getLastSummaryTimestampMs: () => number | undefined;
182
- readonly readAndParseBlob: ReadAndParseBlob;
183
- readonly activeConnection: () => boolean;
184
- readonly getContainerDiagnosticId: () => string;
190
+ readonly runtime: IGarbageCollectionRuntime;
191
+ readonly gcOptions: IGCRuntimeOptions;
192
+ readonly baseLogger: ITelemetryLogger;
193
+ readonly existing: boolean;
194
+ readonly metadata: IContainerRuntimeMetadata | undefined;
195
+ readonly createContainerMetadata: ICreateContainerMetadata;
196
+ readonly baseSnapshot: ISnapshotTree | undefined;
197
+ readonly isSummarizerClient: boolean;
198
+ readonly getNodePackagePath: (nodePath: string) => Promise<readonly string[] | undefined>;
199
+ readonly getLastSummaryTimestampMs: () => number | undefined;
200
+ readonly readAndParseBlob: ReadAndParseBlob;
201
+ readonly activeConnection: () => boolean;
202
+ readonly getContainerDiagnosticId: () => string;
185
203
  }
186
204
 
187
205
  /** The state of node that is unreferenced. */
188
206
  export const UnreferencedState = {
189
- /** The node is active, i.e., it can become referenced again. */
190
- Active: "Active",
191
- /** The node is inactive, i.e., it should not become referenced. */
192
- Inactive: "Inactive",
193
- /** The node is ready to be deleted by the sweep phase. */
194
- SweepReady: "SweepReady",
207
+ /** The node is active, i.e., it can become referenced again. */
208
+ Active: "Active",
209
+ /** The node is inactive, i.e., it should not become referenced. */
210
+ Inactive: "Inactive",
211
+ /** The node is ready to be deleted by the sweep phase. */
212
+ SweepReady: "SweepReady",
195
213
  } as const;
196
214
  export type UnreferencedState = typeof UnreferencedState[keyof typeof UnreferencedState];
197
215
 
198
216
  /** The event that is logged when unreferenced node is used after a certain time. */
199
217
  interface IUnreferencedEventProps {
200
- usageType: "Changed" | "Loaded" | "Revived";
201
- state: UnreferencedState;
202
- id: string;
203
- type: GCNodeType;
204
- unrefTime: number;
205
- age: number;
206
- completedGCRuns: number;
207
- fromId?: string;
208
- timeout?: number;
209
- lastSummaryTime?: number;
210
- externalRequest?: boolean;
211
- viaHandle?: boolean;
218
+ usageType: "Changed" | "Loaded" | "Revived";
219
+ state: UnreferencedState;
220
+ id: string;
221
+ type: GCNodeType;
222
+ unrefTime: number;
223
+ age: number;
224
+ completedGCRuns: number;
225
+ fromId?: string;
226
+ timeout?: number;
227
+ lastSummaryTime?: number;
228
+ externalRequest?: boolean;
229
+ viaHandle?: boolean;
212
230
  }
213
231
 
214
232
  /**
215
233
  * The GC data that is tracked for a summary that is submitted.
216
234
  */
217
235
  interface IGCSummaryTrackingData {
218
- serializedGCState: string | undefined;
219
- serializedTombstones: string | undefined;
220
- }
221
-
222
- /**
223
- * The GC data that is read from a snapshot. It contains the GC state and tombstone state.
224
- */
225
- interface IGCSnapshotData {
226
- gcState: IGarbageCollectionState;
227
- tombstones: string[] | undefined;
236
+ serializedGCState: string | undefined;
237
+ serializedTombstones: string | undefined;
238
+ serializedDeletedNodes: string | undefined;
228
239
  }
229
240
 
230
241
  /**
@@ -232,85 +243,88 @@ interface IGCSnapshotData {
232
243
  * be deleted by the sweep phase.
233
244
  */
234
245
  export class UnreferencedStateTracker {
235
- private _state: UnreferencedState = UnreferencedState.Active;
236
- public get state(): UnreferencedState {
237
- return this._state;
238
- }
239
-
240
- /** Timer to indicate when an unreferenced object is considered Inactive */
241
- private readonly inactiveTimer: TimerWithNoDefaultTimeout;
242
- /** Timer to indicate when an unreferenced object is Sweep-Ready */
243
- private readonly sweepTimer: TimerWithNoDefaultTimeout;
244
-
245
- constructor(
246
- public readonly unreferencedTimestampMs: number,
247
- /** The time after which node transitions to Inactive state. */
248
- private readonly inactiveTimeoutMs: number,
249
- /** The current reference timestamp used to track how long this node has been unreferenced for. */
250
- currentReferenceTimestampMs: number,
251
- /** The time after which node transitions to SweepReady state; undefined if session expiry is disabled. */
252
- private readonly sweepTimeoutMs: number | undefined,
253
- ) {
254
- if (this.sweepTimeoutMs !== undefined) {
255
- assert(this.inactiveTimeoutMs <= this.sweepTimeoutMs,
256
- 0x3b0 /* inactive timeout must not be greater than the sweep timeout */);
257
- }
258
-
259
- this.sweepTimer = new TimerWithNoDefaultTimeout(
260
- () => {
261
- this._state = UnreferencedState.SweepReady;
262
- assert(!this.inactiveTimer.hasTimer, 0x3b1 /* inactiveTimer still running after sweepTimer fired! */);
263
- },
264
- );
265
-
266
- this.inactiveTimer = new TimerWithNoDefaultTimeout(() => {
267
- this._state = UnreferencedState.Inactive;
268
-
269
- // After the node becomes inactive, start the sweep timer after which the node will be ready for sweep.
270
- if (this.sweepTimeoutMs !== undefined) {
271
- this.sweepTimer.restart(this.sweepTimeoutMs - this.inactiveTimeoutMs);
272
- }
273
- });
274
- this.updateTracking(currentReferenceTimestampMs);
275
- }
276
-
277
- /* Updates the unreferenced state based on the provided timestamp. */
278
- public updateTracking(currentReferenceTimestampMs: number) {
279
- const unreferencedDurationMs = currentReferenceTimestampMs - this.unreferencedTimestampMs;
280
-
281
- // If the node has been unreferenced for sweep timeout amount of time, update the state to SweepReady.
282
- if (this.sweepTimeoutMs !== undefined && unreferencedDurationMs >= this.sweepTimeoutMs) {
283
- this._state = UnreferencedState.SweepReady;
284
- this.clearTimers();
285
- return;
286
- }
287
-
288
- // If the node has been unreferenced for inactive timeoutMs amount of time, update the state to inactive.
289
- // Also, start a timer for the sweep timeout.
290
- if (unreferencedDurationMs >= this.inactiveTimeoutMs) {
291
- this._state = UnreferencedState.Inactive;
292
- this.inactiveTimer.clear();
293
-
294
- if (this.sweepTimeoutMs !== undefined) {
295
- this.sweepTimer.restart(this.sweepTimeoutMs - unreferencedDurationMs);
296
- }
297
- return;
298
- }
299
-
300
- // The node is still active. Ensure the inactive timer is running with the proper remaining duration.
301
- this.inactiveTimer.restart(this.inactiveTimeoutMs - unreferencedDurationMs);
302
- }
303
-
304
- private clearTimers() {
305
- this.inactiveTimer.clear();
306
- this.sweepTimer.clear();
307
- }
308
-
309
- /** Stop tracking this node. Reset the unreferenced timers and state, if any. */
310
- public stopTracking() {
311
- this.clearTimers();
312
- this._state = UnreferencedState.Active;
313
- }
246
+ private _state: UnreferencedState = UnreferencedState.Active;
247
+ public get state(): UnreferencedState {
248
+ return this._state;
249
+ }
250
+
251
+ /** Timer to indicate when an unreferenced object is considered Inactive */
252
+ private readonly inactiveTimer: TimerWithNoDefaultTimeout;
253
+ /** Timer to indicate when an unreferenced object is Sweep-Ready */
254
+ private readonly sweepTimer: TimerWithNoDefaultTimeout;
255
+
256
+ constructor(
257
+ public readonly unreferencedTimestampMs: number,
258
+ /** The time after which node transitions to Inactive state. */
259
+ private readonly inactiveTimeoutMs: number,
260
+ /** The current reference timestamp used to track how long this node has been unreferenced for. */
261
+ currentReferenceTimestampMs: number,
262
+ /** The time after which node transitions to SweepReady state; undefined if session expiry is disabled. */
263
+ private readonly sweepTimeoutMs: number | undefined,
264
+ ) {
265
+ if (this.sweepTimeoutMs !== undefined) {
266
+ assert(
267
+ this.inactiveTimeoutMs <= this.sweepTimeoutMs,
268
+ 0x3b0 /* inactive timeout must not be greater than the sweep timeout */,
269
+ );
270
+ }
271
+
272
+ this.sweepTimer = new TimerWithNoDefaultTimeout(() => {
273
+ this._state = UnreferencedState.SweepReady;
274
+ assert(
275
+ !this.inactiveTimer.hasTimer,
276
+ 0x3b1 /* inactiveTimer still running after sweepTimer fired! */,
277
+ );
278
+ });
279
+
280
+ this.inactiveTimer = new TimerWithNoDefaultTimeout(() => {
281
+ this._state = UnreferencedState.Inactive;
282
+
283
+ // After the node becomes inactive, start the sweep timer after which the node will be ready for sweep.
284
+ if (this.sweepTimeoutMs !== undefined) {
285
+ this.sweepTimer.restart(this.sweepTimeoutMs - this.inactiveTimeoutMs);
286
+ }
287
+ });
288
+ this.updateTracking(currentReferenceTimestampMs);
289
+ }
290
+
291
+ /* Updates the unreferenced state based on the provided timestamp. */
292
+ public updateTracking(currentReferenceTimestampMs: number) {
293
+ const unreferencedDurationMs = currentReferenceTimestampMs - this.unreferencedTimestampMs;
294
+
295
+ // If the node has been unreferenced for sweep timeout amount of time, update the state to SweepReady.
296
+ if (this.sweepTimeoutMs !== undefined && unreferencedDurationMs >= this.sweepTimeoutMs) {
297
+ this._state = UnreferencedState.SweepReady;
298
+ this.clearTimers();
299
+ return;
300
+ }
301
+
302
+ // If the node has been unreferenced for inactive timeoutMs amount of time, update the state to inactive.
303
+ // Also, start a timer for the sweep timeout.
304
+ if (unreferencedDurationMs >= this.inactiveTimeoutMs) {
305
+ this._state = UnreferencedState.Inactive;
306
+ this.inactiveTimer.clear();
307
+
308
+ if (this.sweepTimeoutMs !== undefined) {
309
+ this.sweepTimer.restart(this.sweepTimeoutMs - unreferencedDurationMs);
310
+ }
311
+ return;
312
+ }
313
+
314
+ // The node is still active. Ensure the inactive timer is running with the proper remaining duration.
315
+ this.inactiveTimer.restart(this.inactiveTimeoutMs - unreferencedDurationMs);
316
+ }
317
+
318
+ private clearTimers() {
319
+ this.inactiveTimer.clear();
320
+ this.sweepTimer.clear();
321
+ }
322
+
323
+ /** Stop tracking this node. Reset the unreferenced timers and state, if any. */
324
+ public stopTracking() {
325
+ this.clearTimers();
326
+ this._state = UnreferencedState.Active;
327
+ }
314
328
  }
315
329
 
316
330
  /**
@@ -336,1311 +350,1546 @@ export class UnreferencedStateTracker {
336
350
  * ```
337
351
  */
338
352
  export class GarbageCollector implements IGarbageCollector {
339
- public static create(createParams: IGarbageCollectorCreateParams): IGarbageCollector {
340
- return new GarbageCollector(createParams);
341
- }
342
-
343
- /**
344
- * Tells whether the GC state needs to be reset in the next summary. We need to do this if:
345
- *
346
- * 1. GC was enabled and is now disabled. The GC state needs to be removed and everything becomes referenced.
347
- *
348
- * 2. GC was disabled and is now enabled. The GC state needs to be regenerated and added to summary.
349
- *
350
- * 3. The GC version in the latest summary is different from the current GC version. This can happen if:
351
- *
352
- * 3.1. The summary this client loaded with has data from a different GC version.
353
- *
354
- * 3.2. This client's latest summary was updated from a snapshot that has a different GC version.
355
- */
356
- public get summaryStateNeedsReset(): boolean {
357
- return this.initialStateNeedsReset ||
358
- (this.shouldRunGC && this.latestSummaryGCVersion !== this.currentGCVersion);
359
- }
360
-
361
- /**
362
- * Tracks if GC is enabled for this document. This is specified during document creation and doesn't change
363
- * throughout its lifetime.
364
- */
365
- private readonly gcEnabled: boolean;
366
- /**
367
- * Tracks if sweep phase is enabled for this document. This is specified during document creation and doesn't change
368
- * throughout its lifetime.
369
- */
370
- private readonly sweepEnabled: boolean;
371
-
372
- /**
373
- * Tracks if GC should run or not. Even if GC is enabled for a document (see gcEnabled), it can be explicitly
374
- * disabled via runtime options or feature flags.
375
- */
376
- public readonly shouldRunGC: boolean;
377
- /**
378
- * Tracks if sweep phase should run or not. Even if the sweep phase is enabled for a document (see sweepEnabled), it
379
- * can be explicitly disabled via feature flags. It also won't run if session expiry is not enabled.
380
- */
381
- private readonly shouldRunSweep: boolean;
382
-
383
- public readonly trackGCState: boolean;
384
-
385
- private readonly testMode: boolean;
386
- private readonly tombstoneMode: boolean;
387
- private readonly mc: MonitoringContext;
388
-
389
- /**
390
- * Tells whether the initial GC state needs to be reset. This can happen under 2 conditions:
391
- *
392
- * 1. The base snapshot contains GC state but GC is disabled. This will happen the first time GC is disabled after
393
- * it was enabled before. GC state needs to be removed from summary and all nodes should be marked referenced.
394
- *
395
- * 2. The base snapshot does not have GC state but GC is enabled. This will happen the very first time GC runs on
396
- * a document and the first time GC is enabled after is was disabled before.
397
- *
398
- * Note that the state needs reset only for the very first time summary is generated by this client. After that, the
399
- * state will be up-to-date and this flag will be reset.
400
- */
401
- private initialStateNeedsReset: boolean = false;
402
-
403
- // The current GC version that this container is running.
404
- private readonly currentGCVersion = GCVersion;
405
- // This is the version of GC data in the latest summary being tracked.
406
- private latestSummaryGCVersion: GCVersion;
407
-
408
- // Keeps track of the GC state from the last run.
409
- private previousGCDataFromLastRun: IGarbageCollectionData | undefined;
410
- // Keeps a list of references (edges in the GC graph) between GC runs. Each entry has a node id and a list of
411
- // outbound routes from that node.
412
- private readonly newReferencesSinceLastRun: Map<string, string[]> = new Map();
413
- private tombstones: string[] = [];
414
-
415
- /**
416
- * Keeps track of the GC data from the latest summary successfully submitted to and acked from the server.
417
- */
418
- private latestSummaryData: IGCSummaryTrackingData | undefined;
419
- /**
420
- * Keeps track of the GC data from the last summary submitted to the server but not yet acked.
421
- */
422
- private pendingSummaryData: IGCSummaryTrackingData | undefined;
423
-
424
- // Promise when resolved returns the GC data data in the base snapshot.
425
- private readonly baseSnapshotDataP: Promise<IGCSnapshotData | undefined>;
426
- // Promise when resolved initializes the GC state from the data in the base snapshot.
427
- private readonly initializeGCStateFromBaseSnapshotP: Promise<void>;
428
- // The map of data store ids to their GC details in the base summary returned in getDataStoreGCDetails().
429
- private readonly baseGCDetailsP: Promise<Map<string, IGarbageCollectionDetailsBase>>;
430
- // Map of node ids to their unreferenced state tracker.
431
- private readonly unreferencedNodesState: Map<string, UnreferencedStateTracker> = new Map();
432
- // The Timer responsible for closing the container when the session has expired
433
- private sessionExpiryTimer: Timer | undefined;
434
-
435
- // Keeps track of unreferenced events that are logged for a node. This is used to limit the log generation to one
436
- // per event per node.
437
- private readonly loggedUnreferencedEvents: Set<string> = new Set();
438
- // Queue for unreferenced events that should be logged the next time GC runs.
439
- private pendingEventsQueue: IUnreferencedEventProps[] = [];
440
-
441
- // The number of times GC has successfully completed on this instance of GarbageCollector.
442
- private completedRuns = 0;
443
-
444
- private readonly runtime: IGarbageCollectionRuntime;
445
- private readonly createContainerMetadata: ICreateContainerMetadata;
446
- private readonly gcOptions: IGCRuntimeOptions;
447
- private readonly isSummarizerClient: boolean;
448
-
449
- /** The time in ms to expire a session for a client for gc. */
450
- private readonly sessionExpiryTimeoutMs: number | undefined;
451
- /** The time after which an unreferenced node is inactive. */
452
- private readonly inactiveTimeoutMs: number;
453
- /** The time after which an unreferenced node is ready to be swept. */
454
- private readonly sweepTimeoutMs: number | undefined;
455
-
456
- /** For a given node path, returns the node's package path. */
457
- private readonly getNodePackagePath: (nodePath: string) => Promise<readonly string[] | undefined>;
458
- /** Returns the timestamp of the last summary generated for this container. */
459
- private readonly getLastSummaryTimestampMs: () => number | undefined;
460
- /** Returns true if connection is active, i.e. it's "write" connection and the runtime is connected. */
461
- private readonly activeConnection: () => boolean;
462
-
463
- /** Returns a list of all the configurations for garbage collection. */
464
- private get configs() {
465
- return {
466
- gcEnabled: this.gcEnabled,
467
- sweepEnabled: this.sweepEnabled,
468
- runGC: this.shouldRunGC,
469
- runSweep: this.shouldRunSweep,
470
- testMode: this.testMode,
471
- tombstoneMode: this.tombstoneMode,
472
- sessionExpiry: this.sessionExpiryTimeoutMs,
473
- sweepTimeout: this.sweepTimeoutMs,
474
- inactiveTimeout: this.inactiveTimeoutMs,
475
- trackGCState: this.trackGCState,
476
- ...this.gcOptions,
477
- };
478
- }
479
-
480
- /** Handler to respond to when a SweepReady object is used */
481
- private readonly sweepReadyUsageHandler: SweepReadyUsageDetectionHandler;
482
-
483
- protected constructor(createParams: IGarbageCollectorCreateParams) {
484
- this.runtime = createParams.runtime;
485
- this.isSummarizerClient = createParams.isSummarizerClient;
486
- this.gcOptions = createParams.gcOptions;
487
- this.createContainerMetadata = createParams.createContainerMetadata;
488
- this.getNodePackagePath = createParams.getNodePackagePath;
489
- this.getLastSummaryTimestampMs = createParams.getLastSummaryTimestampMs;
490
- this.activeConnection = createParams.activeConnection;
491
-
492
- const baseSnapshot = createParams.baseSnapshot;
493
- const metadata = createParams.metadata;
494
- const readAndParseBlob = createParams.readAndParseBlob;
495
-
496
- this.mc = loggerToMonitoringContext(ChildLogger.create(
497
- createParams.baseLogger, "GarbageCollector", { all: { completedGCRuns: () => this.completedRuns } },
498
- ));
499
-
500
- this.sweepReadyUsageHandler = new SweepReadyUsageDetectionHandler(
501
- createParams.getContainerDiagnosticId(),
502
- this.mc,
503
- this.runtime.closeFn,
504
- );
505
-
506
- let prevSummaryGCVersion: number | undefined;
507
-
508
- /**
509
- * Sweep timeout is the time after which unreferenced content can be swept.
510
- * Sweep timeout = session expiry timeout + snapshot cache expiry timeout + one day buffer.
511
- *
512
- * The snapshot cache expiry timeout cannot be known precisely but the upper bound is 5 days.
513
- * The buffer is added to account for any clock skew or other edge cases.
514
- * We use server timestamps throughout so the skew should be minimal but make it 1 day to be safe.
515
- */
516
- function computeSweepTimeout(sessionExpiryTimeoutMs: number | undefined) {
517
- const maxSnapshotCacheExpiryMs = 5 * oneDayMs;
518
- const bufferMs = oneDayMs;
519
- return sessionExpiryTimeoutMs &&
520
- (sessionExpiryTimeoutMs + maxSnapshotCacheExpiryMs + bufferMs);
521
- }
522
-
523
- /**
524
- * The following GC state is enabled during container creation and cannot be changed throughout its lifetime:
525
- * 1. Whether running GC mark phase is allowed or not.
526
- * 2. Whether running GC sweep phase is allowed or not.
527
- * 3. Whether GC session expiry is enabled or not.
528
- * For existing containers, we get this information from the metadata blob of its summary.
529
- */
530
- if (createParams.existing) {
531
- prevSummaryGCVersion = getGCVersion(metadata);
532
- // Existing documents which did not have metadata blob or had GC disabled have version as 0. For all
533
- // other existing documents, GC is enabled.
534
- this.gcEnabled = prevSummaryGCVersion > 0;
535
- this.sweepEnabled = metadata?.sweepEnabled ?? false;
536
- this.sessionExpiryTimeoutMs = metadata?.sessionExpiryTimeoutMs;
537
- this.sweepTimeoutMs =
538
- metadata?.sweepTimeoutMs
539
- ?? computeSweepTimeout(this.sessionExpiryTimeoutMs); // Backfill old documents that didn't persist this
540
- } else {
541
- // Sweep should not be enabled without enabling GC mark phase. We could silently disable sweep in this
542
- // scenario but explicitly failing makes it clearer and promotes correct usage.
543
- if (this.gcOptions.sweepAllowed && this.gcOptions.gcAllowed === false) {
544
- throw new UsageError("GC sweep phase cannot be enabled without enabling GC mark phase");
545
- }
546
-
547
- // This Test Override only applies for new containers
548
- const testOverrideSweepTimeoutMs =
549
- this.mc.config.getNumber("Fluid.GarbageCollection.TestOverride.SweepTimeoutMs");
550
-
551
- // For new documents, GC is enabled by default. It can be explicitly disabled by setting the gcAllowed
552
- // flag in GC options to false.
553
- this.gcEnabled = this.gcOptions.gcAllowed !== false;
554
- // The sweep phase has to be explicitly enabled by setting the sweepAllowed flag in GC options to true.
555
- // ...unless we're using the TestOverride
556
- this.sweepEnabled = this.gcOptions.sweepAllowed === true || testOverrideSweepTimeoutMs !== undefined;
557
-
558
- // Set the Session Expiry only if the flag is enabled and GC is enabled.
559
- if (this.mc.config.getBoolean(runSessionExpiryKey) && this.gcEnabled) {
560
- this.sessionExpiryTimeoutMs = this.gcOptions.sessionExpiryTimeoutMs ?? defaultSessionExpiryDurationMs;
561
- }
562
- this.sweepTimeoutMs =
563
- testOverrideSweepTimeoutMs
564
- ?? computeSweepTimeout(this.sessionExpiryTimeoutMs);
565
- }
566
-
567
- // If session expiry is enabled, we need to close the container when the session expiry timeout expires.
568
- if (this.sessionExpiryTimeoutMs !== undefined) {
569
- // If Test Override config is set, override Session Expiry timeout.
570
- const overrideSessionExpiryTimeoutMs =
571
- this.mc.config.getNumber("Fluid.GarbageCollection.TestOverride.SessionExpiryMs");
572
- const timeoutMs = overrideSessionExpiryTimeoutMs ?? this.sessionExpiryTimeoutMs;
573
-
574
- this.sessionExpiryTimer = new Timer(
575
- timeoutMs,
576
- () => { this.runtime.closeFn(new ClientSessionExpiredError(`Client session expired.`, timeoutMs)); },
577
- );
578
- this.sessionExpiryTimer.start();
579
- }
580
-
581
- // For existing document, the latest summary is the one that we loaded from. So, use its GC version as the
582
- // latest tracked GC version. For new documents, we will be writing the first summary with the current version.
583
- this.latestSummaryGCVersion = prevSummaryGCVersion ?? this.currentGCVersion;
584
-
585
- /**
586
- * Whether GC should run or not. The following conditions have to be met to run sweep:
587
- *
588
- * 1. GC should be enabled for this container.
589
- *
590
- * 2. GC should not be disabled via disableGC GC option.
591
- *
592
- * These conditions can be overridden via runGCKey feature flag.
593
- */
594
- this.shouldRunGC = this.mc.config.getBoolean(runGCKey) ?? (
595
- // GC must be enabled for the document.
596
- this.gcEnabled
597
- // GC must not be disabled via GC options.
598
- && !this.gcOptions.disableGC
599
- );
600
-
601
- /**
602
- * Whether sweep should run or not. The following conditions have to be met to run sweep:
603
- *
604
- * 1. Overall GC or mark phase must be enabled (this.shouldRunGC).
605
- * 2. Sweep timeout should be available. Without this, we wouldn't know when an object should be deleted.
606
- * 3. The driver must implement the policy limiting the age of snapshots used for loading. Otherwise
607
- * the Sweep Timeout calculation is not valid. We use the persisted value to ensure consistency over time.
608
- * 4. Sweep should be enabled for this container (this.sweepEnabled). This can be overridden via runSweep
609
- * feature flag.
610
- */
611
- this.shouldRunSweep =
612
- this.shouldRunGC
613
- && this.sweepTimeoutMs !== undefined
614
- && (this.mc.config.getBoolean(runSweepKey) ?? this.sweepEnabled);
615
-
616
- this.trackGCState = this.mc.config.getBoolean(trackGCStateKey) === true;
617
-
618
- // Override inactive timeout if test config or gc options to override it is set.
619
- this.inactiveTimeoutMs = this.mc.config.getNumber("Fluid.GarbageCollection.TestOverride.InactiveTimeoutMs") ??
620
- this.gcOptions.inactiveTimeoutMs ??
621
- defaultInactiveTimeoutMs;
622
-
623
- // Inactive timeout must be greater than sweep timeout since a node goes from active -> inactive -> sweep ready.
624
- if (this.sweepTimeoutMs !== undefined && this.inactiveTimeoutMs > this.sweepTimeoutMs) {
625
- throw new UsageError("inactive timeout should not be greater than the sweep timeout");
626
- }
627
-
628
- // Whether we are running in test mode. In this mode, unreferenced nodes are immediately deleted.
629
- this.testMode = this.mc.config.getBoolean(gcTestModeKey) ?? this.gcOptions.runGCInTestMode === true;
630
- // Whether we are running in tombstone mode. This is true by default unless disabled via feature flags.
631
- this.tombstoneMode = this.mc.config.getBoolean(disableTombstoneKey) !== true;
632
-
633
- // The GC state needs to be reset if the base snapshot contains GC tree and GC is disabled or it doesn't
634
- // contain GC tree and GC is enabled.
635
- const gcTreePresent = baseSnapshot?.trees[gcTreeKey] !== undefined;
636
- this.initialStateNeedsReset = gcTreePresent !== this.shouldRunGC;
637
-
638
- // Get the GC data from the base snapshot. Use LazyPromise because we only want to do this once since it
639
- // it involves fetching blobs from storage which is expensive.
640
- this.baseSnapshotDataP = new LazyPromise<IGCSnapshotData | undefined>(async () => {
641
- if (baseSnapshot === undefined) {
642
- return undefined;
643
- }
644
-
645
- try {
646
- // For newer documents, GC data should be present in the GC tree in the root of the snapshot.
647
- const gcSnapshotTree = baseSnapshot.trees[gcTreeKey];
648
- if (gcSnapshotTree !== undefined) {
649
- return getGCDataFromSnapshot(
650
- gcSnapshotTree,
651
- readAndParseBlob,
652
- );
653
- }
654
-
655
- // back-compat - Older documents will have the GC blobs in each data store's summary tree. Get them and
656
- // consolidate into IGarbageCollectionState format.
657
- // Add a node for the root node that is not present in older snapshot format.
658
- const gcState: IGarbageCollectionState = { gcNodes: { "/": { outboundRoutes: [] } } };
659
- const dataStoreSnapshotTree = getSummaryForDatastores(baseSnapshot, metadata);
660
- assert(dataStoreSnapshotTree !== undefined,
661
- 0x2a8 /* "Expected data store snapshot tree in base snapshot" */);
662
- for (const [dsId, dsSnapshotTree] of Object.entries(dataStoreSnapshotTree.trees)) {
663
- const blobId = dsSnapshotTree.blobs[gcBlobKey];
664
- if (blobId === undefined) {
665
- continue;
666
- }
667
-
668
- const gcSummaryDetails = await readAndParseBlob<IGarbageCollectionDetailsBase>(blobId);
669
- // If there are no nodes for this data store, skip it.
670
- if (gcSummaryDetails.gcData?.gcNodes === undefined) {
671
- continue;
672
- }
673
-
674
- const dsRootId = `/${dsId}`;
675
- // Since we used to write GC data at data store level, we won't have an entry for the root ("/").
676
- // Construct that entry by adding root data store ids to its outbound routes.
677
- const initialSnapshotDetails = await readAndParseBlob<ReadFluidDataStoreAttributes>(
678
- dsSnapshotTree.blobs[dataStoreAttributesBlobName],
679
- );
680
- if (initialSnapshotDetails.isRootDataStore) {
681
- gcState.gcNodes["/"].outboundRoutes.push(dsRootId);
682
- }
683
-
684
- for (const [id, outboundRoutes] of Object.entries(gcSummaryDetails.gcData.gcNodes)) {
685
- // Prefix the data store id to the GC node ids to make them relative to the root from being
686
- // relative to the data store. Similar to how its done in DataStore::getGCData.
687
- const rootId = id === "/" ? dsRootId : `${dsRootId}${id}`;
688
- gcState.gcNodes[rootId] = { outboundRoutes: Array.from(outboundRoutes) };
689
- }
690
- assert(gcState.gcNodes[dsRootId] !== undefined,
691
- 0x2a9 /* GC nodes for data store not in GC blob */);
692
- gcState.gcNodes[dsRootId].unreferencedTimestampMs = gcSummaryDetails.unrefTimestamp;
693
- }
694
- // If there is only one node (root node just added above), either GC is disabled or we are loading from
695
- // the first summary generated by detached container. In both cases, GC was not run - return undefined.
696
- return Object.keys(gcState.gcNodes).length === 1 ? undefined : { gcState, tombstones: undefined };
697
- } catch (error) {
698
- const dpe = DataProcessingError.wrapIfUnrecognized(
699
- error,
700
- "FailedToInitializeGC",
701
- );
702
- dpe.addTelemetryProperties({ gcConfigs: JSON.stringify(this.configs) });
703
- throw dpe;
704
- }
705
- });
706
-
707
- /**
708
- * Set up the initializer which initializes the GC state from the data in base snapshot. This is done when
709
- * connected in write mode or when GC runs the first time. It sets up all unreferenced nodes from the base
710
- * GC state and updates their inactive or sweep ready state.
711
- */
712
- this.initializeGCStateFromBaseSnapshotP = new LazyPromise<void>(async () => {
713
- const currentReferenceTimestampMs = this.runtime.getCurrentReferenceTimestampMs();
714
- /**
715
- * If there is no current reference timestamp, skip initialization. We need the current timestamp to track
716
- * how long objects have been unreferenced and if they can be deleted.
717
- *
718
- * Note that the only scenario where there is no reference timestamp is when no ops have ever been processed
719
- * for this container and it is in read mode. In this scenario, there is no point in running GC anyway
720
- * because references in the container do not change without any ops, i.e., there is nothing to collect.
721
- */
722
- if (currentReferenceTimestampMs === undefined) {
723
- // Log an event so we can evaluate how often we run into this scenario.
724
- this.mc.logger.sendErrorEvent({
725
- eventName: "GarbageCollectorInitializedWithoutTimestamp",
726
- gcConfigs: JSON.stringify(this.configs),
727
- });
728
- return;
729
- }
730
-
731
- const baseSnapshotData = await this.baseSnapshotDataP;
732
- /**
733
- * The base snapshot data will not be present if the container is loaded from:
734
- * 1. The first summary created by the detached container.
735
- * 2. A summary that was generated with GC disabled.
736
- * 3. A summary that was generated before GC even existed.
737
- */
738
- if (baseSnapshotData === undefined) {
739
- return;
740
- }
741
-
742
- const gcNodes: { [id: string]: string[]; } = {};
743
- for (const [nodeId, nodeData] of Object.entries(baseSnapshotData.gcState.gcNodes)) {
744
- if (nodeData.unreferencedTimestampMs !== undefined) {
745
- this.unreferencedNodesState.set(
746
- nodeId,
747
- new UnreferencedStateTracker(
748
- nodeData.unreferencedTimestampMs,
749
- this.inactiveTimeoutMs,
750
- currentReferenceTimestampMs,
751
- this.sweepTimeoutMs,
752
- ),
753
- );
754
- }
755
- gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);
756
- }
757
- this.previousGCDataFromLastRun = { gcNodes };
758
-
759
- // If tracking state across summaries, update latest summary data from the base snapshot's GC data.
760
- if (this.trackGCState) {
761
- this.latestSummaryData = {
762
- serializedGCState: JSON.stringify(generateSortedGCState(baseSnapshotData.gcState)),
763
- serializedTombstones: JSON.stringify(baseSnapshotData.tombstones),
764
- };
765
- }
766
- });
767
-
768
- // Get the GC details for each node from the GC state in the base summary. This is returned in getBaseGCDetails
769
- // which the caller uses to initialize each node's GC state.
770
- this.baseGCDetailsP = new LazyPromise<Map<string, IGarbageCollectionDetailsBase>>(async () => {
771
- const baseSnapshotData = await this.baseSnapshotDataP;
772
- if (baseSnapshotData === undefined) {
773
- return new Map();
774
- }
775
-
776
- const gcNodes: { [id: string]: string[]; } = {};
777
- for (const [nodeId, nodeData] of Object.entries(baseSnapshotData.gcState.gcNodes)) {
778
- gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);
779
- }
780
- // Run GC on the nodes in the base summary to get the routes used in each node in the container.
781
- // This is an optimization for space (vs performance) wherein we don't need to store the used routes of
782
- // each node in the summary.
783
- const usedRoutes = runGarbageCollection(gcNodes, ["/"]).referencedNodeIds;
784
-
785
- const baseGCDetailsMap = unpackChildNodesGCDetails({ gcData: { gcNodes }, usedRoutes });
786
- // Currently, the nodes may write the GC data. So, we need to update its base GC details with the
787
- // unreferenced timestamp. Once we start writing the GC data here, we won't need to do this anymore.
788
- for (const [nodeId, nodeData] of Object.entries(baseSnapshotData.gcState.gcNodes)) {
789
- if (nodeData.unreferencedTimestampMs !== undefined) {
790
- const dataStoreGCDetails = baseGCDetailsMap.get(nodeId.slice(1));
791
- if (dataStoreGCDetails !== undefined) {
792
- dataStoreGCDetails.unrefTimestamp = nodeData.unreferencedTimestampMs;
793
- }
794
- }
795
- }
796
- return baseGCDetailsMap;
797
- });
798
-
799
- // Log all the GC options and the state determined by the garbage collector. This is interesting only for the
800
- // summarizer client since it is the only one that runs GC. It also helps keep the telemetry less noisy.
801
- if (this.isSummarizerClient) {
802
- this.mc.logger.sendTelemetryEvent({
803
- eventName: "GarbageCollectorLoaded",
804
- gcConfigs: JSON.stringify(this.configs),
805
- });
806
- }
807
- }
808
-
809
- /**
810
- * Called during container initialization. Initialize the tombstone state so that object are marked as tombstones
811
- * before they are loaded or used. This is important to get accurate information of whether tombstoned object are
812
- * in use or not.
813
- */
814
- public async initializeBaseState(): Promise<void> {
815
- const baseSnapshotData = await this.baseSnapshotDataP;
816
- /**
817
- * The base snapshot data or tombstone state will not be present if the container is loaded from:
818
- * 1. The first summary created by the detached container.
819
- * 2. A summary that was generated with GC disabled.
820
- * 3. A summary that was generated before GC even existed.
821
- * 4. A summary that was generated with tombstone feature disabled.
822
- */
823
- if (!this.tombstoneMode || baseSnapshotData?.tombstones === undefined) {
824
- return;
825
- }
826
- this.tombstones = baseSnapshotData.tombstones;
827
- this.runtime.updateUnusedRoutes(this.tombstones, true /* tombstone */);
828
- }
829
-
830
- /**
831
- * Called when the connection state of the runtime changes, i.e., it connects or disconnects. GC subscribes to this
832
- * to initialize the base state for non-summarizer clients so that they can track inactive / sweep ready nodes.
833
- * @param connected - Whether the runtime connected / disconnected.
834
- * @param clientId - The clientId of this runtime.
835
- */
836
- public setConnectionState(connected: boolean, clientId?: string | undefined): void {
837
- /**
838
- * For all clients, initialize the base state when the container becomes active, i.e., it transitions
839
- * to "write" mode. This will ensure that the container's own join op is processed and there is a recent
840
- * reference timestamp that will be used to update the state of unreferenced nodes. Also, all trailing ops which
841
- * could affect the GC state will have been processed.
842
- *
843
- * If GC is up-to-date for the client and the summarizing client, there will be an doubling of both
844
- * InactiveObject_Loaded and SweepReady_Loaded errors, as there will be one from the sending client and one from
845
- * the receiving summarizer client.
846
- *
847
- * Ideally, this initialization should only be done for summarizer client. However, we are currently rolling out
848
- * sweep in phases and we want to track when inactive and sweep ready objects are used in any client.
849
- */
850
- if (this.activeConnection() && this.shouldRunGC) {
851
- this.initializeGCStateFromBaseSnapshotP.catch((error) => {});
852
- }
853
- }
854
-
855
- /**
856
- * Runs garbage collection and updates the reference / used state of the nodes in the container.
857
- * @returns stats of the GC run or undefined if GC did not run.
858
- */
859
- public async collectGarbage(
860
- options: {
861
- /** Logger to use for logging GC events */
862
- logger?: ITelemetryLogger;
863
- /** True to run GC sweep phase after the mark phase */
864
- runSweep?: boolean;
865
- /** True to generate full GC data */
866
- fullGC?: boolean;
867
- },
868
- ): Promise<IGCStats | undefined> {
869
- const fullGC = options.fullGC ?? (this.gcOptions.runFullGC === true || this.summaryStateNeedsReset);
870
- const logger = options.logger
871
- ? ChildLogger.create(options.logger, undefined, { all: { completedGCRuns: () => this.completedRuns } })
872
- : this.mc.logger;
873
-
874
- /**
875
- * If there is no current reference timestamp, skip running GC. We need the current timestamp to track
876
- * how long objects have been unreferenced and if they should be deleted.
877
- *
878
- * Note that the only scenario where GC is called and there is no reference timestamp is when no ops have ever
879
- * been processed for this container and it is in read mode. In this scenario, there is no point in running GC
880
- * anyway because references in the container do not change without any ops, i.e., there is nothing to collect.
881
- */
882
- const currentReferenceTimestampMs = this.runtime.getCurrentReferenceTimestampMs();
883
- if (currentReferenceTimestampMs === undefined) {
884
- // Log an event so we can evaluate how often we run into this scenario.
885
- logger.sendErrorEvent({
886
- eventName: "CollectGarbageCalledWithoutTimestamp",
887
- gcConfigs: JSON.stringify(this.configs),
888
- });
889
- return undefined;
890
- }
891
-
892
- return PerformanceEvent.timedExecAsync(logger, { eventName: "GarbageCollection" }, async (event) => {
893
- await this.runPreGCSteps();
894
-
895
- // Get the runtime's GC data and run GC on the reference graph in it.
896
- const gcData = await this.runtime.getGCData(fullGC);
897
- const gcResult = runGarbageCollection(gcData.gcNodes, ["/"]);
898
-
899
- const gcStats = await this.runPostGCSteps(gcData, gcResult, logger, currentReferenceTimestampMs);
900
- event.end({ ...gcStats, timestamp: currentReferenceTimestampMs });
901
- this.completedRuns++;
902
- return gcStats;
903
- }, { end: true, cancel: "error" });
904
- }
905
-
906
- private async runPreGCSteps() {
907
- // Ensure that state has been initialized from the base snapshot data.
908
- await this.initializeGCStateFromBaseSnapshotP;
909
- // Let the runtime update its pending state before GC runs.
910
- await this.runtime.updateStateBeforeGC();
911
- }
912
-
913
- private async runPostGCSteps(
914
- gcData: IGarbageCollectionData,
915
- gcResult: IGCResult,
916
- logger: ITelemetryLogger,
917
- currentReferenceTimestampMs: number,
918
- ): Promise<IGCStats> {
919
- // Generate statistics from the current run. This is done before updating the current state because it
920
- // generates some of its data based on previous state of the system.
921
- const gcStats = this.generateStats(gcResult);
922
-
923
- // Update the state since the last GC run. There can be nodes that were referenced between the last and
924
- // the current run. We need to identify than and update their unreferenced state if needed.
925
- this.updateStateSinceLastRun(gcData, logger);
926
-
927
- // Update the current state and update the runtime of all routes or ids that used as per the GC run.
928
- this.updateCurrentState(gcData, gcResult, currentReferenceTimestampMs);
929
- this.runtime.updateUsedRoutes(gcResult.referencedNodeIds);
930
-
931
- // Log events for objects that are ready to be deleted by sweep. When we have sweep enabled, we will
932
- // delete these objects here instead.
933
- this.logSweepEvents(logger, currentReferenceTimestampMs);
934
-
935
- // If we are running in GC test mode, delete objects for unused routes. This enables testing scenarios
936
- // involving access to deleted data.
937
- if (this.testMode) {
938
- this.runtime.updateUnusedRoutes(gcResult.deletedNodeIds, false /* tombstone */);
939
- } else if (this.tombstoneMode) {
940
- // If we are running in GC tombstone mode, tombstone objects for unused routes. This enables testing
941
- // scenarios involving access to "deleted" data without actually deleting the data from summaries.
942
- // Note: we will not tombstone in test mode
943
- this.runtime.updateUnusedRoutes(this.tombstones, true /* tombstone */);
944
- }
945
-
946
- // Log pending unreferenced events such as a node being used after inactive. This is done after GC runs and
947
- // updates its state so that we don't send false positives based on intermediate state. For example, we may get
948
- // reference to an unreferenced node from another unreferenced node which means the node wasn't revived.
949
- await this.logUnreferencedEvents(logger);
950
-
951
- return gcStats;
952
- }
953
-
954
- /**
955
- * Summarizes the GC data and returns it as a summary tree.
956
- * We current write the entire GC state in a single blob. This can be modified later to write multiple
957
- * blobs. All the blob keys should start with `gcBlobPrefix`.
958
- */
959
- public summarize(
960
- fullTree: boolean,
961
- trackState: boolean,
962
- telemetryContext?: ITelemetryContext,
963
- ): ISummarizeResult | undefined {
964
- if (!this.shouldRunGC || this.previousGCDataFromLastRun === undefined) {
965
- return;
966
- }
967
-
968
- const gcState: IGarbageCollectionState = { gcNodes: {} };
969
- for (const [nodeId, outboundRoutes] of Object.entries(this.previousGCDataFromLastRun.gcNodes)) {
970
- gcState.gcNodes[nodeId] = {
971
- outboundRoutes,
972
- unreferencedTimestampMs: this.unreferencedNodesState.get(nodeId)?.unreferencedTimestampMs,
973
- };
974
- }
975
-
976
- const serializedGCState = JSON.stringify(generateSortedGCState(gcState));
977
- const serializedTombstones = this.tombstoneMode
978
- ? (this.tombstones.length > 0 ? JSON.stringify(this.tombstones.sort()) : undefined)
979
- : undefined;
980
-
981
- /**
982
- * Incremental summary of GC data - If any of the GC state or tombstone state hasn't changed since the last
983
- * summary, send summary handles for them. Otherwise, send the data in summary blobs.
984
- */
985
- if (this.trackGCState) {
986
- this.pendingSummaryData = { serializedGCState, serializedTombstones };
987
- if (trackState && !fullTree && this.latestSummaryData !== undefined) {
988
- // If neither GC state or tombstone state changed, send a summary handle for the entire GC data.
989
- if (this.latestSummaryData.serializedGCState === serializedGCState
990
- && this.latestSummaryData.serializedTombstones === serializedTombstones) {
991
- const stats = mergeStats();
992
- stats.handleNodeCount++;
993
- return {
994
- summary: {
995
- type: SummaryType.Handle,
996
- handle: `/${gcTreeKey}`,
997
- handleType: SummaryType.Tree,
998
- },
999
- stats,
1000
- };
1001
- }
1002
-
1003
- // If either or both of GC state or tombstone state changed, build a GC summary tree.
1004
- return this.buildGCSummaryTree(serializedGCState, serializedTombstones, true /* trackState */);
1005
- }
1006
- }
1007
- // If not tracking GC state, build a GC summary tree without any summary handles.
1008
- return this.buildGCSummaryTree(serializedGCState, serializedTombstones, false /* trackState */);
1009
- }
1010
-
1011
- /**
1012
- * Builds the GC summary tree which contains GC state and tombstone state.
1013
- * If trackState is false, both GC state and tombstone state are written as summary blobs.
1014
- * If trackState is true, summary blob is written for GC state or tombstone state if they changed.
1015
- * @param serializedGCState - The GC state serialized as string.
1016
- * @param serializedTombstones - THe tombstone state serialized as string.
1017
- * @param trackState - Whether we are tracking GC state across summaries.
1018
- * @returns the GC summary tree.
1019
- */
1020
- private buildGCSummaryTree(
1021
- serializedGCState: string,
1022
- serializedTombstones: string | undefined,
1023
- trackState: boolean,
1024
- ): ISummaryTreeWithStats {
1025
- const gcStateBlobKey = `${gcBlobPrefix}_root`;
1026
- const builder = new SummaryTreeBuilder();
1027
-
1028
- // If the GC state hasn't changed, write a summary handle, else write a summary blob for it.
1029
- if (this.latestSummaryData?.serializedGCState === serializedGCState && trackState) {
1030
- builder.addHandle(gcStateBlobKey, SummaryType.Blob, `/${gcTreeKey}/${gcStateBlobKey}`);
1031
- } else {
1032
- builder.addBlob(gcStateBlobKey, serializedGCState);
1033
- }
1034
-
1035
- // If there is no tombstone data, return only the GC state.
1036
- if (serializedTombstones === undefined) {
1037
- return builder.getSummaryTree();
1038
- }
1039
-
1040
- // If the tombstone state hasn't changed, write a summary handle, else write a summary blob for it.
1041
- if (this.latestSummaryData?.serializedTombstones === serializedTombstones && trackState) {
1042
- builder.addHandle(gcTombstoneBlobKey, SummaryType.Blob, `/${gcTreeKey}/${gcTombstoneBlobKey}`);
1043
- } else {
1044
- builder.addBlob(gcTombstoneBlobKey, serializedTombstones);
1045
- }
1046
- return builder.getSummaryTree();
1047
- }
1048
-
1049
- public getMetadata(): IGCMetadata {
1050
- return {
1051
- /**
1052
- * If GC is enabled, the GC data is written using the current GC version and that is the gcFeature that goes
1053
- * into the metadata blob. If GC is disabled, the gcFeature is 0.
1054
- */
1055
- gcFeature: this.gcEnabled ? this.currentGCVersion : 0,
1056
- sessionExpiryTimeoutMs: this.sessionExpiryTimeoutMs,
1057
- sweepEnabled: this.sweepEnabled,
1058
- sweepTimeoutMs: this.sweepTimeoutMs,
1059
- };
1060
- }
1061
-
1062
- /**
1063
- * Returns a map of node ids to their base GC details generated from the base summary. This is used by the caller
1064
- * to initialize the GC state of the nodes.
1065
- */
1066
- public async getBaseGCDetails(): Promise<Map<string, IGarbageCollectionDetailsBase>> {
1067
- return this.baseGCDetailsP;
1068
- }
1069
-
1070
- /**
1071
- * Called when the latest summary of the system has been refreshed. This will be used to update the state of the
1072
- * latest summary tracked.
1073
- */
1074
- public async latestSummaryStateRefreshed(
1075
- result: RefreshSummaryResult,
1076
- readAndParseBlob: ReadAndParseBlob,
1077
- ): Promise<void> {
1078
- if (!this.shouldRunGC || !result.latestSummaryUpdated) {
1079
- return;
1080
- }
1081
-
1082
- // If the summary was tracked by this client, it was the one that generated the summary in the first place.
1083
- // Basically, it was written in the current GC version.
1084
- if (result.wasSummaryTracked) {
1085
- this.latestSummaryGCVersion = this.currentGCVersion;
1086
- this.initialStateNeedsReset = false;
1087
- if (this.trackGCState) {
1088
- this.latestSummaryData = this.pendingSummaryData;
1089
- this.pendingSummaryData = undefined;
1090
- }
1091
- return;
1092
- }
1093
- // If the summary was not tracked by this client, update latest GC version and blob from the snapshot in the
1094
- // result as that is now the latest summary.
1095
- const snapshot = result.snapshot;
1096
- const metadataBlobId = snapshot.blobs[metadataBlobName];
1097
- if (metadataBlobId) {
1098
- const metadata = await readAndParseBlob<IContainerRuntimeMetadata>(metadataBlobId);
1099
- this.latestSummaryGCVersion = getGCVersion(metadata);
1100
- }
1101
-
1102
- const gcSnapshotTree = snapshot.trees[gcTreeKey];
1103
- if (gcSnapshotTree !== undefined && this.trackGCState) {
1104
- const latestGCData = await getGCDataFromSnapshot(
1105
- gcSnapshotTree,
1106
- readAndParseBlob,
1107
- );
1108
- this.latestSummaryData = {
1109
- serializedGCState: JSON.stringify(generateSortedGCState(latestGCData.gcState)),
1110
- serializedTombstones: JSON.stringify(latestGCData.tombstones),
1111
- };
1112
- } else {
1113
- this.latestSummaryData = undefined;
1114
- }
1115
- this.pendingSummaryData = undefined;
1116
- }
1117
-
1118
- /**
1119
- * Called when a node with the given id is updated. If the node is inactive, log an error.
1120
- * @param nodePath - The id of the node that changed.
1121
- * @param reason - Whether the node was loaded or changed.
1122
- * @param timestampMs - The timestamp when the node changed.
1123
- * @param packagePath - The package path of the node. This may not be available if the node hasn't been loaded yet.
1124
- * @param requestHeaders - If the node was loaded via request path, the headers in the request.
1125
- */
1126
- public nodeUpdated(
1127
- nodePath: string,
1128
- reason: "Loaded" | "Changed",
1129
- timestampMs?: number,
1130
- packagePath?: readonly string[],
1131
- requestHeaders?: IRequestHeader,
1132
- ) {
1133
- if (!this.shouldRunGC) {
1134
- return;
1135
- }
1136
-
1137
- const nodeStateTracker = this.unreferencedNodesState.get(nodePath);
1138
- if (nodeStateTracker && nodeStateTracker.state !== UnreferencedState.Active) {
1139
- this.inactiveNodeUsed(
1140
- reason,
1141
- nodePath,
1142
- nodeStateTracker,
1143
- undefined /* fromNodeId */,
1144
- packagePath,
1145
- timestampMs,
1146
- requestHeaders,
1147
- );
1148
- }
1149
- }
1150
-
1151
- /**
1152
- * Called when an outbound reference is added to a node. This is used to identify all nodes that have been
1153
- * referenced between summaries so that their unreferenced timestamp can be reset.
1154
- *
1155
- * @param fromNodePath - The node from which the reference is added.
1156
- * @param toNodePath - The node to which the reference is added.
1157
- */
1158
- public addedOutboundReference(fromNodePath: string, toNodePath: string) {
1159
- if (!this.shouldRunGC) {
1160
- return;
1161
- }
1162
-
1163
- const outboundRoutes = this.newReferencesSinceLastRun.get(fromNodePath) ?? [];
1164
- outboundRoutes.push(toNodePath);
1165
- this.newReferencesSinceLastRun.set(fromNodePath, outboundRoutes);
1166
-
1167
- const nodeStateTracker = this.unreferencedNodesState.get(toNodePath);
1168
- if (nodeStateTracker && nodeStateTracker.state !== UnreferencedState.Active) {
1169
- this.inactiveNodeUsed("Revived", toNodePath, nodeStateTracker, fromNodePath);
1170
- }
1171
- }
1172
-
1173
- public dispose(): void {
1174
- this.sessionExpiryTimer?.clear();
1175
- this.sessionExpiryTimer = undefined;
1176
- }
1177
-
1178
- /**
1179
- * Updates the state of the system as per the current GC run. It does the following:
1180
- * 1. Sets up the current GC state as per the gcData.
1181
- * 2. Starts tracking for nodes that have become unreferenced in this run.
1182
- * 3. Clears tracking for nodes that were unreferenced but became referenced in this run.
1183
- * @param gcData - The data representing the reference graph on which GC is run.
1184
- * @param gcResult - The result of the GC run on the gcData.
1185
- * @param currentReferenceTimestampMs - The timestamp to be used for unreferenced nodes' timestamp.
1186
- */
1187
- private updateCurrentState(
1188
- gcData: IGarbageCollectionData,
1189
- gcResult: IGCResult,
1190
- currentReferenceTimestampMs: number,
1191
- ) {
1192
- this.previousGCDataFromLastRun = cloneGCData(gcData);
1193
- this.tombstones = [];
1194
- this.newReferencesSinceLastRun.clear();
1195
-
1196
- // Iterate through the referenced nodes and stop tracking if they were unreferenced before.
1197
- for (const nodeId of gcResult.referencedNodeIds) {
1198
- const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
1199
- if (nodeStateTracker !== undefined) {
1200
- // Stop tracking so as to clear out any running timers.
1201
- nodeStateTracker.stopTracking();
1202
- // Delete the node as we don't need to track it any more.
1203
- this.unreferencedNodesState.delete(nodeId);
1204
- }
1205
- }
1206
-
1207
- /**
1208
- * If a node became unreferenced in this run, start tracking it.
1209
- * If a node was already unreferenced, update its tracking information. Since the current reference time is
1210
- * from the ops seen, this will ensure that we keep updating the unreferenced state as time moves forward.
1211
- */
1212
- for (const nodeId of gcResult.deletedNodeIds) {
1213
- const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
1214
- if (nodeStateTracker === undefined) {
1215
- this.unreferencedNodesState.set(
1216
- nodeId,
1217
- new UnreferencedStateTracker(
1218
- currentReferenceTimestampMs,
1219
- this.inactiveTimeoutMs,
1220
- currentReferenceTimestampMs,
1221
- this.sweepTimeoutMs,
1222
- ),
1223
- );
1224
- } else {
1225
- nodeStateTracker.updateTracking(currentReferenceTimestampMs);
1226
- if (this.tombstoneMode && nodeStateTracker.state === UnreferencedState.SweepReady) {
1227
- const nodeType = this.runtime.getNodeType(nodeId);
1228
- if (nodeType === GCNodeType.DataStore || nodeType === GCNodeType.Blob) {
1229
- this.tombstones.push(nodeId);
1230
- }
1231
- }
1232
- }
1233
- }
1234
- }
1235
-
1236
- /**
1237
- * Since GC runs periodically, the GC data that is generated only tells us the state of the world at that point in
1238
- * time. There can be nodes that were referenced in between two runs and their unreferenced state needs to be
1239
- * updated. For example, in the following scenarios not updating the unreferenced timestamp can lead to deletion of
1240
- * these objects while there can be in-memory referenced to it:
1241
- * 1. A node transitions from `unreferenced -> referenced -> unreferenced` between two runs. When the reference is
1242
- * added, the object may have been accessed and in-memory reference to it added.
1243
- * 2. A reference is added from one unreferenced node to one or more unreferenced nodes. Even though the node[s] were
1244
- * unreferenced, they could have been accessed and in-memory reference to them added.
1245
- *
1246
- * This function identifies nodes that were referenced since last run and removes their unreferenced state, if any.
1247
- * If these nodes are currently unreferenced, they will be assigned new unreferenced state by the current run.
1248
- */
1249
- private updateStateSinceLastRun(currentGCData: IGarbageCollectionData, logger: ITelemetryLogger) {
1250
- // If we haven't run GC before there is nothing to do.
1251
- if (this.previousGCDataFromLastRun === undefined) {
1252
- return;
1253
- }
1254
-
1255
- // Find any references that haven't been identified correctly.
1256
- const missingExplicitReferences = this.findMissingExplicitReferences(
1257
- currentGCData,
1258
- this.previousGCDataFromLastRun,
1259
- this.newReferencesSinceLastRun,
1260
- );
1261
-
1262
- if (missingExplicitReferences.length > 0) {
1263
- missingExplicitReferences.forEach((missingExplicitReference) => {
1264
- const event: ITelemetryPerformanceEvent = {
1265
- eventName: "gcUnknownOutboundReferences",
1266
- gcNodeId: missingExplicitReference[0],
1267
- gcRoutes: JSON.stringify(missingExplicitReference[1]),
1268
- };
1269
- logger.sendPerformanceEvent(event);
1270
- });
1271
- }
1272
-
1273
- // No references were added since the last run so we don't have to update reference states of any unreferenced
1274
- // nodes
1275
- if (this.newReferencesSinceLastRun.size === 0) {
1276
- return;
1277
- }
1278
-
1279
- /**
1280
- * Generate a super set of the GC data that contains the nodes and edges from last run, plus any new node and
1281
- * edges that have been added since then. To do this, combine the GC data from the last run and the current
1282
- * run, and then add the references since last run.
1283
- *
1284
- * Note on why we need to combine the data from previous run, current run and all references in between -
1285
- * 1. We need data from last run because some of its references may have been deleted since then. If those
1286
- * references added new outbound references before they were deleted, we need to detect them.
1287
- *
1288
- * 2. We need new outbound references since last run because some of them may have been deleted later. If those
1289
- * references added new outbound references before they were deleted, we need to detect them.
1290
- *
1291
- * 3. We need data from the current run because currently we may not detect when DDSes are referenced:
1292
- * - We don't require DDSes handles to be stored in a referenced DDS.
1293
- * - A new data store may have "root" DDSes already created and we don't detect them today.
1294
- */
1295
- const gcDataSuperSet = concatGarbageCollectionData(this.previousGCDataFromLastRun, currentGCData);
1296
- const newOutboundRoutesSinceLastRun: string[] = [];
1297
- this.newReferencesSinceLastRun.forEach((outboundRoutes: string[], sourceNodeId: string) => {
1298
- if (gcDataSuperSet.gcNodes[sourceNodeId] === undefined) {
1299
- gcDataSuperSet.gcNodes[sourceNodeId] = outboundRoutes;
1300
- } else {
1301
- gcDataSuperSet.gcNodes[sourceNodeId].push(...outboundRoutes);
1302
- }
1303
- newOutboundRoutesSinceLastRun.push(...outboundRoutes);
1304
- });
1305
-
1306
- /**
1307
- * Run GC on the above reference graph starting with root and all new outbound routes. This will generate a
1308
- * list of all nodes that could have been referenced since the last run. If any of these nodes are unreferenced,
1309
- * unreferenced, stop tracking them and remove from unreferenced list.
1310
- * Note that some of these nodes may be unreferenced now and if so, the current run will mark them as
1311
- * unreferenced and add unreferenced state.
1312
- */
1313
- const gcResult = runGarbageCollection(gcDataSuperSet.gcNodes, ["/", ...newOutboundRoutesSinceLastRun]);
1314
- for (const nodeId of gcResult.referencedNodeIds) {
1315
- const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
1316
- if (nodeStateTracker !== undefined) {
1317
- // Stop tracking so as to clear out any running timers.
1318
- nodeStateTracker.stopTracking();
1319
- // Delete the unreferenced state as we don't need to track it any more.
1320
- this.unreferencedNodesState.delete(nodeId);
1321
- }
1322
- }
1323
- }
1324
-
1325
- /**
1326
- * Finds all new references or outbound routes in the current graph that haven't been explicitly notified to GC.
1327
- * The principle is that every new reference or outbound route must be notified to GC via the
1328
- * addedOutboundReference method. It it hasn't, its a bug and we want to identify these scenarios.
1329
- *
1330
- * In more simple terms:
1331
- * Missing Explicit References = Current References - Previous References - Explicitly Added References;
1332
- *
1333
- * @param currentGCData - The GC data (reference graph) from the current GC run.
1334
- * @param previousGCData - The GC data (reference graph) from the previous GC run.
1335
- * @param explicitReferences - New references added explicity between the previous and the current run.
1336
- * @returns - a list of missing explicit references
1337
- */
1338
- private findMissingExplicitReferences(
1339
- currentGCData: IGarbageCollectionData,
1340
- previousGCData: IGarbageCollectionData,
1341
- explicitReferences: Map<string, string[]>,
1342
- ): [string, string[]][] {
1343
- assert(
1344
- previousGCData !== undefined,
1345
- 0x2b7, /* "Can't validate correctness without GC data from last run" */
1346
- );
1347
-
1348
- const currentGraph = Object.entries(currentGCData.gcNodes);
1349
- const missingExplicitReferences: [string, string[]][] = [];
1350
- currentGraph.forEach(([nodeId, currentOutboundRoutes]) => {
1351
- const previousRoutes = previousGCData.gcNodes[nodeId] ?? [];
1352
- const explicitRoutes = explicitReferences.get(nodeId) ?? [];
1353
- const missingExplicitRoutes: string[] = [];
1354
- currentOutboundRoutes.forEach((route) => {
1355
- const isBlobOrDataStoreRoute =
1356
- this.runtime.getNodeType(route) === GCNodeType.Blob ||
1357
- this.runtime.getNodeType(route) === GCNodeType.DataStore;
1358
- // Ignore implicitly added DDS routes to their parent datastores
1359
- const notRouteFromDDSToParentDataStore = !nodeId.startsWith(route);
1360
- if (
1361
- isBlobOrDataStoreRoute &&
1362
- notRouteFromDDSToParentDataStore &&
1363
- (!previousRoutes.includes(route) && !explicitRoutes.includes(route))
1364
- ) {
1365
- missingExplicitRoutes.push(route);
1366
- }
1367
- });
1368
- if (missingExplicitRoutes.length > 0) {
1369
- missingExplicitReferences.push([nodeId, missingExplicitRoutes]);
1370
- }
1371
- });
1372
-
1373
- // Ideally missingExplicitReferences should always have a size 0
1374
- return missingExplicitReferences;
1375
- }
1376
-
1377
- /**
1378
- * Generates the stats of a garbage collection run from the given results of the run.
1379
- * @param gcResult - The result of a GC run.
1380
- * @returns the GC stats of the GC run.
1381
- */
1382
- private generateStats(gcResult: IGCResult): IGCStats {
1383
- const gcStats: IGCStats = {
1384
- nodeCount: 0,
1385
- dataStoreCount: 0,
1386
- attachmentBlobCount: 0,
1387
- unrefNodeCount: 0,
1388
- unrefDataStoreCount: 0,
1389
- unrefAttachmentBlobCount: 0,
1390
- updatedNodeCount: 0,
1391
- updatedDataStoreCount: 0,
1392
- updatedAttachmentBlobCount: 0,
1393
- };
1394
-
1395
- const updateNodeStats = (nodeId: string, referenced: boolean) => {
1396
- gcStats.nodeCount++;
1397
- // If there is no previous GC data, every node's state is generated and is considered as updated.
1398
- // Otherwise, find out if any node went from referenced to unreferenced or vice-versa.
1399
- const stateUpdated = this.previousGCDataFromLastRun === undefined ||
1400
- this.unreferencedNodesState.has(nodeId) === referenced;
1401
- if (stateUpdated) {
1402
- gcStats.updatedNodeCount++;
1403
- }
1404
- if (!referenced) {
1405
- gcStats.unrefNodeCount++;
1406
- }
1407
-
1408
- if (this.runtime.getNodeType(nodeId) === GCNodeType.DataStore) {
1409
- gcStats.dataStoreCount++;
1410
- if (stateUpdated) {
1411
- gcStats.updatedDataStoreCount++;
1412
- }
1413
- if (!referenced) {
1414
- gcStats.unrefDataStoreCount++;
1415
- }
1416
- }
1417
- if (this.runtime.getNodeType(nodeId) === GCNodeType.Blob) {
1418
- gcStats.attachmentBlobCount++;
1419
- if (stateUpdated) {
1420
- gcStats.updatedAttachmentBlobCount++;
1421
- }
1422
- if (!referenced) {
1423
- gcStats.unrefAttachmentBlobCount++;
1424
- }
1425
- }
1426
- };
1427
-
1428
- for (const nodeId of gcResult.referencedNodeIds) {
1429
- updateNodeStats(nodeId, true /* referenced */);
1430
- }
1431
-
1432
- for (const nodeId of gcResult.deletedNodeIds) {
1433
- updateNodeStats(nodeId, false /* referenced */);
1434
- }
1435
-
1436
- return gcStats;
1437
- }
1438
-
1439
- /**
1440
- * For nodes that are ready to sweep, log an event for now. Until we start running sweep which deletes objects,
1441
- * this will give us a view into how much deleted content a container has.
1442
- */
1443
- private logSweepEvents(logger: ITelemetryLogger, currentReferenceTimestampMs: number) {
1444
- if (this.mc.config.getBoolean(disableSweepLogKey) === true || this.sweepTimeoutMs === undefined) {
1445
- return;
1446
- }
1447
-
1448
- this.unreferencedNodesState.forEach((nodeStateTracker, nodeId) => {
1449
- if (nodeStateTracker.state !== UnreferencedState.SweepReady) {
1450
- return;
1451
- }
1452
-
1453
- const nodeType = this.runtime.getNodeType(nodeId);
1454
- if (nodeType !== GCNodeType.DataStore && nodeType !== GCNodeType.Blob) {
1455
- return;
1456
- }
1457
-
1458
- // Log deleted event for each node only once to reduce noise in telemetry.
1459
- const uniqueEventId = `Deleted-${nodeId}`;
1460
- if (this.loggedUnreferencedEvents.has(uniqueEventId)) {
1461
- return;
1462
- }
1463
- this.loggedUnreferencedEvents.add(uniqueEventId);
1464
- logger.sendTelemetryEvent({
1465
- eventName: "GCObjectDeleted",
1466
- id: nodeId,
1467
- type: nodeType,
1468
- age: currentReferenceTimestampMs - nodeStateTracker.unreferencedTimestampMs,
1469
- timeout: this.sweepTimeoutMs,
1470
- completedGCRuns: this.completedRuns,
1471
- lastSummaryTime: this.getLastSummaryTimestampMs(),
1472
- });
1473
- });
1474
- }
1475
-
1476
- /**
1477
- * Called when an inactive node is used after. Queue up an event that will be logged next time GC runs.
1478
- */
1479
- private inactiveNodeUsed(
1480
- usageType: "Changed" | "Loaded" | "Revived",
1481
- nodeId: string,
1482
- nodeStateTracker: UnreferencedStateTracker,
1483
- fromNodeId?: string,
1484
- packagePath?: readonly string[],
1485
- currentReferenceTimestampMs = this.runtime.getCurrentReferenceTimestampMs(),
1486
- requestHeaders?: IRequestHeader,
1487
- ) {
1488
- // If there is no reference timestamp to work with, no ops have been processed after creation. If so, skip
1489
- // logging as nothing interesting would have happened worth logging.
1490
- // If the node is active, skip logging.
1491
- if (currentReferenceTimestampMs === undefined || nodeStateTracker.state === UnreferencedState.Active) {
1492
- return;
1493
- }
1494
-
1495
- // We only care about data stores and attachment blobs for this telemetry since GC only marks these objects
1496
- // as unreferenced. Also, if an inactive DDS is used, the corresponding data store store will also be used.
1497
- const nodeType = this.runtime.getNodeType(nodeId);
1498
- if (nodeType !== GCNodeType.DataStore && nodeType !== GCNodeType.Blob) {
1499
- return;
1500
- }
1501
-
1502
- const state = nodeStateTracker.state;
1503
- const uniqueEventId = `${state}-${nodeId}-${usageType}`;
1504
- if (this.loggedUnreferencedEvents.has(uniqueEventId)) {
1505
- return;
1506
- }
1507
- this.loggedUnreferencedEvents.add(uniqueEventId);
1508
-
1509
- const propsToLog = {
1510
- id: nodeId,
1511
- type: nodeType,
1512
- unrefTime: nodeStateTracker.unreferencedTimestampMs,
1513
- age: currentReferenceTimestampMs - nodeStateTracker.unreferencedTimestampMs,
1514
- timeout: nodeStateTracker.state === UnreferencedState.Inactive
1515
- ? this.inactiveTimeoutMs
1516
- : this.sweepTimeoutMs,
1517
- completedGCRuns: this.completedRuns,
1518
- lastSummaryTime: this.getLastSummaryTimestampMs(),
1519
- ...this.createContainerMetadata,
1520
- externalRequest: requestHeaders?.[RuntimeHeaders.externalRequest],
1521
- viaHandle: requestHeaders?.[RuntimeHeaders.viaHandle],
1522
- fromId: fromNodeId,
1523
- };
1524
-
1525
- // For summarizer client, queue the event so it is logged the next time GC runs if the event is still valid.
1526
- // For non-summarizer client, log the event now since GC won't run on it. This may result in false positives
1527
- // but it's a good signal nonetheless and we can consume it with a grain of salt.
1528
- // Inactive errors are usages of Objects that are unreferenced for at least a period of 7 days.
1529
- // SweepReady errors are usages of Objects that will be deleted by GC Sweep!
1530
- if (this.isSummarizerClient) {
1531
- this.pendingEventsQueue.push({ ...propsToLog, usageType, state });
1532
- } else {
1533
- // For non-summarizer clients, only log "Loaded" type events since these objects may not be loaded in the
1534
- // summarizer clients if they are based off of user actions (such as scrolling to content for these objects)
1535
- // Events generated:
1536
- // InactiveObject_Loaded, SweepReadyObject_Loaded
1537
- if (usageType === "Loaded") {
1538
- this.mc.logger.sendErrorEvent({
1539
- ...propsToLog,
1540
- eventName: `${state}Object_${usageType}`,
1541
- pkg: packagePathToTelemetryProperty(packagePath),
1542
- stack: generateStack(),
1543
- });
1544
- }
1545
-
1546
- // If SweepReady Usage Detection is enabed, the handler may close the interactive container.
1547
- // Once Sweep is fully implemented, this will be removed since the objects will be gone
1548
- // and errors will arise elsewhere in the runtime
1549
- if (state === UnreferencedState.SweepReady) {
1550
- this.sweepReadyUsageHandler.usageDetectedInInteractiveClient({ ...propsToLog, usageType });
1551
- }
1552
- }
1553
- }
1554
-
1555
- private async logUnreferencedEvents(logger: ITelemetryLogger) {
1556
- // Events sent come only from the summarizer client. In between summaries, events are pushed to a queue and at
1557
- // summary time they are then logged.
1558
- // Events generated:
1559
- // InactiveObject_Loaded, InactiveObject_Changed, InactiveObject_Revived
1560
- // SweepReadyObject_Loaded, SweepReadyObject_Changed, SweepReadyObject_Revived
1561
- for (const eventProps of this.pendingEventsQueue) {
1562
- const { usageType, state, ...propsToLog } = eventProps;
1563
- /**
1564
- * Revived event is logged only if the node is active. If the node is not active, the reference to it was
1565
- * from another unreferenced node and this scenario is not interesting to log.
1566
- * Loaded and Changed events are logged only if the node is not active. If the node is active, it was
1567
- * revived and a Revived event will be logged for it.
1568
- */
1569
- const nodeStateTracker = this.unreferencedNodesState.get(eventProps.id);
1570
- const active = nodeStateTracker === undefined || nodeStateTracker.state === UnreferencedState.Active;
1571
- if ((usageType === "Revived") === active) {
1572
- const pkg = await this.getNodePackagePath(eventProps.id);
1573
- const fromPkg = eventProps.fromId ? await this.getNodePackagePath(eventProps.fromId) : undefined;
1574
- logger.sendErrorEvent({
1575
- ...propsToLog,
1576
- eventName: `${state}Object_${usageType}`,
1577
- pkg: pkg ? { value: pkg.join("/"), tag: TelemetryDataTag.CodeArtifact } : undefined,
1578
- fromPkg: fromPkg ? { value: fromPkg.join("/"), tag: TelemetryDataTag.CodeArtifact } : undefined,
1579
- });
1580
- }
1581
- }
1582
- this.pendingEventsQueue = [];
1583
- }
1584
- }
1585
-
1586
- /**
1587
- * Gets the base garbage collection state from the given snapshot tree. It contains GC state and tombstone state.
1588
- * The GC state may be written into multiple blobs. Merge the GC state from all such blobs into one.
1589
- */
1590
- async function getGCDataFromSnapshot(
1591
- gcSnapshotTree: ISnapshotTree,
1592
- readAndParseBlob: ReadAndParseBlob,
1593
- ): Promise<IGCSnapshotData> {
1594
- let rootGCState: IGarbageCollectionState = { gcNodes: {} };
1595
- let tombstones: string[] | undefined;
1596
- for (const key of Object.keys(gcSnapshotTree.blobs)) {
1597
- if (key === gcTombstoneBlobKey) {
1598
- tombstones = await readAndParseBlob<string[]>(gcSnapshotTree.blobs[key]);
1599
- continue;
1600
- }
1601
-
1602
- // Skip blobs that do not start with the GC prefix.
1603
- if (!key.startsWith(gcBlobPrefix)) {
1604
- continue;
1605
- }
1606
-
1607
- const blobId = gcSnapshotTree.blobs[key];
1608
- if (blobId === undefined) {
1609
- continue;
1610
- }
1611
- const gcState = await readAndParseBlob<IGarbageCollectionState>(blobId);
1612
- assert(gcState !== undefined, 0x2ad /* "GC blob missing from snapshot" */);
1613
- // Merge the GC state of this blob into the root GC state.
1614
- rootGCState = concatGarbageCollectionStates(rootGCState, gcState);
1615
- }
1616
- return { gcState: rootGCState, tombstones };
353
+ public static create(createParams: IGarbageCollectorCreateParams): IGarbageCollector {
354
+ return new GarbageCollector(createParams);
355
+ }
356
+
357
+ /**
358
+ * Tells whether the GC state needs to be reset in the next summary. We need to do this if:
359
+ *
360
+ * 1. GC was enabled and is now disabled. The GC state needs to be removed and everything becomes referenced.
361
+ *
362
+ * 2. GC was disabled and is now enabled. The GC state needs to be regenerated and added to summary.
363
+ *
364
+ * 3. GC is enabled and the latest summary state is refreshed from a snapshot that had GC disabled and vice-versa.
365
+ *
366
+ * 4. The GC version in the latest summary is different from the current GC version. This can happen if:
367
+ *
368
+ * 4.1. The summary this client loaded with has data from a different GC version.
369
+ *
370
+ * 4.2. This client's latest summary was updated from a snapshot that has a different GC version.
371
+ */
372
+ public get summaryStateNeedsReset(): boolean {
373
+ return (
374
+ this.gcStateNeedsReset ||
375
+ (this.shouldRunGC && this.latestSummaryGCVersion !== this.currentGCVersion)
376
+ );
377
+ }
378
+
379
+ /**
380
+ * Tracks if GC is enabled for this document. This is specified during document creation and doesn't change
381
+ * throughout its lifetime.
382
+ */
383
+ private readonly gcEnabled: boolean;
384
+ /**
385
+ * Tracks if sweep phase is enabled for this document. This is specified during document creation and doesn't change
386
+ * throughout its lifetime.
387
+ */
388
+ private readonly sweepEnabled: boolean;
389
+
390
+ /**
391
+ * Tracks if GC should run or not. Even if GC is enabled for a document (see gcEnabled), it can be explicitly
392
+ * disabled via runtime options or feature flags.
393
+ */
394
+ public readonly shouldRunGC: boolean;
395
+ /**
396
+ * Tracks if sweep phase should run or not. Even if the sweep phase is enabled for a document (see sweepEnabled), it
397
+ * can be explicitly disabled via feature flags. It also won't run if session expiry is not enabled.
398
+ */
399
+ private readonly shouldRunSweep: boolean;
400
+
401
+ public readonly trackGCState: boolean;
402
+
403
+ private readonly testMode: boolean;
404
+ private readonly tombstoneMode: boolean;
405
+ private readonly mc: MonitoringContext;
406
+
407
+ /**
408
+ * Tells whether the GC state needs to be reset. This can happen under 3 conditions:
409
+ *
410
+ * 1. The base snapshot contains GC state but GC is disabled. This will happen the first time GC is disabled after
411
+ * it was enabled before. GC state needs to be removed from summary and all nodes should be marked referenced.
412
+ *
413
+ * 2. The base snapshot does not have GC state but GC is enabled. This will happen the very first time GC runs on
414
+ * a document and the first time GC is enabled after is was disabled before.
415
+ *
416
+ * 3. GC is enabled and the latest summary state is refreshed from a snapshot that had GC disabled and vice-versa.
417
+ *
418
+ * Note that the state will be reset only once for the first summary generated after this returns true. After that,
419
+ * this will return false.
420
+ */
421
+ private get gcStateNeedsReset(): boolean {
422
+ return this.wasGCRunInLatestSummary !== this.shouldRunGC;
423
+ }
424
+ // Tracks whether there was GC was run in latest summary being tracked.
425
+ private wasGCRunInLatestSummary: boolean;
426
+
427
+ // The current GC version that this container is running.
428
+ private readonly currentGCVersion: GCVersion;
429
+ // This is the version of GC data in the latest summary being tracked.
430
+ private latestSummaryGCVersion: GCVersion;
431
+
432
+ // Keeps track of the GC state from the last run.
433
+ private gcDataFromLastRun: IGarbageCollectionData | undefined;
434
+ // Keeps a list of references (edges in the GC graph) between GC runs. Each entry has a node id and a list of
435
+ // outbound routes from that node.
436
+ private readonly newReferencesSinceLastRun: Map<string, string[]> = new Map();
437
+ // A list of nodes that have been tombstoned.
438
+ private tombstones: string[] = [];
439
+ // A list of nodes that have been deleted during sweep phase.
440
+ private deletedNodes: Set<string> = new Set();
441
+
442
+ /**
443
+ * Keeps track of the GC data from the latest summary successfully submitted to and acked from the server.
444
+ */
445
+ private latestSummaryData: IGCSummaryTrackingData | undefined;
446
+ /**
447
+ * Keeps track of the GC data from the last summary submitted to the server but not yet acked.
448
+ */
449
+ private pendingSummaryData: IGCSummaryTrackingData | undefined;
450
+
451
+ // Promise when resolved returns the GC data data in the base snapshot.
452
+ private readonly baseSnapshotDataP: Promise<IGarbageCollectionSnapshotData | undefined>;
453
+ // Promise when resolved initializes the GC state from the data in the base snapshot.
454
+ private readonly initializeGCStateFromBaseSnapshotP: Promise<void>;
455
+ // The GC details generated from the base snapshot.
456
+ private readonly baseGCDetailsP: Promise<IGarbageCollectionDetailsBase>;
457
+ // Map of node ids to their unreferenced state tracker.
458
+ private readonly unreferencedNodesState: Map<string, UnreferencedStateTracker> = new Map();
459
+ // The Timer responsible for closing the container when the session has expired
460
+ private sessionExpiryTimer: Timer | undefined;
461
+
462
+ // Keeps track of unreferenced events that are logged for a node. This is used to limit the log generation to one
463
+ // per event per node.
464
+ private readonly loggedUnreferencedEvents: Set<string> = new Set();
465
+ // Queue for unreferenced events that should be logged the next time GC runs.
466
+ private pendingEventsQueue: IUnreferencedEventProps[] = [];
467
+
468
+ // The number of times GC has successfully completed on this instance of GarbageCollector.
469
+ private completedRuns = 0;
470
+
471
+ private readonly runtime: IGarbageCollectionRuntime;
472
+ private readonly createContainerMetadata: ICreateContainerMetadata;
473
+ private readonly gcOptions: IGCRuntimeOptions;
474
+ private readonly isSummarizerClient: boolean;
475
+
476
+ /** The time in ms to expire a session for a client for gc. */
477
+ private readonly sessionExpiryTimeoutMs: number | undefined;
478
+ /** The time after which an unreferenced node is inactive. */
479
+ private readonly inactiveTimeoutMs: number;
480
+ /** The time after which an unreferenced node is ready to be swept. */
481
+ private readonly sweepTimeoutMs: number | undefined;
482
+
483
+ /** For a given node path, returns the node's package path. */
484
+ private readonly getNodePackagePath: (
485
+ nodePath: string,
486
+ ) => Promise<readonly string[] | undefined>;
487
+ /** Returns the timestamp of the last summary generated for this container. */
488
+ private readonly getLastSummaryTimestampMs: () => number | undefined;
489
+ /** Returns true if connection is active, i.e. it's "write" connection and the runtime is connected. */
490
+ private readonly activeConnection: () => boolean;
491
+
492
+ /** Returns a list of all the configurations for garbage collection. */
493
+ private get configs() {
494
+ return {
495
+ gcEnabled: this.gcEnabled,
496
+ sweepEnabled: this.sweepEnabled,
497
+ runGC: this.shouldRunGC,
498
+ runSweep: this.shouldRunSweep,
499
+ testMode: this.testMode,
500
+ tombstoneMode: this.tombstoneMode,
501
+ sessionExpiry: this.sessionExpiryTimeoutMs,
502
+ sweepTimeout: this.sweepTimeoutMs,
503
+ inactiveTimeout: this.inactiveTimeoutMs,
504
+ trackGCState: this.trackGCState,
505
+ ...this.gcOptions,
506
+ };
507
+ }
508
+
509
+ /** Handler to respond to when a SweepReady object is used */
510
+ private readonly sweepReadyUsageHandler: SweepReadyUsageDetectionHandler;
511
+
512
+ protected constructor(createParams: IGarbageCollectorCreateParams) {
513
+ this.runtime = createParams.runtime;
514
+ this.isSummarizerClient = createParams.isSummarizerClient;
515
+ this.gcOptions = createParams.gcOptions;
516
+ this.createContainerMetadata = createParams.createContainerMetadata;
517
+ this.getNodePackagePath = createParams.getNodePackagePath;
518
+ this.getLastSummaryTimestampMs = createParams.getLastSummaryTimestampMs;
519
+ this.activeConnection = createParams.activeConnection;
520
+
521
+ const baseSnapshot = createParams.baseSnapshot;
522
+ const metadata = createParams.metadata;
523
+ const readAndParseBlob = createParams.readAndParseBlob;
524
+
525
+ this.mc = loggerToMonitoringContext(
526
+ ChildLogger.create(createParams.baseLogger, "GarbageCollector", {
527
+ all: { completedGCRuns: () => this.completedRuns },
528
+ }),
529
+ );
530
+
531
+ // If version upgrade is not enabled, fall back to the stable GC version.
532
+ this.currentGCVersion =
533
+ this.mc.config.getBoolean(gcVersionUpgradeToV2Key) === true
534
+ ? currentGCVersion
535
+ : stableGCVersion;
536
+
537
+ this.sweepReadyUsageHandler = new SweepReadyUsageDetectionHandler(
538
+ createParams.getContainerDiagnosticId(),
539
+ this.mc,
540
+ this.runtime.closeFn,
541
+ );
542
+
543
+ let prevSummaryGCVersion: number | undefined;
544
+
545
+ /**
546
+ * Sweep timeout is the time after which unreferenced content can be swept.
547
+ * Sweep timeout = session expiry timeout + snapshot cache expiry timeout + one day buffer.
548
+ *
549
+ * The snapshot cache expiry timeout cannot be known precisely but the upper bound is 5 days.
550
+ * The buffer is added to account for any clock skew or other edge cases.
551
+ * We use server timestamps throughout so the skew should be minimal but make it 1 day to be safe.
552
+ */
553
+ function computeSweepTimeout(sessionExpiryTimeoutMs: number | undefined) {
554
+ const maxSnapshotCacheExpiryMs = 5 * oneDayMs;
555
+ const bufferMs = oneDayMs;
556
+ return (
557
+ sessionExpiryTimeoutMs &&
558
+ sessionExpiryTimeoutMs + maxSnapshotCacheExpiryMs + bufferMs
559
+ );
560
+ }
561
+
562
+ /**
563
+ * The following GC state is enabled during container creation and cannot be changed throughout its lifetime:
564
+ * 1. Whether running GC mark phase is allowed or not.
565
+ * 2. Whether running GC sweep phase is allowed or not.
566
+ * 3. Whether GC session expiry is enabled or not.
567
+ * For existing containers, we get this information from the metadata blob of its summary.
568
+ */
569
+ if (createParams.existing) {
570
+ prevSummaryGCVersion = getGCVersion(metadata);
571
+ // Existing documents which did not have metadata blob or had GC disabled have version as 0. For all
572
+ // other existing documents, GC is enabled.
573
+ this.gcEnabled = prevSummaryGCVersion > 0;
574
+ this.sweepEnabled = metadata?.sweepEnabled ?? false;
575
+ this.sessionExpiryTimeoutMs = metadata?.sessionExpiryTimeoutMs;
576
+ this.sweepTimeoutMs =
577
+ metadata?.sweepTimeoutMs ?? computeSweepTimeout(this.sessionExpiryTimeoutMs); // Backfill old documents that didn't persist this
578
+ } else {
579
+ // Sweep should not be enabled without enabling GC mark phase. We could silently disable sweep in this
580
+ // scenario but explicitly failing makes it clearer and promotes correct usage.
581
+ if (this.gcOptions.sweepAllowed && this.gcOptions.gcAllowed === false) {
582
+ throw new UsageError(
583
+ "GC sweep phase cannot be enabled without enabling GC mark phase",
584
+ );
585
+ }
586
+
587
+ // This Test Override only applies for new containers
588
+ const testOverrideSweepTimeoutMs = this.mc.config.getNumber(
589
+ "Fluid.GarbageCollection.TestOverride.SweepTimeoutMs",
590
+ );
591
+
592
+ // For new documents, GC is enabled by default. It can be explicitly disabled by setting the gcAllowed
593
+ // flag in GC options to false.
594
+ this.gcEnabled = this.gcOptions.gcAllowed !== false;
595
+ // The sweep phase has to be explicitly enabled by setting the sweepAllowed flag in GC options to true.
596
+ this.sweepEnabled = this.gcOptions.sweepAllowed === true;
597
+
598
+ // Set the Session Expiry only if the flag is enabled and GC is enabled.
599
+ if (this.mc.config.getBoolean(runSessionExpiryKey) && this.gcEnabled) {
600
+ this.sessionExpiryTimeoutMs =
601
+ this.gcOptions.sessionExpiryTimeoutMs ?? defaultSessionExpiryDurationMs;
602
+ }
603
+ this.sweepTimeoutMs =
604
+ testOverrideSweepTimeoutMs ?? computeSweepTimeout(this.sessionExpiryTimeoutMs);
605
+ }
606
+
607
+ // If session expiry is enabled, we need to close the container when the session expiry timeout expires.
608
+ if (this.sessionExpiryTimeoutMs !== undefined) {
609
+ // If Test Override config is set, override Session Expiry timeout.
610
+ const overrideSessionExpiryTimeoutMs = this.mc.config.getNumber(
611
+ "Fluid.GarbageCollection.TestOverride.SessionExpiryMs",
612
+ );
613
+ const timeoutMs = overrideSessionExpiryTimeoutMs ?? this.sessionExpiryTimeoutMs;
614
+
615
+ this.sessionExpiryTimer = new Timer(timeoutMs, () => {
616
+ this.runtime.closeFn(
617
+ new ClientSessionExpiredError(`Client session expired.`, timeoutMs),
618
+ );
619
+ });
620
+ this.sessionExpiryTimer.start();
621
+ }
622
+
623
+ // For existing document, the latest summary is the one that we loaded from. So, use its GC version as the
624
+ // latest tracked GC version. For new documents, we will be writing the first summary with the current version.
625
+ this.latestSummaryGCVersion = prevSummaryGCVersion ?? this.currentGCVersion;
626
+
627
+ /**
628
+ * Whether GC should run or not. The following conditions have to be met to run sweep:
629
+ *
630
+ * 1. GC should be enabled for this container.
631
+ *
632
+ * 2. GC should not be disabled via disableGC GC option.
633
+ *
634
+ * These conditions can be overridden via runGCKey feature flag.
635
+ */
636
+ this.shouldRunGC =
637
+ this.mc.config.getBoolean(runGCKey) ??
638
+ // GC must be enabled for the document.
639
+ (this.gcEnabled &&
640
+ // GC must not be disabled via GC options.
641
+ !this.gcOptions.disableGC);
642
+
643
+ /**
644
+ * Whether sweep should run or not. The following conditions have to be met to run sweep:
645
+ *
646
+ * 1. Overall GC or mark phase must be enabled (this.shouldRunGC).
647
+ * 2. Sweep timeout should be available. Without this, we wouldn't know when an object should be deleted.
648
+ * 3. The driver must implement the policy limiting the age of snapshots used for loading. Otherwise
649
+ * the Sweep Timeout calculation is not valid. We use the persisted value to ensure consistency over time.
650
+ * 4. Sweep should be enabled for this container (this.sweepEnabled). This can be overridden via runSweep
651
+ * feature flag.
652
+ */
653
+ this.shouldRunSweep =
654
+ this.shouldRunGC &&
655
+ this.sweepTimeoutMs !== undefined &&
656
+ (this.mc.config.getBoolean(runSweepKey) ?? this.sweepEnabled);
657
+
658
+ this.trackGCState = this.mc.config.getBoolean(trackGCStateKey) === true;
659
+
660
+ // Override inactive timeout if test config or gc options to override it is set.
661
+ this.inactiveTimeoutMs =
662
+ this.mc.config.getNumber("Fluid.GarbageCollection.TestOverride.InactiveTimeoutMs") ??
663
+ this.gcOptions.inactiveTimeoutMs ??
664
+ defaultInactiveTimeoutMs;
665
+
666
+ // Inactive timeout must be greater than sweep timeout since a node goes from active -> inactive -> sweep ready.
667
+ if (this.sweepTimeoutMs !== undefined && this.inactiveTimeoutMs > this.sweepTimeoutMs) {
668
+ throw new UsageError("inactive timeout should not be greater than the sweep timeout");
669
+ }
670
+
671
+ // Whether we are running in test mode. In this mode, unreferenced nodes are immediately deleted.
672
+ this.testMode =
673
+ this.mc.config.getBoolean(gcTestModeKey) ?? this.gcOptions.runGCInTestMode === true;
674
+ // Whether we are running in tombstone mode. This is enabled by default if sweep won't run. It can be disabled
675
+ // via feature flags.
676
+ this.tombstoneMode =
677
+ !this.shouldRunSweep && this.mc.config.getBoolean(disableTombstoneKey) !== true;
678
+
679
+ // If GC ran in the container that generated the base snapshot, it will have a GC tree.
680
+ this.wasGCRunInLatestSummary = baseSnapshot?.trees[gcTreeKey] !== undefined;
681
+
682
+ // Get the GC data from the base snapshot. Use LazyPromise because we only want to do this once since it
683
+ // it involves fetching blobs from storage which is expensive.
684
+ this.baseSnapshotDataP = new LazyPromise<IGarbageCollectionSnapshotData | undefined>(
685
+ async () => {
686
+ if (baseSnapshot === undefined) {
687
+ return undefined;
688
+ }
689
+
690
+ try {
691
+ // For newer documents, GC data should be present in the GC tree in the root of the snapshot.
692
+ const gcSnapshotTree = baseSnapshot.trees[gcTreeKey];
693
+ if (gcSnapshotTree !== undefined) {
694
+ return getGCDataFromSnapshot(gcSnapshotTree, readAndParseBlob);
695
+ }
696
+
697
+ // back-compat - Older documents will have the GC blobs in each data store's summary tree. Get them and
698
+ // consolidate into IGarbageCollectionState format.
699
+ // Add a node for the root node that is not present in older snapshot format.
700
+ const gcState: IGarbageCollectionState = {
701
+ gcNodes: { "/": { outboundRoutes: [] } },
702
+ };
703
+ const dataStoreSnapshotTree = getSummaryForDatastores(baseSnapshot, metadata);
704
+ assert(
705
+ dataStoreSnapshotTree !== undefined,
706
+ 0x2a8 /* "Expected data store snapshot tree in base snapshot" */,
707
+ );
708
+ for (const [dsId, dsSnapshotTree] of Object.entries(
709
+ dataStoreSnapshotTree.trees,
710
+ )) {
711
+ const blobId = dsSnapshotTree.blobs[gcTreeKey];
712
+ if (blobId === undefined) {
713
+ continue;
714
+ }
715
+
716
+ const gcSummaryDetails =
717
+ await readAndParseBlob<IGarbageCollectionSummaryDetailsLegacy>(blobId);
718
+ // If there are no nodes for this data store, skip it.
719
+ if (gcSummaryDetails.gcData?.gcNodes === undefined) {
720
+ continue;
721
+ }
722
+
723
+ const dsRootId = `/${dsId}`;
724
+ // Since we used to write GC data at data store level, we won't have an entry for the root ("/").
725
+ // Construct that entry by adding root data store ids to its outbound routes.
726
+ const initialSnapshotDetails =
727
+ await readAndParseBlob<ReadFluidDataStoreAttributes>(
728
+ dsSnapshotTree.blobs[dataStoreAttributesBlobName],
729
+ );
730
+ if (initialSnapshotDetails.isRootDataStore) {
731
+ gcState.gcNodes["/"].outboundRoutes.push(dsRootId);
732
+ }
733
+
734
+ for (const [id, outboundRoutes] of Object.entries(
735
+ gcSummaryDetails.gcData.gcNodes,
736
+ )) {
737
+ // Prefix the data store id to the GC node ids to make them relative to the root from being
738
+ // relative to the data store. Similar to how its done in DataStore::getGCData.
739
+ const rootId = id === "/" ? dsRootId : `${dsRootId}${id}`;
740
+ gcState.gcNodes[rootId] = {
741
+ outboundRoutes: Array.from(outboundRoutes),
742
+ };
743
+ }
744
+ assert(
745
+ gcState.gcNodes[dsRootId] !== undefined,
746
+ 0x2a9 /* GC nodes for data store not in GC blob */,
747
+ );
748
+ gcState.gcNodes[dsRootId].unreferencedTimestampMs =
749
+ gcSummaryDetails.unrefTimestamp;
750
+ }
751
+ // If there is only one node (root node just added above), either GC is disabled or we are loading from
752
+ // the first summary generated by detached container. In both cases, GC was not run - return undefined.
753
+ return Object.keys(gcState.gcNodes).length === 1
754
+ ? undefined
755
+ : { gcState, tombstones: undefined, deletedNodes: undefined };
756
+ } catch (error) {
757
+ const dpe = DataProcessingError.wrapIfUnrecognized(
758
+ error,
759
+ "FailedToInitializeGC",
760
+ );
761
+ dpe.addTelemetryProperties({ gcConfigs: JSON.stringify(this.configs) });
762
+ throw dpe;
763
+ }
764
+ },
765
+ );
766
+
767
+ /**
768
+ * Set up the initializer which initializes the GC state from the data in base snapshot. This is done when
769
+ * connected in write mode or when GC runs the first time. It sets up all unreferenced nodes from the base
770
+ * GC state and updates their inactive or sweep ready state.
771
+ */
772
+ this.initializeGCStateFromBaseSnapshotP = new LazyPromise<void>(async () => {
773
+ /**
774
+ * If there is no current reference timestamp, skip initialization. We need the current timestamp to track
775
+ * how long objects have been unreferenced and if they can be deleted.
776
+ *
777
+ * Note that the only scenario where there is no reference timestamp is when no ops have ever been processed
778
+ * for this container and it is in read mode. In this scenario, there is no point in running GC anyway
779
+ * because references in the container do not change without any ops, i.e., there is nothing to collect.
780
+ */
781
+ const currentReferenceTimestampMs = this.runtime.getCurrentReferenceTimestampMs();
782
+ if (currentReferenceTimestampMs === undefined) {
783
+ // Log an event so we can evaluate how often we run into this scenario.
784
+ this.mc.logger.sendErrorEvent({
785
+ eventName: "GarbageCollectorInitializedWithoutTimestamp",
786
+ gcConfigs: JSON.stringify(this.configs),
787
+ });
788
+ return;
789
+ }
790
+ /**
791
+ * The base snapshot data will not be present if the container is loaded from:
792
+ * 1. The first summary created by the detached container.
793
+ * 2. A summary that was generated with GC disabled.
794
+ * 3. A summary that was generated before GC even existed.
795
+ */
796
+ const baseSnapshotData = await this.baseSnapshotDataP;
797
+ if (baseSnapshotData === undefined) {
798
+ return;
799
+ }
800
+ this.updateStateFromSnapshotData(baseSnapshotData, currentReferenceTimestampMs);
801
+ });
802
+
803
+ // Get the GC details from the GC state in the base summary. This is returned in getBaseGCDetails which is
804
+ // used to initialize the GC state of all the nodes in the container.
805
+ this.baseGCDetailsP = new LazyPromise<IGarbageCollectionDetailsBase>(async () => {
806
+ const baseSnapshotData = await this.baseSnapshotDataP;
807
+ if (baseSnapshotData === undefined) {
808
+ return {};
809
+ }
810
+
811
+ const gcNodes: { [id: string]: string[] } = {};
812
+ for (const [nodeId, nodeData] of Object.entries(baseSnapshotData.gcState.gcNodes)) {
813
+ gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);
814
+ }
815
+ // Run GC on the nodes in the base summary to get the routes used in each node in the container.
816
+ // This is an optimization for space (vs performance) wherein we don't need to store the used routes of
817
+ // each node in the summary.
818
+ const usedRoutes = runGarbageCollection(gcNodes, ["/"]).referencedNodeIds;
819
+
820
+ return { gcData: { gcNodes }, usedRoutes };
821
+ });
822
+
823
+ // Log all the GC options and the state determined by the garbage collector. This is interesting only for the
824
+ // summarizer client since it is the only one that runs GC. It also helps keep the telemetry less noisy.
825
+ if (this.isSummarizerClient) {
826
+ this.mc.logger.sendTelemetryEvent({
827
+ eventName: "GarbageCollectorLoaded",
828
+ gcConfigs: JSON.stringify(this.configs),
829
+ });
830
+ }
831
+ }
832
+
833
+ /**
834
+ * Called during container initialization. Initialize from the tombstone state in the base snapshot. This is done
835
+ * during initialization so that deleted or tombstoned objects are marked as such before they are loaded or used.
836
+ */
837
+ public async initializeBaseState(): Promise<void> {
838
+ const baseSnapshotData = await this.baseSnapshotDataP;
839
+ /**
840
+ * The base snapshot data will not be present if the container is loaded from:
841
+ * 1. The first summary created by the detached container.
842
+ * 2. A summary that was generated with GC disabled.
843
+ * 3. A summary that was generated before GC even existed.
844
+ */
845
+ if (baseSnapshotData === undefined) {
846
+ return;
847
+ }
848
+
849
+ // Initialize the deleted nodes from the snapshot. This is done irrespective of whether sweep is enabled or not
850
+ // to identify deleted nodes' usage.
851
+ if (baseSnapshotData.deletedNodes !== undefined) {
852
+ this.deletedNodes = new Set(baseSnapshotData.deletedNodes);
853
+ }
854
+
855
+ // If running in tombstone mode, initialize the tombstone state from the snapshot. Also, notify the runtime of
856
+ // tombstone routes.
857
+ if (this.tombstoneMode && baseSnapshotData.tombstones !== undefined) {
858
+ this.tombstones = Array.from(baseSnapshotData.tombstones);
859
+ this.runtime.updateTombstonedRoutes(this.tombstones);
860
+ }
861
+ }
862
+
863
+ /**
864
+ * Update state from the given snapshot data. This is done during load and during refreshing state from a snapshot.
865
+ * All current tracking is reset and updated from the data in the snapshot.
866
+ * @param snapshotData - The snapshot data to update state from. If this is undefined, all GC state and tracking
867
+ * is reset.
868
+ * @param currentReferenceTimestampMs - The current reference timestamp for marking unreferenced nodes' unreferenced
869
+ * timestamp.
870
+ */
871
+ private updateStateFromSnapshotData(
872
+ snapshotData: IGarbageCollectionSnapshotData | undefined,
873
+ currentReferenceTimestampMs: number,
874
+ ) {
875
+ /**
876
+ * Note: "newReferencesSinceLastRun" is not reset here. This is done because there may be references since the
877
+ * snapshot that we are updating state from. For example, this client may have processed ops till seq#1000 and
878
+ * its refreshing state from a summary that happened at seq#900. In this case, there may be references between
879
+ * seq#901 and seq#1000 that we don't want to reset.
880
+ * Unfortunately, there is no way to track the seq# of ops that add references, so we choose to not reset any
881
+ * references here. This should be fine because, in the worst case, we may end up updating the unreferenced
882
+ * timestamp of a node which will delay its deletion. Although not ideal, this will only happen in rare
883
+ * scenarios, so it should be okay.
884
+ */
885
+
886
+ // Clear all existing unreferenced state tracking.
887
+ for (const [, nodeStateTracker] of this.unreferencedNodesState) {
888
+ nodeStateTracker.stopTracking();
889
+ }
890
+ this.unreferencedNodesState.clear();
891
+
892
+ // If running sweep, the tombstone state represents the list of nodes that have been deleted during sweep.
893
+ // If running in tombstone mode, the tombstone state represents the list of nodes that have been marked as
894
+ // tombstones.
895
+ // If this call is because we are refreshing from a snapshot due to an ack, it is likely that the GC state
896
+ // in the snapshot is newer than this client's. And so, the deleted / tombstone nodes need to be updated.
897
+ if (this.shouldRunSweep) {
898
+ const snapshotDeletedNodes = snapshotData?.tombstones
899
+ ? new Set(snapshotData.tombstones)
900
+ : undefined;
901
+ // If the snapshot contains deleted nodes that are not yet deleted by this client, ask the runtime to
902
+ // delete them.
903
+ if (snapshotDeletedNodes !== undefined) {
904
+ const newDeletedNodes: string[] = [];
905
+ for (const nodeId of snapshotDeletedNodes) {
906
+ if (!this.deletedNodes.has(nodeId)) {
907
+ newDeletedNodes.push(nodeId);
908
+ }
909
+ }
910
+ if (newDeletedNodes.length > 0) {
911
+ // Call container runtime to delete these nodes and add deleted nodes to this.deletedNodes.
912
+ }
913
+ }
914
+ } else if (this.tombstoneMode) {
915
+ // The snapshot may contain more or fewer tombstone nodes than this client. Update tombstone state and
916
+ // notify the runtime to update its state as well.
917
+ this.tombstones = snapshotData?.tombstones ? Array.from(snapshotData.tombstones) : [];
918
+ this.runtime.updateTombstonedRoutes(this.tombstones);
919
+ }
920
+
921
+ // If there is no snapshot data, it means this snapshot was generated with GC disabled. Unset all GC state.
922
+ if (snapshotData === undefined) {
923
+ this.gcDataFromLastRun = undefined;
924
+ this.latestSummaryData = undefined;
925
+ return;
926
+ }
927
+
928
+ // Update unreferenced state tracking as per the GC state in the snapshot data and update gcDataFromLastRun
929
+ // to the GC data from the snapshot data.
930
+ const gcNodes: { [id: string]: string[] } = {};
931
+ for (const [nodeId, nodeData] of Object.entries(snapshotData.gcState.gcNodes)) {
932
+ if (nodeData.unreferencedTimestampMs !== undefined) {
933
+ this.unreferencedNodesState.set(
934
+ nodeId,
935
+ new UnreferencedStateTracker(
936
+ nodeData.unreferencedTimestampMs,
937
+ this.inactiveTimeoutMs,
938
+ currentReferenceTimestampMs,
939
+ this.sweepTimeoutMs,
940
+ ),
941
+ );
942
+ }
943
+ gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);
944
+ }
945
+ this.gcDataFromLastRun = { gcNodes };
946
+
947
+ // If tracking state across summaries, update latest summary data from the snapshot's GC data.
948
+ if (this.trackGCState) {
949
+ this.latestSummaryData = {
950
+ serializedGCState: JSON.stringify(generateSortedGCState(snapshotData.gcState)),
951
+ serializedTombstones: JSON.stringify(snapshotData.tombstones),
952
+ serializedDeletedNodes: JSON.stringify(snapshotData.deletedNodes),
953
+ };
954
+ }
955
+ }
956
+
957
+ /**
958
+ * Called when the connection state of the runtime changes, i.e., it connects or disconnects. GC subscribes to this
959
+ * to initialize the base state for non-summarizer clients so that they can track inactive / sweep ready nodes.
960
+ * @param connected - Whether the runtime connected / disconnected.
961
+ * @param clientId - The clientId of this runtime.
962
+ */
963
+ public setConnectionState(connected: boolean, clientId?: string | undefined): void {
964
+ /**
965
+ * For all clients, initialize the base state when the container becomes active, i.e., it transitions
966
+ * to "write" mode. This will ensure that the container's own join op is processed and there is a recent
967
+ * reference timestamp that will be used to update the state of unreferenced nodes. Also, all trailing ops which
968
+ * could affect the GC state will have been processed.
969
+ *
970
+ * If GC is up-to-date for the client and the summarizing client, there will be an doubling of both
971
+ * InactiveObject_Loaded and SweepReady_Loaded errors, as there will be one from the sending client and one from
972
+ * the receiving summarizer client.
973
+ *
974
+ * Ideally, this initialization should only be done for summarizer client. However, we are currently rolling out
975
+ * sweep in phases and we want to track when inactive and sweep ready objects are used in any client.
976
+ */
977
+ if (this.activeConnection() && this.shouldRunGC) {
978
+ this.initializeGCStateFromBaseSnapshotP.catch((error) => {});
979
+ }
980
+ }
981
+
982
+ /**
983
+ * Runs garbage collection and updates the reference / used state of the nodes in the container.
984
+ * @returns stats of the GC run or undefined if GC did not run.
985
+ */
986
+ public async collectGarbage(options: {
987
+ /** Logger to use for logging GC events */
988
+ logger?: ITelemetryLogger;
989
+ /** True to run GC sweep phase after the mark phase */
990
+ runSweep?: boolean;
991
+ /** True to generate full GC data */
992
+ fullGC?: boolean;
993
+ }): Promise<IGCStats | undefined> {
994
+ const fullGC =
995
+ options.fullGC ?? (this.gcOptions.runFullGC === true || this.summaryStateNeedsReset);
996
+ const logger = options.logger
997
+ ? ChildLogger.create(options.logger, undefined, {
998
+ all: { completedGCRuns: () => this.completedRuns },
999
+ })
1000
+ : this.mc.logger;
1001
+
1002
+ /**
1003
+ * If there is no current reference timestamp, skip running GC. We need the current timestamp to track
1004
+ * how long objects have been unreferenced and if they should be deleted.
1005
+ *
1006
+ * Note that the only scenario where GC is called and there is no reference timestamp is when no ops have ever
1007
+ * been processed for this container and it is in read mode. In this scenario, there is no point in running GC
1008
+ * anyway because references in the container do not change without any ops, i.e., there is nothing to collect.
1009
+ */
1010
+ const currentReferenceTimestampMs = this.runtime.getCurrentReferenceTimestampMs();
1011
+ if (currentReferenceTimestampMs === undefined) {
1012
+ // Log an event so we can evaluate how often we run into this scenario.
1013
+ logger.sendErrorEvent({
1014
+ eventName: "CollectGarbageCalledWithoutTimestamp",
1015
+ gcConfigs: JSON.stringify(this.configs),
1016
+ });
1017
+ return undefined;
1018
+ }
1019
+
1020
+ return PerformanceEvent.timedExecAsync(
1021
+ logger,
1022
+ { eventName: "GarbageCollection" },
1023
+ async (event) => {
1024
+ await this.runPreGCSteps();
1025
+
1026
+ // Get the runtime's GC data and run GC on the reference graph in it.
1027
+ const gcData = await this.runtime.getGCData(fullGC);
1028
+ const gcResult = runGarbageCollection(gcData.gcNodes, ["/"]);
1029
+
1030
+ const gcStats = await this.runPostGCSteps(
1031
+ gcData,
1032
+ gcResult,
1033
+ logger,
1034
+ currentReferenceTimestampMs,
1035
+ );
1036
+ event.end({ ...gcStats, timestamp: currentReferenceTimestampMs });
1037
+ this.completedRuns++;
1038
+ return gcStats;
1039
+ },
1040
+ { end: true, cancel: "error" },
1041
+ );
1042
+ }
1043
+
1044
+ private async runPreGCSteps() {
1045
+ // Ensure that state has been initialized from the base snapshot data.
1046
+ await this.initializeGCStateFromBaseSnapshotP;
1047
+ // Let the runtime update its pending state before GC runs.
1048
+ await this.runtime.updateStateBeforeGC();
1049
+ }
1050
+
1051
+ private async runPostGCSteps(
1052
+ gcData: IGarbageCollectionData,
1053
+ gcResult: IGCResult,
1054
+ logger: ITelemetryLogger,
1055
+ currentReferenceTimestampMs: number,
1056
+ ): Promise<IGCStats> {
1057
+ // Generate statistics from the current run. This is done before updating the current state because it
1058
+ // generates some of its data based on previous state of the system.
1059
+ const gcStats = this.generateStats(gcResult);
1060
+
1061
+ // Update the state since the last GC run. There can be nodes that were referenced between the last and
1062
+ // the current run. We need to identify than and update their unreferenced state if needed.
1063
+ this.updateStateSinceLastRun(gcData, logger);
1064
+
1065
+ // Update the current state and update the runtime of all routes or ids that used as per the GC run.
1066
+ this.updateCurrentState(gcData, gcResult, currentReferenceTimestampMs);
1067
+ this.runtime.updateUsedRoutes(gcResult.referencedNodeIds);
1068
+
1069
+ // Log events for objects that are ready to be deleted by sweep. When we have sweep enabled, we will
1070
+ // delete these objects here instead.
1071
+ this.logSweepEvents(logger, currentReferenceTimestampMs);
1072
+
1073
+ // If we are running in GC test mode, delete objects for unused routes. This enables testing scenarios
1074
+ // involving access to deleted data.
1075
+ if (this.testMode) {
1076
+ this.runtime.updateUnusedRoutes(gcResult.deletedNodeIds);
1077
+ } else if (this.tombstoneMode) {
1078
+ // If we are running in GC tombstone mode, update tombstoned routes. This enables testing scenarios
1079
+ // involving access to "deleted" data without actually deleting the data from summaries.
1080
+ // Note: we will not tombstone in test mode.
1081
+ this.runtime.updateTombstonedRoutes(this.tombstones);
1082
+ }
1083
+
1084
+ // Log pending unreferenced events such as a node being used after inactive. This is done after GC runs and
1085
+ // updates its state so that we don't send false positives based on intermediate state. For example, we may get
1086
+ // reference to an unreferenced node from another unreferenced node which means the node wasn't revived.
1087
+ await this.logUnreferencedEvents(logger);
1088
+
1089
+ return gcStats;
1090
+ }
1091
+
1092
+ /**
1093
+ * Summarizes the GC data and returns it as a summary tree.
1094
+ * We current write the entire GC state in a single blob. This can be modified later to write multiple
1095
+ * blobs. All the blob keys should start with `gcBlobPrefix`.
1096
+ */
1097
+ public summarize(
1098
+ fullTree: boolean,
1099
+ trackState: boolean,
1100
+ telemetryContext?: ITelemetryContext,
1101
+ ): ISummarizeResult | undefined {
1102
+ if (!this.shouldRunGC || this.gcDataFromLastRun === undefined) {
1103
+ return;
1104
+ }
1105
+
1106
+ const gcState: IGarbageCollectionState = { gcNodes: {} };
1107
+ for (const [nodeId, outboundRoutes] of Object.entries(this.gcDataFromLastRun.gcNodes)) {
1108
+ gcState.gcNodes[nodeId] = {
1109
+ outboundRoutes,
1110
+ unreferencedTimestampMs:
1111
+ this.unreferencedNodesState.get(nodeId)?.unreferencedTimestampMs,
1112
+ };
1113
+ }
1114
+
1115
+ const serializedGCState = JSON.stringify(generateSortedGCState(gcState));
1116
+ // Serialize and write deleted nodes, if any. This is done irrespective of whether sweep is enabled or not so
1117
+ // to identify deleted nodes' usage.
1118
+ const serializedDeletedNodes =
1119
+ this.deletedNodes.size > 0
1120
+ ? JSON.stringify(Array.from(this.deletedNodes).sort())
1121
+ : undefined;
1122
+ // If running in tombstone mode, serialize and write tombstones, if any.
1123
+ const serializedTombstones = this.tombstoneMode
1124
+ ? this.tombstones.length > 0
1125
+ ? JSON.stringify(this.tombstones.sort())
1126
+ : undefined
1127
+ : undefined;
1128
+
1129
+ /**
1130
+ * Incremental summary of GC data - If none of GC state, deleted nodes or tombstones changed since last summary,
1131
+ * write summary handle instead of summary tree for GC.
1132
+ * Otherwise, write the GC summary tree. In the tree, for each of these that changed, write a summary blob and
1133
+ * for each of these that did not change, write a summary handle.
1134
+ */
1135
+ if (this.trackGCState) {
1136
+ this.pendingSummaryData = {
1137
+ serializedGCState,
1138
+ serializedTombstones,
1139
+ serializedDeletedNodes,
1140
+ };
1141
+ if (trackState && !fullTree && this.latestSummaryData !== undefined) {
1142
+ // If nothing changed since last summary, send a summary handle for the entire GC data.
1143
+ if (
1144
+ this.latestSummaryData.serializedGCState === serializedGCState &&
1145
+ this.latestSummaryData.serializedTombstones === serializedTombstones
1146
+ ) {
1147
+ const stats = mergeStats();
1148
+ stats.handleNodeCount++;
1149
+ return {
1150
+ summary: {
1151
+ type: SummaryType.Handle,
1152
+ handle: `/${gcTreeKey}`,
1153
+ handleType: SummaryType.Tree,
1154
+ },
1155
+ stats,
1156
+ };
1157
+ }
1158
+
1159
+ // If some state changed, build a GC summary tree.
1160
+ return this.buildGCSummaryTree(
1161
+ serializedGCState,
1162
+ serializedTombstones,
1163
+ serializedDeletedNodes,
1164
+ true /* trackState */,
1165
+ );
1166
+ }
1167
+ }
1168
+ // If not tracking GC state, build a GC summary tree without any summary handles.
1169
+ return this.buildGCSummaryTree(
1170
+ serializedGCState,
1171
+ serializedTombstones,
1172
+ serializedDeletedNodes,
1173
+ false /* trackState */,
1174
+ );
1175
+ }
1176
+
1177
+ /**
1178
+ * Builds the GC summary tree which contains GC state, deleted nodes and tombstones.
1179
+ * If trackState is false, all of GC state, deleted nodes and tombstones are written as summary blobs.
1180
+ * If trackState is true, only states that changed are written. Rest are written as handles.
1181
+ * @param serializedGCState - The GC state serialized as string.
1182
+ * @param serializedTombstones - The tombstone state serialized as string.
1183
+ * @param serializedDeletedNodes - Deleted nodes serialized as string.
1184
+ * @param trackState - Whether we are tracking GC state across summaries.
1185
+ * @returns the GC summary tree.
1186
+ */
1187
+ private buildGCSummaryTree(
1188
+ serializedGCState: string,
1189
+ serializedTombstones: string | undefined,
1190
+ serializedDeletedNodes: string | undefined,
1191
+ trackState: boolean,
1192
+ ): ISummaryTreeWithStats {
1193
+ const gcStateBlobKey = `${gcBlobPrefix}_root`;
1194
+ const builder = new SummaryTreeBuilder();
1195
+
1196
+ // If the GC state hasn't changed, write a summary handle, else write a summary blob for it.
1197
+ if (this.latestSummaryData?.serializedGCState === serializedGCState && trackState) {
1198
+ builder.addHandle(gcStateBlobKey, SummaryType.Blob, `/${gcTreeKey}/${gcStateBlobKey}`);
1199
+ } else {
1200
+ builder.addBlob(gcStateBlobKey, serializedGCState);
1201
+ }
1202
+
1203
+ // If tombstones exist, write a summary handle if it hasn't changed. If it has changed, write a
1204
+ // summary blob.
1205
+ if (serializedTombstones !== undefined) {
1206
+ if (
1207
+ this.latestSummaryData?.serializedTombstones === serializedTombstones &&
1208
+ trackState
1209
+ ) {
1210
+ builder.addHandle(
1211
+ gcTombstoneBlobKey,
1212
+ SummaryType.Blob,
1213
+ `/${gcTreeKey}/${gcTombstoneBlobKey}`,
1214
+ );
1215
+ } else {
1216
+ builder.addBlob(gcTombstoneBlobKey, serializedTombstones);
1217
+ }
1218
+ }
1219
+
1220
+ // If there are no deleted nodes, return the summary tree.
1221
+ if (serializedDeletedNodes === undefined) {
1222
+ return builder.getSummaryTree();
1223
+ }
1224
+
1225
+ // If the deleted nodes hasn't changed, write a summary handle, else write a summary blob for it.
1226
+ if (
1227
+ this.latestSummaryData?.serializedDeletedNodes === serializedDeletedNodes &&
1228
+ trackState
1229
+ ) {
1230
+ builder.addHandle(
1231
+ gcDeletedBlobKey,
1232
+ SummaryType.Blob,
1233
+ `/${gcTreeKey}/${gcDeletedBlobKey}`,
1234
+ );
1235
+ } else {
1236
+ builder.addBlob(gcDeletedBlobKey, serializedDeletedNodes);
1237
+ }
1238
+ return builder.getSummaryTree();
1239
+ }
1240
+
1241
+ public getMetadata(): IGCMetadata {
1242
+ return {
1243
+ /**
1244
+ * If GC is enabled, the GC data is written using the current GC version and that is the gcFeature that goes
1245
+ * into the metadata blob. If GC is disabled, the gcFeature is 0.
1246
+ */
1247
+ gcFeature: this.gcEnabled ? this.currentGCVersion : 0,
1248
+ sessionExpiryTimeoutMs: this.sessionExpiryTimeoutMs,
1249
+ sweepEnabled: this.sweepEnabled,
1250
+ sweepTimeoutMs: this.sweepTimeoutMs,
1251
+ };
1252
+ }
1253
+
1254
+ /**
1255
+ * Returns a the GC details generated from the base summary. This is used to initialize the GC state of the nodes
1256
+ * in the container.
1257
+ */
1258
+ public async getBaseGCDetails(): Promise<IGarbageCollectionDetailsBase> {
1259
+ return this.baseGCDetailsP;
1260
+ }
1261
+
1262
+ /**
1263
+ * Called to refresh the latest summary state. This happens when either a pending summary is acked or a snapshot
1264
+ * is downloaded and should be used to update the state.
1265
+ */
1266
+ public async refreshLatestSummary(
1267
+ result: RefreshSummaryResult,
1268
+ proposalHandle: string | undefined,
1269
+ summaryRefSeq: number,
1270
+ readAndParseBlob: ReadAndParseBlob,
1271
+ ): Promise<void> {
1272
+ // If the latest summary was updated and the summary was tracked, this client is the one that generated this
1273
+ // summary. So, update wasGCRunInLatestSummary.
1274
+ // Note that this has to be updated if GC did not run too. Otherwise, `gcStateNeedsReset` will always return
1275
+ // true in scenarios where GC is disabled but enabled in the snapshot we loaded from.
1276
+ if (result.latestSummaryUpdated && result.wasSummaryTracked) {
1277
+ this.wasGCRunInLatestSummary = this.shouldRunGC;
1278
+ }
1279
+
1280
+ if (!result.latestSummaryUpdated || !this.shouldRunGC) {
1281
+ return;
1282
+ }
1283
+
1284
+ // If the summary was tracked by this client, it was the one that generated the summary in the first place.
1285
+ // Update latest state from pending.
1286
+ if (result.wasSummaryTracked) {
1287
+ this.latestSummaryGCVersion = this.currentGCVersion;
1288
+ if (this.trackGCState) {
1289
+ this.latestSummaryData = this.pendingSummaryData;
1290
+ this.pendingSummaryData = undefined;
1291
+ }
1292
+ return;
1293
+ }
1294
+
1295
+ // If the summary was not tracked by this client, the state should be updated from the downloaded snapshot.
1296
+ const snapshot = result.snapshot;
1297
+ const metadataBlobId = snapshot.blobs[metadataBlobName];
1298
+ if (metadataBlobId) {
1299
+ const metadata = await readAndParseBlob<IContainerRuntimeMetadata>(metadataBlobId);
1300
+ this.latestSummaryGCVersion = getGCVersion(metadata);
1301
+ }
1302
+
1303
+ // The current reference timestamp should be available if we are refreshing state from a snapshot. There has
1304
+ // to be at least one op (summary op / ack, if nothing else) if a snapshot was taken.
1305
+ const currentReferenceTimestampMs = this.runtime.getCurrentReferenceTimestampMs();
1306
+ if (currentReferenceTimestampMs === undefined) {
1307
+ throw DataProcessingError.create(
1308
+ "No reference timestamp when updating GC state from snapshot",
1309
+ "refreshLatestSummary",
1310
+ undefined,
1311
+ { proposalHandle, summaryRefSeq, details: JSON.stringify(this.configs) },
1312
+ );
1313
+ }
1314
+ const gcSnapshotTree = snapshot.trees[gcTreeKey];
1315
+ // If GC ran in the container that generated this snapshot, it will have a GC tree.
1316
+ this.wasGCRunInLatestSummary = gcSnapshotTree !== undefined;
1317
+ let latestGCData: IGarbageCollectionSnapshotData | undefined;
1318
+ if (gcSnapshotTree !== undefined) {
1319
+ latestGCData = await getGCDataFromSnapshot(gcSnapshotTree, readAndParseBlob);
1320
+ }
1321
+ this.updateStateFromSnapshotData(latestGCData, currentReferenceTimestampMs);
1322
+ this.pendingSummaryData = undefined;
1323
+ }
1324
+
1325
+ /**
1326
+ * Called when a node with the given id is updated. If the node is inactive, log an error.
1327
+ * @param nodePath - The id of the node that changed.
1328
+ * @param reason - Whether the node was loaded or changed.
1329
+ * @param timestampMs - The timestamp when the node changed.
1330
+ * @param packagePath - The package path of the node. This may not be available if the node hasn't been loaded yet.
1331
+ * @param requestHeaders - If the node was loaded via request path, the headers in the request.
1332
+ */
1333
+ public nodeUpdated(
1334
+ nodePath: string,
1335
+ reason: "Loaded" | "Changed",
1336
+ timestampMs?: number,
1337
+ packagePath?: readonly string[],
1338
+ requestHeaders?: IRequestHeader,
1339
+ ) {
1340
+ if (!this.shouldRunGC) {
1341
+ return;
1342
+ }
1343
+
1344
+ const nodeStateTracker = this.unreferencedNodesState.get(nodePath);
1345
+ if (nodeStateTracker && nodeStateTracker.state !== UnreferencedState.Active) {
1346
+ this.inactiveNodeUsed(
1347
+ reason,
1348
+ nodePath,
1349
+ nodeStateTracker,
1350
+ undefined /* fromNodeId */,
1351
+ packagePath,
1352
+ timestampMs,
1353
+ requestHeaders,
1354
+ );
1355
+ }
1356
+ }
1357
+
1358
+ /**
1359
+ * Called when an outbound reference is added to a node. This is used to identify all nodes that have been
1360
+ * referenced between summaries so that their unreferenced timestamp can be reset.
1361
+ *
1362
+ * @param fromNodePath - The node from which the reference is added.
1363
+ * @param toNodePath - The node to which the reference is added.
1364
+ */
1365
+ public addedOutboundReference(fromNodePath: string, toNodePath: string) {
1366
+ if (!this.shouldRunGC) {
1367
+ return;
1368
+ }
1369
+
1370
+ const outboundRoutes = this.newReferencesSinceLastRun.get(fromNodePath) ?? [];
1371
+ outboundRoutes.push(toNodePath);
1372
+ this.newReferencesSinceLastRun.set(fromNodePath, outboundRoutes);
1373
+
1374
+ const nodeStateTracker = this.unreferencedNodesState.get(toNodePath);
1375
+ if (nodeStateTracker && nodeStateTracker.state !== UnreferencedState.Active) {
1376
+ this.inactiveNodeUsed("Revived", toNodePath, nodeStateTracker, fromNodePath);
1377
+ }
1378
+
1379
+ if (this.tombstones.includes(toNodePath)) {
1380
+ const nodeType = this.runtime.getNodeType(toNodePath);
1381
+
1382
+ let eventName = "GC_Tombstone_SubDatastore_Revived";
1383
+ if (nodeType === GCNodeType.DataStore) {
1384
+ eventName = "GC_Tombstone_Datastore_Revived";
1385
+ } else if (nodeType === GCNodeType.Blob) {
1386
+ eventName = "GC_Tombstone_Blob_Revived";
1387
+ }
1388
+
1389
+ sendGCUnexpectedUsageEvent(
1390
+ this.mc,
1391
+ {
1392
+ eventName,
1393
+ category: "generic",
1394
+ isSummarizerClient: this.isSummarizerClient,
1395
+ url: trimLeadingSlashes(toNodePath),
1396
+ nodeType,
1397
+ },
1398
+ undefined /* packagePath */,
1399
+ );
1400
+ }
1401
+ }
1402
+
1403
+ /**
1404
+ * Returns whether a node with the given path has been deleted or not. This can be used by the runtime to identify
1405
+ * cases where objects are used after they are deleted and throw / log errors accordingly.
1406
+ */
1407
+ public isNodeDeleted(nodePath: string): boolean {
1408
+ return this.deletedNodes.has(nodePath);
1409
+ }
1410
+
1411
+ public dispose(): void {
1412
+ this.sessionExpiryTimer?.clear();
1413
+ this.sessionExpiryTimer = undefined;
1414
+ }
1415
+
1416
+ /**
1417
+ * Updates the state of the system as per the current GC run. It does the following:
1418
+ * 1. Sets up the current GC state as per the gcData.
1419
+ * 2. Starts tracking for nodes that have become unreferenced in this run.
1420
+ * 3. Clears tracking for nodes that were unreferenced but became referenced in this run.
1421
+ * @param gcData - The data representing the reference graph on which GC is run.
1422
+ * @param gcResult - The result of the GC run on the gcData.
1423
+ * @param currentReferenceTimestampMs - The timestamp to be used for unreferenced nodes' timestamp.
1424
+ */
1425
+ private updateCurrentState(
1426
+ gcData: IGarbageCollectionData,
1427
+ gcResult: IGCResult,
1428
+ currentReferenceTimestampMs: number,
1429
+ ) {
1430
+ this.gcDataFromLastRun = cloneGCData(gcData);
1431
+ this.tombstones = [];
1432
+ this.newReferencesSinceLastRun.clear();
1433
+
1434
+ // Iterate through the referenced nodes and stop tracking if they were unreferenced before.
1435
+ for (const nodeId of gcResult.referencedNodeIds) {
1436
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
1437
+ if (nodeStateTracker !== undefined) {
1438
+ // Stop tracking so as to clear out any running timers.
1439
+ nodeStateTracker.stopTracking();
1440
+ // Delete the node as we don't need to track it any more.
1441
+ this.unreferencedNodesState.delete(nodeId);
1442
+ }
1443
+ }
1444
+
1445
+ /**
1446
+ * If a node became unreferenced in this run, start tracking it.
1447
+ * If a node was already unreferenced, update its tracking information. Since the current reference time is
1448
+ * from the ops seen, this will ensure that we keep updating the unreferenced state as time moves forward.
1449
+ */
1450
+ for (const nodeId of gcResult.deletedNodeIds) {
1451
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
1452
+ if (nodeStateTracker === undefined) {
1453
+ this.unreferencedNodesState.set(
1454
+ nodeId,
1455
+ new UnreferencedStateTracker(
1456
+ currentReferenceTimestampMs,
1457
+ this.inactiveTimeoutMs,
1458
+ currentReferenceTimestampMs,
1459
+ this.sweepTimeoutMs,
1460
+ ),
1461
+ );
1462
+ } else {
1463
+ nodeStateTracker.updateTracking(currentReferenceTimestampMs);
1464
+ if (this.tombstoneMode && nodeStateTracker.state === UnreferencedState.SweepReady) {
1465
+ const nodeType = this.runtime.getNodeType(nodeId);
1466
+ if (nodeType === GCNodeType.DataStore || nodeType === GCNodeType.Blob) {
1467
+ this.tombstones.push(nodeId);
1468
+ }
1469
+ }
1470
+ }
1471
+ }
1472
+ }
1473
+
1474
+ /**
1475
+ * Since GC runs periodically, the GC data that is generated only tells us the state of the world at that point in
1476
+ * time. There can be nodes that were referenced in between two runs and their unreferenced state needs to be
1477
+ * updated. For example, in the following scenarios not updating the unreferenced timestamp can lead to deletion of
1478
+ * these objects while there can be in-memory referenced to it:
1479
+ * 1. A node transitions from `unreferenced -> referenced -> unreferenced` between two runs. When the reference is
1480
+ * added, the object may have been accessed and in-memory reference to it added.
1481
+ * 2. A reference is added from one unreferenced node to one or more unreferenced nodes. Even though the node[s] were
1482
+ * unreferenced, they could have been accessed and in-memory reference to them added.
1483
+ *
1484
+ * This function identifies nodes that were referenced since last run and removes their unreferenced state, if any.
1485
+ * If these nodes are currently unreferenced, they will be assigned new unreferenced state by the current run.
1486
+ */
1487
+ private updateStateSinceLastRun(
1488
+ currentGCData: IGarbageCollectionData,
1489
+ logger: ITelemetryLogger,
1490
+ ) {
1491
+ // If we haven't run GC before there is nothing to do.
1492
+ if (this.gcDataFromLastRun === undefined) {
1493
+ return;
1494
+ }
1495
+
1496
+ // Find any references that haven't been identified correctly.
1497
+ const missingExplicitReferences = this.findMissingExplicitReferences(
1498
+ currentGCData,
1499
+ this.gcDataFromLastRun,
1500
+ this.newReferencesSinceLastRun,
1501
+ );
1502
+
1503
+ if (missingExplicitReferences.length > 0) {
1504
+ missingExplicitReferences.forEach((missingExplicitReference) => {
1505
+ logger.sendErrorEvent({
1506
+ eventName: "gcUnknownOutboundReferences",
1507
+ gcNodeId: missingExplicitReference[0],
1508
+ gcRoutes: JSON.stringify(missingExplicitReference[1]),
1509
+ });
1510
+ });
1511
+ }
1512
+
1513
+ // No references were added since the last run so we don't have to update reference states of any unreferenced
1514
+ // nodes
1515
+ if (this.newReferencesSinceLastRun.size === 0) {
1516
+ return;
1517
+ }
1518
+
1519
+ /**
1520
+ * Generate a super set of the GC data that contains the nodes and edges from last run, plus any new node and
1521
+ * edges that have been added since then. To do this, combine the GC data from the last run and the current
1522
+ * run, and then add the references since last run.
1523
+ *
1524
+ * Note on why we need to combine the data from previous run, current run and all references in between -
1525
+ * 1. We need data from last run because some of its references may have been deleted since then. If those
1526
+ * references added new outbound references before they were deleted, we need to detect them.
1527
+ *
1528
+ * 2. We need new outbound references since last run because some of them may have been deleted later. If those
1529
+ * references added new outbound references before they were deleted, we need to detect them.
1530
+ *
1531
+ * 3. We need data from the current run because currently we may not detect when DDSes are referenced:
1532
+ * - We don't require DDSes handles to be stored in a referenced DDS.
1533
+ * - A new data store may have "root" DDSes already created and we don't detect them today.
1534
+ */
1535
+ const gcDataSuperSet = concatGarbageCollectionData(this.gcDataFromLastRun, currentGCData);
1536
+ const newOutboundRoutesSinceLastRun: string[] = [];
1537
+ this.newReferencesSinceLastRun.forEach((outboundRoutes: string[], sourceNodeId: string) => {
1538
+ if (gcDataSuperSet.gcNodes[sourceNodeId] === undefined) {
1539
+ gcDataSuperSet.gcNodes[sourceNodeId] = outboundRoutes;
1540
+ } else {
1541
+ gcDataSuperSet.gcNodes[sourceNodeId].push(...outboundRoutes);
1542
+ }
1543
+ newOutboundRoutesSinceLastRun.push(...outboundRoutes);
1544
+ });
1545
+
1546
+ /**
1547
+ * Run GC on the above reference graph starting with root and all new outbound routes. This will generate a
1548
+ * list of all nodes that could have been referenced since the last run. If any of these nodes are unreferenced,
1549
+ * unreferenced, stop tracking them and remove from unreferenced list.
1550
+ * Note that some of these nodes may be unreferenced now and if so, the current run will mark them as
1551
+ * unreferenced and add unreferenced state.
1552
+ */
1553
+ const gcResult = runGarbageCollection(gcDataSuperSet.gcNodes, [
1554
+ "/",
1555
+ ...newOutboundRoutesSinceLastRun,
1556
+ ]);
1557
+ for (const nodeId of gcResult.referencedNodeIds) {
1558
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
1559
+ if (nodeStateTracker !== undefined) {
1560
+ // Stop tracking so as to clear out any running timers.
1561
+ nodeStateTracker.stopTracking();
1562
+ // Delete the unreferenced state as we don't need to track it any more.
1563
+ this.unreferencedNodesState.delete(nodeId);
1564
+ }
1565
+ }
1566
+ }
1567
+
1568
+ /**
1569
+ * Finds all new references or outbound routes in the current graph that haven't been explicitly notified to GC.
1570
+ * The principle is that every new reference or outbound route must be notified to GC via the
1571
+ * addedOutboundReference method. It it hasn't, its a bug and we want to identify these scenarios.
1572
+ *
1573
+ * In more simple terms:
1574
+ * Missing Explicit References = Current References - Previous References - Explicitly Added References;
1575
+ *
1576
+ * @param currentGCData - The GC data (reference graph) from the current GC run.
1577
+ * @param previousGCData - The GC data (reference graph) from the previous GC run.
1578
+ * @param explicitReferences - New references added explicity between the previous and the current run.
1579
+ * @returns - a list of missing explicit references
1580
+ */
1581
+ private findMissingExplicitReferences(
1582
+ currentGCData: IGarbageCollectionData,
1583
+ previousGCData: IGarbageCollectionData,
1584
+ explicitReferences: Map<string, string[]>,
1585
+ ): [string, string[]][] {
1586
+ assert(
1587
+ previousGCData !== undefined,
1588
+ 0x2b7 /* "Can't validate correctness without GC data from last run" */,
1589
+ );
1590
+
1591
+ const currentGraph = Object.entries(currentGCData.gcNodes);
1592
+ const missingExplicitReferences: [string, string[]][] = [];
1593
+ currentGraph.forEach(([nodeId, currentOutboundRoutes]) => {
1594
+ const previousRoutes = previousGCData.gcNodes[nodeId] ?? [];
1595
+ const explicitRoutes = explicitReferences.get(nodeId) ?? [];
1596
+ const missingExplicitRoutes: string[] = [];
1597
+
1598
+ /**
1599
+ * 1. For routes in the current GC data, routes that were not present in previous GC data and did not have
1600
+ * explicit references should be added to missing explicit routes list.
1601
+ * 2. Only include data store and blob routes since GC only works for these two.
1602
+ * Note: Due to a bug with de-duped blobs, only adding data store routes for now.
1603
+ * 3. Ignore DDS routes to their parent datastores since those were added implicitly. So, there won't be
1604
+ * explicit routes to them.
1605
+ */
1606
+ currentOutboundRoutes.forEach((route) => {
1607
+ const nodeType = this.runtime.getNodeType(route);
1608
+ if (
1609
+ (nodeType === GCNodeType.DataStore || nodeType === GCNodeType.Blob) &&
1610
+ !nodeId.startsWith(route) &&
1611
+ !previousRoutes.includes(route) &&
1612
+ !explicitRoutes.includes(route)
1613
+ ) {
1614
+ missingExplicitRoutes.push(route);
1615
+ }
1616
+ });
1617
+ if (missingExplicitRoutes.length > 0) {
1618
+ missingExplicitReferences.push([nodeId, missingExplicitRoutes]);
1619
+ }
1620
+ });
1621
+
1622
+ // Ideally missingExplicitReferences should always have a size 0
1623
+ return missingExplicitReferences;
1624
+ }
1625
+
1626
+ /**
1627
+ * Generates the stats of a garbage collection run from the given results of the run.
1628
+ * @param gcResult - The result of a GC run.
1629
+ * @returns the GC stats of the GC run.
1630
+ */
1631
+ private generateStats(gcResult: IGCResult): IGCStats {
1632
+ const gcStats: IGCStats = {
1633
+ nodeCount: 0,
1634
+ dataStoreCount: 0,
1635
+ attachmentBlobCount: 0,
1636
+ unrefNodeCount: 0,
1637
+ unrefDataStoreCount: 0,
1638
+ unrefAttachmentBlobCount: 0,
1639
+ updatedNodeCount: 0,
1640
+ updatedDataStoreCount: 0,
1641
+ updatedAttachmentBlobCount: 0,
1642
+ };
1643
+
1644
+ const updateNodeStats = (nodeId: string, referenced: boolean) => {
1645
+ gcStats.nodeCount++;
1646
+ // If there is no previous GC data, every node's state is generated and is considered as updated.
1647
+ // Otherwise, find out if any node went from referenced to unreferenced or vice-versa.
1648
+ const stateUpdated =
1649
+ this.gcDataFromLastRun === undefined ||
1650
+ this.unreferencedNodesState.has(nodeId) === referenced;
1651
+ if (stateUpdated) {
1652
+ gcStats.updatedNodeCount++;
1653
+ }
1654
+ if (!referenced) {
1655
+ gcStats.unrefNodeCount++;
1656
+ }
1657
+
1658
+ if (this.runtime.getNodeType(nodeId) === GCNodeType.DataStore) {
1659
+ gcStats.dataStoreCount++;
1660
+ if (stateUpdated) {
1661
+ gcStats.updatedDataStoreCount++;
1662
+ }
1663
+ if (!referenced) {
1664
+ gcStats.unrefDataStoreCount++;
1665
+ }
1666
+ }
1667
+ if (this.runtime.getNodeType(nodeId) === GCNodeType.Blob) {
1668
+ gcStats.attachmentBlobCount++;
1669
+ if (stateUpdated) {
1670
+ gcStats.updatedAttachmentBlobCount++;
1671
+ }
1672
+ if (!referenced) {
1673
+ gcStats.unrefAttachmentBlobCount++;
1674
+ }
1675
+ }
1676
+ };
1677
+
1678
+ for (const nodeId of gcResult.referencedNodeIds) {
1679
+ updateNodeStats(nodeId, true /* referenced */);
1680
+ }
1681
+
1682
+ for (const nodeId of gcResult.deletedNodeIds) {
1683
+ updateNodeStats(nodeId, false /* referenced */);
1684
+ }
1685
+
1686
+ return gcStats;
1687
+ }
1688
+
1689
+ /**
1690
+ * For nodes that are ready to sweep, log an event for now. Until we start running sweep which deletes objects,
1691
+ * this will give us a view into how much deleted content a container has.
1692
+ */
1693
+ private logSweepEvents(logger: ITelemetryLogger, currentReferenceTimestampMs: number) {
1694
+ if (
1695
+ this.mc.config.getBoolean(disableSweepLogKey) === true ||
1696
+ this.sweepTimeoutMs === undefined
1697
+ ) {
1698
+ return;
1699
+ }
1700
+
1701
+ this.unreferencedNodesState.forEach((nodeStateTracker, nodeId) => {
1702
+ if (nodeStateTracker.state !== UnreferencedState.SweepReady) {
1703
+ return;
1704
+ }
1705
+
1706
+ const nodeType = this.runtime.getNodeType(nodeId);
1707
+ if (nodeType !== GCNodeType.DataStore && nodeType !== GCNodeType.Blob) {
1708
+ return;
1709
+ }
1710
+
1711
+ // Log deleted event for each node only once to reduce noise in telemetry.
1712
+ const uniqueEventId = `Deleted-${nodeId}`;
1713
+ if (this.loggedUnreferencedEvents.has(uniqueEventId)) {
1714
+ return;
1715
+ }
1716
+ this.loggedUnreferencedEvents.add(uniqueEventId);
1717
+ logger.sendTelemetryEvent({
1718
+ eventName: "GCObjectDeleted",
1719
+ id: nodeId,
1720
+ type: nodeType,
1721
+ age: currentReferenceTimestampMs - nodeStateTracker.unreferencedTimestampMs,
1722
+ timeout: this.sweepTimeoutMs,
1723
+ completedGCRuns: this.completedRuns,
1724
+ lastSummaryTime: this.getLastSummaryTimestampMs(),
1725
+ });
1726
+ });
1727
+ }
1728
+
1729
+ /**
1730
+ * Called when an inactive node is used after. Queue up an event that will be logged next time GC runs.
1731
+ */
1732
+ private inactiveNodeUsed(
1733
+ usageType: "Changed" | "Loaded" | "Revived",
1734
+ nodeId: string,
1735
+ nodeStateTracker: UnreferencedStateTracker,
1736
+ fromNodeId?: string,
1737
+ packagePath?: readonly string[],
1738
+ currentReferenceTimestampMs = this.runtime.getCurrentReferenceTimestampMs(),
1739
+ requestHeaders?: IRequestHeader,
1740
+ ) {
1741
+ // If there is no reference timestamp to work with, no ops have been processed after creation. If so, skip
1742
+ // logging as nothing interesting would have happened worth logging.
1743
+ // If the node is active, skip logging.
1744
+ if (
1745
+ currentReferenceTimestampMs === undefined ||
1746
+ nodeStateTracker.state === UnreferencedState.Active
1747
+ ) {
1748
+ return;
1749
+ }
1750
+
1751
+ // We only care about data stores and attachment blobs for this telemetry since GC only marks these objects
1752
+ // as unreferenced. Also, if an inactive DDS is used, the corresponding data store store will also be used.
1753
+ const nodeType = this.runtime.getNodeType(nodeId);
1754
+ if (nodeType !== GCNodeType.DataStore && nodeType !== GCNodeType.Blob) {
1755
+ return;
1756
+ }
1757
+
1758
+ const state = nodeStateTracker.state;
1759
+ const uniqueEventId = `${state}-${nodeId}-${usageType}`;
1760
+ if (this.loggedUnreferencedEvents.has(uniqueEventId)) {
1761
+ return;
1762
+ }
1763
+ this.loggedUnreferencedEvents.add(uniqueEventId);
1764
+
1765
+ const propsToLog = {
1766
+ id: nodeId,
1767
+ type: nodeType,
1768
+ unrefTime: nodeStateTracker.unreferencedTimestampMs,
1769
+ age: currentReferenceTimestampMs - nodeStateTracker.unreferencedTimestampMs,
1770
+ timeout:
1771
+ nodeStateTracker.state === UnreferencedState.Inactive
1772
+ ? this.inactiveTimeoutMs
1773
+ : this.sweepTimeoutMs,
1774
+ completedGCRuns: this.completedRuns,
1775
+ lastSummaryTime: this.getLastSummaryTimestampMs(),
1776
+ ...this.createContainerMetadata,
1777
+ externalRequest: requestHeaders?.[RuntimeHeaders.externalRequest],
1778
+ viaHandle: requestHeaders?.[RuntimeHeaders.viaHandle],
1779
+ fromId: fromNodeId,
1780
+ };
1781
+
1782
+ // For summarizer client, queue the event so it is logged the next time GC runs if the event is still valid.
1783
+ // For non-summarizer client, log the event now since GC won't run on it. This may result in false positives
1784
+ // but it's a good signal nonetheless and we can consume it with a grain of salt.
1785
+ // Inactive errors are usages of Objects that are unreferenced for at least a period of 7 days.
1786
+ // SweepReady errors are usages of Objects that will be deleted by GC Sweep!
1787
+ if (this.isSummarizerClient) {
1788
+ this.pendingEventsQueue.push({ ...propsToLog, usageType, state });
1789
+ } else {
1790
+ // For non-summarizer clients, only log "Loaded" type events since these objects may not be loaded in the
1791
+ // summarizer clients if they are based off of user actions (such as scrolling to content for these objects)
1792
+ // Events generated:
1793
+ // InactiveObject_Loaded, SweepReadyObject_Loaded
1794
+ if (usageType === "Loaded") {
1795
+ const event = {
1796
+ ...propsToLog,
1797
+ eventName: `${state}Object_${usageType}`,
1798
+ pkg: packagePathToTelemetryProperty(packagePath),
1799
+ stack: generateStack(),
1800
+ };
1801
+
1802
+ // Do not log the inactive object x events as error events as they are not the best signal for
1803
+ // detecting something wrong with GC either from the partner or from the runtime itself.
1804
+ if (state === UnreferencedState.Inactive) {
1805
+ this.mc.logger.sendTelemetryEvent(event);
1806
+ } else {
1807
+ this.mc.logger.sendErrorEvent(event);
1808
+ }
1809
+ }
1810
+
1811
+ // If SweepReady Usage Detection is enabled, the handler may close the interactive container.
1812
+ // Once Sweep is fully implemented, this will be removed since the objects will be gone
1813
+ // and errors will arise elsewhere in the runtime
1814
+ if (state === UnreferencedState.SweepReady) {
1815
+ this.sweepReadyUsageHandler.usageDetectedInInteractiveClient({
1816
+ ...propsToLog,
1817
+ usageType,
1818
+ });
1819
+ }
1820
+ }
1821
+ }
1822
+
1823
+ private async logUnreferencedEvents(logger: ITelemetryLogger) {
1824
+ // Events sent come only from the summarizer client. In between summaries, events are pushed to a queue and at
1825
+ // summary time they are then logged.
1826
+ // Events generated:
1827
+ // InactiveObject_Loaded, InactiveObject_Changed, InactiveObject_Revived
1828
+ // SweepReadyObject_Loaded, SweepReadyObject_Changed, SweepReadyObject_Revived
1829
+ for (const eventProps of this.pendingEventsQueue) {
1830
+ const { usageType, state, ...propsToLog } = eventProps;
1831
+ /**
1832
+ * Revived event is logged only if the node is active. If the node is not active, the reference to it was
1833
+ * from another unreferenced node and this scenario is not interesting to log.
1834
+ * Loaded and Changed events are logged only if the node is not active. If the node is active, it was
1835
+ * revived and a Revived event will be logged for it.
1836
+ */
1837
+ const nodeStateTracker = this.unreferencedNodesState.get(eventProps.id);
1838
+ const active =
1839
+ nodeStateTracker === undefined ||
1840
+ nodeStateTracker.state === UnreferencedState.Active;
1841
+ if ((usageType === "Revived") === active) {
1842
+ const pkg = await this.getNodePackagePath(eventProps.id);
1843
+ const fromPkg = eventProps.fromId
1844
+ ? await this.getNodePackagePath(eventProps.fromId)
1845
+ : undefined;
1846
+ const event = {
1847
+ ...propsToLog,
1848
+ eventName: `${state}Object_${usageType}`,
1849
+ pkg: pkg
1850
+ ? { value: pkg.join("/"), tag: TelemetryDataTag.CodeArtifact }
1851
+ : undefined,
1852
+ fromPkg: fromPkg
1853
+ ? { value: fromPkg.join("/"), tag: TelemetryDataTag.CodeArtifact }
1854
+ : undefined,
1855
+ };
1856
+
1857
+ if (state === UnreferencedState.Inactive) {
1858
+ logger.sendTelemetryEvent(event);
1859
+ } else {
1860
+ logger.sendErrorEvent(event);
1861
+ }
1862
+ }
1863
+ }
1864
+ this.pendingEventsQueue = [];
1865
+ }
1617
1866
  }
1618
1867
 
1619
1868
  function generateSortedGCState(gcState: IGarbageCollectionState): IGarbageCollectionState {
1620
- const sortableArray: [string, IGarbageCollectionNodeData][] = Object.entries(gcState.gcNodes);
1621
- sortableArray.sort(([a], [b]) => a.localeCompare(b));
1622
- const sortedGCState: IGarbageCollectionState = { gcNodes: {} };
1623
- for (const [nodeId, nodeData] of sortableArray) {
1624
- nodeData.outboundRoutes.sort();
1625
- sortedGCState.gcNodes[nodeId] = nodeData;
1626
- }
1627
- return sortedGCState;
1869
+ const sortableArray: [string, IGarbageCollectionNodeData][] = Object.entries(gcState.gcNodes);
1870
+ sortableArray.sort(([a], [b]) => a.localeCompare(b));
1871
+ const sortedGCState: IGarbageCollectionState = { gcNodes: {} };
1872
+ for (const [nodeId, nodeData] of sortableArray) {
1873
+ nodeData.outboundRoutes.sort();
1874
+ sortedGCState.gcNodes[nodeId] = nodeData;
1875
+ }
1876
+ return sortedGCState;
1628
1877
  }
1629
1878
 
1630
1879
  /** A wrapper around common-utils Timer that requires the timeout when calling start/restart */
1631
1880
  class TimerWithNoDefaultTimeout extends Timer {
1632
- constructor(
1633
- private readonly callback: () => void,
1634
- ) {
1635
- // The default timeout/handlers will never be used since start/restart pass overrides below
1636
- super(0, () => { throw new Error("DefaultHandler should not be used"); });
1637
- }
1638
-
1639
- start(timeoutMs: number) {
1640
- super.start(timeoutMs, this.callback);
1641
- }
1642
-
1643
- restart(timeoutMs: number): void {
1644
- super.restart(timeoutMs, this.callback);
1645
- }
1881
+ constructor(private readonly callback: () => void) {
1882
+ // The default timeout/handlers will never be used since start/restart pass overrides below
1883
+ super(0, () => {
1884
+ throw new Error("DefaultHandler should not be used");
1885
+ });
1886
+ }
1887
+
1888
+ start(timeoutMs: number) {
1889
+ super.start(timeoutMs, this.callback);
1890
+ }
1891
+
1892
+ restart(timeoutMs: number): void {
1893
+ super.restart(timeoutMs, this.callback);
1894
+ }
1646
1895
  }