@fluidframework/container-runtime 2.63.0-359734 → 2.63.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 (70) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/api-report/container-runtime.legacy.beta.api.md +7 -1
  3. package/container-runtime.test-files.tar +0 -0
  4. package/dist/blobManager/blobManager.d.ts +62 -55
  5. package/dist/blobManager/blobManager.d.ts.map +1 -1
  6. package/dist/blobManager/blobManager.js +330 -308
  7. package/dist/blobManager/blobManager.js.map +1 -1
  8. package/dist/containerRuntime.d.ts.map +1 -1
  9. package/dist/containerRuntime.js +11 -9
  10. package/dist/containerRuntime.js.map +1 -1
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/legacy.d.ts +1 -0
  15. package/dist/packageVersion.d.ts +1 -1
  16. package/dist/packageVersion.d.ts.map +1 -1
  17. package/dist/packageVersion.js +1 -1
  18. package/dist/packageVersion.js.map +1 -1
  19. package/dist/summary/index.d.ts +2 -2
  20. package/dist/summary/index.d.ts.map +1 -1
  21. package/dist/summary/index.js +2 -1
  22. package/dist/summary/index.js.map +1 -1
  23. package/dist/summary/summarizerTypes.d.ts +8 -1
  24. package/dist/summary/summarizerTypes.d.ts.map +1 -1
  25. package/dist/summary/summarizerTypes.js.map +1 -1
  26. package/dist/summary/summaryDelayLoadedModule/runningSummarizer.d.ts.map +1 -1
  27. package/dist/summary/summaryDelayLoadedModule/runningSummarizer.js +4 -3
  28. package/dist/summary/summaryDelayLoadedModule/runningSummarizer.js.map +1 -1
  29. package/dist/summary/summaryHelpers.d.ts +3 -0
  30. package/dist/summary/summaryHelpers.d.ts.map +1 -1
  31. package/dist/summary/summaryHelpers.js +5 -1
  32. package/dist/summary/summaryHelpers.js.map +1 -1
  33. package/lib/blobManager/blobManager.d.ts +62 -55
  34. package/lib/blobManager/blobManager.d.ts.map +1 -1
  35. package/lib/blobManager/blobManager.js +334 -312
  36. package/lib/blobManager/blobManager.js.map +1 -1
  37. package/lib/containerRuntime.d.ts.map +1 -1
  38. package/lib/containerRuntime.js +12 -10
  39. package/lib/containerRuntime.js.map +1 -1
  40. package/lib/index.d.ts +1 -1
  41. package/lib/index.d.ts.map +1 -1
  42. package/lib/index.js.map +1 -1
  43. package/lib/legacy.d.ts +1 -0
  44. package/lib/packageVersion.d.ts +1 -1
  45. package/lib/packageVersion.d.ts.map +1 -1
  46. package/lib/packageVersion.js +1 -1
  47. package/lib/packageVersion.js.map +1 -1
  48. package/lib/summary/index.d.ts +2 -2
  49. package/lib/summary/index.d.ts.map +1 -1
  50. package/lib/summary/index.js +1 -1
  51. package/lib/summary/index.js.map +1 -1
  52. package/lib/summary/summarizerTypes.d.ts +8 -1
  53. package/lib/summary/summarizerTypes.d.ts.map +1 -1
  54. package/lib/summary/summarizerTypes.js.map +1 -1
  55. package/lib/summary/summaryDelayLoadedModule/runningSummarizer.d.ts.map +1 -1
  56. package/lib/summary/summaryDelayLoadedModule/runningSummarizer.js +4 -3
  57. package/lib/summary/summaryDelayLoadedModule/runningSummarizer.js.map +1 -1
  58. package/lib/summary/summaryHelpers.d.ts +3 -0
  59. package/lib/summary/summaryHelpers.d.ts.map +1 -1
  60. package/lib/summary/summaryHelpers.js +3 -0
  61. package/lib/summary/summaryHelpers.js.map +1 -1
  62. package/package.json +37 -18
  63. package/src/blobManager/blobManager.ts +428 -352
  64. package/src/containerRuntime.ts +33 -27
  65. package/src/index.ts +1 -0
  66. package/src/packageVersion.ts +1 -1
  67. package/src/summary/index.ts +2 -0
  68. package/src/summary/summarizerTypes.ts +10 -1
  69. package/src/summary/summaryDelayLoadedModule/runningSummarizer.ts +4 -6
  70. package/src/summary/summaryHelpers.ts +4 -0
@@ -3,7 +3,7 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import { createEmitter } from "@fluid-internal/client-utils";
6
+ import { bufferToString, createEmitter, stringToBuffer } from "@fluid-internal/client-utils";
7
7
  import {
8
8
  AttachState,
9
9
  type IContainerStorageService,
@@ -22,7 +22,7 @@ import type {
22
22
  Listenable,
23
23
  PayloadState,
24
24
  } from "@fluidframework/core-interfaces/internal";
25
- import { assert, Deferred } from "@fluidframework/core-utils/internal";
25
+ import { assert } from "@fluidframework/core-utils/internal";
26
26
  import type { ICreateBlobResponse } from "@fluidframework/driver-definitions/internal";
27
27
  import type {
28
28
  IGarbageCollectionData,
@@ -40,7 +40,6 @@ import {
40
40
  LoggingError,
41
41
  type MonitoringContext,
42
42
  PerformanceEvent,
43
- UsageError,
44
43
  createChildMonitoringContext,
45
44
  } from "@fluidframework/telemetry-utils/internal";
46
45
  import { v4 as uuid } from "uuid";
@@ -48,7 +47,6 @@ import { v4 as uuid } from "uuid";
48
47
  import { isBlobMetadata } from "../metadata.js";
49
48
 
50
49
  import {
51
- getStorageIds,
52
50
  summarizeBlobManagerState,
53
51
  toRedirectTable,
54
52
  type IBlobManagerLoadInfo,
@@ -80,9 +78,9 @@ export class BlobHandle
80
78
  return (this._events ??= createEmitter<ILocalFluidHandleEvents>());
81
79
  }
82
80
 
83
- private _state: PayloadState = "pending";
81
+ private _payloadState: PayloadState;
84
82
  public get payloadState(): PayloadState {
85
- return this._state;
83
+ return this._payloadState;
86
84
  }
87
85
 
88
86
  /**
@@ -99,16 +97,18 @@ export class BlobHandle
99
97
  public constructor(
100
98
  public readonly path: string,
101
99
  public readonly routeContext: IFluidHandleContext,
100
+ // TODO: just take the blob rather than a get function?
102
101
  public get: () => Promise<ArrayBufferLike>,
103
102
  public readonly payloadPending: boolean,
104
103
  private readonly onAttachGraph?: () => void,
105
104
  ) {
106
105
  super();
106
+ this._payloadState = payloadPending ? "pending" : "shared";
107
107
  this.absolutePath = generateHandleContextPath(path, this.routeContext);
108
108
  }
109
109
 
110
110
  public readonly notifyShared = (): void => {
111
- this._state = "shared";
111
+ this._payloadState = "shared";
112
112
  this._events?.emit("payloadShared");
113
113
  };
114
114
 
@@ -125,7 +125,7 @@ export class BlobHandle
125
125
  }
126
126
  }
127
127
 
128
- // Restrict the IContainerRuntime interface to the subset required by BlobManager. This helps to make
128
+ // Restrict the IContainerRuntime interface to the subset required by BlobManager. This helps to make
129
129
  // the contract explicit and reduces the amount of mocking required for tests.
130
130
  export type IBlobManagerRuntime = Pick<
131
131
  IContainerRuntime,
@@ -136,35 +136,99 @@ export type IBlobManagerRuntime = Pick<
136
136
  export type ICreateBlobResponseWithTTL = ICreateBlobResponse &
137
137
  Partial<Record<"minTTLInSeconds", number>>;
138
138
 
139
- interface PendingBlob {
139
+ /**
140
+ * A blob tracked by BlobManager that is only available on the local client. It is not currently
141
+ * attempting an upload.
142
+ */
143
+ interface LocalOnlyBlob {
144
+ state: "localOnly";
145
+ blob: ArrayBufferLike;
146
+ }
147
+
148
+ /**
149
+ * A blob tracked by BlobManager that is only known to be available on the local client, but is in
150
+ * the process of being uploaded to storage.
151
+ */
152
+ interface UploadingBlob {
153
+ state: "uploading";
154
+ blob: ArrayBufferLike;
155
+ }
156
+
157
+ /**
158
+ * A blob tracked by BlobManager that has been uploaded to storage. If the TTL has not expired, it
159
+ * should still be available in storage. It is not currently attempting to send a BlobAttach op.
160
+ */
161
+ interface UploadedBlob {
162
+ state: "uploaded";
163
+ blob: ArrayBufferLike;
164
+ storageId: string;
165
+ uploadTime: number;
166
+ minTTLInSeconds: number | undefined;
167
+ }
168
+
169
+ /**
170
+ * A blob tracked by BlobManager that has been uploaded to storage and is in the process of sending
171
+ * a BlobAttach op and waiting for the ack.
172
+ */
173
+ interface AttachingBlob {
174
+ state: "attaching";
175
+ blob: ArrayBufferLike;
176
+ storageId: string;
177
+ uploadTime: number;
178
+ minTTLInSeconds: number | undefined;
179
+ }
180
+
181
+ /**
182
+ * A blob tracked by BlobManager that has been uploaded to storage and its BlobAttach op has been
183
+ * ack'd. It is fully shared and available to all clients, and is no longer considered pending.
184
+ */
185
+ interface AttachedBlob {
186
+ state: "attached";
140
187
  blob: ArrayBufferLike;
141
- opsent?: boolean;
142
- storageId?: string;
143
- handleP: Deferred<BlobHandle>;
144
- uploadP?: Promise<void>;
145
- uploadTime?: number;
146
- minTTLInSeconds?: number;
147
- attached?: boolean;
148
- acked?: boolean;
149
- abortSignal?: AbortSignal;
150
188
  }
151
189
 
190
+ /**
191
+ * Blobs that were created locally are tracked, and may be in one of these states. When first
192
+ * created, they are in localOnly state. The process of sharing has two steps, blob upload and
193
+ * sending a BlobAttach op. Progress through the stages may regress back to localOnly if we
194
+ * determine the storage may have deleted the blob before we could finish attaching it.
195
+ */
196
+ type LocalBlobRecord =
197
+ | LocalOnlyBlob
198
+ | UploadingBlob
199
+ | UploadedBlob
200
+ | AttachingBlob
201
+ | AttachedBlob;
202
+
203
+ /**
204
+ * Serializable form of the LocalBlobRecord that can be used to save and restore pending state.
205
+ * Omits attached blobs since they are fully uploaded and don't need to be saved and restored.
206
+ * Omits uploading and attaching states since upon restore we will need to restart those processes.
207
+ */
208
+ type SerializableLocalBlobRecord =
209
+ | (Omit<LocalOnlyBlob, "blob"> & { blob: string })
210
+ | (Omit<UploadedBlob, "blob"> & { blob: string });
211
+
152
212
  export interface IPendingBlobs {
153
- [localId: string]: {
154
- blob: string;
155
- storageId?: string;
156
- uploadTime?: number;
157
- minTTLInSeconds?: number;
158
- acked?: boolean;
159
- };
213
+ [localId: string]: SerializableLocalBlobRecord;
160
214
  }
161
215
 
216
+ /**
217
+ * Check if for a given uploaded or attaching blob, the TTL is too close to expiry to safely attempt
218
+ * an attach. Currently using a heuristic of half the TTL duration having passed since upload.
219
+ */
220
+ const isTTLTooCloseToExpiry = (blobRecord: UploadedBlob | AttachingBlob): boolean =>
221
+ blobRecord.minTTLInSeconds !== undefined &&
222
+ Date.now() - blobRecord.uploadTime > (blobRecord.minTTLInSeconds / 2) * 1000;
223
+
162
224
  interface IBlobManagerInternalEvents {
163
- uploadFailed: (localId: string, error: unknown) => void;
164
- handleAttached: (pending: PendingBlob) => void;
225
+ blobExpired: (localId: string) => void;
226
+ handleAttached: (pending: LocalBlobRecord) => void;
165
227
  processedBlobAttach: (localId: string, storageId: string) => void;
166
228
  }
167
229
 
230
+ const createAbortError = (): LoggingError => new LoggingError("uploadBlob aborted");
231
+
168
232
  export const blobManagerBasePath = "_blobs";
169
233
 
170
234
  export class BlobManager {
@@ -181,16 +245,19 @@ export class BlobManager {
181
245
  private readonly redirectTable: Map<string, string>;
182
246
 
183
247
  /**
184
- * Blobs which we have not yet seen a BlobAttach op round-trip and not yet attached to a DDS.
248
+ * The localBlobCache has a dual role of caching locally-created blobs, as well as tracking their state as they
249
+ * are shared. Keys are localIds.
185
250
  */
186
- private readonly pendingBlobs: Map<string, PendingBlob> = new Map();
187
-
251
+ private readonly localBlobCache: Map<string, LocalBlobRecord> = new Map();
188
252
  /**
189
- * Track ops in flight for online flow. This is used for optimizations where if we receive an ack for a storage ID,
190
- * we can resolve all pending blobs with the same storage ID even though they may have different local IDs. That's
191
- * because we know that the server will not delete the blob corresponding to that storage ID.
253
+ * Blobs with an attached handle that have not finished blob-attaching are the set we need to provide from
254
+ * getPendingState(). This stores their local IDs, and then we can look them up against the localBlobCache.
192
255
  */
193
- private readonly opsInFlight: Map<string, Set<string>> = new Map();
256
+ private readonly pendingBlobsWithAttachedHandles: Set<string> = new Set();
257
+ /**
258
+ * Local IDs for any pending blobs we loaded with and have not yet started the upload/attach flow for.
259
+ */
260
+ private readonly pendingOnlyLocalIds: Set<string> = new Set();
194
261
 
195
262
  private readonly sendBlobAttachOp: (localId: string, storageId: string) => void;
196
263
 
@@ -230,7 +297,7 @@ export class BlobManager {
230
297
  // blobPath's format - `/<basePath>/<localId>`.
231
298
  readonly isBlobDeleted: (blobPath: string) => boolean;
232
299
  readonly runtime: IBlobManagerRuntime;
233
- stashedBlobs: IPendingBlobs | undefined;
300
+ pendingBlobs: IPendingBlobs | undefined;
234
301
  readonly localIdGenerator?: (() => string) | undefined;
235
302
  readonly createBlobPayloadPending: boolean;
236
303
  }) {
@@ -242,11 +309,13 @@ export class BlobManager {
242
309
  blobRequested,
243
310
  isBlobDeleted,
244
311
  runtime,
312
+ pendingBlobs,
245
313
  localIdGenerator,
246
314
  createBlobPayloadPending,
247
315
  } = props;
248
316
  this.routeContext = routeContext;
249
317
  this.storage = storage;
318
+ this.sendBlobAttachOp = sendBlobAttachOp;
250
319
  this.blobRequested = blobRequested;
251
320
  this.isBlobDeleted = isBlobDeleted;
252
321
  this.runtime = runtime;
@@ -260,51 +329,34 @@ export class BlobManager {
260
329
 
261
330
  this.redirectTable = toRedirectTable(blobManagerLoadInfo, this.mc.logger);
262
331
 
263
- this.sendBlobAttachOp = (localId: string, storageId: string) => {
264
- const pendingEntry = this.pendingBlobs.get(localId);
265
- assert(
266
- pendingEntry !== undefined,
267
- 0x725 /* Must have pending blob entry for upcoming op */,
268
- );
269
- if (
270
- pendingEntry?.uploadTime !== undefined &&
271
- pendingEntry?.minTTLInSeconds !== undefined
272
- ) {
273
- const secondsSinceUpload = (Date.now() - pendingEntry.uploadTime) / 1000;
274
- const expired = pendingEntry.minTTLInSeconds - secondsSinceUpload < 0;
275
- this.mc.logger.sendTelemetryEvent({
276
- eventName: "sendBlobAttach",
277
- secondsSinceUpload,
278
- minTTLInSeconds: pendingEntry.minTTLInSeconds,
279
- expired,
280
- });
281
- if (expired) {
282
- // reupload blob and reset previous fields
283
- this.pendingBlobs.set(localId, {
284
- ...pendingEntry,
285
- storageId: undefined,
286
- uploadTime: undefined,
287
- minTTLInSeconds: undefined,
288
- opsent: false,
289
- uploadP: this.uploadBlob(localId, pendingEntry.blob),
290
- });
291
- return;
292
- }
332
+ // We populate the localBlobCache with any pending blobs we are provided, which makes them available
333
+ // to access even though they are not shared yet. However, we don't start the share flow until it is
334
+ // explicitly invoked via sharePendingBlobs() in case we are loaded in a frozen container.
335
+ if (pendingBlobs !== undefined) {
336
+ for (const [localId, serializableBlobRecord] of Object.entries(pendingBlobs)) {
337
+ assert(
338
+ !this.redirectTable.has(localId),
339
+ 0xc7e /* Pending blob already in redirect table */,
340
+ );
341
+ const localBlobRecord = {
342
+ ...serializableBlobRecord,
343
+ blob: stringToBuffer(serializableBlobRecord.blob, "base64"),
344
+ };
345
+ this.localBlobCache.set(localId, localBlobRecord);
346
+ // Since we received these blobs from pending state, we'll assume they were only added to the
347
+ // pending state at generation time because their handles were attached. We add them back here
348
+ // in case we need to round-trip them back out again due to another getPendingBlobs() call.
349
+ this.pendingBlobsWithAttachedHandles.add(localId);
350
+ this.pendingOnlyLocalIds.add(localId);
293
351
  }
294
- pendingEntry.opsent = true;
295
- sendBlobAttachOp(localId, storageId);
296
- };
297
- }
298
-
299
- private createAbortError(pending?: PendingBlob): LoggingError {
300
- return new LoggingError("uploadBlob aborted", {
301
- acked: pending?.acked,
302
- uploadTime: pending?.uploadTime,
303
- });
352
+ }
304
353
  }
305
354
 
355
+ /**
356
+ * Returns whether a blob with the given localId can be retrieved by the BlobManager via getBlob().
357
+ */
306
358
  public hasBlob(localId: string): boolean {
307
- return this.redirectTable.get(localId) !== undefined;
359
+ return this.redirectTable.has(localId) || this.localBlobCache.has(localId);
308
360
  }
309
361
 
310
362
  /**
@@ -340,16 +392,17 @@ export class BlobManager {
340
392
  // is configured.
341
393
  this.blobRequested(getGCNodePathFromLocalId(localId));
342
394
 
343
- const pending = this.pendingBlobs.get(localId);
344
- if (pending) {
345
- return pending.blob;
395
+ const localBlobRecord = this.localBlobCache.get(localId);
396
+ if (localBlobRecord !== undefined) {
397
+ return localBlobRecord.blob;
346
398
  }
347
399
 
348
400
  let storageId = this.redirectTable.get(localId);
349
401
  if (storageId === undefined) {
350
402
  // Only blob handles explicitly marked with pending payload are permitted to exist without
351
403
  // yet knowing their storage id. Otherwise they must already be associated with a storage id.
352
- // Handles for detached blobs are not payload pending.
404
+ // Handles for detached blobs are not payload pending, though they should also always be present
405
+ // in the localBlobCache and therefore should never need to refer to storage.
353
406
  assert(payloadPending, 0x11f /* "requesting unknown blobs" */);
354
407
  // If we didn't find it in the redirectTable and it's payloadPending, assume the attach op is coming
355
408
  // eventually and wait. We do this even if the local client doesn't have the blob payloadPending flag
@@ -385,48 +438,25 @@ export class BlobManager {
385
438
  );
386
439
  }
387
440
 
388
- private getBlobHandle(localId: string): BlobHandle {
389
- assert(
390
- this.redirectTable.has(localId) || this.pendingBlobs.has(localId),
391
- 0x384 /* requesting handle for unknown blob */,
392
- );
393
- const pending = this.pendingBlobs.get(localId);
394
- // Create a callback function for once the handle has been attached
395
- const callback = pending
396
- ? () => {
397
- pending.attached = true;
398
- // Notify listeners (e.g. serialization process) that handle has been attached
399
- this.internalEvents.emit("handleAttached", pending);
400
- this.deletePendingBlobMaybe(localId);
401
- }
402
- : undefined;
441
+ private getNonPayloadPendingBlobHandle(localId: string): BlobHandle {
442
+ const localBlobRecord = this.localBlobCache.get(localId);
443
+ assert(localBlobRecord !== undefined, 0x384 /* requesting handle for unknown blob */);
444
+ assert(localBlobRecord.state === "attached", 0xc7f /* Expected blob to be attached */);
445
+
403
446
  return new BlobHandle(
404
447
  getGCNodePathFromLocalId(localId),
405
448
  this.routeContext,
406
449
  async () => this.getBlob(localId, false),
407
450
  false, // payloadPending
408
- callback,
409
451
  );
410
452
  }
411
453
 
412
- private async createBlobDetached(
413
- blob: ArrayBufferLike,
414
- ): Promise<IFluidHandleInternalPayloadPending<ArrayBufferLike>> {
415
- const localId = this.localIdGenerator();
416
- // Blobs created while the container is detached are stored in IDetachedBlobStorage.
417
- // The 'IContainerStorageService.createBlob()' call below will respond with a pseudo storage ID.
418
- // That pseudo storage ID will be replaced with the real storage ID at attach time.
419
- const { id: detachedStorageId } = await this.storage.createBlob(blob);
420
- this.setRedirection(localId, detachedStorageId);
421
- return this.getBlobHandle(localId);
422
- }
423
-
424
454
  public async createBlob(
425
455
  blob: ArrayBufferLike,
426
456
  signal?: AbortSignal,
427
457
  ): Promise<IFluidHandleInternalPayloadPending<ArrayBufferLike>> {
428
458
  if (this.runtime.attachState === AttachState.Detached) {
429
- return this.createBlobDetached(blob);
459
+ return this.createBlobDetached(blob, signal);
430
460
  }
431
461
  if (this.runtime.attachState === AttachState.Attaching) {
432
462
  // blob upload is not supported in "Attaching" state
@@ -439,48 +469,46 @@ export class BlobManager {
439
469
  );
440
470
 
441
471
  return this.createBlobPayloadPending
442
- ? this.createBlobWithPayloadPending(blob)
472
+ ? this.createBlobWithPayloadPending(blob, signal)
443
473
  : this.createBlobLegacy(blob, signal);
444
474
  }
445
475
 
446
- private async createBlobLegacy(
476
+ private async createBlobDetached(
447
477
  blob: ArrayBufferLike,
448
478
  signal?: AbortSignal,
449
479
  ): Promise<IFluidHandleInternalPayloadPending<ArrayBufferLike>> {
450
480
  if (signal?.aborted === true) {
451
- throw this.createAbortError();
481
+ throw createAbortError();
452
482
  }
453
-
454
- // Create a local ID for the blob. After uploading it to storage and before returning it, a local ID to
455
- // storage ID mapping is created.
456
483
  const localId = this.localIdGenerator();
457
- const pendingEntry: PendingBlob = {
458
- blob,
459
- handleP: new Deferred(),
460
- uploadP: this.uploadBlob(localId, blob),
461
- attached: false,
462
- acked: false,
463
- abortSignal: signal,
464
- opsent: false,
465
- };
466
- this.pendingBlobs.set(localId, pendingEntry);
467
-
468
- const abortListener = (): void => {
469
- if (pendingEntry.acked !== true) {
470
- pendingEntry.handleP.reject(this.createAbortError(pendingEntry));
471
- }
472
- };
473
- signal?.addEventListener("abort", abortListener, { once: true });
484
+ this.localBlobCache.set(localId, { state: "uploading", blob });
485
+ // Blobs created while the container is detached are stored in IDetachedBlobStorage.
486
+ // The 'IContainerStorageService.createBlob()' call below will respond with a pseudo storage ID.
487
+ // That pseudo storage ID will be replaced with the real storage ID at attach time.
488
+ const { id: detachedStorageId } = await this.storage.createBlob(blob);
489
+ // From the perspective of the BlobManager, the blob is now fully attached. The actual
490
+ // upload/attach process at container attach time is treated as opaque to this tracking.
491
+ this.localBlobCache.set(localId, { state: "attached", blob });
492
+ this.redirectTable.set(localId, detachedStorageId);
493
+ return this.getNonPayloadPendingBlobHandle(localId);
494
+ }
474
495
 
475
- return pendingEntry.handleP.promise.finally(() => {
476
- signal?.removeEventListener("abort", abortListener);
477
- });
496
+ private async createBlobLegacy(
497
+ blob: ArrayBufferLike,
498
+ signal?: AbortSignal,
499
+ ): Promise<IFluidHandleInternalPayloadPending<ArrayBufferLike>> {
500
+ const localId = this.localIdGenerator();
501
+ this.localBlobCache.set(localId, { state: "localOnly", blob });
502
+ await this.uploadAndAttach(localId, signal);
503
+ return this.getNonPayloadPendingBlobHandle(localId);
478
504
  }
479
505
 
480
506
  private createBlobWithPayloadPending(
481
507
  blob: ArrayBufferLike,
508
+ signal?: AbortSignal,
482
509
  ): IFluidHandleInternalPayloadPending<ArrayBufferLike> {
483
510
  const localId = this.localIdGenerator();
511
+ this.localBlobCache.set(localId, { state: "localOnly", blob });
484
512
 
485
513
  const blobHandle = new BlobHandle(
486
514
  getGCNodePathFromLocalId(localId),
@@ -488,157 +516,209 @@ export class BlobManager {
488
516
  async () => blob,
489
517
  true, // payloadPending
490
518
  () => {
491
- const pendingEntry: PendingBlob = {
492
- blob,
493
- handleP: new Deferred(),
494
- uploadP: this.uploadBlob(localId, blob),
495
- attached: true,
496
- acked: false,
497
- opsent: false,
498
- };
499
- this.pendingBlobs.set(localId, pendingEntry);
519
+ this.pendingBlobsWithAttachedHandles.add(localId);
520
+ const uploadAndAttachP = this.uploadAndAttach(localId, signal);
521
+ uploadAndAttachP.then(blobHandle.notifyShared).catch((error) => {
522
+ // TODO: notifyShared won't fail directly, but it emits an event to the customer.
523
+ // Consider what to do if the customer's code throws. reportError is nice.
524
+ });
525
+ uploadAndAttachP.catch(blobHandle.notifyFailed);
500
526
  },
501
527
  );
502
528
 
503
- const onProcessedBlobAttach = (_localId: string, _storageId: string): void => {
504
- if (_localId === localId) {
505
- this.internalEvents.off("processedBlobAttach", onProcessedBlobAttach);
506
- blobHandle.notifyShared();
507
- }
508
- };
509
- this.internalEvents.on("processedBlobAttach", onProcessedBlobAttach);
510
-
511
- const onUploadFailed = (_localId: string, error: unknown): void => {
512
- if (_localId === localId) {
513
- this.internalEvents.off("uploadFailed", onUploadFailed);
514
- blobHandle.notifyFailed(error);
515
- }
516
- };
517
- this.internalEvents.on("uploadFailed", onUploadFailed);
518
-
519
529
  return blobHandle;
520
530
  }
521
531
 
522
532
  /**
523
- * Upload a blob to the storage service.
524
- * @returns A promise that resolves when the upload is complete and a blob attach op has been sent (but not ack'd).
533
+ * Upload and attach the localBlobCache entry for the given localId.
525
534
  *
526
- * @privateRemarks This method must not reject, as there is no error handling for it in current tracking.
535
+ * Expects the localBlobCache entry for the given localId to be in either localOnly or uploaded state
536
+ * when called. Returns a promise that resolves when the blob completes uploading and attaching, or else
537
+ * rejects if an error is encountered or the signal is aborted.
527
538
  */
528
- private async uploadBlob(localId: string, blob: ArrayBufferLike): Promise<void> {
529
- let response: ICreateBlobResponseWithTTL;
530
- try {
531
- response = await this.storage.createBlob(blob);
532
- } catch (error) {
533
- const entry = this.pendingBlobs.get(localId);
534
- this.mc.logger.sendTelemetryEvent({
535
- eventName: "UploadBlobReject",
536
- // TODO: better typing
537
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
538
- error: error as any,
539
- message: entry === undefined ? "Missing pendingBlob" : undefined,
540
- localId,
541
- });
542
- // We probably should assert the pendingBlobs entry here, but we don't currently have any error handling
543
- // for the uploadP - a promise rejection would be unhandled anyway. For now we can detect this with the
544
- // message on the UploadBlobReject telemetry.
545
- if (entry !== undefined) {
546
- entry.handleP.reject(error);
547
- this.deletePendingBlob(localId);
548
- }
549
- this.internalEvents.emit("uploadFailed", localId, error);
550
- return;
539
+ private readonly uploadAndAttach = async (
540
+ localId: string,
541
+ signal?: AbortSignal,
542
+ ): Promise<void> => {
543
+ if (signal?.aborted === true) {
544
+ this.localBlobCache.delete(localId);
545
+ this.pendingBlobsWithAttachedHandles.delete(localId);
546
+ throw createAbortError();
551
547
  }
548
+ const localBlobRecordInitial = this.localBlobCache.get(localId);
549
+ assert(
550
+ localBlobRecordInitial?.state === "localOnly" ||
551
+ localBlobRecordInitial?.state === "uploaded",
552
+ 0xc80 /* Expect uploadAndAttach to be called with either localOnly or uploaded state */,
553
+ );
554
+ const { blob } = localBlobRecordInitial;
552
555
 
553
- try {
554
- this.onUploadResolve(localId, response);
555
- } catch (error) {
556
- this.mc.logger.sendTelemetryEvent({
557
- eventName: "OnUploadResolveError",
558
- // TODO: better typing
559
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
560
- error: error as any,
561
- localId,
562
- });
563
- }
564
- }
556
+ /**
557
+ * Expects the localBlobCache entry for the given localId to be in either localOnly or uploaded state
558
+ * when called. Returns a promise that resolves when the blob is in uploaded or attached state, or else
559
+ * rejects on error during upload or if the signal is aborted.
560
+ *
561
+ * Most of the time this should be expected to exit in uploaded state, but if we are loading from pending
562
+ * state we may see an attach op from the client that generated the pending state, which can complete the
563
+ * attach while the upload is outstanding.
564
+ */
565
+ const ensureUploaded = async (): Promise<void> => {
566
+ const localBlobRecord = this.localBlobCache.get(localId);
567
+ if (localBlobRecord?.state === "uploaded") {
568
+ // In normal creation flows, the blob will be in localOnly state here. But in the case of loading
569
+ // with pending state we can call it with an uploaded-but-not-attached blob. Start the upload
570
+ // flow only if it's localOnly.
571
+ return;
572
+ }
573
+ assert(
574
+ localBlobRecord?.state === "localOnly",
575
+ 0xc81 /* Attempting to upload from unexpected state */,
576
+ );
565
577
 
566
- /**
567
- * Set up a mapping in the redirect table from fromId to toId. Also, notify the runtime that a reference is added
568
- * which is required for GC.
569
- */
570
- private setRedirection(fromId: string, toId: string): void {
571
- this.redirectTable.set(fromId, toId);
572
- }
578
+ this.localBlobCache.set(localId, { state: "uploading", blob });
579
+ await new Promise<void>((resolve, reject) => {
580
+ // If we eventually have driver-level support for abort, then this can simplify a bit as we won't
581
+ // need to track upload completion and abort separately. Until then, we need to handle the case that
582
+ // the upload continues and settles after becoming irrelevant due to signal abort or blob attach.
583
+ let uploadHasBecomeIrrelevant = false;
584
+ const onSignalAbort = (): void => {
585
+ removeListeners();
586
+ uploadHasBecomeIrrelevant = true;
587
+ this.localBlobCache.delete(localId);
588
+ this.pendingBlobsWithAttachedHandles.delete(localId);
589
+ reject(createAbortError());
590
+ };
591
+ const onProcessedBlobAttach = (_localId: string, _storageId: string): void => {
592
+ if (_localId === localId) {
593
+ removeListeners();
594
+ uploadHasBecomeIrrelevant = true;
595
+ resolve();
596
+ }
597
+ };
598
+ const removeListeners = (): void => {
599
+ this.internalEvents.off("processedBlobAttach", onProcessedBlobAttach);
600
+ signal?.removeEventListener("abort", onSignalAbort);
601
+ };
602
+ this.internalEvents.on("processedBlobAttach", onProcessedBlobAttach);
603
+ signal?.addEventListener("abort", onSignalAbort);
604
+
605
+ this.storage
606
+ .createBlob(blob)
607
+ .then((createBlobResponse: ICreateBlobResponseWithTTL) => {
608
+ if (!uploadHasBecomeIrrelevant) {
609
+ removeListeners();
610
+ this.localBlobCache.set(localId, {
611
+ state: "uploaded",
612
+ blob,
613
+ storageId: createBlobResponse.id,
614
+ uploadTime: Date.now(),
615
+ minTTLInSeconds: createBlobResponse.minTTLInSeconds,
616
+ });
617
+ resolve();
618
+ }
619
+ })
620
+ .catch((error) => {
621
+ if (!uploadHasBecomeIrrelevant) {
622
+ removeListeners();
623
+ // If the storage call errors, we can't recover. Reject to throw back to the caller.
624
+ this.localBlobCache.delete(localId);
625
+ this.pendingBlobsWithAttachedHandles.delete(localId);
626
+ reject(error);
627
+ }
628
+ });
629
+ });
630
+ };
573
631
 
574
- private deletePendingBlobMaybe(localId: string): void {
575
- if (this.pendingBlobs.has(localId)) {
576
- const entry = this.pendingBlobs.get(localId);
577
- if (entry?.attached === true && entry?.acked === true) {
578
- this.deletePendingBlob(localId);
632
+ /**
633
+ * Expects the localBlobCache entry for the given localId to be in uploaded or attached state when called.
634
+ * Returns a promise that resolves to true if the blob is successfully attached, or false if it cannot be
635
+ * attached and the upload flow needs to be restarted from the top (currently only if the TTL expires before
636
+ * attach can be completed). In the latter case, the localBlobRecord will also be reset to localOnly state.
637
+ * The promise rejects if the signal is aborted.
638
+ */
639
+ const tryAttach = async (): Promise<boolean> => {
640
+ const localBlobRecord = this.localBlobCache.get(localId);
641
+ if (localBlobRecord?.state === "attached") {
642
+ // In normal creation flows, the blob will be in uploaded state here. But if we are loading from pending
643
+ // state and see an attach op from the client that generated the pending state, we may have reached
644
+ // attached state in the middle of the upload attempt. In that case there's no more work to do and we
645
+ // can just return.
646
+ return true;
579
647
  }
580
- }
581
- }
648
+ assert(
649
+ localBlobRecord?.state === "uploaded",
650
+ 0xc82 /* Attempting to attach from unexpected state */,
651
+ );
582
652
 
583
- private deletePendingBlob(id: string): void {
584
- this.pendingBlobs.delete(id);
585
- }
653
+ // If we just uploaded the blob TTL really shouldn't be expired at this location. But if we loaded from
654
+ // pending state, the upload may have happened some time far in the past and could be expired here.
655
+ if (isTTLTooCloseToExpiry(localBlobRecord)) {
656
+ // If the TTL is expired, we assume it's gone from the storage and so is effectively localOnly again.
657
+ // Then when we re-enter the loop, we'll re-upload it.
658
+ this.localBlobCache.set(localId, { state: "localOnly", blob });
659
+ // Emitting here isn't really necessary since the only listener would be attached below. Including here
660
+ // for completeness though, in case we add other listeners in the future.
661
+ this.internalEvents.emit("blobExpired", localId);
662
+ return false;
663
+ } else {
664
+ this.localBlobCache.set(localId, {
665
+ ...localBlobRecord,
666
+ state: "attaching",
667
+ });
586
668
 
587
- private onUploadResolve(
588
- localId: string,
589
- response: ICreateBlobResponseWithTTL,
590
- ): ICreateBlobResponseWithTTL | undefined {
591
- const entry = this.pendingBlobs.get(localId);
592
-
593
- assert(entry !== undefined, 0x6c8 /* pending blob entry not found for uploaded blob */);
594
- if (entry.abortSignal?.aborted === true && entry.opsent !== true) {
595
- this.mc.logger.sendTelemetryEvent({
596
- eventName: "BlobAborted",
597
- localId,
598
- });
599
- this.deletePendingBlob(localId);
600
- return;
601
- }
602
- assert(
603
- entry.storageId === undefined,
604
- 0x386 /* Must have pending blob entry for uploaded blob */,
605
- );
606
- entry.storageId = response.id;
607
- entry.uploadTime = Date.now();
608
- entry.minTTLInSeconds = response.minTTLInSeconds;
609
- // Send a blob attach op. This serves two purposes:
610
- // 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
611
- // until its storage ID is added to the next summary.
612
- // 2. It will create a local ID to storage ID mapping in all clients which is needed to retrieve the
613
- // blob from the server via the storage ID.
614
- if (entry.opsent !== true) {
615
- this.sendBlobAttachOp(localId, response.id);
616
- }
617
- const storageIds = getStorageIds(this.redirectTable);
618
- if (storageIds.has(response.id)) {
619
- // The blob is de-duped. Set up a local ID to storage ID mapping and return the blob. Since this is
620
- // an existing blob, we don't have to wait for the op to be ack'd since this step has already
621
- // happened before and so, the server won't delete it.
622
- this.setRedirection(localId, response.id);
623
- const blobHandle = this.getBlobHandle(localId);
624
- blobHandle.notifyShared();
625
- entry.handleP.resolve(blobHandle);
626
- this.deletePendingBlobMaybe(localId);
627
- } else {
628
- // If there is already an op for this storage ID, append the local ID to the list. Once any op for
629
- // this storage ID is ack'd, all pending blobs for it can be resolved since the op will keep the
630
- // blob alive in storage.
631
- let setForRemoteId = this.opsInFlight.get(response.id);
632
- if (setForRemoteId === undefined) {
633
- setForRemoteId = new Set();
634
- this.opsInFlight.set(response.id, setForRemoteId);
669
+ // Send and await a blob attach op. This serves two purposes:
670
+ // 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
671
+ // until its storage ID is added to the next summary.
672
+ // 2. It will create a local ID to storage ID mapping in all clients which is needed to retrieve the
673
+ // blob from the server via the storage ID.
674
+ return new Promise<boolean>((resolve, reject) => {
675
+ const onProcessedBlobAttach = (_localId: string, _storageId: string): void => {
676
+ if (_localId === localId) {
677
+ removeListeners();
678
+ resolve(true);
679
+ }
680
+ };
681
+ // Although we already checked for TTL expiry above, the op we're about to send may later be asked
682
+ // to resubmit. Before we resubmit, we check again for TTL expiry - this listener is how we learn if
683
+ // we discovered expiry in the resubmit flow.
684
+ const onBlobExpired = (_localId: string): void => {
685
+ if (_localId === localId) {
686
+ removeListeners();
687
+ resolve(false);
688
+ }
689
+ };
690
+ const onSignalAbort = (): void => {
691
+ removeListeners();
692
+ this.localBlobCache.delete(localId);
693
+ this.pendingBlobsWithAttachedHandles.delete(localId);
694
+ reject(createAbortError());
695
+ };
696
+ const removeListeners = (): void => {
697
+ this.internalEvents.off("processedBlobAttach", onProcessedBlobAttach);
698
+ this.internalEvents.off("blobExpired", onBlobExpired);
699
+ signal?.removeEventListener("abort", onSignalAbort);
700
+ };
701
+
702
+ this.internalEvents.on("processedBlobAttach", onProcessedBlobAttach);
703
+ this.internalEvents.on("blobExpired", onBlobExpired);
704
+ signal?.addEventListener("abort", onSignalAbort);
705
+ this.sendBlobAttachOp(localId, localBlobRecord.storageId);
706
+ });
635
707
  }
636
- // seeing the same localId twice can happen if a blob is being reuploaded and stashed.
637
- // TODO: review stashing logic and see if we can avoid this, as well in tests.
638
- setForRemoteId.add(localId);
708
+ };
709
+
710
+ let attachCompleted = false;
711
+ while (!attachCompleted) {
712
+ await ensureUploaded();
713
+ attachCompleted = await tryAttach();
714
+
715
+ // If something stopped the attach from completing successfully (currently just TTL expiry),
716
+ // we expect that the blob was already updated to reflect the updated state (i.e. back to localOnly)
717
+ // and we'll try the loop again from the top.
639
718
  }
640
- return response;
641
- }
719
+ // When the blob successfully attaches, the localBlobRecord will have been updated to attached state
720
+ // at the time we processed the op, so there's nothing else to do here.
721
+ };
642
722
 
643
723
  /**
644
724
  * Resubmit a BlobAttach op. Used to add storage IDs to ops that were
@@ -648,14 +728,22 @@ export class BlobManager {
648
728
  public reSubmit(metadata: Record<string, unknown> | undefined): void {
649
729
  assert(isBlobMetadata(metadata), 0xc01 /* Expected blob metadata for a BlobAttach op */);
650
730
  const { localId, blobId: storageId } = metadata;
651
- // Any blob that we're actively trying to advance to attached state must have a
652
- // pendingBlobs entry. Decline to resubmit for anything else.
653
- // For example, we might be asked to resubmit stashed ops for blobs that never had
654
- // their handle attached - these won't have a pendingBlobs entry and we shouldn't
655
- // try to attach them since they won't be accessible to the customer and would just
656
- // be considered garbage immediately.
657
- if (this.pendingBlobs.has(localId)) {
658
- this.sendBlobAttachOp(localId, storageId);
731
+ // Any blob that we're actively trying to advance to attached state must be in attaching state.
732
+ // Decline to resubmit for anything else.
733
+ // For example, we might be asked to resubmit stashed ops for blobs that never had their handle
734
+ // attached - these won't have a localBlobCache entry because we filter them out when generating
735
+ // pending state. We shouldn't try to attach them since they won't be accessible to the customer
736
+ // and would just be considered garbage immediately.
737
+ // TODO: Is it possible that we'd be asked to resubmit for a pending blob before we call sharePendingBlobs?
738
+ const localBlobRecord = this.localBlobCache.get(localId);
739
+ if (localBlobRecord?.state === "attaching") {
740
+ // If the TTL is expired, we assume it's gone from the storage and so is effectively localOnly again.
741
+ if (isTTLTooCloseToExpiry(localBlobRecord)) {
742
+ this.localBlobCache.set(localId, { state: "localOnly", blob: localBlobRecord.blob });
743
+ this.internalEvents.emit("blobExpired", localId);
744
+ } else {
745
+ this.sendBlobAttachOp(localId, storageId);
746
+ }
659
747
  }
660
748
  }
661
749
 
@@ -665,46 +753,25 @@ export class BlobManager {
665
753
  0xc02 /* Expected blob metadata for a BlobAttach op */,
666
754
  );
667
755
  const { localId, blobId: storageId } = message.metadata;
668
- const pendingEntry = this.pendingBlobs.get(localId);
669
- if (pendingEntry?.abortSignal?.aborted === true) {
670
- this.deletePendingBlob(localId);
671
- return;
756
+ const maybeLocalBlobRecord = this.localBlobCache.get(localId);
757
+ if (maybeLocalBlobRecord !== undefined) {
758
+ const attachedBlobRecord: AttachedBlob = {
759
+ state: "attached",
760
+ blob: maybeLocalBlobRecord.blob,
761
+ };
762
+ // Processing a blob attach op is authoritative and may stomp on any existing state. Other
763
+ // callsites that update localBlobCache entries must take proper caution to handle the case
764
+ // that a blob attach op is processed concurrently.
765
+ this.localBlobCache.set(localId, attachedBlobRecord);
766
+ // Note there may or may not be an entry in pendingBlobsWithAttachedHandles for this localId,
767
+ // in particular for the non-payloadPending case since we should be reaching this point
768
+ // before even returning a handle to the caller.
769
+ this.pendingBlobsWithAttachedHandles.delete(localId);
770
+ this.pendingOnlyLocalIds.delete(localId);
672
771
  }
673
-
674
- this.setRedirection(localId, storageId);
772
+ this.redirectTable.set(localId, storageId);
675
773
  // set identity (id -> id) entry
676
- this.setRedirection(storageId, storageId);
677
-
678
- if (local) {
679
- const waitingBlobs = this.opsInFlight.get(storageId);
680
- if (waitingBlobs !== undefined) {
681
- // For each op corresponding to this storage ID that we are waiting for, resolve the pending blob.
682
- // This is safe because the server will keep the blob alive and the op containing the local ID to
683
- // storage ID is already in flight and any op containing this local ID will be sequenced after that.
684
- for (const pendingLocalId of waitingBlobs) {
685
- const entry = this.pendingBlobs.get(pendingLocalId);
686
- assert(
687
- entry !== undefined,
688
- 0x38f /* local online BlobAttach op with no pending blob entry */,
689
- );
690
- this.setRedirection(pendingLocalId, storageId);
691
- entry.acked = true;
692
- const blobHandle = this.getBlobHandle(pendingLocalId);
693
- blobHandle.notifyShared();
694
- entry.handleP.resolve(blobHandle);
695
- this.deletePendingBlobMaybe(pendingLocalId);
696
- }
697
- this.opsInFlight.delete(storageId);
698
- }
699
- const localEntry = this.pendingBlobs.get(localId);
700
- if (localEntry) {
701
- localEntry.acked = true;
702
- const blobHandle = this.getBlobHandle(localId);
703
- blobHandle.notifyShared();
704
- localEntry.handleP.resolve(blobHandle);
705
- this.deletePendingBlobMaybe(localId);
706
- }
707
- }
774
+ this.redirectTable.set(storageId, storageId);
708
775
  this.internalEvents.emit("processedBlobAttach", localId, storageId);
709
776
  }
710
777
 
@@ -732,19 +799,9 @@ export class BlobManager {
732
799
  return gcData;
733
800
  }
734
801
 
735
- /**
736
- * Delete attachment blobs that are sweep ready.
737
- * @param sweepReadyBlobRoutes - The routes of blobs that are sweep ready and should be deleted. These routes will
738
- * be based off of local ids.
739
- * @returns The routes of blobs that were deleted.
740
- */
741
- public deleteSweepReadyNodes(sweepReadyBlobRoutes: readonly string[]): readonly string[] {
742
- this.deleteBlobsFromRedirectTable(sweepReadyBlobRoutes);
743
- return [...sweepReadyBlobRoutes];
744
- }
745
-
746
802
  /**
747
803
  * Delete blobs with the given routes from the redirect table.
804
+ * @returns The routes of blobs that were deleted.
748
805
  *
749
806
  * @remarks
750
807
  * The routes are GC nodes paths of format -`/<blobManagerBasePath>/<localId>`.
@@ -759,11 +816,11 @@ export class BlobManager {
759
816
  * will ensure we don't create an attachment blob for them at the next summary. The service would then delete them
760
817
  * some time in the future.
761
818
  */
762
- private deleteBlobsFromRedirectTable(blobRoutes: readonly string[]): void {
819
+ public deleteSweepReadyNodes(sweepReadyBlobRoutes: readonly string[]): readonly string[] {
763
820
  // maybeUnusedStorageIds is used to compute the set of storage IDs that *used to have a local ID*, but that
764
821
  // local ID is being deleted.
765
822
  const maybeUnusedStorageIds: Set<string> = new Set();
766
- for (const route of blobRoutes) {
823
+ for (const route of sweepReadyBlobRoutes) {
767
824
  const localId = getLocalIdFromGCNodePath(route);
768
825
  // If the blob hasn't already been deleted, log an error because this should never happen.
769
826
  // If the blob has already been deleted, log a telemetry event. This can happen because multiple GC
@@ -799,6 +856,7 @@ export class BlobManager {
799
856
  for (const storageId of maybeUnusedStorageIds) {
800
857
  this.redirectTable.delete(storageId);
801
858
  }
859
+ return [...sweepReadyBlobRoutes];
802
860
  }
803
861
 
804
862
  /**
@@ -851,41 +909,59 @@ export class BlobManager {
851
909
  for (const [localId, detachedStorageId] of redirectTableEntries) {
852
910
  const newStorageId = detachedStorageTable.get(detachedStorageId);
853
911
  assert(newStorageId !== undefined, 0xc53 /* Couldn't find a matching storage ID */);
854
- this.setRedirection(localId, newStorageId);
912
+ this.redirectTable.set(localId, newStorageId);
855
913
  // set identity (id -> id) entry
856
- this.setRedirection(newStorageId, newStorageId);
914
+ this.redirectTable.set(newStorageId, newStorageId);
857
915
  }
858
916
  };
859
917
 
918
+ /**
919
+ * Upload and attach any pending blobs that the BlobManager was loaded with that have not already
920
+ * been attached in the meantime.
921
+ * @returns A promise that resolves when all the uploads and attaches have completed, or rejects
922
+ * if any of them fail.
923
+ */
924
+ public readonly sharePendingBlobs = async (): Promise<void> => {
925
+ const localIdsToUpload = [...this.pendingOnlyLocalIds];
926
+ this.pendingOnlyLocalIds.clear();
927
+ // TODO: Determine if Promise.all is ergonomic at the callsite. Would Promise.allSettled be better?
928
+ await Promise.all<void>(
929
+ localIdsToUpload.map(async (localId) => this.uploadAndAttach(localId)),
930
+ );
931
+ };
932
+
860
933
  /**
861
934
  * To be used in getPendingLocalState flow. Get a serializable record of the blobs that are
862
935
  * pending upload and/or their BlobAttach op, which can be given to a new BlobManager to
863
936
  * resume work.
864
- *
865
- * @privateRemarks
866
- * For now, we don't track any pending blobs since the getPendingBlobs flow doesn't enable
867
- * restoring to a state where an accessible handle has been stored by the customer (and we'll
868
- * just drop any BlobAttach ops on the ground during reSubmit). However, once we add support
869
- * for payload-pending handles, this will return the blobs associated with those handles.
870
937
  */
871
938
  public getPendingBlobs(): IPendingBlobs | undefined {
872
- return undefined;
873
- }
874
-
875
- /**
876
- * Part of container serialization when imminent closure is enabled (Currently when calling closeAndGetPendingLocalState).
877
- * This asynchronous function resolves all pending createBlob calls and waits for each blob
878
- * to be attached. It will also send BlobAttach ops for each pending blob that hasn't sent it
879
- * yet so that serialized container can resubmit them when rehydrated.
880
- *
881
- * @param stopBlobAttachingSignal - Optional signal to abort the blob attaching process.
882
- * @returns - A promise that resolves with the details of the attached blobs,
883
- * or undefined if no blobs were processed.
884
- */
885
- public async attachAndGetPendingBlobs(
886
- stopBlobAttachingSignal?: AbortSignal,
887
- ): Promise<IPendingBlobs | undefined> {
888
- throw new UsageError("attachAndGetPendingBlobs is no longer supported");
939
+ const pendingBlobs: IPendingBlobs = {};
940
+ for (const localId of this.pendingBlobsWithAttachedHandles) {
941
+ const localBlobRecord = this.localBlobCache.get(localId);
942
+ assert(localBlobRecord !== undefined, 0xc83 /* Pending blob must be in local cache */);
943
+ assert(
944
+ localBlobRecord.state !== "attached",
945
+ 0xc84 /* Pending blob must not be in attached state */,
946
+ );
947
+ // We downgrade uploading blobs to localOnly, and attaching blobs to uploaded. In the case of
948
+ // uploading blobs, we don't have a way to retrieve the eventual storageId so the upload will
949
+ // need to be restarted anyway. In the case of attaching blobs, we can't know whether the
950
+ // BlobAttach op will eventually be ack'd. So we assume we'll need to send another op, but also
951
+ // remain prepared to handle seeing the ack of the original op after loading from pending state.
952
+ pendingBlobs[localId] =
953
+ localBlobRecord.state === "localOnly" || localBlobRecord.state === "uploading"
954
+ ? {
955
+ state: "localOnly",
956
+ blob: bufferToString(localBlobRecord.blob, "base64"),
957
+ }
958
+ : {
959
+ ...localBlobRecord,
960
+ state: "uploaded",
961
+ blob: bufferToString(localBlobRecord.blob, "base64"),
962
+ };
963
+ }
964
+ return Object.keys(pendingBlobs).length > 0 ? pendingBlobs : undefined;
889
965
  }
890
966
  }
891
967