@fluidframework/container-runtime 2.0.0-internal.2.2.1 → 2.0.0-internal.2.3.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 (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 +44 -33
  6. package/dist/blobManager.d.ts.map +1 -1
  7. package/dist/blobManager.js +130 -97
  8. package/dist/blobManager.js.map +1 -1
  9. package/dist/containerRuntime.d.ts +39 -8
  10. package/dist/containerRuntime.d.ts.map +1 -1
  11. package/dist/containerRuntime.js +117 -61
  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 +4 -3
  16. package/dist/dataStoreContext.js.map +1 -1
  17. package/dist/dataStores.d.ts +9 -6
  18. package/dist/dataStores.d.ts.map +1 -1
  19. package/dist/dataStores.js +30 -24
  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 -151
  24. package/dist/garbageCollection.js.map +1 -1
  25. package/dist/garbageCollectionConstants.d.ts +6 -3
  26. package/dist/garbageCollectionConstants.d.ts.map +1 -1
  27. package/dist/garbageCollectionConstants.js +7 -7
  28. package/dist/garbageCollectionConstants.js.map +1 -1
  29. package/dist/garbageCollectionTombstoneUtils.d.ts +13 -0
  30. package/dist/garbageCollectionTombstoneUtils.d.ts.map +1 -0
  31. package/dist/garbageCollectionTombstoneUtils.js +28 -0
  32. package/dist/garbageCollectionTombstoneUtils.js.map +1 -0
  33. package/dist/index.d.ts +0 -1
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +1 -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 +44 -33
  91. package/lib/blobManager.d.ts.map +1 -1
  92. package/lib/blobManager.js +131 -98
  93. package/lib/blobManager.js.map +1 -1
  94. package/lib/containerRuntime.d.ts +39 -8
  95. package/lib/containerRuntime.d.ts.map +1 -1
  96. package/lib/containerRuntime.js +115 -59
  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 +5 -4
  101. package/lib/dataStoreContext.js.map +1 -1
  102. package/lib/dataStores.d.ts +9 -6
  103. package/lib/dataStores.d.ts.map +1 -1
  104. package/lib/dataStores.js +32 -26
  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 -147
  109. package/lib/garbageCollection.js.map +1 -1
  110. package/lib/garbageCollectionConstants.d.ts +6 -3
  111. package/lib/garbageCollectionConstants.d.ts.map +1 -1
  112. package/lib/garbageCollectionConstants.js +6 -6
  113. package/lib/garbageCollectionConstants.js.map +1 -1
  114. package/lib/garbageCollectionTombstoneUtils.d.ts +13 -0
  115. package/lib/garbageCollectionTombstoneUtils.d.ts.map +1 -0
  116. package/lib/garbageCollectionTombstoneUtils.js +24 -0
  117. package/lib/garbageCollectionTombstoneUtils.js.map +1 -0
  118. package/lib/index.d.ts +0 -1
  119. package/lib/index.d.ts.map +1 -1
  120. package/lib/index.js +0 -1
  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 +37 -19
  173. package/src/batchTracker.ts +1 -1
  174. package/src/blobManager.ts +146 -103
  175. package/src/containerRuntime.ts +166 -65
  176. package/src/dataStoreContext.ts +5 -5
  177. package/src/dataStores.ts +40 -30
  178. package/src/garbageCollection.ts +254 -183
  179. package/src/garbageCollectionConstants.ts +7 -6
  180. package/src/garbageCollectionTombstoneUtils.ts +31 -0
  181. package/src/index.ts +0 -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
@@ -25,6 +25,7 @@ import {
25
25
  import { Throttler, formExponentialFn, IThrottler } from "./throttler";
26
26
  import { summarizerClientType } from "./summarizerClientElection";
27
27
  import { throwOnTombstoneUsageKey } 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
 
@@ -146,7 +153,7 @@ export class BlobManager {
146
153
  /** If true, throw an error when a tombstone attachment blob is retrieved. */
147
154
  private readonly throwOnTombstoneUsage: 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,22 +163,24 @@ 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
186
  this.throwOnTombstoneUsage =
@@ -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
  */
@@ -261,10 +275,11 @@ export class BlobManager {
261
275
  const request = { url: blobId };
262
276
  if (this.tombstonedBlobs.has(blobId) ) {
263
277
  const error = responseToException(createResponseError(404, "Blob removed by gc", request), request);
264
- this.mc.logger.sendErrorEvent({
278
+ const event = {
265
279
  eventName: "GC_Tombstone_Blob_Requested",
266
280
  url: request.url,
267
- }, error);
281
+ };
282
+ sendGCTombstoneEvent(this.mc, event, this.runtime.clientDetails.type === summarizerClientType, [BlobManager.basePath], error);
268
283
  if (this.throwOnTombstoneUsage) {
269
284
  throw error;
270
285
  }
@@ -274,7 +289,7 @@ export class BlobManager {
274
289
  if (pending) {
275
290
  return pending.blob;
276
291
  }
277
- let storageId;
292
+ let storageId: string;
278
293
  if (this.runtime.attachState === AttachState.Detached) {
279
294
  assert(this.redirectTable.has(blobId), 0x383 /* requesting unknown blobs */);
280
295
 
@@ -282,12 +297,13 @@ export class BlobManager {
282
297
  // The 'IDocumentStorageService.readBlob()' call below will retrieve these via localId.
283
298
  storageId = blobId;
284
299
  } else {
285
- storageId = this.redirectTable.get(blobId);
286
- assert(!!storageId, 0x11f /* "requesting unknown blobs" */);
300
+ const attachedStorageId = this.redirectTable.get(blobId);
301
+ assert(!!attachedStorageId, 0x11f /* "requesting unknown blobs" */);
302
+ storageId = attachedStorageId;
287
303
  }
288
304
 
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));
305
+ // Let runtime know that the corresponding GC node was requested.
306
+ this.blobRequested(this.getBlobGCNodePath(blobId));
291
307
 
292
308
  return PerformanceEvent.timedExecAsync(
293
309
  this.mc.logger,
@@ -313,7 +329,7 @@ export class BlobManager {
313
329
  // Blobs created while the container is detached are stored in IDetachedBlobStorage.
314
330
  // The 'IDocumentStorageService.createBlob()' call below will respond with a localId.
315
331
  const response = await this.getStorage().createBlob(blob);
316
- this.redirectTable.set(response.id, undefined);
332
+ this.setRedirection(response.id, undefined);
317
333
  return this.getBlobHandle(response.id);
318
334
  }
319
335
 
@@ -329,8 +345,8 @@ export class BlobManager {
329
345
  assert(this.runtime.attachState === AttachState.Attached,
330
346
  0x385 /* For clarity and paranoid defense against adding future attachment states */);
331
347
 
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.
348
+ // Create a local ID for the blob. After uploading it to storage and before returning it, a local ID to
349
+ // storage ID mapping is created.
334
350
  const localId = uuid();
335
351
  const pendingEntry: PendingBlob = {
336
352
  blob,
@@ -355,6 +371,28 @@ export class BlobManager {
355
371
  );
356
372
  }
357
373
 
374
+ /**
375
+ * Set up a mapping in the redirect table from fromId to toId. Also, notify the runtime that a reference is added
376
+ * which is required for GC.
377
+ */
378
+ private setRedirection(fromId: string, toId: string | undefined) {
379
+ this.redirectTable.set(fromId, toId);
380
+ // Notify runtime of a reference added if toId is not undefined. It can be undefined when a blob is uploaded in
381
+ // detached mode. In this case, the entry will be updated when the blob is updated.
382
+ if (toId !== undefined) {
383
+ this.addedBlobReference(this.getBlobGCNodePath(fromId), this.getBlobGCNodePath(toId));
384
+ }
385
+ }
386
+
387
+ private deleteAndEmitsIfEmpty(id: string) {
388
+ if (this.pendingBlobs.has(id)) {
389
+ this.pendingBlobs.delete(id);
390
+ if (!this.hasPendingBlobs) {
391
+ this.emit("noPendingBlobs");
392
+ }
393
+ }
394
+ }
395
+
358
396
  private onUploadResolve(localId: string, response: ICreateBlobResponse) {
359
397
  const entry = this.pendingBlobs.get(localId);
360
398
  assert(entry?.status === PendingBlobStatus.OnlinePendingUpload ||
@@ -363,16 +401,23 @@ export class BlobManager {
363
401
  entry.storageId = response.id;
364
402
  if (this.runtime.connected) {
365
403
  if (entry.status === PendingBlobStatus.OnlinePendingUpload) {
404
+ // Send a blob attach op. This serves two purposes:
405
+ // 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
406
+ // until its storage ID is added to the next summary.
407
+ // 2. It will create a local ID to storage ID mapping in all clients which is needed to retrieve the
408
+ // blob from the server via the storage ID.
409
+ this.sendBlobAttachOp(localId, response.id);
366
410
  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);
411
+ // The blob is de-duped. Set up a local ID to storage ID mapping and return the blob. Since this is
412
+ // an existing blob, we don't have to wait for the op to be ack'd since this step has already
413
+ // happened before and so, the server won't delete it.
414
+ this.setRedirection(localId, response.id);
415
+ entry.handleP.resolve(this.getBlobHandle(localId));
416
+ this.deleteAndEmitsIfEmpty(localId);
371
417
  } 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
- }
418
+ // If there is already an op for this storage ID, append the local ID to the list. Once any op for
419
+ // this storage ID is ack'd, all pending blobs for it can be resolved since the op will keep the
420
+ // blob alive in storage.
376
421
  this.opsInFlight.set(response.id, (this.opsInFlight.get(response.id) ?? []).concat(localId));
377
422
  entry.status = PendingBlobStatus.OnlinePendingOp;
378
423
  }
@@ -414,16 +459,21 @@ export class BlobManager {
414
459
  assert([PendingBlobStatus.OnlinePendingUpload, PendingBlobStatus.OnlinePendingOp].includes(entry.status),
415
460
  0x38a /* Blob must be in online flow to transition to offline flow */);
416
461
 
462
+ /**
463
+ * If we haven't already submitted a BlobAttach op for this entry, send it before returning the blob handle.
464
+ * This will make sure that the BlobAttach op is sequenced prior to any ops referencing the handle. Otherwise,
465
+ * an invalid handle could be added to the document.
466
+ * storageId may be undefined but since we are not connected we will have a chance to add it when reSubmit()
467
+ * is called on reconnection.
468
+ */
469
+ if (entry.status !== PendingBlobStatus.OnlinePendingOp) {
470
+ this.sendBlobAttachOp(localId, entry.storageId);
471
+ }
472
+
417
473
  entry.status = entry.status === PendingBlobStatus.OnlinePendingUpload
418
474
  ? PendingBlobStatus.OfflinePendingUpload
419
475
  : PendingBlobStatus.OfflinePendingOp;
420
476
 
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
477
  entry.handleP.resolve(this.getBlobHandle(localId));
428
478
  }
429
479
 
@@ -434,33 +484,42 @@ export class BlobManager {
434
484
  */
435
485
  public reSubmit(metadata: Record<string, unknown> | undefined) {
436
486
  assert(!!metadata, 0x38b /* Resubmitted ops must have metadata */);
437
- const { blobId, localId }: { blobId?: string; localId?: string; } = metadata;
487
+ const { localId, blobId }: { localId?: string; blobId?: string } = metadata;
488
+ assert(localId !== undefined, 0x50d /* local ID not available on reSubmit */);
438
489
  if (!blobId) {
439
- assert(!!localId, 0x38c /* Submitted BlobAttach ops must have a blobId or localId */);
440
490
  // We submitted this op while offline. The blob should have been uploaded by now.
441
491
  const pendingEntry = this.pendingBlobs.get(localId);
442
492
  assert(pendingEntry?.status === PendingBlobStatus.OfflinePendingOp &&
443
493
  !!pendingEntry?.storageId, 0x38d /* blob must be uploaded before resubmitting BlobAttach op */);
444
- return this.sendBlobAttachOp(pendingEntry.storageId, localId);
494
+ return this.sendBlobAttachOp(localId, pendingEntry.storageId);
445
495
  }
446
- return this.sendBlobAttachOp(blobId, localId);
496
+ return this.sendBlobAttachOp(localId, blobId);
447
497
  }
448
498
 
449
499
  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);
500
+ const localId = message.metadata?.localId;
501
+ const blobId = message.metadata?.blobId;
502
+ assert(blobId !== undefined, 0x12a /* "Missing blob id on metadata" */);
503
+
504
+ // Set up a mapping from local ID to storage ID. This is crucial since without this the blob cannot be
505
+ // requested from the server.
506
+ // Note: The check for undefined is needed for back-compat when localId was not part of the BlobAttach op that
507
+ // was sent when online.
508
+ if (localId !== undefined) {
509
+ this.setRedirection(localId, blobId);
453
510
  }
454
511
  // set identity (id -> id) entry
455
- this.redirectTable.set(message.metadata.blobId, message.metadata.blobId);
512
+ this.setRedirection(blobId, blobId);
456
513
 
457
514
  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);
515
+ assert(localId !== undefined, 0x50e /* local ID not present in blob attach message */);
516
+ const waitingBlobs = this.opsInFlight.get(blobId);
517
+ if (waitingBlobs !== undefined) {
518
+ // For each op corresponding to this storage ID that we are waiting for, resolve the pending blob.
519
+ // This is safe because the server will keep the blob alive and the op containing the local ID to
520
+ // storage ID is already in flight and any op containing this local ID will be sequenced after that.
521
+ waitingBlobs.forEach((pendingLocalId) => {
522
+ const pendingBlobEntry = this.pendingBlobs.get(pendingLocalId);
464
523
  assert(
465
524
  pendingBlobEntry !== undefined,
466
525
  0x38f, /* local online BlobAttach op with no pending blob entry */
@@ -468,16 +527,15 @@ export class BlobManager {
468
527
 
469
528
  // It's possible we transitioned to offline flow while waiting for this op.
470
529
  if (pendingBlobEntry.status === PendingBlobStatus.OnlinePendingOp) {
471
- pendingBlobEntry.handleP.resolve(this.getBlobHandle(message.metadata.blobId));
472
- this.pendingBlobs.delete(localId);
530
+ this.setRedirection(pendingLocalId, blobId);
531
+ pendingBlobEntry.handleP.resolve(this.getBlobHandle(pendingLocalId));
532
+ this.deleteAndEmitsIfEmpty(pendingLocalId);
473
533
  }
474
534
  });
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);
535
+ this.opsInFlight.delete(blobId);
480
536
  }
537
+ // For blobs that were transitioned to offline flow while waiting for this op, the entry should be deleted.
538
+ this.deleteAndEmitsIfEmpty(localId);
481
539
  }
482
540
  }
483
541
 
@@ -531,75 +589,60 @@ export class BlobManager {
531
589
  */
532
590
  public getGCData(fullGC: boolean = false): IGarbageCollectionData {
533
591
  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
592
  for (const [localId, storageId] of this.redirectTable) {
548
593
  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
594
  gcData.gcNodes[this.getBlobGCNodePath(localId)] = [this.getBlobGCNodePath(storageId)];
552
595
  }
553
-
554
596
  return gcData;
555
597
  }
556
598
 
557
599
  /**
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.
600
+ * This is called to update blobs whose routes are unused. The unused blobs are deleted.
601
+ * @param unusedRoutes - The routes of the blob nodes that are unused.
560
602
  */
561
- public updateUsedRoutes(usedRoutes: string[]) {
603
+ public updateUnusedRoutes(unusedRoutes: string[]): void {
562
604
  // The routes or blob node paths are in the same format as returned in getGCData -
563
605
  // `/<BlobManager.basePath>/<blobId>`.
564
- for (const route of usedRoutes) {
606
+ for (const route of unusedRoutes) {
565
607
  const pathParts = route.split("/");
566
608
  assert(
567
609
  pathParts.length === 3 && pathParts[1] === BlobManager.basePath,
568
- 0x4bc /* Invalid blob node id in used routes. */,
610
+ 0x2d5 /* "Invalid blob node id in unused routes." */,
569
611
  );
570
612
  const blobId = pathParts[2];
571
- // Un-tombstone the blob if it was marked tombstone.
572
- this.tombstonedBlobs.delete(blobId);
613
+ this.redirectTable.delete(blobId);
573
614
  }
574
615
  }
575
616
 
576
617
  /**
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.
618
+ * This is called to update blobs whose routes are tombstones. Tombstoned blobs enable testing scenarios with
619
+ * accessing deleted content without actually deleting content from summaries.
620
+ * @param tombstonedRoutes - The routes of blob nodes that are tombstones.
582
621
  */
583
- public updateUnusedRoutes(unusedRoutes: string[], tombstone: boolean): void {
622
+ public updateTombstonedRoutes(tombstonedRoutes: string[]) {
623
+ const tombstonedBlobsSet: Set<string> = new Set();
584
624
  // The routes or blob node paths are in the same format as returned in getGCData -
585
625
  // `/<BlobManager.basePath>/<blobId>`.
586
- for (const route of unusedRoutes) {
626
+ for (const route of tombstonedRoutes) {
587
627
  const pathParts = route.split("/");
588
628
  assert(
589
629
  pathParts.length === 3 && pathParts[1] === BlobManager.basePath,
590
- 0x2d5 /* "Invalid blob node id in unused routes." */,
630
+ 0x50f /* Invalid blob node id in tombstoned routes. */,
591
631
  );
592
- const blobId = pathParts[2];
632
+ tombstonedBlobsSet.add(pathParts[2]);
633
+ }
593
634
 
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);
635
+ // Remove blobs from the tombstone list that were tombstoned but aren't anymore as per the tombstoneRoutes.
636
+ for (const blobId of this.tombstonedBlobs) {
637
+ if (!tombstonedBlobsSet.has(blobId)) {
638
+ this.tombstonedBlobs.delete(blobId);
601
639
  }
602
640
  }
641
+
642
+ // Mark blobs that are now tombstoned by adding them to the tombstone list.
643
+ for (const blobId of tombstonedBlobsSet) {
644
+ this.tombstonedBlobs.add(blobId);
645
+ }
603
646
  }
604
647
 
605
648
  public summarize(telemetryContext?: ITelemetryContext): ISummaryTreeWithStats {
@@ -630,9 +673,9 @@ export class BlobManager {
630
673
  0x391 /* Redirect table size must match BlobManager's local ID count */);
631
674
  for (const [localId, storageId] of table) {
632
675
  assert(this.redirectTable.has(localId), 0x254 /* "unrecognized id in redirect table" */);
633
- this.redirectTable.set(localId, storageId);
676
+ this.setRedirection(localId, storageId);
634
677
  // set identity (id -> id) entry
635
- this.redirectTable.set(storageId, storageId);
678
+ this.setRedirection(storageId, storageId);
636
679
  }
637
680
  }
638
681