@fluidframework/container-runtime 2.0.0-internal.3.4.2 → 2.0.0-internal.4.0.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 (137) hide show
  1. package/dist/containerRuntime.d.ts +1 -5
  2. package/dist/containerRuntime.d.ts.map +1 -1
  3. package/dist/containerRuntime.js +12 -16
  4. package/dist/containerRuntime.js.map +1 -1
  5. package/dist/gc/garbageCollection.d.ts +2 -1
  6. package/dist/gc/garbageCollection.d.ts.map +1 -1
  7. package/dist/gc/garbageCollection.js.map +1 -1
  8. package/dist/gc/gcDefinitions.d.ts +2 -3
  9. package/dist/gc/gcDefinitions.d.ts.map +1 -1
  10. package/dist/gc/gcDefinitions.js.map +1 -1
  11. package/dist/gc/gcSummaryStateTracker.d.ts +2 -1
  12. package/dist/gc/gcSummaryStateTracker.d.ts.map +1 -1
  13. package/dist/gc/gcSummaryStateTracker.js.map +1 -1
  14. package/dist/gc/index.d.ts +1 -2
  15. package/dist/gc/index.d.ts.map +1 -1
  16. package/dist/gc/index.js.map +1 -1
  17. package/dist/index.d.ts +1 -2
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/packageVersion.d.ts +1 -1
  22. package/dist/packageVersion.js +1 -1
  23. package/dist/packageVersion.js.map +1 -1
  24. package/dist/pendingStateManager.d.ts +2 -8
  25. package/dist/pendingStateManager.d.ts.map +1 -1
  26. package/dist/pendingStateManager.js +6 -12
  27. package/dist/pendingStateManager.js.map +1 -1
  28. package/dist/summary/index.d.ts +2 -2
  29. package/dist/summary/index.d.ts.map +1 -1
  30. package/dist/summary/index.js +4 -5
  31. package/dist/summary/index.js.map +1 -1
  32. package/dist/summary/summarizer.d.ts +2 -5
  33. package/dist/summary/summarizer.d.ts.map +1 -1
  34. package/dist/summary/summarizer.js +8 -13
  35. package/dist/summary/summarizer.js.map +1 -1
  36. package/dist/summary/summarizerNode/index.d.ts +8 -0
  37. package/dist/summary/summarizerNode/index.d.ts.map +1 -0
  38. package/dist/summary/summarizerNode/index.js +12 -0
  39. package/dist/summary/summarizerNode/index.js.map +1 -0
  40. package/dist/summary/summarizerNode/summarizerNode.d.ts +149 -0
  41. package/dist/summary/summarizerNode/summarizerNode.d.ts.map +1 -0
  42. package/dist/summary/summarizerNode/summarizerNode.js +520 -0
  43. package/dist/summary/summarizerNode/summarizerNode.js.map +1 -0
  44. package/dist/summary/summarizerNode/summarizerNodeUtils.d.ts +125 -0
  45. package/dist/summary/summarizerNode/summarizerNodeUtils.d.ts.map +1 -0
  46. package/dist/summary/summarizerNode/summarizerNodeUtils.js +132 -0
  47. package/dist/summary/summarizerNode/summarizerNodeUtils.js.map +1 -0
  48. package/dist/summary/summarizerNode/summarizerNodeWithGc.d.ts +22 -0
  49. package/dist/summary/summarizerNode/summarizerNodeWithGc.d.ts.map +1 -0
  50. package/dist/summary/summarizerNode/summarizerNodeWithGc.js +420 -0
  51. package/dist/summary/summarizerNode/summarizerNodeWithGc.js.map +1 -0
  52. package/dist/summary/summarizerTypes.d.ts +5 -17
  53. package/dist/summary/summarizerTypes.d.ts.map +1 -1
  54. package/dist/summary/summarizerTypes.js +0 -5
  55. package/dist/summary/summarizerTypes.js.map +1 -1
  56. package/lib/containerRuntime.d.ts +1 -5
  57. package/lib/containerRuntime.d.ts.map +1 -1
  58. package/lib/containerRuntime.js +13 -17
  59. package/lib/containerRuntime.js.map +1 -1
  60. package/lib/gc/garbageCollection.d.ts +2 -1
  61. package/lib/gc/garbageCollection.d.ts.map +1 -1
  62. package/lib/gc/garbageCollection.js +1 -1
  63. package/lib/gc/garbageCollection.js.map +1 -1
  64. package/lib/gc/gcDefinitions.d.ts +2 -3
  65. package/lib/gc/gcDefinitions.d.ts.map +1 -1
  66. package/lib/gc/gcDefinitions.js.map +1 -1
  67. package/lib/gc/gcSummaryStateTracker.d.ts +2 -1
  68. package/lib/gc/gcSummaryStateTracker.d.ts.map +1 -1
  69. package/lib/gc/gcSummaryStateTracker.js +1 -1
  70. package/lib/gc/gcSummaryStateTracker.js.map +1 -1
  71. package/lib/gc/index.d.ts +1 -2
  72. package/lib/gc/index.d.ts.map +1 -1
  73. package/lib/gc/index.js.map +1 -1
  74. package/lib/index.d.ts +1 -2
  75. package/lib/index.d.ts.map +1 -1
  76. package/lib/index.js +1 -1
  77. package/lib/index.js.map +1 -1
  78. package/lib/packageVersion.d.ts +1 -1
  79. package/lib/packageVersion.js +1 -1
  80. package/lib/packageVersion.js.map +1 -1
  81. package/lib/pendingStateManager.d.ts +2 -8
  82. package/lib/pendingStateManager.d.ts.map +1 -1
  83. package/lib/pendingStateManager.js +6 -12
  84. package/lib/pendingStateManager.js.map +1 -1
  85. package/lib/summary/index.d.ts +2 -2
  86. package/lib/summary/index.d.ts.map +1 -1
  87. package/lib/summary/index.js +1 -2
  88. package/lib/summary/index.js.map +1 -1
  89. package/lib/summary/summarizer.d.ts +2 -5
  90. package/lib/summary/summarizer.d.ts.map +1 -1
  91. package/lib/summary/summarizer.js +8 -13
  92. package/lib/summary/summarizer.js.map +1 -1
  93. package/lib/summary/summarizerNode/index.d.ts +8 -0
  94. package/lib/summary/summarizerNode/index.d.ts.map +1 -0
  95. package/lib/summary/summarizerNode/index.js +7 -0
  96. package/lib/summary/summarizerNode/index.js.map +1 -0
  97. package/lib/summary/summarizerNode/summarizerNode.d.ts +149 -0
  98. package/lib/summary/summarizerNode/summarizerNode.d.ts.map +1 -0
  99. package/lib/summary/summarizerNode/summarizerNode.js +515 -0
  100. package/lib/summary/summarizerNode/summarizerNode.js.map +1 -0
  101. package/lib/summary/summarizerNode/summarizerNodeUtils.d.ts +125 -0
  102. package/lib/summary/summarizerNode/summarizerNodeUtils.d.ts.map +1 -0
  103. package/lib/summary/summarizerNode/summarizerNodeUtils.js +125 -0
  104. package/lib/summary/summarizerNode/summarizerNodeUtils.js.map +1 -0
  105. package/lib/summary/summarizerNode/summarizerNodeWithGc.d.ts +22 -0
  106. package/lib/summary/summarizerNode/summarizerNodeWithGc.d.ts.map +1 -0
  107. package/lib/summary/summarizerNode/summarizerNodeWithGc.js +416 -0
  108. package/lib/summary/summarizerNode/summarizerNodeWithGc.js.map +1 -0
  109. package/lib/summary/summarizerTypes.d.ts +5 -17
  110. package/lib/summary/summarizerTypes.d.ts.map +1 -1
  111. package/lib/summary/summarizerTypes.js +1 -4
  112. package/lib/summary/summarizerTypes.js.map +1 -1
  113. package/package.json +52 -15
  114. package/src/containerRuntime.ts +19 -19
  115. package/src/gc/garbageCollection.ts +2 -6
  116. package/src/gc/gcDefinitions.ts +6 -4
  117. package/src/gc/gcSummaryStateTracker.ts +2 -7
  118. package/src/gc/index.ts +1 -1
  119. package/src/index.ts +0 -7
  120. package/src/packageVersion.ts +1 -1
  121. package/src/pendingStateManager.ts +8 -20
  122. package/src/summary/index.ts +8 -2
  123. package/src/summary/summarizer.ts +7 -21
  124. package/src/summary/summarizerNode/index.ts +12 -0
  125. package/src/summary/summarizerNode/summarizerNode.ts +744 -0
  126. package/src/summary/summarizerNode/summarizerNodeUtils.ts +214 -0
  127. package/src/summary/summarizerNode/summarizerNodeWithGc.ts +631 -0
  128. package/src/summary/summarizerTypes.ts +6 -22
  129. package/dist/summary/summarizerHandle.d.ts +0 -12
  130. package/dist/summary/summarizerHandle.d.ts.map +0 -1
  131. package/dist/summary/summarizerHandle.js +0 -22
  132. package/dist/summary/summarizerHandle.js.map +0 -1
  133. package/lib/summary/summarizerHandle.d.ts +0 -12
  134. package/lib/summary/summarizerHandle.d.ts.map +0 -1
  135. package/lib/summary/summarizerHandle.js +0 -18
  136. package/lib/summary/summarizerHandle.js.map +0 -1
  137. package/src/summary/summarizerHandle.ts +0 -23
@@ -0,0 +1,631 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { ITelemetryLogger } from "@fluidframework/common-definitions";
7
+ import { assert, LazyPromise } from "@fluidframework/common-utils";
8
+ import {
9
+ cloneGCData,
10
+ getGCDataFromSnapshot,
11
+ runGarbageCollection,
12
+ unpackChildNodesGCDetails,
13
+ unpackChildNodesUsedRoutes,
14
+ } from "@fluidframework/garbage-collector";
15
+ import { ISnapshotTree } from "@fluidframework/protocol-definitions";
16
+ import {
17
+ CreateChildSummarizerNodeParam,
18
+ gcTreeKey,
19
+ IGarbageCollectionData,
20
+ IGarbageCollectionDetailsBase,
21
+ ISummarizeInternalResult,
22
+ ISummarizeResult,
23
+ ISummarizerNodeConfigWithGC,
24
+ ISummarizerNodeWithGC,
25
+ SummarizeInternalFn,
26
+ ITelemetryContext,
27
+ } from "@fluidframework/runtime-definitions";
28
+ import { LoggingError, TelemetryDataTag } from "@fluidframework/telemetry-utils";
29
+ import { ReadAndParseBlob } from "@fluidframework/runtime-utils";
30
+ import { SummarizerNode } from "./summarizerNode";
31
+ import {
32
+ EscapedPath,
33
+ ICreateChildDetails,
34
+ IInitialSummary,
35
+ ISummarizerNodeRootContract,
36
+ parseSummaryForSubtrees,
37
+ SummaryNode,
38
+ } from "./summarizerNodeUtils";
39
+
40
+ export interface IRootSummarizerNodeWithGC
41
+ extends ISummarizerNodeWithGC,
42
+ ISummarizerNodeRootContract {}
43
+
44
+ // Extend SummaryNode to add used routes tracking to it.
45
+ class SummaryNodeWithGC extends SummaryNode {
46
+ constructor(
47
+ public readonly serializedUsedRoutes: string,
48
+ summary: {
49
+ readonly referenceSequenceNumber: number;
50
+ readonly basePath: EscapedPath | undefined;
51
+ readonly localPath: EscapedPath;
52
+ additionalPath?: EscapedPath;
53
+ },
54
+ ) {
55
+ super(summary);
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Extends the functionality of SummarizerNode to manage this node's garbage collection data:
61
+ *
62
+ * - Adds a new API `getGCData` to return GC data of this node.
63
+ *
64
+ * - Caches the result of `getGCData` to be used if nothing changes between summaries.
65
+ *
66
+ * - Manages the used routes of this node. These are used to identify if this node is referenced in the document
67
+ * and to determine if the node's used state changed since last summary.
68
+ *
69
+ * - Adds trackState param to summarize. If trackState is false, it bypasses the SummarizerNode and calls
70
+ * directly into summarizeInternal method.
71
+ */
72
+ class SummarizerNodeWithGC extends SummarizerNode implements IRootSummarizerNodeWithGC {
73
+ // Tracks the work-in-progress used routes during summary.
74
+ private wipSerializedUsedRoutes: string | undefined;
75
+
76
+ // This is the last known used routes of this node as seen by the server as part of a summary.
77
+ private referenceUsedRoutes: string[] | undefined;
78
+
79
+ // The base GC details of this node used to initialize the GC state.
80
+ private readonly baseGCDetailsP: LazyPromise<IGarbageCollectionDetailsBase>;
81
+
82
+ // Keeps track of whether we have loaded the base details to ensure that we only do it once.
83
+ private baseGCDetailsLoaded: boolean = false;
84
+
85
+ // The base GC details for the child nodes. This is passed to child nodes when creating them.
86
+ private readonly childNodesBaseGCDetailsP: LazyPromise<
87
+ Map<string, IGarbageCollectionDetailsBase>
88
+ >;
89
+
90
+ private gcData: IGarbageCollectionData | undefined;
91
+
92
+ // Set used routes to have self route by default. This makes the node referenced by default. This is done to ensure
93
+ // that this node is not marked as collected when running GC has been disabled. Once, the option to disable GC is
94
+ // removed (from runGC flag in IContainerRuntimeOptions), this should be changed to be have no routes by default.
95
+ private usedRoutes: string[] = [""];
96
+
97
+ // True if GC is disabled for this node. If so, do not track GC specific state for a summary.
98
+ private readonly gcDisabled: boolean;
99
+
100
+ /**
101
+ * Do not call constructor directly.
102
+ * Use createRootSummarizerNodeWithGC to create root node, or createChild to create child nodes.
103
+ */
104
+ public constructor(
105
+ logger: ITelemetryLogger,
106
+ private readonly summarizeFn: (
107
+ fullTree: boolean,
108
+ trackState: boolean,
109
+ telemetryContext?: ITelemetryContext,
110
+ ) => Promise<ISummarizeInternalResult>,
111
+ config: ISummarizerNodeConfigWithGC,
112
+ changeSequenceNumber: number,
113
+ /** Undefined means created without summary */
114
+ latestSummary?: SummaryNode,
115
+ initialSummary?: IInitialSummary,
116
+ wipSummaryLogger?: ITelemetryLogger,
117
+ private readonly getGCDataFn?: (fullGC?: boolean) => Promise<IGarbageCollectionData>,
118
+ getBaseGCDetailsFn?: () => Promise<IGarbageCollectionDetailsBase>,
119
+ /** A unique id of this node to be logged when sending telemetry. */
120
+ telemetryId?: string,
121
+ ) {
122
+ super(
123
+ logger,
124
+ async (fullTree: boolean, _trackState: boolean, telemetryContext?: ITelemetryContext) =>
125
+ summarizeFn(fullTree, true /* trackState */, telemetryContext),
126
+ config,
127
+ changeSequenceNumber,
128
+ latestSummary,
129
+ initialSummary,
130
+ wipSummaryLogger,
131
+ telemetryId,
132
+ );
133
+
134
+ this.gcDisabled = config.gcDisabled === true;
135
+
136
+ this.baseGCDetailsP = new LazyPromise(async () => {
137
+ return (await getBaseGCDetailsFn?.()) ?? { usedRoutes: [] };
138
+ });
139
+
140
+ this.childNodesBaseGCDetailsP = new LazyPromise(async () => {
141
+ await this.loadBaseGCDetails();
142
+ return unpackChildNodesGCDetails({ gcData: this.gcData, usedRoutes: this.usedRoutes });
143
+ });
144
+ }
145
+
146
+ /**
147
+ * Loads state from this node's initial GC summary details. This contains the following data from the last summary
148
+ * seen by the server for this client:
149
+ * - usedRoutes: This is used to figure out if the used state of this node changed since last summary.
150
+ * - gcData: The garbage collection data of this node that is required for running GC.
151
+ */
152
+ private async loadBaseGCDetails() {
153
+ if (this.baseGCDetailsLoaded) {
154
+ return;
155
+ }
156
+ const baseGCDetails = await this.baseGCDetailsP;
157
+
158
+ // Possible race - If there were parallel calls to loadBaseGCDetails, we want to make sure that we update
159
+ // the state from the base details only once.
160
+ if (this.baseGCDetailsLoaded) {
161
+ return;
162
+ }
163
+ this.baseGCDetailsLoaded = true;
164
+
165
+ // Update GC data, used routes and reference used routes. The used routes are sorted because they are compared
166
+ // across GC runs to check if they changed. Sorting ensures that the elements are in the same order.
167
+ // If the GC details has GC data, initialize our GC data from it.
168
+ if (baseGCDetails.gcData !== undefined) {
169
+ this.gcData = cloneGCData(baseGCDetails.gcData);
170
+ }
171
+ if (baseGCDetails.usedRoutes !== undefined) {
172
+ this.usedRoutes = Array.from(baseGCDetails.usedRoutes).sort();
173
+ this.referenceUsedRoutes = Array.from(baseGCDetails.usedRoutes).sort();
174
+ }
175
+ }
176
+
177
+ public async summarize(
178
+ fullTree: boolean,
179
+ trackState: boolean = true,
180
+ telemetryContext?: ITelemetryContext,
181
+ ): Promise<ISummarizeResult> {
182
+ // If GC is not disabled and a summary is in progress, GC should have run and updated the used routes for this
183
+ // summary by calling updateUsedRoutes which sets wipSerializedUsedRoutes.
184
+ if (!this.gcDisabled && this.isSummaryInProgress()) {
185
+ assert(
186
+ this.wipSerializedUsedRoutes !== undefined,
187
+ 0x1b1 /* "wip used routes should be set if tracking a summary" */,
188
+ );
189
+ }
190
+
191
+ // If trackState is true, get summary from base summarizer node which tracks summary state.
192
+ // If trackState is false, get summary from summarizeInternal.
193
+ return trackState
194
+ ? super.summarize(fullTree, true /* trackState */, telemetryContext)
195
+ : this.summarizeFn(fullTree, trackState, telemetryContext);
196
+ }
197
+
198
+ /**
199
+ * Returns the GC data of this node. If nothing has changed since last summary, it tries to reuse the data from
200
+ * the previous summary. Else, it gets new GC data from the underlying Fluid object.
201
+ * @param fullGC - true to bypass optimizations and force full generation of GC data.
202
+ */
203
+ public async getGCData(fullGC: boolean = false): Promise<IGarbageCollectionData> {
204
+ assert(
205
+ !this.gcDisabled,
206
+ 0x1b2 /* "Getting GC data should not be called when GC is disabled!" */,
207
+ );
208
+ assert(
209
+ this.getGCDataFn !== undefined,
210
+ 0x1b3 /* "GC data cannot be retrieved without getGCDataFn" */,
211
+ );
212
+
213
+ // Load GC details from the initial summary, if not already loaded. If this is the first time this function is
214
+ // called and the node's data has not changed since last summary, the GC data in initial details is returned.
215
+ await this.loadBaseGCDetails();
216
+
217
+ // If there is no new data since last summary and we have GC data from the previous run, return it. The previous
218
+ // GC data may not be available if loaded from a snapshot with either GC disabled or before GC was added.
219
+ // Note - canReuseHandle is checked to be consistent with summarize - generate GC data for nodes for which
220
+ // summary must be generated.
221
+ if (this.canReuseHandle && !fullGC && !this.hasDataChanged() && this.gcData !== undefined) {
222
+ return cloneGCData(this.gcData);
223
+ }
224
+
225
+ const gcData = await this.getGCDataFn(fullGC);
226
+ this.gcData = cloneGCData(gcData);
227
+ return gcData;
228
+ }
229
+
230
+ /**
231
+ * Called during the start of a summary. Updates the work-in-progress used routes.
232
+ */
233
+ public startSummary(referenceSequenceNumber: number, summaryLogger: ITelemetryLogger) {
234
+ // If GC is disabled, skip setting wip used routes since we should not track GC state.
235
+ if (!this.gcDisabled) {
236
+ assert(
237
+ this.wipSerializedUsedRoutes === undefined,
238
+ 0x1b4 /* "We should not already be tracking used routes when to track a new summary" */,
239
+ );
240
+ }
241
+ super.startSummary(referenceSequenceNumber, summaryLogger);
242
+ }
243
+
244
+ /**
245
+ * Called after summary has been uploaded to the server. Add the work-in-progress state to the pending
246
+ * summary queue. We track this until we get an ack from the server for this summary.
247
+ */
248
+ protected completeSummaryCore(
249
+ proposalHandle: string,
250
+ parentPath: EscapedPath | undefined,
251
+ parentSkipRecursion: boolean,
252
+ ) {
253
+ let wipSerializedUsedRoutes: string | undefined;
254
+ // If GC is disabled, don't set wip used routes.
255
+ if (!this.gcDisabled) {
256
+ wipSerializedUsedRoutes = this.wipSerializedUsedRoutes;
257
+ /**
258
+ * The absence of wip used routes indicates that GC was not run on this node. This can happen if:
259
+ * 1. A child node was created after GC was already run on the parent. For example, a data store
260
+ * is realized (loaded) after GC was run on it creating summarizer nodes for its DDSes. In this
261
+ * case, the used routes of the parent should be passed on the child nodes and it should be fine.
262
+ * 2. A new node was created but GC was never run on it. This can mean that the GC data generated
263
+ * during summarize is complete . We should not continue, log and throw an error. This will help us
264
+ * identify these cases and take appropriate action.
265
+ */
266
+ if (wipSerializedUsedRoutes === undefined) {
267
+ this.throwUnexpectedError({
268
+ eventName: "NodeDidNotRunGC",
269
+ proposalHandle,
270
+ });
271
+ }
272
+ }
273
+
274
+ super.completeSummaryCore(proposalHandle, parentPath, parentSkipRecursion);
275
+
276
+ // If GC is disabled, skip setting pending summary with GC state.
277
+ if (!this.gcDisabled) {
278
+ const summaryNode = this.pendingSummaries.get(proposalHandle);
279
+ if (summaryNode !== undefined) {
280
+ const summaryNodeWithGC = new SummaryNodeWithGC(
281
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
282
+ wipSerializedUsedRoutes!,
283
+ summaryNode,
284
+ );
285
+ this.pendingSummaries.set(proposalHandle, summaryNodeWithGC);
286
+ }
287
+ }
288
+ }
289
+
290
+ /**
291
+ * Clears the work-in-progress state.
292
+ */
293
+ public clearSummary() {
294
+ this.wipSerializedUsedRoutes = undefined;
295
+ super.clearSummary();
296
+ }
297
+
298
+ /**
299
+ * Called when we get an ack from the server for a summary we sent. Update the reference state of this node
300
+ * from the state in the pending summary queue.
301
+ */
302
+ protected refreshLatestSummaryFromPending(
303
+ proposalHandle: string,
304
+ referenceSequenceNumber: number,
305
+ ): void {
306
+ // If GC is disabled, skip setting referenced used routes since we are not tracking GC state.
307
+ if (!this.gcDisabled) {
308
+ const summaryNode = this.pendingSummaries.get(proposalHandle);
309
+ if (summaryNode !== undefined) {
310
+ // If a pending summary exists, it must have used routes since GC is enabled.
311
+ const summaryNodeWithGC = summaryNode as SummaryNodeWithGC;
312
+ if (summaryNodeWithGC.serializedUsedRoutes === undefined) {
313
+ const error = new LoggingError("MissingGCStateInPendingSummary", {
314
+ proposalHandle,
315
+ referenceSequenceNumber,
316
+ id: {
317
+ tag: TelemetryDataTag.CodeArtifact,
318
+ value: this.telemetryNodeId,
319
+ },
320
+ });
321
+ this.logger.sendErrorEvent(
322
+ {
323
+ eventName: error.message,
324
+ },
325
+ error,
326
+ );
327
+ throw error;
328
+ }
329
+ this.referenceUsedRoutes = JSON.parse(summaryNodeWithGC.serializedUsedRoutes);
330
+ }
331
+ }
332
+
333
+ return super.refreshLatestSummaryFromPending(proposalHandle, referenceSequenceNumber);
334
+ }
335
+
336
+ /**
337
+ * Called when we need to upload the reference state from the given summary.
338
+ */
339
+ protected async refreshLatestSummaryFromSnapshot(
340
+ referenceSequenceNumber: number,
341
+ snapshotTree: ISnapshotTree,
342
+ basePath: EscapedPath | undefined,
343
+ localPath: EscapedPath,
344
+ correlatedSummaryLogger: ITelemetryLogger,
345
+ readAndParseBlob: ReadAndParseBlob,
346
+ ): Promise<void> {
347
+ await this.refreshGCStateFromSnapshot(
348
+ referenceSequenceNumber,
349
+ snapshotTree,
350
+ readAndParseBlob,
351
+ );
352
+ return super.refreshLatestSummaryFromSnapshot(
353
+ referenceSequenceNumber,
354
+ snapshotTree,
355
+ basePath,
356
+ localPath,
357
+ correlatedSummaryLogger,
358
+ readAndParseBlob,
359
+ );
360
+ }
361
+
362
+ /**
363
+ * Updates GC state from the given snapshot if GC is enabled and the snapshot is newer than the one this node
364
+ * is tracking.
365
+ */
366
+ private async refreshGCStateFromSnapshot(
367
+ referenceSequenceNumber: number,
368
+ snapshotTree: ISnapshotTree,
369
+ readAndParseBlob: ReadAndParseBlob,
370
+ ): Promise<void> {
371
+ // If GC is disabled or we have seen a newer summary, skip updating GC state.
372
+ if (this.gcDisabled || this.referenceSequenceNumber >= referenceSequenceNumber) {
373
+ return;
374
+ }
375
+
376
+ // Load the base GC details before proceeding because if that happens later it can overwrite the GC details
377
+ // written by the following code.
378
+ await this.loadBaseGCDetails();
379
+
380
+ // Possible re-entrancy. We may already have processed this while loading base GC details.
381
+ if (this.referenceSequenceNumber >= referenceSequenceNumber) {
382
+ return;
383
+ }
384
+
385
+ /**
386
+ * GC data is written at root of the snapshot tree under "gc" sub-tree. This data needs to be propagated to
387
+ * all the nodes in the container.
388
+ * The root summarizer node reads the GC data from the "gc" sub-tree, runs GC on it to get used routes in
389
+ * the container and updates its GC data and referenced used routes. It then gets the GC data and used
390
+ * routes of all its children and adds it to their snapshot tree.
391
+ * All the other nodes gets the GC data and used routes from their snapshot tree and updates their state.
392
+ * They get the GC data and used routes of their children and add it to their snapshot tree and so on.
393
+ *
394
+ * Note that if the snapshot does not have GC tree, GC data will be set to undefined and used routes will be
395
+ * set to self-route (meaning referenced) for all nodes. This is important because the GC data needs to be
396
+ * regenerated in the next summary.
397
+ */
398
+ let gcDetails: IGarbageCollectionDetailsBase | undefined;
399
+ const gcSnapshotTree = snapshotTree.trees[gcTreeKey];
400
+ if (gcSnapshotTree !== undefined) {
401
+ // If there is a GC tree in the snapshot, this is the root summarizer node. Read GC data from the tree
402
+ // process it as explained above.
403
+ const gcSnapshotData = await getGCDataFromSnapshot(gcSnapshotTree, readAndParseBlob);
404
+
405
+ const gcNodes: { [id: string]: string[] } = {};
406
+ for (const [nodeId, nodeData] of Object.entries(gcSnapshotData.gcState.gcNodes)) {
407
+ gcNodes[nodeId] = Array.from(nodeData.outboundRoutes);
408
+ }
409
+ // Run GC on the nodes in the snapshot to get the used routes for each node in the container.
410
+ const usedRoutes = runGarbageCollection(gcNodes, ["/"]).referencedNodeIds;
411
+ gcDetails = { gcData: { gcNodes }, usedRoutes };
412
+ } else {
413
+ // If there is a GC blob in the snapshot, it's a non-root summarizer nodes - The root summarizer node
414
+ // writes GC blob in the snapshot of child nodes. Get GC data and used routes from the blob.
415
+ const gcDetailsBlob = snapshotTree.blobs[gcTreeKey];
416
+ if (gcDetailsBlob !== undefined) {
417
+ gcDetails = JSON.parse(gcDetailsBlob) as IGarbageCollectionDetailsBase;
418
+ }
419
+ }
420
+
421
+ // Update this node to the same GC state it was when the ack corresponding to this summary was processed.
422
+ this.gcData = gcDetails?.gcData !== undefined ? cloneGCData(gcDetails.gcData) : undefined;
423
+ this.referenceUsedRoutes =
424
+ gcDetails?.usedRoutes !== undefined ? Array.from(gcDetails.usedRoutes) : undefined;
425
+ // If there are no used routes in the GC details, set it to have self route which will make the node
426
+ // referenced. This scenario can only happen if the snapshot is from a client where GC was not run or
427
+ // disabled. In both the cases, the node should be referenced.
428
+ this.usedRoutes =
429
+ gcDetails?.usedRoutes !== undefined ? Array.from(gcDetails.usedRoutes) : [""];
430
+
431
+ if (gcDetails === undefined) {
432
+ return;
433
+ }
434
+
435
+ // Generate the GC data and used routes of children GC nodes and add it to their snapshot tree.
436
+ const gcDetailsMap = unpackChildNodesGCDetails(gcDetails);
437
+ const { childrenTree } = parseSummaryForSubtrees(snapshotTree);
438
+ gcDetailsMap.forEach((childGCDetails: IGarbageCollectionDetailsBase, childId: string) => {
439
+ if (childrenTree.trees[childId] !== undefined) {
440
+ childrenTree.trees[childId].blobs[gcTreeKey] = JSON.stringify(childGCDetails);
441
+ }
442
+ });
443
+ }
444
+
445
+ /**
446
+ * Override the createChild method to return an instance of SummarizerNodeWithGC.
447
+ */
448
+ public createChild(
449
+ /** Summarize function */
450
+ summarizeInternalFn: SummarizeInternalFn,
451
+ /** Initial id or path part of this node */
452
+ id: string,
453
+ /**
454
+ * Information needed to create the node.
455
+ * If it is from a base summary, it will assert that a summary has been seen.
456
+ * Attach information if it is created from an attach op.
457
+ */
458
+ createParam: CreateChildSummarizerNodeParam,
459
+ config: ISummarizerNodeConfigWithGC = {},
460
+ getGCDataFn?: (fullGC?: boolean) => Promise<IGarbageCollectionData>,
461
+ getBaseGCDetailsFn?: () => Promise<IGarbageCollectionDetailsBase>,
462
+ ): ISummarizerNodeWithGC {
463
+ assert(!this.children.has(id), 0x1b6 /* "Create SummarizerNode child already exists" */);
464
+ /**
465
+ * Update the child node's base GC details from this node's current GC details instead of updating from the base
466
+ * GC details of this node. This will handle scenarios where the GC details was updated during refresh from
467
+ * snapshot and the child node wasn't created then. If a child is created after that, its GC details should be
468
+ * the one from the downloaded snapshot and not the base GC details.
469
+ */
470
+ const getChildBaseGCDetailsFn = async () => {
471
+ const childNodesBaseGCDetails = await this.childNodesBaseGCDetailsP;
472
+ return childNodesBaseGCDetails.get(id) ?? {};
473
+ };
474
+
475
+ const createDetails: ICreateChildDetails = this.getCreateDetailsForChild(id, createParam);
476
+ const child = new SummarizerNodeWithGC(
477
+ this.logger,
478
+ summarizeInternalFn,
479
+ {
480
+ ...config,
481
+ // Propagate our gcDisabled state to the child if its not explicity specified in child's config.
482
+ gcDisabled: config.gcDisabled ?? this.gcDisabled,
483
+ },
484
+ createDetails.changeSequenceNumber,
485
+ createDetails.latestSummary,
486
+ createDetails.initialSummary,
487
+ this.wipSummaryLogger,
488
+ getGCDataFn,
489
+ getChildBaseGCDetailsFn,
490
+ createDetails.telemetryNodeId,
491
+ );
492
+
493
+ // There may be additional state that has to be updated in this child. For example, if a summary is being
494
+ // tracked, the child's summary tracking state needs to be updated too.
495
+ this.maybeUpdateChildState(child, id);
496
+
497
+ this.children.set(id, child);
498
+ return child;
499
+ }
500
+
501
+ /**
502
+ * Updates the state of the child if required. For example, if a summary is currently being tracked, the child's
503
+ * summary tracking state needs to be updated too.
504
+ * Also, in case a child node gets realized in between Summary Op and Summary Ack, let's initialize the child's
505
+ * pending summary as well. Finally, if the pendingSummaries entries have serializedRoutes, replicate them to the
506
+ * pendingSummaries from the child nodes.
507
+ * @param child - The child node whose state is to be updated.
508
+ * @param id - Initial id or path part of this node
509
+ */
510
+ protected maybeUpdateChildState(child: SummarizerNodeWithGC, id: string) {
511
+ super.maybeUpdateChildState(child, id);
512
+
513
+ // In case we have pending summaries on the parent, let's initialize it on the child.
514
+ if (child.latestSummary !== undefined) {
515
+ for (const [key, value] of this.pendingSummaries.entries()) {
516
+ const summaryNodeWithGC = value as SummaryNodeWithGC;
517
+ if (summaryNodeWithGC.serializedUsedRoutes !== undefined) {
518
+ const childNodeUsedRoutes = unpackChildNodesUsedRoutes(
519
+ JSON.parse(summaryNodeWithGC.serializedUsedRoutes),
520
+ );
521
+ const newSerializedRoutes = childNodeUsedRoutes.get(id) ?? [""];
522
+ const newLatestSummaryNode = new SummaryNodeWithGC(
523
+ JSON.stringify(newSerializedRoutes),
524
+ {
525
+ referenceSequenceNumber: value.referenceSequenceNumber,
526
+ basePath: value.basePath,
527
+ localPath: value.localPath,
528
+ },
529
+ );
530
+ child.addPendingSummary(key, newLatestSummaryNode);
531
+ }
532
+ }
533
+ }
534
+ }
535
+
536
+ /**
537
+ * Deletes the child node with the given id.
538
+ */
539
+ public deleteChild(id: string): void {
540
+ this.children.delete(id);
541
+ }
542
+
543
+ /**
544
+ * Override the getChild method to return an instance of SummarizerNodeWithGC.
545
+ */
546
+ public getChild(id: string): ISummarizerNodeWithGC | undefined {
547
+ return this.children.get(id) as SummarizerNodeWithGC;
548
+ }
549
+
550
+ public isReferenced(): boolean {
551
+ return this.usedRoutes.includes("") || this.usedRoutes.includes("/");
552
+ }
553
+
554
+ public updateUsedRoutes(usedRoutes: string[]) {
555
+ // Sort the given routes before updating. This will ensure that the routes compared in hasUsedStateChanged()
556
+ // are in the same order.
557
+ this.usedRoutes = usedRoutes.sort();
558
+
559
+ // If GC is not disabled and a summary is in progress, update the work-in-progress used routes so that it can
560
+ // be tracked for this summary.
561
+ if (!this.gcDisabled && this.isSummaryInProgress()) {
562
+ this.wipSerializedUsedRoutes = JSON.stringify(this.usedRoutes);
563
+ }
564
+ }
565
+
566
+ /**
567
+ * Override the hasChanged method. If this node data or its used state changed, the node is considered changed.
568
+ */
569
+ protected hasChanged(): boolean {
570
+ return this.hasDataChanged() || this.hasUsedStateChanged();
571
+ }
572
+
573
+ /**
574
+ * This tells whether the data in this node has changed or not.
575
+ */
576
+ private hasDataChanged(): boolean {
577
+ return super.hasChanged();
578
+ }
579
+
580
+ /**
581
+ * This tells whether the used state of this node has changed since last successful summary. If the used routes
582
+ * of this node changed, its used state is considered changed. Basically, if this node or any of its child nodes
583
+ * was previously used and became unused (or vice versa), its used state has changed.
584
+ */
585
+ private hasUsedStateChanged(): boolean {
586
+ // If GC is disabled, we are not tracking used state, return false.
587
+ if (this.gcDisabled) {
588
+ return false;
589
+ }
590
+
591
+ return (
592
+ this.referenceUsedRoutes === undefined ||
593
+ JSON.stringify(this.usedRoutes) !== JSON.stringify(this.referenceUsedRoutes)
594
+ );
595
+ }
596
+ }
597
+
598
+ /**
599
+ * Creates a root summarizer node with GC functionality built-in.
600
+ * @param logger - Logger to use within SummarizerNode
601
+ * @param summarizeInternalFn - Function to generate summary
602
+ * @param changeSequenceNumber - Sequence number of latest change to new node/subtree
603
+ * @param referenceSequenceNumber - Reference sequence number of last acked summary,
604
+ * or undefined if not loaded from summary
605
+ * @param config - Configure behavior of summarizer node
606
+ * @param getGCDataFn - Function to get the GC data of this node
607
+ * @param baseGCDetailsP - Function to get the initial GC details of this node
608
+ */
609
+ export const createRootSummarizerNodeWithGC = (
610
+ logger: ITelemetryLogger,
611
+ summarizeInternalFn: SummarizeInternalFn,
612
+ changeSequenceNumber: number,
613
+ referenceSequenceNumber: number | undefined,
614
+ config: ISummarizerNodeConfigWithGC = {},
615
+ getGCDataFn?: (fullGC?: boolean) => Promise<IGarbageCollectionData>,
616
+ getBaseGCDetailsFn?: () => Promise<IGarbageCollectionDetailsBase>,
617
+ ): IRootSummarizerNodeWithGC =>
618
+ new SummarizerNodeWithGC(
619
+ logger,
620
+ summarizeInternalFn,
621
+ config,
622
+ changeSequenceNumber,
623
+ referenceSequenceNumber === undefined
624
+ ? undefined
625
+ : SummaryNode.createForRoot(referenceSequenceNumber),
626
+ undefined /* initialSummary */,
627
+ undefined /* wipSummaryLogger */,
628
+ getGCDataFn,
629
+ getBaseGCDetailsFn,
630
+ "" /* telemetryId */,
631
+ );
@@ -10,7 +10,6 @@ import {
10
10
  ITelemetryProperties,
11
11
  } from "@fluidframework/common-definitions";
12
12
  import { ITelemetryLoggerPropertyBag } from "@fluidframework/telemetry-utils";
13
- import { IFluidLoadable } from "@fluidframework/core-interfaces";
14
13
  import { ContainerWarning, IDeltaManager } from "@fluidframework/container-definitions";
15
14
  import {
16
15
  ISequencedDocumentMessage,
@@ -22,21 +21,6 @@ import { ISummaryConfigurationHeuristics } from "../containerRuntime";
22
21
  import { ISummaryAckMessage, ISummaryNackMessage, ISummaryOpMessage } from "./summaryCollection";
23
22
  import { SummarizeReason } from "./summaryGenerator";
24
23
 
25
- /**
26
- * @deprecated This will be removed in a later release.
27
- */
28
- export const ISummarizer: keyof IProvideSummarizer = "ISummarizer";
29
-
30
- /**
31
- * @deprecated This will be removed in a later release.
32
- */
33
- export interface IProvideSummarizer {
34
- /**
35
- * @deprecated This will be removed in a later release.
36
- */
37
- readonly ISummarizer: ISummarizer;
38
- }
39
-
40
24
  /**
41
25
  * Similar to AbortSignal, but using promise instead of events
42
26
  * @param T - cancellation reason type
@@ -84,8 +68,6 @@ export interface IConnectableRuntime {
84
68
  readonly disposed: boolean;
85
69
  readonly connected: boolean;
86
70
  readonly clientId: string | undefined;
87
- /** @deprecated - Moved to `ISummarizerRuntime` as it's no longer needed here */
88
- readonly deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>;
89
71
  once(event: "connected" | "disconnected" | "dispose", listener: () => void): this;
90
72
  }
91
73
 
@@ -322,10 +304,12 @@ export interface ISummarizerEvents extends IEvent {
322
304
  (event: "summarizingError", listener: (error: ISummarizingWarning) => void);
323
305
  }
324
306
 
325
- export interface ISummarizer
326
- extends IEventProvider<ISummarizerEvents>,
327
- IFluidLoadable,
328
- Partial<IProvideSummarizer> {
307
+ export interface ISummarizer extends IEventProvider<ISummarizerEvents> {
308
+ /**
309
+ * Allows {@link ISummarizer} to be used with our {@link @fluidframework/core-interfaces#FluidObject} pattern.
310
+ */
311
+ readonly ISummarizer?: ISummarizer;
312
+
329
313
  /*
330
314
  * Asks summarizer to move to exit.
331
315
  * Summarizer will finish current processes, which may take a while.
@@ -1,12 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- import { FluidObjectHandle } from "@fluidframework/datastore";
6
- import { IFluidHandle } from "@fluidframework/core-interfaces";
7
- export declare class SummarizerHandle extends FluidObjectHandle {
8
- get(): Promise<any>;
9
- attach(): void;
10
- bind(handle: IFluidHandle): void;
11
- }
12
- //# sourceMappingURL=summarizerHandle.d.ts.map