@fluidframework/container-loader 2.0.0-rc.3.0.2 → 2.0.0-rc.4.0.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 (188) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/api-report/container-loader.api.md +5 -1
  3. package/dist/attachment.d.ts +3 -2
  4. package/dist/attachment.d.ts.map +1 -1
  5. package/dist/attachment.js +5 -5
  6. package/dist/attachment.js.map +1 -1
  7. package/dist/audience.d.ts +6 -4
  8. package/dist/audience.d.ts.map +1 -1
  9. package/dist/audience.js +18 -3
  10. package/dist/audience.js.map +1 -1
  11. package/dist/catchUpMonitor.d.ts +1 -1
  12. package/dist/catchUpMonitor.d.ts.map +1 -1
  13. package/dist/catchUpMonitor.js.map +1 -1
  14. package/dist/connectionManager.d.ts +7 -3
  15. package/dist/connectionManager.d.ts.map +1 -1
  16. package/dist/connectionManager.js +57 -38
  17. package/dist/connectionManager.js.map +1 -1
  18. package/dist/connectionStateHandler.d.ts +31 -10
  19. package/dist/connectionStateHandler.d.ts.map +1 -1
  20. package/dist/connectionStateHandler.js +49 -36
  21. package/dist/connectionStateHandler.js.map +1 -1
  22. package/dist/container.d.ts +22 -13
  23. package/dist/container.d.ts.map +1 -1
  24. package/dist/container.js +145 -117
  25. package/dist/container.js.map +1 -1
  26. package/dist/containerContext.d.ts +3 -3
  27. package/dist/containerContext.d.ts.map +1 -1
  28. package/dist/containerContext.js.map +1 -1
  29. package/dist/containerStorageAdapter.d.ts +12 -3
  30. package/dist/containerStorageAdapter.d.ts.map +1 -1
  31. package/dist/containerStorageAdapter.js +42 -4
  32. package/dist/containerStorageAdapter.js.map +1 -1
  33. package/dist/contracts.d.ts +2 -2
  34. package/dist/contracts.d.ts.map +1 -1
  35. package/dist/contracts.js.map +1 -1
  36. package/dist/debugLogger.d.ts +1 -2
  37. package/dist/debugLogger.d.ts.map +1 -1
  38. package/dist/debugLogger.js.map +1 -1
  39. package/dist/deltaManager.d.ts +5 -6
  40. package/dist/deltaManager.d.ts.map +1 -1
  41. package/dist/deltaManager.js +29 -24
  42. package/dist/deltaManager.js.map +1 -1
  43. package/dist/deltaQueue.d.ts +1 -1
  44. package/dist/deltaQueue.d.ts.map +1 -1
  45. package/dist/deltaQueue.js.map +1 -1
  46. package/dist/error.d.ts +1 -2
  47. package/dist/error.d.ts.map +1 -1
  48. package/dist/error.js.map +1 -1
  49. package/dist/index.d.ts +1 -0
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +3 -1
  52. package/dist/index.js.map +1 -1
  53. package/dist/legacy.d.ts +2 -2
  54. package/dist/loadPaused.d.ts +35 -0
  55. package/dist/loadPaused.d.ts.map +1 -0
  56. package/dist/loadPaused.js +115 -0
  57. package/dist/loadPaused.js.map +1 -0
  58. package/dist/loader.d.ts +1 -1
  59. package/dist/loader.d.ts.map +1 -1
  60. package/dist/loader.js +1 -14
  61. package/dist/loader.js.map +1 -1
  62. package/dist/location-redirection-utilities/resolveWithLocationRedirection.d.ts.map +1 -1
  63. package/dist/location-redirection-utilities/resolveWithLocationRedirection.js +4 -4
  64. package/dist/location-redirection-utilities/resolveWithLocationRedirection.js.map +1 -1
  65. package/dist/packageVersion.d.ts +1 -1
  66. package/dist/packageVersion.js +1 -1
  67. package/dist/packageVersion.js.map +1 -1
  68. package/dist/protocol.d.ts.map +1 -1
  69. package/dist/protocol.js +3 -0
  70. package/dist/protocol.js.map +1 -1
  71. package/dist/public.d.ts +1 -1
  72. package/dist/retriableDocumentStorageService.d.ts +1 -1
  73. package/dist/retriableDocumentStorageService.d.ts.map +1 -1
  74. package/dist/retriableDocumentStorageService.js.map +1 -1
  75. package/dist/serializedStateManager.d.ts +89 -9
  76. package/dist/serializedStateManager.d.ts.map +1 -1
  77. package/dist/serializedStateManager.js +150 -34
  78. package/dist/serializedStateManager.js.map +1 -1
  79. package/dist/utils.d.ts +11 -1
  80. package/dist/utils.d.ts.map +1 -1
  81. package/dist/utils.js +29 -14
  82. package/dist/utils.js.map +1 -1
  83. package/lib/attachment.d.ts +3 -2
  84. package/lib/attachment.d.ts.map +1 -1
  85. package/lib/attachment.js +5 -5
  86. package/lib/attachment.js.map +1 -1
  87. package/lib/audience.d.ts +6 -4
  88. package/lib/audience.d.ts.map +1 -1
  89. package/lib/audience.js +19 -4
  90. package/lib/audience.js.map +1 -1
  91. package/lib/catchUpMonitor.d.ts +1 -1
  92. package/lib/catchUpMonitor.d.ts.map +1 -1
  93. package/lib/catchUpMonitor.js.map +1 -1
  94. package/lib/connectionManager.d.ts +7 -3
  95. package/lib/connectionManager.d.ts.map +1 -1
  96. package/lib/connectionManager.js +36 -17
  97. package/lib/connectionManager.js.map +1 -1
  98. package/lib/connectionStateHandler.d.ts +31 -10
  99. package/lib/connectionStateHandler.d.ts.map +1 -1
  100. package/lib/connectionStateHandler.js +49 -36
  101. package/lib/connectionStateHandler.js.map +1 -1
  102. package/lib/container.d.ts +22 -13
  103. package/lib/container.d.ts.map +1 -1
  104. package/lib/container.js +146 -118
  105. package/lib/container.js.map +1 -1
  106. package/lib/containerContext.d.ts +3 -3
  107. package/lib/containerContext.d.ts.map +1 -1
  108. package/lib/containerContext.js.map +1 -1
  109. package/lib/containerStorageAdapter.d.ts +12 -3
  110. package/lib/containerStorageAdapter.d.ts.map +1 -1
  111. package/lib/containerStorageAdapter.js +42 -4
  112. package/lib/containerStorageAdapter.js.map +1 -1
  113. package/lib/contracts.d.ts +2 -2
  114. package/lib/contracts.d.ts.map +1 -1
  115. package/lib/contracts.js +1 -1
  116. package/lib/contracts.js.map +1 -1
  117. package/lib/debugLogger.d.ts +1 -2
  118. package/lib/debugLogger.d.ts.map +1 -1
  119. package/lib/debugLogger.js.map +1 -1
  120. package/lib/deltaManager.d.ts +5 -6
  121. package/lib/deltaManager.d.ts.map +1 -1
  122. package/lib/deltaManager.js +10 -5
  123. package/lib/deltaManager.js.map +1 -1
  124. package/lib/deltaQueue.d.ts +1 -1
  125. package/lib/deltaQueue.d.ts.map +1 -1
  126. package/lib/deltaQueue.js.map +1 -1
  127. package/lib/error.d.ts +1 -2
  128. package/lib/error.d.ts.map +1 -1
  129. package/lib/error.js.map +1 -1
  130. package/lib/index.d.ts +1 -0
  131. package/lib/index.d.ts.map +1 -1
  132. package/lib/index.js +1 -0
  133. package/lib/index.js.map +1 -1
  134. package/lib/legacy.d.ts +2 -2
  135. package/lib/loadPaused.d.ts +35 -0
  136. package/lib/loadPaused.d.ts.map +1 -0
  137. package/lib/loadPaused.js +111 -0
  138. package/lib/loadPaused.js.map +1 -0
  139. package/lib/loader.d.ts +1 -1
  140. package/lib/loader.d.ts.map +1 -1
  141. package/lib/loader.js +3 -16
  142. package/lib/loader.js.map +1 -1
  143. package/lib/location-redirection-utilities/resolveWithLocationRedirection.d.ts.map +1 -1
  144. package/lib/location-redirection-utilities/resolveWithLocationRedirection.js +1 -1
  145. package/lib/location-redirection-utilities/resolveWithLocationRedirection.js.map +1 -1
  146. package/lib/packageVersion.d.ts +1 -1
  147. package/lib/packageVersion.js +1 -1
  148. package/lib/packageVersion.js.map +1 -1
  149. package/lib/protocol.d.ts.map +1 -1
  150. package/lib/protocol.js +3 -0
  151. package/lib/protocol.js.map +1 -1
  152. package/lib/public.d.ts +1 -1
  153. package/lib/retriableDocumentStorageService.d.ts +1 -1
  154. package/lib/retriableDocumentStorageService.d.ts.map +1 -1
  155. package/lib/retriableDocumentStorageService.js +1 -1
  156. package/lib/retriableDocumentStorageService.js.map +1 -1
  157. package/lib/serializedStateManager.d.ts +89 -9
  158. package/lib/serializedStateManager.d.ts.map +1 -1
  159. package/lib/serializedStateManager.js +146 -30
  160. package/lib/serializedStateManager.js.map +1 -1
  161. package/lib/tsdoc-metadata.json +1 -1
  162. package/lib/utils.d.ts +11 -1
  163. package/lib/utils.d.ts.map +1 -1
  164. package/lib/utils.js +15 -1
  165. package/lib/utils.js.map +1 -1
  166. package/package.json +24 -21
  167. package/src/attachment.ts +12 -13
  168. package/src/audience.ts +30 -9
  169. package/src/catchUpMonitor.ts +1 -1
  170. package/src/connectionManager.ts +45 -22
  171. package/src/connectionStateHandler.ts +78 -45
  172. package/src/container.ts +181 -160
  173. package/src/containerContext.ts +2 -2
  174. package/src/containerStorageAdapter.ts +61 -6
  175. package/src/contracts.ts +5 -4
  176. package/src/debugLogger.ts +1 -1
  177. package/src/deltaManager.ts +15 -8
  178. package/src/deltaQueue.ts +1 -1
  179. package/src/error.ts +1 -1
  180. package/src/index.ts +1 -0
  181. package/src/loadPaused.ts +140 -0
  182. package/src/loader.ts +6 -23
  183. package/src/location-redirection-utilities/resolveWithLocationRedirection.ts +1 -1
  184. package/src/packageVersion.ts +1 -1
  185. package/src/protocol.ts +4 -0
  186. package/src/retriableDocumentStorageService.ts +5 -2
  187. package/src/serializedStateManager.ts +215 -48
  188. package/src/utils.ts +19 -1
@@ -5,9 +5,14 @@
5
5
  import { IGetPendingLocalStateProps, IRuntime } from "@fluidframework/container-definitions/internal";
6
6
  import { IDocumentStorageService, IResolvedUrl, ISnapshot } from "@fluidframework/driver-definitions/internal";
7
7
  import { ISequencedDocumentMessage, ISnapshotTree, IVersion } from "@fluidframework/protocol-definitions";
8
- import { ITelemetryLoggerExt } from "@fluidframework/telemetry-utils";
9
8
  import { MonitoringContext } from "@fluidframework/telemetry-utils/internal";
9
+ import type { ITelemetryBaseLogger, IEventProvider, IEvent } from "@fluidframework/core-interfaces";
10
10
  import { ISerializableBlobContents } from "./containerStorageAdapter.js";
11
+ /**
12
+ * This is very similar to {@link @fluidframework/protocol-definitions/internal#ISnapshot}, but the difference is
13
+ * that the blobs of ISnapshot are of type ArrayBufferLike, while the blobs of this interface are serializable because
14
+ * they are already converted to string.
15
+ */
11
16
  export interface SnapshotWithBlobs {
12
17
  /**
13
18
  * Snapshot from which container initially loaded.
@@ -22,18 +27,34 @@ export interface SnapshotWithBlobs {
22
27
  /**
23
28
  * State saved by a container at close time, to be used to load a new instance
24
29
  * of the container to the same state
30
+ *
31
+ * This is very similar to {@link @fluidframework/protocol-definitions/internal#ISnapshot}, but the difference is
32
+ * that the blobs of ISnapshot are of type ArrayBufferLike, while the blobs of this interface are serializable because
33
+ * they are already converted to string.
34
+ *
25
35
  * @internal
26
36
  */
27
37
  export interface IPendingContainerState extends SnapshotWithBlobs {
38
+ /** This container was attached (as opposed to IPendingDetachedContainerState.attached which is false) */
28
39
  attached: true;
40
+ /**
41
+ * Runtime-specific state that will be needed to properly rehydrate
42
+ * (it's included in ContainerContext passed to instantiateRuntime)
43
+ */
29
44
  pendingRuntimeState: unknown;
45
+ /**
46
+ * Any group snapshots (aka delay-loaded) we've downloaded from the service for this container
47
+ */
48
+ loadedGroupIdSnapshots?: Record<string, ISnapshotInfo>;
30
49
  /**
31
50
  * All ops since base snapshot sequence number up to the latest op
32
51
  * seen when the container was closed. Used to apply stashed (saved pending)
33
52
  * ops at the same sequence number at which they were made.
34
53
  */
35
54
  savedOps: ISequencedDocumentMessage[];
55
+ /** The Container's URL in the service, needed to hook up the driver during rehydration */
36
56
  url: string;
57
+ /** If the Container was connected when serialized, its clientId. Used as the initial clientId upon rehydration, until reconnected. */
37
58
  clientId?: string;
38
59
  }
39
60
  /**
@@ -42,41 +63,99 @@ export interface IPendingContainerState extends SnapshotWithBlobs {
42
63
  * @internal
43
64
  */
44
65
  export interface IPendingDetachedContainerState extends SnapshotWithBlobs {
66
+ /** This container was not attached (as opposed to IPendingContainerState.attached which is true) */
45
67
  attached: false;
68
+ /** Indicates whether we expect the rehydrated container to have non-empty Detached Blob Storage */
46
69
  hasAttachmentBlobs: boolean;
70
+ /**
71
+ * Runtime-specific state that will be needed to properly rehydrate
72
+ * (it's included in ContainerContext passed to instantiateRuntime)
73
+ */
47
74
  pendingRuntimeState?: unknown;
48
75
  }
49
76
  export interface ISnapshotInfo extends SnapshotWithBlobs {
50
77
  snapshotSequenceNumber: number;
78
+ snapshotFetchedTime?: number | undefined;
79
+ }
80
+ export type ISerializedStateManagerDocumentStorageService = Pick<IDocumentStorageService, "getSnapshot" | "getSnapshotTree" | "getVersions" | "readBlob"> & {
81
+ loadedGroupIdSnapshots: Record<string, ISnapshot>;
82
+ };
83
+ interface ISerializerEvent extends IEvent {
84
+ (event: "saved", listener: (dirty: boolean) => void): void;
51
85
  }
86
+ /**
87
+ * Helper class to manage the state of the container needed for proper serialization.
88
+ *
89
+ * It holds the pendingLocalState the container was rehydrated from (if any),
90
+ * as well as the snapshot to be used for serialization.
91
+ * It also keeps track of container dirty state and which local ops have been processed
92
+ */
52
93
  export declare class SerializedStateManager {
53
94
  private readonly pendingLocalState;
54
95
  private readonly storageAdapter;
55
96
  private readonly _offlineLoadEnabled;
56
- private readonly newSnapshotFetched?;
97
+ private readonly containerDirty;
57
98
  private readonly processedOps;
58
- private snapshot;
59
99
  private readonly mc;
100
+ private snapshot;
60
101
  private latestSnapshot;
61
- private refreshSnapshot;
62
- constructor(pendingLocalState: IPendingContainerState | undefined, subLogger: ITelemetryLoggerExt, storageAdapter: Pick<IDocumentStorageService, "readBlob" | "getSnapshotTree" | "getSnapshot" | "getVersions">, _offlineLoadEnabled: boolean, newSnapshotFetched?: (() => void) | undefined);
102
+ private refreshSnapshotP;
103
+ private readonly lastSavedOpSequenceNumber;
104
+ /**
105
+ * @param pendingLocalState - The pendingLocalState being rehydrated, if any (undefined when loading directly from storage)
106
+ * @param subLogger - Container's logger to use as parent for our logger
107
+ * @param storageAdapter - Storage adapter for fetching snapshots
108
+ * @param _offlineLoadEnabled - Is serializing/rehydrating containers allowed?
109
+ * @param containerEvent - Source of the "saved" event when the container has all its pending state uploaded
110
+ * @param containerDirty - Is the container "dirty"? That's the opposite of "saved" - there is pending state that may not have been received yet by the service.
111
+ */
112
+ constructor(pendingLocalState: IPendingContainerState | undefined, subLogger: ITelemetryBaseLogger, storageAdapter: ISerializedStateManagerDocumentStorageService, _offlineLoadEnabled: boolean, containerEvent: IEventProvider<ISerializerEvent>, containerDirty: () => boolean);
63
113
  get offlineLoadEnabled(): boolean;
114
+ /**
115
+ * Promise that will resolve (or reject) once we've tried to download the latest snapshot(s) from storage
116
+ */
117
+ get waitForInitialRefresh(): Promise<void> | undefined;
118
+ /**
119
+ * Called whenever an incoming op is processed by the Container
120
+ */
64
121
  addProcessedOp(message: ISequencedDocumentMessage): void;
122
+ /**
123
+ * This wraps the basic functionality of fetching the snapshot for this container during Container load.
124
+ *
125
+ * If we have pendingLocalState, we get the snapshot from there.
126
+ * Otherwise, fetch it from storage (according to specifiedVersion if provided)
127
+ *
128
+ * @param specifiedVersion - If a version is specified and we don't have pendingLocalState, fetch this version from storage
129
+ * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree.
130
+ * @returns The snapshot to boot the container from
131
+ */
65
132
  fetchSnapshot(specifiedVersion: string | undefined, supportGetSnapshotApi: boolean): Promise<{
66
- baseSnapshot: ISnapshotTree;
133
+ baseSnapshot: ISnapshotTree | ISnapshot;
67
134
  version: IVersion | undefined;
68
135
  }>;
136
+ /**
137
+ * Fetch the latest snapshot for the container, including delay-loaded groupIds if pendingLocalState was provided and contained any groupIds.
138
+ * Note that this will update the StorageAdapter's cached snapshots for the groupIds (if present)
139
+ *
140
+ * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree (must be true to fetch by groupIds)
141
+ */
142
+ private refreshLatestSnapshot;
69
143
  /**
70
144
  * Updates class snapshot and processedOps if we have a new snapshot and it's among processedOps range.
71
145
  */
72
146
  private updateSnapshotAndProcessedOpsMaybe;
73
147
  /**
148
+ * When the Container attaches, we need to stash the initial snapshot (a form of the attach summary).
74
149
  * This method is only meant to be used by Container.attach() to set the initial
75
150
  * base snapshot when attaching.
76
- * @param snapshot - snapshot and blobs collected while attaching
151
+ * @param snapshot - snapshot and blobs collected while attaching (a form of the attach summary)
77
152
  */
78
153
  setInitialSnapshot(snapshot: SnapshotWithBlobs | undefined): void;
79
- getPendingLocalStateCore(props: IGetPendingLocalStateProps, clientId: string | undefined, runtime: Pick<IRuntime, "getPendingLocalState">, resolvedUrl: IResolvedUrl): Promise<string>;
154
+ /**
155
+ * Assembles and serializes the {@link IPendingContainerState} for the container,
156
+ * to be stored and used to rehydrate the container at a later time.
157
+ */
158
+ getPendingLocalState(props: IGetPendingLocalStateProps, clientId: string | undefined, runtime: Pick<IRuntime, "getPendingLocalState">, resolvedUrl: IResolvedUrl): Promise<string>;
80
159
  }
81
160
  /**
82
161
  * Retrieves the most recent snapshot and returns its info.
@@ -86,7 +165,7 @@ export declare class SerializedStateManager {
86
165
  * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree.
87
166
  * @returns a SnapshotInfo object containing the snapshot tree, snapshot blobs and its sequence number.
88
167
  */
89
- export declare function getLatestSnapshotInfo(mc: MonitoringContext, storageAdapter: Pick<IDocumentStorageService, "getSnapshot" | "getSnapshotTree" | "getVersions" | "readBlob">, supportGetSnapshotApi: boolean): Promise<ISnapshotInfo | undefined>;
168
+ export declare function getLatestSnapshotInfo(mc: MonitoringContext, storageAdapter: ISerializedStateManagerDocumentStorageService, supportGetSnapshotApi: boolean): Promise<ISnapshotInfo | undefined>;
90
169
  /**
91
170
  * Fetches an ISnapshot from a storage adapter based on the specified version.
92
171
  *
@@ -111,4 +190,5 @@ export declare function fetchISnapshotTree(mc: MonitoringContext, storageAdapter
111
190
  snapshot?: ISnapshotTree;
112
191
  version?: IVersion | undefined;
113
192
  }>;
193
+ export {};
114
194
  //# sourceMappingURL=serializedStateManager.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"serializedStateManager.d.ts","sourceRoot":"","sources":["../src/serializedStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACN,0BAA0B,EAC1B,QAAQ,EACR,MAAM,gDAAgD,CAAC;AAExD,OAAO,EACN,uBAAuB,EACvB,YAAY,EACZ,SAAS,EACT,MAAM,6CAA6C,CAAC;AAErD,OAAO,EAEN,yBAAyB,EACzB,aAAa,EACb,QAAQ,EACR,MAAM,sCAAsC,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c,EAAE,IAAI,CAAC,uBAAuB,EAAE,iBAAiB,GAAG,aAAa,CAAC,EAChF,gBAAgB,EAAE,MAAM,GAAG,SAAS,GAClC,OAAO,CAAC;IAAE,QAAQ,CAAC,EAAE,aAAa,CAAC;IAAC,OAAO,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAA;CAAE,CAAC,CAsBvE"}
@@ -2,41 +2,85 @@
2
2
  * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
3
  * Licensed under the MIT License.
4
4
  */
5
+ import { stringToBuffer } from "@fluid-internal/client-utils";
5
6
  import { assert } from "@fluidframework/core-utils/internal";
6
- import { isInstanceOfISnapshot } from "@fluidframework/driver-utils/internal";
7
+ import { FetchSource, } from "@fluidframework/driver-definitions/internal";
8
+ import { getSnapshotTree } from "@fluidframework/driver-utils/internal";
7
9
  import { PerformanceEvent, UsageError, createChildMonitoringContext, } from "@fluidframework/telemetry-utils/internal";
8
10
  import { getBlobContentsFromTree } from "./containerStorageAdapter.js";
9
- import { getDocumentAttributes } from "./utils.js";
11
+ import { convertSnapshotToSnapshotInfo, getDocumentAttributes } from "./utils.js";
12
+ /**
13
+ * Helper class to manage the state of the container needed for proper serialization.
14
+ *
15
+ * It holds the pendingLocalState the container was rehydrated from (if any),
16
+ * as well as the snapshot to be used for serialization.
17
+ * It also keeps track of container dirty state and which local ops have been processed
18
+ */
10
19
  export class SerializedStateManager {
11
- constructor(pendingLocalState, subLogger, storageAdapter, _offlineLoadEnabled, newSnapshotFetched) {
20
+ /**
21
+ * @param pendingLocalState - The pendingLocalState being rehydrated, if any (undefined when loading directly from storage)
22
+ * @param subLogger - Container's logger to use as parent for our logger
23
+ * @param storageAdapter - Storage adapter for fetching snapshots
24
+ * @param _offlineLoadEnabled - Is serializing/rehydrating containers allowed?
25
+ * @param containerEvent - Source of the "saved" event when the container has all its pending state uploaded
26
+ * @param containerDirty - Is the container "dirty"? That's the opposite of "saved" - there is pending state that may not have been received yet by the service.
27
+ */
28
+ constructor(pendingLocalState, subLogger, storageAdapter, _offlineLoadEnabled, containerEvent, containerDirty) {
12
29
  this.pendingLocalState = pendingLocalState;
13
30
  this.storageAdapter = storageAdapter;
14
31
  this._offlineLoadEnabled = _offlineLoadEnabled;
15
- this.newSnapshotFetched = newSnapshotFetched;
32
+ this.containerDirty = containerDirty;
16
33
  this.processedOps = [];
34
+ this.lastSavedOpSequenceNumber = 0;
17
35
  this.mc = createChildMonitoringContext({
18
36
  logger: subLogger,
19
37
  namespace: "serializedStateManager",
20
38
  });
39
+ if (pendingLocalState && pendingLocalState.savedOps.length > 0) {
40
+ const savedOpsSize = pendingLocalState.savedOps.length;
41
+ this.lastSavedOpSequenceNumber =
42
+ pendingLocalState.savedOps[savedOpsSize - 1].sequenceNumber;
43
+ }
44
+ containerEvent.once("saved", () => this.updateSnapshotAndProcessedOpsMaybe());
21
45
  }
22
46
  get offlineLoadEnabled() {
23
47
  return this._offlineLoadEnabled;
24
48
  }
49
+ /**
50
+ * Promise that will resolve (or reject) once we've tried to download the latest snapshot(s) from storage
51
+ */
52
+ get waitForInitialRefresh() {
53
+ return this.refreshSnapshotP;
54
+ }
55
+ /**
56
+ * Called whenever an incoming op is processed by the Container
57
+ */
25
58
  addProcessedOp(message) {
26
59
  if (this.offlineLoadEnabled) {
27
60
  this.processedOps.push(message);
28
61
  this.updateSnapshotAndProcessedOpsMaybe();
29
62
  }
30
63
  }
64
+ /**
65
+ * This wraps the basic functionality of fetching the snapshot for this container during Container load.
66
+ *
67
+ * If we have pendingLocalState, we get the snapshot from there.
68
+ * Otherwise, fetch it from storage (according to specifiedVersion if provided)
69
+ *
70
+ * @param specifiedVersion - If a version is specified and we don't have pendingLocalState, fetch this version from storage
71
+ * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree.
72
+ * @returns The snapshot to boot the container from
73
+ */
31
74
  async fetchSnapshot(specifiedVersion, supportGetSnapshotApi) {
32
75
  if (this.pendingLocalState === undefined) {
33
- const { baseSnapshot, version } = await getSnapshotTree(this.mc, this.storageAdapter, supportGetSnapshotApi, specifiedVersion);
76
+ const { baseSnapshot, version } = await getSnapshot(this.mc, this.storageAdapter, supportGetSnapshotApi, specifiedVersion);
77
+ const baseSnapshotTree = getSnapshotTree(baseSnapshot);
34
78
  // non-interactive clients will not have any pending state we want to save
35
79
  if (this.offlineLoadEnabled) {
36
- const snapshotBlobs = await getBlobContentsFromTree(baseSnapshot, this.storageAdapter);
37
- const attributes = await getDocumentAttributes(this.storageAdapter, baseSnapshot);
80
+ const snapshotBlobs = await getBlobContentsFromTree(baseSnapshotTree, this.storageAdapter);
81
+ const attributes = await getDocumentAttributes(this.storageAdapter, baseSnapshotTree);
38
82
  this.snapshot = {
39
- baseSnapshot,
83
+ baseSnapshot: baseSnapshotTree,
40
84
  snapshotBlobs,
41
85
  snapshotSequenceNumber: attributes.sequenceNumber,
42
86
  };
@@ -51,19 +95,66 @@ export class SerializedStateManager {
51
95
  snapshotBlobs,
52
96
  snapshotSequenceNumber: attributes.sequenceNumber,
53
97
  };
54
- this.refreshSnapshot ?? (this.refreshSnapshot = (async () => {
55
- this.latestSnapshot = await getLatestSnapshotInfo(this.mc, this.storageAdapter, supportGetSnapshotApi);
56
- this.newSnapshotFetched?.();
57
- this.updateSnapshotAndProcessedOpsMaybe();
58
- })());
59
- return { baseSnapshot, version: undefined };
98
+ if (this.refreshSnapshotP === undefined &&
99
+ this.mc.config.getBoolean("Fluid.Container.enableOfflineSnapshotRefresh") === true) {
100
+ // Don't block on the refresh snapshot call - it is for the next time we serialize, not booting this incarnation
101
+ this.refreshSnapshotP = this.refreshLatestSnapshot(supportGetSnapshotApi);
102
+ this.refreshSnapshotP.catch((e) => {
103
+ this.mc.logger.sendErrorEvent({
104
+ eventName: "RefreshLatestSnapshotFailed",
105
+ error: e,
106
+ });
107
+ });
108
+ }
109
+ const blobContents = new Map();
110
+ for (const [id, value] of Object.entries(snapshotBlobs)) {
111
+ blobContents.set(id, stringToBuffer(value, "utf8"));
112
+ }
113
+ const iSnapshot = {
114
+ sequenceNumber: this.snapshot.snapshotSequenceNumber,
115
+ snapshotTree: baseSnapshot,
116
+ blobContents,
117
+ latestSequenceNumber: undefined,
118
+ ops: [],
119
+ snapshotFormatV: 1,
120
+ };
121
+ return { baseSnapshot: iSnapshot, version: undefined };
60
122
  }
61
123
  }
124
+ /**
125
+ * Fetch the latest snapshot for the container, including delay-loaded groupIds if pendingLocalState was provided and contained any groupIds.
126
+ * Note that this will update the StorageAdapter's cached snapshots for the groupIds (if present)
127
+ *
128
+ * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree (must be true to fetch by groupIds)
129
+ */
130
+ async refreshLatestSnapshot(supportGetSnapshotApi) {
131
+ this.latestSnapshot = await getLatestSnapshotInfo(this.mc, this.storageAdapter, supportGetSnapshotApi);
132
+ // These are loading groupIds that the containerRuntime has requested over its lifetime.
133
+ // We will fetch the latest snapshot for the groupIds, which will update storageAdapter.loadedGroupIdSnapshots's cache
134
+ const downloadedGroupIds = Object.keys(this.storageAdapter.loadedGroupIdSnapshots);
135
+ if (supportGetSnapshotApi && downloadedGroupIds.length > 0) {
136
+ assert(this.storageAdapter.getSnapshot !== undefined, 0x972 /* getSnapshot should exist */);
137
+ // (This is a separate network call from above because it requires work for storage to add a special base groupId)
138
+ const snapshot = await this.storageAdapter.getSnapshot({
139
+ versionId: undefined,
140
+ scenarioName: "getLatestSnapshotInfo",
141
+ cacheSnapshot: false,
142
+ loadingGroupIds: downloadedGroupIds,
143
+ fetchSource: FetchSource.noCache,
144
+ });
145
+ assert(snapshot !== undefined, 0x973 /* Snapshot should exist */);
146
+ }
147
+ this.updateSnapshotAndProcessedOpsMaybe();
148
+ }
62
149
  /**
63
150
  * Updates class snapshot and processedOps if we have a new snapshot and it's among processedOps range.
64
151
  */
65
152
  updateSnapshotAndProcessedOpsMaybe() {
66
- if (this.latestSnapshot === undefined || this.processedOps.length === 0) {
153
+ if (this.latestSnapshot === undefined ||
154
+ this.processedOps.length === 0 ||
155
+ this.processedOps[this.processedOps.length - 1].sequenceNumber <
156
+ this.lastSavedOpSequenceNumber ||
157
+ this.containerDirty()) {
67
158
  // can't refresh latest snapshot until we have processed the ops up to it.
68
159
  // Pending state would be behind the latest snapshot.
69
160
  return;
@@ -100,9 +191,10 @@ export class SerializedStateManager {
100
191
  }
101
192
  }
102
193
  /**
194
+ * When the Container attaches, we need to stash the initial snapshot (a form of the attach summary).
103
195
  * This method is only meant to be used by Container.attach() to set the initial
104
196
  * base snapshot when attaching.
105
- * @param snapshot - snapshot and blobs collected while attaching
197
+ * @param snapshot - snapshot and blobs collected while attaching (a form of the attach summary)
106
198
  */
107
199
  setInitialSnapshot(snapshot) {
108
200
  if (this.offlineLoadEnabled) {
@@ -117,7 +209,11 @@ export class SerializedStateManager {
117
209
  this.snapshot = { ...snapshot, snapshotSequenceNumber: attributes.sequenceNumber };
118
210
  }
119
211
  }
120
- async getPendingLocalStateCore(props, clientId, runtime, resolvedUrl) {
212
+ /**
213
+ * Assembles and serializes the {@link IPendingContainerState} for the container,
214
+ * to be stored and used to rehydrate the container at a later time.
215
+ */
216
+ async getPendingLocalState(props, clientId, runtime, resolvedUrl) {
121
217
  return PerformanceEvent.timedExecAsync(this.mc.logger, {
122
218
  eventName: "getPendingLocalState",
123
219
  notifyImminentClosure: props.notifyImminentClosure,
@@ -128,16 +224,32 @@ export class SerializedStateManager {
128
224
  throw new UsageError("Can't get pending local state unless offline load is enabled");
129
225
  }
130
226
  assert(this.snapshot !== undefined, 0x8e5 /* no base data */);
131
- const pendingRuntimeState = await runtime.getPendingLocalState(props);
227
+ const pendingRuntimeState = await runtime.getPendingLocalState({
228
+ ...props,
229
+ snapshotSequenceNumber: this.snapshot.snapshotSequenceNumber,
230
+ sessionExpiryTimerStarted: this.snapshot.snapshotFetchedTime,
231
+ });
232
+ // This conversion is required because ArrayBufferLike doesn't survive JSON.stringify
233
+ const loadedGroupIdSnapshots = {};
234
+ let hasGroupIdSnapshots = false;
235
+ const groupIdSnapshots = Object.entries(this.storageAdapter.loadedGroupIdSnapshots);
236
+ if (groupIdSnapshots.length > 0) {
237
+ for (const [groupId, snapshot] of groupIdSnapshots) {
238
+ hasGroupIdSnapshots = true;
239
+ loadedGroupIdSnapshots[groupId] = convertSnapshotToSnapshotInfo(snapshot);
240
+ }
241
+ }
132
242
  const pendingState = {
133
243
  attached: true,
134
244
  pendingRuntimeState,
135
245
  baseSnapshot: this.snapshot.baseSnapshot,
136
246
  snapshotBlobs: this.snapshot.snapshotBlobs,
247
+ loadedGroupIdSnapshots: hasGroupIdSnapshots
248
+ ? loadedGroupIdSnapshots
249
+ : undefined,
137
250
  savedOps: this.processedOps,
138
251
  url: resolvedUrl.url,
139
- // no need to save this if there is no pending runtime state
140
- clientId: pendingRuntimeState !== undefined ? clientId : undefined,
252
+ clientId,
141
253
  };
142
254
  return JSON.stringify(pendingState);
143
255
  });
@@ -153,11 +265,18 @@ export class SerializedStateManager {
153
265
  */
154
266
  export async function getLatestSnapshotInfo(mc, storageAdapter, supportGetSnapshotApi) {
155
267
  return PerformanceEvent.timedExecAsync(mc.logger, { eventName: "GetLatestSnapshotInfo" }, async () => {
156
- const { baseSnapshot } = await getSnapshotTree(mc, storageAdapter, supportGetSnapshotApi, undefined);
157
- const snapshotBlobs = await getBlobContentsFromTree(baseSnapshot, storageAdapter);
158
- const attributes = await getDocumentAttributes(storageAdapter, baseSnapshot);
268
+ const { baseSnapshot } = await getSnapshot(mc, storageAdapter, supportGetSnapshotApi, undefined);
269
+ const baseSnapshotTree = getSnapshotTree(baseSnapshot);
270
+ const snapshotFetchedTime = Date.now();
271
+ const snapshotBlobs = await getBlobContentsFromTree(baseSnapshotTree, storageAdapter);
272
+ const attributes = await getDocumentAttributes(storageAdapter, baseSnapshotTree);
159
273
  const snapshotSequenceNumber = attributes.sequenceNumber;
160
- return { baseSnapshot, snapshotBlobs, snapshotSequenceNumber };
274
+ return {
275
+ baseSnapshot: baseSnapshotTree,
276
+ snapshotBlobs,
277
+ snapshotSequenceNumber,
278
+ snapshotFetchedTime,
279
+ };
161
280
  }).catch(() => undefined);
162
281
  }
163
282
  /**
@@ -169,15 +288,12 @@ export async function getLatestSnapshotInfo(mc, storageAdapter, supportGetSnapsh
169
288
  * @param specifiedVersion - An optional version string specifying the version of the snapshot tree to fetch.
170
289
  * @returns - An ISnapshotTree and its version.
171
290
  */
172
- async function getSnapshotTree(mc, storageAdapter, supportGetSnapshotApi, specifiedVersion) {
291
+ async function getSnapshot(mc, storageAdapter, supportGetSnapshotApi, specifiedVersion) {
173
292
  const { snapshot, version } = supportGetSnapshotApi
174
293
  ? await fetchISnapshot(mc, storageAdapter, specifiedVersion)
175
294
  : await fetchISnapshotTree(mc, storageAdapter, specifiedVersion);
176
- const baseSnapshot = isInstanceOfISnapshot(snapshot)
177
- ? snapshot.snapshotTree
178
- : snapshot;
179
- assert(baseSnapshot !== undefined, 0x8e4 /* Snapshot should exist */);
180
- return { baseSnapshot, version };
295
+ assert(snapshot !== undefined, 0x8e4 /* Snapshot should exist */);
296
+ return { baseSnapshot: snapshot, version };
181
297
  }
182
298
  /**
183
299
  * Fetches an ISnapshot from a storage adapter based on the specified version.
@@ -1 +1 @@
1
- {"version":3,"file":"serializedStateManager.js","sourceRoot":"","sources":["../src/serializedStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAM7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAQ9E,OAAO,EAEN,gBAAgB,EAChB,UAAU,EACV,4BAA4B,GAC5B,MAAM,0CAA0C,CAAC;AAElD,OAAO,EAA6B,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAClG,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AA8CnD,MAAM,OAAO,sBAAsB;IAOlC,YACkB,iBAAqD,EACtE,SAA8B,EACb,cAGhB,EACgB,mBAA4B,EA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G,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE5B,IAAI,OAAO,KAAK,SAAS,IAAI,gBAAgB,KAAK,SAAS,EAAE;QAC5D,+EAA+E;QAC/E,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC;YACxB,SAAS,EAAE,6BAA6B;YACxC,EAAE,EAAE,gBAAgB;SACpB,CAAC,CAAC;KACH;IACD,MAAM,QAAQ,GAAG,CAAC,MAAM,cAAc,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,IAAI,SAAS,CAAC;IAE9E,IAAI,QAAQ,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE;QACpD,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,SAAS,EAAE,uBAAuB,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;KACjF;SAAM,IAAI,QAAQ,KAAK,SAAS,IAAI,OAAO,EAAE,EAAE,KAAK,SAAS,EAAE;QAC/D,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC;YACxB,SAAS,EAAE,wCAAwC;YACnD,UAAU,EAAE,OAAO,KAAK,SAAS,EAAE,mFAAmF;SACtH,CAAC,CAAC;KACH;IACD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n\tIGetPendingLocalStateProps,\n\tIRuntime,\n} from \"@fluidframework/container-definitions/internal\";\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport {\n\tIDocumentStorageService,\n\tIResolvedUrl,\n\tISnapshot,\n} from \"@fluidframework/driver-definitions/internal\";\nimport { isInstanceOfISnapshot } from \"@fluidframework/driver-utils/internal\";\nimport {\n\ttype IDocumentAttributes,\n\tISequencedDocumentMessage,\n\tISnapshotTree,\n\tIVersion,\n} from \"@fluidframework/protocol-definitions\";\nimport { ITelemetryLoggerExt } from \"@fluidframework/telemetry-utils\";\nimport {\n\tMonitoringContext,\n\tPerformanceEvent,\n\tUsageError,\n\tcreateChildMonitoringContext,\n} from \"@fluidframework/telemetry-utils/internal\";\n\nimport { ISerializableBlobContents, getBlobContentsFromTree } from \"./containerStorageAdapter.js\";\nimport { getDocumentAttributes } from \"./utils.js\";\n\nexport interface SnapshotWithBlobs {\n\t/**\n\t * Snapshot from which container initially loaded.\n\t */\n\tbaseSnapshot: ISnapshotTree;\n\t/**\n\t * Serializable blobs from the base snapshot. Used to load offline since\n\t * storage is not available.\n\t */\n\tsnapshotBlobs: ISerializableBlobContents;\n}\n/**\n * State saved by a container at close time, to be used to load a new instance\n * of the container to the same state\n * @internal\n */\nexport interface IPendingContainerState extends SnapshotWithBlobs {\n\tattached: true;\n\tpendingRuntimeState: unknown;\n\t/**\n\t * All ops since base snapshot sequence number up to the latest op\n\t * seen when the container was closed. Used to apply stashed (saved pending)\n\t * ops at the same sequence number at which they were made.\n\t */\n\tsavedOps: ISequencedDocumentMessage[];\n\turl: string;\n\tclientId?: string;\n}\n\n/**\n * State saved by a container in detached state, to be used to load a new instance\n * of the container to the same state (rehydrate)\n * @internal\n */\nexport interface IPendingDetachedContainerState extends SnapshotWithBlobs {\n\tattached: false;\n\thasAttachmentBlobs: boolean;\n\tpendingRuntimeState?: unknown;\n}\n\nexport interface ISnapshotInfo extends SnapshotWithBlobs {\n\tsnapshotSequenceNumber: number;\n}\n\nexport class SerializedStateManager {\n\tprivate readonly processedOps: ISequencedDocumentMessage[] = [];\n\tprivate snapshot: ISnapshotInfo | undefined;\n\tprivate readonly mc: MonitoringContext;\n\tprivate latestSnapshot: ISnapshotInfo | undefined;\n\tprivate refreshSnapshot: Promise<void> | undefined;\n\n\tconstructor(\n\t\tprivate readonly pendingLocalState: IPendingContainerState | undefined,\n\t\tsubLogger: ITelemetryLoggerExt,\n\t\tprivate readonly storageAdapter: Pick<\n\t\t\tIDocumentStorageService,\n\t\t\t\"readBlob\" | \"getSnapshotTree\" | \"getSnapshot\" | \"getVersions\"\n\t\t>,\n\t\tprivate readonly _offlineLoadEnabled: boolean,\n\t\tprivate readonly newSnapshotFetched?: () => void,\n\t) {\n\t\tthis.mc = createChildMonitoringContext({\n\t\t\tlogger: subLogger,\n\t\t\tnamespace: \"serializedStateManager\",\n\t\t});\n\t}\n\n\tpublic get offlineLoadEnabled(): boolean {\n\t\treturn this._offlineLoadEnabled;\n\t}\n\n\tpublic addProcessedOp(message: ISequencedDocumentMessage) {\n\t\tif (this.offlineLoadEnabled) {\n\t\t\tthis.processedOps.push(message);\n\t\t\tthis.updateSnapshotAndProcessedOpsMaybe();\n\t\t}\n\t}\n\n\tpublic async fetchSnapshot(\n\t\tspecifiedVersion: string | undefined,\n\t\tsupportGetSnapshotApi: boolean,\n\t) {\n\t\tif (this.pendingLocalState === undefined) {\n\t\t\tconst { baseSnapshot, version } = await getSnapshotTree(\n\t\t\t\tthis.mc,\n\t\t\t\tthis.storageAdapter,\n\t\t\t\tsupportGetSnapshotApi,\n\t\t\t\tspecifiedVersion,\n\t\t\t);\n\t\t\t// non-interactive clients will not have any pending state we want to save\n\t\t\tif (this.offlineLoadEnabled) {\n\t\t\t\tconst snapshotBlobs = await getBlobContentsFromTree(\n\t\t\t\t\tbaseSnapshot,\n\t\t\t\t\tthis.storageAdapter,\n\t\t\t\t);\n\t\t\t\tconst attributes = await getDocumentAttributes(this.storageAdapter, baseSnapshot);\n\t\t\t\tthis.snapshot = {\n\t\t\t\t\tbaseSnapshot,\n\t\t\t\t\tsnapshotBlobs,\n\t\t\t\t\tsnapshotSequenceNumber: attributes.sequenceNumber,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn { baseSnapshot, version };\n\t\t} else {\n\t\t\tconst { baseSnapshot, snapshotBlobs } = this.pendingLocalState;\n\t\t\tconst attributes = await getDocumentAttributes(this.storageAdapter, baseSnapshot);\n\t\t\tthis.snapshot = {\n\t\t\t\tbaseSnapshot,\n\t\t\t\tsnapshotBlobs,\n\t\t\t\tsnapshotSequenceNumber: attributes.sequenceNumber,\n\t\t\t};\n\t\t\tthis.refreshSnapshot ??= (async () => {\n\t\t\t\tthis.latestSnapshot = await getLatestSnapshotInfo(\n\t\t\t\t\tthis.mc,\n\t\t\t\t\tthis.storageAdapter,\n\t\t\t\t\tsupportGetSnapshotApi,\n\t\t\t\t);\n\t\t\t\tthis.newSnapshotFetched?.();\n\t\t\t\tthis.updateSnapshotAndProcessedOpsMaybe();\n\t\t\t})();\n\n\t\t\treturn { baseSnapshot, version: undefined };\n\t\t}\n\t}\n\n\t/**\n\t * Updates class snapshot and processedOps if we have a new snapshot and it's among processedOps range.\n\t */\n\tprivate updateSnapshotAndProcessedOpsMaybe() {\n\t\tif (this.latestSnapshot === undefined || this.processedOps.length === 0) {\n\t\t\t// can't refresh latest snapshot until we have processed the ops up to it.\n\t\t\t// Pending state would be behind the latest snapshot.\n\t\t\treturn;\n\t\t}\n\t\tconst snapshotSequenceNumber = this.latestSnapshot.snapshotSequenceNumber;\n\t\tconst firstProcessedOpSequenceNumber = this.processedOps[0].sequenceNumber;\n\t\tconst lastProcessedOpSequenceNumber =\n\t\t\tthis.processedOps[this.processedOps.length - 1].sequenceNumber;\n\n\t\tif (snapshotSequenceNumber < firstProcessedOpSequenceNumber) {\n\t\t\t// Snapshot seq number is older than our first processed op, which could mean we're fetching\n\t\t\t// the same snapshot that we already have or snapshot is too old, implicating an unexpected behavior.\n\t\t\tthis.mc.logger.sendTelemetryEvent({\n\t\t\t\teventName: \"OldSnapshotFetchWhileRefreshing\",\n\t\t\t\tsnapshotSequenceNumber,\n\t\t\t\tfirstProcessedOpSequenceNumber,\n\t\t\t\tlastProcessedOpSequenceNumber,\n\t\t\t\tstashedSnapshotSequenceNumber: this.snapshot?.snapshotSequenceNumber,\n\t\t\t});\n\t\t\tthis.latestSnapshot = undefined;\n\t\t} else if (snapshotSequenceNumber <= lastProcessedOpSequenceNumber) {\n\t\t\t// Snapshot seq num is between the first and last processed op.\n\t\t\t// Remove the ops that are already part of the snapshot\n\t\t\tthis.processedOps.splice(\n\t\t\t\t0,\n\t\t\t\tsnapshotSequenceNumber - firstProcessedOpSequenceNumber + 1,\n\t\t\t);\n\t\t\tthis.snapshot = this.latestSnapshot;\n\t\t\tthis.latestSnapshot = undefined;\n\t\t\tthis.mc.logger.sendTelemetryEvent({\n\t\t\t\teventName: \"SnapshotRefreshed\",\n\t\t\t\tsnapshotSequenceNumber,\n\t\t\t\tfirstProcessedOpSequenceNumber,\n\t\t\t\tnewFirstProcessedOpSequenceNumber:\n\t\t\t\t\tthis.processedOps.length === 0\n\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t: this.processedOps[0].sequenceNumber,\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * This method is only meant to be used by Container.attach() to set the initial\n\t * base snapshot when attaching.\n\t * @param snapshot - snapshot and blobs collected while attaching\n\t */\n\tpublic setInitialSnapshot(snapshot: SnapshotWithBlobs | undefined) {\n\t\tif (this.offlineLoadEnabled) {\n\t\t\tassert(\n\t\t\t\tthis.snapshot === undefined,\n\t\t\t\t0x937 /* inital snapshot should only be defined once */,\n\t\t\t);\n\t\t\tassert(snapshot !== undefined, 0x938 /* attachment snapshot should be defined */);\n\t\t\tconst { baseSnapshot, snapshotBlobs } = snapshot;\n\t\t\tconst attributesHash =\n\t\t\t\t\".protocol\" in baseSnapshot.trees\n\t\t\t\t\t? baseSnapshot.trees[\".protocol\"].blobs.attributes\n\t\t\t\t\t: baseSnapshot.blobs[\".attributes\"];\n\t\t\tconst attributes = JSON.parse(snapshotBlobs[attributesHash]);\n\t\t\tassert(\n\t\t\t\tattributes.sequenceNumber === 0,\n\t\t\t\t0x939 /* trying to set a non attachment snapshot */,\n\t\t\t);\n\t\t\tthis.snapshot = { ...snapshot, snapshotSequenceNumber: attributes.sequenceNumber };\n\t\t}\n\t}\n\n\tpublic async getPendingLocalStateCore(\n\t\tprops: IGetPendingLocalStateProps,\n\t\tclientId: string | undefined,\n\t\truntime: Pick<IRuntime, \"getPendingLocalState\">,\n\t\tresolvedUrl: IResolvedUrl,\n\t) {\n\t\treturn PerformanceEvent.timedExecAsync(\n\t\t\tthis.mc.logger,\n\t\t\t{\n\t\t\t\teventName: \"getPendingLocalState\",\n\t\t\t\tnotifyImminentClosure: props.notifyImminentClosure,\n\t\t\t\tprocessedOpsSize: this.processedOps.length,\n\t\t\t\tclientId,\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tif (!this.offlineLoadEnabled) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\"Can't get pending local state unless offline load is enabled\",\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tassert(this.snapshot !== undefined, 0x8e5 /* no base data */);\n\t\t\t\tconst pendingRuntimeState = await runtime.getPendingLocalState(props);\n\t\t\t\tconst pendingState: IPendingContainerState = {\n\t\t\t\t\tattached: true,\n\t\t\t\t\tpendingRuntimeState,\n\t\t\t\t\tbaseSnapshot: this.snapshot.baseSnapshot,\n\t\t\t\t\tsnapshotBlobs: this.snapshot.snapshotBlobs,\n\t\t\t\t\tsavedOps: this.processedOps,\n\t\t\t\t\turl: resolvedUrl.url,\n\t\t\t\t\t// no need to save this if there is no pending runtime state\n\t\t\t\t\tclientId: pendingRuntimeState !== undefined ? clientId : undefined,\n\t\t\t\t};\n\n\t\t\t\treturn JSON.stringify(pendingState);\n\t\t\t},\n\t\t);\n\t}\n}\n\n/**\n * Retrieves the most recent snapshot and returns its info.\n *\n * @param mc - The monitoring context.\n * @param storageAdapter - The storage adapter providing methods to retrieve the snapshot.\n * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree.\n * @returns a SnapshotInfo object containing the snapshot tree, snapshot blobs and its sequence number.\n */\nexport async function getLatestSnapshotInfo(\n\tmc: MonitoringContext,\n\tstorageAdapter: Pick<\n\t\tIDocumentStorageService,\n\t\t\"getSnapshot\" | \"getSnapshotTree\" | \"getVersions\" | \"readBlob\"\n\t>,\n\tsupportGetSnapshotApi: boolean,\n): Promise<ISnapshotInfo | undefined> {\n\treturn PerformanceEvent.timedExecAsync(\n\t\tmc.logger,\n\t\t{ eventName: \"GetLatestSnapshotInfo\" },\n\t\tasync () => {\n\t\t\tconst { baseSnapshot } = await getSnapshotTree(\n\t\t\t\tmc,\n\t\t\t\tstorageAdapter,\n\t\t\t\tsupportGetSnapshotApi,\n\t\t\t\tundefined,\n\t\t\t);\n\t\t\tconst snapshotBlobs = await getBlobContentsFromTree(baseSnapshot, storageAdapter);\n\t\t\tconst attributes: IDocumentAttributes = await getDocumentAttributes(\n\t\t\t\tstorageAdapter,\n\t\t\t\tbaseSnapshot,\n\t\t\t);\n\t\t\tconst snapshotSequenceNumber = attributes.sequenceNumber;\n\t\t\treturn { baseSnapshot, snapshotBlobs, snapshotSequenceNumber };\n\t\t},\n\t).catch(() => undefined);\n}\n\n/**\n * Retrieves a snapshot from the storage adapter and transforms it into an ISnapshotTree object.\n *\n * @param mc - The monitoring context.\n * @param storageAdapter - The storage adapter providing methods to retrieve the snapshot.\n * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree.\n * @param specifiedVersion - An optional version string specifying the version of the snapshot tree to fetch.\n * @returns - An ISnapshotTree and its version.\n */\nasync function getSnapshotTree(\n\tmc: MonitoringContext,\n\tstorageAdapter: Pick<\n\t\tIDocumentStorageService,\n\t\t\"getSnapshot\" | \"getSnapshotTree\" | \"getVersions\"\n\t>,\n\tsupportGetSnapshotApi: boolean,\n\tspecifiedVersion: string | undefined,\n): Promise<{ baseSnapshot: ISnapshotTree; version?: IVersion }> {\n\tconst { snapshot, version } = supportGetSnapshotApi\n\t\t? await fetchISnapshot(mc, storageAdapter, specifiedVersion)\n\t\t: await fetchISnapshotTree(mc, storageAdapter, specifiedVersion);\n\tconst baseSnapshot: ISnapshotTree | undefined = isInstanceOfISnapshot(snapshot)\n\t\t? snapshot.snapshotTree\n\t\t: snapshot;\n\tassert(baseSnapshot !== undefined, 0x8e4 /* Snapshot should exist */);\n\treturn { baseSnapshot, version };\n}\n\n/**\n * Fetches an ISnapshot from a storage adapter based on the specified version.\n *\n * @param mc - The monitoring context.\n * @param storageAdapter - The storage adapter providing a getSnapshot method to retrieve the ISnapshot and version.\n * @param specifiedVersion - An optional version string specifying the version of the snapshot tree to fetch.\n * @returns - The fetched snapshot tree and its version.\n */\nexport async function fetchISnapshot(\n\tmc: MonitoringContext,\n\tstorageAdapter: Pick<IDocumentStorageService, \"getSnapshot\">,\n\tspecifiedVersion: string | undefined,\n): Promise<{ snapshot?: ISnapshot; version?: IVersion }> {\n\tconst snapshot = await storageAdapter.getSnapshot?.({ versionId: specifiedVersion });\n\tconst version: IVersion | undefined =\n\t\tsnapshot?.snapshotTree.id === undefined\n\t\t\t? undefined\n\t\t\t: {\n\t\t\t\t\tid: snapshot.snapshotTree.id,\n\t\t\t\t\ttreeId: snapshot.snapshotTree.id,\n\t\t\t };\n\n\tif (snapshot === undefined && specifiedVersion !== undefined) {\n\t\tmc.logger.sendErrorEvent({\n\t\t\teventName: \"getSnapshotTreeFailed\",\n\t\t\tid: specifiedVersion,\n\t\t});\n\t} else if (snapshot !== undefined && version?.id === undefined) {\n\t\tmc.logger.sendErrorEvent({\n\t\t\teventName: \"getSnapshotFetchedTreeWithoutVersionId\",\n\t\t\thasVersion: version !== undefined, // if hasVersion is true, this means that the contract with the service was broken.\n\t\t});\n\t}\n\treturn { snapshot, version };\n}\n\n/**\n * Fetches an ISnapshotTree from a storage adapter based on the specified version.\n *\n * @param mc - The monitoring context.\n * @param storageAdapter - The storage adapter providing methods to retrieve the ISnapshotTree and version.\n * @param specifiedVersion - An optional version string specifying the version of the snapshot tree to fetch.\n * @returns - The fetched snapshot tree and its version.\n */\nexport async function fetchISnapshotTree(\n\tmc: MonitoringContext,\n\tstorageAdapter: Pick<IDocumentStorageService, \"getSnapshotTree\" | \"getVersions\">,\n\tspecifiedVersion: string | undefined,\n): Promise<{ snapshot?: ISnapshotTree; version?: IVersion | undefined }> {\n\tconst versions = await storageAdapter.getVersions(specifiedVersion ?? null, 1);\n\tconst version = versions[0];\n\n\tif (version === undefined && specifiedVersion !== undefined) {\n\t\t// We should have a defined version to load from if specified version requested\n\t\tmc.logger.sendErrorEvent({\n\t\t\teventName: \"NoVersionFoundWhenSpecified\",\n\t\t\tid: specifiedVersion,\n\t\t});\n\t}\n\tconst snapshot = (await storageAdapter.getSnapshotTree(version)) ?? undefined;\n\n\tif (snapshot === undefined && version !== undefined) {\n\t\tmc.logger.sendErrorEvent({ eventName: \"getSnapshotTreeFailed\", id: version.id });\n\t} else if (snapshot !== undefined && version?.id === undefined) {\n\t\tmc.logger.sendErrorEvent({\n\t\t\teventName: \"getSnapshotFetchedTreeWithoutVersionId\",\n\t\t\thasVersion: version !== undefined, // if hasVersion is true, this means that the contract with the service was broken.\n\t\t});\n\t}\n\treturn { snapshot, version };\n}\n"]}
1
+ {"version":3,"file":"serializedStateManager.js","sourceRoot":"","sources":["../src/serializedStateManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EACN,WAAW,GAIX,MAAM,6CAA6C,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAC;AAOxE,OAAO,EAEN,gBAAgB,EAChB,UAAU,EACV,4BAA4B,GAC5B,MAAM,0CAA0C,CAAC;AAElD,OAAO,EAA6B,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAClG,OAAO,EAAE,6BAA6B,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAsFlF;;;;;;GAMG;AACH,MAAM,OAAO,sBAAsB;IAQlC;;;;;;;OAOG;IACH,YACkB,iBAAqD,EACtE,SAA+B,EACd,cAA6D,EAC7D,mBAA4B,EAC7C,cAAgD,EAC/B,cAA6B;QAL7B,sBAAiB,GAAjB,iBAAiB,CAAo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AAO,EAAE,CAAC;AAC9B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACvC,EAAqB,EACrB,cAAgF,EAChF,gBAAoC;IAEpC,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,WAAW,CAAC,gBAAgB,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC;IAC/E,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE5B,IAAI,OAAO,KAAK,SAAS,IAAI,gBAAgB,KAAK,SAAS,EAAE;QAC5D,+EAA+E;QAC/E,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC;YACxB,SAAS,EAAE,6BAA6B;YACxC,EAAE,EAAE,gBAAgB;SACpB,CAAC,CAAC;KACH;IACD,MAAM,QAAQ,GAAG,CAAC,MAAM,cAAc,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,IAAI,SAAS,CAAC;IAE9E,IAAI,QAAQ,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE;QACpD,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,SAAS,EAAE,uBAAuB,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;KACjF;SAAM,IAAI,QAAQ,KAAK,SAAS,IAAI,OAAO,EAAE,EAAE,KAAK,SAAS,EAAE;QAC/D,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC;YACxB,SAAS,EAAE,wCAAwC;YACnD,UAAU,EAAE,OAAO,KAAK,SAAS,EAAE,mFAAmF;SACtH,CAAC,CAAC;KACH;IACD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport {\n\tIGetPendingLocalStateProps,\n\tIRuntime,\n} from \"@fluidframework/container-definitions/internal\";\nimport { stringToBuffer } from \"@fluid-internal/client-utils\";\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport {\n\tFetchSource,\n\tIDocumentStorageService,\n\tIResolvedUrl,\n\tISnapshot,\n} from \"@fluidframework/driver-definitions/internal\";\nimport { getSnapshotTree } from \"@fluidframework/driver-utils/internal\";\nimport {\n\ttype IDocumentAttributes,\n\tISequencedDocumentMessage,\n\tISnapshotTree,\n\tIVersion,\n} from \"@fluidframework/protocol-definitions\";\nimport {\n\tMonitoringContext,\n\tPerformanceEvent,\n\tUsageError,\n\tcreateChildMonitoringContext,\n} from \"@fluidframework/telemetry-utils/internal\";\nimport type { ITelemetryBaseLogger, IEventProvider, IEvent } from \"@fluidframework/core-interfaces\";\nimport { ISerializableBlobContents, getBlobContentsFromTree } from \"./containerStorageAdapter.js\";\nimport { convertSnapshotToSnapshotInfo, getDocumentAttributes } from \"./utils.js\";\n\n/**\n * This is very similar to {@link @fluidframework/protocol-definitions/internal#ISnapshot}, but the difference is\n * that the blobs of ISnapshot are of type ArrayBufferLike, while the blobs of this interface are serializable because\n * they are already converted to string.\n */\nexport interface SnapshotWithBlobs {\n\t/**\n\t * Snapshot from which container initially loaded.\n\t */\n\tbaseSnapshot: ISnapshotTree;\n\t/**\n\t * Serializable blobs from the base snapshot. Used to load offline since\n\t * storage is not available.\n\t */\n\tsnapshotBlobs: ISerializableBlobContents;\n}\n\n/**\n * State saved by a container at close time, to be used to load a new instance\n * of the container to the same state\n *\n * This is very similar to {@link @fluidframework/protocol-definitions/internal#ISnapshot}, but the difference is\n * that the blobs of ISnapshot are of type ArrayBufferLike, while the blobs of this interface are serializable because\n * they are already converted to string.\n *\n * @internal\n */\nexport interface IPendingContainerState extends SnapshotWithBlobs {\n\t/** This container was attached (as opposed to IPendingDetachedContainerState.attached which is false) */\n\tattached: true;\n\t/**\n\t * Runtime-specific state that will be needed to properly rehydrate\n\t * (it's included in ContainerContext passed to instantiateRuntime)\n\t */\n\tpendingRuntimeState: unknown;\n\t/**\n\t * Any group snapshots (aka delay-loaded) we've downloaded from the service for this container\n\t */\n\tloadedGroupIdSnapshots?: Record<string, ISnapshotInfo>;\n\t/**\n\t * All ops since base snapshot sequence number up to the latest op\n\t * seen when the container was closed. Used to apply stashed (saved pending)\n\t * ops at the same sequence number at which they were made.\n\t */\n\tsavedOps: ISequencedDocumentMessage[];\n\t/** The Container's URL in the service, needed to hook up the driver during rehydration */\n\turl: string;\n\t/** If the Container was connected when serialized, its clientId. Used as the initial clientId upon rehydration, until reconnected. */\n\tclientId?: string;\n}\n\n/**\n * State saved by a container in detached state, to be used to load a new instance\n * of the container to the same state (rehydrate)\n * @internal\n */\nexport interface IPendingDetachedContainerState extends SnapshotWithBlobs {\n\t/** This container was not attached (as opposed to IPendingContainerState.attached which is true) */\n\tattached: false;\n\t/** Indicates whether we expect the rehydrated container to have non-empty Detached Blob Storage */\n\thasAttachmentBlobs: boolean;\n\t/**\n\t * Runtime-specific state that will be needed to properly rehydrate\n\t * (it's included in ContainerContext passed to instantiateRuntime)\n\t */\n\tpendingRuntimeState?: unknown;\n}\n\nexport interface ISnapshotInfo extends SnapshotWithBlobs {\n\tsnapshotSequenceNumber: number;\n\tsnapshotFetchedTime?: number | undefined;\n}\n\nexport type ISerializedStateManagerDocumentStorageService = Pick<\n\tIDocumentStorageService,\n\t\"getSnapshot\" | \"getSnapshotTree\" | \"getVersions\" | \"readBlob\"\n> & {\n\tloadedGroupIdSnapshots: Record<string, ISnapshot>;\n};\n\ninterface ISerializerEvent extends IEvent {\n\t(event: \"saved\", listener: (dirty: boolean) => void): void;\n}\n\n/**\n * Helper class to manage the state of the container needed for proper serialization.\n *\n * It holds the pendingLocalState the container was rehydrated from (if any),\n * as well as the snapshot to be used for serialization.\n * It also keeps track of container dirty state and which local ops have been processed\n */\nexport class SerializedStateManager {\n\tprivate readonly processedOps: ISequencedDocumentMessage[] = [];\n\tprivate readonly mc: MonitoringContext;\n\tprivate snapshot: ISnapshotInfo | undefined;\n\tprivate latestSnapshot: ISnapshotInfo | undefined;\n\tprivate refreshSnapshotP: Promise<void> | undefined;\n\tprivate readonly lastSavedOpSequenceNumber: number = 0;\n\n\t/**\n\t * @param pendingLocalState - The pendingLocalState being rehydrated, if any (undefined when loading directly from storage)\n\t * @param subLogger - Container's logger to use as parent for our logger\n\t * @param storageAdapter - Storage adapter for fetching snapshots\n\t * @param _offlineLoadEnabled - Is serializing/rehydrating containers allowed?\n\t * @param containerEvent - Source of the \"saved\" event when the container has all its pending state uploaded\n\t * @param containerDirty - Is the container \"dirty\"? That's the opposite of \"saved\" - there is pending state that may not have been received yet by the service.\n\t */\n\tconstructor(\n\t\tprivate readonly pendingLocalState: IPendingContainerState | undefined,\n\t\tsubLogger: ITelemetryBaseLogger,\n\t\tprivate readonly storageAdapter: ISerializedStateManagerDocumentStorageService,\n\t\tprivate readonly _offlineLoadEnabled: boolean,\n\t\tcontainerEvent: IEventProvider<ISerializerEvent>,\n\t\tprivate readonly containerDirty: () => boolean,\n\t) {\n\t\tthis.mc = createChildMonitoringContext({\n\t\t\tlogger: subLogger,\n\t\t\tnamespace: \"serializedStateManager\",\n\t\t});\n\n\t\tif (pendingLocalState && pendingLocalState.savedOps.length > 0) {\n\t\t\tconst savedOpsSize = pendingLocalState.savedOps.length;\n\t\t\tthis.lastSavedOpSequenceNumber =\n\t\t\t\tpendingLocalState.savedOps[savedOpsSize - 1].sequenceNumber;\n\t\t}\n\t\tcontainerEvent.once(\"saved\", () => this.updateSnapshotAndProcessedOpsMaybe());\n\t}\n\n\tpublic get offlineLoadEnabled(): boolean {\n\t\treturn this._offlineLoadEnabled;\n\t}\n\n\t/**\n\t * Promise that will resolve (or reject) once we've tried to download the latest snapshot(s) from storage\n\t */\n\tpublic get waitForInitialRefresh(): Promise<void> | undefined {\n\t\treturn this.refreshSnapshotP;\n\t}\n\n\t/**\n\t * Called whenever an incoming op is processed by the Container\n\t */\n\tpublic addProcessedOp(message: ISequencedDocumentMessage) {\n\t\tif (this.offlineLoadEnabled) {\n\t\t\tthis.processedOps.push(message);\n\t\t\tthis.updateSnapshotAndProcessedOpsMaybe();\n\t\t}\n\t}\n\n\t/**\n\t * This wraps the basic functionality of fetching the snapshot for this container during Container load.\n\t *\n\t * If we have pendingLocalState, we get the snapshot from there.\n\t * Otherwise, fetch it from storage (according to specifiedVersion if provided)\n\t *\n\t * @param specifiedVersion - If a version is specified and we don't have pendingLocalState, fetch this version from storage\n\t * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree.\n\t * @returns The snapshot to boot the container from\n\t */\n\tpublic async fetchSnapshot(\n\t\tspecifiedVersion: string | undefined,\n\t\tsupportGetSnapshotApi: boolean,\n\t) {\n\t\tif (this.pendingLocalState === undefined) {\n\t\t\tconst { baseSnapshot, version } = await getSnapshot(\n\t\t\t\tthis.mc,\n\t\t\t\tthis.storageAdapter,\n\t\t\t\tsupportGetSnapshotApi,\n\t\t\t\tspecifiedVersion,\n\t\t\t);\n\t\t\tconst baseSnapshotTree: ISnapshotTree | undefined = getSnapshotTree(baseSnapshot);\n\t\t\t// non-interactive clients will not have any pending state we want to save\n\t\t\tif (this.offlineLoadEnabled) {\n\t\t\t\tconst snapshotBlobs = await getBlobContentsFromTree(\n\t\t\t\t\tbaseSnapshotTree,\n\t\t\t\t\tthis.storageAdapter,\n\t\t\t\t);\n\t\t\t\tconst attributes = await getDocumentAttributes(\n\t\t\t\t\tthis.storageAdapter,\n\t\t\t\t\tbaseSnapshotTree,\n\t\t\t\t);\n\t\t\t\tthis.snapshot = {\n\t\t\t\t\tbaseSnapshot: baseSnapshotTree,\n\t\t\t\t\tsnapshotBlobs,\n\t\t\t\t\tsnapshotSequenceNumber: attributes.sequenceNumber,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn { baseSnapshot, version };\n\t\t} else {\n\t\t\tconst { baseSnapshot, snapshotBlobs } = this.pendingLocalState;\n\t\t\tconst attributes = await getDocumentAttributes(this.storageAdapter, baseSnapshot);\n\t\t\tthis.snapshot = {\n\t\t\t\tbaseSnapshot,\n\t\t\t\tsnapshotBlobs,\n\t\t\t\tsnapshotSequenceNumber: attributes.sequenceNumber,\n\t\t\t};\n\n\t\t\tif (\n\t\t\t\tthis.refreshSnapshotP === undefined &&\n\t\t\t\tthis.mc.config.getBoolean(\"Fluid.Container.enableOfflineSnapshotRefresh\") === true\n\t\t\t) {\n\t\t\t\t// Don't block on the refresh snapshot call - it is for the next time we serialize, not booting this incarnation\n\t\t\t\tthis.refreshSnapshotP = this.refreshLatestSnapshot(supportGetSnapshotApi);\n\t\t\t\tthis.refreshSnapshotP.catch((e) => {\n\t\t\t\t\tthis.mc.logger.sendErrorEvent({\n\t\t\t\t\t\teventName: \"RefreshLatestSnapshotFailed\",\n\t\t\t\t\t\terror: e,\n\t\t\t\t\t});\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst blobContents = new Map<string, ArrayBuffer>();\n\t\t\tfor (const [id, value] of Object.entries(snapshotBlobs)) {\n\t\t\t\tblobContents.set(id, stringToBuffer(value, \"utf8\"));\n\t\t\t}\n\t\t\tconst iSnapshot: ISnapshot = {\n\t\t\t\tsequenceNumber: this.snapshot.snapshotSequenceNumber,\n\t\t\t\tsnapshotTree: baseSnapshot,\n\t\t\t\tblobContents,\n\t\t\t\tlatestSequenceNumber: undefined,\n\t\t\t\tops: [],\n\t\t\t\tsnapshotFormatV: 1,\n\t\t\t};\n\t\t\treturn { baseSnapshot: iSnapshot, version: undefined };\n\t\t}\n\t}\n\n\t/**\n\t * Fetch the latest snapshot for the container, including delay-loaded groupIds if pendingLocalState was provided and contained any groupIds.\n\t * Note that this will update the StorageAdapter's cached snapshots for the groupIds (if present)\n\t *\n\t * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree (must be true to fetch by groupIds)\n\t */\n\tprivate async refreshLatestSnapshot(supportGetSnapshotApi: boolean): Promise<void> {\n\t\tthis.latestSnapshot = await getLatestSnapshotInfo(\n\t\t\tthis.mc,\n\t\t\tthis.storageAdapter,\n\t\t\tsupportGetSnapshotApi,\n\t\t);\n\n\t\t// These are loading groupIds that the containerRuntime has requested over its lifetime.\n\t\t// We will fetch the latest snapshot for the groupIds, which will update storageAdapter.loadedGroupIdSnapshots's cache\n\t\tconst downloadedGroupIds = Object.keys(this.storageAdapter.loadedGroupIdSnapshots);\n\t\tif (supportGetSnapshotApi && downloadedGroupIds.length > 0) {\n\t\t\tassert(\n\t\t\t\tthis.storageAdapter.getSnapshot !== undefined,\n\t\t\t\t0x972 /* getSnapshot should exist */,\n\t\t\t);\n\t\t\t// (This is a separate network call from above because it requires work for storage to add a special base groupId)\n\t\t\tconst snapshot = await this.storageAdapter.getSnapshot({\n\t\t\t\tversionId: undefined,\n\t\t\t\tscenarioName: \"getLatestSnapshotInfo\",\n\t\t\t\tcacheSnapshot: false,\n\t\t\t\tloadingGroupIds: downloadedGroupIds,\n\t\t\t\tfetchSource: FetchSource.noCache,\n\t\t\t});\n\t\t\tassert(snapshot !== undefined, 0x973 /* Snapshot should exist */);\n\t\t}\n\n\t\tthis.updateSnapshotAndProcessedOpsMaybe();\n\t}\n\n\t/**\n\t * Updates class snapshot and processedOps if we have a new snapshot and it's among processedOps range.\n\t */\n\tprivate updateSnapshotAndProcessedOpsMaybe() {\n\t\tif (\n\t\t\tthis.latestSnapshot === undefined ||\n\t\t\tthis.processedOps.length === 0 ||\n\t\t\tthis.processedOps[this.processedOps.length - 1].sequenceNumber <\n\t\t\t\tthis.lastSavedOpSequenceNumber ||\n\t\t\tthis.containerDirty()\n\t\t) {\n\t\t\t// can't refresh latest snapshot until we have processed the ops up to it.\n\t\t\t// Pending state would be behind the latest snapshot.\n\t\t\treturn;\n\t\t}\n\t\tconst snapshotSequenceNumber = this.latestSnapshot.snapshotSequenceNumber;\n\t\tconst firstProcessedOpSequenceNumber = this.processedOps[0].sequenceNumber;\n\t\tconst lastProcessedOpSequenceNumber =\n\t\t\tthis.processedOps[this.processedOps.length - 1].sequenceNumber;\n\n\t\tif (snapshotSequenceNumber < firstProcessedOpSequenceNumber) {\n\t\t\t// Snapshot seq number is older than our first processed op, which could mean we're fetching\n\t\t\t// the same snapshot that we already have or snapshot is too old, implicating an unexpected behavior.\n\t\t\tthis.mc.logger.sendTelemetryEvent({\n\t\t\t\teventName: \"OldSnapshotFetchWhileRefreshing\",\n\t\t\t\tsnapshotSequenceNumber,\n\t\t\t\tfirstProcessedOpSequenceNumber,\n\t\t\t\tlastProcessedOpSequenceNumber,\n\t\t\t\tstashedSnapshotSequenceNumber: this.snapshot?.snapshotSequenceNumber,\n\t\t\t});\n\t\t\tthis.latestSnapshot = undefined;\n\t\t} else if (snapshotSequenceNumber <= lastProcessedOpSequenceNumber) {\n\t\t\t// Snapshot seq num is between the first and last processed op.\n\t\t\t// Remove the ops that are already part of the snapshot\n\t\t\tthis.processedOps.splice(\n\t\t\t\t0,\n\t\t\t\tsnapshotSequenceNumber - firstProcessedOpSequenceNumber + 1,\n\t\t\t);\n\t\t\tthis.snapshot = this.latestSnapshot;\n\t\t\tthis.latestSnapshot = undefined;\n\t\t\tthis.mc.logger.sendTelemetryEvent({\n\t\t\t\teventName: \"SnapshotRefreshed\",\n\t\t\t\tsnapshotSequenceNumber,\n\t\t\t\tfirstProcessedOpSequenceNumber,\n\t\t\t\tnewFirstProcessedOpSequenceNumber:\n\t\t\t\t\tthis.processedOps.length === 0\n\t\t\t\t\t\t? undefined\n\t\t\t\t\t\t: this.processedOps[0].sequenceNumber,\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * When the Container attaches, we need to stash the initial snapshot (a form of the attach summary).\n\t * This method is only meant to be used by Container.attach() to set the initial\n\t * base snapshot when attaching.\n\t * @param snapshot - snapshot and blobs collected while attaching (a form of the attach summary)\n\t */\n\tpublic setInitialSnapshot(snapshot: SnapshotWithBlobs | undefined) {\n\t\tif (this.offlineLoadEnabled) {\n\t\t\tassert(\n\t\t\t\tthis.snapshot === undefined,\n\t\t\t\t0x937 /* inital snapshot should only be defined once */,\n\t\t\t);\n\t\t\tassert(snapshot !== undefined, 0x938 /* attachment snapshot should be defined */);\n\t\t\tconst { baseSnapshot, snapshotBlobs } = snapshot;\n\t\t\tconst attributesHash =\n\t\t\t\t\".protocol\" in baseSnapshot.trees\n\t\t\t\t\t? baseSnapshot.trees[\".protocol\"].blobs.attributes\n\t\t\t\t\t: baseSnapshot.blobs[\".attributes\"];\n\t\t\tconst attributes = JSON.parse(snapshotBlobs[attributesHash]);\n\t\t\tassert(\n\t\t\t\tattributes.sequenceNumber === 0,\n\t\t\t\t0x939 /* trying to set a non attachment snapshot */,\n\t\t\t);\n\t\t\tthis.snapshot = { ...snapshot, snapshotSequenceNumber: attributes.sequenceNumber };\n\t\t}\n\t}\n\n\t/**\n\t * Assembles and serializes the {@link IPendingContainerState} for the container,\n\t * to be stored and used to rehydrate the container at a later time.\n\t */\n\tpublic async getPendingLocalState(\n\t\tprops: IGetPendingLocalStateProps,\n\t\tclientId: string | undefined,\n\t\truntime: Pick<IRuntime, \"getPendingLocalState\">,\n\t\tresolvedUrl: IResolvedUrl,\n\t): Promise<string> {\n\t\treturn PerformanceEvent.timedExecAsync(\n\t\t\tthis.mc.logger,\n\t\t\t{\n\t\t\t\teventName: \"getPendingLocalState\",\n\t\t\t\tnotifyImminentClosure: props.notifyImminentClosure,\n\t\t\t\tprocessedOpsSize: this.processedOps.length,\n\t\t\t\tclientId,\n\t\t\t},\n\t\t\tasync () => {\n\t\t\t\tif (!this.offlineLoadEnabled) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\"Can't get pending local state unless offline load is enabled\",\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tassert(this.snapshot !== undefined, 0x8e5 /* no base data */);\n\t\t\t\tconst pendingRuntimeState = await runtime.getPendingLocalState({\n\t\t\t\t\t...props,\n\t\t\t\t\tsnapshotSequenceNumber: this.snapshot.snapshotSequenceNumber,\n\t\t\t\t\tsessionExpiryTimerStarted: this.snapshot.snapshotFetchedTime,\n\t\t\t\t});\n\t\t\t\t// This conversion is required because ArrayBufferLike doesn't survive JSON.stringify\n\t\t\t\tconst loadedGroupIdSnapshots = {};\n\t\t\t\tlet hasGroupIdSnapshots = false;\n\t\t\t\tconst groupIdSnapshots = Object.entries(this.storageAdapter.loadedGroupIdSnapshots);\n\t\t\t\tif (groupIdSnapshots.length > 0) {\n\t\t\t\t\tfor (const [groupId, snapshot] of groupIdSnapshots) {\n\t\t\t\t\t\thasGroupIdSnapshots = true;\n\t\t\t\t\t\tloadedGroupIdSnapshots[groupId] = convertSnapshotToSnapshotInfo(snapshot);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst pendingState: IPendingContainerState = {\n\t\t\t\t\tattached: true,\n\t\t\t\t\tpendingRuntimeState,\n\t\t\t\t\tbaseSnapshot: this.snapshot.baseSnapshot,\n\t\t\t\t\tsnapshotBlobs: this.snapshot.snapshotBlobs,\n\t\t\t\t\tloadedGroupIdSnapshots: hasGroupIdSnapshots\n\t\t\t\t\t\t? loadedGroupIdSnapshots\n\t\t\t\t\t\t: undefined,\n\t\t\t\t\tsavedOps: this.processedOps,\n\t\t\t\t\turl: resolvedUrl.url,\n\t\t\t\t\tclientId,\n\t\t\t\t};\n\n\t\t\t\treturn JSON.stringify(pendingState);\n\t\t\t},\n\t\t);\n\t}\n}\n\n/**\n * Retrieves the most recent snapshot and returns its info.\n *\n * @param mc - The monitoring context.\n * @param storageAdapter - The storage adapter providing methods to retrieve the snapshot.\n * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree.\n * @returns a SnapshotInfo object containing the snapshot tree, snapshot blobs and its sequence number.\n */\nexport async function getLatestSnapshotInfo(\n\tmc: MonitoringContext,\n\tstorageAdapter: ISerializedStateManagerDocumentStorageService,\n\tsupportGetSnapshotApi: boolean,\n): Promise<ISnapshotInfo | undefined> {\n\treturn PerformanceEvent.timedExecAsync(\n\t\tmc.logger,\n\t\t{ eventName: \"GetLatestSnapshotInfo\" },\n\t\tasync () => {\n\t\t\tconst { baseSnapshot } = await getSnapshot(\n\t\t\t\tmc,\n\t\t\t\tstorageAdapter,\n\t\t\t\tsupportGetSnapshotApi,\n\t\t\t\tundefined,\n\t\t\t);\n\n\t\t\tconst baseSnapshotTree: ISnapshotTree | undefined = getSnapshotTree(baseSnapshot);\n\t\t\tconst snapshotFetchedTime = Date.now();\n\t\t\tconst snapshotBlobs = await getBlobContentsFromTree(baseSnapshotTree, storageAdapter);\n\t\t\tconst attributes: IDocumentAttributes = await getDocumentAttributes(\n\t\t\t\tstorageAdapter,\n\t\t\t\tbaseSnapshotTree,\n\t\t\t);\n\t\t\tconst snapshotSequenceNumber = attributes.sequenceNumber;\n\t\t\treturn {\n\t\t\t\tbaseSnapshot: baseSnapshotTree,\n\t\t\t\tsnapshotBlobs,\n\t\t\t\tsnapshotSequenceNumber,\n\t\t\t\tsnapshotFetchedTime,\n\t\t\t};\n\t\t},\n\t).catch(() => undefined);\n}\n\n/**\n * Retrieves a snapshot from the storage adapter and transforms it into an ISnapshotTree object.\n *\n * @param mc - The monitoring context.\n * @param storageAdapter - The storage adapter providing methods to retrieve the snapshot.\n * @param supportGetSnapshotApi - a boolean indicating whether to use the fetchISnapshot or fetchISnapshotTree.\n * @param specifiedVersion - An optional version string specifying the version of the snapshot tree to fetch.\n * @returns - An ISnapshotTree and its version.\n */\nasync function getSnapshot(\n\tmc: MonitoringContext,\n\tstorageAdapter: Pick<\n\t\tIDocumentStorageService,\n\t\t\"getSnapshot\" | \"getSnapshotTree\" | \"getVersions\"\n\t>,\n\tsupportGetSnapshotApi: boolean,\n\tspecifiedVersion: string | undefined,\n): Promise<{ baseSnapshot: ISnapshot | ISnapshotTree; version?: IVersion }> {\n\tconst { snapshot, version } = supportGetSnapshotApi\n\t\t? await fetchISnapshot(mc, storageAdapter, specifiedVersion)\n\t\t: await fetchISnapshotTree(mc, storageAdapter, specifiedVersion);\n\tassert(snapshot !== undefined, 0x8e4 /* Snapshot should exist */);\n\treturn { baseSnapshot: snapshot, version };\n}\n\n/**\n * Fetches an ISnapshot from a storage adapter based on the specified version.\n *\n * @param mc - The monitoring context.\n * @param storageAdapter - The storage adapter providing a getSnapshot method to retrieve the ISnapshot and version.\n * @param specifiedVersion - An optional version string specifying the version of the snapshot tree to fetch.\n * @returns - The fetched snapshot tree and its version.\n */\nexport async function fetchISnapshot(\n\tmc: MonitoringContext,\n\tstorageAdapter: Pick<IDocumentStorageService, \"getSnapshot\">,\n\tspecifiedVersion: string | undefined,\n): Promise<{ snapshot?: ISnapshot; version?: IVersion }> {\n\tconst snapshot = await storageAdapter.getSnapshot?.({ versionId: specifiedVersion });\n\tconst version: IVersion | undefined =\n\t\tsnapshot?.snapshotTree.id === undefined\n\t\t\t? undefined\n\t\t\t: {\n\t\t\t\t\tid: snapshot.snapshotTree.id,\n\t\t\t\t\ttreeId: snapshot.snapshotTree.id,\n\t\t\t };\n\n\tif (snapshot === undefined && specifiedVersion !== undefined) {\n\t\tmc.logger.sendErrorEvent({\n\t\t\teventName: \"getSnapshotTreeFailed\",\n\t\t\tid: specifiedVersion,\n\t\t});\n\t} else if (snapshot !== undefined && version?.id === undefined) {\n\t\tmc.logger.sendErrorEvent({\n\t\t\teventName: \"getSnapshotFetchedTreeWithoutVersionId\",\n\t\t\thasVersion: version !== undefined, // if hasVersion is true, this means that the contract with the service was broken.\n\t\t});\n\t}\n\treturn { snapshot, version };\n}\n\n/**\n * Fetches an ISnapshotTree from a storage adapter based on the specified version.\n *\n * @param mc - The monitoring context.\n * @param storageAdapter - The storage adapter providing methods to retrieve the ISnapshotTree and version.\n * @param specifiedVersion - An optional version string specifying the version of the snapshot tree to fetch.\n * @returns - The fetched snapshot tree and its version.\n */\nexport async function fetchISnapshotTree(\n\tmc: MonitoringContext,\n\tstorageAdapter: Pick<IDocumentStorageService, \"getSnapshotTree\" | \"getVersions\">,\n\tspecifiedVersion: string | undefined,\n): Promise<{ snapshot?: ISnapshotTree; version?: IVersion | undefined }> {\n\tconst versions = await storageAdapter.getVersions(specifiedVersion ?? null, 1);\n\tconst version = versions[0];\n\n\tif (version === undefined && specifiedVersion !== undefined) {\n\t\t// We should have a defined version to load from if specified version requested\n\t\tmc.logger.sendErrorEvent({\n\t\t\teventName: \"NoVersionFoundWhenSpecified\",\n\t\t\tid: specifiedVersion,\n\t\t});\n\t}\n\tconst snapshot = (await storageAdapter.getSnapshotTree(version)) ?? undefined;\n\n\tif (snapshot === undefined && version !== undefined) {\n\t\tmc.logger.sendErrorEvent({ eventName: \"getSnapshotTreeFailed\", id: version.id });\n\t} else if (snapshot !== undefined && version?.id === undefined) {\n\t\tmc.logger.sendErrorEvent({\n\t\t\teventName: \"getSnapshotFetchedTreeWithoutVersionId\",\n\t\t\thasVersion: version !== undefined, // if hasVersion is true, this means that the contract with the service was broken.\n\t\t});\n\t}\n\treturn { snapshot, version };\n}\n"]}
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.42.3"
8
+ "packageVersion": "7.43.1"
9
9
  }
10
10
  ]
11
11
  }
package/lib/utils.d.ts CHANGED
@@ -6,7 +6,7 @@ import { IDocumentStorageService, type ISnapshot } from "@fluidframework/driver-
6
6
  import { CombinedAppAndProtocolSummary, DeltaStreamConnectionForbiddenError } from "@fluidframework/driver-utils/internal";
7
7
  import { IDocumentAttributes, ISnapshotTree, ISummaryTree } from "@fluidframework/protocol-definitions";
8
8
  import { ISerializableBlobContents } from "./containerStorageAdapter.js";
9
- import type { IPendingDetachedContainerState, ISnapshotInfo, SnapshotWithBlobs } from "./serializedStateManager.js";
9
+ import type { IPendingContainerState, IPendingDetachedContainerState, ISnapshotInfo, SnapshotWithBlobs } from "./serializedStateManager.js";
10
10
  export interface ISnapshotTreeWithBlobContents extends ISnapshotTree {
11
11
  blobsContents: {
12
12
  [path: string]: ArrayBufferLike;
@@ -77,7 +77,17 @@ export declare const getSnapshotTreeAndBlobsFromSerializedContainer: (detachedCo
77
77
  export declare function getProtocolSnapshotTree(snapshot: ISnapshotTree): ISnapshotTree;
78
78
  export declare const combineSnapshotTreeAndSnapshotBlobs: (baseSnapshot: ISnapshotTree, snapshotBlobs: ISerializableBlobContents) => ISnapshotTreeWithBlobContents;
79
79
  export declare function isDeltaStreamConnectionForbiddenError(error: any): error is DeltaStreamConnectionForbiddenError;
80
+ /**
81
+ * Parses the given string into {@link IPendingDetachedContainerState} format,
82
+ * with validation (if invalid, throws a UsageError).
83
+ * This is the inverse of the JSON.stringify call in {@link Container.serialize}
84
+ */
80
85
  export declare function getDetachedContainerStateFromSerializedContainer(serializedContainer: string): IPendingDetachedContainerState;
86
+ /**
87
+ * Blindly parses the given string into {@link IPendingContainerState} format.
88
+ * This is the inverse of the JSON.stringify call in {@link SerializedStateManager.getPendingLocalState}
89
+ */
90
+ export declare function getAttachedContainerStateFromSerializedContainer(serializedContainer: string | undefined): IPendingContainerState | undefined;
81
91
  /**
82
92
  * Ensures only a single instance of the provided async function is running.
83
93
  * If there are multiple calls they will all get the same promise to wait on.