@fluidframework/container-runtime 0.52.0 → 0.54.0-47413

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 (132) hide show
  1. package/dist/containerHandleContext.d.ts +0 -1
  2. package/dist/containerHandleContext.d.ts.map +1 -1
  3. package/dist/containerHandleContext.js +0 -1
  4. package/dist/containerHandleContext.js.map +1 -1
  5. package/dist/containerRuntime.d.ts +43 -19
  6. package/dist/containerRuntime.d.ts.map +1 -1
  7. package/dist/containerRuntime.js +201 -111
  8. package/dist/containerRuntime.js.map +1 -1
  9. package/dist/dataStoreContext.d.ts +33 -4
  10. package/dist/dataStoreContext.d.ts.map +1 -1
  11. package/dist/dataStoreContext.js +45 -17
  12. package/dist/dataStoreContext.js.map +1 -1
  13. package/dist/dataStores.d.ts +14 -10
  14. package/dist/dataStores.d.ts.map +1 -1
  15. package/dist/dataStores.js +73 -41
  16. package/dist/dataStores.js.map +1 -1
  17. package/dist/garbageCollection.d.ts +82 -15
  18. package/dist/garbageCollection.d.ts.map +1 -1
  19. package/dist/garbageCollection.js +359 -26
  20. package/dist/garbageCollection.js.map +1 -1
  21. package/dist/index.d.ts +2 -2
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +11 -2
  24. package/dist/index.js.map +1 -1
  25. package/dist/packageVersion.d.ts +1 -1
  26. package/dist/packageVersion.d.ts.map +1 -1
  27. package/dist/packageVersion.js +1 -1
  28. package/dist/packageVersion.js.map +1 -1
  29. package/dist/pendingStateManager.d.ts +0 -1
  30. package/dist/pendingStateManager.d.ts.map +1 -1
  31. package/dist/pendingStateManager.js +0 -36
  32. package/dist/pendingStateManager.js.map +1 -1
  33. package/dist/runningSummarizer.d.ts +3 -2
  34. package/dist/runningSummarizer.d.ts.map +1 -1
  35. package/dist/runningSummarizer.js +6 -6
  36. package/dist/runningSummarizer.js.map +1 -1
  37. package/dist/summarizer.d.ts +23 -3
  38. package/dist/summarizer.d.ts.map +1 -1
  39. package/dist/summarizer.js +135 -45
  40. package/dist/summarizer.js.map +1 -1
  41. package/dist/summarizerTypes.d.ts +3 -10
  42. package/dist/summarizerTypes.d.ts.map +1 -1
  43. package/dist/summarizerTypes.js.map +1 -1
  44. package/dist/summaryFormat.d.ts +10 -1
  45. package/dist/summaryFormat.d.ts.map +1 -1
  46. package/dist/summaryFormat.js +2 -1
  47. package/dist/summaryFormat.js.map +1 -1
  48. package/dist/summaryGenerator.d.ts.map +1 -1
  49. package/dist/summaryGenerator.js +1 -3
  50. package/dist/summaryGenerator.js.map +1 -1
  51. package/dist/summaryManager.d.ts +0 -15
  52. package/dist/summaryManager.d.ts.map +1 -1
  53. package/dist/summaryManager.js +1 -35
  54. package/dist/summaryManager.js.map +1 -1
  55. package/lib/containerHandleContext.d.ts +0 -1
  56. package/lib/containerHandleContext.d.ts.map +1 -1
  57. package/lib/containerHandleContext.js +0 -1
  58. package/lib/containerHandleContext.js.map +1 -1
  59. package/lib/containerRuntime.d.ts +43 -19
  60. package/lib/containerRuntime.d.ts.map +1 -1
  61. package/lib/containerRuntime.js +206 -117
  62. package/lib/containerRuntime.js.map +1 -1
  63. package/lib/dataStoreContext.d.ts +33 -4
  64. package/lib/dataStoreContext.d.ts.map +1 -1
  65. package/lib/dataStoreContext.js +45 -17
  66. package/lib/dataStoreContext.js.map +1 -1
  67. package/lib/dataStores.d.ts +14 -10
  68. package/lib/dataStores.d.ts.map +1 -1
  69. package/lib/dataStores.js +76 -44
  70. package/lib/dataStores.js.map +1 -1
  71. package/lib/garbageCollection.d.ts +82 -15
  72. package/lib/garbageCollection.d.ts.map +1 -1
  73. package/lib/garbageCollection.js +361 -28
  74. package/lib/garbageCollection.js.map +1 -1
  75. package/lib/index.d.ts +2 -2
  76. package/lib/index.d.ts.map +1 -1
  77. package/lib/index.js +2 -1
  78. package/lib/index.js.map +1 -1
  79. package/lib/packageVersion.d.ts +1 -1
  80. package/lib/packageVersion.d.ts.map +1 -1
  81. package/lib/packageVersion.js +1 -1
  82. package/lib/packageVersion.js.map +1 -1
  83. package/lib/pendingStateManager.d.ts +0 -1
  84. package/lib/pendingStateManager.d.ts.map +1 -1
  85. package/lib/pendingStateManager.js +0 -36
  86. package/lib/pendingStateManager.js.map +1 -1
  87. package/lib/runningSummarizer.d.ts +3 -2
  88. package/lib/runningSummarizer.d.ts.map +1 -1
  89. package/lib/runningSummarizer.js +6 -6
  90. package/lib/runningSummarizer.js.map +1 -1
  91. package/lib/summarizer.d.ts +23 -3
  92. package/lib/summarizer.d.ts.map +1 -1
  93. package/lib/summarizer.js +135 -45
  94. package/lib/summarizer.js.map +1 -1
  95. package/lib/summarizerTypes.d.ts +3 -10
  96. package/lib/summarizerTypes.d.ts.map +1 -1
  97. package/lib/summarizerTypes.js.map +1 -1
  98. package/lib/summaryFormat.d.ts +10 -1
  99. package/lib/summaryFormat.d.ts.map +1 -1
  100. package/lib/summaryFormat.js +1 -0
  101. package/lib/summaryFormat.js.map +1 -1
  102. package/lib/summaryGenerator.d.ts.map +1 -1
  103. package/lib/summaryGenerator.js +1 -3
  104. package/lib/summaryGenerator.js.map +1 -1
  105. package/lib/summaryManager.d.ts +0 -15
  106. package/lib/summaryManager.d.ts.map +1 -1
  107. package/lib/summaryManager.js +1 -34
  108. package/lib/summaryManager.js.map +1 -1
  109. package/package.json +14 -14
  110. package/src/containerHandleContext.ts +0 -1
  111. package/src/containerRuntime.ts +280 -140
  112. package/src/dataStoreContext.ts +59 -20
  113. package/src/dataStores.ts +116 -54
  114. package/src/garbageCollection.ts +492 -29
  115. package/src/index.ts +20 -2
  116. package/src/packageVersion.ts +1 -1
  117. package/src/pendingStateManager.ts +0 -43
  118. package/src/runningSummarizer.ts +12 -10
  119. package/src/summarizer.ts +154 -53
  120. package/src/summarizerTypes.ts +3 -11
  121. package/src/summaryFormat.ts +11 -1
  122. package/src/summaryGenerator.ts +2 -3
  123. package/src/summaryManager.ts +2 -49
  124. package/dist/localStorageFeatureGates.d.ts +0 -13
  125. package/dist/localStorageFeatureGates.d.ts.map +0 -1
  126. package/dist/localStorageFeatureGates.js +0 -31
  127. package/dist/localStorageFeatureGates.js.map +0 -1
  128. package/lib/localStorageFeatureGates.d.ts +0 -13
  129. package/lib/localStorageFeatureGates.d.ts.map +0 -1
  130. package/lib/localStorageFeatureGates.js +0 -27
  131. package/lib/localStorageFeatureGates.js.map +0 -1
  132. package/src/localStorageFeatureGates.ts +0 -27
@@ -2,36 +2,97 @@
2
2
  * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
3
  * Licensed under the MIT License.
4
4
  */
5
- import { runGarbageCollection } from "@fluidframework/garbage-collector";
6
- import { ChildLogger, PerformanceEvent } from "@fluidframework/telemetry-utils";
7
- import { getLocalStorageFeatureGate } from "./localStorageFeatureGates";
8
- import { getGCVersion, metadataBlobName, } from "./summaryFormat";
5
+ import { assert, LazyPromise, Timer } from "@fluidframework/common-utils";
6
+ import { cloneGCData, concatGarbageCollectionStates, concatGarbageCollectionData, runGarbageCollection, unpackChildNodesGCDetails, } from "@fluidframework/garbage-collector";
7
+ import { gcBlobKey, } from "@fluidframework/runtime-definitions";
8
+ import { SummaryTreeBuilder, } from "@fluidframework/runtime-utils";
9
+ import { ChildLogger, loggerToMonitoringContext, PerformanceEvent, } from "@fluidframework/telemetry-utils";
10
+ import { getSummaryForDatastores } from "./dataStores";
11
+ import { getGCVersion, metadataBlobName, dataStoreAttributesBlobName, } from "./summaryFormat";
9
12
  /** This is the current version of garbage collection. */
10
13
  const GCVersion = 1;
14
+ // The key for the GC tree in summary.
15
+ export const gcTreeKey = "gc";
16
+ // They prefix for GC blobs in the GC tree in summary.
17
+ export const gcBlobPrefix = "__gc";
11
18
  // Local storage key to turn GC on / off.
12
- const runGCKey = "FluidRunGC";
19
+ const runGCKey = "Fluid.GarbageCollection.RunGC";
13
20
  // Local storage key to turn GC test mode on / off.
14
- const gcTestModeKey = "FluidGCTestMode";
21
+ const gcTestModeKey = "Fluid.GarbageCollection.GCTestMode";
15
22
  // Local storage key to turn GC sweep on / off.
16
- const runSweepKey = "FluidRunSweep";
23
+ const runSweepKey = "Fluid.GarbageCollection.RunSweep";
24
+ const defaultDeleteTimeoutMs = 7 * 24 * 60 * 60 * 1000; // 7 days
25
+ /**
26
+ * Helper class that tracks the state of an unreferenced node such as the time it was unreferenced. It also sets
27
+ * the node's state to inactive if it remains unreferenced for a given amount of time (inactiveTimeoutMs).
28
+ */
29
+ class UnreferencedStateTracker {
30
+ constructor(unreferencedTimestampMs, inactiveTimeoutMs) {
31
+ this.unreferencedTimestampMs = unreferencedTimestampMs;
32
+ this.inactive = false;
33
+ // Keeps track of all inactive events that are logged. This is used to limit the log generation for each event to 1
34
+ // so that it is not noisy.
35
+ this.inactiveEventsLogged = new Set();
36
+ // If the timeout has already expired, the node should become inactive immediately. Otherwise, start a timer of
37
+ // inactiveTimeoutMs after which the node will become inactive.
38
+ if (inactiveTimeoutMs <= 0) {
39
+ this.inactive = true;
40
+ }
41
+ else {
42
+ this.timer = new Timer(inactiveTimeoutMs, () => { this.inactive = true; });
43
+ this.timer.start();
44
+ }
45
+ }
46
+ /** Stop tracking this node. Reset the unreferenced timer, if any, and reset inactive state. */
47
+ stopTracking() {
48
+ var _a;
49
+ (_a = this.timer) === null || _a === void 0 ? void 0 : _a.clear();
50
+ this.inactive = false;
51
+ }
52
+ /** Logs an error with the given properties if the node is inactive. */
53
+ logIfInactive(logger, eventName, currentTimestampMs, deleteTimeoutMs, inactiveNodeId) {
54
+ if (this.inactive && !this.inactiveEventsLogged.has(eventName)) {
55
+ logger.sendErrorEvent({
56
+ eventName,
57
+ age: currentTimestampMs - this.unreferencedTimestampMs,
58
+ timeout: deleteTimeoutMs,
59
+ id: inactiveNodeId,
60
+ });
61
+ this.inactiveEventsLogged.add(eventName);
62
+ }
63
+ }
64
+ }
17
65
  /**
18
66
  * The garbage collector for the container runtime. It consolidates the garbage collection functionality and maintains
19
67
  * its state across summaries.
20
68
  */
21
69
  export class GarbageCollector {
22
70
  constructor(provider, gcOptions,
23
- /**
24
- * After GC has run, called to delete objects in the runtime whose routes are unused. This is not part of the
25
- * provider because its specific to this garbage collector implementation and is not part of the contract.
26
- */
27
- deleteUnusedRoutes, baseLogger, existing, metadata) {
28
- var _a, _b, _c;
71
+ /** After GC has run, called to delete objects in the runtime whose routes are unused. */
72
+ deleteUnusedRoutes,
73
+ /** Returns the current timestamp to be assigned to nodes that become unreferenced. */
74
+ getCurrentTimestampMs, baseSnapshot, readAndParseBlob, baseLogger, existing, metadata) {
75
+ var _a, _b, _c, _d;
29
76
  this.provider = provider;
30
77
  this.gcOptions = gcOptions;
31
78
  this.deleteUnusedRoutes = deleteUnusedRoutes;
79
+ this.getCurrentTimestampMs = getCurrentTimestampMs;
80
+ /**
81
+ * Tells whether the GC data should be written to the root of the summary tree. We do this under 2 conditions:
82
+ * 1. If `writeDataAtRoot` GC option is enabled.
83
+ * 2. If the base summary has the GC data written at the root. This is to support forward compatibility where when
84
+ * we start writing the GC data at root, older versions can detect that and write at root too.
85
+ */
86
+ this._writeDataAtRoot = false;
32
87
  // The current GC version that this container is running.
33
88
  this.currentGCVersion = GCVersion;
34
- this.logger = ChildLogger.create(baseLogger, "GarbageCollector");
89
+ // Keeps a list of references (edges in the GC graph) between GC runs. Each entry has a node id and a list of
90
+ // outbound routes from that node.
91
+ this.referencesSinceLastRun = new Map();
92
+ // Map of node ids to their unreferenced state tracker.
93
+ this.unreferencedNodesState = new Map();
94
+ this.mc = loggerToMonitoringContext(ChildLogger.create(baseLogger, "GarbageCollector"));
95
+ this.deleteTimeoutMs = (_a = this.gcOptions.deleteTimeoutMs) !== null && _a !== void 0 ? _a : defaultDeleteTimeoutMs;
35
96
  let prevSummaryGCVersion;
36
97
  // GC can only be enabled during creation. After that, it can never be enabled again. So, for existing
37
98
  // documents, we get this information from the metadata blob.
@@ -49,7 +110,7 @@ export class GarbageCollector {
49
110
  // latest tracked GC version. For new documents, we will be writing the first summary with the current version.
50
111
  this.latestSummaryGCVersion = prevSummaryGCVersion !== null && prevSummaryGCVersion !== void 0 ? prevSummaryGCVersion : this.currentGCVersion;
51
112
  // Whether GC should run or not. Can override with localStorage flag.
52
- this.shouldRunGC = (_a = getLocalStorageFeatureGate(runGCKey)) !== null && _a !== void 0 ? _a : (
113
+ this.shouldRunGC = (_b = this.mc.config.getBoolean(runGCKey)) !== null && _b !== void 0 ? _b : (
53
114
  // GC must be enabled for the document.
54
115
  this.gcEnabled
55
116
  // GC must not be disabled via GC options.
@@ -57,16 +118,118 @@ export class GarbageCollector {
57
118
  // Whether GC sweep phase should run or not. If this is false, only GC mark phase is run. Can override with
58
119
  // localStorage flag.
59
120
  this.shouldRunSweep = this.shouldRunGC &&
60
- ((_b = getLocalStorageFeatureGate(runSweepKey)) !== null && _b !== void 0 ? _b : gcOptions.runSweep === true);
121
+ ((_c = this.mc.config.getBoolean(runSweepKey)) !== null && _c !== void 0 ? _c : gcOptions.runSweep === true);
61
122
  // Whether we are running in test mode. In this mode, unreferenced nodes are immediately deleted.
62
- this.testMode = (_c = getLocalStorageFeatureGate(gcTestModeKey)) !== null && _c !== void 0 ? _c : gcOptions.runGCInTestMode === true;
123
+ this.testMode = (_d = this.mc.config.getBoolean(gcTestModeKey)) !== null && _d !== void 0 ? _d : gcOptions.runGCInTestMode === true;
124
+ // If `writeDataAtRoot` GC option is true, we should write the GC data into the root of the summary tree. This
125
+ // GC option is used for testing only. It will be removed once we start writing GC data into root by default.
126
+ this._writeDataAtRoot = this.gcOptions.writeDataAtRoot === true;
127
+ // Get the GC state from the GC blob in the base snapshot. Use LazyPromise because we only want to do
128
+ // this once since it involves fetching blobs from storage which is expensive.
129
+ const baseSummaryStateP = new LazyPromise(async () => {
130
+ var _a;
131
+ if (baseSnapshot === undefined) {
132
+ return undefined;
133
+ }
134
+ // For newer documents, GC data should be present in the GC tree in the root of the snapshot.
135
+ const gcSnapshotTree = baseSnapshot.trees[gcTreeKey];
136
+ if (gcSnapshotTree !== undefined) {
137
+ // forward-compat - If a newer version has written the GC tree at root, we should also do the same.
138
+ this._writeDataAtRoot = true;
139
+ return getGCStateFromSnapshot(gcSnapshotTree, readAndParseBlob);
140
+ }
141
+ // back-compat - Older documents will have the GC blobs in each data store's summary tree. Get them and
142
+ // consolidate into IGarbageCollectionState format.
143
+ // Add a node for the root node that is not present in older snapshot format.
144
+ const gcState = { gcNodes: { "/": { outboundRoutes: [] } } };
145
+ const dataStoreSnaphotTree = getSummaryForDatastores(baseSnapshot, metadata);
146
+ assert(dataStoreSnaphotTree !== undefined, 0x2a8 /* "Expected data store snapshot tree in base snapshot" */);
147
+ for (const [dsId, dsSnapshotTree] of Object.entries(dataStoreSnaphotTree.trees)) {
148
+ const blobId = dsSnapshotTree.blobs[gcBlobKey];
149
+ if (blobId === undefined) {
150
+ continue;
151
+ }
152
+ const gcSummaryDetails = await readAndParseBlob(blobId);
153
+ // If there are no nodes for this data store, skip it.
154
+ if (((_a = gcSummaryDetails.gcData) === null || _a === void 0 ? void 0 : _a.gcNodes) === undefined) {
155
+ continue;
156
+ }
157
+ const dsRootId = `/${dsId}`;
158
+ // Since we used to write GC data at data store level, we won't have an entry for the root ("/").
159
+ // Construct that entry by adding root data store ids to its outbound routes.
160
+ const initialSnapshotDetails = await readAndParseBlob(dsSnapshotTree.blobs[dataStoreAttributesBlobName]);
161
+ if (initialSnapshotDetails.isRootDataStore) {
162
+ gcState.gcNodes["/"].outboundRoutes.push(dsRootId);
163
+ }
164
+ for (const [id, outboundRoutes] of Object.entries(gcSummaryDetails.gcData.gcNodes)) {
165
+ // Prefix the data store id to the GC node ids to make them relative to the root from being
166
+ // relative to the data store. Similar to how its done in DataStore::getGCData.
167
+ const rootId = id === "/" ? dsRootId : `${dsRootId}${id}`;
168
+ gcState.gcNodes[rootId] = { outboundRoutes: Array.from(outboundRoutes) };
169
+ }
170
+ assert(gcState.gcNodes[dsRootId] !== undefined, 0x2a9 /* `GC nodes for data store ${dsId} not in GC blob` */);
171
+ gcState.gcNodes[dsRootId].unreferencedTimestampMs = gcSummaryDetails.unrefTimestamp;
172
+ }
173
+ // If there is only one node (root node just added above), either GC is disabled or we are loading from the
174
+ // very first summary generated by detached container. In both cases, GC was not run - return undefined.
175
+ return Object.keys(gcState.gcNodes).length === 1 ? undefined : gcState;
176
+ });
177
+ // Set up the initializer which initializes the base GC state from the base snapshot. Use lazy promise because
178
+ // we only do this once - the very first time we run GC.
179
+ this.initializeBaseStateP = new LazyPromise(async () => {
180
+ const currentTimestampMs = this.getCurrentTimestampMs();
181
+ const baseState = await baseSummaryStateP;
182
+ if (baseState === undefined) {
183
+ return;
184
+ }
185
+ const gcNodes = {};
186
+ for (const [nodeId, nodeData] of Object.entries(baseState.gcNodes)) {
187
+ const unreferencedTimestampMs = nodeData.unreferencedTimestampMs;
188
+ if (unreferencedTimestampMs !== undefined) {
189
+ // Get how long it has been since the node was unreferenced. Start a timeout for the remaining time
190
+ // left for it to be eligible for deletion.
191
+ const unreferencedDurationMs = currentTimestampMs - unreferencedTimestampMs;
192
+ this.unreferencedNodesState.set(nodeId, new UnreferencedStateTracker(unreferencedTimestampMs, this.deleteTimeoutMs - unreferencedDurationMs));
193
+ }
194
+ gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);
195
+ }
196
+ this.gcDataFromLastRun = { gcNodes };
197
+ });
198
+ // Get the GC details for each data store from the GC state in the base summary. This is returned in
199
+ // getDataStoreBaseGCDetails and is used to initialize each data store's base GC details.
200
+ this.dataStoreGCDetailsP = new LazyPromise(async () => {
201
+ const baseState = await baseSummaryStateP;
202
+ if (baseState === undefined) {
203
+ return new Map();
204
+ }
205
+ const gcNodes = {};
206
+ for (const [nodeId, nodeData] of Object.entries(baseState.gcNodes)) {
207
+ gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);
208
+ }
209
+ // Run GC on the nodes in the base summary to get the routes used in each node in the container.
210
+ // This is an optimization for space (vs performance) wherein we don't need to store the used routes of
211
+ // each node in the summary.
212
+ const usedRoutes = runGarbageCollection(gcNodes, ["/"], this.mc.logger).referencedNodeIds;
213
+ const dataStoreGCDetailsMap = unpackChildNodesGCDetails({ gcData: { gcNodes }, usedRoutes });
214
+ // Currently, the data stores write the GC data. So, we need to update it's base GC details with the
215
+ // unreferenced timestamp. Once we start writing the GC data here, we won't need to do this anymore.
216
+ for (const [nodeId, nodeData] of Object.entries(baseState.gcNodes)) {
217
+ if (nodeData.unreferencedTimestampMs !== undefined) {
218
+ const dataStoreGCDetails = dataStoreGCDetailsMap.get(nodeId.slice(1));
219
+ if (dataStoreGCDetails !== undefined) {
220
+ dataStoreGCDetails.unrefTimestamp = nodeData.unreferencedTimestampMs;
221
+ }
222
+ }
223
+ }
224
+ return dataStoreGCDetailsMap;
225
+ });
63
226
  }
64
- static create(provider, gcOptions, deleteUnusedRoutes, baseLogger, existing, metadata) {
65
- return new GarbageCollector(provider, gcOptions, deleteUnusedRoutes, baseLogger, existing, metadata);
227
+ static create(provider, gcOptions, deleteUnusedRoutes, getCurrentTimestampMs, baseSnapshot, readAndParseBlob, baseLogger, existing, metadata) {
228
+ return new GarbageCollector(provider, gcOptions, deleteUnusedRoutes, getCurrentTimestampMs, baseSnapshot, readAndParseBlob, baseLogger, existing, metadata);
66
229
  }
67
230
  /**
68
231
  * This tracks two things:
69
- * 1. Whether GC is enabled - If this is 0, GC is disabled. If this is > 0, GC is enabled.
232
+ * 1. Whether GC is enabled - If this is 0, GC is disabled. If this is greater than 0, GC is enabled.
70
233
  * 2. If GC is enabled, the version of GC used to generate the GC data written in a summary.
71
234
  */
72
235
  get gcSummaryFeatureVersion() {
@@ -81,37 +244,71 @@ export class GarbageCollector {
81
244
  // 2. This client's latest summary was updated from a snapshot that has a different GC version.
82
245
  return this.shouldRunGC && this.latestSummaryGCVersion !== this.currentGCVersion;
83
246
  }
247
+ get writeDataAtRoot() {
248
+ return this._writeDataAtRoot;
249
+ }
84
250
  /**
85
251
  * Runs garbage collection and udpates the reference / used state of the nodes in the container.
86
252
  * @returns the number of data stores that have been marked as unreferenced.
87
253
  */
88
254
  async collectGarbage(options) {
89
- const { logger = this.logger, runSweep = this.shouldRunSweep, fullGC = this.gcOptions.runFullGC === true || this.hasGCVersionChanged, } = options;
255
+ const { logger = this.mc.logger, runSweep = this.shouldRunSweep, fullGC = this.gcOptions.runFullGC === true || this.hasGCVersionChanged, } = options;
90
256
  return PerformanceEvent.timedExecAsync(logger, { eventName: "GarbageCollection" }, async (event) => {
257
+ await this.initializeBaseStateP;
91
258
  const gcStats = {};
92
259
  // Get the runtime's GC data and run GC on the reference graph in it.
93
260
  const gcData = await this.provider.getGCData(fullGC);
94
- const { referencedNodeIds, deletedNodeIds } = runGarbageCollection(gcData.gcNodes, ["/"], logger);
95
- // Remove this node's route ("/") and notify data stores of routes that are used in it.
96
- const usedRoutes = referencedNodeIds.filter((id) => { return id !== "/"; });
97
- const dataStoreUsedStateStats = this.provider.updateUsedRoutes(usedRoutes);
261
+ this.updateStateSinceLatestRun(gcData);
262
+ const gcResult = runGarbageCollection(gcData.gcNodes, ["/"], logger);
263
+ const currentTimestampMs = this.getCurrentTimestampMs();
264
+ // Update the current state of the system based on the GC run.
265
+ this.updateCurrentState(gcData, gcResult, currentTimestampMs);
266
+ const dataStoreUsedStateStats = this.provider.updateUsedRoutes(gcResult.referencedNodeIds, currentTimestampMs);
98
267
  if (runSweep) {
99
268
  // Placeholder for running sweep logic.
100
269
  }
101
270
  // Update stats to be reported in the peformance event.
102
- gcStats.deletedNodes = deletedNodeIds.length;
103
- gcStats.totalNodes = referencedNodeIds.length + deletedNodeIds.length;
271
+ gcStats.deletedNodes = gcResult.deletedNodeIds.length;
272
+ gcStats.totalNodes = gcResult.referencedNodeIds.length + gcResult.deletedNodeIds.length;
104
273
  gcStats.deletedDataStores = dataStoreUsedStateStats.unusedNodeCount;
105
274
  gcStats.totalDataStores = dataStoreUsedStateStats.totalNodeCount;
106
275
  // If we are running in GC test mode, delete objects for unused routes. This enables testing scenarios
107
276
  // involving access to deleted data.
108
277
  if (this.testMode) {
109
- this.deleteUnusedRoutes(deletedNodeIds);
278
+ this.deleteUnusedRoutes(gcResult.deletedNodeIds);
110
279
  }
111
280
  event.end(gcStats);
112
281
  return gcStats;
113
282
  }, { end: true, cancel: "error" });
114
283
  }
284
+ /**
285
+ * Summarizes the GC data and returns it as a summary tree.
286
+ * We current write the entire GC state in a single blob. This can be modified later to write multiple
287
+ * blobs. All the blob keys should start with `gcBlobPrefix`.
288
+ */
289
+ summarize() {
290
+ var _a;
291
+ if (!this.shouldRunGC || this.gcDataFromLastRun === undefined) {
292
+ return;
293
+ }
294
+ const gcState = { gcNodes: {} };
295
+ for (const [nodeId, outboundRoutes] of Object.entries(this.gcDataFromLastRun.gcNodes)) {
296
+ gcState.gcNodes[nodeId] = {
297
+ outboundRoutes,
298
+ unreferencedTimestampMs: (_a = this.unreferencedNodesState.get(nodeId)) === null || _a === void 0 ? void 0 : _a.unreferencedTimestampMs,
299
+ };
300
+ }
301
+ const builder = new SummaryTreeBuilder();
302
+ builder.addBlob(`${gcBlobPrefix}_root`, JSON.stringify(gcState));
303
+ return builder.getSummaryTree();
304
+ }
305
+ /**
306
+ * Returns a map of data store ids to their base GC details generated from the base summary.This is used to
307
+ * initialize the data stores with their base GC state.
308
+ */
309
+ async getDataStoreBaseGCDetails() {
310
+ return this.dataStoreGCDetailsP;
311
+ }
115
312
  /**
116
313
  * Called when the latest summary of the system has been refreshed. This will be used to update the state of the
117
314
  * latest summary tracked.
@@ -130,6 +327,28 @@ export class GarbageCollector {
130
327
  // that is now the latest summary.
131
328
  await this.updateSummaryGCVersionFromSnapshot(result.snapshot, readAndParseBlob);
132
329
  }
330
+ /**
331
+ * Called when a node with the given id is changed. If the node is inactive, log an error.
332
+ */
333
+ nodeChanged(id) {
334
+ var _a;
335
+ // Prefix "/" if needed to make it relative to the root.
336
+ const nodeId = id.startsWith("/") ? id : `/${id}`;
337
+ (_a = this.unreferencedNodesState.get(nodeId)) === null || _a === void 0 ? void 0 : _a.logIfInactive(this.mc.logger, "inactiveObjectChanged", this.getCurrentTimestampMs(), this.deleteTimeoutMs, nodeId);
338
+ }
339
+ /**
340
+ * Called when an outbound reference is added to a node. This is used to identify all nodes that have been
341
+ * referenced between summaries so that their unreferenced timestamp can be reset.
342
+ *
343
+ * @param fromNodeId - The node from which the reference is added.
344
+ * @param toNodeId - The node to which the reference is added.
345
+ */
346
+ addedOutboundReference(fromNodeId, toNodeId) {
347
+ var _a;
348
+ const outboundRoutes = (_a = this.referencesSinceLastRun.get(fromNodeId)) !== null && _a !== void 0 ? _a : [];
349
+ outboundRoutes.push(toNodeId);
350
+ this.referencesSinceLastRun.set(fromNodeId, outboundRoutes);
351
+ }
133
352
  /**
134
353
  * Update the latest summary GC version from the metadata blob in the given snapshot.
135
354
  */
@@ -140,5 +359,119 @@ export class GarbageCollector {
140
359
  this.latestSummaryGCVersion = getGCVersion(metadata);
141
360
  }
142
361
  }
362
+ /**
363
+ * Updates the state of the system as per the current GC run. It does the following:
364
+ * 1. Sets up the current GC state as per the gcData.
365
+ * 2. Starts tracking for nodes that have become unreferenced in this run.
366
+ * 3. Clears tracking for nodes that were unreferenced but became referenced in this run.
367
+ * @param gcData - The data representing the reference graph on which GC is run.
368
+ * @param gcResult - The result of the GC run on the gcData.
369
+ * @param currentTimestampMs - The current timestamp to be used for unreferenced nodes' timestamp.
370
+ */
371
+ updateCurrentState(gcData, gcResult, currentTimestampMs) {
372
+ this.gcDataFromLastRun = cloneGCData(gcData);
373
+ this.referencesSinceLastRun.clear();
374
+ // Iterate through the deleted nodes and start tracking if they became unreferenced in this run.
375
+ for (const nodeId of gcResult.deletedNodeIds) {
376
+ // The time when the node became unreferenced. This is added to the current GC state.
377
+ let unreferencedTimestampMs = currentTimestampMs;
378
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
379
+ if (nodeStateTracker !== undefined) {
380
+ unreferencedTimestampMs = nodeStateTracker.unreferencedTimestampMs;
381
+ }
382
+ else {
383
+ // Start tracking this node as it became unreferenced in this run.
384
+ this.unreferencedNodesState.set(nodeId, new UnreferencedStateTracker(unreferencedTimestampMs, this.deleteTimeoutMs));
385
+ }
386
+ }
387
+ // Iterate through the referenced nodes and stop tracking if they were unreferenced before.
388
+ for (const nodeId of gcResult.referencedNodeIds) {
389
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
390
+ if (nodeStateTracker !== undefined) {
391
+ // If this node has been unreferenced for longer than deleteTimeoutMs and is being referenced,
392
+ // log an error as this may mean the deleteTimeoutMs is not long enough.
393
+ nodeStateTracker.logIfInactive(this.mc.logger, "inactiveObjectRevived", currentTimestampMs, this.deleteTimeoutMs, nodeId);
394
+ // Stop tracking so as to clear out any running timers.
395
+ nodeStateTracker.stopTracking();
396
+ // Delete the node as we don't need to track it any more.
397
+ this.unreferencedNodesState.delete(nodeId);
398
+ }
399
+ }
400
+ }
401
+ /**
402
+ * Since GC runs periodically, the GC data that is generated only tells us the state of the world at that point in
403
+ * time. It's possible that nodes transition from `unreferenced -> referenced -> unreferenced` between two runs. The
404
+ * unreferenced timestamp of such nodes needs to be reset as they may have been accessed when they were referenced.
405
+ *
406
+ * This function identifies nodes that were referenced since last run and removes their unreferenced state, if any.
407
+ * If these nodes are currently unreferenced, they will be assigned new unreferenced state by the current run.
408
+ */
409
+ updateStateSinceLatestRun(currentGCData) {
410
+ // If we haven't run GC before or no references were added since the last run, there is nothing to do.
411
+ if (this.gcDataFromLastRun === undefined || this.referencesSinceLastRun.size === 0) {
412
+ return;
413
+ }
414
+ /**
415
+ * Generate a super set of the GC data that contains the nodes and edges from last run, plus any new node and
416
+ * edges that have been added since then. To do this, combine the GC data from the last run and the current
417
+ * run, and then add the references since last run.
418
+ *
419
+ * Note on why we need to combine the data from previous run, current run and all references in between -
420
+ * 1. We need data from last run because some of its references may have been deleted since then. If those
421
+ * references added new outbound references before getting deleted, we need to detect them.
422
+ * 2. We need new outbound references since last run because some of them may have been deleted later. If those
423
+ * references added new outbound references before getting deleted, we need to detect them.
424
+ * 3. We need data from the current run because currently we may not detect when DDSs are referenced:
425
+ * - We don't require DDSs handles to be stored in a referenced DDS. For this, we need GC at DDS level
426
+ * which is tracked by https://github.com/microsoft/FluidFramework/issues/8470.
427
+ * - A new data store may have "root" DDSs already created and we don't detect them today.
428
+ */
429
+ const gcDataSuperSet = concatGarbageCollectionData(this.gcDataFromLastRun, currentGCData);
430
+ this.referencesSinceLastRun.forEach((outboundRoutes, sourceNodeId) => {
431
+ if (gcDataSuperSet.gcNodes[sourceNodeId] === undefined) {
432
+ gcDataSuperSet.gcNodes[sourceNodeId] = outboundRoutes;
433
+ }
434
+ else {
435
+ gcDataSuperSet.gcNodes[sourceNodeId].push(...outboundRoutes);
436
+ }
437
+ });
438
+ /**
439
+ * Run GC on the above reference graph to find all nodes that are referenced. For each one, if they are
440
+ * unreferenced, stop tracking them and remove from unreferenced list.
441
+ * Some of these nodes may be unreferenced now and if so, the current run will add unreferenced state for them.
442
+ */
443
+ const gcResult = runGarbageCollection(gcDataSuperSet.gcNodes, ["/"], this.mc.logger);
444
+ for (const nodeId of gcResult.referencedNodeIds) {
445
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
446
+ if (nodeStateTracker !== undefined) {
447
+ // Stop tracking so as to clear out any running timers.
448
+ nodeStateTracker.stopTracking();
449
+ // Delete the node as we don't need to track it any more.
450
+ this.unreferencedNodesState.delete(nodeId);
451
+ }
452
+ }
453
+ }
454
+ }
455
+ /**
456
+ * Gets the garbage collection state from the given snapshot tree. The GC state may be written into multiple blobs.
457
+ * Merge the GC state from all such blobs and return the merged GC state.
458
+ */
459
+ async function getGCStateFromSnapshot(gcSnapshotTree, readAndParseBlob) {
460
+ let rootGCState = { gcNodes: {} };
461
+ for (const key of Object.keys(gcSnapshotTree.blobs)) {
462
+ // Skip blobs that do not stsart with the GC prefix.
463
+ if (!key.startsWith(gcBlobPrefix)) {
464
+ continue;
465
+ }
466
+ const blobId = gcSnapshotTree.blobs[key];
467
+ if (blobId === undefined) {
468
+ continue;
469
+ }
470
+ const gcState = await readAndParseBlob(blobId);
471
+ assert(gcState !== undefined, 0x2ad /* "GC blob missing from snapshot" */);
472
+ // Merge the GC state of this blob into the root GC state.
473
+ rootGCState = concatGarbageCollectionStates(rootGCState, gcState);
474
+ }
475
+ return rootGCState;
143
476
  }
144
477
  //# sourceMappingURL=garbageCollection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"garbageCollection.js","sourceRoot":"","sources":["../src/garbageCollection.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AAIzE,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAGhF,OAAO,EAAE,0BAA0B,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,EACH,YAAY,EAGZ,gBAAgB,GACnB,MAAM,iBAAiB,CAAC;AAEzB,yDAAyD;AACzD,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB,yCAAyC;AACzC,MAAM,QAAQ,GAAG,YAAY,CAAC;AAC9B,mDAAmD;AACnD,MAAM,aAAa,GAAG,iBAAiB,CAAC;AACxC,+CAA+C;AAC/C,MAAM,WAAW,GAAG,eAAe,CAAC;AA4CpC;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IAkDzB,YACqB,QAAmC,EACnC,SAA4B;IAC7C;;;OAGG;IACc,kBAAoD,EACrE,UAA4B,EAC5B,QAAiB,EACjB,QAAoC;;QATnB,aAAQ,GAAR,QAAQ,CAA2B;QACnC,cAAS,GAAT,SAAS,CAAmB;QAK5B,uBAAkB,GAAlB,kBAAkB,CAAkC;QAZzE,yDAAyD;QACxC,qBAAgB,GAAG,SAAS,CAAC;QAgB1C,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;QAEjE,IAAI,oBAAwC,CAAC;QAC7C,sGAAsG;QACtG,6DAA6D;QAC7D,IAAI,QAAQ,EAAE;YACV,oBAAoB,GAAG,YA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C,iBAAiB,GAAG,uBAAuB,CAAC,eAAe,CAAC;YACpE,OAAO,CAAC,eAAe,GAAG,uBAAuB,CAAC,cAAc,CAAC;YAEjE,sGAAsG;YACtG,oCAAoC;YACpC,IAAI,IAAI,CAAC,QAAQ,EAAE;gBACf,IAAI,CAAC,kBAAkB,CAAC,cAAc,CAAC,CAAC;aAC3C;YACD,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YACnB,OAAO,OAAmB,CAAC;QAC/B,CAAC,EACD,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IACpC,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,2BAA2B,CACpC,MAA4B,EAC5B,gBAAkC;QAElC,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,MAAM,CAAC,oBAAoB,EAAE;YACnD,OAAO;SACV;QAED,2GAA2G;QAC3G,uDAAuD;QACvD,IAAI,MAAM,CAAC,iBAAiB,EAAE;YAC1B,IAAI,CAAC,sBAAsB,GAAG,IAAI,CAAC,gBAAgB,CAAC;YACpD,OAAO;SACV;QACD,6GAA6G;QAC7G,kCAAkC;QAClC,MAAM,IAAI,CAAC,kCAAkC,CAAC,MAAM,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;IACrF,CAAC;IAED;;OAEG;IACK,KAAK,CAAC,kCAAkC,CAAC,QAAuB,EAAE,gBAAkC;QACxG,MAAM,cAAc,GAAG,QAAQ,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;QACxD,IAAI,cAAc,EAAE;YAChB,MAAM,QAAQ,GAAG,MAAM,gBAAgB,CAA4B,cAAc,CAAC,CAAC;YACnF,IAAI,CAAC,sBAAsB,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;SACxD;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { ITelemetryLogger } from \"@fluidframework/common-definitions\";\nimport { runGarbageCollection } from \"@fluidframework/garbage-collector\";\nimport { ISnapshotTree } from \"@fluidframework/protocol-definitions\";\nimport { IGarbageCollectionData } from \"@fluidframework/runtime-definitions\";\nimport { ReadAndParseBlob, RefreshSummaryResult } from \"@fluidframework/runtime-utils\";\nimport { ChildLogger, PerformanceEvent } from \"@fluidframework/telemetry-utils\";\n\nimport { IGCRuntimeOptions } from \"./containerRuntime\";\nimport { getLocalStorageFeatureGate } from \"./localStorageFeatureGates\";\nimport {\n getGCVersion,\n GCVersion,\n IContainerRuntimeMetadata,\n metadataBlobName,\n} from \"./summaryFormat\";\n\n/** This is the current version of garbage collection. */\nconst GCVersion = 1;\n\n// Local storage key to turn GC on / off.\nconst runGCKey = \"FluidRunGC\";\n// Local storage key to turn GC test mode on / off.\nconst gcTestModeKey = \"FluidGCTestMode\";\n// Local storage key to turn GC sweep on / off.\nconst runSweepKey = \"FluidRunSweep\";\n\n/** The used state statistics of a node. */\nexport interface IUsedStateStats {\n totalNodeCount: number;\n unusedNodeCount: number;\n}\n\n/** The statistics of the system state after a garbage collection run. */\nexport interface IGCStats {\n totalNodes: number;\n deletedNodes: number;\n totalDataStores: number;\n deletedDataStores: number;\n}\n\n/** Defines the APIs for the runtime object to be passed to the garbage collector. */\nexport interface IGarbageCollectionRuntime {\n /** Returns the garbage collection data of the runtime. */\n getGCData(fullGC?: boolean): Promise<IGarbageCollectionData>;\n /** After GC has run, called to notify the runtime of routes that are used in it. */\n updateUsedRoutes(usedRoutes: string[]): IUsedStateStats;\n}\n\n/** Defines the contract for the garbage collector. */\nexport interface IGarbageCollector {\n /** Tells whether GC should run or not. */\n readonly shouldRunGC: boolean;\n /**\n * This tracks two things:\n * 1. Whether GC is enabled - If this is 0, GC is disabled. If this is > 0, GC is enabled.\n * 2. If GC is enabled, the version of GC used to generate the GC data written in a summary.\n */\n readonly gcSummaryFeatureVersion: number;\n /** Tells whether the GC version has changed compared to the version in the latest summary. */\n readonly hasGCVersionChanged: boolean;\n /** Run garbage collection and update the reference / used state of the system. */\n collectGarbage(\n options: { logger?: ITelemetryLogger, runGC?: boolean, runSweep?: boolean, fullGC?: boolean },\n ): Promise<IGCStats>;\n /** Called when the latest summary of the system has been refreshed. */\n latestSummaryStateRefreshed(result: RefreshSummaryResult, readAndParseBlob: ReadAndParseBlob): Promise<void>;\n}\n\n/**\n * The garbage collector for the container runtime. It consolidates the garbage collection functionality and maintains\n * its state across summaries.\n */\nexport class GarbageCollector implements IGarbageCollector {\n public static create(\n provider: IGarbageCollectionRuntime,\n gcOptions: IGCRuntimeOptions,\n deleteUnusedRoutes: (unusedRoutes: string[]) => void,\n baseLogger: ITelemetryLogger,\n existing: boolean,\n metadata?: IContainerRuntimeMetadata,\n ): IGarbageCollector {\n return new GarbageCollector(provider, gcOptions, deleteUnusedRoutes, baseLogger, existing, metadata);\n }\n\n /**\n * Tells whether GC should be run based on the GC options and local storage flags.\n */\n public readonly shouldRunGC: boolean;\n\n /**\n * This tracks two things:\n * 1. Whether GC is enabled - If this is 0, GC is disabled. If this is > 0, GC is enabled.\n * 2. If GC is enabled, the version of GC used to generate the GC data written in a summary.\n */\n public get gcSummaryFeatureVersion(): number {\n return this.gcEnabled ? this.currentGCVersion : 0;\n }\n\n /**\n * Tells whether the GC version has changed compared to the version in the latest summary.\n */\n public get hasGCVersionChanged(): boolean {\n // The current version can differ from the latest summary version in two cases:\n // 1. The summary this client loaded with has data from a different GC version.\n // 2. This client's latest summary was updated from a snapshot that has a different GC version.\n return this.shouldRunGC && this.latestSummaryGCVersion !== this.currentGCVersion;\n }\n\n /**\n * Tracks if GC is enabled for this document. This is specified during document creation and doesn't change\n * throughout its lifetime.\n */\n private readonly gcEnabled: boolean;\n private readonly shouldRunSweep: boolean;\n private readonly testMode: boolean;\n private readonly logger: ITelemetryLogger;\n\n // The current GC version that this container is running.\n private readonly currentGCVersion = GCVersion;\n // This is the version of GC data in the latest summary being tracked.\n private latestSummaryGCVersion: GCVersion;\n\n protected constructor(\n private readonly provider: IGarbageCollectionRuntime,\n private readonly gcOptions: IGCRuntimeOptions,\n /**\n * After GC has run, called to delete objects in the runtime whose routes are unused. This is not part of the\n * provider because its specific to this garbage collector implementation and is not part of the contract.\n */\n private readonly deleteUnusedRoutes: (unusedRoutes: string[]) => void,\n baseLogger: ITelemetryLogger,\n existing: boolean,\n metadata?: IContainerRuntimeMetadata,\n ) {\n this.logger = ChildLogger.create(baseLogger, \"GarbageCollector\");\n\n let prevSummaryGCVersion: number | undefined;\n // GC can only be enabled during creation. After that, it can never be enabled again. So, for existing\n // documents, we get this information from the metadata blob.\n if (existing) {\n prevSummaryGCVersion = getGCVersion(metadata);\n // Existing documents which did not have metadata blob or had GC disabled have version as 0. For all\n // other exsiting documents, GC is enabled.\n this.gcEnabled = prevSummaryGCVersion > 0;\n } else {\n // For new documents, GC has to be exlicitly enabled via the gcAllowed flag in GC options.\n this.gcEnabled = gcOptions.gcAllowed === true;\n }\n // For existing document, the latest summary is the one that we loaded from. So, use its GC version as the\n // latest tracked GC version. For new documents, we will be writing the first summary with the current version.\n this.latestSummaryGCVersion = prevSummaryGCVersion ?? this.currentGCVersion;\n\n // Whether GC should run or not. Can override with localStorage flag.\n this.shouldRunGC = getLocalStorageFeatureGate(runGCKey) ?? (\n // GC must be enabled for the document.\n this.gcEnabled\n // GC must not be disabled via GC options.\n && !gcOptions.disableGC\n );\n\n // Whether GC sweep phase should run or not. If this is false, only GC mark phase is run. Can override with\n // localStorage flag.\n this.shouldRunSweep = this.shouldRunGC &&\n (getLocalStorageFeatureGate(runSweepKey) ?? gcOptions.runSweep === true);\n\n // Whether we are running in test mode. In this mode, unreferenced nodes are immediately deleted.\n this.testMode = getLocalStorageFeatureGate(gcTestModeKey) ?? gcOptions.runGCInTestMode === true;\n }\n\n /**\n * Runs garbage collection and udpates the reference / used state of the nodes in the container.\n * @returns the number of data stores that have been marked as unreferenced.\n */\n public async collectGarbage(\n options: {\n /** Logger to use for logging GC events */\n logger?: ITelemetryLogger,\n /** True to run GC sweep phase after the mark phase */\n runSweep?: boolean,\n /** True to generate full GC data */\n fullGC?: boolean,\n },\n ): Promise<IGCStats> {\n const {\n logger = this.logger,\n runSweep = this.shouldRunSweep,\n fullGC = this.gcOptions.runFullGC === true || this.hasGCVersionChanged,\n } = options;\n\n return PerformanceEvent.timedExecAsync(logger, { eventName: \"GarbageCollection\" }, async (event) => {\n const gcStats: {\n deletedNodes?: number,\n totalNodes?: number,\n deletedDataStores?: number,\n totalDataStores?: number,\n } = {};\n\n // Get the runtime's GC data and run GC on the reference graph in it.\n const gcData = await this.provider.getGCData(fullGC);\n const { referencedNodeIds, deletedNodeIds } = runGarbageCollection(\n gcData.gcNodes,\n [ \"/\" ],\n logger,\n );\n\n // Remove this node's route (\"/\") and notify data stores of routes that are used in it.\n const usedRoutes = referencedNodeIds.filter((id: string) => { return id !== \"/\"; });\n const dataStoreUsedStateStats = this.provider.updateUsedRoutes(usedRoutes);\n\n if (runSweep) {\n // Placeholder for running sweep logic.\n }\n\n // Update stats to be reported in the peformance event.\n gcStats.deletedNodes = deletedNodeIds.length;\n gcStats.totalNodes = referencedNodeIds.length + deletedNodeIds.length;\n gcStats.deletedDataStores = dataStoreUsedStateStats.unusedNodeCount;\n gcStats.totalDataStores = dataStoreUsedStateStats.totalNodeCount;\n\n // If we are running in GC test mode, delete objects for unused routes. This enables testing scenarios\n // involving access to deleted data.\n if (this.testMode) {\n this.deleteUnusedRoutes(deletedNodeIds);\n }\n event.end(gcStats);\n return gcStats as IGCStats;\n },\n { end: true, cancel: \"error\" });\n }\n\n /**\n * Called when the latest summary of the system has been refreshed. This will be used to update the state of the\n * latest summary tracked.\n */\n public async latestSummaryStateRefreshed(\n result: RefreshSummaryResult,\n readAndParseBlob: ReadAndParseBlob,\n ): Promise<void> {\n if (!this.shouldRunGC || !result.latestSummaryUpdated) {\n return;\n }\n\n // If the summary was tracked by this client, it was the one that generated the summary in the first place.\n // Basically, it was written in the current GC version.\n if (result.wasSummaryTracked) {\n this.latestSummaryGCVersion = this.currentGCVersion;\n return;\n }\n // If the summary was not tracked by this client, update latest GC version from the snapshot in the result as\n // that is now the latest summary.\n await this.updateSummaryGCVersionFromSnapshot(result.snapshot, readAndParseBlob);\n }\n\n /**\n * Update the latest summary GC version from the metadata blob in the given snapshot.\n */\n private async updateSummaryGCVersionFromSnapshot(snapshot: ISnapshotTree, readAndParseBlob: ReadAndParseBlob) {\n const metadataBlobId = snapshot.blobs[metadataBlobName];\n if (metadataBlobId) {\n const metadata = await readAndParseBlob<IContainerRuntimeMetadata>(metadataBlobId);\n this.latestSummaryGCVersion = getGCVersion(metadata);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"garbageCollection.js","sourceRoot":"","sources":["../src/garbageCollection.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EACH,WAAW,EACX,6BAA6B,EAC7B,2BAA2B,EAE3B,oBAAoB,EACpB,yBAAyB,GAC5B,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACH,SAAS,GAKZ,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAGH,kBAAkB,GACrB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EACH,WAAW,EACX,yBAAyB,EAEzB,gBAAgB,GAClB,MAAM,iCAAiC,CAAC;AAG1C,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EACH,YAAY,EAGZ,gBAAgB,EAEhB,2BAA2B,GAC9B,MAAM,iBAAiB,CAAC;AAEzB,yDAAyD;AACzD,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB,sCAAsC;AACtC,MAAM,CAAC,MAAM,SAAS,GAAG,IAAI,CAAC;AAC9B,sDAAsD;AACtD,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC;AAEnC,yCAAyC;AACzC,MAAM,QAAQ,GAAG,+BAA+B,CAA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KAAK,CAAC,GAAG,CAAC,CAAC;QACzC,IAAI,MAAM,KAAK,SAAS,EAAE;YACtB,SAAS;SACZ;QACD,MAAM,OAAO,GAAG,MAAM,gBAAgB,CAA0B,MAAM,CAAC,CAAC;QACxE,MAAM,CAAC,OAAO,KAAK,SAAS,EAAE,KAAK,CAAC,qCAAqC,CAAC,CAAC;QAC3E,0DAA0D;QAC1D,WAAW,GAAG,6BAA6B,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;KACrE;IACD,OAAO,WAAW,CAAC;AACvB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { ITelemetryLogger } from \"@fluidframework/common-definitions\";\nimport { assert, LazyPromise, Timer } from \"@fluidframework/common-utils\";\nimport {\n cloneGCData,\n concatGarbageCollectionStates,\n concatGarbageCollectionData,\n IGCResult,\n runGarbageCollection,\n unpackChildNodesGCDetails,\n} from \"@fluidframework/garbage-collector\";\nimport { ISnapshotTree } from \"@fluidframework/protocol-definitions\";\nimport {\n gcBlobKey,\n IGarbageCollectionData,\n IGarbageCollectionState,\n IGarbageCollectionSummaryDetails,\n ISummaryTreeWithStats,\n} from \"@fluidframework/runtime-definitions\";\nimport {\n ReadAndParseBlob,\n RefreshSummaryResult,\n SummaryTreeBuilder,\n} from \"@fluidframework/runtime-utils\";\nimport {\n ChildLogger,\n loggerToMonitoringContext,\n MonitoringContext,\n PerformanceEvent,\n } from \"@fluidframework/telemetry-utils\";\n\nimport { IGCRuntimeOptions } from \"./containerRuntime\";\nimport { getSummaryForDatastores } from \"./dataStores\";\nimport {\n getGCVersion,\n GCVersion,\n IContainerRuntimeMetadata,\n metadataBlobName,\n ReadFluidDataStoreAttributes,\n dataStoreAttributesBlobName,\n} from \"./summaryFormat\";\n\n/** This is the current version of garbage collection. */\nconst GCVersion = 1;\n\n// The key for the GC tree in summary.\nexport const gcTreeKey = \"gc\";\n// They prefix for GC blobs in the GC tree in summary.\nexport const gcBlobPrefix = \"__gc\";\n\n// Local storage key to turn GC on / off.\nconst runGCKey = \"Fluid.GarbageCollection.RunGC\";\n// Local storage key to turn GC test mode on / off.\nconst gcTestModeKey = \"Fluid.GarbageCollection.GCTestMode\";\n// Local storage key to turn GC sweep on / off.\nconst runSweepKey = \"Fluid.GarbageCollection.RunSweep\";\n\nconst defaultDeleteTimeoutMs = 7 * 24 * 60 * 60 * 1000; // 7 days\n\n/** The used state statistics of a node. */\nexport interface IUsedStateStats {\n totalNodeCount: number;\n unusedNodeCount: number;\n}\n\n/** The statistics of the system state after a garbage collection run. */\nexport interface IGCStats {\n totalNodes: number;\n deletedNodes: number;\n totalDataStores: number;\n deletedDataStores: number;\n}\n\n/** Defines the APIs for the runtime object to be passed to the garbage collector. */\nexport interface IGarbageCollectionRuntime {\n /** Returns the garbage collection data of the runtime. */\n getGCData(fullGC?: boolean): Promise<IGarbageCollectionData>;\n /** After GC has run, called to notify the runtime of routes that are used in it. */\n updateUsedRoutes(usedRoutes: string[], gcTimestamp?: number): IUsedStateStats;\n}\n\n/** Defines the contract for the garbage collector. */\nexport interface IGarbageCollector {\n /** Tells whether GC should run or not. */\n readonly shouldRunGC: boolean;\n /**\n * This tracks two things:\n * 1. Whether GC is enabled - If this is 0, GC is disabled. If this is greater than 0, GC is enabled.\n * 2. If GC is enabled, the version of GC used to generate the GC data written in a summary.\n */\n readonly gcSummaryFeatureVersion: number;\n /** Tells whether the GC version has changed compared to the version in the latest summary. */\n readonly hasGCVersionChanged: boolean;\n /** Tells whether GC data should be written to the root of the summary tree. */\n readonly writeDataAtRoot: boolean;\n /** Run garbage collection and update the reference / used state of the system. */\n collectGarbage(\n options: { logger?: ITelemetryLogger, runGC?: boolean, runSweep?: boolean, fullGC?: boolean },\n ): Promise<IGCStats>;\n /** Summarizes the GC data and returns it as a summary tree. */\n summarize(): ISummaryTreeWithStats | undefined;\n /** Returns a map of each data store id to its GC details in the base summary. */\n getDataStoreBaseGCDetails(): Promise<Map<string, IGarbageCollectionSummaryDetails>>;\n /** Called when the latest summary of the system has been refreshed. */\n latestSummaryStateRefreshed(result: RefreshSummaryResult, readAndParseBlob: ReadAndParseBlob): Promise<void>;\n /** Called when a node is changed. Used to detect and log when an inactive node is changed. */\n nodeChanged(id: string): void;\n /** Called when a reference is added to a node. Used to identify nodes that were referenced between summaries. */\n addedOutboundReference(fromNodeId: string, toNodeId: string): void;\n}\n\n/**\n * Helper class that tracks the state of an unreferenced node such as the time it was unreferenced. It also sets\n * the node's state to inactive if it remains unreferenced for a given amount of time (inactiveTimeoutMs).\n */\nclass UnreferencedStateTracker {\n private inactive: boolean = false;\n // Keeps track of all inactive events that are logged. This is used to limit the log generation for each event to 1\n // so that it is not noisy.\n private readonly inactiveEventsLogged: Set<string> = new Set();\n private readonly timer: Timer | undefined;\n\n constructor(\n public readonly unreferencedTimestampMs: number,\n inactiveTimeoutMs: number,\n ) {\n // If the timeout has already expired, the node should become inactive immediately. Otherwise, start a timer of\n // inactiveTimeoutMs after which the node will become inactive.\n if (inactiveTimeoutMs <= 0) {\n this.inactive = true;\n } else {\n this.timer = new Timer(inactiveTimeoutMs, () => { this.inactive = true; });\n this.timer.start();\n }\n }\n\n /** Stop tracking this node. Reset the unreferenced timer, if any, and reset inactive state. */\n public stopTracking() {\n this.timer?.clear();\n this.inactive = false;\n }\n\n /** Logs an error with the given properties if the node is inactive. */\n public logIfInactive(\n logger: ITelemetryLogger,\n eventName: string,\n currentTimestampMs: number,\n deleteTimeoutMs: number,\n inactiveNodeId: string,\n ) {\n if (this.inactive && !this.inactiveEventsLogged.has(eventName)) {\n logger.sendErrorEvent({\n eventName,\n age: currentTimestampMs - this.unreferencedTimestampMs,\n timeout: deleteTimeoutMs,\n id: inactiveNodeId,\n });\n this.inactiveEventsLogged.add(eventName);\n }\n }\n}\n\n/**\n * The garbage collector for the container runtime. It consolidates the garbage collection functionality and maintains\n * its state across summaries.\n */\nexport class GarbageCollector implements IGarbageCollector {\n public static create(\n provider: IGarbageCollectionRuntime,\n gcOptions: IGCRuntimeOptions,\n deleteUnusedRoutes: (unusedRoutes: string[]) => void,\n getCurrentTimestampMs: () => number,\n baseSnapshot: ISnapshotTree | undefined,\n readAndParseBlob: ReadAndParseBlob,\n baseLogger: ITelemetryLogger,\n existing: boolean,\n metadata?: IContainerRuntimeMetadata,\n ): IGarbageCollector {\n return new GarbageCollector(\n provider,\n gcOptions,\n deleteUnusedRoutes,\n getCurrentTimestampMs,\n baseSnapshot,\n readAndParseBlob,\n baseLogger,\n existing,\n metadata,\n );\n }\n\n /**\n * Tells whether GC should be run based on the GC options and local storage flags.\n */\n public readonly shouldRunGC: boolean;\n\n /**\n * This tracks two things:\n * 1. Whether GC is enabled - If this is 0, GC is disabled. If this is greater than 0, GC is enabled.\n * 2. If GC is enabled, the version of GC used to generate the GC data written in a summary.\n */\n public get gcSummaryFeatureVersion(): number {\n return this.gcEnabled ? this.currentGCVersion : 0;\n }\n\n /**\n * Tells whether the GC version has changed compared to the version in the latest summary.\n */\n public get hasGCVersionChanged(): boolean {\n // The current version can differ from the latest summary version in two cases:\n // 1. The summary this client loaded with has data from a different GC version.\n // 2. This client's latest summary was updated from a snapshot that has a different GC version.\n return this.shouldRunGC && this.latestSummaryGCVersion !== this.currentGCVersion;\n }\n\n /**\n * Tracks if GC is enabled for this document. This is specified during document creation and doesn't change\n * throughout its lifetime.\n */\n private readonly gcEnabled: boolean;\n private readonly shouldRunSweep: boolean;\n private readonly testMode: boolean;\n private readonly mc: MonitoringContext;\n\n /**\n * Tells whether the GC data should be written to the root of the summary tree. We do this under 2 conditions:\n * 1. If `writeDataAtRoot` GC option is enabled.\n * 2. If the base summary has the GC data written at the root. This is to support forward compatibility where when\n * we start writing the GC data at root, older versions can detect that and write at root too.\n */\n private _writeDataAtRoot: boolean = false;\n public get writeDataAtRoot(): boolean {\n return this._writeDataAtRoot;\n }\n\n // The current GC version that this container is running.\n private readonly currentGCVersion = GCVersion;\n // This is the version of GC data in the latest summary being tracked.\n private latestSummaryGCVersion: GCVersion;\n\n // Keeps track of the GC state from the last run.\n private gcDataFromLastRun: IGarbageCollectionData | undefined;\n // Keeps a list of references (edges in the GC graph) between GC runs. Each entry has a node id and a list of\n // outbound routes from that node.\n private readonly referencesSinceLastRun: Map<string, string[]> = new Map();\n\n // Promise when resolved initializes the base state of the nodes from the base summary state.\n private readonly initializeBaseStateP: Promise<void>;\n // The map of data store ids to their GC details in the base summary returned in getDataStoreGCDetails().\n private readonly dataStoreGCDetailsP: Promise<Map<string, IGarbageCollectionSummaryDetails>>;\n // The time after which an unreferenced node can be deleted. Currently, we only set the node's state to expired.\n private readonly deleteTimeoutMs: number;\n // Map of node ids to their unreferenced state tracker.\n private readonly unreferencedNodesState: Map<string, UnreferencedStateTracker> = new Map();\n\n protected constructor(\n private readonly provider: IGarbageCollectionRuntime,\n private readonly gcOptions: IGCRuntimeOptions,\n /** After GC has run, called to delete objects in the runtime whose routes are unused. */\n private readonly deleteUnusedRoutes: (unusedRoutes: string[]) => void,\n /** Returns the current timestamp to be assigned to nodes that become unreferenced. */\n private readonly getCurrentTimestampMs: () => number,\n baseSnapshot: ISnapshotTree | undefined,\n readAndParseBlob: ReadAndParseBlob,\n baseLogger: ITelemetryLogger,\n existing: boolean,\n metadata?: IContainerRuntimeMetadata,\n ) {\n this.mc = loggerToMonitoringContext(\n ChildLogger.create(baseLogger, \"GarbageCollector\"));\n\n this.deleteTimeoutMs = this.gcOptions.deleteTimeoutMs ?? defaultDeleteTimeoutMs;\n\n let prevSummaryGCVersion: number | undefined;\n // GC can only be enabled during creation. After that, it can never be enabled again. So, for existing\n // documents, we get this information from the metadata blob.\n if (existing) {\n prevSummaryGCVersion = getGCVersion(metadata);\n // Existing documents which did not have metadata blob or had GC disabled have version as 0. For all\n // other exsiting documents, GC is enabled.\n this.gcEnabled = prevSummaryGCVersion > 0;\n } else {\n // For new documents, GC has to be exlicitly enabled via the gcAllowed flag in GC options.\n this.gcEnabled = gcOptions.gcAllowed === true;\n }\n // For existing document, the latest summary is the one that we loaded from. So, use its GC version as the\n // latest tracked GC version. For new documents, we will be writing the first summary with the current version.\n this.latestSummaryGCVersion = prevSummaryGCVersion ?? this.currentGCVersion;\n\n // Whether GC should run or not. Can override with localStorage flag.\n this.shouldRunGC = this.mc.config.getBoolean(runGCKey) ?? (\n // GC must be enabled for the document.\n this.gcEnabled\n // GC must not be disabled via GC options.\n && !gcOptions.disableGC\n );\n\n // Whether GC sweep phase should run or not. If this is false, only GC mark phase is run. Can override with\n // localStorage flag.\n this.shouldRunSweep = this.shouldRunGC &&\n (this.mc.config.getBoolean(runSweepKey) ?? gcOptions.runSweep === true);\n\n // Whether we are running in test mode. In this mode, unreferenced nodes are immediately deleted.\n this.testMode = this.mc.config.getBoolean(gcTestModeKey) ?? gcOptions.runGCInTestMode === true;\n\n // If `writeDataAtRoot` GC option is true, we should write the GC data into the root of the summary tree. This\n // GC option is used for testing only. It will be removed once we start writing GC data into root by default.\n this._writeDataAtRoot = this.gcOptions.writeDataAtRoot === true;\n\n // Get the GC state from the GC blob in the base snapshot. Use LazyPromise because we only want to do\n // this once since it involves fetching blobs from storage which is expensive.\n const baseSummaryStateP = new LazyPromise<IGarbageCollectionState | undefined>(async () => {\n if (baseSnapshot === undefined) {\n return undefined;\n }\n\n // For newer documents, GC data should be present in the GC tree in the root of the snapshot.\n const gcSnapshotTree = baseSnapshot.trees[gcTreeKey];\n if (gcSnapshotTree !== undefined) {\n // forward-compat - If a newer version has written the GC tree at root, we should also do the same.\n this._writeDataAtRoot = true;\n return getGCStateFromSnapshot(gcSnapshotTree, readAndParseBlob);\n }\n\n // back-compat - Older documents will have the GC blobs in each data store's summary tree. Get them and\n // consolidate into IGarbageCollectionState format.\n // Add a node for the root node that is not present in older snapshot format.\n const gcState: IGarbageCollectionState = { gcNodes: { \"/\": { outboundRoutes: [] } } };\n const dataStoreSnaphotTree = getSummaryForDatastores(baseSnapshot, metadata);\n assert(dataStoreSnaphotTree !== undefined,\n 0x2a8 /* \"Expected data store snapshot tree in base snapshot\" */);\n for (const [dsId, dsSnapshotTree] of Object.entries(dataStoreSnaphotTree.trees)) {\n const blobId = dsSnapshotTree.blobs[gcBlobKey];\n if (blobId === undefined) {\n continue;\n }\n\n const gcSummaryDetails = await readAndParseBlob<IGarbageCollectionSummaryDetails>(blobId);\n // If there are no nodes for this data store, skip it.\n if (gcSummaryDetails.gcData?.gcNodes === undefined) {\n continue;\n }\n\n const dsRootId = `/${dsId}`;\n // Since we used to write GC data at data store level, we won't have an entry for the root (\"/\").\n // Construct that entry by adding root data store ids to its outbound routes.\n const initialSnapshotDetails = await readAndParseBlob<ReadFluidDataStoreAttributes>(\n dsSnapshotTree.blobs[dataStoreAttributesBlobName],\n );\n if (initialSnapshotDetails.isRootDataStore) {\n gcState.gcNodes[\"/\"].outboundRoutes.push(dsRootId);\n }\n\n for (const [id, outboundRoutes] of Object.entries(gcSummaryDetails.gcData.gcNodes)) {\n // Prefix the data store id to the GC node ids to make them relative to the root from being\n // relative to the data store. Similar to how its done in DataStore::getGCData.\n const rootId = id === \"/\" ? dsRootId : `${dsRootId}${id}`;\n gcState.gcNodes[rootId] = { outboundRoutes: Array.from(outboundRoutes) };\n }\n assert(gcState.gcNodes[dsRootId] !== undefined,\n 0x2a9 /* `GC nodes for data store ${dsId} not in GC blob` */);\n gcState.gcNodes[dsRootId].unreferencedTimestampMs = gcSummaryDetails.unrefTimestamp;\n }\n\n // If there is only one node (root node just added above), either GC is disabled or we are loading from the\n // very first summary generated by detached container. In both cases, GC was not run - return undefined.\n return Object.keys(gcState.gcNodes).length === 1 ? undefined : gcState;\n });\n\n // Set up the initializer which initializes the base GC state from the base snapshot. Use lazy promise because\n // we only do this once - the very first time we run GC.\n this.initializeBaseStateP = new LazyPromise<void>(async () => {\n const currentTimestampMs = this.getCurrentTimestampMs();\n const baseState = await baseSummaryStateP;\n if (baseState === undefined) {\n return;\n }\n\n const gcNodes: { [ id: string ]: string[] } = {};\n for (const [nodeId, nodeData] of Object.entries(baseState.gcNodes)) {\n const unreferencedTimestampMs = nodeData.unreferencedTimestampMs;\n if (unreferencedTimestampMs !== undefined) {\n // Get how long it has been since the node was unreferenced. Start a timeout for the remaining time\n // left for it to be eligible for deletion.\n const unreferencedDurationMs = currentTimestampMs - unreferencedTimestampMs;\n this.unreferencedNodesState.set(\n nodeId,\n new UnreferencedStateTracker(\n unreferencedTimestampMs,\n this.deleteTimeoutMs - unreferencedDurationMs,\n ),\n );\n }\n gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);\n }\n this.gcDataFromLastRun = { gcNodes };\n });\n\n // Get the GC details for each data store from the GC state in the base summary. This is returned in\n // getDataStoreBaseGCDetails and is used to initialize each data store's base GC details.\n this.dataStoreGCDetailsP = new LazyPromise<Map<string, IGarbageCollectionSummaryDetails>>(async () => {\n const baseState = await baseSummaryStateP;\n if (baseState === undefined) {\n return new Map();\n }\n\n const gcNodes: { [ id: string ]: string[] } = {};\n for (const [nodeId, nodeData] of Object.entries(baseState.gcNodes)) {\n gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);\n }\n // Run GC on the nodes in the base summary to get the routes used in each node in the container.\n // This is an optimization for space (vs performance) wherein we don't need to store the used routes of\n // each node in the summary.\n const usedRoutes = runGarbageCollection(\n gcNodes,\n [ \"/\" ],\n this.mc.logger,\n ).referencedNodeIds;\n\n const dataStoreGCDetailsMap = unpackChildNodesGCDetails({ gcData: { gcNodes }, usedRoutes });\n // Currently, the data stores write the GC data. So, we need to update it's base GC details with the\n // unreferenced timestamp. Once we start writing the GC data here, we won't need to do this anymore.\n for (const [nodeId, nodeData] of Object.entries(baseState.gcNodes)) {\n if (nodeData.unreferencedTimestampMs !== undefined) {\n const dataStoreGCDetails = dataStoreGCDetailsMap.get(nodeId.slice(1));\n if (dataStoreGCDetails !== undefined) {\n dataStoreGCDetails.unrefTimestamp = nodeData.unreferencedTimestampMs;\n }\n }\n }\n return dataStoreGCDetailsMap;\n });\n }\n\n /**\n * Runs garbage collection and udpates the reference / used state of the nodes in the container.\n * @returns the number of data stores that have been marked as unreferenced.\n */\n public async collectGarbage(\n options: {\n /** Logger to use for logging GC events */\n logger?: ITelemetryLogger,\n /** True to run GC sweep phase after the mark phase */\n runSweep?: boolean,\n /** True to generate full GC data */\n fullGC?: boolean,\n },\n ): Promise<IGCStats> {\n const {\n logger = this.mc.logger,\n runSweep = this.shouldRunSweep,\n fullGC = this.gcOptions.runFullGC === true || this.hasGCVersionChanged,\n } = options;\n\n return PerformanceEvent.timedExecAsync(logger, { eventName: \"GarbageCollection\" }, async (event) => {\n await this.initializeBaseStateP;\n\n const gcStats: {\n deletedNodes?: number,\n totalNodes?: number,\n deletedDataStores?: number,\n totalDataStores?: number,\n } = {};\n\n // Get the runtime's GC data and run GC on the reference graph in it.\n const gcData = await this.provider.getGCData(fullGC);\n\n this.updateStateSinceLatestRun(gcData);\n\n const gcResult = runGarbageCollection(\n gcData.gcNodes,\n [ \"/\" ],\n logger,\n );\n\n const currentTimestampMs = this.getCurrentTimestampMs();\n // Update the current state of the system based on the GC run.\n this.updateCurrentState(gcData, gcResult, currentTimestampMs);\n\n const dataStoreUsedStateStats =\n this.provider.updateUsedRoutes(gcResult.referencedNodeIds, currentTimestampMs);\n\n if (runSweep) {\n // Placeholder for running sweep logic.\n }\n\n // Update stats to be reported in the peformance event.\n gcStats.deletedNodes = gcResult.deletedNodeIds.length;\n gcStats.totalNodes = gcResult.referencedNodeIds.length + gcResult.deletedNodeIds.length;\n gcStats.deletedDataStores = dataStoreUsedStateStats.unusedNodeCount;\n gcStats.totalDataStores = dataStoreUsedStateStats.totalNodeCount;\n\n // If we are running in GC test mode, delete objects for unused routes. This enables testing scenarios\n // involving access to deleted data.\n if (this.testMode) {\n this.deleteUnusedRoutes(gcResult.deletedNodeIds);\n }\n event.end(gcStats);\n return gcStats as IGCStats;\n },\n { end: true, cancel: \"error\" });\n }\n\n /**\n * Summarizes the GC data and returns it as a summary tree.\n * We current write the entire GC state in a single blob. This can be modified later to write multiple\n * blobs. All the blob keys should start with `gcBlobPrefix`.\n */\n public summarize(): ISummaryTreeWithStats | undefined {\n if (!this.shouldRunGC || this.gcDataFromLastRun === undefined) {\n return;\n }\n\n const gcState: IGarbageCollectionState = { gcNodes: {} };\n for (const [nodeId, outboundRoutes] of Object.entries(this.gcDataFromLastRun.gcNodes)) {\n gcState.gcNodes[nodeId] = {\n outboundRoutes,\n unreferencedTimestampMs: this.unreferencedNodesState.get(nodeId)?.unreferencedTimestampMs,\n };\n }\n\n const builder = new SummaryTreeBuilder();\n builder.addBlob(`${gcBlobPrefix}_root`, JSON.stringify(gcState));\n return builder.getSummaryTree();\n }\n\n /**\n * Returns a map of data store ids to their base GC details generated from the base summary.This is used to\n * initialize the data stores with their base GC state.\n */\n public async getDataStoreBaseGCDetails(): Promise<Map<string, IGarbageCollectionSummaryDetails>> {\n return this.dataStoreGCDetailsP;\n }\n\n /**\n * Called when the latest summary of the system has been refreshed. This will be used to update the state of the\n * latest summary tracked.\n */\n public async latestSummaryStateRefreshed(\n result: RefreshSummaryResult,\n readAndParseBlob: ReadAndParseBlob,\n ): Promise<void> {\n if (!this.shouldRunGC || !result.latestSummaryUpdated) {\n return;\n }\n\n // If the summary was tracked by this client, it was the one that generated the summary in the first place.\n // Basically, it was written in the current GC version.\n if (result.wasSummaryTracked) {\n this.latestSummaryGCVersion = this.currentGCVersion;\n return;\n }\n // If the summary was not tracked by this client, update latest GC version from the snapshot in the result as\n // that is now the latest summary.\n await this.updateSummaryGCVersionFromSnapshot(result.snapshot, readAndParseBlob);\n }\n\n /**\n * Called when a node with the given id is changed. If the node is inactive, log an error.\n */\n public nodeChanged(id: string) {\n // Prefix \"/\" if needed to make it relative to the root.\n const nodeId = id.startsWith(\"/\") ? id : `/${id}`;\n this.unreferencedNodesState.get(nodeId)?.logIfInactive(\n this.mc.logger,\n \"inactiveObjectChanged\",\n this.getCurrentTimestampMs(),\n this.deleteTimeoutMs,\n nodeId,\n );\n }\n\n /**\n * Called when an outbound reference is added to a node. This is used to identify all nodes that have been\n * referenced between summaries so that their unreferenced timestamp can be reset.\n *\n * @param fromNodeId - The node from which the reference is added.\n * @param toNodeId - The node to which the reference is added.\n */\n public addedOutboundReference(fromNodeId: string, toNodeId: string) {\n const outboundRoutes = this.referencesSinceLastRun.get(fromNodeId) ?? [];\n outboundRoutes.push(toNodeId);\n this.referencesSinceLastRun.set(fromNodeId, outboundRoutes);\n }\n\n /**\n * Update the latest summary GC version from the metadata blob in the given snapshot.\n */\n private async updateSummaryGCVersionFromSnapshot(snapshot: ISnapshotTree, readAndParseBlob: ReadAndParseBlob) {\n const metadataBlobId = snapshot.blobs[metadataBlobName];\n if (metadataBlobId) {\n const metadata = await readAndParseBlob<IContainerRuntimeMetadata>(metadataBlobId);\n this.latestSummaryGCVersion = getGCVersion(metadata);\n }\n }\n\n /**\n * Updates the state of the system as per the current GC run. It does the following:\n * 1. Sets up the current GC state as per the gcData.\n * 2. Starts tracking for nodes that have become unreferenced in this run.\n * 3. Clears tracking for nodes that were unreferenced but became referenced in this run.\n * @param gcData - The data representing the reference graph on which GC is run.\n * @param gcResult - The result of the GC run on the gcData.\n * @param currentTimestampMs - The current timestamp to be used for unreferenced nodes' timestamp.\n */\n private updateCurrentState(gcData: IGarbageCollectionData, gcResult: IGCResult, currentTimestampMs: number) {\n this.gcDataFromLastRun = cloneGCData(gcData);\n this.referencesSinceLastRun.clear();\n\n // Iterate through the deleted nodes and start tracking if they became unreferenced in this run.\n for (const nodeId of gcResult.deletedNodeIds) {\n // The time when the node became unreferenced. This is added to the current GC state.\n let unreferencedTimestampMs: number = currentTimestampMs;\n const nodeStateTracker = this.unreferencedNodesState.get(nodeId);\n if (nodeStateTracker !== undefined) {\n unreferencedTimestampMs = nodeStateTracker.unreferencedTimestampMs;\n } else {\n // Start tracking this node as it became unreferenced in this run.\n this.unreferencedNodesState.set(\n nodeId,\n new UnreferencedStateTracker(unreferencedTimestampMs, this.deleteTimeoutMs),\n );\n }\n }\n\n // Iterate through the referenced nodes and stop tracking if they were unreferenced before.\n for (const nodeId of gcResult.referencedNodeIds) {\n const nodeStateTracker = this.unreferencedNodesState.get(nodeId);\n if (nodeStateTracker !== undefined) {\n // If this node has been unreferenced for longer than deleteTimeoutMs and is being referenced,\n // log an error as this may mean the deleteTimeoutMs is not long enough.\n nodeStateTracker.logIfInactive(\n this.mc.logger,\n \"inactiveObjectRevived\",\n currentTimestampMs,\n this.deleteTimeoutMs,\n nodeId,\n );\n // Stop tracking so as to clear out any running timers.\n nodeStateTracker.stopTracking();\n // Delete the node as we don't need to track it any more.\n this.unreferencedNodesState.delete(nodeId);\n }\n }\n }\n\n /**\n * Since GC runs periodically, the GC data that is generated only tells us the state of the world at that point in\n * time. It's possible that nodes transition from `unreferenced -> referenced -> unreferenced` between two runs. The\n * unreferenced timestamp of such nodes needs to be reset as they may have been accessed when they were referenced.\n *\n * This function identifies nodes that were referenced since last run and removes their unreferenced state, if any.\n * If these nodes are currently unreferenced, they will be assigned new unreferenced state by the current run.\n */\n private updateStateSinceLatestRun(currentGCData: IGarbageCollectionData) {\n // If we haven't run GC before or no references were added since the last run, there is nothing to do.\n if (this.gcDataFromLastRun === undefined || this.referencesSinceLastRun.size === 0) {\n return;\n }\n\n /**\n * Generate a super set of the GC data that contains the nodes and edges from last run, plus any new node and\n * edges that have been added since then. To do this, combine the GC data from the last run and the current\n * run, and then add the references since last run.\n *\n * Note on why we need to combine the data from previous run, current run and all references in between -\n * 1. We need data from last run because some of its references may have been deleted since then. If those\n * references added new outbound references before getting deleted, we need to detect them.\n * 2. We need new outbound references since last run because some of them may have been deleted later. If those\n * references added new outbound references before getting deleted, we need to detect them.\n * 3. We need data from the current run because currently we may not detect when DDSs are referenced:\n * - We don't require DDSs handles to be stored in a referenced DDS. For this, we need GC at DDS level\n * which is tracked by https://github.com/microsoft/FluidFramework/issues/8470.\n * - A new data store may have \"root\" DDSs already created and we don't detect them today.\n */\n const gcDataSuperSet = concatGarbageCollectionData(this.gcDataFromLastRun, currentGCData);\n this.referencesSinceLastRun.forEach((outboundRoutes: string[], sourceNodeId: string) => {\n if (gcDataSuperSet.gcNodes[sourceNodeId] === undefined) {\n gcDataSuperSet.gcNodes[sourceNodeId] = outboundRoutes;\n } else {\n gcDataSuperSet.gcNodes[sourceNodeId].push(...outboundRoutes);\n }\n });\n\n /**\n * Run GC on the above reference graph to find all nodes that are referenced. For each one, if they are\n * unreferenced, stop tracking them and remove from unreferenced list.\n * Some of these nodes may be unreferenced now and if so, the current run will add unreferenced state for them.\n */\n const gcResult = runGarbageCollection(gcDataSuperSet.gcNodes, [\"/\"], this.mc.logger);\n for (const nodeId of gcResult.referencedNodeIds) {\n const nodeStateTracker = this.unreferencedNodesState.get(nodeId);\n if (nodeStateTracker !== undefined) {\n // Stop tracking so as to clear out any running timers.\n nodeStateTracker.stopTracking();\n // Delete the node as we don't need to track it any more.\n this.unreferencedNodesState.delete(nodeId);\n }\n }\n }\n}\n\n/**\n * Gets the garbage collection state from the given snapshot tree. The GC state may be written into multiple blobs.\n * Merge the GC state from all such blobs and return the merged GC state.\n*/\nasync function getGCStateFromSnapshot(\n gcSnapshotTree: ISnapshotTree,\n readAndParseBlob: ReadAndParseBlob,\n): Promise<IGarbageCollectionState> {\n let rootGCState: IGarbageCollectionState = { gcNodes: {} };\n for (const key of Object.keys(gcSnapshotTree.blobs)) {\n // Skip blobs that do not stsart with the GC prefix.\n if (!key.startsWith(gcBlobPrefix)) {\n continue;\n }\n\n const blobId = gcSnapshotTree.blobs[key];\n if (blobId === undefined) {\n continue;\n }\n const gcState = await readAndParseBlob<IGarbageCollectionState>(blobId);\n assert(gcState !== undefined, 0x2ad /* \"GC blob missing from snapshot\" */);\n // Merge the GC state of this blob into the root GC state.\n rootGCState = concatGarbageCollectionStates(rootGCState, gcState);\n }\n return rootGCState;\n}\n"]}