@fluidframework/container-runtime 2.63.0-359461 → 2.63.0-359962

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.
@@ -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,31 @@ 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(!this.redirectTable.has(localId), "Pending blob already in redirect table");
338
+ const localBlobRecord = {
339
+ ...serializableBlobRecord,
340
+ blob: stringToBuffer(serializableBlobRecord.blob, "base64"),
341
+ };
342
+ this.localBlobCache.set(localId, localBlobRecord);
343
+ // Since we received these blobs from pending state, we'll assume they were only added to the
344
+ // pending state at generation time because their handles were attached. We add them back here
345
+ // in case we need to round-trip them back out again due to another getPendingBlobs() call.
346
+ this.pendingBlobsWithAttachedHandles.add(localId);
347
+ this.pendingOnlyLocalIds.add(localId);
293
348
  }
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
- });
349
+ }
304
350
  }
305
351
 
352
+ /**
353
+ * Returns whether a blob with the given localId can be retrieved by the BlobManager via getBlob().
354
+ */
306
355
  public hasBlob(localId: string): boolean {
307
- return this.redirectTable.get(localId) !== undefined;
356
+ return this.redirectTable.has(localId) || this.localBlobCache.has(localId);
308
357
  }
309
358
 
310
359
  /**
@@ -340,16 +389,17 @@ export class BlobManager {
340
389
  // is configured.
341
390
  this.blobRequested(getGCNodePathFromLocalId(localId));
342
391
 
343
- const pending = this.pendingBlobs.get(localId);
344
- if (pending) {
345
- return pending.blob;
392
+ const localBlobRecord = this.localBlobCache.get(localId);
393
+ if (localBlobRecord !== undefined) {
394
+ return localBlobRecord.blob;
346
395
  }
347
396
 
348
397
  let storageId = this.redirectTable.get(localId);
349
398
  if (storageId === undefined) {
350
399
  // Only blob handles explicitly marked with pending payload are permitted to exist without
351
400
  // yet knowing their storage id. Otherwise they must already be associated with a storage id.
352
- // Handles for detached blobs are not payload pending.
401
+ // Handles for detached blobs are not payload pending, though they should also always be present
402
+ // in the localBlobCache and therefore should never need to refer to storage.
353
403
  assert(payloadPending, 0x11f /* "requesting unknown blobs" */);
354
404
  // If we didn't find it in the redirectTable and it's payloadPending, assume the attach op is coming
355
405
  // eventually and wait. We do this even if the local client doesn't have the blob payloadPending flag
@@ -385,48 +435,25 @@ export class BlobManager {
385
435
  );
386
436
  }
387
437
 
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;
438
+ private getNonPayloadPendingBlobHandle(localId: string): BlobHandle {
439
+ const localBlobRecord = this.localBlobCache.get(localId);
440
+ assert(localBlobRecord !== undefined, 0x384 /* requesting handle for unknown blob */);
441
+ assert(localBlobRecord.state === "attached", "Expected blob to be attached");
442
+
403
443
  return new BlobHandle(
404
444
  getGCNodePathFromLocalId(localId),
405
445
  this.routeContext,
406
446
  async () => this.getBlob(localId, false),
407
447
  false, // payloadPending
408
- callback,
409
448
  );
410
449
  }
411
450
 
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
451
  public async createBlob(
425
452
  blob: ArrayBufferLike,
426
453
  signal?: AbortSignal,
427
454
  ): Promise<IFluidHandleInternalPayloadPending<ArrayBufferLike>> {
428
455
  if (this.runtime.attachState === AttachState.Detached) {
429
- return this.createBlobDetached(blob);
456
+ return this.createBlobDetached(blob, signal);
430
457
  }
431
458
  if (this.runtime.attachState === AttachState.Attaching) {
432
459
  // blob upload is not supported in "Attaching" state
@@ -439,48 +466,46 @@ export class BlobManager {
439
466
  );
440
467
 
441
468
  return this.createBlobPayloadPending
442
- ? this.createBlobWithPayloadPending(blob)
469
+ ? this.createBlobWithPayloadPending(blob, signal)
443
470
  : this.createBlobLegacy(blob, signal);
444
471
  }
445
472
 
446
- private async createBlobLegacy(
473
+ private async createBlobDetached(
447
474
  blob: ArrayBufferLike,
448
475
  signal?: AbortSignal,
449
476
  ): Promise<IFluidHandleInternalPayloadPending<ArrayBufferLike>> {
450
477
  if (signal?.aborted === true) {
451
- throw this.createAbortError();
478
+ throw createAbortError();
452
479
  }
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
480
  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 });
481
+ this.localBlobCache.set(localId, { state: "uploading", blob });
482
+ // Blobs created while the container is detached are stored in IDetachedBlobStorage.
483
+ // The 'IContainerStorageService.createBlob()' call below will respond with a pseudo storage ID.
484
+ // That pseudo storage ID will be replaced with the real storage ID at attach time.
485
+ const { id: detachedStorageId } = await this.storage.createBlob(blob);
486
+ // From the perspective of the BlobManager, the blob is now fully attached. The actual
487
+ // upload/attach process at container attach time is treated as opaque to this tracking.
488
+ this.localBlobCache.set(localId, { state: "attached", blob });
489
+ this.redirectTable.set(localId, detachedStorageId);
490
+ return this.getNonPayloadPendingBlobHandle(localId);
491
+ }
474
492
 
475
- return pendingEntry.handleP.promise.finally(() => {
476
- signal?.removeEventListener("abort", abortListener);
477
- });
493
+ private async createBlobLegacy(
494
+ blob: ArrayBufferLike,
495
+ signal?: AbortSignal,
496
+ ): Promise<IFluidHandleInternalPayloadPending<ArrayBufferLike>> {
497
+ const localId = this.localIdGenerator();
498
+ this.localBlobCache.set(localId, { state: "localOnly", blob });
499
+ await this.uploadAndAttach(localId, signal);
500
+ return this.getNonPayloadPendingBlobHandle(localId);
478
501
  }
479
502
 
480
503
  private createBlobWithPayloadPending(
481
504
  blob: ArrayBufferLike,
505
+ signal?: AbortSignal,
482
506
  ): IFluidHandleInternalPayloadPending<ArrayBufferLike> {
483
507
  const localId = this.localIdGenerator();
508
+ this.localBlobCache.set(localId, { state: "localOnly", blob });
484
509
 
485
510
  const blobHandle = new BlobHandle(
486
511
  getGCNodePathFromLocalId(localId),
@@ -488,157 +513,209 @@ export class BlobManager {
488
513
  async () => blob,
489
514
  true, // payloadPending
490
515
  () => {
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);
516
+ this.pendingBlobsWithAttachedHandles.add(localId);
517
+ const uploadAndAttachP = this.uploadAndAttach(localId, signal);
518
+ uploadAndAttachP.then(blobHandle.notifyShared).catch((error) => {
519
+ // TODO: notifyShared won't fail directly, but it emits an event to the customer.
520
+ // Consider what to do if the customer's code throws. reportError is nice.
521
+ });
522
+ uploadAndAttachP.catch(blobHandle.notifyFailed);
500
523
  },
501
524
  );
502
525
 
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
526
  return blobHandle;
520
527
  }
521
528
 
522
529
  /**
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).
530
+ * Upload and attach the localBlobCache entry for the given localId.
525
531
  *
526
- * @privateRemarks This method must not reject, as there is no error handling for it in current tracking.
532
+ * Expects the localBlobCache entry for the given localId to be in either localOnly or uploaded state
533
+ * when called. Returns a promise that resolves when the blob completes uploading and attaching, or else
534
+ * rejects if an error is encountered or the signal is aborted.
527
535
  */
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;
536
+ private readonly uploadAndAttach = async (
537
+ localId: string,
538
+ signal?: AbortSignal,
539
+ ): Promise<void> => {
540
+ if (signal?.aborted === true) {
541
+ this.localBlobCache.delete(localId);
542
+ this.pendingBlobsWithAttachedHandles.delete(localId);
543
+ throw createAbortError();
551
544
  }
545
+ const localBlobRecordInitial = this.localBlobCache.get(localId);
546
+ assert(
547
+ localBlobRecordInitial?.state === "localOnly" ||
548
+ localBlobRecordInitial?.state === "uploaded",
549
+ "Expect uploadAndAttach to be called with either localOnly or uploaded state",
550
+ );
551
+ const { blob } = localBlobRecordInitial;
552
552
 
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
- }
553
+ /**
554
+ * Expects the localBlobCache entry for the given localId to be in either localOnly or uploaded state
555
+ * when called. Returns a promise that resolves when the blob is in uploaded or attached state, or else
556
+ * rejects on error during upload or if the signal is aborted.
557
+ *
558
+ * Most of the time this should be expected to exit in uploaded state, but if we are loading from pending
559
+ * state we may see an attach op from the client that generated the pending state, which can complete the
560
+ * attach while the upload is outstanding.
561
+ */
562
+ const ensureUploaded = async (): Promise<void> => {
563
+ const localBlobRecord = this.localBlobCache.get(localId);
564
+ if (localBlobRecord?.state === "uploaded") {
565
+ // In normal creation flows, the blob will be in localOnly state here. But in the case of loading
566
+ // with pending state we can call it with an uploaded-but-not-attached blob. Start the upload
567
+ // flow only if it's localOnly.
568
+ return;
569
+ }
570
+ assert(
571
+ localBlobRecord?.state === "localOnly",
572
+ "Attempting to upload from unexpected state",
573
+ );
565
574
 
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
- }
575
+ this.localBlobCache.set(localId, { state: "uploading", blob });
576
+ await new Promise<void>((resolve, reject) => {
577
+ // If we eventually have driver-level support for abort, then this can simplify a bit as we won't
578
+ // need to track upload completion and abort separately. Until then, we need to handle the case that
579
+ // the upload continues and settles after becoming irrelevant due to signal abort or blob attach.
580
+ let uploadHasBecomeIrrelevant = false;
581
+ const onSignalAbort = (): void => {
582
+ removeListeners();
583
+ uploadHasBecomeIrrelevant = true;
584
+ this.localBlobCache.delete(localId);
585
+ this.pendingBlobsWithAttachedHandles.delete(localId);
586
+ reject(createAbortError());
587
+ };
588
+ const onProcessedBlobAttach = (_localId: string, _storageId: string): void => {
589
+ if (_localId === localId) {
590
+ removeListeners();
591
+ uploadHasBecomeIrrelevant = true;
592
+ resolve();
593
+ }
594
+ };
595
+ const removeListeners = (): void => {
596
+ this.internalEvents.off("processedBlobAttach", onProcessedBlobAttach);
597
+ signal?.removeEventListener("abort", onSignalAbort);
598
+ };
599
+ this.internalEvents.on("processedBlobAttach", onProcessedBlobAttach);
600
+ signal?.addEventListener("abort", onSignalAbort);
601
+
602
+ this.storage
603
+ .createBlob(blob)
604
+ .then((createBlobResponse: ICreateBlobResponseWithTTL) => {
605
+ if (!uploadHasBecomeIrrelevant) {
606
+ removeListeners();
607
+ this.localBlobCache.set(localId, {
608
+ state: "uploaded",
609
+ blob,
610
+ storageId: createBlobResponse.id,
611
+ uploadTime: Date.now(),
612
+ minTTLInSeconds: createBlobResponse.minTTLInSeconds,
613
+ });
614
+ resolve();
615
+ }
616
+ })
617
+ .catch((error) => {
618
+ if (!uploadHasBecomeIrrelevant) {
619
+ removeListeners();
620
+ // If the storage call errors, we can't recover. Reject to throw back to the caller.
621
+ this.localBlobCache.delete(localId);
622
+ this.pendingBlobsWithAttachedHandles.delete(localId);
623
+ reject(error);
624
+ }
625
+ });
626
+ });
627
+ };
573
628
 
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);
629
+ /**
630
+ * Expects the localBlobCache entry for the given localId to be in uploaded or attached state when called.
631
+ * Returns a promise that resolves to true if the blob is successfully attached, or false if it cannot be
632
+ * attached and the upload flow needs to be restarted from the top (currently only if the TTL expires before
633
+ * attach can be completed). In the latter case, the localBlobRecord will also be reset to localOnly state.
634
+ * The promise rejects if the signal is aborted.
635
+ */
636
+ const tryAttach = async (): Promise<boolean> => {
637
+ const localBlobRecord = this.localBlobCache.get(localId);
638
+ if (localBlobRecord?.state === "attached") {
639
+ // In normal creation flows, the blob will be in uploaded state here. But if we are loading from pending
640
+ // state and see an attach op from the client that generated the pending state, we may have reached
641
+ // attached state in the middle of the upload attempt. In that case there's no more work to do and we
642
+ // can just return.
643
+ return true;
579
644
  }
580
- }
581
- }
645
+ assert(
646
+ localBlobRecord?.state === "uploaded",
647
+ "Attempting to attach from unexpected state",
648
+ );
582
649
 
583
- private deletePendingBlob(id: string): void {
584
- this.pendingBlobs.delete(id);
585
- }
650
+ // If we just uploaded the blob TTL really shouldn't be expired at this location. But if we loaded from
651
+ // pending state, the upload may have happened some time far in the past and could be expired here.
652
+ if (isTTLTooCloseToExpiry(localBlobRecord)) {
653
+ // If the TTL is expired, we assume it's gone from the storage and so is effectively localOnly again.
654
+ // Then when we re-enter the loop, we'll re-upload it.
655
+ this.localBlobCache.set(localId, { state: "localOnly", blob });
656
+ // Emitting here isn't really necessary since the only listener would be attached below. Including here
657
+ // for completeness though, in case we add other listeners in the future.
658
+ this.internalEvents.emit("blobExpired", localId);
659
+ return false;
660
+ } else {
661
+ this.localBlobCache.set(localId, {
662
+ ...localBlobRecord,
663
+ state: "attaching",
664
+ });
586
665
 
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);
666
+ // Send and await a blob attach op. This serves two purposes:
667
+ // 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
668
+ // until its storage ID is added to the next summary.
669
+ // 2. It will create a local ID to storage ID mapping in all clients which is needed to retrieve the
670
+ // blob from the server via the storage ID.
671
+ return new Promise<boolean>((resolve, reject) => {
672
+ const onProcessedBlobAttach = (_localId: string, _storageId: string): void => {
673
+ if (_localId === localId) {
674
+ removeListeners();
675
+ resolve(true);
676
+ }
677
+ };
678
+ // Although we already checked for TTL expiry above, the op we're about to send may later be asked
679
+ // to resubmit. Before we resubmit, we check again for TTL expiry - this listener is how we learn if
680
+ // we discovered expiry in the resubmit flow.
681
+ const onBlobExpired = (_localId: string): void => {
682
+ if (_localId === localId) {
683
+ removeListeners();
684
+ resolve(false);
685
+ }
686
+ };
687
+ const onSignalAbort = (): void => {
688
+ removeListeners();
689
+ this.localBlobCache.delete(localId);
690
+ this.pendingBlobsWithAttachedHandles.delete(localId);
691
+ reject(createAbortError());
692
+ };
693
+ const removeListeners = (): void => {
694
+ this.internalEvents.off("processedBlobAttach", onProcessedBlobAttach);
695
+ this.internalEvents.off("blobExpired", onBlobExpired);
696
+ signal?.removeEventListener("abort", onSignalAbort);
697
+ };
698
+
699
+ this.internalEvents.on("processedBlobAttach", onProcessedBlobAttach);
700
+ this.internalEvents.on("blobExpired", onBlobExpired);
701
+ signal?.addEventListener("abort", onSignalAbort);
702
+ this.sendBlobAttachOp(localId, localBlobRecord.storageId);
703
+ });
635
704
  }
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);
705
+ };
706
+
707
+ let attachCompleted = false;
708
+ while (!attachCompleted) {
709
+ await ensureUploaded();
710
+ attachCompleted = await tryAttach();
711
+
712
+ // If something stopped the attach from completing successfully (currently just TTL expiry),
713
+ // we expect that the blob was already updated to reflect the updated state (i.e. back to localOnly)
714
+ // and we'll try the loop again from the top.
639
715
  }
640
- return response;
641
- }
716
+ // When the blob successfully attaches, the localBlobRecord will have been updated to attached state
717
+ // at the time we processed the op, so there's nothing else to do here.
718
+ };
642
719
 
643
720
  /**
644
721
  * Resubmit a BlobAttach op. Used to add storage IDs to ops that were
@@ -648,14 +725,22 @@ export class BlobManager {
648
725
  public reSubmit(metadata: Record<string, unknown> | undefined): void {
649
726
  assert(isBlobMetadata(metadata), 0xc01 /* Expected blob metadata for a BlobAttach op */);
650
727
  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);
728
+ // Any blob that we're actively trying to advance to attached state must be in attaching state.
729
+ // Decline to resubmit for anything else.
730
+ // For example, we might be asked to resubmit stashed ops for blobs that never had their handle
731
+ // attached - these won't have a localBlobCache entry because we filter them out when generating
732
+ // pending state. We shouldn't try to attach them since they won't be accessible to the customer
733
+ // and would just be considered garbage immediately.
734
+ // TODO: Is it possible that we'd be asked to resubmit for a pending blob before we call sharePendingBlobs?
735
+ const localBlobRecord = this.localBlobCache.get(localId);
736
+ if (localBlobRecord?.state === "attaching") {
737
+ // If the TTL is expired, we assume it's gone from the storage and so is effectively localOnly again.
738
+ if (isTTLTooCloseToExpiry(localBlobRecord)) {
739
+ this.localBlobCache.set(localId, { state: "localOnly", blob: localBlobRecord.blob });
740
+ this.internalEvents.emit("blobExpired", localId);
741
+ } else {
742
+ this.sendBlobAttachOp(localId, storageId);
743
+ }
659
744
  }
660
745
  }
661
746
 
@@ -665,46 +750,25 @@ export class BlobManager {
665
750
  0xc02 /* Expected blob metadata for a BlobAttach op */,
666
751
  );
667
752
  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;
753
+ const maybeLocalBlobRecord = this.localBlobCache.get(localId);
754
+ if (maybeLocalBlobRecord !== undefined) {
755
+ const attachedBlobRecord: AttachedBlob = {
756
+ state: "attached",
757
+ blob: maybeLocalBlobRecord.blob,
758
+ };
759
+ // Processing a blob attach op is authoritative and may stomp on any existing state. Other
760
+ // callsites that update localBlobCache entries must take proper caution to handle the case
761
+ // that a blob attach op is processed concurrently.
762
+ this.localBlobCache.set(localId, attachedBlobRecord);
763
+ // Note there may or may not be an entry in pendingBlobsWithAttachedHandles for this localId,
764
+ // in particular for the non-payloadPending case since we should be reaching this point
765
+ // before even returning a handle to the caller.
766
+ this.pendingBlobsWithAttachedHandles.delete(localId);
767
+ this.pendingOnlyLocalIds.delete(localId);
672
768
  }
673
-
674
- this.setRedirection(localId, storageId);
769
+ this.redirectTable.set(localId, storageId);
675
770
  // 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
- }
771
+ this.redirectTable.set(storageId, storageId);
708
772
  this.internalEvents.emit("processedBlobAttach", localId, storageId);
709
773
  }
710
774
 
@@ -732,19 +796,9 @@ export class BlobManager {
732
796
  return gcData;
733
797
  }
734
798
 
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
799
  /**
747
800
  * Delete blobs with the given routes from the redirect table.
801
+ * @returns The routes of blobs that were deleted.
748
802
  *
749
803
  * @remarks
750
804
  * The routes are GC nodes paths of format -`/<blobManagerBasePath>/<localId>`.
@@ -759,11 +813,11 @@ export class BlobManager {
759
813
  * will ensure we don't create an attachment blob for them at the next summary. The service would then delete them
760
814
  * some time in the future.
761
815
  */
762
- private deleteBlobsFromRedirectTable(blobRoutes: readonly string[]): void {
816
+ public deleteSweepReadyNodes(sweepReadyBlobRoutes: readonly string[]): readonly string[] {
763
817
  // maybeUnusedStorageIds is used to compute the set of storage IDs that *used to have a local ID*, but that
764
818
  // local ID is being deleted.
765
819
  const maybeUnusedStorageIds: Set<string> = new Set();
766
- for (const route of blobRoutes) {
820
+ for (const route of sweepReadyBlobRoutes) {
767
821
  const localId = getLocalIdFromGCNodePath(route);
768
822
  // If the blob hasn't already been deleted, log an error because this should never happen.
769
823
  // If the blob has already been deleted, log a telemetry event. This can happen because multiple GC
@@ -799,6 +853,7 @@ export class BlobManager {
799
853
  for (const storageId of maybeUnusedStorageIds) {
800
854
  this.redirectTable.delete(storageId);
801
855
  }
856
+ return [...sweepReadyBlobRoutes];
802
857
  }
803
858
 
804
859
  /**
@@ -851,41 +906,59 @@ export class BlobManager {
851
906
  for (const [localId, detachedStorageId] of redirectTableEntries) {
852
907
  const newStorageId = detachedStorageTable.get(detachedStorageId);
853
908
  assert(newStorageId !== undefined, 0xc53 /* Couldn't find a matching storage ID */);
854
- this.setRedirection(localId, newStorageId);
909
+ this.redirectTable.set(localId, newStorageId);
855
910
  // set identity (id -> id) entry
856
- this.setRedirection(newStorageId, newStorageId);
911
+ this.redirectTable.set(newStorageId, newStorageId);
857
912
  }
858
913
  };
859
914
 
915
+ /**
916
+ * Upload and attach any pending blobs that the BlobManager was loaded with that have not already
917
+ * been attached in the meantime.
918
+ * @returns A promise that resolves when all the uploads and attaches have completed, or rejects
919
+ * if any of them fail.
920
+ */
921
+ public readonly sharePendingBlobs = async (): Promise<void> => {
922
+ const localIdsToUpload = [...this.pendingOnlyLocalIds];
923
+ this.pendingOnlyLocalIds.clear();
924
+ // TODO: Determine if Promise.all is ergonomic at the callsite. Would Promise.allSettled be better?
925
+ await Promise.all<void>(
926
+ localIdsToUpload.map(async (localId) => this.uploadAndAttach(localId)),
927
+ );
928
+ };
929
+
860
930
  /**
861
931
  * To be used in getPendingLocalState flow. Get a serializable record of the blobs that are
862
932
  * pending upload and/or their BlobAttach op, which can be given to a new BlobManager to
863
933
  * 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
934
  */
871
935
  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");
936
+ const pendingBlobs: IPendingBlobs = {};
937
+ for (const localId of this.pendingBlobsWithAttachedHandles) {
938
+ const localBlobRecord = this.localBlobCache.get(localId);
939
+ assert(localBlobRecord !== undefined, "Pending blob must be in local cache");
940
+ assert(
941
+ localBlobRecord.state === "uploading" ||
942
+ localBlobRecord.state === "uploaded" ||
943
+ localBlobRecord.state === "attaching",
944
+ "Pending blob must be in uploading, uploaded, or attaching state",
945
+ );
946
+ // We treat blobs in both uploaded and attaching state as just being uploaded, to distrust
947
+ // whether we expect to see any ack for a BlobAttach op. We just remain prepared to handle
948
+ // a BlobAttach op for it if we do see one after loading from pending state.
949
+ pendingBlobs[localId] =
950
+ localBlobRecord.state === "uploading"
951
+ ? {
952
+ state: "localOnly",
953
+ blob: bufferToString(localBlobRecord.blob, "base64"),
954
+ }
955
+ : {
956
+ ...localBlobRecord,
957
+ state: "uploaded",
958
+ blob: bufferToString(localBlobRecord.blob, "base64"),
959
+ };
960
+ }
961
+ return Object.keys(pendingBlobs).length > 0 ? pendingBlobs : undefined;
889
962
  }
890
963
  }
891
964