@fluidframework/container-runtime 1.2.2 → 2.0.0-internal.1.0.0.82159

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