@fluidframework/container-runtime 2.0.0-internal.4.3.0 → 2.0.0-internal.4.4.0

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 (103) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/blobManager.d.ts.map +1 -1
  3. package/dist/blobManager.js +3 -2
  4. package/dist/blobManager.js.map +1 -1
  5. package/dist/containerRuntime.d.ts.map +1 -1
  6. package/dist/containerRuntime.js +13 -7
  7. package/dist/containerRuntime.js.map +1 -1
  8. package/dist/dataStoreContext.d.ts.map +1 -1
  9. package/dist/dataStoreContext.js +1 -2
  10. package/dist/dataStoreContext.js.map +1 -1
  11. package/dist/gc/garbageCollection.d.ts +55 -43
  12. package/dist/gc/garbageCollection.d.ts.map +1 -1
  13. package/dist/gc/garbageCollection.js +219 -203
  14. package/dist/gc/garbageCollection.js.map +1 -1
  15. package/dist/gc/gcConfigs.d.ts.map +1 -1
  16. package/dist/gc/gcConfigs.js +8 -10
  17. package/dist/gc/gcConfigs.js.map +1 -1
  18. package/dist/gc/gcDefinitions.d.ts +2 -0
  19. package/dist/gc/gcDefinitions.d.ts.map +1 -1
  20. package/dist/gc/gcDefinitions.js.map +1 -1
  21. package/dist/gc/gcHelpers.d.ts +11 -1
  22. package/dist/gc/gcHelpers.d.ts.map +1 -1
  23. package/dist/gc/gcHelpers.js +18 -3
  24. package/dist/gc/gcHelpers.js.map +1 -1
  25. package/dist/gc/gcSummaryStateTracker.d.ts +6 -2
  26. package/dist/gc/gcSummaryStateTracker.d.ts.map +1 -1
  27. package/dist/gc/gcSummaryStateTracker.js +16 -6
  28. package/dist/gc/gcSummaryStateTracker.js.map +1 -1
  29. package/dist/gc/gcTelemetry.d.ts +2 -2
  30. package/dist/gc/gcTelemetry.d.ts.map +1 -1
  31. package/dist/gc/gcTelemetry.js +42 -22
  32. package/dist/gc/gcTelemetry.js.map +1 -1
  33. package/dist/gc/index.d.ts +1 -2
  34. package/dist/gc/index.d.ts.map +1 -1
  35. package/dist/gc/index.js +2 -5
  36. package/dist/gc/index.js.map +1 -1
  37. package/dist/packageVersion.d.ts +1 -1
  38. package/dist/packageVersion.js +1 -1
  39. package/dist/packageVersion.js.map +1 -1
  40. package/dist/scheduleManager.js +15 -4
  41. package/dist/scheduleManager.js.map +1 -1
  42. package/lib/blobManager.d.ts.map +1 -1
  43. package/lib/blobManager.js +3 -2
  44. package/lib/blobManager.js.map +1 -1
  45. package/lib/containerRuntime.d.ts.map +1 -1
  46. package/lib/containerRuntime.js +13 -7
  47. package/lib/containerRuntime.js.map +1 -1
  48. package/lib/dataStoreContext.d.ts.map +1 -1
  49. package/lib/dataStoreContext.js +1 -2
  50. package/lib/dataStoreContext.js.map +1 -1
  51. package/lib/gc/garbageCollection.d.ts +55 -43
  52. package/lib/gc/garbageCollection.d.ts.map +1 -1
  53. package/lib/gc/garbageCollection.js +219 -203
  54. package/lib/gc/garbageCollection.js.map +1 -1
  55. package/lib/gc/gcConfigs.d.ts.map +1 -1
  56. package/lib/gc/gcConfigs.js +8 -10
  57. package/lib/gc/gcConfigs.js.map +1 -1
  58. package/lib/gc/gcDefinitions.d.ts +2 -0
  59. package/lib/gc/gcDefinitions.d.ts.map +1 -1
  60. package/lib/gc/gcDefinitions.js.map +1 -1
  61. package/lib/gc/gcHelpers.d.ts +11 -1
  62. package/lib/gc/gcHelpers.d.ts.map +1 -1
  63. package/lib/gc/gcHelpers.js +16 -2
  64. package/lib/gc/gcHelpers.js.map +1 -1
  65. package/lib/gc/gcSummaryStateTracker.d.ts +6 -2
  66. package/lib/gc/gcSummaryStateTracker.d.ts.map +1 -1
  67. package/lib/gc/gcSummaryStateTracker.js +16 -6
  68. package/lib/gc/gcSummaryStateTracker.js.map +1 -1
  69. package/lib/gc/gcTelemetry.d.ts +2 -2
  70. package/lib/gc/gcTelemetry.d.ts.map +1 -1
  71. package/lib/gc/gcTelemetry.js +43 -23
  72. package/lib/gc/gcTelemetry.js.map +1 -1
  73. package/lib/gc/index.d.ts +1 -2
  74. package/lib/gc/index.d.ts.map +1 -1
  75. package/lib/gc/index.js +1 -2
  76. package/lib/gc/index.js.map +1 -1
  77. package/lib/packageVersion.d.ts +1 -1
  78. package/lib/packageVersion.js +1 -1
  79. package/lib/packageVersion.js.map +1 -1
  80. package/lib/scheduleManager.js +15 -4
  81. package/lib/scheduleManager.js.map +1 -1
  82. package/package.json +15 -16
  83. package/src/blobManager.ts +3 -2
  84. package/src/containerRuntime.ts +10 -4
  85. package/src/dataStoreContext.ts +1 -2
  86. package/src/gc/garbageCollection.ts +274 -257
  87. package/src/gc/gcConfigs.ts +12 -11
  88. package/src/gc/gcDefinitions.ts +2 -0
  89. package/src/gc/gcHelpers.ts +20 -2
  90. package/src/gc/gcSummaryStateTracker.ts +19 -7
  91. package/src/gc/gcTelemetry.ts +52 -37
  92. package/src/gc/index.ts +1 -5
  93. package/src/packageVersion.ts +1 -1
  94. package/src/scheduleManager.ts +19 -7
  95. package/dist/gc/gcSweepReadyUsageDetection.d.ts +0 -53
  96. package/dist/gc/gcSweepReadyUsageDetection.d.ts.map +0 -1
  97. package/dist/gc/gcSweepReadyUsageDetection.js +0 -130
  98. package/dist/gc/gcSweepReadyUsageDetection.js.map +0 -1
  99. package/lib/gc/gcSweepReadyUsageDetection.d.ts +0 -53
  100. package/lib/gc/gcSweepReadyUsageDetection.d.ts.map +0 -1
  101. package/lib/gc/gcSweepReadyUsageDetection.js +0 -125
  102. package/lib/gc/gcSweepReadyUsageDetection.js.map +0 -1
  103. package/src/gc/gcSweepReadyUsageDetection.ts +0 -145
@@ -121,6 +121,11 @@ export class GarbageCollector implements IGarbageCollector {
121
121
  return this.summaryStateTracker.doesSummaryStateNeedReset;
122
122
  }
123
123
 
124
+ /** Returns the count of data stores whose GC state updated since the last summary. */
125
+ public get updatedDSCountSinceLastSummary(): number {
126
+ return this.summaryStateTracker.updatedDSCountSinceLastSummary;
127
+ }
128
+
124
129
  protected constructor(createParams: IGarbageCollectorCreateParams) {
125
130
  this.runtime = createParams.runtime;
126
131
  this.isSummarizerClient = createParams.isSummarizerClient;
@@ -197,10 +202,7 @@ export class GarbageCollector implements IGarbageCollector {
197
202
  // in the snapshot cannot be interpreted correctly. Set everything to undefined except for
198
203
  // deletedNodes because irrespective of GC versions, these nodes have been deleted and cannot be
199
204
  // brought back. The deletedNodes info is needed to identify when these nodes are used.
200
- if (
201
- this.configs.gcVersionInBaseSnapshot !==
202
- this.summaryStateTracker.currentGCVersion
203
- ) {
205
+ if (this.configs.gcVersionInEffect !== this.configs.gcVersionInBaseSnapshot) {
204
206
  return {
205
207
  gcState: undefined,
206
208
  tombstones: undefined,
@@ -429,6 +431,14 @@ export class GarbageCollector implements IGarbageCollector {
429
431
  }
430
432
  }
431
433
 
434
+ /**
435
+ * Returns a the GC details generated from the base summary. This is used to initialize the GC state of the nodes
436
+ * in the container.
437
+ */
438
+ public async getBaseGCDetails(): Promise<IGarbageCollectionDetailsBase> {
439
+ return this.baseGCDetailsP;
440
+ }
441
+
432
442
  /**
433
443
  * Runs garbage collection and updates the reference / used state of the nodes in the container.
434
444
  * @returns stats of the GC run or undefined if GC did not run.
@@ -482,53 +492,159 @@ export class GarbageCollector implements IGarbageCollector {
482
492
  logger,
483
493
  { eventName: "GarbageCollection" },
484
494
  async (event) => {
485
- await this.runPreGCSteps();
486
-
487
- // Get the runtime's GC data and run GC on the reference graph in it.
488
- const gcData = await this.runtime.getGCData(fullGC);
489
- const gcResult = runGarbageCollection(gcData.gcNodes, ["/"]);
490
-
491
- const gcStats = await this.runPostGCSteps(
492
- gcData,
493
- gcResult,
494
- logger,
495
- currentReferenceTimestampMs,
496
- );
495
+ /** Pre-GC steps */
496
+ // Ensure that state has been initialized from the base snapshot data.
497
+ await this.initializeGCStateFromBaseSnapshotP;
498
+ // Let the runtime update its pending state before GC runs.
499
+ await this.runtime.updateStateBeforeGC();
500
+
501
+ /** GC step */
502
+ const gcStats = await this.runGC(fullGC, currentReferenceTimestampMs, logger);
497
503
  event.end({ ...gcStats, timestamp: currentReferenceTimestampMs });
504
+
505
+ /** Post-GC steps */
506
+ // Log pending unreferenced events such as a node being used after inactive. This is done after GC runs and
507
+ // updates its state so that we don't send false positives based on intermediate state. For example, we may get
508
+ // reference to an unreferenced node from another unreferenced node which means the node wasn't revived.
509
+ await this.telemetryTracker.logPendingEvents(logger);
510
+ // Update the state of summary state tracker from this run's stats.
511
+ this.summaryStateTracker.updateStateFromGCRunStats(gcStats);
512
+ this.newReferencesSinceLastRun.clear();
498
513
  this.completedRuns++;
514
+
499
515
  return gcStats;
500
516
  },
501
517
  { end: true, cancel: "error" },
502
518
  );
503
519
  }
504
520
 
505
- private async runPreGCSteps() {
506
- // Ensure that state has been initialized from the base snapshot data.
507
- await this.initializeGCStateFromBaseSnapshotP;
508
- // Let the runtime update its pending state before GC runs.
509
- await this.runtime.updateStateBeforeGC();
510
- }
511
-
512
- private async runPostGCSteps(
513
- gcData: IGarbageCollectionData,
514
- gcResult: IGCResult,
515
- logger: ITelemetryLogger,
521
+ /**
522
+ * Runs garbage collection. It does the following:
523
+ * 1. It generates / analyzes the runtime's reference graph.
524
+ * 2. Generates stats for the GC run based on previous / current GC state.
525
+ * 3. Runs Mark phase.
526
+ * 4. Runs Sweep phase.
527
+ */
528
+ private async runGC(
529
+ fullGC: boolean,
516
530
  currentReferenceTimestampMs: number,
531
+ logger: ITelemetryLogger,
517
532
  ): Promise<IGCStats> {
518
- // Generate statistics from the current run. This is done before updating the current state because it
519
- // generates some of its data based on previous state of the system.
533
+ // 1. Generate / analyze the runtime's reference graph.
534
+ // Get the reference graph (gcData) and run GC algorithm to get referenced / unreferenced nodes.
535
+ const gcData = await this.runtime.getGCData(fullGC);
536
+ const gcResult = runGarbageCollection(gcData.gcNodes, ["/"]);
537
+ // Get all referenced nodes - References in this run + references between the previous and current runs.
538
+ const allReferencedNodeIds =
539
+ this.findAllNodesReferencedBetweenGCs(gcData, this.gcDataFromLastRun, logger) ??
540
+ gcResult.referencedNodeIds;
541
+
542
+ // 2. Generate stats based on the previous / current GC state.
543
+ // Must happen before running Mark / Sweep phase because previous GC state will be updated in these stages.
520
544
  const gcStats = this.generateStats(gcResult);
521
545
 
522
- // Update the current mark state and update the runtime of all used routes or ids that used as per the GC run.
523
- const sweepReadyNodes = this.updateMarkPhase(
524
- gcData,
546
+ // 3. Run the Mark phase.
547
+ // It will mark nodes as referenced / unreferenced and return a list of node ids that are ready to be swept.
548
+ const sweepReadyNodeIds = this.runMarkPhase(
525
549
  gcResult,
550
+ allReferencedNodeIds,
551
+ currentReferenceTimestampMs,
552
+ );
553
+
554
+ // 4. Run the Sweep phase.
555
+ // It will delete sweep ready nodes and return a list of deleted node ids.
556
+ const deletedNodeIds = this.runSweepPhase(
557
+ gcResult,
558
+ sweepReadyNodeIds,
526
559
  currentReferenceTimestampMs,
527
560
  logger,
528
561
  );
562
+
563
+ this.gcDataFromLastRun = cloneGCData(
564
+ gcData,
565
+ (id: string) => deletedNodeIds.includes(id) /* filter out deleted nodes */,
566
+ );
567
+ return gcStats;
568
+ }
569
+
570
+ /**
571
+ * Runs the GC Mark phase. It does the following:
572
+ * 1. Marks all referenced nodes in this run by clearing tracking for them.
573
+ * 2. Marks unreferenced nodes in this run by starting tracking for them.
574
+ * 3. Calls the runtime to update nodes that were marked referenced.
575
+ *
576
+ * @param gcResult - The result of the GC run on the gcData.
577
+ * @param allReferencedNodeIds - Nodes referenced in this GC run + referenced between previous and current GC run.
578
+ * @param currentReferenceTimestampMs - The timestamp to be used for unreferenced nodes' timestamp.
579
+ * @returns - A list of sweep ready nodes, i.e., nodes that ready to be deleted.
580
+ */
581
+ private runMarkPhase(
582
+ gcResult: IGCResult,
583
+ allReferencedNodeIds: string[],
584
+ currentReferenceTimestampMs: number,
585
+ ): string[] {
586
+ // 1. Marks all referenced nodes by clearing their unreferenced tracker, if any.
587
+ for (const nodeId of allReferencedNodeIds) {
588
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
589
+ if (nodeStateTracker !== undefined) {
590
+ // Stop tracking so as to clear out any running timers.
591
+ nodeStateTracker.stopTracking();
592
+ // Delete the node as we don't need to track it any more.
593
+ this.unreferencedNodesState.delete(nodeId);
594
+ }
595
+ }
596
+
597
+ // 2. Mark unreferenced nodes in this run by starting unreferenced tracking for them.
598
+ const sweepReadyNodeIds: string[] = [];
599
+ for (const nodeId of gcResult.deletedNodeIds) {
600
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
601
+ if (nodeStateTracker === undefined) {
602
+ this.unreferencedNodesState.set(
603
+ nodeId,
604
+ new UnreferencedStateTracker(
605
+ currentReferenceTimestampMs,
606
+ this.configs.inactiveTimeoutMs,
607
+ currentReferenceTimestampMs,
608
+ this.configs.sweepTimeoutMs,
609
+ ),
610
+ );
611
+ } else {
612
+ // If a node was already unreferenced, update its tracking information. Since the current reference time
613
+ // is from the ops seen, this will ensure that we keep updating unreferenced state as time moves forward.
614
+ nodeStateTracker.updateTracking(currentReferenceTimestampMs);
615
+
616
+ // If a node is sweep ready, store it so it can be returned.
617
+ if (nodeStateTracker.state === UnreferencedState.SweepReady) {
618
+ sweepReadyNodeIds.push(nodeId);
619
+ }
620
+ }
621
+ }
622
+
623
+ // 3. Call the runtime to update referenced nodes in this run.
529
624
  this.runtime.updateUsedRoutes(gcResult.referencedNodeIds);
530
625
 
531
- // Log events for objects that are ready to be deleted by sweep.
626
+ return sweepReadyNodeIds;
627
+ }
628
+
629
+ /**
630
+ * Runs the GC Sweep phase. It does the following:
631
+ * 1. Calls the runtime to delete nodes that are sweep ready.
632
+ * 2. Clears tracking for deleted nodes.
633
+ *
634
+ * @param gcResult - The result of the GC run on the gcData.
635
+ * @param sweepReadyNodes - List of nodes that are sweep ready.
636
+ * @param currentReferenceTimestampMs - The timestamp to be used for unreferenced nodes' timestamp.
637
+ * @param logger - The logger to be used to log any telemetry.
638
+ * @returns - A list of nodes that have been deleted.
639
+ */
640
+ private runSweepPhase(
641
+ gcResult: IGCResult,
642
+ sweepReadyNodes: string[],
643
+ currentReferenceTimestampMs: number,
644
+ logger: ITelemetryLogger,
645
+ ): string[] {
646
+ // Log events for objects that are ready to be deleted by sweep. This will give us data on sweep when
647
+ // its not enabled.
532
648
  this.telemetryTracker.logSweepEvents(
533
649
  logger,
534
650
  currentReferenceTimestampMs,
@@ -537,30 +653,136 @@ export class GarbageCollector implements IGarbageCollector {
537
653
  this.getLastSummaryTimestampMs(),
538
654
  );
539
655
 
540
- let updatedGCData: IGarbageCollectionData = gcData;
541
-
542
- if (this.configs.shouldRunSweep) {
543
- updatedGCData = this.runSweepPhase(sweepReadyNodes, gcData);
544
- } else if (this.configs.testMode) {
545
- // If we are running in GC test mode, delete objects for unused routes. This enables testing scenarios
546
- // involving access to deleted data.
656
+ /**
657
+ * Currently, there are 3 modes for sweep:
658
+ * Test mode - Unreferenced nodes are immediately deleted without waiting for them to be sweep ready.
659
+ * Tombstone mode - Sweep ready modes are marked as tombstones instead of being deleted.
660
+ * Sweep mode - Sweep ready modes are deleted.
661
+ *
662
+ * These modes serve as staging for applications that want to enable sweep by providing an incremental
663
+ * way to test and validate sweep works as expected.
664
+ */
665
+ if (this.configs.testMode) {
666
+ // If we are running in GC test mode, unreferenced nodes (gcResult.deletedNodeIds) are deleted.
547
667
  this.runtime.updateUnusedRoutes(gcResult.deletedNodeIds);
548
- } else if (this.configs.tombstoneMode) {
668
+ return [];
669
+ }
670
+
671
+ if (this.configs.tombstoneMode) {
549
672
  this.tombstones = sweepReadyNodes;
550
673
  // If we are running in GC tombstone mode, update tombstoned routes. This enables testing scenarios
551
674
  // involving access to "deleted" data without actually deleting the data from summaries.
552
- // Note: we will not tombstone in test mode.
553
675
  this.runtime.updateTombstonedRoutes(this.tombstones);
676
+ return [];
554
677
  }
555
678
 
556
- this.gcDataFromLastRun = cloneGCData(updatedGCData);
679
+ if (!this.configs.shouldRunSweep) {
680
+ return [];
681
+ }
557
682
 
558
- // Log pending unreferenced events such as a node being used after inactive. This is done after GC runs and
559
- // updates its state so that we don't send false positives based on intermediate state. For example, we may get
560
- // reference to an unreferenced node from another unreferenced node which means the node wasn't revived.
561
- await this.telemetryTracker.logPendingEvents(logger);
683
+ // 1. Call the runtime to delete sweep ready nodes. The runtime returns a list of nodes it deleted.
684
+ // TODO: GC:Validation - validate that removed routes are not double delete and that the child routes of
685
+ // removed routes are deleted as well.
686
+ const deletedNodeIds = this.runtime.deleteSweepReadyNodes(sweepReadyNodes);
562
687
 
563
- return gcStats;
688
+ // 2. Clear unreferenced state tracking for deleted nodes.
689
+ for (const nodeId of deletedNodeIds) {
690
+ const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
691
+ // TODO: GC:Validation - assert that the nodeStateTracker is defined
692
+ if (nodeStateTracker !== undefined) {
693
+ // Stop tracking so as to clear out any running timers.
694
+ nodeStateTracker.stopTracking();
695
+ // Delete the node as we don't need to track it any more.
696
+ this.unreferencedNodesState.delete(nodeId);
697
+ }
698
+ // TODO: GC:Validation - assert that the deleted node is not a duplicate
699
+ this.deletedNodes.add(nodeId);
700
+ }
701
+ return deletedNodeIds;
702
+ }
703
+
704
+ /**
705
+ * Since GC runs periodically, the GC data that is generated only tells us the state of the world at that point in
706
+ * time. There can be nodes that were referenced in between two runs and their unreferenced state needs to be
707
+ * updated. For example, in the following scenarios not updating the unreferenced timestamp can lead to deletion of
708
+ * these objects while there can be in-memory referenced to it:
709
+ * 1. A node transitions from `unreferenced -> referenced -> unreferenced` between two runs. When the reference is
710
+ * added, the object may have been accessed and in-memory reference to it added.
711
+ * 2. A reference is added from one unreferenced node to one or more unreferenced nodes. Even though the node[s] were
712
+ * unreferenced, they could have been accessed and in-memory reference to them added.
713
+ *
714
+ * This function identifies nodes that were referenced since the last run.
715
+ * If these nodes are currently unreferenced, they will be assigned new unreferenced state by the current run.
716
+ *
717
+ * @returns - a list of all nodes referenced from the last local summary until now.
718
+ */
719
+ private findAllNodesReferencedBetweenGCs(
720
+ currentGCData: IGarbageCollectionData,
721
+ previousGCData: IGarbageCollectionData | undefined,
722
+ logger: ITelemetryLogger,
723
+ ): string[] | undefined {
724
+ // If we haven't run GC before there is nothing to do.
725
+ // No previousGCData, means nothing is unreferenced, and there are no reference state trackers to clear
726
+ if (previousGCData === undefined) {
727
+ return undefined;
728
+ }
729
+
730
+ /**
731
+ * If there are references that were not explicitly notified to GC, log an error because this should never happen.
732
+ * If it does, this may result in the unreferenced timestamps of these nodes not updated when they were referenced.
733
+ */
734
+ this.telemetryTracker.logIfMissingExplicitReferences(
735
+ currentGCData,
736
+ previousGCData,
737
+ this.newReferencesSinceLastRun,
738
+ logger,
739
+ );
740
+
741
+ // No references were added since the last run so we don't have to update reference states of any unreferenced
742
+ // nodes. There is no in between state at this point.
743
+ if (this.newReferencesSinceLastRun.size === 0) {
744
+ return undefined;
745
+ }
746
+
747
+ /**
748
+ * Generate a super set of the GC data that contains the nodes and edges from last run, plus any new node and
749
+ * edges that have been added since then. To do this, combine the GC data from the last run and the current
750
+ * run, and then add the references since last run.
751
+ *
752
+ * Note on why we need to combine the data from previous run, current run and all references in between -
753
+ * 1. We need data from last run because some of its references may have been deleted since then. If those
754
+ * references added new outbound references before they were deleted, we need to detect them.
755
+ *
756
+ * 2. We need new outbound references since last run because some of them may have been deleted later. If those
757
+ * references added new outbound references before they were deleted, we need to detect them.
758
+ *
759
+ * 3. We need data from the current run because currently we may not detect when DDSes are referenced:
760
+ * - We don't require DDSes handles to be stored in a referenced DDS.
761
+ * - A new data store may have "root" DDSes already created and we don't detect them today.
762
+ */
763
+ const gcDataSuperSet = concatGarbageCollectionData(previousGCData, currentGCData);
764
+ const newOutboundRoutesSinceLastRun: string[] = [];
765
+ this.newReferencesSinceLastRun.forEach((outboundRoutes: string[], sourceNodeId: string) => {
766
+ if (gcDataSuperSet.gcNodes[sourceNodeId] === undefined) {
767
+ gcDataSuperSet.gcNodes[sourceNodeId] = outboundRoutes;
768
+ } else {
769
+ gcDataSuperSet.gcNodes[sourceNodeId].push(...outboundRoutes);
770
+ }
771
+ newOutboundRoutesSinceLastRun.push(...outboundRoutes);
772
+ });
773
+
774
+ /**
775
+ * Run GC on the above reference graph starting with root and all new outbound routes. This will generate a
776
+ * list of all nodes that could have been referenced since the last run. If any of these nodes are unreferenced,
777
+ * unreferenced, stop tracking them and remove from unreferenced list.
778
+ * Note that some of these nodes may be unreferenced now and if so, the current run will mark them as
779
+ * unreferenced and add unreferenced state.
780
+ */
781
+ const gcResult = runGarbageCollection(gcDataSuperSet.gcNodes, [
782
+ "/",
783
+ ...newOutboundRoutesSinceLastRun,
784
+ ]);
785
+ return gcResult.referencedNodeIds;
564
786
  }
565
787
 
566
788
  /**
@@ -598,10 +820,10 @@ export class GarbageCollector implements IGarbageCollector {
598
820
  public getMetadata(): IGCMetadata {
599
821
  return {
600
822
  /**
601
- * If GC is enabled, the GC data is written using the current GC version and that is the gcFeature that goes
823
+ * If GC is enabled, the GC data is written using the GC version in effect and that is the gcFeature that goes
602
824
  * into the metadata blob. If GC is disabled, the gcFeature is 0.
603
825
  */
604
- gcFeature: this.configs.gcEnabled ? this.summaryStateTracker.currentGCVersion : 0,
826
+ gcFeature: this.configs.gcEnabled ? this.configs.gcVersionInEffect : 0,
605
827
  gcFeatureMatrix: this.configs.persistedGcFeatureMatrix,
606
828
  sessionExpiryTimeoutMs: this.configs.sessionExpiryTimeoutMs,
607
829
  sweepEnabled: false, // DEPRECATED - to be removed
@@ -609,14 +831,6 @@ export class GarbageCollector implements IGarbageCollector {
609
831
  };
610
832
  }
611
833
 
612
- /**
613
- * Returns a the GC details generated from the base summary. This is used to initialize the GC state of the nodes
614
- * in the container.
615
- */
616
- public async getBaseGCDetails(): Promise<IGarbageCollectionDetailsBase> {
617
- return this.baseGCDetailsP;
618
- }
619
-
620
834
  /**
621
835
  * Called to refresh the latest summary state. This happens when either a pending summary is acked or a snapshot
622
836
  * is downloaded and should be used to update the state.
@@ -674,7 +888,7 @@ export class GarbageCollector implements IGarbageCollector {
674
888
  }
675
889
 
676
890
  this.telemetryTracker.nodeUsed({
677
- nodeId: nodePath,
891
+ id: nodePath,
678
892
  usageType: reason,
679
893
  currentReferenceTimestampMs:
680
894
  timestampMs ?? this.runtime.getCurrentReferenceTimestampMs(),
@@ -703,7 +917,7 @@ export class GarbageCollector implements IGarbageCollector {
703
917
  this.newReferencesSinceLastRun.set(fromNodePath, outboundRoutes);
704
918
 
705
919
  this.telemetryTracker.nodeUsed({
706
- nodeId: toNodePath,
920
+ id: toNodePath,
707
921
  usageType: "Revived",
708
922
  currentReferenceTimestampMs: this.runtime.getCurrentReferenceTimestampMs(),
709
923
  packagePath: undefined,
@@ -727,203 +941,6 @@ export class GarbageCollector implements IGarbageCollector {
727
941
  this.sessionExpiryTimer = undefined;
728
942
  }
729
943
 
730
- /**
731
- * Updates the state of the system as per the current GC run. It does the following:
732
- * 1. Sets up the current GC state as per the gcData.
733
- * 2. Starts tracking for nodes that have become unreferenced in this run.
734
- * 3. Clears tracking for nodes that were unreferenced but became referenced in this run.
735
- * @param gcData - The data representing the reference graph on which GC is run.
736
- * @param gcResult - The result of the GC run on the gcData.
737
- * @param currentReferenceTimestampMs - The timestamp to be used for unreferenced nodes' timestamp.
738
- * @returns - A list of sweep ready nodes. (Nodes ready to be deleted)
739
- */
740
- private updateMarkPhase(
741
- gcData: IGarbageCollectionData,
742
- gcResult: IGCResult,
743
- currentReferenceTimestampMs: number,
744
- logger: ITelemetryLogger,
745
- ) {
746
- // Get references from the current GC run + references between previous and current run and then update each
747
- // node's state
748
- const allNodesReferencedBetweenGCs =
749
- this.findAllNodesReferencedBetweenGCs(gcData, this.gcDataFromLastRun, logger) ??
750
- gcResult.referencedNodeIds;
751
- this.newReferencesSinceLastRun.clear();
752
-
753
- // Iterate through the referenced nodes and stop tracking if they were unreferenced before.
754
- for (const nodeId of allNodesReferencedBetweenGCs) {
755
- const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
756
- if (nodeStateTracker !== undefined) {
757
- // Stop tracking so as to clear out any running timers.
758
- nodeStateTracker.stopTracking();
759
- // Delete the node as we don't need to track it any more.
760
- this.unreferencedNodesState.delete(nodeId);
761
- }
762
- }
763
-
764
- /**
765
- * If a node became unreferenced in this run, start tracking it.
766
- * If a node was already unreferenced, update its tracking information. Since the current reference time is
767
- * from the ops seen, this will ensure that we keep updating the unreferenced state as time moves forward.
768
- *
769
- * If a node is sweep ready, store and then return it.
770
- */
771
- const sweepReadyNodes: string[] = [];
772
- for (const nodeId of gcResult.deletedNodeIds) {
773
- const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
774
- if (nodeStateTracker === undefined) {
775
- this.unreferencedNodesState.set(
776
- nodeId,
777
- new UnreferencedStateTracker(
778
- currentReferenceTimestampMs,
779
- this.configs.inactiveTimeoutMs,
780
- currentReferenceTimestampMs,
781
- this.configs.sweepTimeoutMs,
782
- ),
783
- );
784
- } else {
785
- nodeStateTracker.updateTracking(currentReferenceTimestampMs);
786
- if (nodeStateTracker.state === UnreferencedState.SweepReady) {
787
- sweepReadyNodes.push(nodeId);
788
- }
789
- }
790
- }
791
-
792
- return sweepReadyNodes;
793
- }
794
-
795
- /**
796
- * Deletes nodes from both the runtime and garbage collection
797
- * @param sweepReadyNodes - nodes that are ready to be deleted
798
- */
799
- private runSweepPhase(sweepReadyNodes: string[], gcData: IGarbageCollectionData) {
800
- // TODO: GC:Validation - validate that removed routes are not double deleted
801
- // TODO: GC:Validation - validate that the child routes of removed routes are deleted as well
802
- const sweptRoutes = this.runtime.deleteSweepReadyNodes(sweepReadyNodes);
803
- const updatedGCData = this.deleteSweptRoutes(sweptRoutes, gcData);
804
-
805
- for (const nodeId of sweptRoutes) {
806
- const nodeStateTracker = this.unreferencedNodesState.get(nodeId);
807
- // TODO: GC:Validation - assert that the nodeStateTracker is defined
808
- if (nodeStateTracker !== undefined) {
809
- // Stop tracking so as to clear out any running timers.
810
- nodeStateTracker.stopTracking();
811
- // Delete the node as we don't need to track it any more.
812
- this.unreferencedNodesState.delete(nodeId);
813
- }
814
- // TODO: GC:Validation - assert that the deleted node is not a duplicate
815
- this.deletedNodes.add(nodeId);
816
- }
817
-
818
- return updatedGCData;
819
- }
820
-
821
- /**
822
- * @returns IGarbageCollectionData after deleting the sweptRoutes from the gcData
823
- */
824
- private deleteSweptRoutes(
825
- sweptRoutes: string[],
826
- gcData: IGarbageCollectionData,
827
- ): IGarbageCollectionData {
828
- const sweptRoutesSet = new Set<string>(sweptRoutes);
829
- const gcNodes: { [id: string]: string[] } = {};
830
- for (const [id, outboundRoutes] of Object.entries(gcData.gcNodes)) {
831
- if (!sweptRoutesSet.has(id)) {
832
- gcNodes[id] = Array.from(outboundRoutes);
833
- }
834
- }
835
-
836
- // TODO: GC:Validation - assert that the nodeId is in gcData
837
-
838
- return {
839
- gcNodes,
840
- };
841
- }
842
-
843
- /**
844
- * Since GC runs periodically, the GC data that is generated only tells us the state of the world at that point in
845
- * time. There can be nodes that were referenced in between two runs and their unreferenced state needs to be
846
- * updated. For example, in the following scenarios not updating the unreferenced timestamp can lead to deletion of
847
- * these objects while there can be in-memory referenced to it:
848
- * 1. A node transitions from `unreferenced -> referenced -> unreferenced` between two runs. When the reference is
849
- * added, the object may have been accessed and in-memory reference to it added.
850
- * 2. A reference is added from one unreferenced node to one or more unreferenced nodes. Even though the node[s] were
851
- * unreferenced, they could have been accessed and in-memory reference to them added.
852
- *
853
- * This function identifies nodes that were referenced since the last run.
854
- * If these nodes are currently unreferenced, they will be assigned new unreferenced state by the current run.
855
- *
856
- * @returns - a list of all nodes referenced from the last local summary until now.
857
- */
858
- private findAllNodesReferencedBetweenGCs(
859
- currentGCData: IGarbageCollectionData,
860
- previousGCData: IGarbageCollectionData | undefined,
861
- logger: ITelemetryLogger,
862
- ): string[] | undefined {
863
- // If we haven't run GC before there is nothing to do.
864
- // No previousGCData, means nothing is unreferenced, and there are no reference state trackers to clear
865
- if (previousGCData === undefined) {
866
- return undefined;
867
- }
868
-
869
- /**
870
- * If there are references that were not explicitly notified to GC, log an error because this should never happen.
871
- * If it does, this may result in the unreferenced timestamps of these nodes not updated when they were referenced.
872
- */
873
- this.telemetryTracker.logIfMissingExplicitReferences(
874
- currentGCData,
875
- previousGCData,
876
- this.newReferencesSinceLastRun,
877
- logger,
878
- );
879
-
880
- // No references were added since the last run so we don't have to update reference states of any unreferenced
881
- // nodes. There is no in between state at this point.
882
- if (this.newReferencesSinceLastRun.size === 0) {
883
- return undefined;
884
- }
885
-
886
- /**
887
- * Generate a super set of the GC data that contains the nodes and edges from last run, plus any new node and
888
- * edges that have been added since then. To do this, combine the GC data from the last run and the current
889
- * run, and then add the references since last run.
890
- *
891
- * Note on why we need to combine the data from previous run, current run and all references in between -
892
- * 1. We need data from last run because some of its references may have been deleted since then. If those
893
- * references added new outbound references before they were deleted, we need to detect them.
894
- *
895
- * 2. We need new outbound references since last run because some of them may have been deleted later. If those
896
- * references added new outbound references before they were deleted, we need to detect them.
897
- *
898
- * 3. We need data from the current run because currently we may not detect when DDSes are referenced:
899
- * - We don't require DDSes handles to be stored in a referenced DDS.
900
- * - A new data store may have "root" DDSes already created and we don't detect them today.
901
- */
902
- const gcDataSuperSet = concatGarbageCollectionData(previousGCData, currentGCData);
903
- const newOutboundRoutesSinceLastRun: string[] = [];
904
- this.newReferencesSinceLastRun.forEach((outboundRoutes: string[], sourceNodeId: string) => {
905
- if (gcDataSuperSet.gcNodes[sourceNodeId] === undefined) {
906
- gcDataSuperSet.gcNodes[sourceNodeId] = outboundRoutes;
907
- } else {
908
- gcDataSuperSet.gcNodes[sourceNodeId].push(...outboundRoutes);
909
- }
910
- newOutboundRoutesSinceLastRun.push(...outboundRoutes);
911
- });
912
-
913
- /**
914
- * Run GC on the above reference graph starting with root and all new outbound routes. This will generate a
915
- * list of all nodes that could have been referenced since the last run. If any of these nodes are unreferenced,
916
- * unreferenced, stop tracking them and remove from unreferenced list.
917
- * Note that some of these nodes may be unreferenced now and if so, the current run will mark them as
918
- * unreferenced and add unreferenced state.
919
- */
920
- const gcResult = runGarbageCollection(gcDataSuperSet.gcNodes, [
921
- "/",
922
- ...newOutboundRoutesSinceLastRun,
923
- ]);
924
- return gcResult.referencedNodeIds;
925
- }
926
-
927
944
  /**
928
945
  * Generates the stats of a garbage collection run from the given results of the run.
929
946
  * @param gcResult - The result of a GC run.
@@ -106,21 +106,26 @@ export function generateGCConfigs(
106
106
  createParams.gcOptions[gcSweepGenerationOptionName] /* currentGeneration */,
107
107
  );
108
108
 
109
+ // If version upgrade is not enabled, fall back to the stable GC version.
110
+ const gcVersionInEffect =
111
+ mc.config.getBoolean(gcVersionUpgradeToV3Key) === true ? currentGCVersion : stableGCVersion;
112
+
113
+ // The GC version is up-to-date if the GC version in effect is at least equal to the GC version in base snapshot.
114
+ // If it is not up-to-date, there is a newer version of GC out there which is more reliable than this. So, GC
115
+ // should not run as it may produce incorrect / unreliable state.
116
+ const isGCVersionUpToDate =
117
+ gcVersionInBaseSnapshot === undefined || gcVersionInEffect >= gcVersionInBaseSnapshot;
118
+
109
119
  /**
110
120
  * Whether GC should run or not. The following conditions have to be met to run sweep:
111
- *
112
121
  * 1. GC should be enabled for this container.
113
- *
114
122
  * 2. GC should not be disabled via disableGC GC option.
115
- *
123
+ * 3. The current GC version should be greater of equal to the GC version in the base snapshot.
116
124
  * These conditions can be overridden via runGCKey feature flag.
117
125
  */
118
126
  const shouldRunGC =
119
127
  mc.config.getBoolean(runGCKey) ??
120
- // GC must be enabled for the document.
121
- (gcEnabled &&
122
- // GC must not be disabled via GC options.
123
- !createParams.gcOptions.disableGC);
128
+ (gcEnabled && !createParams.gcOptions.disableGC && isGCVersionUpToDate);
124
129
 
125
130
  /**
126
131
  * Whether sweep should run or not. The following conditions have to be met to run sweep:
@@ -156,10 +161,6 @@ export function generateGCConfigs(
156
161
  const tombstoneMode = !shouldRunSweep && mc.config.getBoolean(disableTombstoneKey) !== true;
157
162
  const runFullGC = createParams.gcOptions.runFullGC;
158
163
 
159
- // If version upgrade is not enabled, fall back to the stable GC version.
160
- const gcVersionInEffect =
161
- mc.config.getBoolean(gcVersionUpgradeToV3Key) === true ? currentGCVersion : stableGCVersion;
162
-
163
164
  return {
164
165
  gcEnabled,
165
166
  sweepEnabled,