@fluidframework/container-runtime 1.2.7 → 2.0.0-dev.1.3.0.96595
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.
- package/.mocharc.js +12 -0
- package/dist/batchManager.d.ts +37 -0
- package/dist/batchManager.d.ts.map +1 -0
- package/dist/batchManager.js +73 -0
- package/dist/batchManager.js.map +1 -0
- package/dist/batchTracker.d.ts +1 -2
- package/dist/batchTracker.d.ts.map +1 -1
- package/dist/batchTracker.js +2 -3
- package/dist/batchTracker.js.map +1 -1
- package/dist/blobManager.d.ts +87 -25
- package/dist/blobManager.d.ts.map +1 -1
- package/dist/blobManager.js +317 -99
- package/dist/blobManager.js.map +1 -1
- package/dist/containerRuntime.d.ts +109 -124
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +349 -542
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStore.js +29 -24
- package/dist/dataStore.js.map +1 -1
- package/dist/dataStoreContext.d.ts +20 -14
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +49 -58
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/dataStores.d.ts +12 -5
- package/dist/dataStores.d.ts.map +1 -1
- package/dist/dataStores.js +21 -20
- package/dist/dataStores.js.map +1 -1
- package/dist/deltaScheduler.d.ts +6 -4
- package/dist/deltaScheduler.d.ts.map +1 -1
- package/dist/deltaScheduler.js +6 -4
- package/dist/deltaScheduler.js.map +1 -1
- package/dist/garbageCollection.d.ts +74 -14
- package/dist/garbageCollection.d.ts.map +1 -1
- package/dist/garbageCollection.js +249 -170
- package/dist/garbageCollection.js.map +1 -1
- package/dist/gcSweepReadyUsageDetection.d.ts +53 -0
- package/dist/gcSweepReadyUsageDetection.d.ts.map +1 -0
- package/dist/gcSweepReadyUsageDetection.js +126 -0
- package/dist/gcSweepReadyUsageDetection.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/opProperties.d.ts +7 -0
- package/dist/opProperties.d.ts.map +1 -0
- package/dist/opProperties.js +20 -0
- package/dist/opProperties.js.map +1 -0
- package/dist/orderedClientElection.d.ts +28 -10
- package/dist/orderedClientElection.d.ts.map +1 -1
- package/dist/orderedClientElection.js +14 -4
- package/dist/orderedClientElection.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/pendingStateManager.d.ts +0 -11
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +24 -46
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/runningSummarizer.d.ts +14 -4
- package/dist/runningSummarizer.d.ts.map +1 -1
- package/dist/runningSummarizer.js +68 -26
- package/dist/runningSummarizer.js.map +1 -1
- package/dist/scheduleManager.d.ts +31 -0
- package/dist/scheduleManager.d.ts.map +1 -0
- package/dist/scheduleManager.js +243 -0
- package/dist/scheduleManager.js.map +1 -0
- package/dist/summarizer.d.ts +0 -2
- package/dist/summarizer.d.ts.map +1 -1
- package/dist/summarizer.js +1 -12
- package/dist/summarizer.js.map +1 -1
- package/dist/summarizerHeuristics.d.ts +26 -4
- package/dist/summarizerHeuristics.d.ts.map +1 -1
- package/dist/summarizerHeuristics.js +95 -18
- package/dist/summarizerHeuristics.js.map +1 -1
- package/dist/summarizerTypes.d.ts +45 -18
- package/dist/summarizerTypes.d.ts.map +1 -1
- package/dist/summarizerTypes.js +1 -1
- package/dist/summarizerTypes.js.map +1 -1
- package/dist/summaryCollection.d.ts +1 -0
- package/dist/summaryCollection.d.ts.map +1 -1
- package/dist/summaryCollection.js +31 -15
- package/dist/summaryCollection.js.map +1 -1
- package/dist/summaryFormat.d.ts +0 -5
- package/dist/summaryFormat.d.ts.map +1 -1
- package/dist/summaryFormat.js.map +1 -1
- package/dist/summaryGenerator.d.ts +1 -0
- package/dist/summaryGenerator.d.ts.map +1 -1
- package/dist/summaryGenerator.js +11 -9
- package/dist/summaryGenerator.js.map +1 -1
- package/dist/summaryManager.d.ts +2 -2
- package/dist/summaryManager.d.ts.map +1 -1
- package/dist/summaryManager.js +22 -7
- package/dist/summaryManager.js.map +1 -1
- package/lib/batchManager.d.ts +37 -0
- package/lib/batchManager.d.ts.map +1 -0
- package/lib/batchManager.js +69 -0
- package/lib/batchManager.js.map +1 -0
- package/lib/batchTracker.d.ts +1 -2
- package/lib/batchTracker.d.ts.map +1 -1
- package/lib/batchTracker.js +2 -3
- package/lib/batchTracker.js.map +1 -1
- package/lib/blobManager.d.ts +87 -25
- package/lib/blobManager.d.ts.map +1 -1
- package/lib/blobManager.js +319 -101
- package/lib/blobManager.js.map +1 -1
- package/lib/containerRuntime.d.ts +109 -124
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +355 -547
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStore.js +29 -24
- package/lib/dataStore.js.map +1 -1
- package/lib/dataStoreContext.d.ts +20 -14
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +46 -55
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/dataStores.d.ts +12 -5
- package/lib/dataStores.d.ts.map +1 -1
- package/lib/dataStores.js +21 -20
- package/lib/dataStores.js.map +1 -1
- package/lib/deltaScheduler.d.ts +6 -4
- package/lib/deltaScheduler.d.ts.map +1 -1
- package/lib/deltaScheduler.js +6 -4
- package/lib/deltaScheduler.js.map +1 -1
- package/lib/garbageCollection.d.ts +74 -14
- package/lib/garbageCollection.d.ts.map +1 -1
- package/lib/garbageCollection.js +238 -160
- package/lib/garbageCollection.js.map +1 -1
- package/lib/gcSweepReadyUsageDetection.d.ts +53 -0
- package/lib/gcSweepReadyUsageDetection.d.ts.map +1 -0
- package/lib/gcSweepReadyUsageDetection.js +121 -0
- package/lib/gcSweepReadyUsageDetection.js.map +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/opProperties.d.ts +7 -0
- package/lib/opProperties.d.ts.map +1 -0
- package/lib/opProperties.js +16 -0
- package/lib/opProperties.js.map +1 -0
- package/lib/orderedClientElection.d.ts +28 -10
- package/lib/orderedClientElection.d.ts.map +1 -1
- package/lib/orderedClientElection.js +14 -4
- package/lib/orderedClientElection.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.d.ts.map +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/pendingStateManager.d.ts +0 -11
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +24 -46
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/runningSummarizer.d.ts +14 -4
- package/lib/runningSummarizer.d.ts.map +1 -1
- package/lib/runningSummarizer.js +68 -26
- package/lib/runningSummarizer.js.map +1 -1
- package/lib/scheduleManager.d.ts +31 -0
- package/lib/scheduleManager.d.ts.map +1 -0
- package/lib/scheduleManager.js +239 -0
- package/lib/scheduleManager.js.map +1 -0
- package/lib/summarizer.d.ts +0 -2
- package/lib/summarizer.d.ts.map +1 -1
- package/lib/summarizer.js +1 -12
- package/lib/summarizer.js.map +1 -1
- package/lib/summarizerHeuristics.d.ts +26 -4
- package/lib/summarizerHeuristics.d.ts.map +1 -1
- package/lib/summarizerHeuristics.js +95 -18
- package/lib/summarizerHeuristics.js.map +1 -1
- package/lib/summarizerTypes.d.ts +45 -18
- package/lib/summarizerTypes.d.ts.map +1 -1
- package/lib/summarizerTypes.js +1 -1
- package/lib/summarizerTypes.js.map +1 -1
- package/lib/summaryCollection.d.ts +1 -0
- package/lib/summaryCollection.d.ts.map +1 -1
- package/lib/summaryCollection.js +31 -15
- package/lib/summaryCollection.js.map +1 -1
- package/lib/summaryFormat.d.ts +0 -5
- package/lib/summaryFormat.d.ts.map +1 -1
- package/lib/summaryFormat.js.map +1 -1
- package/lib/summaryGenerator.d.ts +1 -0
- package/lib/summaryGenerator.d.ts.map +1 -1
- package/lib/summaryGenerator.js +11 -9
- package/lib/summaryGenerator.js.map +1 -1
- package/lib/summaryManager.d.ts +2 -2
- package/lib/summaryManager.d.ts.map +1 -1
- package/lib/summaryManager.js +22 -7
- package/lib/summaryManager.js.map +1 -1
- package/package.json +65 -24
- package/src/batchManager.ts +91 -0
- package/src/batchTracker.ts +2 -3
- package/src/blobManager.ts +385 -118
- package/src/containerRuntime.ts +529 -740
- package/src/dataStore.ts +49 -37
- package/src/dataStoreContext.ts +44 -56
- package/src/dataStores.ts +34 -30
- package/src/deltaScheduler.ts +6 -4
- package/src/garbageCollection.ts +297 -206
- package/src/gcSweepReadyUsageDetection.ts +139 -0
- package/src/index.ts +1 -2
- package/src/opProperties.ts +19 -0
- package/src/orderedClientElection.ts +31 -10
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +27 -59
- package/src/runningSummarizer.ts +75 -22
- package/src/scheduleManager.ts +314 -0
- package/src/summarizer.ts +1 -18
- package/src/summarizerHeuristics.ts +133 -19
- package/src/summarizerTypes.ts +53 -18
- package/src/summaryCollection.ts +33 -18
- package/src/summaryFormat.ts +0 -6
- package/src/summaryGenerator.ts +40 -22
- package/src/summaryManager.ts +22 -7
- package/dist/opTelemetry.d.ts +0 -22
- package/dist/opTelemetry.d.ts.map +0 -1
- package/dist/opTelemetry.js +0 -59
- package/dist/opTelemetry.js.map +0 -1
- package/lib/opTelemetry.d.ts +0 -22
- package/lib/opTelemetry.d.ts.map +0 -1
- package/lib/opTelemetry.js +0 -55
- package/lib/opTelemetry.js.map +0 -1
- package/src/opTelemetry.ts +0 -71
package/lib/blobManager.js
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
+
import { v4 as uuid } from "uuid";
|
|
5
6
|
import { generateHandleContextPath, SummaryTreeBuilder } from "@fluidframework/runtime-utils";
|
|
6
|
-
import { assert, Deferred } from "@fluidframework/common-utils";
|
|
7
|
+
import { assert, bufferToString, Deferred, stringToBuffer } from "@fluidframework/common-utils";
|
|
7
8
|
import { AttachState } from "@fluidframework/container-definitions";
|
|
8
|
-
import { PerformanceEvent } from "@fluidframework/telemetry-utils";
|
|
9
|
+
import { ChildLogger, PerformanceEvent } from "@fluidframework/telemetry-utils";
|
|
10
|
+
import { Throttler, formExponentialFn } from "./throttler";
|
|
9
11
|
/**
|
|
10
12
|
* This class represents blob (long string)
|
|
11
13
|
* This object is used only when creating (writing) new blob and serialization purposes.
|
|
@@ -32,33 +34,111 @@ export class BlobHandle {
|
|
|
32
34
|
throw new Error("Cannot bind to blob handle");
|
|
33
35
|
}
|
|
34
36
|
}
|
|
37
|
+
class CancellableThrottler {
|
|
38
|
+
constructor(throttler) {
|
|
39
|
+
this.throttler = throttler;
|
|
40
|
+
this.cancelP = new Deferred();
|
|
41
|
+
}
|
|
42
|
+
async getDelay() {
|
|
43
|
+
return Promise.race([
|
|
44
|
+
this.cancelP.promise,
|
|
45
|
+
new Promise((resolve) => setTimeout(resolve, this.throttler.getDelay())),
|
|
46
|
+
]);
|
|
47
|
+
}
|
|
48
|
+
cancel() {
|
|
49
|
+
this.cancelP.resolve();
|
|
50
|
+
this.cancelP = new Deferred();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Note that while offline we "submit" an op before uploading the blob, but we always
|
|
54
|
+
// expect blobs to be uploaded before we actually see the op round-trip
|
|
55
|
+
var PendingBlobStatus;
|
|
56
|
+
(function (PendingBlobStatus) {
|
|
57
|
+
PendingBlobStatus[PendingBlobStatus["OnlinePendingUpload"] = 0] = "OnlinePendingUpload";
|
|
58
|
+
PendingBlobStatus[PendingBlobStatus["OnlinePendingOp"] = 1] = "OnlinePendingOp";
|
|
59
|
+
PendingBlobStatus[PendingBlobStatus["OfflinePendingUpload"] = 2] = "OfflinePendingUpload";
|
|
60
|
+
PendingBlobStatus[PendingBlobStatus["OfflinePendingOp"] = 3] = "OfflinePendingOp";
|
|
61
|
+
})(PendingBlobStatus || (PendingBlobStatus = {}));
|
|
35
62
|
export class BlobManager {
|
|
36
|
-
constructor(routeContext, snapshot, getStorage,
|
|
63
|
+
constructor(routeContext, snapshot, getStorage,
|
|
64
|
+
/**
|
|
65
|
+
* Submit a BlobAttach op. When a blob is uploaded, there is a short grace period before which
|
|
66
|
+
* the blob is deleted. The BlobAttach op notifies the server that blob is in use. The server
|
|
67
|
+
* will then not delete the blob as long as it is listed as referenced in future summaries.
|
|
68
|
+
* The summarizing client will know to include the storage ID in the summary when it sees the op.
|
|
69
|
+
*
|
|
70
|
+
* The op may also include a local ID to inform all clients of the relation to the storage
|
|
71
|
+
* ID, without knowledge of which they cannot request the blob from storage. This is also
|
|
72
|
+
* included in the redirect table in the summary.
|
|
73
|
+
*/
|
|
74
|
+
sendBlobAttachOp,
|
|
37
75
|
// To be called when a blob node is requested. blobPath is the path of the blob's node in GC's graph. It's
|
|
38
76
|
// of the format `/<BlobManager.basePath>/<blobId>`.
|
|
39
|
-
gcNodeUpdated, runtime,
|
|
77
|
+
gcNodeUpdated, runtime, stashedBlobs = {}) {
|
|
40
78
|
this.routeContext = routeContext;
|
|
41
79
|
this.getStorage = getStorage;
|
|
42
|
-
this.
|
|
80
|
+
this.sendBlobAttachOp = sendBlobAttachOp;
|
|
43
81
|
this.gcNodeUpdated = gcNodeUpdated;
|
|
44
82
|
this.runtime = runtime;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
this.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Blobs which have not been uploaded or for which we have not yet seen a BlobAttach op round-trip.
|
|
85
|
+
* Until we see the op round-trip, there is a possibility we may need to re-upload the blob, so
|
|
86
|
+
* we must save it. This is true for both the online and offline flow.
|
|
87
|
+
*/
|
|
88
|
+
this.pendingBlobs = new Map();
|
|
89
|
+
/**
|
|
90
|
+
* Track ops in flight for online flow. Used to avoid searching pendingBlobs since BlobAttach ops
|
|
91
|
+
* don't include local ID in online flow.
|
|
92
|
+
*/
|
|
93
|
+
this.opsInFlight = new Map();
|
|
94
|
+
this.retryThrottler = new CancellableThrottler(new Throttler(60 * 1000, // 60 sec delay window
|
|
95
|
+
30 * 1000, // 30 sec max delay
|
|
96
|
+
// throttling function increases exponentially (0ms, 40ms, 80ms, 160ms, etc)
|
|
97
|
+
formExponentialFn({ coefficient: 20, initialDelay: 0 })));
|
|
98
|
+
this.logger = ChildLogger.create(this.runtime.logger, "BlobManager");
|
|
99
|
+
this.runtime.on("disconnected", () => this.onDisconnected());
|
|
100
|
+
this.redirectTable = this.load(snapshot);
|
|
101
|
+
// Begin uploading stashed blobs from previous container instance
|
|
102
|
+
Object.entries(stashedBlobs).forEach(([localId, entry]) => {
|
|
103
|
+
const blob = stringToBuffer(entry.blob, "base64");
|
|
104
|
+
this.pendingBlobs.set(localId, {
|
|
105
|
+
blob,
|
|
106
|
+
status: PendingBlobStatus.OfflinePendingUpload,
|
|
107
|
+
handleP: new Deferred(),
|
|
108
|
+
uploadP: this.uploadBlob(localId, blob),
|
|
109
|
+
});
|
|
57
110
|
});
|
|
58
|
-
this.load(snapshot);
|
|
59
111
|
}
|
|
60
|
-
|
|
61
|
-
return
|
|
112
|
+
get pendingOfflineUploads() {
|
|
113
|
+
return Array.from(this.pendingBlobs.values())
|
|
114
|
+
.filter((e) => e.status === PendingBlobStatus.OfflinePendingUpload);
|
|
115
|
+
}
|
|
116
|
+
get hasPendingOfflineUploads() {
|
|
117
|
+
return this.pendingOfflineUploads.length > 0;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Upload blobs added while offline. This must be completed before connecting and resubmitting ops.
|
|
121
|
+
*/
|
|
122
|
+
async onConnected() {
|
|
123
|
+
this.retryThrottler.cancel();
|
|
124
|
+
const pendingUploads = this.pendingOfflineUploads.map(async (e) => e.uploadP);
|
|
125
|
+
await PerformanceEvent.timedExecAsync(this.logger, {
|
|
126
|
+
eventName: "BlobUploadOnConnected",
|
|
127
|
+
count: pendingUploads.length,
|
|
128
|
+
}, async () => Promise.all(pendingUploads), { start: true, end: true });
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Transition online blobs waiting for BlobAttach op round-trip since we will not see the op until we are connected
|
|
132
|
+
* again
|
|
133
|
+
*/
|
|
134
|
+
onDisconnected() {
|
|
135
|
+
for (const [localId, entry] of this.pendingBlobs) {
|
|
136
|
+
if (entry.status === PendingBlobStatus.OnlinePendingOp) {
|
|
137
|
+
// This will submit another BlobAttach op for this blob. This is necessary because the one we sent
|
|
138
|
+
// already didn't have the local ID.
|
|
139
|
+
this.transitionToOffline(localId);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
62
142
|
}
|
|
63
143
|
/**
|
|
64
144
|
* For a blobId, returns its path in GC's graph. The node path is of the format `/<BlobManager.basePath>/<blobId>`
|
|
@@ -68,61 +148,200 @@ export class BlobManager {
|
|
|
68
148
|
getBlobGCNodePath(blobId) {
|
|
69
149
|
return `/${BlobManager.basePath}/${blobId}`;
|
|
70
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Set of actual storage IDs (i.e., IDs that can be requested from storage). This will be empty if the container is
|
|
153
|
+
* detached or there are no (non-pending) attachment blobs in the document
|
|
154
|
+
*/
|
|
155
|
+
get storageIds() {
|
|
156
|
+
const ids = new Set(this.redirectTable.values());
|
|
157
|
+
// If we are detached, we will not have storage IDs, only undefined
|
|
158
|
+
const undefinedValueInTable = ids.delete(undefined);
|
|
159
|
+
// For a detached container, entries are inserted into the redirect table with an undefined storage ID.
|
|
160
|
+
// For an attached container, entries are inserted w/storage ID after the BlobAttach op round-trips.
|
|
161
|
+
assert(!undefinedValueInTable || this.runtime.attachState === AttachState.Detached && ids.size === 0, 0x382 /* 'redirectTable' must contain only undefined while detached / defined values while attached */);
|
|
162
|
+
return ids;
|
|
163
|
+
}
|
|
71
164
|
async getBlob(blobId) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
165
|
+
const pending = this.pendingBlobs.get(blobId);
|
|
166
|
+
if (pending) {
|
|
167
|
+
return pending.blob;
|
|
168
|
+
}
|
|
169
|
+
let storageId;
|
|
170
|
+
if (this.runtime.attachState === AttachState.Detached) {
|
|
171
|
+
assert(this.redirectTable.has(blobId), 0x383 /* requesting unknown blobs */);
|
|
172
|
+
// Blobs created while the container is detached are stored in IDetachedBlobStorage.
|
|
173
|
+
// The 'IDocumentStorageService.readBlob()' call below will retrieve these via localId.
|
|
174
|
+
storageId = blobId;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
storageId = this.redirectTable.get(blobId);
|
|
178
|
+
assert(!!storageId, 0x11f /* "requesting unknown blobs" */);
|
|
179
|
+
}
|
|
180
|
+
// When a GC-able (not pending) blob is retrieved, let runtime know that the corresponding GC node got updated.
|
|
76
181
|
this.gcNodeUpdated(this.getBlobGCNodePath(blobId));
|
|
77
|
-
return
|
|
182
|
+
return PerformanceEvent.timedExecAsync(this.logger, { eventName: "AttachmentReadBlob", id: storageId }, async () => {
|
|
78
183
|
return this.getStorage().readBlob(storageId);
|
|
79
|
-
}, { end: true, cancel: "error" })
|
|
184
|
+
}, { end: true, cancel: "error" });
|
|
185
|
+
}
|
|
186
|
+
getBlobHandle(id) {
|
|
187
|
+
assert(this.redirectTable.has(id) || this.pendingBlobs.has(id), 0x384 /* requesting handle for unknown blob */);
|
|
188
|
+
return new BlobHandle(`${BlobManager.basePath}/${id}`, this.routeContext, async () => this.getBlob(id));
|
|
189
|
+
}
|
|
190
|
+
async createBlobDetached(blob) {
|
|
191
|
+
// Blobs created while the container is detached are stored in IDetachedBlobStorage.
|
|
192
|
+
// The 'IDocumentStorageService.createBlob()' call below will respond with a localId.
|
|
193
|
+
const response = await this.getStorage().createBlob(blob);
|
|
194
|
+
this.redirectTable.set(response.id, undefined);
|
|
195
|
+
return this.getBlobHandle(response.id);
|
|
80
196
|
}
|
|
81
197
|
async createBlob(blob) {
|
|
82
|
-
|
|
198
|
+
if (this.runtime.attachState === AttachState.Detached) {
|
|
199
|
+
return this.createBlobDetached(blob);
|
|
200
|
+
}
|
|
83
201
|
if (this.runtime.attachState === AttachState.Attaching) {
|
|
84
202
|
// blob upload is not supported in "Attaching" state
|
|
85
203
|
this.logger.sendTelemetryEvent({ eventName: "CreateBlobWhileAttaching" });
|
|
86
204
|
await new Promise((resolve) => this.runtime.once("attached", resolve));
|
|
87
205
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
206
|
+
assert(this.runtime.attachState === AttachState.Attached, 0x385 /* For clarity and paranoid defense against adding future attachment states */);
|
|
207
|
+
// Create a local ID for each blob. This is used to support blobs if/when the client goes
|
|
208
|
+
// offline since we don't have the ID from storage yet. If online flow succeeds this won't be used.
|
|
209
|
+
const localId = uuid();
|
|
210
|
+
const pendingEntry = {
|
|
211
|
+
blob,
|
|
212
|
+
status: PendingBlobStatus.OnlinePendingUpload,
|
|
213
|
+
handleP: new Deferred(),
|
|
214
|
+
uploadP: this.uploadBlob(localId, blob),
|
|
215
|
+
};
|
|
216
|
+
this.pendingBlobs.set(localId, pendingEntry);
|
|
217
|
+
return pendingEntry.handleP.promise;
|
|
218
|
+
}
|
|
219
|
+
async uploadBlob(localId, blob) {
|
|
220
|
+
return PerformanceEvent.timedExecAsync(this.logger, { eventName: "createBlob" }, async () => this.getStorage().createBlob(blob), { end: true, cancel: this.runtime.connected ? "error" : "generic" }).then((response) => this.onUploadResolve(localId, response), async (err) => this.onUploadReject(localId, err));
|
|
221
|
+
}
|
|
222
|
+
onUploadResolve(localId, response) {
|
|
223
|
+
var _a;
|
|
224
|
+
const entry = this.pendingBlobs.get(localId);
|
|
225
|
+
assert((entry === null || entry === void 0 ? void 0 : entry.status) === PendingBlobStatus.OnlinePendingUpload ||
|
|
226
|
+
(entry === null || entry === void 0 ? void 0 : entry.status) === PendingBlobStatus.OfflinePendingUpload, 0x386 /* Must have pending blob entry for uploaded blob */);
|
|
227
|
+
entry.storageId = response.id;
|
|
228
|
+
if (this.runtime.connected) {
|
|
229
|
+
if (entry.status === PendingBlobStatus.OnlinePendingUpload) {
|
|
230
|
+
if (this.storageIds.has(response.id)) {
|
|
231
|
+
// Storage may dedupe blobs and give us an ID we already know
|
|
232
|
+
// no need to submit BlobAttach op in this case
|
|
233
|
+
entry.handleP.resolve(this.getBlobHandle(response.id));
|
|
234
|
+
this.pendingBlobs.delete(localId);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
// Check for still-pending duplicates too; if an op is already in flight we can wait for that one
|
|
238
|
+
if (!this.opsInFlight.has(response.id)) {
|
|
239
|
+
this.sendBlobAttachOp(response.id);
|
|
240
|
+
}
|
|
241
|
+
this.opsInFlight.set(response.id, ((_a = this.opsInFlight.get(response.id)) !== null && _a !== void 0 ? _a : []).concat(localId));
|
|
242
|
+
entry.status = PendingBlobStatus.OnlinePendingOp;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else if (entry.status === PendingBlobStatus.OfflinePendingUpload) {
|
|
246
|
+
// We already submitted a BlobAttach op for this blob when it was transitioned to offline flow
|
|
247
|
+
entry.status = PendingBlobStatus.OfflinePendingOp;
|
|
248
|
+
}
|
|
102
249
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
250
|
+
else {
|
|
251
|
+
// connected to storage but not ordering service?
|
|
252
|
+
this.logger.sendTelemetryEvent({ eventName: "BlobUploadSuccessWhileDisconnected" });
|
|
253
|
+
if (entry.status === PendingBlobStatus.OnlinePendingUpload) {
|
|
254
|
+
this.transitionToOffline(localId);
|
|
255
|
+
}
|
|
256
|
+
entry.status = PendingBlobStatus.OfflinePendingOp;
|
|
106
257
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
258
|
+
return response;
|
|
259
|
+
}
|
|
260
|
+
async onUploadReject(localId, error) {
|
|
261
|
+
const entry = this.pendingBlobs.get(localId);
|
|
262
|
+
assert(!!entry, 0x387 /* Must have pending blob entry for blob which failed to upload */);
|
|
263
|
+
if (!this.runtime.connected) {
|
|
264
|
+
if (entry.status === PendingBlobStatus.OnlinePendingUpload) {
|
|
265
|
+
this.transitionToOffline(localId);
|
|
266
|
+
}
|
|
267
|
+
// we are probably not connected to storage but start another upload request in case we are
|
|
268
|
+
entry.uploadP = this.retryThrottler.getDelay().then(async () => this.uploadBlob(localId, entry.blob));
|
|
269
|
+
return entry.uploadP;
|
|
112
270
|
}
|
|
113
|
-
|
|
271
|
+
else {
|
|
272
|
+
entry.handleP.reject(error);
|
|
273
|
+
throw error;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
transitionToOffline(localId) {
|
|
277
|
+
assert(!this.runtime.connected, 0x388 /* Must only transition to offline flow while runtime is disconnected */);
|
|
278
|
+
const entry = this.pendingBlobs.get(localId);
|
|
279
|
+
assert(!!entry, 0x389 /* No pending blob entry */);
|
|
280
|
+
assert([PendingBlobStatus.OnlinePendingUpload, PendingBlobStatus.OnlinePendingOp].includes(entry.status), 0x38a /* Blob must be in online flow to transition to offline flow */);
|
|
281
|
+
entry.status = entry.status === PendingBlobStatus.OnlinePendingUpload
|
|
282
|
+
? PendingBlobStatus.OfflinePendingUpload
|
|
283
|
+
: PendingBlobStatus.OfflinePendingOp;
|
|
284
|
+
// Submit a BlobAttach op. It's important we submit this op now before returning the blob handle so the
|
|
285
|
+
// BlobAttach op is sequenced prior to any ops referencing the handle. Otherwise an invalid handle could be
|
|
286
|
+
// added to the document if the ops are not all successfully submitted upon reconnection.
|
|
287
|
+
// storageId may be undefined but since we are not connected we will have a chance to add it when reSubmit()
|
|
288
|
+
// is called
|
|
289
|
+
this.sendBlobAttachOp(entry.storageId, localId);
|
|
290
|
+
entry.handleP.resolve(this.getBlobHandle(localId));
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Resubmit a BlobAttach op. Used to add storage IDs to ops that were
|
|
294
|
+
* submitted to runtime while disconnected.
|
|
295
|
+
* @param metadata - op metadata containing storage and/or local IDs
|
|
296
|
+
*/
|
|
297
|
+
reSubmit(metadata) {
|
|
298
|
+
assert(!!metadata, 0x38b /* Resubmitted ops must have metadata */);
|
|
299
|
+
const { blobId, localId } = metadata;
|
|
300
|
+
if (!blobId) {
|
|
301
|
+
assert(!!localId, 0x38c /* Submitted BlobAttach ops must have a blobId or localId */);
|
|
302
|
+
// We submitted this op while offline. The blob should have been uploaded by now.
|
|
303
|
+
const pendingEntry = this.pendingBlobs.get(localId);
|
|
304
|
+
assert((pendingEntry === null || pendingEntry === void 0 ? void 0 : pendingEntry.status) === PendingBlobStatus.OfflinePendingOp &&
|
|
305
|
+
!!(pendingEntry === null || pendingEntry === void 0 ? void 0 : pendingEntry.storageId), 0x38d /* blob must be uploaded before resubmitting BlobAttach op */);
|
|
306
|
+
return this.sendBlobAttachOp(pendingEntry.storageId, localId);
|
|
307
|
+
}
|
|
308
|
+
return this.sendBlobAttachOp(blobId, localId);
|
|
114
309
|
}
|
|
115
|
-
processBlobAttachOp(
|
|
310
|
+
processBlobAttachOp(message, local) {
|
|
311
|
+
var _a, _b;
|
|
312
|
+
assert((_a = message === null || message === void 0 ? void 0 : message.metadata) === null || _a === void 0 ? void 0 : _a.blobId, 0x12a /* "Missing blob id on metadata" */);
|
|
313
|
+
if (message.metadata.localId !== undefined) {
|
|
314
|
+
this.redirectTable.set(message.metadata.localId, message.metadata.blobId);
|
|
315
|
+
}
|
|
316
|
+
// set identity (id -> id) entry
|
|
317
|
+
this.redirectTable.set(message.metadata.blobId, message.metadata.blobId);
|
|
116
318
|
if (local) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
319
|
+
if (message.metadata.localId === undefined) {
|
|
320
|
+
// Since there is no local ID, we know this op was submitted while online.
|
|
321
|
+
const waitingBlobs = this.opsInFlight.get(message.metadata.blobId);
|
|
322
|
+
assert(!!waitingBlobs, 0x38e /* local online BlobAttach op with no pending blob */);
|
|
323
|
+
waitingBlobs.forEach((localId) => {
|
|
324
|
+
const pendingBlobEntry = this.pendingBlobs.get(localId);
|
|
325
|
+
assert(pendingBlobEntry !== undefined, 0x38f);
|
|
326
|
+
// It's possible we transitioned to offline flow while waiting for this op.
|
|
327
|
+
if (pendingBlobEntry.status === PendingBlobStatus.OnlinePendingOp) {
|
|
328
|
+
pendingBlobEntry.handleP.resolve(this.getBlobHandle(message.metadata.blobId));
|
|
329
|
+
this.pendingBlobs.delete(localId);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
// Each local ID is unique; get the pending blob entry and delete it
|
|
335
|
+
assert(((_b = this.pendingBlobs.get(message.metadata.localId)) === null || _b === void 0 ? void 0 : _b.status) === PendingBlobStatus.OfflinePendingOp, 0x1f8 /* "local BlobAttach op with no pending blob" */);
|
|
336
|
+
this.pendingBlobs.delete(message.metadata.localId);
|
|
337
|
+
}
|
|
121
338
|
}
|
|
122
|
-
this.blobIds.add(blobId);
|
|
123
339
|
}
|
|
124
340
|
/**
|
|
125
341
|
* Reads blobs needed to load BlobManager from storage.
|
|
342
|
+
* @param blobsTree - Tree containing IDs of previously attached blobs. We
|
|
343
|
+
* look for the IDs in the blob entries of the tree since the both the r11s
|
|
344
|
+
* and SPO drivers replace the attachment types returned in snapshot() with blobs.
|
|
126
345
|
*/
|
|
127
346
|
static async load(blobsTree, tryFetchBlob) {
|
|
128
347
|
if (!blobsTree) {
|
|
@@ -138,33 +357,23 @@ export class BlobManager {
|
|
|
138
357
|
return { ids, redirectTable };
|
|
139
358
|
}
|
|
140
359
|
/**
|
|
141
|
-
* Load a set of previously attached blob IDs from a previous snapshot.
|
|
142
|
-
* that BlobManager tracking and reporting attached blobs is a temporary
|
|
143
|
-
* solution since storage expects attached blobs to be reported and any that
|
|
144
|
-
* are not reported as attached may be GCed. In the future attached blob
|
|
145
|
-
* IDs will be collected at summarization time, and runtime will not care
|
|
146
|
-
* about the existence or specific formatting of this tree in returned
|
|
147
|
-
* snapshots.
|
|
148
|
-
*
|
|
149
|
-
* @param blobsTree - Tree containing IDs of previously attached blobs. This
|
|
150
|
-
* corresponds to snapshot() below. We look for the IDs in the blob entries
|
|
151
|
-
* of the tree since the both the r11s and SPO drivers replace the
|
|
152
|
-
* attachment types returned in snapshot() with blobs.
|
|
360
|
+
* Load a set of previously attached blob IDs and redirect table from a previous snapshot.
|
|
153
361
|
*/
|
|
154
362
|
load(snapshot) {
|
|
155
363
|
var _a, _b, _c;
|
|
156
|
-
if (snapshot.ids) {
|
|
157
|
-
const detached = this.runtime.attachState === AttachState.Detached;
|
|
158
|
-
snapshot.ids.map((entry) => detached ? this.detachedBlobIds.add(entry) : this.blobIds.add(entry));
|
|
159
|
-
}
|
|
160
|
-
if (snapshot.redirectTable) {
|
|
161
|
-
this.redirectTable = new Map(snapshot.redirectTable);
|
|
162
|
-
}
|
|
163
364
|
this.logger.sendTelemetryEvent({
|
|
164
365
|
eventName: "AttachmentBlobsLoaded",
|
|
165
366
|
count: (_b = (_a = snapshot.ids) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0,
|
|
166
367
|
redirectTable: (_c = snapshot.redirectTable) === null || _c === void 0 ? void 0 : _c.length,
|
|
167
368
|
});
|
|
369
|
+
const table = new Map(snapshot.redirectTable);
|
|
370
|
+
if (snapshot.ids) {
|
|
371
|
+
const detached = this.runtime.attachState === AttachState.Detached;
|
|
372
|
+
// If we are detached, we don't have storage IDs yet, so set to undefined
|
|
373
|
+
// Otherwise, set identity (id -> id) entries
|
|
374
|
+
snapshot.ids.forEach((entry) => table.set(entry, detached ? undefined : entry));
|
|
375
|
+
}
|
|
376
|
+
return table;
|
|
168
377
|
}
|
|
169
378
|
/**
|
|
170
379
|
* Generates data used for garbage collection. Each blob uploaded represents a node in the GC graph as it can be
|
|
@@ -174,22 +383,23 @@ export class BlobManager {
|
|
|
174
383
|
*/
|
|
175
384
|
getGCData(fullGC = false) {
|
|
176
385
|
const gcData = { gcNodes: {} };
|
|
177
|
-
|
|
386
|
+
/**
|
|
387
|
+
* The node path is of the format `/_blobs/blobId`. This path must match the path of the blob handle returned
|
|
388
|
+
* by the createBlob API because blobs are marked referenced by storing these handles in a referenced DDS.
|
|
389
|
+
*/
|
|
390
|
+
this.storageIds.forEach((blobId) => {
|
|
178
391
|
gcData.gcNodes[this.getBlobGCNodePath(blobId)] = [];
|
|
179
392
|
});
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
// added above when adding nodes for this.blobIds.
|
|
191
|
-
gcData.gcNodes[this.getBlobGCNodePath(localId)] = [this.getBlobGCNodePath(storageId)];
|
|
192
|
-
}
|
|
393
|
+
// For some blobs, the handle returned on creation is based off of the localId. So, these
|
|
394
|
+
// nodes can be referenced by storing the localId handle. When that happens, the corresponding storageId node
|
|
395
|
+
// must also be marked referenced. So, we add a route from the localId node to the storageId node.
|
|
396
|
+
// Note that because of de-duping, there can be multiple localIds that all redirect to the same storageId or
|
|
397
|
+
// a blob may be referenced via its storageId handle.
|
|
398
|
+
for (const [localId, storageId] of this.redirectTable) {
|
|
399
|
+
assert(!!storageId, 0x390 /* Must be attached to get GC data */);
|
|
400
|
+
// Add node for the localId and add a route to the storageId node. The storageId node will have been
|
|
401
|
+
// added above when adding nodes for this.blobIds.
|
|
402
|
+
gcData.gcNodes[this.getBlobGCNodePath(localId)] = [this.getBlobGCNodePath(storageId)];
|
|
193
403
|
}
|
|
194
404
|
return gcData;
|
|
195
405
|
}
|
|
@@ -211,32 +421,40 @@ export class BlobManager {
|
|
|
211
421
|
this.redirectTable.delete(blobId);
|
|
212
422
|
continue;
|
|
213
423
|
}
|
|
214
|
-
this.blobIds.delete(blobId);
|
|
215
424
|
}
|
|
216
425
|
}
|
|
217
426
|
summarize(telemetryContext) {
|
|
218
|
-
//
|
|
219
|
-
|
|
220
|
-
const attachingOrAttached = !!this.redirectTable || this.runtime.attachState !== AttachState.Detached;
|
|
221
|
-
const blobIds = attachingOrAttached ? this.blobIds : this.detachedBlobIds;
|
|
427
|
+
// if storageIds is empty, it means we are detached and have only local IDs, or that there are no blobs attached
|
|
428
|
+
const blobIds = this.storageIds.size > 0 ? Array.from(this.storageIds) : Array.from(this.redirectTable.keys());
|
|
222
429
|
const builder = new SummaryTreeBuilder();
|
|
223
430
|
blobIds.forEach((blobId) => {
|
|
224
431
|
builder.addAttachment(blobId);
|
|
225
432
|
});
|
|
226
|
-
|
|
227
|
-
|
|
433
|
+
// Any non-identity entries in the table need to be saved in the summary
|
|
434
|
+
if (this.redirectTable.size > blobIds.length) {
|
|
435
|
+
builder.addBlob(BlobManager.redirectTableBlobName,
|
|
436
|
+
// filter out identity entries
|
|
437
|
+
JSON.stringify(Array.from(this.redirectTable.entries())
|
|
438
|
+
.filter(([localId, storageId]) => localId !== storageId)));
|
|
228
439
|
}
|
|
229
440
|
return builder.getSummaryTree();
|
|
230
441
|
}
|
|
231
442
|
setRedirectTable(table) {
|
|
232
443
|
assert(this.runtime.attachState === AttachState.Detached, 0x252 /* "redirect table can only be set in detached container" */);
|
|
233
|
-
assert(
|
|
444
|
+
assert(this.redirectTable.size === table.size, 0x391 /* Redirect table size must match BlobManager's local ID count */);
|
|
234
445
|
for (const [localId, storageId] of table) {
|
|
235
|
-
assert(this.
|
|
236
|
-
this.
|
|
446
|
+
assert(this.redirectTable.has(localId), 0x254 /* "unrecognized id in redirect table" */);
|
|
447
|
+
this.redirectTable.set(localId, storageId);
|
|
448
|
+
// set identity (id -> id) entry
|
|
449
|
+
this.redirectTable.set(storageId, storageId);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
getPendingBlobs() {
|
|
453
|
+
const blobs = {};
|
|
454
|
+
for (const [key, entry] of this.pendingBlobs) {
|
|
455
|
+
blobs[key] = { blob: bufferToString(entry.blob, "base64") };
|
|
237
456
|
}
|
|
238
|
-
|
|
239
|
-
this.redirectTable = table;
|
|
457
|
+
return blobs;
|
|
240
458
|
}
|
|
241
459
|
}
|
|
242
460
|
BlobManager.basePath = "_blobs";
|