@fluidframework/container-runtime 2.0.0-internal.3.0.5 → 2.0.0-internal.3.1.1

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