@fluidframework/runtime-utils 2.0.0-dev.3.1.0.125672 → 2.0.0-dev.4.1.0.148229

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 (71) hide show
  1. package/dist/index.d.ts +2 -2
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +4 -4
  4. package/dist/index.js.map +1 -1
  5. package/dist/packageVersion.d.ts +1 -1
  6. package/dist/packageVersion.js +1 -1
  7. package/dist/packageVersion.js.map +1 -1
  8. package/dist/summaryUtils.d.ts +33 -1
  9. package/dist/summaryUtils.d.ts.map +1 -1
  10. package/dist/summaryUtils.js +83 -1
  11. package/dist/summaryUtils.js.map +1 -1
  12. package/dist/unpackUsedRoutes.d.ts +11 -0
  13. package/dist/unpackUsedRoutes.d.ts.map +1 -0
  14. package/dist/unpackUsedRoutes.js +33 -0
  15. package/dist/unpackUsedRoutes.js.map +1 -0
  16. package/lib/index.d.ts +2 -2
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +2 -2
  19. package/lib/index.js.map +1 -1
  20. package/lib/packageVersion.d.ts +1 -1
  21. package/lib/packageVersion.js +1 -1
  22. package/lib/packageVersion.js.map +1 -1
  23. package/lib/summaryUtils.d.ts +33 -1
  24. package/lib/summaryUtils.d.ts.map +1 -1
  25. package/lib/summaryUtils.js +81 -0
  26. package/lib/summaryUtils.js.map +1 -1
  27. package/lib/unpackUsedRoutes.d.ts +11 -0
  28. package/lib/unpackUsedRoutes.d.ts.map +1 -0
  29. package/lib/unpackUsedRoutes.js +29 -0
  30. package/lib/unpackUsedRoutes.js.map +1 -0
  31. package/package.json +53 -55
  32. package/src/index.ts +2 -8
  33. package/src/packageVersion.ts +1 -1
  34. package/src/summaryUtils.ts +94 -0
  35. package/src/unpackUsedRoutes.ts +30 -0
  36. package/dist/summarizerNode/index.d.ts +0 -8
  37. package/dist/summarizerNode/index.d.ts.map +0 -1
  38. package/dist/summarizerNode/index.js +0 -12
  39. package/dist/summarizerNode/index.js.map +0 -1
  40. package/dist/summarizerNode/summarizerNode.d.ts +0 -140
  41. package/dist/summarizerNode/summarizerNode.d.ts.map +0 -1
  42. package/dist/summarizerNode/summarizerNode.js +0 -448
  43. package/dist/summarizerNode/summarizerNode.js.map +0 -1
  44. package/dist/summarizerNode/summarizerNodeUtils.d.ts +0 -128
  45. package/dist/summarizerNode/summarizerNodeUtils.d.ts.map +0 -1
  46. package/dist/summarizerNode/summarizerNodeUtils.js +0 -132
  47. package/dist/summarizerNode/summarizerNodeUtils.js.map +0 -1
  48. package/dist/summarizerNode/summarizerNodeWithGc.d.ts +0 -135
  49. package/dist/summarizerNode/summarizerNodeWithGc.d.ts.map +0 -1
  50. package/dist/summarizerNode/summarizerNodeWithGc.js +0 -361
  51. package/dist/summarizerNode/summarizerNodeWithGc.js.map +0 -1
  52. package/lib/summarizerNode/index.d.ts +0 -8
  53. package/lib/summarizerNode/index.d.ts.map +0 -1
  54. package/lib/summarizerNode/index.js +0 -7
  55. package/lib/summarizerNode/index.js.map +0 -1
  56. package/lib/summarizerNode/summarizerNode.d.ts +0 -140
  57. package/lib/summarizerNode/summarizerNode.d.ts.map +0 -1
  58. package/lib/summarizerNode/summarizerNode.js +0 -443
  59. package/lib/summarizerNode/summarizerNode.js.map +0 -1
  60. package/lib/summarizerNode/summarizerNodeUtils.d.ts +0 -128
  61. package/lib/summarizerNode/summarizerNodeUtils.d.ts.map +0 -1
  62. package/lib/summarizerNode/summarizerNodeUtils.js +0 -125
  63. package/lib/summarizerNode/summarizerNodeUtils.js.map +0 -1
  64. package/lib/summarizerNode/summarizerNodeWithGc.d.ts +0 -135
  65. package/lib/summarizerNode/summarizerNodeWithGc.d.ts.map +0 -1
  66. package/lib/summarizerNode/summarizerNodeWithGc.js +0 -356
  67. package/lib/summarizerNode/summarizerNodeWithGc.js.map +0 -1
  68. package/src/summarizerNode/index.ts +0 -8
  69. package/src/summarizerNode/summarizerNode.ts +0 -631
  70. package/src/summarizerNode/summarizerNodeUtils.ts +0 -219
  71. package/src/summarizerNode/summarizerNodeWithGc.ts +0 -556
@@ -1,631 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
-
6
- import {
7
- ISummarizerNode,
8
- ISummarizerNodeConfig,
9
- ISummarizeResult,
10
- ISummaryTreeWithStats,
11
- CreateChildSummarizerNodeParam,
12
- CreateSummarizerNodeSource,
13
- SummarizeInternalFn,
14
- ITelemetryContext,
15
- } from "@fluidframework/runtime-definitions";
16
- import {
17
- ISequencedDocumentMessage,
18
- SummaryType,
19
- ISnapshotTree,
20
- SummaryObject,
21
- } from "@fluidframework/protocol-definitions";
22
- import { ITelemetryLogger } from "@fluidframework/common-definitions";
23
- import { assert, unreachableCase } from "@fluidframework/common-utils";
24
- import { mergeStats, convertToSummaryTree, calculateStats } from "../summaryUtils";
25
- import { ReadAndParseBlob } from "../utils";
26
- import {
27
- EscapedPath,
28
- ICreateChildDetails,
29
- IInitialSummary,
30
- ISummarizerNodeRootContract,
31
- parseSummaryForSubtrees,
32
- parseSummaryTreeForSubtrees,
33
- RefreshSummaryResult,
34
- SummaryNode,
35
- } from "./summarizerNodeUtils";
36
-
37
- export interface IRootSummarizerNode extends ISummarizerNode, ISummarizerNodeRootContract {}
38
-
39
- /**
40
- * Encapsulates the summarizing work and state of an individual tree node in the
41
- * summary tree. It tracks changes and allows for optimizations when unchanged, or
42
- * can allow for fallback summaries to be generated when an error is encountered.
43
- * Usage is for the root node to call startSummary first to begin tracking a WIP
44
- * (work in progress) summary. Then all nodes will call summarize to summaries their
45
- * individual parts. Once completed and uploaded to storage, the root node will call
46
- * completeSummary or clearSummary to clear the WIP summary tracking state if something
47
- * went wrong. The SummarizerNodes will track all pending summaries that have been
48
- * recorded by the completeSummary call. When one of them is acked, the root node should
49
- * call refreshLatestSummary to inform the tree of SummarizerNodes of the new baseline
50
- * latest successful summary.
51
- */
52
- export class SummarizerNode implements IRootSummarizerNode {
53
- /**
54
- * The reference sequence number of the most recent acked summary.
55
- * Returns 0 if there is not yet an acked summary.
56
- */
57
- public get referenceSequenceNumber() {
58
- return this._latestSummary?.referenceSequenceNumber ?? 0;
59
- }
60
-
61
- protected readonly children = new Map<string, SummarizerNode>();
62
- protected readonly pendingSummaries = new Map<string, SummaryNode>();
63
- private wipReferenceSequenceNumber: number | undefined;
64
- private wipLocalPaths: { localPath: EscapedPath; additionalPath?: EscapedPath } | undefined;
65
- private wipSkipRecursion = false;
66
-
67
- public startSummary(referenceSequenceNumber: number, summaryLogger: ITelemetryLogger) {
68
- assert(
69
- this.wipSummaryLogger === undefined,
70
- 0x19f /* "wipSummaryLogger should not be set yet in startSummary" */,
71
- );
72
- assert(
73
- this.wipReferenceSequenceNumber === undefined,
74
- 0x1a0 /* "Already tracking a summary" */,
75
- );
76
-
77
- this.wipSummaryLogger = summaryLogger;
78
-
79
- for (const child of this.children.values()) {
80
- child.startSummary(referenceSequenceNumber, this.wipSummaryLogger);
81
- }
82
- this.wipReferenceSequenceNumber = referenceSequenceNumber;
83
- }
84
-
85
- public async summarize(
86
- fullTree: boolean,
87
- trackState: boolean = true,
88
- telemetryContext?: ITelemetryContext,
89
- ): Promise<ISummarizeResult> {
90
- assert(
91
- this.isTrackingInProgress(),
92
- 0x1a1 /* "summarize should not be called when not tracking the summary" */,
93
- );
94
- assert(
95
- this.wipSummaryLogger !== undefined,
96
- 0x1a2 /* "wipSummaryLogger should have been set in startSummary or ctor" */,
97
- );
98
-
99
- // Try to reuse the tree if unchanged
100
- if (this.canReuseHandle && !fullTree && !this.hasChanged()) {
101
- const latestSummary = this._latestSummary;
102
- if (latestSummary !== undefined) {
103
- this.wipLocalPaths = {
104
- localPath: latestSummary.localPath,
105
- additionalPath: latestSummary.additionalPath,
106
- };
107
- this.wipSkipRecursion = true;
108
- const stats = mergeStats();
109
- stats.handleNodeCount++;
110
- return {
111
- summary: {
112
- type: SummaryType.Handle,
113
- handle: latestSummary.fullPath.path,
114
- handleType: SummaryType.Tree,
115
- },
116
- stats,
117
- };
118
- }
119
- }
120
-
121
- const result = await this.summarizeInternalFn(fullTree, true, telemetryContext);
122
- this.wipLocalPaths = { localPath: EscapedPath.create(result.id) };
123
- if (result.pathPartsForChildren !== undefined) {
124
- this.wipLocalPaths.additionalPath = EscapedPath.createAndConcat(
125
- result.pathPartsForChildren,
126
- );
127
- }
128
- return { summary: result.summary, stats: result.stats };
129
- }
130
-
131
- /**
132
- * Complete the WIP summary for the given proposalHandle
133
- */
134
- public completeSummary(proposalHandle: string) {
135
- this.completeSummaryCore(proposalHandle, undefined, false);
136
- }
137
-
138
- /**
139
- * Recursive implementation for completeSummary, with additional internal-only parameters
140
- */
141
- protected completeSummaryCore(
142
- proposalHandle: string,
143
- parentPath: EscapedPath | undefined,
144
- parentSkipRecursion: boolean,
145
- ) {
146
- assert(
147
- this.wipSummaryLogger !== undefined,
148
- 0x1a3 /* "wipSummaryLogger should have been set in startSummary or ctor" */,
149
- );
150
- assert(this.wipReferenceSequenceNumber !== undefined, 0x1a4 /* "Not tracking a summary" */);
151
- let localPathsToUse = this.wipLocalPaths;
152
-
153
- if (parentSkipRecursion) {
154
- const latestSummary = this._latestSummary;
155
- if (latestSummary !== undefined) {
156
- // This case the parent node created a failure summary or was reused.
157
- // This node and all children should only try to reference their path
158
- // by its last known good state in the actual summary tree.
159
- // If parent fails or is reused, the child summarize is not called so
160
- // it did not get a chance to change its paths.
161
- // In this case, essentially only propagate the new summary ref seq num.
162
- localPathsToUse = {
163
- localPath: latestSummary.localPath,
164
- additionalPath: latestSummary.additionalPath,
165
- };
166
- } else {
167
- // This case the child is added after the latest non-failure summary.
168
- // This node and all children should consider themselves as still not
169
- // having a successful summary yet.
170
- // We cannot "reuse" this node if unchanged since that summary, because
171
- // handles will be unable to point to that node. It never made it to the
172
- // tree itself, and only exists as an attach op in the _outstandingOps.
173
- this.clearSummary();
174
- return;
175
- }
176
- }
177
-
178
- // This should come from wipLocalPaths in normal cases, or from the latestSummary
179
- // if parentIsFailure or parentIsReused is true.
180
- // If there is no latestSummary, clearSummary and return before reaching this code.
181
- assert(!!localPathsToUse, 0x1a5 /* "Tracked summary local paths not set" */);
182
-
183
- const summary = new SummaryNode({
184
- ...localPathsToUse,
185
- referenceSequenceNumber: this.wipReferenceSequenceNumber,
186
- basePath: parentPath,
187
- });
188
- const fullPathForChildren = summary.fullPathForChildren;
189
- for (const child of this.children.values()) {
190
- child.completeSummaryCore(
191
- proposalHandle,
192
- fullPathForChildren,
193
- this.wipSkipRecursion || parentSkipRecursion,
194
- );
195
- }
196
- // Note that this overwrites existing pending summary with
197
- // the same proposalHandle. If proposalHandle is something like
198
- // a hash or unique identifier, this should be fine. If storage
199
- // can return the same proposalHandle for a different summary,
200
- // this should still be okay, because we should be proposing the
201
- // newer one later which would have to overwrite the previous one.
202
- this.pendingSummaries.set(proposalHandle, summary);
203
- this.clearSummary();
204
- }
205
-
206
- public clearSummary() {
207
- this.wipReferenceSequenceNumber = undefined;
208
- this.wipLocalPaths = undefined;
209
- this.wipSkipRecursion = false;
210
- this.wipSummaryLogger = undefined;
211
- for (const child of this.children.values()) {
212
- child.clearSummary();
213
- }
214
- }
215
-
216
- /**
217
- * Refreshes the latest summary tracked by this node. If we have a pending summary for the given proposal handle,
218
- * it becomes the latest summary. If the current summary is already ahead (e.g., loaded from a service summary),
219
- * we skip the update. Otherwise, we get the snapshot by calling `getSnapshot` and update latest
220
- * summary based off of that.
221
- *
222
- * @returns A RefreshSummaryResult type which returns information based on the following three scenarios:
223
- *
224
- * 1. The latest summary was not udpated.
225
- *
226
- * 2. The latest summary was updated and the summary corresponding to the params was being tracked.
227
- *
228
- * 3. The latest summary was updated but the summary corresponding to the params was not tracked. In this
229
- * case, the latest summary is updated based on the downloaded snapshot which is also returned.
230
- */
231
- public async refreshLatestSummary(
232
- proposalHandle: string | undefined,
233
- summaryRefSeq: number,
234
- getSnapshot: () => Promise<ISnapshotTree>,
235
- readAndParseBlob: ReadAndParseBlob,
236
- correlatedSummaryLogger: ITelemetryLogger,
237
- ): Promise<RefreshSummaryResult> {
238
- this.defaultLogger.sendTelemetryEvent({
239
- eventName: "refreshLatestSummary_start",
240
- proposalHandle,
241
- referenceSequenceNumber: this.referenceSequenceNumber,
242
- summaryRefSeq,
243
- });
244
-
245
- if (proposalHandle !== undefined) {
246
- const maybeSummaryNode = this.pendingSummaries.get(proposalHandle);
247
-
248
- if (maybeSummaryNode !== undefined) {
249
- this.refreshLatestSummaryFromPending(
250
- proposalHandle,
251
- maybeSummaryNode.referenceSequenceNumber,
252
- );
253
- return { latestSummaryUpdated: true, wasSummaryTracked: true };
254
- }
255
-
256
- const props = {
257
- summaryRefSeq,
258
- pendingSize: this.pendingSummaries.size ?? undefined,
259
- };
260
- this.defaultLogger.sendTelemetryEvent({
261
- eventName: "PendingSummaryNotFound",
262
- proposalHandle,
263
- referenceSequenceNumber: this.referenceSequenceNumber,
264
- details: JSON.stringify(props),
265
- });
266
- }
267
-
268
- // If we have seen a summary same or later as the current one, ignore it.
269
- if (this.referenceSequenceNumber >= summaryRefSeq) {
270
- return { latestSummaryUpdated: false };
271
- }
272
-
273
- const snapshotTree = await getSnapshot();
274
- await this.refreshLatestSummaryFromSnapshot(
275
- summaryRefSeq,
276
- snapshotTree,
277
- undefined,
278
- EscapedPath.create(""),
279
- correlatedSummaryLogger,
280
- readAndParseBlob,
281
- );
282
- return { latestSummaryUpdated: true, wasSummaryTracked: false, snapshot: snapshotTree };
283
- }
284
- /**
285
- * Called when we get an ack from the server for a summary we've just sent. Updates the reference state of this node
286
- * from the state in the pending summary queue.
287
- * @param proposalHandle - Handle for the current proposal.
288
- * @param referenceSequenceNumber - reference sequence number of sent summary.
289
- */
290
- protected refreshLatestSummaryFromPending(
291
- proposalHandle: string,
292
- referenceSequenceNumber: number,
293
- ): void {
294
- const summaryNode = this.pendingSummaries.get(proposalHandle);
295
- if (summaryNode === undefined) {
296
- // This should only happen if parent skipped recursion AND no prior summary existed.
297
- assert(
298
- this._latestSummary === undefined,
299
- 0x1a6 /* "Not found pending summary, but this node has previously completed a summary" */,
300
- );
301
- return;
302
- } else {
303
- assert(
304
- referenceSequenceNumber === summaryNode.referenceSequenceNumber,
305
- 0x1a7 /* Pending summary reference sequence number should be consistent */,
306
- );
307
-
308
- // Clear earlier pending summaries
309
- this.pendingSummaries.delete(proposalHandle);
310
- }
311
-
312
- this.refreshLatestSummaryCore(referenceSequenceNumber);
313
-
314
- this._latestSummary = summaryNode;
315
- // Propagate update to all child nodes
316
- for (const child of this.children.values()) {
317
- child.refreshLatestSummaryFromPending(proposalHandle, referenceSequenceNumber);
318
- }
319
- }
320
-
321
- protected async refreshLatestSummaryFromSnapshot(
322
- referenceSequenceNumber: number,
323
- snapshotTree: ISnapshotTree,
324
- basePath: EscapedPath | undefined,
325
- localPath: EscapedPath,
326
- correlatedSummaryLogger: ITelemetryLogger,
327
- readAndParseBlob: ReadAndParseBlob,
328
- ): Promise<void> {
329
- // Possible re-entrancy. If we have already seen a summary later than this one, ignore it.
330
- if (this.referenceSequenceNumber >= referenceSequenceNumber) {
331
- return;
332
- }
333
-
334
- this.refreshLatestSummaryCore(referenceSequenceNumber);
335
-
336
- this._latestSummary = new SummaryNode({
337
- referenceSequenceNumber,
338
- basePath,
339
- localPath,
340
- });
341
-
342
- const pathParts: string[] = [];
343
- const { childrenTree, childrenPathPart } = parseSummaryForSubtrees(snapshotTree);
344
- if (childrenPathPart !== undefined) {
345
- pathParts.push(childrenPathPart);
346
- }
347
-
348
- if (pathParts.length > 0) {
349
- this._latestSummary.additionalPath = EscapedPath.createAndConcat(pathParts);
350
- }
351
-
352
- // Propagate update to all child nodes
353
- const pathForChildren = this._latestSummary.fullPathForChildren;
354
- await Promise.all(
355
- Array.from(this.children)
356
- .filter(([id]) => {
357
- // Assuming subtrees missing from snapshot are newer than the snapshot,
358
- // but might be nice to assert this using earliest seq for node.
359
- return childrenTree.trees[id] !== undefined;
360
- })
361
- .map(async ([id, child]) => {
362
- return child.refreshLatestSummaryFromSnapshot(
363
- referenceSequenceNumber,
364
- childrenTree.trees[id],
365
- pathForChildren,
366
- EscapedPath.create(id),
367
- correlatedSummaryLogger,
368
- readAndParseBlob,
369
- );
370
- }),
371
- );
372
- }
373
-
374
- private refreshLatestSummaryCore(referenceSequenceNumber: number): void {
375
- for (const [key, value] of this.pendingSummaries) {
376
- if (value.referenceSequenceNumber < referenceSequenceNumber) {
377
- this.pendingSummaries.delete(key);
378
- }
379
- }
380
- }
381
-
382
- public updateBaseSummaryState(snapshot: ISnapshotTree) {
383
- // Check base summary to see if it has any additional path parts
384
- // separating child SummarizerNodes. Checks for .channels subtrees.
385
- const { childrenPathPart } = parseSummaryForSubtrees(snapshot);
386
- if (childrenPathPart !== undefined && this._latestSummary !== undefined) {
387
- this._latestSummary.additionalPath = EscapedPath.create(childrenPathPart);
388
- }
389
- }
390
-
391
- public recordChange(op: ISequencedDocumentMessage): void {
392
- this.invalidate(op.sequenceNumber);
393
- }
394
-
395
- public invalidate(sequenceNumber: number): void {
396
- if (sequenceNumber > this._changeSequenceNumber) {
397
- this._changeSequenceNumber = sequenceNumber;
398
- }
399
- }
400
-
401
- /**
402
- * True if a change has been recorded with sequence number exceeding
403
- * the latest successfully acked summary reference sequence number.
404
- * False implies that the previous summary can be reused.
405
- */
406
- protected hasChanged(): boolean {
407
- return this._changeSequenceNumber > this.referenceSequenceNumber;
408
- }
409
-
410
- public get latestSummary(): Readonly<SummaryNode> | undefined {
411
- return this._latestSummary;
412
- }
413
-
414
- protected readonly canReuseHandle: boolean;
415
-
416
- /**
417
- * Do not call constructor directly.
418
- * Use createRootSummarizerNode to create root node, or createChild to create child nodes.
419
- */
420
- public constructor(
421
- protected readonly defaultLogger: ITelemetryLogger,
422
- private readonly summarizeInternalFn: SummarizeInternalFn,
423
- config: ISummarizerNodeConfig,
424
- private _changeSequenceNumber: number,
425
- /** Undefined means created without summary */
426
- private _latestSummary?: SummaryNode,
427
- private readonly initialSummary?: IInitialSummary,
428
- protected wipSummaryLogger?: ITelemetryLogger,
429
- ) {
430
- this.canReuseHandle = config.canReuseHandle ?? true;
431
- }
432
-
433
- public createChild(
434
- /** Summarize function */
435
- summarizeInternalFn: SummarizeInternalFn,
436
- /** Initial id or path part of this node */
437
- id: string,
438
- /**
439
- * Information needed to create the node.
440
- * If it is from a base summary, it will assert that a summary has been seen.
441
- * Attach information if it is created from an attach op.
442
- */
443
- createParam: CreateChildSummarizerNodeParam,
444
- config: ISummarizerNodeConfig = {},
445
- ): ISummarizerNode {
446
- assert(!this.children.has(id), 0x1ab /* "Create SummarizerNode child already exists" */);
447
-
448
- const createDetails: ICreateChildDetails = this.getCreateDetailsForChild(id, createParam);
449
- const child = new SummarizerNode(
450
- this.defaultLogger,
451
- summarizeInternalFn,
452
- config,
453
- createDetails.changeSequenceNumber,
454
- createDetails.latestSummary,
455
- createDetails.initialSummary,
456
- this.wipSummaryLogger,
457
- );
458
-
459
- // There may be additional state that has to be updated in this child. For example, if a summary is being
460
- // tracked, the child's summary tracking state needs to be updated too. Same goes for pendingSummaries we might
461
- // have outstanding on the parent in case we realize nodes in between Summary Op and Summary Ack.
462
- this.maybeUpdateChildState(child);
463
-
464
- this.children.set(id, child);
465
- return child;
466
- }
467
-
468
- public getChild(id: string): ISummarizerNode | undefined {
469
- return this.children.get(id);
470
- }
471
-
472
- /**
473
- * Returns the details needed to create a child node.
474
- * @param id - Initial id or path part of the child node.
475
- * @param createParam - Information needed to create the node.
476
- * @returns the details needed to create the child node.
477
- */
478
- protected getCreateDetailsForChild(
479
- id: string,
480
- createParam: CreateChildSummarizerNodeParam,
481
- ): ICreateChildDetails {
482
- let initialSummary: IInitialSummary | undefined;
483
- let latestSummary: SummaryNode | undefined;
484
- let changeSequenceNumber: number;
485
-
486
- const parentLatestSummary = this._latestSummary;
487
- switch (createParam.type) {
488
- case CreateSummarizerNodeSource.FromAttach: {
489
- if (
490
- parentLatestSummary !== undefined &&
491
- createParam.sequenceNumber <= parentLatestSummary.referenceSequenceNumber
492
- ) {
493
- // Prioritize latest summary if it was after this node was attached.
494
- latestSummary = parentLatestSummary.createForChild(id);
495
- } else {
496
- const summary = convertToSummaryTree(
497
- createParam.snapshot,
498
- ) as ISummaryTreeWithStats;
499
- initialSummary = {
500
- sequenceNumber: createParam.sequenceNumber,
501
- id,
502
- summary,
503
- };
504
- }
505
- changeSequenceNumber = createParam.sequenceNumber;
506
- break;
507
- }
508
- case CreateSummarizerNodeSource.FromSummary: {
509
- if (this.initialSummary === undefined) {
510
- assert(
511
- !!parentLatestSummary,
512
- 0x1ac /* "Cannot create child from summary if parent does not have latest summary" */,
513
- );
514
- }
515
- // fallthrough to local
516
- }
517
- case CreateSummarizerNodeSource.Local: {
518
- const parentInitialSummary = this.initialSummary;
519
- if (parentInitialSummary !== undefined) {
520
- let childSummary: SummaryObject | undefined;
521
- if (parentInitialSummary.summary !== undefined) {
522
- const { childrenTree } = parseSummaryTreeForSubtrees(
523
- parentInitialSummary.summary.summary,
524
- );
525
- assert(
526
- childrenTree.type === SummaryType.Tree,
527
- 0x1d6 /* "Parent summary object is not a tree" */,
528
- );
529
- childSummary = childrenTree.tree[id];
530
- }
531
- if (createParam.type === CreateSummarizerNodeSource.FromSummary) {
532
- // Locally created would not have differential subtree.
533
- assert(!!childSummary, 0x1ad /* "Missing child summary tree" */);
534
- }
535
- let childSummaryWithStats: ISummaryTreeWithStats | undefined;
536
- if (childSummary !== undefined) {
537
- assert(
538
- childSummary.type === SummaryType.Tree,
539
- 0x1ae /* "Child summary object is not a tree" */,
540
- );
541
- childSummaryWithStats = {
542
- summary: childSummary,
543
- stats: calculateStats(childSummary),
544
- };
545
- }
546
- initialSummary = {
547
- sequenceNumber: parentInitialSummary.sequenceNumber,
548
- id,
549
- summary: childSummaryWithStats,
550
- };
551
- }
552
- latestSummary = parentLatestSummary?.createForChild(id);
553
- changeSequenceNumber = parentLatestSummary?.referenceSequenceNumber ?? -1;
554
- break;
555
- }
556
- default: {
557
- const type = (createParam as unknown as CreateChildSummarizerNodeParam).type;
558
- unreachableCase(createParam, `Unexpected CreateSummarizerNodeSource: ${type}`);
559
- }
560
- }
561
-
562
- return {
563
- initialSummary,
564
- latestSummary,
565
- changeSequenceNumber,
566
- };
567
- }
568
-
569
- /**
570
- * Updates the state of the child if required. For example, if a summary is currently being tracked, the child's
571
- * summary tracking state needs to be updated too.
572
- * Also, in case a child node gets realized in between Summary Op and Summary Ack, let's initialize the child's
573
- * pending summary as well.
574
- * @param child - The child node whose state is to be updated.
575
- */
576
- protected maybeUpdateChildState(child: SummarizerNode) {
577
- // If we are tracking a summary, this child was created after the tracking started. So, we need to update the
578
- // child's tracking state as well.
579
- if (this.isTrackingInProgress()) {
580
- child.wipReferenceSequenceNumber = this.wipReferenceSequenceNumber;
581
- }
582
- // In case we have pending summaries on the parent, let's initialize it on the child.
583
- if (child._latestSummary !== undefined) {
584
- for (const [key, value] of this.pendingSummaries.entries()) {
585
- const newLatestSummaryNode = new SummaryNode({
586
- referenceSequenceNumber: value.referenceSequenceNumber,
587
- basePath: child._latestSummary.basePath,
588
- localPath: child._latestSummary.localPath,
589
- });
590
-
591
- child.addPendingSummary(key, newLatestSummaryNode);
592
- }
593
- }
594
- }
595
-
596
- protected addPendingSummary(key: string, summary: SummaryNode) {
597
- this.pendingSummaries.set(key, summary);
598
- }
599
- /**
600
- * Tells whether summary tracking is in progress. True if "startSummary" API is called before summarize.
601
- */
602
- protected isTrackingInProgress(): boolean {
603
- return this.wipReferenceSequenceNumber !== undefined;
604
- }
605
- }
606
-
607
- /**
608
- * Creates a root summarizer node.
609
- * @param logger - Logger to use within SummarizerNode
610
- * @param summarizeInternalFn - Function to generate summary
611
- * @param changeSequenceNumber - Sequence number of latest change to new node/subtree
612
- * @param referenceSequenceNumber - Reference sequence number of last acked summary,
613
- * or undefined if not loaded from summary
614
- * @param config - Configure behavior of summarizer node
615
- */
616
- export const createRootSummarizerNode = (
617
- logger: ITelemetryLogger,
618
- summarizeInternalFn: SummarizeInternalFn,
619
- changeSequenceNumber: number,
620
- referenceSequenceNumber: number | undefined,
621
- config: ISummarizerNodeConfig = {},
622
- ): IRootSummarizerNode =>
623
- new SummarizerNode(
624
- logger,
625
- summarizeInternalFn,
626
- config,
627
- changeSequenceNumber,
628
- referenceSequenceNumber === undefined
629
- ? undefined
630
- : SummaryNode.createForRoot(referenceSequenceNumber),
631
- );