@fluidframework/container-runtime 1.2.6 → 2.0.0-dev.1.3.0.96595

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 (221) hide show
  1. package/.mocharc.js +12 -0
  2. package/dist/batchManager.d.ts +37 -0
  3. package/dist/batchManager.d.ts.map +1 -0
  4. package/dist/batchManager.js +73 -0
  5. package/dist/batchManager.js.map +1 -0
  6. package/dist/batchTracker.d.ts +1 -2
  7. package/dist/batchTracker.d.ts.map +1 -1
  8. package/dist/batchTracker.js +2 -3
  9. package/dist/batchTracker.js.map +1 -1
  10. package/dist/blobManager.d.ts +87 -25
  11. package/dist/blobManager.d.ts.map +1 -1
  12. package/dist/blobManager.js +317 -99
  13. package/dist/blobManager.js.map +1 -1
  14. package/dist/containerRuntime.d.ts +109 -124
  15. package/dist/containerRuntime.d.ts.map +1 -1
  16. package/dist/containerRuntime.js +349 -542
  17. package/dist/containerRuntime.js.map +1 -1
  18. package/dist/dataStore.js +29 -24
  19. package/dist/dataStore.js.map +1 -1
  20. package/dist/dataStoreContext.d.ts +20 -14
  21. package/dist/dataStoreContext.d.ts.map +1 -1
  22. package/dist/dataStoreContext.js +49 -58
  23. package/dist/dataStoreContext.js.map +1 -1
  24. package/dist/dataStores.d.ts +12 -5
  25. package/dist/dataStores.d.ts.map +1 -1
  26. package/dist/dataStores.js +21 -20
  27. package/dist/dataStores.js.map +1 -1
  28. package/dist/deltaScheduler.d.ts +6 -4
  29. package/dist/deltaScheduler.d.ts.map +1 -1
  30. package/dist/deltaScheduler.js +6 -4
  31. package/dist/deltaScheduler.js.map +1 -1
  32. package/dist/garbageCollection.d.ts +74 -14
  33. package/dist/garbageCollection.d.ts.map +1 -1
  34. package/dist/garbageCollection.js +249 -170
  35. package/dist/garbageCollection.js.map +1 -1
  36. package/dist/gcSweepReadyUsageDetection.d.ts +53 -0
  37. package/dist/gcSweepReadyUsageDetection.d.ts.map +1 -0
  38. package/dist/gcSweepReadyUsageDetection.js +126 -0
  39. package/dist/gcSweepReadyUsageDetection.js.map +1 -0
  40. package/dist/index.d.ts +2 -1
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +3 -2
  43. package/dist/index.js.map +1 -1
  44. package/dist/opProperties.d.ts +7 -0
  45. package/dist/opProperties.d.ts.map +1 -0
  46. package/dist/opProperties.js +20 -0
  47. package/dist/opProperties.js.map +1 -0
  48. package/dist/orderedClientElection.d.ts +28 -10
  49. package/dist/orderedClientElection.d.ts.map +1 -1
  50. package/dist/orderedClientElection.js +14 -4
  51. package/dist/orderedClientElection.js.map +1 -1
  52. package/dist/packageVersion.d.ts +1 -1
  53. package/dist/packageVersion.d.ts.map +1 -1
  54. package/dist/packageVersion.js +1 -1
  55. package/dist/packageVersion.js.map +1 -1
  56. package/dist/pendingStateManager.d.ts +0 -11
  57. package/dist/pendingStateManager.d.ts.map +1 -1
  58. package/dist/pendingStateManager.js +24 -46
  59. package/dist/pendingStateManager.js.map +1 -1
  60. package/dist/runningSummarizer.d.ts +14 -4
  61. package/dist/runningSummarizer.d.ts.map +1 -1
  62. package/dist/runningSummarizer.js +68 -26
  63. package/dist/runningSummarizer.js.map +1 -1
  64. package/dist/scheduleManager.d.ts +31 -0
  65. package/dist/scheduleManager.d.ts.map +1 -0
  66. package/dist/scheduleManager.js +243 -0
  67. package/dist/scheduleManager.js.map +1 -0
  68. package/dist/summarizer.d.ts +0 -2
  69. package/dist/summarizer.d.ts.map +1 -1
  70. package/dist/summarizer.js +1 -12
  71. package/dist/summarizer.js.map +1 -1
  72. package/dist/summarizerHeuristics.d.ts +26 -4
  73. package/dist/summarizerHeuristics.d.ts.map +1 -1
  74. package/dist/summarizerHeuristics.js +95 -18
  75. package/dist/summarizerHeuristics.js.map +1 -1
  76. package/dist/summarizerTypes.d.ts +45 -18
  77. package/dist/summarizerTypes.d.ts.map +1 -1
  78. package/dist/summarizerTypes.js +1 -1
  79. package/dist/summarizerTypes.js.map +1 -1
  80. package/dist/summaryCollection.d.ts +1 -0
  81. package/dist/summaryCollection.d.ts.map +1 -1
  82. package/dist/summaryCollection.js +31 -15
  83. package/dist/summaryCollection.js.map +1 -1
  84. package/dist/summaryFormat.d.ts +0 -5
  85. package/dist/summaryFormat.d.ts.map +1 -1
  86. package/dist/summaryFormat.js.map +1 -1
  87. package/dist/summaryGenerator.d.ts +1 -0
  88. package/dist/summaryGenerator.d.ts.map +1 -1
  89. package/dist/summaryGenerator.js +11 -9
  90. package/dist/summaryGenerator.js.map +1 -1
  91. package/dist/summaryManager.d.ts +2 -2
  92. package/dist/summaryManager.d.ts.map +1 -1
  93. package/dist/summaryManager.js +22 -7
  94. package/dist/summaryManager.js.map +1 -1
  95. package/lib/batchManager.d.ts +37 -0
  96. package/lib/batchManager.d.ts.map +1 -0
  97. package/lib/batchManager.js +69 -0
  98. package/lib/batchManager.js.map +1 -0
  99. package/lib/batchTracker.d.ts +1 -2
  100. package/lib/batchTracker.d.ts.map +1 -1
  101. package/lib/batchTracker.js +2 -3
  102. package/lib/batchTracker.js.map +1 -1
  103. package/lib/blobManager.d.ts +87 -25
  104. package/lib/blobManager.d.ts.map +1 -1
  105. package/lib/blobManager.js +319 -101
  106. package/lib/blobManager.js.map +1 -1
  107. package/lib/containerRuntime.d.ts +109 -124
  108. package/lib/containerRuntime.d.ts.map +1 -1
  109. package/lib/containerRuntime.js +355 -547
  110. package/lib/containerRuntime.js.map +1 -1
  111. package/lib/dataStore.js +29 -24
  112. package/lib/dataStore.js.map +1 -1
  113. package/lib/dataStoreContext.d.ts +20 -14
  114. package/lib/dataStoreContext.d.ts.map +1 -1
  115. package/lib/dataStoreContext.js +46 -55
  116. package/lib/dataStoreContext.js.map +1 -1
  117. package/lib/dataStores.d.ts +12 -5
  118. package/lib/dataStores.d.ts.map +1 -1
  119. package/lib/dataStores.js +21 -20
  120. package/lib/dataStores.js.map +1 -1
  121. package/lib/deltaScheduler.d.ts +6 -4
  122. package/lib/deltaScheduler.d.ts.map +1 -1
  123. package/lib/deltaScheduler.js +6 -4
  124. package/lib/deltaScheduler.js.map +1 -1
  125. package/lib/garbageCollection.d.ts +74 -14
  126. package/lib/garbageCollection.d.ts.map +1 -1
  127. package/lib/garbageCollection.js +238 -160
  128. package/lib/garbageCollection.js.map +1 -1
  129. package/lib/gcSweepReadyUsageDetection.d.ts +53 -0
  130. package/lib/gcSweepReadyUsageDetection.d.ts.map +1 -0
  131. package/lib/gcSweepReadyUsageDetection.js +121 -0
  132. package/lib/gcSweepReadyUsageDetection.js.map +1 -0
  133. package/lib/index.d.ts +2 -1
  134. package/lib/index.d.ts.map +1 -1
  135. package/lib/index.js +2 -1
  136. package/lib/index.js.map +1 -1
  137. package/lib/opProperties.d.ts +7 -0
  138. package/lib/opProperties.d.ts.map +1 -0
  139. package/lib/opProperties.js +16 -0
  140. package/lib/opProperties.js.map +1 -0
  141. package/lib/orderedClientElection.d.ts +28 -10
  142. package/lib/orderedClientElection.d.ts.map +1 -1
  143. package/lib/orderedClientElection.js +14 -4
  144. package/lib/orderedClientElection.js.map +1 -1
  145. package/lib/packageVersion.d.ts +1 -1
  146. package/lib/packageVersion.d.ts.map +1 -1
  147. package/lib/packageVersion.js +1 -1
  148. package/lib/packageVersion.js.map +1 -1
  149. package/lib/pendingStateManager.d.ts +0 -11
  150. package/lib/pendingStateManager.d.ts.map +1 -1
  151. package/lib/pendingStateManager.js +24 -46
  152. package/lib/pendingStateManager.js.map +1 -1
  153. package/lib/runningSummarizer.d.ts +14 -4
  154. package/lib/runningSummarizer.d.ts.map +1 -1
  155. package/lib/runningSummarizer.js +68 -26
  156. package/lib/runningSummarizer.js.map +1 -1
  157. package/lib/scheduleManager.d.ts +31 -0
  158. package/lib/scheduleManager.d.ts.map +1 -0
  159. package/lib/scheduleManager.js +239 -0
  160. package/lib/scheduleManager.js.map +1 -0
  161. package/lib/summarizer.d.ts +0 -2
  162. package/lib/summarizer.d.ts.map +1 -1
  163. package/lib/summarizer.js +1 -12
  164. package/lib/summarizer.js.map +1 -1
  165. package/lib/summarizerHeuristics.d.ts +26 -4
  166. package/lib/summarizerHeuristics.d.ts.map +1 -1
  167. package/lib/summarizerHeuristics.js +95 -18
  168. package/lib/summarizerHeuristics.js.map +1 -1
  169. package/lib/summarizerTypes.d.ts +45 -18
  170. package/lib/summarizerTypes.d.ts.map +1 -1
  171. package/lib/summarizerTypes.js +1 -1
  172. package/lib/summarizerTypes.js.map +1 -1
  173. package/lib/summaryCollection.d.ts +1 -0
  174. package/lib/summaryCollection.d.ts.map +1 -1
  175. package/lib/summaryCollection.js +31 -15
  176. package/lib/summaryCollection.js.map +1 -1
  177. package/lib/summaryFormat.d.ts +0 -5
  178. package/lib/summaryFormat.d.ts.map +1 -1
  179. package/lib/summaryFormat.js.map +1 -1
  180. package/lib/summaryGenerator.d.ts +1 -0
  181. package/lib/summaryGenerator.d.ts.map +1 -1
  182. package/lib/summaryGenerator.js +11 -9
  183. package/lib/summaryGenerator.js.map +1 -1
  184. package/lib/summaryManager.d.ts +2 -2
  185. package/lib/summaryManager.d.ts.map +1 -1
  186. package/lib/summaryManager.js +22 -7
  187. package/lib/summaryManager.js.map +1 -1
  188. package/package.json +65 -24
  189. package/src/batchManager.ts +91 -0
  190. package/src/batchTracker.ts +2 -3
  191. package/src/blobManager.ts +385 -118
  192. package/src/containerRuntime.ts +529 -740
  193. package/src/dataStore.ts +49 -37
  194. package/src/dataStoreContext.ts +44 -56
  195. package/src/dataStores.ts +34 -30
  196. package/src/deltaScheduler.ts +6 -4
  197. package/src/garbageCollection.ts +297 -206
  198. package/src/gcSweepReadyUsageDetection.ts +139 -0
  199. package/src/index.ts +1 -2
  200. package/src/opProperties.ts +19 -0
  201. package/src/orderedClientElection.ts +31 -10
  202. package/src/packageVersion.ts +1 -1
  203. package/src/pendingStateManager.ts +27 -59
  204. package/src/runningSummarizer.ts +75 -22
  205. package/src/scheduleManager.ts +314 -0
  206. package/src/summarizer.ts +1 -18
  207. package/src/summarizerHeuristics.ts +133 -19
  208. package/src/summarizerTypes.ts +53 -18
  209. package/src/summaryCollection.ts +33 -18
  210. package/src/summaryFormat.ts +0 -6
  211. package/src/summaryGenerator.ts +40 -22
  212. package/src/summaryManager.ts +22 -7
  213. package/dist/opTelemetry.d.ts +0 -22
  214. package/dist/opTelemetry.d.ts.map +0 -1
  215. package/dist/opTelemetry.js +0 -59
  216. package/dist/opTelemetry.js.map +0 -1
  217. package/lib/opTelemetry.d.ts +0 -22
  218. package/lib/opTelemetry.d.ts.map +0 -1
  219. package/lib/opTelemetry.js +0 -55
  220. package/lib/opTelemetry.js.map +0 -1
  221. package/src/opTelemetry.ts +0 -71
@@ -3,20 +3,22 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
+ import { v4 as uuid } from "uuid";
6
7
  import { IFluidHandle, IFluidHandleContext } from "@fluidframework/core-interfaces";
7
8
  import { IDocumentStorageService } from "@fluidframework/driver-definitions";
8
- import { ISnapshotTree } from "@fluidframework/protocol-definitions";
9
+ import { ICreateBlobResponse, ISequencedDocumentMessage, ISnapshotTree } from "@fluidframework/protocol-definitions";
9
10
  import { generateHandleContextPath, SummaryTreeBuilder } from "@fluidframework/runtime-utils";
10
11
  import { ITelemetryLogger } from "@fluidframework/common-definitions";
11
- import { assert, Deferred } from "@fluidframework/common-utils";
12
- import { IContainerRuntime } from "@fluidframework/container-runtime-definitions";
12
+ import { assert, bufferToString, Deferred, stringToBuffer, TypedEventEmitter } from "@fluidframework/common-utils";
13
+ import { IContainerRuntime, IContainerRuntimeEvents } from "@fluidframework/container-runtime-definitions";
13
14
  import { AttachState } from "@fluidframework/container-definitions";
14
- import { PerformanceEvent } from "@fluidframework/telemetry-utils";
15
+ import { ChildLogger, PerformanceEvent } from "@fluidframework/telemetry-utils";
15
16
  import {
16
17
  IGarbageCollectionData,
17
18
  ISummaryTreeWithStats,
18
19
  ITelemetryContext,
19
20
  } from "@fluidframework/runtime-definitions";
21
+ import { Throttler, formExponentialFn, IThrottler } from "./throttler";
20
22
 
21
23
  /**
22
24
  * This class represents blob (long string)
@@ -53,6 +55,23 @@ export class BlobHandle implements IFluidHandle<ArrayBufferLike> {
53
55
  }
54
56
  }
55
57
 
58
+ class CancellableThrottler {
59
+ constructor(private readonly throttler: IThrottler) { }
60
+ private cancelP = new Deferred<void>();
61
+
62
+ public async getDelay(): Promise<void> {
63
+ return Promise.race([
64
+ this.cancelP.promise,
65
+ new Promise<void>((resolve) => setTimeout(resolve, this.throttler.getDelay())),
66
+ ]);
67
+ }
68
+
69
+ public cancel() {
70
+ this.cancelP.resolve();
71
+ this.cancelP = new Deferred<void>();
72
+ }
73
+ }
74
+
56
75
  /**
57
76
  * Information from a snapshot needed to load BlobManager
58
77
  */
@@ -61,40 +80,135 @@ export interface IBlobManagerLoadInfo {
61
80
  redirectTable?: [string, string][];
62
81
  }
63
82
 
83
+ // Restrict the IContainerRuntime interface to the subset required by BlobManager. This helps to make
84
+ // the contract explicit and reduces the amount of mocking required for tests.
85
+ export type IBlobManagerRuntime =
86
+ Pick<IContainerRuntime, "attachState" | "connected" | "logger"> & TypedEventEmitter<IContainerRuntimeEvents>;
87
+
88
+ // Note that while offline we "submit" an op before uploading the blob, but we always
89
+ // expect blobs to be uploaded before we actually see the op round-trip
90
+ enum PendingBlobStatus {
91
+ OnlinePendingUpload,
92
+ OnlinePendingOp,
93
+ OfflinePendingUpload,
94
+ OfflinePendingOp,
95
+ }
96
+
97
+ interface PendingBlob {
98
+ blob: ArrayBufferLike;
99
+ status: PendingBlobStatus;
100
+ storageId?: string;
101
+ handleP: Deferred<IFluidHandle<ArrayBufferLike>>;
102
+ uploadP: Promise<ICreateBlobResponse>;
103
+ }
104
+
105
+ export interface IPendingBlobs { [id: string]: { blob: string; }; }
106
+
64
107
  export class BlobManager {
65
108
  public static readonly basePath = "_blobs";
66
109
  private static readonly redirectTableBlobName = ".redirectTable";
67
- // uploaded blob IDs
68
- private readonly blobIds: Set<string> = new Set();
69
- // blobs for which upload is pending. maps to a promise that will resolve once the blob has been uploaded and a
70
- // BlobAttach op has round-tripped.
71
- private readonly pendingBlobIds: Map<string, Deferred<void>> = new Map();
72
- // blobs uploaded while detached; cleared upon attach
73
- private readonly detachedBlobIds: Set<string> = new Set();
74
- // map of detached blob IDs to IDs used by storage. used to support blob handles given out while detached
75
- private redirectTable: Map<string, string> | undefined;
110
+ private readonly logger: ITelemetryLogger;
111
+
112
+ /**
113
+ * Map of local (offline/detached) IDs to storage IDs. Contains identity entries
114
+ * (id id) for storage IDs, so all requested IDs should be a key in this map.
115
+ * Blobs created while the container is detached are stored in IDetachedBlobStorage
116
+ * which gives local IDs; the storage IDs are filled in at attach time.
117
+ */
118
+ private readonly redirectTable: Map<string, string | undefined>;
119
+
120
+ /**
121
+ * Blobs which have not been uploaded or for which we have not yet seen a BlobAttach op round-trip.
122
+ * Until we see the op round-trip, there is a possibility we may need to re-upload the blob, so
123
+ * we must save it. This is true for both the online and offline flow.
124
+ */
125
+ private readonly pendingBlobs: Map<string, PendingBlob> = new Map();
126
+
127
+ /**
128
+ * Track ops in flight for online flow. Used to avoid searching pendingBlobs since BlobAttach ops
129
+ * don't include local ID in online flow.
130
+ */
131
+ private readonly opsInFlight: Map<string, string[]> = new Map();
132
+
133
+ private readonly retryThrottler = new CancellableThrottler(new Throttler(
134
+ 60 * 1000, // 60 sec delay window
135
+ 30 * 1000, // 30 sec max delay
136
+ // throttling function increases exponentially (0ms, 40ms, 80ms, 160ms, etc)
137
+ formExponentialFn({ coefficient: 20, initialDelay: 0 }),
138
+ ));
76
139
 
77
140
  constructor(
78
141
  private readonly routeContext: IFluidHandleContext,
79
142
  snapshot: IBlobManagerLoadInfo,
80
143
  private readonly getStorage: () => IDocumentStorageService,
81
- private readonly attachBlobCallback: (blobId: string) => void,
144
+ /**
145
+ * Submit a BlobAttach op. When a blob is uploaded, there is a short grace period before which
146
+ * the blob is deleted. The BlobAttach op notifies the server that blob is in use. The server
147
+ * will then not delete the blob as long as it is listed as referenced in future summaries.
148
+ * The summarizing client will know to include the storage ID in the summary when it sees the op.
149
+ *
150
+ * The op may also include a local ID to inform all clients of the relation to the storage
151
+ * ID, without knowledge of which they cannot request the blob from storage. This is also
152
+ * included in the redirect table in the summary.
153
+ */
154
+ private readonly sendBlobAttachOp: (storageId?: string, localId?: string) => void,
82
155
  // To be called when a blob node is requested. blobPath is the path of the blob's node in GC's graph. It's
83
156
  // of the format `/<BlobManager.basePath>/<blobId>`.
84
157
  private readonly gcNodeUpdated: (blobPath: string) => void,
85
- private readonly runtime: IContainerRuntime,
86
- private readonly logger: ITelemetryLogger,
158
+ private readonly runtime: IBlobManagerRuntime,
159
+ stashedBlobs: IPendingBlobs = {},
87
160
  ) {
88
- this.runtime.once("dispose", () => {
89
- for (const promise of this.pendingBlobIds.values()) {
90
- promise.reject(new Error("runtime disposed while blobAttach op in flight"));
91
- }
161
+ this.logger = ChildLogger.create(this.runtime.logger, "BlobManager");
162
+ this.runtime.on("disconnected", () => this.onDisconnected());
163
+ this.redirectTable = this.load(snapshot);
164
+
165
+ // Begin uploading stashed blobs from previous container instance
166
+ Object.entries(stashedBlobs).forEach(([localId, entry]) => {
167
+ const blob = stringToBuffer(entry.blob, "base64");
168
+ this.pendingBlobs.set(localId, {
169
+ blob,
170
+ status: PendingBlobStatus.OfflinePendingUpload,
171
+ handleP: new Deferred(),
172
+ uploadP: this.uploadBlob(localId, blob),
173
+ });
92
174
  });
93
- this.load(snapshot);
94
175
  }
95
176
 
96
- private hasBlob(id: string): boolean {
97
- return this.blobIds.has(id) || this.detachedBlobIds.has(id);
177
+ private get pendingOfflineUploads() {
178
+ return Array.from(this.pendingBlobs.values())
179
+ .filter((e) => e.status === PendingBlobStatus.OfflinePendingUpload);
180
+ }
181
+
182
+ public get hasPendingOfflineUploads(): boolean {
183
+ return this.pendingOfflineUploads.length > 0;
184
+ }
185
+
186
+ /**
187
+ * Upload blobs added while offline. This must be completed before connecting and resubmitting ops.
188
+ */
189
+ public async onConnected() {
190
+ this.retryThrottler.cancel();
191
+ const pendingUploads = this.pendingOfflineUploads.map(async (e) => e.uploadP);
192
+ await PerformanceEvent.timedExecAsync(this.logger, {
193
+ eventName: "BlobUploadOnConnected",
194
+ count: pendingUploads.length,
195
+ }, async () => Promise.all(pendingUploads),
196
+ { start: true, end: true },
197
+ );
198
+ }
199
+
200
+ /**
201
+ * Transition online blobs waiting for BlobAttach op round-trip since we will not see the op until we are connected
202
+ * again
203
+ */
204
+ private onDisconnected() {
205
+ for (const [localId, entry] of this.pendingBlobs) {
206
+ if (entry.status === PendingBlobStatus.OnlinePendingOp) {
207
+ // This will submit another BlobAttach op for this blob. This is necessary because the one we sent
208
+ // already didn't have the local ID.
209
+ this.transitionToOffline(localId);
210
+ }
211
+ }
98
212
  }
99
213
 
100
214
  /**
@@ -106,87 +220,241 @@ export class BlobManager {
106
220
  return `/${BlobManager.basePath}/${blobId}`;
107
221
  }
108
222
 
109
- public async getBlob(blobId: string): Promise<IFluidHandle<ArrayBufferLike>> {
110
- const storageId = this.redirectTable?.get(blobId) ?? blobId;
111
- assert(this.hasBlob(storageId), 0x11f /* "requesting unknown blobs" */);
223
+ /**
224
+ * Set of actual storage IDs (i.e., IDs that can be requested from storage). This will be empty if the container is
225
+ * detached or there are no (non-pending) attachment blobs in the document
226
+ */
227
+ private get storageIds(): Set<string> {
228
+ const ids = new Set<string | undefined>(this.redirectTable.values());
229
+
230
+ // If we are detached, we will not have storage IDs, only undefined
231
+ const undefinedValueInTable = ids.delete(undefined);
232
+
233
+ // For a detached container, entries are inserted into the redirect table with an undefined storage ID.
234
+ // For an attached container, entries are inserted w/storage ID after the BlobAttach op round-trips.
235
+ assert(!undefinedValueInTable || this.runtime.attachState === AttachState.Detached && ids.size === 0,
236
+ 0x382 /* 'redirectTable' must contain only undefined while detached / defined values while attached */);
237
+
238
+ return ids as Set<string>;
239
+ }
240
+
241
+ public async getBlob(blobId: string): Promise<ArrayBufferLike> {
242
+ const pending = this.pendingBlobs.get(blobId);
243
+ if (pending) {
244
+ return pending.blob;
245
+ }
246
+ let storageId;
247
+ if (this.runtime.attachState === AttachState.Detached) {
248
+ assert(this.redirectTable.has(blobId), 0x383 /* requesting unknown blobs */);
249
+
250
+ // Blobs created while the container is detached are stored in IDetachedBlobStorage.
251
+ // The 'IDocumentStorageService.readBlob()' call below will retrieve these via localId.
252
+ storageId = blobId;
253
+ } else {
254
+ storageId = this.redirectTable.get(blobId);
255
+ assert(!!storageId, 0x11f /* "requesting unknown blobs" */);
256
+ }
112
257
 
113
- // When this blob is retrieved, let the container runtime know that the corresponding GC node got updated.
258
+ // When a GC-able (not pending) blob is retrieved, let runtime know that the corresponding GC node got updated.
114
259
  this.gcNodeUpdated(this.getBlobGCNodePath(blobId));
115
260
 
261
+ return PerformanceEvent.timedExecAsync(
262
+ this.logger,
263
+ { eventName: "AttachmentReadBlob", id: storageId },
264
+ async () => {
265
+ return this.getStorage().readBlob(storageId);
266
+ },
267
+ { end: true, cancel: "error" },
268
+ );
269
+ }
270
+
271
+ private getBlobHandle(id: string): IFluidHandle<ArrayBufferLike> {
272
+ assert(this.redirectTable.has(id) || this.pendingBlobs.has(id),
273
+ 0x384 /* requesting handle for unknown blob */);
116
274
  return new BlobHandle(
117
- `${BlobManager.basePath}/${storageId}`,
275
+ `${BlobManager.basePath}/${id}`,
118
276
  this.routeContext,
119
- async () => PerformanceEvent.timedExecAsync(
120
- this.logger,
121
- { eventName: "AttachmentReadBlob", id: storageId },
122
- async () => {
123
- return this.getStorage().readBlob(storageId);
124
- },
125
- { end: true, cancel: "error" },
126
- ),
277
+ async () => this.getBlob(id),
127
278
  );
128
279
  }
129
280
 
281
+ private async createBlobDetached(blob: ArrayBufferLike): Promise<IFluidHandle<ArrayBufferLike>> {
282
+ // Blobs created while the container is detached are stored in IDetachedBlobStorage.
283
+ // The 'IDocumentStorageService.createBlob()' call below will respond with a localId.
284
+ const response = await this.getStorage().createBlob(blob);
285
+ this.redirectTable.set(response.id, undefined);
286
+ return this.getBlobHandle(response.id);
287
+ }
288
+
130
289
  public async createBlob(blob: ArrayBufferLike): Promise<IFluidHandle<ArrayBufferLike>> {
290
+ if (this.runtime.attachState === AttachState.Detached) {
291
+ return this.createBlobDetached(blob);
292
+ }
131
293
  if (this.runtime.attachState === AttachState.Attaching) {
132
294
  // blob upload is not supported in "Attaching" state
133
295
  this.logger.sendTelemetryEvent({ eventName: "CreateBlobWhileAttaching" });
134
296
  await new Promise<void>((resolve) => this.runtime.once("attached", resolve));
135
297
  }
298
+ assert(this.runtime.attachState === AttachState.Attached,
299
+ 0x385 /* For clarity and paranoid defense against adding future attachment states */);
300
+
301
+ // Create a local ID for each blob. This is used to support blobs if/when the client goes
302
+ // offline since we don't have the ID from storage yet. If online flow succeeds this won't be used.
303
+ const localId = uuid();
304
+ const pendingEntry: PendingBlob = {
305
+ blob,
306
+ status: PendingBlobStatus.OnlinePendingUpload,
307
+ handleP: new Deferred(),
308
+ uploadP: this.uploadBlob(localId, blob),
309
+ };
310
+ this.pendingBlobs.set(localId, pendingEntry);
311
+
312
+ return pendingEntry.handleP.promise;
313
+ }
136
314
 
137
- if (!this.runtime.connected && this.runtime.attachState === AttachState.Attached) {
138
- // see https://github.com/microsoft/FluidFramework/issues/8246
139
- // Avoid getting storage if we are offline since it might be undefined. In the future we will return
140
- // handles immediately while offline
141
- await new Promise((resolve) => this.runtime.once("connected", resolve));
142
- }
143
-
144
- const response = await PerformanceEvent.timedExecAsync(
315
+ private async uploadBlob(localId: string, blob: ArrayBufferLike): Promise<ICreateBlobResponse> {
316
+ return PerformanceEvent.timedExecAsync(
145
317
  this.logger,
146
318
  { eventName: "createBlob" },
147
319
  async () => this.getStorage().createBlob(blob),
148
- { end: true, cancel: "error" },
320
+ { end: true, cancel: this.runtime.connected ? "error" : "generic" },
321
+ ).then(
322
+ (response) => this.onUploadResolve(localId, response),
323
+ async (err) => this.onUploadReject(localId, err),
149
324
  );
325
+ }
150
326
 
151
- const handle = new BlobHandle(
152
- `${BlobManager.basePath}/${response.id}`,
153
- this.routeContext,
154
- // get() should go through BlobManager.getBlob() so handles created while detached can be redirected
155
- // to the correct storage id after they are uploaded
156
- async () => this.getBlob(response.id).then(async (h) => h.get()),
157
- );
327
+ private onUploadResolve(localId: string, response: ICreateBlobResponse) {
328
+ const entry = this.pendingBlobs.get(localId);
329
+ assert(entry?.status === PendingBlobStatus.OnlinePendingUpload ||
330
+ entry?.status === PendingBlobStatus.OfflinePendingUpload,
331
+ 0x386 /* Must have pending blob entry for uploaded blob */);
332
+ entry.storageId = response.id;
333
+ if (this.runtime.connected) {
334
+ if (entry.status === PendingBlobStatus.OnlinePendingUpload) {
335
+ if (this.storageIds.has(response.id)) {
336
+ // Storage may dedupe blobs and give us an ID we already know
337
+ // no need to submit BlobAttach op in this case
338
+ entry.handleP.resolve(this.getBlobHandle(response.id));
339
+ this.pendingBlobs.delete(localId);
340
+ } else {
341
+ // Check for still-pending duplicates too; if an op is already in flight we can wait for that one
342
+ if (!this.opsInFlight.has(response.id)) {
343
+ this.sendBlobAttachOp(response.id);
344
+ }
345
+ this.opsInFlight.set(response.id, (this.opsInFlight.get(response.id) ?? []).concat(localId));
346
+ entry.status = PendingBlobStatus.OnlinePendingOp;
347
+ }
348
+ } else if (entry.status === PendingBlobStatus.OfflinePendingUpload) {
349
+ // We already submitted a BlobAttach op for this blob when it was transitioned to offline flow
350
+ entry.status = PendingBlobStatus.OfflinePendingOp;
351
+ }
352
+ } else {
353
+ // connected to storage but not ordering service?
354
+ this.logger.sendTelemetryEvent({ eventName: "BlobUploadSuccessWhileDisconnected" });
355
+ if (entry.status === PendingBlobStatus.OnlinePendingUpload) {
356
+ this.transitionToOffline(localId);
357
+ }
358
+ entry.status = PendingBlobStatus.OfflinePendingOp;
359
+ }
360
+ return response;
361
+ }
158
362
 
159
- if (this.runtime.attachState === AttachState.Detached) {
160
- this.detachedBlobIds.add(response.id);
161
- return handle;
363
+ private async onUploadReject(localId: string, error) {
364
+ const entry = this.pendingBlobs.get(localId);
365
+ assert(!!entry, 0x387 /* Must have pending blob entry for blob which failed to upload */);
366
+ if (!this.runtime.connected) {
367
+ if (entry.status === PendingBlobStatus.OnlinePendingUpload) {
368
+ this.transitionToOffline(localId);
369
+ }
370
+ // we are probably not connected to storage but start another upload request in case we are
371
+ entry.uploadP = this.retryThrottler.getDelay().then(async () => this.uploadBlob(localId, entry.blob));
372
+ return entry.uploadP;
373
+ } else {
374
+ entry.handleP.reject(error);
375
+ throw error;
162
376
  }
377
+ }
163
378
 
164
- // Note - server will de-dup blobs, so we might get existing blobId!
165
- if (this.pendingBlobIds.has(response.id)) {
166
- await this.pendingBlobIds.get(response.id)?.promise;
167
- } else if (!this.blobIds.has(response.id)) {
168
- this.pendingBlobIds.set(response.id, new Deferred<void>());
379
+ private transitionToOffline(localId: string) {
380
+ assert(!this.runtime.connected, 0x388 /* Must only transition to offline flow while runtime is disconnected */);
381
+ const entry = this.pendingBlobs.get(localId);
382
+ assert(!!entry, 0x389 /* No pending blob entry */);
383
+ assert([PendingBlobStatus.OnlinePendingUpload, PendingBlobStatus.OnlinePendingOp].includes(entry.status),
384
+ 0x38a /* Blob must be in online flow to transition to offline flow */);
385
+
386
+ entry.status = entry.status === PendingBlobStatus.OnlinePendingUpload
387
+ ? PendingBlobStatus.OfflinePendingUpload
388
+ : PendingBlobStatus.OfflinePendingOp;
389
+
390
+ // Submit a BlobAttach op. It's important we submit this op now before returning the blob handle so the
391
+ // BlobAttach op is sequenced prior to any ops referencing the handle. Otherwise an invalid handle could be
392
+ // added to the document if the ops are not all successfully submitted upon reconnection.
393
+ // storageId may be undefined but since we are not connected we will have a chance to add it when reSubmit()
394
+ // is called
395
+ this.sendBlobAttachOp(entry.storageId, localId);
396
+ entry.handleP.resolve(this.getBlobHandle(localId));
397
+ }
169
398
 
170
- // send blob attach op and wait until we see it to return the handle
171
- this.attachBlobCallback(response.id);
172
- await this.pendingBlobIds.get(response.id)?.promise;
399
+ /**
400
+ * Resubmit a BlobAttach op. Used to add storage IDs to ops that were
401
+ * submitted to runtime while disconnected.
402
+ * @param metadata - op metadata containing storage and/or local IDs
403
+ */
404
+ public reSubmit(metadata: Record<string, unknown> | undefined) {
405
+ assert(!!metadata, 0x38b /* Resubmitted ops must have metadata */);
406
+ const { blobId, localId }: { blobId?: string; localId?: string; } = metadata;
407
+ if (!blobId) {
408
+ assert(!!localId, 0x38c /* Submitted BlobAttach ops must have a blobId or localId */);
409
+ // We submitted this op while offline. The blob should have been uploaded by now.
410
+ const pendingEntry = this.pendingBlobs.get(localId);
411
+ assert(pendingEntry?.status === PendingBlobStatus.OfflinePendingOp &&
412
+ !!pendingEntry?.storageId, 0x38d /* blob must be uploaded before resubmitting BlobAttach op */);
413
+ return this.sendBlobAttachOp(pendingEntry.storageId, localId);
173
414
  }
174
-
175
- return handle;
415
+ return this.sendBlobAttachOp(blobId, localId);
176
416
  }
177
417
 
178
- public processBlobAttachOp(blobId: string, local: boolean) {
418
+ public processBlobAttachOp(message: ISequencedDocumentMessage, local: boolean) {
419
+ assert(message?.metadata?.blobId, 0x12a /* "Missing blob id on metadata" */);
420
+ if (message.metadata.localId !== undefined) {
421
+ this.redirectTable.set(message.metadata.localId, message.metadata.blobId);
422
+ }
423
+ // set identity (id -> id) entry
424
+ this.redirectTable.set(message.metadata.blobId, message.metadata.blobId);
425
+
179
426
  if (local) {
180
- const pendingBlobP = this.pendingBlobIds.get(blobId);
181
- assert(pendingBlobP !== undefined, 0x1f8 /* "local BlobAttach op with no pending blob" */);
182
- pendingBlobP.resolve();
183
- this.pendingBlobIds.delete(blobId);
427
+ if (message.metadata.localId === undefined) {
428
+ // Since there is no local ID, we know this op was submitted while online.
429
+ const waitingBlobs = this.opsInFlight.get(message.metadata.blobId);
430
+ assert(!!waitingBlobs, 0x38e /* local online BlobAttach op with no pending blob */);
431
+ waitingBlobs.forEach((localId) => {
432
+ const pendingBlobEntry = this.pendingBlobs.get(localId);
433
+ assert(
434
+ pendingBlobEntry !== undefined,
435
+ 0x38f, /* local online BlobAttach op with no pending blob entry */
436
+ );
437
+
438
+ // It's possible we transitioned to offline flow while waiting for this op.
439
+ if (pendingBlobEntry.status === PendingBlobStatus.OnlinePendingOp) {
440
+ pendingBlobEntry.handleP.resolve(this.getBlobHandle(message.metadata.blobId));
441
+ this.pendingBlobs.delete(localId);
442
+ }
443
+ });
444
+ } else {
445
+ // Each local ID is unique; get the pending blob entry and delete it
446
+ assert(this.pendingBlobs.get(message.metadata.localId)?.status === PendingBlobStatus.OfflinePendingOp,
447
+ 0x1f8 /* "local BlobAttach op with no pending blob" */);
448
+ this.pendingBlobs.delete(message.metadata.localId);
449
+ }
184
450
  }
185
- this.blobIds.add(blobId);
186
451
  }
187
452
 
188
453
  /**
189
454
  * Reads blobs needed to load BlobManager from storage.
455
+ * @param blobsTree - Tree containing IDs of previously attached blobs. We
456
+ * look for the IDs in the blob entries of the tree since the both the r11s
457
+ * and SPO drivers replace the attachment types returned in snapshot() with blobs.
190
458
  */
191
459
  public static async load(
192
460
  blobsTree: ISnapshotTree | undefined,
@@ -206,32 +474,22 @@ export class BlobManager {
206
474
  }
207
475
 
208
476
  /**
209
- * Load a set of previously attached blob IDs from a previous snapshot. Note
210
- * that BlobManager tracking and reporting attached blobs is a temporary
211
- * solution since storage expects attached blobs to be reported and any that
212
- * are not reported as attached may be GCed. In the future attached blob
213
- * IDs will be collected at summarization time, and runtime will not care
214
- * about the existence or specific formatting of this tree in returned
215
- * snapshots.
216
- *
217
- * @param blobsTree - Tree containing IDs of previously attached blobs. This
218
- * corresponds to snapshot() below. We look for the IDs in the blob entries
219
- * of the tree since the both the r11s and SPO drivers replace the
220
- * attachment types returned in snapshot() with blobs.
477
+ * Load a set of previously attached blob IDs and redirect table from a previous snapshot.
221
478
  */
222
- private load(snapshot: IBlobManagerLoadInfo): void {
223
- if (snapshot.ids) {
224
- const detached = this.runtime.attachState === AttachState.Detached;
225
- snapshot.ids.map((entry) => detached ? this.detachedBlobIds.add(entry) : this.blobIds.add(entry));
226
- }
227
- if (snapshot.redirectTable) {
228
- this.redirectTable = new Map(snapshot.redirectTable);
229
- }
479
+ private load(snapshot: IBlobManagerLoadInfo): Map<string, string | undefined> {
230
480
  this.logger.sendTelemetryEvent({
231
481
  eventName: "AttachmentBlobsLoaded",
232
482
  count: snapshot.ids?.length ?? 0,
233
483
  redirectTable: snapshot.redirectTable?.length,
234
484
  });
485
+ const table = new Map<string, string | undefined>(snapshot.redirectTable);
486
+ if (snapshot.ids) {
487
+ const detached = this.runtime.attachState === AttachState.Detached;
488
+ // If we are detached, we don't have storage IDs yet, so set to undefined
489
+ // Otherwise, set identity (id -> id) entries
490
+ snapshot.ids.forEach((entry) => table.set(entry, detached ? undefined : entry));
491
+ }
492
+ return table;
235
493
  }
236
494
 
237
495
  /**
@@ -242,24 +500,24 @@ export class BlobManager {
242
500
  */
243
501
  public getGCData(fullGC: boolean = false): IGarbageCollectionData {
244
502
  const gcData: IGarbageCollectionData = { gcNodes: {} };
245
-
246
- this.blobIds.forEach((blobId: string) => {
503
+ /**
504
+ * The node path is of the format `/_blobs/blobId`. This path must match the path of the blob handle returned
505
+ * by the createBlob API because blobs are marked referenced by storing these handles in a referenced DDS.
506
+ */
507
+ this.storageIds.forEach((blobId: string) => {
247
508
  gcData.gcNodes[this.getBlobGCNodePath(blobId)] = [];
248
509
  });
249
510
 
250
- /**
251
- * For all blobs in the redirect table, the handle returned on creation is based off of the localId. So, these
252
- * nodes can be referenced by storing the localId handle. When that happens, the corresponding storageId node
253
- * must also be marked referenced. So, we add a route from the localId node to the storageId node.
254
- * Note that because of de-duping, there can be multiple localIds that all redirect to the same storageId or
255
- * a blob may be referenced via its storageId handle.
256
- */
257
- if (this.redirectTable !== undefined) {
258
- for (const [localId, storageId] of this.redirectTable) {
259
- // Add node for the localId and add a route to the storageId node. The storageId node will have been
260
- // added above when adding nodes for this.blobIds.
261
- gcData.gcNodes[this.getBlobGCNodePath(localId)] = [this.getBlobGCNodePath(storageId)];
262
- }
511
+ // For some blobs, the handle returned on creation is based off of the localId. So, these
512
+ // nodes can be referenced by storing the localId handle. When that happens, the corresponding storageId node
513
+ // must also be marked referenced. So, we add a route from the localId node to the storageId node.
514
+ // Note that because of de-duping, there can be multiple localIds that all redirect to the same storageId or
515
+ // a blob may be referenced via its storageId handle.
516
+ for (const [localId, storageId] of this.redirectTable) {
517
+ assert(!!storageId, 0x390 /* Must be attached to get GC data */);
518
+ // Add node for the localId and add a route to the storageId node. The storageId node will have been
519
+ // added above when adding nodes for this.blobIds.
520
+ gcData.gcNodes[this.getBlobGCNodePath(localId)] = [this.getBlobGCNodePath(storageId)];
263
521
  }
264
522
 
265
523
  return gcData;
@@ -286,24 +544,24 @@ export class BlobManager {
286
544
  this.redirectTable.delete(blobId);
287
545
  continue;
288
546
  }
289
- this.blobIds.delete(blobId);
290
547
  }
291
548
  }
292
549
 
293
550
  public summarize(telemetryContext?: ITelemetryContext): ISummaryTreeWithStats {
294
- // If we have a redirect table it means the container is about to transition to "Attaching" state, so we need
295
- // to return an actual snapshot containing all the real storage IDs we know about.
296
- const attachingOrAttached = !!this.redirectTable || this.runtime.attachState !== AttachState.Detached;
297
- const blobIds = attachingOrAttached ? this.blobIds : this.detachedBlobIds;
551
+ // if storageIds is empty, it means we are detached and have only local IDs, or that there are no blobs attached
552
+ const blobIds = this.storageIds.size > 0 ? Array.from(this.storageIds) : Array.from(this.redirectTable.keys());
298
553
  const builder = new SummaryTreeBuilder();
299
554
  blobIds.forEach((blobId) => {
300
555
  builder.addAttachment(blobId);
301
556
  });
302
557
 
303
- if (this.redirectTable && this.redirectTable.size > 0) {
558
+ // Any non-identity entries in the table need to be saved in the summary
559
+ if (this.redirectTable.size > blobIds.length) {
304
560
  builder.addBlob(
305
561
  BlobManager.redirectTableBlobName,
306
- JSON.stringify(Array.from(this.redirectTable.entries())),
562
+ // filter out identity entries
563
+ JSON.stringify(Array.from(this.redirectTable.entries())
564
+ .filter(([localId, storageId]) => localId !== storageId)),
307
565
  );
308
566
  }
309
567
 
@@ -313,12 +571,21 @@ export class BlobManager {
313
571
  public setRedirectTable(table: Map<string, string>) {
314
572
  assert(this.runtime.attachState === AttachState.Detached,
315
573
  0x252 /* "redirect table can only be set in detached container" */);
316
- assert(!this.redirectTable, 0x253 /* "redirect table already exists" */);
574
+ assert(this.redirectTable.size === table.size,
575
+ 0x391 /* Redirect table size must match BlobManager's local ID count */);
317
576
  for (const [localId, storageId] of table) {
318
- assert(this.detachedBlobIds.delete(localId), 0x254 /* "unrecognized id in redirect table" */);
319
- this.blobIds.add(storageId);
577
+ assert(this.redirectTable.has(localId), 0x254 /* "unrecognized id in redirect table" */);
578
+ this.redirectTable.set(localId, storageId);
579
+ // set identity (id -> id) entry
580
+ this.redirectTable.set(storageId, storageId);
581
+ }
582
+ }
583
+
584
+ public getPendingBlobs(): IPendingBlobs {
585
+ const blobs = {};
586
+ for (const [key, entry] of this.pendingBlobs) {
587
+ blobs[key] = { blob: bufferToString(entry.blob, "base64") };
320
588
  }
321
- assert(this.detachedBlobIds.size === 0, 0x255 /* "detached blob id absent in redirect table" */);
322
- this.redirectTable = table;
589
+ return blobs;
323
590
  }
324
591
  }