@fluidframework/container-runtime 2.0.0-internal.2.2.1 → 2.0.0-internal.2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/.eslintrc.js +19 -8
  2. package/dist/batchTracker.d.ts +1 -2
  3. package/dist/batchTracker.d.ts.map +1 -1
  4. package/dist/batchTracker.js.map +1 -1
  5. package/dist/blobManager.d.ts +45 -34
  6. package/dist/blobManager.d.ts.map +1 -1
  7. package/dist/blobManager.js +135 -102
  8. package/dist/blobManager.js.map +1 -1
  9. package/dist/containerRuntime.d.ts +54 -8
  10. package/dist/containerRuntime.d.ts.map +1 -1
  11. package/dist/containerRuntime.js +143 -72
  12. package/dist/containerRuntime.js.map +1 -1
  13. package/dist/dataStoreContext.d.ts +1 -1
  14. package/dist/dataStoreContext.d.ts.map +1 -1
  15. package/dist/dataStoreContext.js +6 -8
  16. package/dist/dataStoreContext.js.map +1 -1
  17. package/dist/dataStores.d.ts +12 -9
  18. package/dist/dataStores.d.ts.map +1 -1
  19. package/dist/dataStores.js +41 -35
  20. package/dist/dataStores.js.map +1 -1
  21. package/dist/garbageCollection.d.ts +41 -20
  22. package/dist/garbageCollection.d.ts.map +1 -1
  23. package/dist/garbageCollection.js +205 -150
  24. package/dist/garbageCollection.js.map +1 -1
  25. package/dist/garbageCollectionConstants.d.ts +7 -3
  26. package/dist/garbageCollectionConstants.d.ts.map +1 -1
  27. package/dist/garbageCollectionConstants.js +10 -8
  28. package/dist/garbageCollectionConstants.js.map +1 -1
  29. package/dist/garbageCollectionTombstoneUtils.d.ts +14 -0
  30. package/dist/garbageCollectionTombstoneUtils.d.ts.map +1 -0
  31. package/dist/garbageCollectionTombstoneUtils.js +23 -0
  32. package/dist/garbageCollectionTombstoneUtils.js.map +1 -0
  33. package/dist/index.d.ts +1 -2
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +3 -5
  36. package/dist/index.js.map +1 -1
  37. package/dist/opLifecycle/batchManager.d.ts +13 -1
  38. package/dist/opLifecycle/batchManager.d.ts.map +1 -1
  39. package/dist/opLifecycle/batchManager.js +35 -1
  40. package/dist/opLifecycle/batchManager.js.map +1 -1
  41. package/dist/opLifecycle/definitions.d.ts +25 -1
  42. package/dist/opLifecycle/definitions.d.ts.map +1 -1
  43. package/dist/opLifecycle/definitions.js.map +1 -1
  44. package/dist/opLifecycle/index.d.ts +2 -2
  45. package/dist/opLifecycle/index.d.ts.map +1 -1
  46. package/dist/opLifecycle/index.js +2 -1
  47. package/dist/opLifecycle/index.js.map +1 -1
  48. package/dist/opLifecycle/opCompressor.d.ts +1 -1
  49. package/dist/opLifecycle/opCompressor.d.ts.map +1 -1
  50. package/dist/opLifecycle/opCompressor.js +24 -10
  51. package/dist/opLifecycle/opCompressor.js.map +1 -1
  52. package/dist/opLifecycle/opDecompressor.d.ts +2 -1
  53. package/dist/opLifecycle/opDecompressor.d.ts.map +1 -1
  54. package/dist/opLifecycle/opDecompressor.js +30 -17
  55. package/dist/opLifecycle/opDecompressor.js.map +1 -1
  56. package/dist/opLifecycle/opSplitter.d.ts +34 -2
  57. package/dist/opLifecycle/opSplitter.d.ts.map +1 -1
  58. package/dist/opLifecycle/opSplitter.js +114 -5
  59. package/dist/opLifecycle/opSplitter.js.map +1 -1
  60. package/dist/opLifecycle/outbox.d.ts +5 -0
  61. package/dist/opLifecycle/outbox.d.ts.map +1 -1
  62. package/dist/opLifecycle/outbox.js +24 -14
  63. package/dist/opLifecycle/outbox.js.map +1 -1
  64. package/dist/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
  65. package/dist/opLifecycle/remoteMessageProcessor.js +17 -2
  66. package/dist/opLifecycle/remoteMessageProcessor.js.map +1 -1
  67. package/dist/packageVersion.d.ts +1 -1
  68. package/dist/packageVersion.js +1 -1
  69. package/dist/packageVersion.js.map +1 -1
  70. package/dist/runningSummarizer.d.ts.map +1 -1
  71. package/dist/runningSummarizer.js +0 -1
  72. package/dist/runningSummarizer.js.map +1 -1
  73. package/dist/scheduleManager.d.ts +0 -1
  74. package/dist/scheduleManager.d.ts.map +1 -1
  75. package/dist/scheduleManager.js +9 -20
  76. package/dist/scheduleManager.js.map +1 -1
  77. package/dist/summarizer.d.ts +0 -1
  78. package/dist/summarizer.d.ts.map +1 -1
  79. package/dist/summarizer.js +2 -1
  80. package/dist/summarizer.js.map +1 -1
  81. package/dist/summarizerTypes.d.ts +1 -0
  82. package/dist/summarizerTypes.d.ts.map +1 -1
  83. package/dist/summarizerTypes.js.map +1 -1
  84. package/dist/summaryFormat.d.ts.map +1 -1
  85. package/dist/summaryFormat.js +1 -2
  86. package/dist/summaryFormat.js.map +1 -1
  87. package/lib/batchTracker.d.ts +1 -2
  88. package/lib/batchTracker.d.ts.map +1 -1
  89. package/lib/batchTracker.js.map +1 -1
  90. package/lib/blobManager.d.ts +45 -34
  91. package/lib/blobManager.d.ts.map +1 -1
  92. package/lib/blobManager.js +137 -104
  93. package/lib/blobManager.js.map +1 -1
  94. package/lib/containerRuntime.d.ts +54 -8
  95. package/lib/containerRuntime.d.ts.map +1 -1
  96. package/lib/containerRuntime.js +140 -69
  97. package/lib/containerRuntime.js.map +1 -1
  98. package/lib/dataStoreContext.d.ts +1 -1
  99. package/lib/dataStoreContext.d.ts.map +1 -1
  100. package/lib/dataStoreContext.js +7 -9
  101. package/lib/dataStoreContext.js.map +1 -1
  102. package/lib/dataStores.d.ts +12 -9
  103. package/lib/dataStores.d.ts.map +1 -1
  104. package/lib/dataStores.js +44 -38
  105. package/lib/dataStores.js.map +1 -1
  106. package/lib/garbageCollection.d.ts +41 -20
  107. package/lib/garbageCollection.d.ts.map +1 -1
  108. package/lib/garbageCollection.js +201 -146
  109. package/lib/garbageCollection.js.map +1 -1
  110. package/lib/garbageCollectionConstants.d.ts +7 -3
  111. package/lib/garbageCollectionConstants.d.ts.map +1 -1
  112. package/lib/garbageCollectionConstants.js +9 -7
  113. package/lib/garbageCollectionConstants.js.map +1 -1
  114. package/lib/garbageCollectionTombstoneUtils.d.ts +14 -0
  115. package/lib/garbageCollectionTombstoneUtils.d.ts.map +1 -0
  116. package/lib/garbageCollectionTombstoneUtils.js +19 -0
  117. package/lib/garbageCollectionTombstoneUtils.js.map +1 -0
  118. package/lib/index.d.ts +1 -2
  119. package/lib/index.d.ts.map +1 -1
  120. package/lib/index.js +1 -2
  121. package/lib/index.js.map +1 -1
  122. package/lib/opLifecycle/batchManager.d.ts +13 -1
  123. package/lib/opLifecycle/batchManager.d.ts.map +1 -1
  124. package/lib/opLifecycle/batchManager.js +35 -1
  125. package/lib/opLifecycle/batchManager.js.map +1 -1
  126. package/lib/opLifecycle/definitions.d.ts +25 -1
  127. package/lib/opLifecycle/definitions.d.ts.map +1 -1
  128. package/lib/opLifecycle/definitions.js.map +1 -1
  129. package/lib/opLifecycle/index.d.ts +2 -2
  130. package/lib/opLifecycle/index.d.ts.map +1 -1
  131. package/lib/opLifecycle/index.js +1 -1
  132. package/lib/opLifecycle/index.js.map +1 -1
  133. package/lib/opLifecycle/opCompressor.d.ts +1 -1
  134. package/lib/opLifecycle/opCompressor.d.ts.map +1 -1
  135. package/lib/opLifecycle/opCompressor.js +24 -10
  136. package/lib/opLifecycle/opCompressor.js.map +1 -1
  137. package/lib/opLifecycle/opDecompressor.d.ts +2 -1
  138. package/lib/opLifecycle/opDecompressor.d.ts.map +1 -1
  139. package/lib/opLifecycle/opDecompressor.js +30 -17
  140. package/lib/opLifecycle/opDecompressor.js.map +1 -1
  141. package/lib/opLifecycle/opSplitter.d.ts +34 -2
  142. package/lib/opLifecycle/opSplitter.d.ts.map +1 -1
  143. package/lib/opLifecycle/opSplitter.js +112 -4
  144. package/lib/opLifecycle/opSplitter.js.map +1 -1
  145. package/lib/opLifecycle/outbox.d.ts +5 -0
  146. package/lib/opLifecycle/outbox.d.ts.map +1 -1
  147. package/lib/opLifecycle/outbox.js +24 -14
  148. package/lib/opLifecycle/outbox.js.map +1 -1
  149. package/lib/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
  150. package/lib/opLifecycle/remoteMessageProcessor.js +17 -2
  151. package/lib/opLifecycle/remoteMessageProcessor.js.map +1 -1
  152. package/lib/packageVersion.d.ts +1 -1
  153. package/lib/packageVersion.js +1 -1
  154. package/lib/packageVersion.js.map +1 -1
  155. package/lib/runningSummarizer.d.ts.map +1 -1
  156. package/lib/runningSummarizer.js +0 -1
  157. package/lib/runningSummarizer.js.map +1 -1
  158. package/lib/scheduleManager.d.ts +0 -1
  159. package/lib/scheduleManager.d.ts.map +1 -1
  160. package/lib/scheduleManager.js +9 -20
  161. package/lib/scheduleManager.js.map +1 -1
  162. package/lib/summarizer.d.ts +0 -1
  163. package/lib/summarizer.d.ts.map +1 -1
  164. package/lib/summarizer.js +2 -1
  165. package/lib/summarizer.js.map +1 -1
  166. package/lib/summarizerTypes.d.ts +1 -0
  167. package/lib/summarizerTypes.d.ts.map +1 -1
  168. package/lib/summarizerTypes.js.map +1 -1
  169. package/lib/summaryFormat.d.ts.map +1 -1
  170. package/lib/summaryFormat.js +1 -2
  171. package/lib/summaryFormat.js.map +1 -1
  172. package/package.json +20 -19
  173. package/src/batchTracker.ts +1 -1
  174. package/src/blobManager.ts +159 -111
  175. package/src/containerRuntime.ts +202 -73
  176. package/src/dataStoreContext.ts +15 -16
  177. package/src/dataStores.ts +61 -45
  178. package/src/garbageCollection.ts +258 -183
  179. package/src/garbageCollectionConstants.ts +10 -7
  180. package/src/garbageCollectionTombstoneUtils.ts +28 -0
  181. package/src/index.ts +2 -5
  182. package/src/opLifecycle/batchManager.ts +59 -1
  183. package/src/opLifecycle/definitions.ts +27 -1
  184. package/src/opLifecycle/index.ts +2 -1
  185. package/src/opLifecycle/opCompressor.ts +29 -12
  186. package/src/opLifecycle/opDecompressor.ts +39 -18
  187. package/src/opLifecycle/opSplitter.ts +141 -7
  188. package/src/opLifecycle/outbox.ts +32 -16
  189. package/src/opLifecycle/remoteMessageProcessor.ts +19 -3
  190. package/src/packageVersion.ts +1 -1
  191. package/src/runningSummarizer.ts +0 -1
  192. package/src/scheduleManager.ts +19 -30
  193. package/src/summarizer.ts +1 -1
  194. package/src/summarizerTypes.ts +1 -0
  195. package/src/summaryFormat.ts +1 -2
@@ -24,7 +24,8 @@ import {
24
24
  } from "@fluidframework/runtime-definitions";
25
25
  import { Throttler, formExponentialFn, IThrottler } from "./throttler";
26
26
  import { summarizerClientType } from "./summarizerClientElection";
27
- import { throwOnTombstoneUsageKey } from "./garbageCollectionConstants";
27
+ import { throwOnTombstoneLoadKey } from "./garbageCollectionConstants";
28
+ import { sendGCTombstoneEvent } from "./garbageCollectionTombstoneUtils";
28
29
 
29
30
  /**
30
31
  * This class represents blob (long string)
@@ -110,16 +111,21 @@ interface PendingBlob {
110
111
 
111
112
  export interface IPendingBlobs { [id: string]: { blob: string; }; }
112
113
 
113
- export class BlobManager {
114
+ export interface IBlobManagerEvents {
115
+ (event: "noPendingBlobs", listener: () => void);
116
+ }
117
+
118
+ export class BlobManager extends TypedEventEmitter<IBlobManagerEvents> {
114
119
  public static readonly basePath = "_blobs";
115
120
  private static readonly redirectTableBlobName = ".redirectTable";
116
121
  private readonly mc: MonitoringContext;
117
122
 
118
123
  /**
119
- * Map of local (offline/detached) IDs to storage IDs. Contains identity entries
120
- * (id id) for storage IDs, so all requested IDs should be a key in this map.
121
- * Blobs created while the container is detached are stored in IDetachedBlobStorage
122
- * which gives local IDs; the storage IDs are filled in at attach time.
124
+ * Map of local IDs to storage IDs. Contains identity entries (id → id) for storage IDs. All requested IDs should
125
+ * be a key in this map. Blobs created while the container is detached are stored in IDetachedBlobStorage which
126
+ * gives local IDs; the storage IDs are filled in at attach time.
127
+ * Note: It contains mappings from all clients, i.e., from remote clients as well. local ID comes from the client
128
+ * that uploaded the blob but its mapping to storage ID is needed in all clients in order to retrieve the blob.
123
129
  */
124
130
  private readonly redirectTable: Map<string, string | undefined>;
125
131
 
@@ -131,8 +137,9 @@ export class BlobManager {
131
137
  private readonly pendingBlobs: Map<string, PendingBlob> = new Map();
132
138
 
133
139
  /**
134
- * Track ops in flight for online flow. Used to avoid searching pendingBlobs since BlobAttach ops
135
- * don't include local ID in online flow.
140
+ * Track ops in flight for online flow. This is used for optimizations where if we receive an ack for a storage ID,
141
+ * we can resolve all pending blobs with the same storage ID even though they may have different local IDs. That's
142
+ * because we know that the server will not delete the blob corresponding to that storage ID.
136
143
  */
137
144
  private readonly opsInFlight: Map<string, string[]> = new Map();
138
145
 
@@ -144,9 +151,9 @@ export class BlobManager {
144
151
  ));
145
152
 
146
153
  /** If true, throw an error when a tombstone attachment blob is retrieved. */
147
- private readonly throwOnTombstoneUsage: boolean;
154
+ private readonly throwOnTombstoneLoad: boolean;
148
155
  /**
149
- * This stores ides of tombstoned blobs.
156
+ * This stores IDs of tombstoned blobs.
150
157
  * Tombstone is a temporary feature that imitates a blob getting swept by garbage collection.
151
158
  */
152
159
  private readonly tombstonedBlobs: Set<string> = new Set();
@@ -156,26 +163,28 @@ export class BlobManager {
156
163
  snapshot: IBlobManagerLoadInfo,
157
164
  private readonly getStorage: () => IDocumentStorageService,
158
165
  /**
159
- * Submit a BlobAttach op. When a blob is uploaded, there is a short grace period before which
160
- * the blob is deleted. The BlobAttach op notifies the server that blob is in use. The server
161
- * will then not delete the blob as long as it is listed as referenced in future summaries.
162
- * The summarizing client will know to include the storage ID in the summary when it sees the op.
166
+ * Submit a BlobAttach op. When a blob is uploaded, there is a short grace period before which the blob is
167
+ * deleted. The BlobAttach op notifies the server that blob is in use. The server will then not delete the
168
+ * the blob as long as it is listed as referenced in future summaries. The summarizing client will know to
169
+ * include the storage ID in the summary when it sees the op.
163
170
  *
164
- * The op may also include a local ID to inform all clients of the relation to the storage
165
- * ID, without knowledge of which they cannot request the blob from storage. This is also
166
- * included in the redirect table in the summary.
171
+ * The op will also include a local ID to inform all clients of the relation to the storage ID, without
172
+ * knowledge of which they cannot request the blob from storage. It's important that this op is sequenced
173
+ * before any ops that reference the local ID, otherwise, an invalid handle could be added to the document.
167
174
  */
168
- private readonly sendBlobAttachOp: (storageId?: string, localId?: string) => void,
175
+ private readonly sendBlobAttachOp: (localId: string, storageId?: string) => void,
169
176
  // To be called when a blob node is requested. blobPath is the path of the blob's node in GC's graph. It's
170
177
  // of the format `/<BlobManager.basePath>/<blobId>`.
171
- private readonly gcNodeUpdated: (blobPath: string) => void,
178
+ private readonly blobRequested: (blobPath: string) => void,
179
+ private readonly addedBlobReference: (fromNodePath: string, toNodePath: string) => void,
172
180
  private readonly runtime: IBlobManagerRuntime,
173
181
  stashedBlobs: IPendingBlobs = {},
174
182
  ) {
183
+ super();
175
184
  this.mc = loggerToMonitoringContext(ChildLogger.create(this.runtime.logger, "BlobManager"));
176
185
  // Read the feature flag that tells whether to throw when a tombstone blob is requested.
177
- this.throwOnTombstoneUsage =
178
- this.mc.config.getBoolean(throwOnTombstoneUsageKey) === true &&
186
+ this.throwOnTombstoneLoad =
187
+ this.mc.config.getBoolean(throwOnTombstoneLoadKey) === true &&
179
188
  this.runtime.clientDetails.type !== summarizerClientType;
180
189
 
181
190
  this.runtime.on("disconnected", () => this.onDisconnected());
@@ -202,6 +211,11 @@ export class BlobManager {
202
211
  return this.pendingOfflineUploads.length > 0;
203
212
  }
204
213
 
214
+ public get hasPendingBlobs(): boolean {
215
+ return (this.runtime.attachState !== AttachState.Attached && this.redirectTable.size > 0)
216
+ || this.pendingBlobs.size > 0;
217
+ }
218
+
205
219
  /**
206
220
  * Upload blobs added while offline. This must be completed before connecting and resubmitting ops.
207
221
  */
@@ -260,12 +274,18 @@ export class BlobManager {
260
274
  public async getBlob(blobId: string): Promise<ArrayBufferLike> {
261
275
  const request = { url: blobId };
262
276
  if (this.tombstonedBlobs.has(blobId) ) {
263
- const error = responseToException(createResponseError(404, "Blob removed by gc", request), request);
264
- this.mc.logger.sendErrorEvent({
265
- eventName: "GC_Tombstone_Blob_Requested",
266
- url: request.url,
267
- }, error);
268
- if (this.throwOnTombstoneUsage) {
277
+ const error = responseToException(createResponseError(404, "Blob was deleted", request), request);
278
+ sendGCTombstoneEvent(
279
+ this.mc,
280
+ {
281
+ eventName: "GC_Tombstone_Blob_Requested",
282
+ category: this.throwOnTombstoneLoad ? "error" : "generic",
283
+ isSummarizerClient: this.runtime.clientDetails.type === summarizerClientType,
284
+ },
285
+ [BlobManager.basePath],
286
+ error,
287
+ );
288
+ if (this.throwOnTombstoneLoad) {
269
289
  throw error;
270
290
  }
271
291
  }
@@ -274,7 +294,7 @@ export class BlobManager {
274
294
  if (pending) {
275
295
  return pending.blob;
276
296
  }
277
- let storageId;
297
+ let storageId: string;
278
298
  if (this.runtime.attachState === AttachState.Detached) {
279
299
  assert(this.redirectTable.has(blobId), 0x383 /* requesting unknown blobs */);
280
300
 
@@ -282,12 +302,13 @@ export class BlobManager {
282
302
  // The 'IDocumentStorageService.readBlob()' call below will retrieve these via localId.
283
303
  storageId = blobId;
284
304
  } else {
285
- storageId = this.redirectTable.get(blobId);
286
- assert(!!storageId, 0x11f /* "requesting unknown blobs" */);
305
+ const attachedStorageId = this.redirectTable.get(blobId);
306
+ assert(!!attachedStorageId, 0x11f /* "requesting unknown blobs" */);
307
+ storageId = attachedStorageId;
287
308
  }
288
309
 
289
- // When a GC-able (not pending) blob is retrieved, let runtime know that the corresponding GC node got updated.
290
- this.gcNodeUpdated(this.getBlobGCNodePath(blobId));
310
+ // Let runtime know that the corresponding GC node was requested.
311
+ this.blobRequested(this.getBlobGCNodePath(blobId));
291
312
 
292
313
  return PerformanceEvent.timedExecAsync(
293
314
  this.mc.logger,
@@ -313,7 +334,7 @@ export class BlobManager {
313
334
  // Blobs created while the container is detached are stored in IDetachedBlobStorage.
314
335
  // The 'IDocumentStorageService.createBlob()' call below will respond with a localId.
315
336
  const response = await this.getStorage().createBlob(blob);
316
- this.redirectTable.set(response.id, undefined);
337
+ this.setRedirection(response.id, undefined);
317
338
  return this.getBlobHandle(response.id);
318
339
  }
319
340
 
@@ -329,8 +350,8 @@ export class BlobManager {
329
350
  assert(this.runtime.attachState === AttachState.Attached,
330
351
  0x385 /* For clarity and paranoid defense against adding future attachment states */);
331
352
 
332
- // Create a local ID for each blob. This is used to support blobs if/when the client goes
333
- // offline since we don't have the ID from storage yet. If online flow succeeds this won't be used.
353
+ // Create a local ID for the blob. After uploading it to storage and before returning it, a local ID to
354
+ // storage ID mapping is created.
334
355
  const localId = uuid();
335
356
  const pendingEntry: PendingBlob = {
336
357
  blob,
@@ -355,6 +376,28 @@ export class BlobManager {
355
376
  );
356
377
  }
357
378
 
379
+ /**
380
+ * Set up a mapping in the redirect table from fromId to toId. Also, notify the runtime that a reference is added
381
+ * which is required for GC.
382
+ */
383
+ private setRedirection(fromId: string, toId: string | undefined) {
384
+ this.redirectTable.set(fromId, toId);
385
+ // Notify runtime of a reference added if toId is not undefined. It can be undefined when a blob is uploaded in
386
+ // detached mode. In this case, the entry will be updated when the blob is updated.
387
+ if (toId !== undefined) {
388
+ this.addedBlobReference(this.getBlobGCNodePath(fromId), this.getBlobGCNodePath(toId));
389
+ }
390
+ }
391
+
392
+ private deleteAndEmitsIfEmpty(id: string) {
393
+ if (this.pendingBlobs.has(id)) {
394
+ this.pendingBlobs.delete(id);
395
+ if (!this.hasPendingBlobs) {
396
+ this.emit("noPendingBlobs");
397
+ }
398
+ }
399
+ }
400
+
358
401
  private onUploadResolve(localId: string, response: ICreateBlobResponse) {
359
402
  const entry = this.pendingBlobs.get(localId);
360
403
  assert(entry?.status === PendingBlobStatus.OnlinePendingUpload ||
@@ -363,16 +406,23 @@ export class BlobManager {
363
406
  entry.storageId = response.id;
364
407
  if (this.runtime.connected) {
365
408
  if (entry.status === PendingBlobStatus.OnlinePendingUpload) {
409
+ // Send a blob attach op. This serves two purposes:
410
+ // 1. If its a new blob, i.e., it isn't de-duped, the server will keep the blob alive if it sees this op
411
+ // until its storage ID is added to the next summary.
412
+ // 2. It will create a local ID to storage ID mapping in all clients which is needed to retrieve the
413
+ // blob from the server via the storage ID.
414
+ this.sendBlobAttachOp(localId, response.id);
366
415
  if (this.storageIds.has(response.id)) {
367
- // Storage may dedupe blobs and give us an ID we already know
368
- // no need to submit BlobAttach op in this case
369
- entry.handleP.resolve(this.getBlobHandle(response.id));
370
- this.pendingBlobs.delete(localId);
416
+ // The blob is de-duped. Set up a local ID to storage ID mapping and return the blob. Since this is
417
+ // an existing blob, we don't have to wait for the op to be ack'd since this step has already
418
+ // happened before and so, the server won't delete it.
419
+ this.setRedirection(localId, response.id);
420
+ entry.handleP.resolve(this.getBlobHandle(localId));
421
+ this.deleteAndEmitsIfEmpty(localId);
371
422
  } else {
372
- // Check for still-pending duplicates too; if an op is already in flight we can wait for that one
373
- if (!this.opsInFlight.has(response.id)) {
374
- this.sendBlobAttachOp(response.id);
375
- }
423
+ // If there is already an op for this storage ID, append the local ID to the list. Once any op for
424
+ // this storage ID is ack'd, all pending blobs for it can be resolved since the op will keep the
425
+ // blob alive in storage.
376
426
  this.opsInFlight.set(response.id, (this.opsInFlight.get(response.id) ?? []).concat(localId));
377
427
  entry.status = PendingBlobStatus.OnlinePendingOp;
378
428
  }
@@ -414,16 +464,21 @@ export class BlobManager {
414
464
  assert([PendingBlobStatus.OnlinePendingUpload, PendingBlobStatus.OnlinePendingOp].includes(entry.status),
415
465
  0x38a /* Blob must be in online flow to transition to offline flow */);
416
466
 
467
+ /**
468
+ * If we haven't already submitted a BlobAttach op for this entry, send it before returning the blob handle.
469
+ * This will make sure that the BlobAttach op is sequenced prior to any ops referencing the handle. Otherwise,
470
+ * an invalid handle could be added to the document.
471
+ * storageId may be undefined but since we are not connected we will have a chance to add it when reSubmit()
472
+ * is called on reconnection.
473
+ */
474
+ if (entry.status !== PendingBlobStatus.OnlinePendingOp) {
475
+ this.sendBlobAttachOp(localId, entry.storageId);
476
+ }
477
+
417
478
  entry.status = entry.status === PendingBlobStatus.OnlinePendingUpload
418
479
  ? PendingBlobStatus.OfflinePendingUpload
419
480
  : PendingBlobStatus.OfflinePendingOp;
420
481
 
421
- // Submit a BlobAttach op. It's important we submit this op now before returning the blob handle so the
422
- // BlobAttach op is sequenced prior to any ops referencing the handle. Otherwise an invalid handle could be
423
- // added to the document if the ops are not all successfully submitted upon reconnection.
424
- // storageId may be undefined but since we are not connected we will have a chance to add it when reSubmit()
425
- // is called
426
- this.sendBlobAttachOp(entry.storageId, localId);
427
482
  entry.handleP.resolve(this.getBlobHandle(localId));
428
483
  }
429
484
 
@@ -434,33 +489,42 @@ export class BlobManager {
434
489
  */
435
490
  public reSubmit(metadata: Record<string, unknown> | undefined) {
436
491
  assert(!!metadata, 0x38b /* Resubmitted ops must have metadata */);
437
- const { blobId, localId }: { blobId?: string; localId?: string; } = metadata;
492
+ const { localId, blobId }: { localId?: string; blobId?: string } = metadata;
493
+ assert(localId !== undefined, 0x50d /* local ID not available on reSubmit */);
438
494
  if (!blobId) {
439
- assert(!!localId, 0x38c /* Submitted BlobAttach ops must have a blobId or localId */);
440
495
  // We submitted this op while offline. The blob should have been uploaded by now.
441
496
  const pendingEntry = this.pendingBlobs.get(localId);
442
497
  assert(pendingEntry?.status === PendingBlobStatus.OfflinePendingOp &&
443
498
  !!pendingEntry?.storageId, 0x38d /* blob must be uploaded before resubmitting BlobAttach op */);
444
- return this.sendBlobAttachOp(pendingEntry.storageId, localId);
499
+ return this.sendBlobAttachOp(localId, pendingEntry.storageId);
445
500
  }
446
- return this.sendBlobAttachOp(blobId, localId);
501
+ return this.sendBlobAttachOp(localId, blobId);
447
502
  }
448
503
 
449
504
  public processBlobAttachOp(message: ISequencedDocumentMessage, local: boolean) {
450
- assert(message?.metadata?.blobId, 0x12a /* "Missing blob id on metadata" */);
451
- if (message.metadata.localId !== undefined) {
452
- this.redirectTable.set(message.metadata.localId, message.metadata.blobId);
505
+ const localId = message.metadata?.localId;
506
+ const blobId = message.metadata?.blobId;
507
+ assert(blobId !== undefined, 0x12a /* "Missing blob id on metadata" */);
508
+
509
+ // Set up a mapping from local ID to storage ID. This is crucial since without this the blob cannot be
510
+ // requested from the server.
511
+ // Note: The check for undefined is needed for back-compat when localId was not part of the BlobAttach op that
512
+ // was sent when online.
513
+ if (localId !== undefined) {
514
+ this.setRedirection(localId, blobId);
453
515
  }
454
516
  // set identity (id -> id) entry
455
- this.redirectTable.set(message.metadata.blobId, message.metadata.blobId);
517
+ this.setRedirection(blobId, blobId);
456
518
 
457
519
  if (local) {
458
- if (message.metadata.localId === undefined) {
459
- // Since there is no local ID, we know this op was submitted while online.
460
- const waitingBlobs = this.opsInFlight.get(message.metadata.blobId);
461
- assert(!!waitingBlobs, 0x38e /* local online BlobAttach op with no pending blob */);
462
- waitingBlobs.forEach((localId) => {
463
- const pendingBlobEntry = this.pendingBlobs.get(localId);
520
+ assert(localId !== undefined, 0x50e /* local ID not present in blob attach message */);
521
+ const waitingBlobs = this.opsInFlight.get(blobId);
522
+ if (waitingBlobs !== undefined) {
523
+ // For each op corresponding to this storage ID that we are waiting for, resolve the pending blob.
524
+ // This is safe because the server will keep the blob alive and the op containing the local ID to
525
+ // storage ID is already in flight and any op containing this local ID will be sequenced after that.
526
+ waitingBlobs.forEach((pendingLocalId) => {
527
+ const pendingBlobEntry = this.pendingBlobs.get(pendingLocalId);
464
528
  assert(
465
529
  pendingBlobEntry !== undefined,
466
530
  0x38f, /* local online BlobAttach op with no pending blob entry */
@@ -468,16 +532,15 @@ export class BlobManager {
468
532
 
469
533
  // It's possible we transitioned to offline flow while waiting for this op.
470
534
  if (pendingBlobEntry.status === PendingBlobStatus.OnlinePendingOp) {
471
- pendingBlobEntry.handleP.resolve(this.getBlobHandle(message.metadata.blobId));
472
- this.pendingBlobs.delete(localId);
535
+ this.setRedirection(pendingLocalId, blobId);
536
+ pendingBlobEntry.handleP.resolve(this.getBlobHandle(pendingLocalId));
537
+ this.deleteAndEmitsIfEmpty(pendingLocalId);
473
538
  }
474
539
  });
475
- } else {
476
- // Each local ID is unique; get the pending blob entry and delete it
477
- assert(this.pendingBlobs.get(message.metadata.localId)?.status === PendingBlobStatus.OfflinePendingOp,
478
- 0x1f8 /* "local BlobAttach op with no pending blob" */);
479
- this.pendingBlobs.delete(message.metadata.localId);
540
+ this.opsInFlight.delete(blobId);
480
541
  }
542
+ // For blobs that were transitioned to offline flow while waiting for this op, the entry should be deleted.
543
+ this.deleteAndEmitsIfEmpty(localId);
481
544
  }
482
545
  }
483
546
 
@@ -531,75 +594,60 @@ export class BlobManager {
531
594
  */
532
595
  public getGCData(fullGC: boolean = false): IGarbageCollectionData {
533
596
  const gcData: IGarbageCollectionData = { gcNodes: {} };
534
- /**
535
- * The node path is of the format `/_blobs/blobId`. This path must match the path of the blob handle returned
536
- * by the createBlob API because blobs are marked referenced by storing these handles in a referenced DDS.
537
- */
538
- this.storageIds.forEach((blobId: string) => {
539
- gcData.gcNodes[this.getBlobGCNodePath(blobId)] = [];
540
- });
541
-
542
- // For some blobs, the handle returned on creation is based off of the localId. So, these
543
- // nodes can be referenced by storing the localId handle. When that happens, the corresponding storageId node
544
- // must also be marked referenced. So, we add a route from the localId node to the storageId node.
545
- // Note that because of de-duping, there can be multiple localIds that all redirect to the same storageId or
546
- // a blob may be referenced via its storageId handle.
547
597
  for (const [localId, storageId] of this.redirectTable) {
548
598
  assert(!!storageId, 0x390 /* Must be attached to get GC data */);
549
- // Add node for the localId and add a route to the storageId node. The storageId node will have been
550
- // added above when adding nodes for this.blobIds.
551
599
  gcData.gcNodes[this.getBlobGCNodePath(localId)] = [this.getBlobGCNodePath(storageId)];
552
600
  }
553
-
554
601
  return gcData;
555
602
  }
556
603
 
557
604
  /**
558
- * This is called to update blobs whose routes are used. The used blobs are removed from the tombstone list.
559
- * @param usedRoutes - The routes of the blob nodes that are used.
605
+ * This is called to update blobs whose routes are unused. The unused blobs are deleted.
606
+ * @param unusedRoutes - The routes of the blob nodes that are unused.
560
607
  */
561
- public updateUsedRoutes(usedRoutes: string[]) {
608
+ public updateUnusedRoutes(unusedRoutes: string[]): void {
562
609
  // The routes or blob node paths are in the same format as returned in getGCData -
563
610
  // `/<BlobManager.basePath>/<blobId>`.
564
- for (const route of usedRoutes) {
611
+ for (const route of unusedRoutes) {
565
612
  const pathParts = route.split("/");
566
613
  assert(
567
614
  pathParts.length === 3 && pathParts[1] === BlobManager.basePath,
568
- 0x4bc /* Invalid blob node id in used routes. */,
615
+ 0x2d5 /* "Invalid blob node id in unused routes." */,
569
616
  );
570
617
  const blobId = pathParts[2];
571
- // Un-tombstone the blob if it was marked tombstone.
572
- this.tombstonedBlobs.delete(blobId);
618
+ this.redirectTable.delete(blobId);
573
619
  }
574
620
  }
575
621
 
576
622
  /**
577
- * This is called to update blobs whose routes are unused. The unused blobs are either deleted or marked as
578
- * tombstones.
579
- * @param unusedRoutes - The routes of the blob nodes that are unused.
580
- * @param tombstone - if true, the objects corresponding to unused routes are marked tombstones. Otherwise, they
581
- * are deleted.
623
+ * This is called to update blobs whose routes are tombstones. Tombstoned blobs enable testing scenarios with
624
+ * accessing deleted content without actually deleting content from summaries.
625
+ * @param tombstonedRoutes - The routes of blob nodes that are tombstones.
582
626
  */
583
- public updateUnusedRoutes(unusedRoutes: string[], tombstone: boolean): void {
627
+ public updateTombstonedRoutes(tombstonedRoutes: string[]) {
628
+ const tombstonedBlobsSet: Set<string> = new Set();
584
629
  // The routes or blob node paths are in the same format as returned in getGCData -
585
630
  // `/<BlobManager.basePath>/<blobId>`.
586
- for (const route of unusedRoutes) {
631
+ for (const route of tombstonedRoutes) {
587
632
  const pathParts = route.split("/");
588
633
  assert(
589
634
  pathParts.length === 3 && pathParts[1] === BlobManager.basePath,
590
- 0x2d5 /* "Invalid blob node id in unused routes." */,
635
+ 0x50f /* Invalid blob node id in tombstoned routes. */,
591
636
  );
592
- const blobId = pathParts[2];
637
+ tombstonedBlobsSet.add(pathParts[2]);
638
+ }
593
639
 
594
- if (tombstone) {
595
- // If tombstone is set, add this blob to the tombstone list.
596
- this.tombstonedBlobs.add(blobId);
597
- } else {
598
- // The unused blobId could be a localId. If so, remove it from the redirect table and continue. The
599
- // corresponding storageId may still be used either directly or via other localIds.
600
- this.redirectTable.delete(blobId);
640
+ // Remove blobs from the tombstone list that were tombstoned but aren't anymore as per the tombstoneRoutes.
641
+ for (const blobId of this.tombstonedBlobs) {
642
+ if (!tombstonedBlobsSet.has(blobId)) {
643
+ this.tombstonedBlobs.delete(blobId);
601
644
  }
602
645
  }
646
+
647
+ // Mark blobs that are now tombstoned by adding them to the tombstone list.
648
+ for (const blobId of tombstonedBlobsSet) {
649
+ this.tombstonedBlobs.add(blobId);
650
+ }
603
651
  }
604
652
 
605
653
  public summarize(telemetryContext?: ITelemetryContext): ISummaryTreeWithStats {
@@ -630,9 +678,9 @@ export class BlobManager {
630
678
  0x391 /* Redirect table size must match BlobManager's local ID count */);
631
679
  for (const [localId, storageId] of table) {
632
680
  assert(this.redirectTable.has(localId), 0x254 /* "unrecognized id in redirect table" */);
633
- this.redirectTable.set(localId, storageId);
681
+ this.setRedirection(localId, storageId);
634
682
  // set identity (id -> id) entry
635
- this.redirectTable.set(storageId, storageId);
683
+ this.setRedirection(storageId, storageId);
636
684
  }
637
685
  }
638
686