@fluidframework/datastore 2.0.0-internal.3.0.5 → 2.0.0-internal.3.1.1

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 (65) hide show
  1. package/.eslintrc.js +5 -7
  2. package/.mocharc.js +2 -2
  3. package/README.md +3 -0
  4. package/api-extractor.json +2 -2
  5. package/dist/channelContext.d.ts.map +1 -1
  6. package/dist/channelContext.js.map +1 -1
  7. package/dist/channelDeltaConnection.d.ts.map +1 -1
  8. package/dist/channelDeltaConnection.js.map +1 -1
  9. package/dist/channelStorageService.d.ts.map +1 -1
  10. package/dist/channelStorageService.js +1 -3
  11. package/dist/channelStorageService.js.map +1 -1
  12. package/dist/dataStoreRuntime.d.ts +11 -24
  13. package/dist/dataStoreRuntime.d.ts.map +1 -1
  14. package/dist/dataStoreRuntime.js +68 -86
  15. package/dist/dataStoreRuntime.js.map +1 -1
  16. package/dist/fluidHandle.d.ts.map +1 -1
  17. package/dist/fluidHandle.js.map +1 -1
  18. package/dist/localChannelContext.d.ts.map +1 -1
  19. package/dist/localChannelContext.js +9 -5
  20. package/dist/localChannelContext.js.map +1 -1
  21. package/dist/localChannelStorageService.d.ts.map +1 -1
  22. package/dist/localChannelStorageService.js.map +1 -1
  23. package/dist/packageVersion.d.ts +1 -1
  24. package/dist/packageVersion.js +1 -1
  25. package/dist/packageVersion.js.map +1 -1
  26. package/dist/remoteChannelContext.d.ts.map +1 -1
  27. package/dist/remoteChannelContext.js +2 -2
  28. package/dist/remoteChannelContext.js.map +1 -1
  29. package/lib/channelContext.d.ts.map +1 -1
  30. package/lib/channelContext.js.map +1 -1
  31. package/lib/channelDeltaConnection.d.ts.map +1 -1
  32. package/lib/channelDeltaConnection.js.map +1 -1
  33. package/lib/channelStorageService.d.ts.map +1 -1
  34. package/lib/channelStorageService.js +1 -3
  35. package/lib/channelStorageService.js.map +1 -1
  36. package/lib/dataStoreRuntime.d.ts +11 -24
  37. package/lib/dataStoreRuntime.d.ts.map +1 -1
  38. package/lib/dataStoreRuntime.js +71 -89
  39. package/lib/dataStoreRuntime.js.map +1 -1
  40. package/lib/fluidHandle.d.ts.map +1 -1
  41. package/lib/fluidHandle.js.map +1 -1
  42. package/lib/localChannelContext.d.ts.map +1 -1
  43. package/lib/localChannelContext.js +9 -5
  44. package/lib/localChannelContext.js.map +1 -1
  45. package/lib/localChannelStorageService.d.ts.map +1 -1
  46. package/lib/localChannelStorageService.js.map +1 -1
  47. package/lib/packageVersion.d.ts +1 -1
  48. package/lib/packageVersion.js +1 -1
  49. package/lib/packageVersion.js.map +1 -1
  50. package/lib/remoteChannelContext.d.ts.map +1 -1
  51. package/lib/remoteChannelContext.js +2 -2
  52. package/lib/remoteChannelContext.js.map +1 -1
  53. package/package.json +111 -110
  54. package/prettier.config.cjs +1 -1
  55. package/src/channelContext.ts +66 -65
  56. package/src/channelDeltaConnection.ts +50 -44
  57. package/src/channelStorageService.ts +58 -54
  58. package/src/dataStoreRuntime.ts +1122 -1086
  59. package/src/fluidHandle.ts +87 -91
  60. package/src/localChannelContext.ts +302 -255
  61. package/src/localChannelStorageService.ts +47 -45
  62. package/src/packageVersion.ts +1 -1
  63. package/src/remoteChannelContext.ts +300 -291
  64. package/tsconfig.esnext.json +6 -6
  65. package/tsconfig.json +9 -13
@@ -5,1078 +5,1110 @@
5
5
 
6
6
  import { ITelemetryLogger } from "@fluidframework/common-definitions";
7
7
  import {
8
- FluidObject,
9
- IFluidHandle,
10
- IFluidHandleContext,
11
- IRequest,
12
- IResponse,
8
+ FluidObject,
9
+ IFluidHandle,
10
+ IFluidHandleContext,
11
+ IRequest,
12
+ IResponse,
13
13
  } from "@fluidframework/core-interfaces";
14
14
  import {
15
- IAudience,
16
- IDeltaManager,
17
- AttachState,
18
- ILoaderOptions,
15
+ IAudience,
16
+ IDeltaManager,
17
+ AttachState,
18
+ ILoaderOptions,
19
19
  } from "@fluidframework/container-definitions";
20
20
  import { DataProcessingError, UsageError } from "@fluidframework/container-utils";
21
21
  import {
22
- assert,
23
- Deferred,
24
- LazyPromise,
25
- TypedEventEmitter,
26
- unreachableCase,
22
+ assert,
23
+ Deferred,
24
+ LazyPromise,
25
+ TypedEventEmitter,
26
+ unreachableCase,
27
27
  } from "@fluidframework/common-utils";
28
- import {
29
- ChildLogger,
30
- generateStack,
31
- LoggingError,
32
- loggerToMonitoringContext,
33
- MonitoringContext,
34
- raiseConnectedEvent,
35
- TelemetryDataTag,
36
- } from "@fluidframework/telemetry-utils";
28
+ import { ChildLogger, LoggingError, raiseConnectedEvent } from "@fluidframework/telemetry-utils";
37
29
  import { buildSnapshotTree } from "@fluidframework/driver-utils";
38
30
  import {
39
- IClientDetails,
40
- IDocumentMessage,
41
- ISequencedDocumentMessage,
42
- SummaryType,
43
- ISummaryBlob,
44
- ISummaryTree,
45
- IQuorumClients,
31
+ IClientDetails,
32
+ IDocumentMessage,
33
+ ISequencedDocumentMessage,
34
+ SummaryType,
35
+ ISummaryBlob,
36
+ ISummaryTree,
37
+ IQuorumClients,
46
38
  } from "@fluidframework/protocol-definitions";
47
39
  import {
48
- BindState,
49
- CreateSummarizerNodeSource,
50
- IAttachMessage,
51
- IEnvelope,
52
- IFluidDataStoreContext,
53
- IFluidDataStoreChannel,
54
- IGarbageCollectionData,
55
- IGarbageCollectionDetailsBase,
56
- IInboundSignalMessage,
57
- ISummaryTreeWithStats,
58
- VisibilityState,
59
- ITelemetryContext,
40
+ BindState,
41
+ CreateSummarizerNodeSource,
42
+ IAttachMessage,
43
+ IEnvelope,
44
+ IFluidDataStoreContext,
45
+ IFluidDataStoreChannel,
46
+ IGarbageCollectionData,
47
+ IGarbageCollectionDetailsBase,
48
+ IInboundSignalMessage,
49
+ ISummaryTreeWithStats,
50
+ VisibilityState,
51
+ ITelemetryContext,
60
52
  } from "@fluidframework/runtime-definitions";
61
53
  import {
62
- convertSnapshotTreeToSummaryTree,
63
- convertSummaryTreeToITree,
64
- generateHandleContextPath,
65
- RequestParser,
66
- SummaryTreeBuilder,
67
- create404Response,
68
- createResponseError,
69
- exceptionToResponse,
70
- requestFluidObject,
71
- packagePathToTelemetryProperty,
54
+ convertSnapshotTreeToSummaryTree,
55
+ convertSummaryTreeToITree,
56
+ generateHandleContextPath,
57
+ RequestParser,
58
+ SummaryTreeBuilder,
59
+ create404Response,
60
+ createResponseError,
61
+ exceptionToResponse,
62
+ requestFluidObject,
72
63
  } from "@fluidframework/runtime-utils";
73
64
  import {
74
- IChannel,
75
- IFluidDataStoreRuntime,
76
- IFluidDataStoreRuntimeEvents,
77
- IChannelFactory,
65
+ IChannel,
66
+ IFluidDataStoreRuntime,
67
+ IFluidDataStoreRuntimeEvents,
68
+ IChannelFactory,
78
69
  } from "@fluidframework/datastore-definitions";
79
70
  import {
80
- GCDataBuilder,
81
- removeRouteFromAllNodes,
82
- unpackChildNodesGCDetails,
83
- unpackChildNodesUsedRoutes,
71
+ GCDataBuilder,
72
+ removeRouteFromAllNodes,
73
+ unpackChildNodesGCDetails,
74
+ unpackChildNodesUsedRoutes,
84
75
  } from "@fluidframework/garbage-collector";
85
76
  import { v4 as uuid } from "uuid";
86
77
  import { IChannelContext, summarizeChannel } from "./channelContext";
87
- import { LocalChannelContext, LocalChannelContextBase, RehydratedLocalChannelContext } from "./localChannelContext";
78
+ import {
79
+ LocalChannelContext,
80
+ LocalChannelContextBase,
81
+ RehydratedLocalChannelContext,
82
+ } from "./localChannelContext";
88
83
  import { RemoteChannelContext } from "./remoteChannelContext";
89
84
  import { FluidObjectHandle } from "./fluidHandle";
90
85
 
91
86
  export enum DataStoreMessageType {
92
- // Creates a new channel
93
- Attach = "attach",
94
- ChannelOp = "op",
87
+ // Creates a new channel
88
+ Attach = "attach",
89
+ ChannelOp = "op",
95
90
  }
96
91
 
97
92
  export interface ISharedObjectRegistry {
98
- // TODO consider making this async. A consequence is that either the creation of a distributed data type
99
- // is async or we need a new API to split the synchronous vs. asynchronous creation.
100
- get(name: string): IChannelFactory | undefined;
93
+ // TODO consider making this async. A consequence is that either the creation of a distributed data type
94
+ // is async or we need a new API to split the synchronous vs. asynchronous creation.
95
+ get(name: string): IChannelFactory | undefined;
101
96
  }
102
97
 
103
98
  /**
104
99
  * Base data store class
105
100
  */
106
- export class FluidDataStoreRuntime extends
107
- TypedEventEmitter<IFluidDataStoreRuntimeEvents> implements
108
- IFluidDataStoreChannel, IFluidDataStoreRuntime, IFluidHandleContext {
109
- /**
110
- * @deprecated - Instantiate the class using its constructor instead.
111
- *
112
- * Loads the data store runtime
113
- * @param context - The data store context
114
- * @param sharedObjectRegistry - The registry of shared objects used by this data store
115
- * @param existing - If loading from an existing file.
116
- */
117
- public static load(
118
- context: IFluidDataStoreContext,
119
- sharedObjectRegistry: ISharedObjectRegistry,
120
- existing: boolean,
121
- ): FluidDataStoreRuntime {
122
- return new FluidDataStoreRuntime(
123
- context,
124
- sharedObjectRegistry,
125
- existing,
126
- async (dataStoreRuntime) => requestFluidObject(dataStoreRuntime, "/"));
127
- }
128
-
129
- /**
130
- * {@inheritDoc @fluidframework/datastore-definitions#IFluidDataStoreRuntime.entryPoint}
131
- */
132
- public readonly entryPoint?: IFluidHandle<FluidObject>;
133
-
134
- public get IFluidRouter() { return this; }
135
-
136
- public get connected(): boolean {
137
- return this.dataStoreContext.connected;
138
- }
139
-
140
- public get clientId(): string | undefined {
141
- return this.dataStoreContext.clientId;
142
- }
143
-
144
- public get clientDetails(): IClientDetails {
145
- return this.dataStoreContext.clientDetails;
146
- }
147
-
148
- public get isAttached(): boolean {
149
- return this.attachState !== AttachState.Detached;
150
- }
151
-
152
- public get attachState(): AttachState {
153
- return this._attachState;
154
- }
155
-
156
- public get absolutePath(): string {
157
- return generateHandleContextPath(this.id, this.routeContext);
158
- }
159
-
160
- public get routeContext(): IFluidHandleContext {
161
- return this.dataStoreContext.IFluidHandleContext;
162
- }
163
-
164
- public get IFluidHandleContext() { return this; }
165
-
166
- public get rootRoutingContext() { return this; }
167
- public get channelsRoutingContext() { return this; }
168
- public get objectsRoutingContext() { return this; }
169
-
170
- private _disposed = false;
171
- public get disposed() { return this._disposed; }
172
-
173
- private readonly contexts = new Map<string, IChannelContext>();
174
- private readonly contextsDeferred = new Map<string, Deferred<IChannelContext>>();
175
- private readonly pendingAttach = new Map<string, IAttachMessage>();
176
-
177
- private bindState: BindState;
178
- private readonly deferredAttached = new Deferred<void>();
179
- private readonly localChannelContextQueue = new Map<string, LocalChannelContextBase>();
180
- private readonly notBoundedChannelContextSet = new Set<string>();
181
- private _attachState: AttachState;
182
- public visibilityState: VisibilityState;
183
- // A list of handles that are bound when the data store is not visible. We have to make them visible when the data
184
- // store becomes visible.
185
- private readonly pendingHandlesToMakeVisible: Set<IFluidHandle> = new Set();
186
-
187
- public readonly id: string;
188
- public readonly options: ILoaderOptions;
189
- public readonly deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>;
190
- private readonly quorum: IQuorumClients;
191
- private readonly audience: IAudience;
192
- private readonly mc: MonitoringContext;
193
- public get logger(): ITelemetryLogger {
194
- return this.mc.logger;
195
- }
196
-
197
- // A map of child channel context ids to the their base GC details. This is used to initialize the GC state of the
198
- // channel contexts.
199
- private readonly channelsBaseGCDetails: LazyPromise<Map<string, IGarbageCollectionDetailsBase>>;
200
-
201
- /**
202
- * If the summarizer makes local changes, a telemetry event is logged. This has the potential to be very noisy.
203
- * So, adding a count of how many telemetry events are logged per data store context. This can be
204
- * controlled via feature flags.
205
- */
206
- private localChangesTelemetryCount: number;
207
-
208
- /**
209
- * Invokes the given callback and expects that no ops are submitted
210
- * until execution finishes. If an op is submitted, an error will be raised.
211
- *
212
- * Can be disabled by feature gate `Fluid.ContainerRuntime.DisableOpReentryCheck`
213
- *
214
- * @param callback - the callback to be invoked
215
- */
216
- public ensureNoDataModelChanges<T>(callback: () => T): T {
217
- // back-compat ADO:2309
218
- return this.dataStoreContext.ensureNoDataModelChanges === undefined ?
219
- callback() :
220
- this.dataStoreContext.ensureNoDataModelChanges(callback);
221
- }
222
-
223
- /**
224
- * Create an instance of a DataStore runtime.
225
- *
226
- * @param dataStoreContext - Context object for the runtime.
227
- * @param sharedObjectRegistry - The registry of shared objects that this data store will be able to instantiate.
228
- * @param existing - Pass 'true' if loading this datastore from an existing file; pass 'false' otherwise.
229
- * @param initializeEntryPoint - Function to initialize the entryPoint object for the data store runtime. The
230
- * handle to this data store runtime will point to the object returned by this function. If this function is not
231
- * provided, the handle will be left undefined. This is here so we can start making handles a first-class citizen
232
- * and the primary way of interacting with some Fluid objects, and should be used if possible.
233
- */
234
- public constructor(
235
- private readonly dataStoreContext: IFluidDataStoreContext,
236
- private readonly sharedObjectRegistry: ISharedObjectRegistry,
237
- existing: boolean,
238
- initializeEntryPoint?: (runtime: IFluidDataStoreRuntime) => Promise<FluidObject>,
239
- ) {
240
- super();
241
-
242
- assert(!dataStoreContext.id.includes("/"),
243
- 0x30e /* Id cannot contain slashes. DataStoreContext should have validated this. */);
244
-
245
- this.mc = loggerToMonitoringContext(
246
- ChildLogger.create(dataStoreContext.logger, "FluidDataStoreRuntime", {
247
- all: { dataStoreId: uuid() },
248
- }),
249
- );
250
-
251
- this.id = dataStoreContext.id;
252
- this.options = dataStoreContext.options;
253
- this.deltaManager = dataStoreContext.deltaManager;
254
- this.quorum = dataStoreContext.getQuorum();
255
- this.audience = dataStoreContext.getAudience();
256
-
257
- const tree = dataStoreContext.baseSnapshot;
258
-
259
- this.channelsBaseGCDetails = new LazyPromise(async () => {
260
- const baseGCDetails = await this.dataStoreContext.getBaseGCDetails();
261
- return unpackChildNodesGCDetails(baseGCDetails);
262
- });
263
-
264
- // Must always receive the data store type inside of the attributes
265
- if (tree?.trees !== undefined) {
266
- Object.keys(tree.trees).forEach((path) => {
267
- // Issue #4414
268
- if (path === "_search") { return; }
269
-
270
- let channelContext: IChannelContext;
271
- // If already exists on storage, then create a remote channel. However, if it is case of rehydrating a
272
- // container from snapshot where we load detached container from a snapshot, isLocalDataStore would be
273
- // true. In this case create a RehydratedLocalChannelContext.
274
- if (dataStoreContext.isLocalDataStore) {
275
- channelContext = new RehydratedLocalChannelContext(
276
- path,
277
- this.sharedObjectRegistry,
278
- this,
279
- this.dataStoreContext,
280
- this.dataStoreContext.storage,
281
- this.logger,
282
- (content, localOpMetadata) => this.submitChannelOp(path, content, localOpMetadata),
283
- (address: string) => this.setChannelDirty(address),
284
- (srcHandle: IFluidHandle, outboundHandle: IFluidHandle) =>
285
- this.addedGCOutboundReference(srcHandle, outboundHandle),
286
- tree.trees[path]);
287
- // This is the case of rehydrating a detached container from snapshot. Now due to delay loading of
288
- // data store, if the data store is loaded after the container is attached, then we missed making
289
- // the channel visible. So do it now. Otherwise, add it to local channel context queue, so
290
- // that it can be make it visible later with the data store.
291
- if (dataStoreContext.attachState !== AttachState.Detached) {
292
- (channelContext as LocalChannelContextBase).makeVisible();
293
- } else {
294
- this.localChannelContextQueue.set(path, channelContext as LocalChannelContextBase);
295
- }
296
- } else {
297
- channelContext = new RemoteChannelContext(
298
- this,
299
- dataStoreContext,
300
- dataStoreContext.storage,
301
- (content, localOpMetadata) => this.submitChannelOp(path, content, localOpMetadata),
302
- (address: string) => this.setChannelDirty(address),
303
- (srcHandle: IFluidHandle, outboundHandle: IFluidHandle) =>
304
- this.addedGCOutboundReference(srcHandle, outboundHandle),
305
- path,
306
- tree.trees[path],
307
- this.sharedObjectRegistry,
308
- undefined /* extraBlobs */,
309
- this.dataStoreContext.getCreateChildSummarizerNodeFn(
310
- path,
311
- { type: CreateSummarizerNodeSource.FromSummary },
312
- ),
313
- async () => this.getChannelBaseGCDetails(path));
314
- }
315
- const deferred = new Deferred<IChannelContext>();
316
- deferred.resolve(channelContext);
317
-
318
- this.contexts.set(path, channelContext);
319
- this.contextsDeferred.set(path, deferred);
320
- });
321
- }
322
-
323
- if (initializeEntryPoint) {
324
- const promise = new LazyPromise(async () => initializeEntryPoint(this));
325
- this.entryPoint = new FluidObjectHandle<FluidObject>(promise, "", this.objectsRoutingContext);
326
- }
327
-
328
- this.attachListener();
329
- // If exists on storage or loaded from a snapshot, it should already be bound.
330
- this.bindState = existing ? BindState.Bound : BindState.NotBound;
331
- this._attachState = dataStoreContext.attachState;
332
-
333
- /**
334
- * If existing flag is false, this is a new data store and is not visible. The existing flag can be true in two
335
- * conditions:
336
- *
337
- * 1. It's a local data store that is created when a detached container is rehydrated. In this case, the data
338
- * store is locally visible because the snapshot it is loaded from contains locally visible data stores only.
339
- *
340
- * 2. It's a remote data store that is created when an attached container is loaded is loaded from snapshot or
341
- * when an attach op comes in. In both these cases, the data store is already globally visible.
342
- */
343
- if (existing) {
344
- this.visibilityState = dataStoreContext.attachState === AttachState.Detached
345
- ? VisibilityState.LocallyVisible : VisibilityState.GloballyVisible;
346
- } else {
347
- this.visibilityState = VisibilityState.NotVisible;
348
- }
349
-
350
- // If it's existing we know it has been attached.
351
- if (existing) {
352
- this.deferredAttached.resolve();
353
- }
354
-
355
- // By default, a data store can log maximum 10 local changes telemetry in summarizer.
356
- this.localChangesTelemetryCount =
357
- this.mc.config.getNumber("Fluid.Telemetry.LocalChangesTelemetryCount") ?? 10;
358
- }
359
-
360
- public dispose(): void {
361
- if (this._disposed) {
362
- return;
363
- }
364
- this._disposed = true;
365
-
366
- this.emit("dispose");
367
- this.removeAllListeners();
368
- }
369
-
370
- public async resolveHandle(request: IRequest): Promise<IResponse> {
371
- return this.request(request);
372
- }
373
-
374
- public async request(request: IRequest): Promise<IResponse> {
375
- try {
376
- const parser = RequestParser.create(request);
377
- const id = parser.pathParts[0];
378
-
379
- if (id === "_channels" || id === "_custom") {
380
- return this.request(parser.createSubRequest(1));
381
- }
382
-
383
- // Check for a data type reference first
384
- if (this.contextsDeferred.has(id) && parser.isLeaf(1)) {
385
- try {
386
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
387
- const value = await this.contextsDeferred.get(id)!.promise;
388
- const channel = await value.getChannel();
389
-
390
- return { mimeType: "fluid/object", status: 200, value: channel };
391
- } catch (error) {
392
- this.mc.logger.sendErrorEvent({ eventName: "GetChannelFailedInRequest" }, error);
393
-
394
- return createResponseError(500, `Failed to get Channel: ${error}`, request);
395
- }
396
- }
397
-
398
- // Otherwise defer to an attached request handler
399
- return create404Response(request);
400
- } catch (error) {
401
- return exceptionToResponse(error);
402
- }
403
- }
404
-
405
- public async getChannel(id: string): Promise<IChannel> {
406
- this.verifyNotClosed();
407
-
408
- // TODO we don't assume any channels (even root) in the runtime. If you request a channel that doesn't exist
409
- // we will never resolve the promise. May want a flag to getChannel that doesn't wait for the promise if
410
- // it doesn't exist
411
- if (!this.contextsDeferred.has(id)) {
412
- this.contextsDeferred.set(id, new Deferred<IChannelContext>());
413
- }
414
-
415
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
416
- const context = await this.contextsDeferred.get(id)!.promise;
417
- const channel = await context.getChannel();
418
-
419
- return channel;
420
- }
421
-
422
- public createChannel(id: string = uuid(), type: string): IChannel {
423
- if (id.includes("/")) {
424
- throw new UsageError(`Id cannot contain slashes: ${id}`);
425
- }
426
-
427
- this.verifyNotClosed();
428
-
429
- assert(!this.contexts.has(id), 0x179 /* "createChannel() with existing ID" */);
430
- this.notBoundedChannelContextSet.add(id);
431
- const context = new LocalChannelContext(
432
- id,
433
- this.sharedObjectRegistry,
434
- type,
435
- this,
436
- this.dataStoreContext,
437
- this.dataStoreContext.storage,
438
- this.logger,
439
- (content, localOpMetadata) => this.submitChannelOp(id, content, localOpMetadata),
440
- (address: string) => this.setChannelDirty(address),
441
- (srcHandle: IFluidHandle, outboundHandle: IFluidHandle) =>
442
- this.addedGCOutboundReference(srcHandle, outboundHandle));
443
- this.contexts.set(id, context);
444
-
445
- if (this.contextsDeferred.has(id)) {
446
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
447
- this.contextsDeferred.get(id)!.resolve(context);
448
- } else {
449
- const deferred = new Deferred<IChannelContext>();
450
- deferred.resolve(context);
451
- this.contextsDeferred.set(id, deferred);
452
- }
453
-
454
- // Channels (DDS) should not be created in summarizer client.
455
- this.identifyLocalChangeInSummarizer("DDSCreatedInSummarizer", id, type);
456
-
457
- assert(!!context.channel, 0x17a /* "Channel should be loaded when created!!" */);
458
- return context.channel;
459
- }
460
-
461
- /**
462
- * Binds a channel with the runtime. If the runtime is attached we will attach the channel right away.
463
- * If the runtime is not attached we will defer the attach until the runtime attaches.
464
- * @param channel - channel to be registered.
465
- */
466
- public bindChannel(channel: IChannel): void {
467
- assert(this.notBoundedChannelContextSet.has(channel.id),
468
- 0x17b /* "Channel to be binded should be in not bounded set" */);
469
- this.notBoundedChannelContextSet.delete(channel.id);
470
- // If our data store is attached, then attach the channel.
471
- if (this.isAttached) {
472
- this.attachChannel(channel);
473
- return;
474
- }
475
-
476
- /**
477
- * If this channel is already waiting to be made visible, do nothing. This can happen during attachGraph() when
478
- * a channel's graph is attached. It calls bindToContext on the shared object which will end up back here.
479
- */
480
- if (this.pendingHandlesToMakeVisible.has(channel.handle)) {
481
- return;
482
- }
483
-
484
- this.bind(channel.handle);
485
-
486
- // If our data store is local then add the channel to the queue
487
- if (!this.localChannelContextQueue.has(channel.id)) {
488
- this.localChannelContextQueue.set(channel.id, this.contexts.get(channel.id) as LocalChannelContextBase);
489
- }
490
- }
491
-
492
- /**
493
- * This function is called when a data store becomes root. It does the following:
494
- *
495
- * 1. Marks the data store locally visible in the container.
496
- *
497
- * 2. Attaches the graph of all the handles bound to it.
498
- *
499
- * 3. Calls into the data store context to mark it visible in the container too. If the container is globally
500
- * visible, it will mark us globally visible. Otherwise, it will mark us globally visible when it becomes
501
- * globally visible.
502
- */
503
- public makeVisibleAndAttachGraph() {
504
- if (this.visibilityState !== VisibilityState.NotVisible) {
505
- return;
506
- }
507
- this.visibilityState = VisibilityState.LocallyVisible;
508
-
509
- this.pendingHandlesToMakeVisible.forEach((handle) => {
510
- handle.attachGraph();
511
- });
512
- this.pendingHandlesToMakeVisible.clear();
513
- this.bindToContext();
514
- }
515
-
516
- /**
517
- * This function is called when a handle to this data store is added to a visible DDS.
518
- */
519
- public attachGraph() {
520
- this.makeVisibleAndAttachGraph();
521
- }
522
-
523
- /**
524
- * @deprecated - Not necessary if consumers add a new dataStore to the container by storing its handle.
525
- * Binds this runtime to the container
526
- * This includes the following:
527
- * 1. Sending an Attach op that includes all existing state
528
- * 2. Attaching the graph if the data store becomes attached.
529
- */
530
- public bindToContext() {
531
- if (this.bindState !== BindState.NotBound) {
532
- return;
533
- }
534
- this.bindState = BindState.Binding;
535
- this.dataStoreContext.bindToContext();
536
- this.bindState = BindState.Bound;
537
- }
538
-
539
- public bind(handle: IFluidHandle): void {
540
- // If visible, attach the incoming handle's graph. Else, this will be done when we become visible.
541
- if (this.visibilityState !== VisibilityState.NotVisible) {
542
- handle.attachGraph();
543
- return;
544
- }
545
- this.pendingHandlesToMakeVisible.add(handle);
546
- }
547
-
548
- public setConnectionState(connected: boolean, clientId?: string) {
549
- this.verifyNotClosed();
550
-
551
- for (const [, object] of this.contexts) {
552
- object.setConnectionState(connected, clientId);
553
- }
554
-
555
- raiseConnectedEvent(this.logger, this, connected, clientId);
556
- }
557
-
558
- public getQuorum(): IQuorumClients {
559
- return this.quorum;
560
- }
561
-
562
- public getAudience(): IAudience {
563
- return this.audience;
564
- }
565
-
566
- public async uploadBlob(blob: ArrayBufferLike): Promise<IFluidHandle<ArrayBufferLike>> {
567
- this.verifyNotClosed();
568
-
569
- return this.dataStoreContext.uploadBlob(blob);
570
- }
571
-
572
- public process(message: ISequencedDocumentMessage, local: boolean, localOpMetadata: unknown) {
573
- this.verifyNotClosed();
574
-
575
- try {
576
- // catches as data processing error whether or not they come from async pending queues
577
- switch (message.type) {
578
- case DataStoreMessageType.Attach: {
579
- const attachMessage = message.contents as IAttachMessage;
580
- const id = attachMessage.id;
581
-
582
- // If a non-local operation then go and create the object
583
- // Otherwise mark it as officially attached.
584
- if (local) {
585
- assert(this.pendingAttach.has(id), 0x17c /* "Unexpected attach (local) channel OP" */);
586
- this.pendingAttach.delete(id);
587
- } else {
588
- assert(!this.contexts.has(id),
589
- 0x17d, /* `Unexpected attach channel OP,
101
+ export class FluidDataStoreRuntime
102
+ extends TypedEventEmitter<IFluidDataStoreRuntimeEvents>
103
+ implements IFluidDataStoreChannel, IFluidDataStoreRuntime, IFluidHandleContext
104
+ {
105
+ /**
106
+ * @deprecated - Instantiate the class using its constructor instead.
107
+ *
108
+ * Loads the data store runtime
109
+ * @param context - The data store context
110
+ * @param sharedObjectRegistry - The registry of shared objects used by this data store
111
+ * @param existing - If loading from an existing file.
112
+ */
113
+ public static load(
114
+ context: IFluidDataStoreContext,
115
+ sharedObjectRegistry: ISharedObjectRegistry,
116
+ existing: boolean,
117
+ ): FluidDataStoreRuntime {
118
+ return new FluidDataStoreRuntime(
119
+ context,
120
+ sharedObjectRegistry,
121
+ existing,
122
+ async (dataStoreRuntime) => requestFluidObject(dataStoreRuntime, "/"),
123
+ );
124
+ }
125
+
126
+ /**
127
+ * {@inheritDoc @fluidframework/datastore-definitions#IFluidDataStoreRuntime.entryPoint}
128
+ */
129
+ public readonly entryPoint?: IFluidHandle<FluidObject>;
130
+
131
+ public get IFluidRouter() {
132
+ return this;
133
+ }
134
+
135
+ public get connected(): boolean {
136
+ return this.dataStoreContext.connected;
137
+ }
138
+
139
+ public get clientId(): string | undefined {
140
+ return this.dataStoreContext.clientId;
141
+ }
142
+
143
+ public get clientDetails(): IClientDetails {
144
+ return this.dataStoreContext.clientDetails;
145
+ }
146
+
147
+ public get isAttached(): boolean {
148
+ return this.attachState !== AttachState.Detached;
149
+ }
150
+
151
+ public get attachState(): AttachState {
152
+ return this._attachState;
153
+ }
154
+
155
+ public get absolutePath(): string {
156
+ return generateHandleContextPath(this.id, this.routeContext);
157
+ }
158
+
159
+ public get routeContext(): IFluidHandleContext {
160
+ return this.dataStoreContext.IFluidHandleContext;
161
+ }
162
+
163
+ public get IFluidHandleContext() {
164
+ return this;
165
+ }
166
+
167
+ public get rootRoutingContext() {
168
+ return this;
169
+ }
170
+ public get channelsRoutingContext() {
171
+ return this;
172
+ }
173
+ public get objectsRoutingContext() {
174
+ return this;
175
+ }
176
+
177
+ private _disposed = false;
178
+ public get disposed() {
179
+ return this._disposed;
180
+ }
181
+
182
+ private readonly contexts = new Map<string, IChannelContext>();
183
+ private readonly contextsDeferred = new Map<string, Deferred<IChannelContext>>();
184
+ private readonly pendingAttach = new Map<string, IAttachMessage>();
185
+
186
+ private bindState: BindState;
187
+ private readonly deferredAttached = new Deferred<void>();
188
+ private readonly localChannelContextQueue = new Map<string, LocalChannelContextBase>();
189
+ private readonly notBoundedChannelContextSet = new Set<string>();
190
+ private _attachState: AttachState;
191
+ public visibilityState: VisibilityState;
192
+ // A list of handles that are bound when the data store is not visible. We have to make them visible when the data
193
+ // store becomes visible.
194
+ private readonly pendingHandlesToMakeVisible: Set<IFluidHandle> = new Set();
195
+
196
+ public readonly id: string;
197
+ public readonly options: ILoaderOptions;
198
+ public readonly deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>;
199
+ private readonly quorum: IQuorumClients;
200
+ private readonly audience: IAudience;
201
+ public readonly logger: ITelemetryLogger;
202
+
203
+ // A map of child channel context ids to the their base GC details. This is used to initialize the GC state of the
204
+ // channel contexts.
205
+ private readonly channelsBaseGCDetails: LazyPromise<Map<string, IGarbageCollectionDetailsBase>>;
206
+
207
+ /**
208
+ * Invokes the given callback and expects that no ops are submitted
209
+ * until execution finishes. If an op is submitted, an error will be raised.
210
+ *
211
+ * Can be disabled by feature gate `Fluid.ContainerRuntime.DisableOpReentryCheck`
212
+ *
213
+ * @param callback - the callback to be invoked
214
+ */
215
+ public ensureNoDataModelChanges<T>(callback: () => T): T {
216
+ // back-compat ADO:2309
217
+ return this.dataStoreContext.ensureNoDataModelChanges === undefined
218
+ ? callback()
219
+ : this.dataStoreContext.ensureNoDataModelChanges(callback);
220
+ }
221
+
222
+ /**
223
+ * Create an instance of a DataStore runtime.
224
+ *
225
+ * @param dataStoreContext - Context object for the runtime.
226
+ * @param sharedObjectRegistry - The registry of shared objects that this data store will be able to instantiate.
227
+ * @param existing - Pass 'true' if loading this datastore from an existing file; pass 'false' otherwise.
228
+ * @param initializeEntryPoint - Function to initialize the entryPoint object for the data store runtime. The
229
+ * handle to this data store runtime will point to the object returned by this function. If this function is not
230
+ * provided, the handle will be left undefined. This is here so we can start making handles a first-class citizen
231
+ * and the primary way of interacting with some Fluid objects, and should be used if possible.
232
+ */
233
+ public constructor(
234
+ private readonly dataStoreContext: IFluidDataStoreContext,
235
+ private readonly sharedObjectRegistry: ISharedObjectRegistry,
236
+ existing: boolean,
237
+ initializeEntryPoint?: (runtime: IFluidDataStoreRuntime) => Promise<FluidObject>,
238
+ ) {
239
+ super();
240
+
241
+ assert(
242
+ !dataStoreContext.id.includes("/"),
243
+ 0x30e /* Id cannot contain slashes. DataStoreContext should have validated this. */,
244
+ );
245
+
246
+ this.logger = ChildLogger.create(dataStoreContext.logger, "FluidDataStoreRuntime", {
247
+ all: { dataStoreId: uuid() },
248
+ });
249
+
250
+ this.id = dataStoreContext.id;
251
+ this.options = dataStoreContext.options;
252
+ this.deltaManager = dataStoreContext.deltaManager;
253
+ this.quorum = dataStoreContext.getQuorum();
254
+ this.audience = dataStoreContext.getAudience();
255
+
256
+ const tree = dataStoreContext.baseSnapshot;
257
+
258
+ this.channelsBaseGCDetails = new LazyPromise(async () => {
259
+ const baseGCDetails = await this.dataStoreContext.getBaseGCDetails();
260
+ return unpackChildNodesGCDetails(baseGCDetails);
261
+ });
262
+
263
+ // Must always receive the data store type inside of the attributes
264
+ if (tree?.trees !== undefined) {
265
+ Object.keys(tree.trees).forEach((path) => {
266
+ // Issue #4414
267
+ if (path === "_search") {
268
+ return;
269
+ }
270
+
271
+ let channelContext: IChannelContext;
272
+ // If already exists on storage, then create a remote channel. However, if it is case of rehydrating a
273
+ // container from snapshot where we load detached container from a snapshot, isLocalDataStore would be
274
+ // true. In this case create a RehydratedLocalChannelContext.
275
+ if (dataStoreContext.isLocalDataStore) {
276
+ channelContext = new RehydratedLocalChannelContext(
277
+ path,
278
+ this.sharedObjectRegistry,
279
+ this,
280
+ this.dataStoreContext,
281
+ this.dataStoreContext.storage,
282
+ this.logger,
283
+ (content, localOpMetadata) =>
284
+ this.submitChannelOp(path, content, localOpMetadata),
285
+ (address: string) => this.setChannelDirty(address),
286
+ (srcHandle: IFluidHandle, outboundHandle: IFluidHandle) =>
287
+ this.addedGCOutboundReference(srcHandle, outboundHandle),
288
+ tree.trees[path],
289
+ );
290
+ // This is the case of rehydrating a detached container from snapshot. Now due to delay loading of
291
+ // data store, if the data store is loaded after the container is attached, then we missed making
292
+ // the channel visible. So do it now. Otherwise, add it to local channel context queue, so
293
+ // that it can be make it visible later with the data store.
294
+ if (dataStoreContext.attachState !== AttachState.Detached) {
295
+ (channelContext as LocalChannelContextBase).makeVisible();
296
+ } else {
297
+ this.localChannelContextQueue.set(
298
+ path,
299
+ channelContext as LocalChannelContextBase,
300
+ );
301
+ }
302
+ } else {
303
+ channelContext = new RemoteChannelContext(
304
+ this,
305
+ dataStoreContext,
306
+ dataStoreContext.storage,
307
+ (content, localOpMetadata) =>
308
+ this.submitChannelOp(path, content, localOpMetadata),
309
+ (address: string) => this.setChannelDirty(address),
310
+ (srcHandle: IFluidHandle, outboundHandle: IFluidHandle) =>
311
+ this.addedGCOutboundReference(srcHandle, outboundHandle),
312
+ path,
313
+ tree.trees[path],
314
+ this.sharedObjectRegistry,
315
+ undefined /* extraBlobs */,
316
+ this.dataStoreContext.getCreateChildSummarizerNodeFn(path, {
317
+ type: CreateSummarizerNodeSource.FromSummary,
318
+ }),
319
+ async () => this.getChannelBaseGCDetails(path),
320
+ );
321
+ }
322
+ const deferred = new Deferred<IChannelContext>();
323
+ deferred.resolve(channelContext);
324
+
325
+ this.contexts.set(path, channelContext);
326
+ this.contextsDeferred.set(path, deferred);
327
+ });
328
+ }
329
+
330
+ if (initializeEntryPoint) {
331
+ const promise = new LazyPromise(async () => initializeEntryPoint(this));
332
+ this.entryPoint = new FluidObjectHandle<FluidObject>(
333
+ promise,
334
+ "",
335
+ this.objectsRoutingContext,
336
+ );
337
+ }
338
+
339
+ this.attachListener();
340
+ // If exists on storage or loaded from a snapshot, it should already be bound.
341
+ this.bindState = existing ? BindState.Bound : BindState.NotBound;
342
+ this._attachState = dataStoreContext.attachState;
343
+
344
+ /**
345
+ * If existing flag is false, this is a new data store and is not visible. The existing flag can be true in two
346
+ * conditions:
347
+ *
348
+ * 1. It's a local data store that is created when a detached container is rehydrated. In this case, the data
349
+ * store is locally visible because the snapshot it is loaded from contains locally visible data stores only.
350
+ *
351
+ * 2. It's a remote data store that is created when an attached container is loaded is loaded from snapshot or
352
+ * when an attach op comes in. In both these cases, the data store is already globally visible.
353
+ */
354
+ if (existing) {
355
+ this.visibilityState =
356
+ dataStoreContext.attachState === AttachState.Detached
357
+ ? VisibilityState.LocallyVisible
358
+ : VisibilityState.GloballyVisible;
359
+ } else {
360
+ this.visibilityState = VisibilityState.NotVisible;
361
+ }
362
+
363
+ // If it's existing we know it has been attached.
364
+ if (existing) {
365
+ this.deferredAttached.resolve();
366
+ }
367
+ }
368
+
369
+ public dispose(): void {
370
+ if (this._disposed) {
371
+ return;
372
+ }
373
+ this._disposed = true;
374
+
375
+ this.emit("dispose");
376
+ this.removeAllListeners();
377
+ }
378
+
379
+ public async resolveHandle(request: IRequest): Promise<IResponse> {
380
+ return this.request(request);
381
+ }
382
+
383
+ public async request(request: IRequest): Promise<IResponse> {
384
+ try {
385
+ const parser = RequestParser.create(request);
386
+ const id = parser.pathParts[0];
387
+
388
+ if (id === "_channels" || id === "_custom") {
389
+ return this.request(parser.createSubRequest(1));
390
+ }
391
+
392
+ // Check for a data type reference first
393
+ if (this.contextsDeferred.has(id) && parser.isLeaf(1)) {
394
+ try {
395
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
396
+ const value = await this.contextsDeferred.get(id)!.promise;
397
+ const channel = await value.getChannel();
398
+
399
+ return { mimeType: "fluid/object", status: 200, value: channel };
400
+ } catch (error) {
401
+ this.logger.sendErrorEvent({ eventName: "GetChannelFailedInRequest" }, error);
402
+
403
+ return createResponseError(500, `Failed to get Channel: ${error}`, request);
404
+ }
405
+ }
406
+
407
+ // Otherwise defer to an attached request handler
408
+ return create404Response(request);
409
+ } catch (error) {
410
+ return exceptionToResponse(error);
411
+ }
412
+ }
413
+
414
+ public async getChannel(id: string): Promise<IChannel> {
415
+ this.verifyNotClosed();
416
+
417
+ // TODO we don't assume any channels (even root) in the runtime. If you request a channel that doesn't exist
418
+ // we will never resolve the promise. May want a flag to getChannel that doesn't wait for the promise if
419
+ // it doesn't exist
420
+ if (!this.contextsDeferred.has(id)) {
421
+ this.contextsDeferred.set(id, new Deferred<IChannelContext>());
422
+ }
423
+
424
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
425
+ const context = await this.contextsDeferred.get(id)!.promise;
426
+ const channel = await context.getChannel();
427
+
428
+ return channel;
429
+ }
430
+
431
+ public createChannel(id: string = uuid(), type: string): IChannel {
432
+ if (id.includes("/")) {
433
+ throw new UsageError(`Id cannot contain slashes: ${id}`);
434
+ }
435
+
436
+ this.verifyNotClosed();
437
+
438
+ assert(!this.contexts.has(id), 0x179 /* "createChannel() with existing ID" */);
439
+ this.notBoundedChannelContextSet.add(id);
440
+ const context = new LocalChannelContext(
441
+ id,
442
+ this.sharedObjectRegistry,
443
+ type,
444
+ this,
445
+ this.dataStoreContext,
446
+ this.dataStoreContext.storage,
447
+ this.logger,
448
+ (content, localOpMetadata) => this.submitChannelOp(id, content, localOpMetadata),
449
+ (address: string) => this.setChannelDirty(address),
450
+ (srcHandle: IFluidHandle, outboundHandle: IFluidHandle) =>
451
+ this.addedGCOutboundReference(srcHandle, outboundHandle),
452
+ );
453
+ this.contexts.set(id, context);
454
+
455
+ if (this.contextsDeferred.has(id)) {
456
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
457
+ this.contextsDeferred.get(id)!.resolve(context);
458
+ } else {
459
+ const deferred = new Deferred<IChannelContext>();
460
+ deferred.resolve(context);
461
+ this.contextsDeferred.set(id, deferred);
462
+ }
463
+
464
+ assert(!!context.channel, 0x17a /* "Channel should be loaded when created!!" */);
465
+ return context.channel;
466
+ }
467
+
468
+ /**
469
+ * Binds a channel with the runtime. If the runtime is attached we will attach the channel right away.
470
+ * If the runtime is not attached we will defer the attach until the runtime attaches.
471
+ * @param channel - channel to be registered.
472
+ */
473
+ public bindChannel(channel: IChannel): void {
474
+ assert(
475
+ this.notBoundedChannelContextSet.has(channel.id),
476
+ 0x17b /* "Channel to be binded should be in not bounded set" */,
477
+ );
478
+ this.notBoundedChannelContextSet.delete(channel.id);
479
+ // If our data store is attached, then attach the channel.
480
+ if (this.isAttached) {
481
+ this.attachChannel(channel);
482
+ return;
483
+ }
484
+
485
+ /**
486
+ * If this channel is already waiting to be made visible, do nothing. This can happen during attachGraph() when
487
+ * a channel's graph is attached. It calls bindToContext on the shared object which will end up back here.
488
+ */
489
+ if (this.pendingHandlesToMakeVisible.has(channel.handle)) {
490
+ return;
491
+ }
492
+
493
+ this.bind(channel.handle);
494
+
495
+ // If our data store is local then add the channel to the queue
496
+ if (!this.localChannelContextQueue.has(channel.id)) {
497
+ this.localChannelContextQueue.set(
498
+ channel.id,
499
+ this.contexts.get(channel.id) as LocalChannelContextBase,
500
+ );
501
+ }
502
+ }
503
+
504
+ /**
505
+ * This function is called when a data store becomes root. It does the following:
506
+ *
507
+ * 1. Marks the data store locally visible in the container.
508
+ *
509
+ * 2. Attaches the graph of all the handles bound to it.
510
+ *
511
+ * 3. Calls into the data store context to mark it visible in the container too. If the container is globally
512
+ * visible, it will mark us globally visible. Otherwise, it will mark us globally visible when it becomes
513
+ * globally visible.
514
+ */
515
+ public makeVisibleAndAttachGraph() {
516
+ if (this.visibilityState !== VisibilityState.NotVisible) {
517
+ return;
518
+ }
519
+ this.visibilityState = VisibilityState.LocallyVisible;
520
+
521
+ this.pendingHandlesToMakeVisible.forEach((handle) => {
522
+ handle.attachGraph();
523
+ });
524
+ this.pendingHandlesToMakeVisible.clear();
525
+ this.bindToContext();
526
+ }
527
+
528
+ /**
529
+ * This function is called when a handle to this data store is added to a visible DDS.
530
+ */
531
+ public attachGraph() {
532
+ this.makeVisibleAndAttachGraph();
533
+ }
534
+
535
+ /**
536
+ * @deprecated - Not necessary if consumers add a new dataStore to the container by storing its handle.
537
+ * Binds this runtime to the container
538
+ * This includes the following:
539
+ * 1. Sending an Attach op that includes all existing state
540
+ * 2. Attaching the graph if the data store becomes attached.
541
+ */
542
+ public bindToContext() {
543
+ if (this.bindState !== BindState.NotBound) {
544
+ return;
545
+ }
546
+ this.bindState = BindState.Binding;
547
+ this.dataStoreContext.bindToContext();
548
+ this.bindState = BindState.Bound;
549
+ }
550
+
551
+ public bind(handle: IFluidHandle): void {
552
+ // If visible, attach the incoming handle's graph. Else, this will be done when we become visible.
553
+ if (this.visibilityState !== VisibilityState.NotVisible) {
554
+ handle.attachGraph();
555
+ return;
556
+ }
557
+ this.pendingHandlesToMakeVisible.add(handle);
558
+ }
559
+
560
+ public setConnectionState(connected: boolean, clientId?: string) {
561
+ this.verifyNotClosed();
562
+
563
+ for (const [, object] of this.contexts) {
564
+ object.setConnectionState(connected, clientId);
565
+ }
566
+
567
+ raiseConnectedEvent(this.logger, this, connected, clientId);
568
+ }
569
+
570
+ public getQuorum(): IQuorumClients {
571
+ return this.quorum;
572
+ }
573
+
574
+ public getAudience(): IAudience {
575
+ return this.audience;
576
+ }
577
+
578
+ public async uploadBlob(blob: ArrayBufferLike): Promise<IFluidHandle<ArrayBufferLike>> {
579
+ this.verifyNotClosed();
580
+
581
+ return this.dataStoreContext.uploadBlob(blob);
582
+ }
583
+
584
+ public process(message: ISequencedDocumentMessage, local: boolean, localOpMetadata: unknown) {
585
+ this.verifyNotClosed();
586
+
587
+ try {
588
+ // catches as data processing error whether or not they come from async pending queues
589
+ switch (message.type) {
590
+ case DataStoreMessageType.Attach: {
591
+ const attachMessage = message.contents as IAttachMessage;
592
+ const id = attachMessage.id;
593
+
594
+ // If a non-local operation then go and create the object
595
+ // Otherwise mark it as officially attached.
596
+ if (local) {
597
+ assert(
598
+ this.pendingAttach.has(id),
599
+ 0x17c /* "Unexpected attach (local) channel OP" */,
600
+ );
601
+ this.pendingAttach.delete(id);
602
+ } else {
603
+ assert(
604
+ !this.contexts.has(id),
605
+ 0x17d /* `Unexpected attach channel OP,
590
606
  is in pendingAttach set: ${this.pendingAttach.has(id)},
591
- is local channel contexts: ${this.contexts.get(id) instanceof LocalChannelContextBase}` */);
592
-
593
- const flatBlobs = new Map<string, ArrayBufferLike>();
594
- const snapshotTree = buildSnapshotTree(attachMessage.snapshot.entries, flatBlobs);
595
-
596
- const remoteChannelContext = new RemoteChannelContext(
597
- this,
598
- this.dataStoreContext,
599
- this.dataStoreContext.storage,
600
- (content, localContentMetadata) => this.submitChannelOp(id, content, localContentMetadata),
601
- (address: string) => this.setChannelDirty(address),
602
- (srcHandle: IFluidHandle, outboundHandle: IFluidHandle) =>
603
- this.addedGCOutboundReference(srcHandle, outboundHandle),
604
- id,
605
- snapshotTree,
606
- this.sharedObjectRegistry,
607
- flatBlobs,
608
- this.dataStoreContext.getCreateChildSummarizerNodeFn(
609
- id,
610
- {
611
- type: CreateSummarizerNodeSource.FromAttach,
612
- sequenceNumber: message.sequenceNumber,
613
- snapshot: attachMessage.snapshot,
614
- },
615
- ),
616
- async () => this.getChannelBaseGCDetails(id),
617
- attachMessage.type);
618
-
619
- this.contexts.set(id, remoteChannelContext);
620
- if (this.contextsDeferred.has(id)) {
621
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
622
- this.contextsDeferred.get(id)!.resolve(remoteChannelContext);
623
- } else {
624
- const deferred = new Deferred<IChannelContext>();
625
- deferred.resolve(remoteChannelContext);
626
- this.contextsDeferred.set(id, deferred);
627
- }
628
- }
629
- break;
630
- }
631
-
632
- case DataStoreMessageType.ChannelOp:
633
- this.processChannelOp(message, local, localOpMetadata);
634
- break;
635
- default:
636
- }
637
-
638
- this.emit("op", message);
639
- } catch (error) {
640
- throw DataProcessingError.wrapIfUnrecognized(error, "fluidDataStoreRuntimeFailedToProcessMessage", message);
641
- }
642
- }
643
-
644
- public processSignal(message: IInboundSignalMessage, local: boolean) {
645
- this.emit("signal", message, local);
646
- }
647
-
648
- private isChannelAttached(id: string): boolean {
649
- return (
650
- // Added in createChannel
651
- // Removed when bindChannel is called
652
- !this.notBoundedChannelContextSet.has(id)
653
- // Added in bindChannel only if this is not attached yet
654
- // Removed when this is attached by calling attachGraph
655
- && !this.localChannelContextQueue.has(id)
656
- // Added in attachChannel called by bindChannel
657
- // Removed when attach op is broadcast
658
- && !this.pendingAttach.has(id)
659
- );
660
- }
661
-
662
- /**
663
- * Returns the outbound routes of this channel. Currently, all contexts in this channel are considered
664
- * referenced and are hence outbound. This will change when we have root and non-root channel contexts.
665
- * The only root contexts will be considered as referenced.
666
- */
667
- private getOutboundRoutes(): string[] {
668
- const outboundRoutes: string[] = [];
669
- for (const [contextId] of this.contexts) {
670
- outboundRoutes.push(`${this.absolutePath}/${contextId}`);
671
- }
672
- return outboundRoutes;
673
- }
674
-
675
- /**
676
- * Updates the GC nodes of this channel. It does the following:
677
- * - Adds a back route to self to all its child GC nodes.
678
- * - Adds a node for this channel.
679
- * @param builder - The builder that contains the GC nodes for this channel's children.
680
- */
681
- private updateGCNodes(builder: GCDataBuilder) {
682
- // Add a back route to self in each child's GC nodes. If any child is referenced, then its parent should
683
- // be considered referenced as well.
684
- builder.addRouteToAllNodes(this.absolutePath);
685
-
686
- // Get the outbound routes and add a GC node for this channel.
687
- builder.addNode("/", this.getOutboundRoutes());
688
- }
689
-
690
- /**
691
- * Generates data used for garbage collection. This includes a list of GC nodes that represent this channel
692
- * including any of its child channel contexts. Each node has a set of outbound routes to other GC nodes in the
693
- * document. It does the following:
694
- *
695
- * 1. Calls into each child context to get its GC data.
696
- *
697
- * 2. Prefixes the child context's id to the GC nodes in the child's GC data. This makes sure that the node can be
698
- * identified as belonging to the child.
699
- *
700
- * 3. Adds a GC node for this channel to the nodes received from the children. All these nodes together represent
701
- * the GC data of this channel.
702
- *
703
- * @param fullGC - true to bypass optimizations and force full generation of GC data.
704
- */
705
- public async getGCData(fullGC: boolean = false): Promise<IGarbageCollectionData> {
706
- const builder = new GCDataBuilder();
707
- // Iterate over each channel context and get their GC data.
708
- await Promise.all(Array.from(this.contexts)
709
- .filter(([contextId, _]) => {
710
- // Get GC data only for attached contexts. Detached contexts are not connected in the GC reference
711
- // graph so any references they might have won't be connected as well.
712
- return this.isChannelAttached(contextId);
713
- }).map(async ([contextId, context]) => {
714
- const contextGCData = await context.getGCData(fullGC);
715
- // Prefix the child's id to the ids of its GC nodes so they can be identified as belonging to the child.
716
- // This also gradually builds the id of each node to be a path from the root.
717
- builder.prefixAndAddNodes(contextId, contextGCData.gcNodes);
718
- }));
719
-
720
- this.updateGCNodes(builder);
721
- return builder.getGCData();
722
- }
723
-
724
- /**
725
- * After GC has run, called to notify this channel of routes that are used in it. It calls the child contexts to
726
- * update their used routes.
727
- * @param usedRoutes - The routes that are used in all contexts in this channel.
728
- */
729
- public updateUsedRoutes(usedRoutes: string[]) {
730
- // Get a map of channel ids to routes used in it.
731
- const usedContextRoutes = unpackChildNodesUsedRoutes(usedRoutes);
732
-
733
- // Verify that the used routes are correct.
734
- for (const [id] of usedContextRoutes) {
735
- assert(this.contexts.has(id), 0x17e /* "Used route does not belong to any known context" */);
736
- }
737
-
738
- // Update the used routes in each context. Used routes is empty for unused context.
739
- for (const [contextId, context] of this.contexts) {
740
- context.updateUsedRoutes(usedContextRoutes.get(contextId) ?? []);
741
- }
742
- }
743
-
744
- /**
745
- * Called when a new outbound reference is added to another node. This is used by garbage collection to identify
746
- * all references added in the system.
747
- * @param srcHandle - The handle of the node that added the reference.
748
- * @param outboundHandle - The handle of the outbound node that is referenced.
749
- */
750
- private addedGCOutboundReference(srcHandle: IFluidHandle, outboundHandle: IFluidHandle) {
751
- this.dataStoreContext.addedGCOutboundReference?.(srcHandle, outboundHandle);
752
- }
753
-
754
- /**
755
- * Returns the base GC details for the channel with the given id. This is used to initialize its GC state.
756
- * @param channelId - The id of the channel context that is asked for the initial GC details.
757
- * @returns the requested channel's base GC details.
758
- */
759
- private async getChannelBaseGCDetails(channelId: string): Promise<IGarbageCollectionDetailsBase> {
760
- let channelBaseGCDetails = (await this.channelsBaseGCDetails).get(channelId);
761
- if (channelBaseGCDetails === undefined) {
762
- channelBaseGCDetails = {};
763
- } else if (channelBaseGCDetails.gcData?.gcNodes !== undefined) {
764
- // Note: if the child channel has an explicit handle route to its parent, it will be removed here and
765
- // expected to be added back by the parent when getGCData is called.
766
- removeRouteFromAllNodes(channelBaseGCDetails.gcData.gcNodes, this.absolutePath);
767
- }
768
-
769
- // Currently, channel context's are always considered used. So, it there are no used routes for it, we still
770
- // need to mark it as used. Add self-route (empty string) to the channel context's used routes.
771
- if (channelBaseGCDetails.usedRoutes === undefined || channelBaseGCDetails.usedRoutes.length === 0) {
772
- channelBaseGCDetails.usedRoutes = [""];
773
- }
774
- return channelBaseGCDetails;
775
- }
776
-
777
- /**
778
- * Returns a summary at the current sequence number.
779
- * @param fullTree - true to bypass optimizations and force a full summary tree
780
- * @param trackState - This tells whether we should track state from this summary.
781
- * @param telemetryContext - summary data passed through the layers for telemetry purposes
782
- */
783
- public async summarize(
784
- fullTree: boolean = false,
785
- trackState: boolean = true,
786
- telemetryContext?: ITelemetryContext,
787
- ): Promise<ISummaryTreeWithStats> {
788
- const summaryBuilder = new SummaryTreeBuilder();
789
-
790
- // Iterate over each data store and ask it to summarize
791
- await Promise.all(Array.from(this.contexts)
792
- .filter(([contextId, _]) => {
793
- const isAttached = this.isChannelAttached(contextId);
794
- // We are not expecting local dds! Summary may not capture local state.
795
- assert(isAttached, 0x17f /* "Not expecting detached channels during summarize" */);
796
- // If the object is registered - and we have received the sequenced op creating the object
797
- // (i.e. it has a base mapping) - then we go ahead and summarize
798
- return isAttached;
799
- }).map(async ([contextId, context]) => {
800
- const contextSummary = await context.summarize(fullTree, trackState, telemetryContext);
801
- summaryBuilder.addWithStats(contextId, contextSummary);
802
- }));
803
-
804
- return summaryBuilder.getSummaryTree();
805
- }
806
-
807
- public getAttachSummary(telemetryContext?: ITelemetryContext): ISummaryTreeWithStats {
808
- /**
809
- * back-compat 0.59.1000 - getAttachSummary() is called when making a data store globally visible (previously
810
- * attaching state). Ideally, attachGraph() should have already be called making it locally visible. However,
811
- * before visibility state was added, this may not have been the case and getAttachSummary() could be called:
812
- *
813
- * 1. Before attaching the data store - When a detached container is attached.
814
- *
815
- * 2. After attaching the data store - When a data store is created and bound in an attached container.
816
- *
817
- * The basic idea is that all local object should become locally visible before they are globally visible.
818
- */
819
- this.attachGraph();
820
-
821
- // This assert cannot be added now due to back-compat. To be uncommented when the following issue is fixed -
822
- // https://github.com/microsoft/FluidFramework/issues/9688.
823
- //
824
- // assert(this.visibilityState === VisibilityState.LocallyVisible,
825
- // "The data store should be locally visible when generating attach summary",
826
- // );
827
-
828
- const summaryBuilder = new SummaryTreeBuilder();
829
-
830
- // Craft the .attributes file for each shared object
831
- for (const [contextId, context] of this.contexts) {
832
- if (!(context instanceof LocalChannelContextBase)) {
833
- throw new LoggingError("Should only be called with local channel handles");
834
- }
835
-
836
- if (!this.notBoundedChannelContextSet.has(contextId)) {
837
- let summaryTree: ISummaryTreeWithStats;
838
- if (context.isLoaded) {
839
- const contextSummary = context.getAttachSummary(telemetryContext);
840
- assert(
841
- contextSummary.summary.type === SummaryType.Tree,
842
- 0x180 /* "getAttachSummary should always return a tree" */);
843
- summaryTree = { stats: contextSummary.stats, summary: contextSummary.summary };
844
- } else {
845
- // If this channel is not yet loaded, then there should be no changes in the snapshot from which
846
- // it was created as it is detached container. So just use the previous snapshot.
847
- assert(!!this.dataStoreContext.baseSnapshot,
848
- 0x181 /* "BaseSnapshot should be there as detached container loaded from snapshot" */);
849
- summaryTree = convertSnapshotTreeToSummaryTree(this.dataStoreContext.baseSnapshot.trees[contextId]);
850
- }
851
- summaryBuilder.addWithStats(contextId, summaryTree);
852
- }
853
- }
854
-
855
- return summaryBuilder.getSummaryTree();
856
- }
857
-
858
- public submitMessage(type: DataStoreMessageType, content: any, localOpMetadata: unknown) {
859
- this.submit(type, content, localOpMetadata);
860
- }
861
-
862
- public submitSignal(type: string, content: any) {
863
- this.verifyNotClosed();
864
- return this.dataStoreContext.submitSignal(type, content);
865
- }
866
-
867
- /**
868
- * Will return when the data store is attached.
869
- */
870
- public async waitAttached(): Promise<void> {
871
- return this.deferredAttached.promise;
872
- }
873
-
874
- /**
875
- * Attach channel should only be called after the data store has been attached
876
- */
877
- private attachChannel(channel: IChannel): void {
878
- this.verifyNotClosed();
879
- // If this handle is already attached no need to attach again.
880
- if (channel.handle.isAttached) {
881
- return;
882
- }
883
-
884
- channel.handle.attachGraph();
885
-
886
- assert(this.isAttached, 0x182 /* "Data store should be attached to attach the channel." */);
887
- assert(this.visibilityState === VisibilityState.GloballyVisible,
888
- 0x2d0 /* "Data store should be globally visible to attach channels." */);
889
-
890
- const summarizeResult = summarizeChannel(channel, true /* fullTree */, false /* trackState */);
891
- // Attach message needs the summary in ITree format. Convert the ISummaryTree into an ITree.
892
- const snapshot = convertSummaryTreeToITree(summarizeResult.summary);
893
-
894
- const message: IAttachMessage = {
895
- id: channel.id,
896
- snapshot,
897
- type: channel.attributes.type,
898
- };
899
- this.pendingAttach.set(channel.id, message);
900
- this.submit(DataStoreMessageType.Attach, message);
901
-
902
- const context = this.contexts.get(channel.id) as LocalChannelContextBase;
903
- context.makeVisible();
904
- }
905
-
906
- private submitChannelOp(address: string, contents: any, localOpMetadata: unknown) {
907
- const envelope: IEnvelope = { address, contents };
908
- this.submit(DataStoreMessageType.ChannelOp, envelope, localOpMetadata);
909
- }
910
-
911
- private submit(
912
- type: DataStoreMessageType,
913
- content: any,
914
- localOpMetadata: unknown = undefined): void {
915
- this.verifyNotClosed();
916
- this.dataStoreContext.submitMessage(type, content, localOpMetadata);
917
- }
918
-
919
- /**
920
- * For messages of type MessageType.Operation, finds the right channel and asks it to resubmit the message.
921
- * For all other messages, just submit it again.
922
- * This typically happens when we reconnect and there are unacked messages.
923
- * @param content - The content of the original message.
924
- * @param localOpMetadata - The local metadata associated with the original message.
925
- */
926
- public reSubmit(type: DataStoreMessageType, content: any, localOpMetadata: unknown) {
927
- this.verifyNotClosed();
928
-
929
- switch (type) {
930
- case DataStoreMessageType.ChannelOp:
931
- {
932
- // For Operations, find the right channel and trigger resubmission on it.
933
- const envelope = content as IEnvelope;
934
- const channelContext = this.contexts.get(envelope.address);
935
- assert(!!channelContext, 0x183 /* "There should be a channel context for the op" */);
936
- channelContext.reSubmit(envelope.contents, localOpMetadata);
937
- break;
938
- }
939
- case DataStoreMessageType.Attach:
940
- // For Attach messages, just submit them again.
941
- this.submit(type, content, localOpMetadata);
942
- break;
943
- default:
944
- unreachableCase(type);
945
- }
946
- }
947
-
948
- /**
949
- * Revert a local op.
950
- * @param content - The content of the original message.
951
- * @param localOpMetadata - The local metadata associated with the original message.
952
- */
953
- public rollback?(type: DataStoreMessageType, content: any, localOpMetadata: unknown) {
954
- this.verifyNotClosed();
955
-
956
- switch (type) {
957
- case DataStoreMessageType.ChannelOp:
958
- {
959
- // For Operations, find the right channel and trigger resubmission on it.
960
- const envelope = content as IEnvelope;
961
- const channelContext = this.contexts.get(envelope.address);
962
- assert(!!channelContext, 0x2ed /* "There should be a channel context for the op" */);
963
- channelContext.rollback(envelope.contents, localOpMetadata);
964
- break;
965
- }
966
- default:
967
- throw new LoggingError(`Can't rollback ${type} message`);
968
- }
969
- }
970
-
971
- public async applyStashedOp(content: any): Promise<unknown> {
972
- const envelope = content as IEnvelope;
973
- const channelContext = this.contexts.get(envelope.address);
974
- assert(!!channelContext, 0x184 /* "There should be a channel context for the op" */);
975
- await channelContext.getChannel();
976
- return channelContext.applyStashedOp(envelope.contents);
977
- }
978
-
979
- private setChannelDirty(address: string): void {
980
- this.verifyNotClosed();
981
- this.dataStoreContext.setChannelDirty(address);
982
- }
983
-
984
- private processChannelOp(message: ISequencedDocumentMessage, local: boolean, localOpMetadata: unknown) {
985
- this.verifyNotClosed();
986
-
987
- const envelope = message.contents as IEnvelope;
988
-
989
- const transformed: ISequencedDocumentMessage = {
990
- ...message,
991
- contents: envelope.contents,
992
- };
993
-
994
- const channelContext = this.contexts.get(envelope.address);
995
- assert(!!channelContext, 0x185 /* "Channel not found" */);
996
- channelContext.processOp(transformed, local, localOpMetadata);
997
-
998
- return channelContext;
999
- }
1000
-
1001
- private attachListener() {
1002
- this.setMaxListeners(Number.MAX_SAFE_INTEGER);
1003
- this.dataStoreContext.once("attaching", () => {
1004
- /**
1005
- * back-compat 0.59.1000 - Ideally, attachGraph() should have already been called making the data store
1006
- * locally visible. However, before visibility state was added, this may not have been the case and data
1007
- * store can move to "attaching" state in 2 scenarios:
1008
- * 1) Before attachGraph() is called - When a data store is created and bound in an attached container.
1009
- * 2) After attachGraph() is called - When a detached container is attached.
1010
- *
1011
- * The basic idea is that all local object should become locally visible before they are globally visible.
1012
- */
1013
- this.attachGraph();
1014
-
1015
- this._attachState = AttachState.Attaching;
1016
-
1017
- assert(this.visibilityState === VisibilityState.LocallyVisible,
1018
- 0x2d1 /* "Data store should be locally visible before it can become globally visible." */);
1019
-
1020
- // Mark the data store globally visible and make its child channels visible as well.
1021
- this.visibilityState = VisibilityState.GloballyVisible;
1022
- this.localChannelContextQueue.forEach((channel) => {
1023
- channel.makeVisible();
1024
- });
1025
- this.localChannelContextQueue.clear();
1026
-
1027
- // This promise resolution will be moved to attached event once we fix the scheduler.
1028
- this.deferredAttached.resolve();
1029
- this.emit("attaching");
1030
- });
1031
- this.dataStoreContext.once("attached", () => {
1032
- assert(this.visibilityState === VisibilityState.GloballyVisible,
1033
- 0x2d2 /* "Data store should be globally visible when its attached." */);
1034
- this._attachState = AttachState.Attached;
1035
- this.emit("attached");
1036
- });
1037
- }
1038
-
1039
- private verifyNotClosed() {
1040
- if (this._disposed) {
1041
- throw new LoggingError("Runtime is closed");
1042
- }
1043
- }
1044
-
1045
- /**
1046
- * Summarizer client should not have local changes. These changes can become part of the summary and can break
1047
- * eventual consistency. For example, the next summary (say at ref seq# 100) may contain these changes whereas
1048
- * other clients that are up-to-date till seq# 100 may not have them yet.
1049
- */
1050
- private identifyLocalChangeInSummarizer(
1051
- eventName: string,
1052
- channelId: string,
1053
- channelType: string,
1054
- ) {
1055
- if (this.clientDetails.type !== "summarizer" || this.localChangesTelemetryCount <= 0) {
1056
- return;
1057
- }
1058
-
1059
- // Log a telemetry if there are local changes in the summarizer. This will give us data on how often
1060
- // this is happening and which data stores do this. The eventual goal is to disallow local changes
1061
- // in the summarizer and the data will help us plan this.
1062
- this.mc.logger.sendTelemetryEvent({
1063
- eventName,
1064
- channelType,
1065
- channelId: {
1066
- value: channelId,
1067
- tag: TelemetryDataTag.CodeArtifact,
1068
- },
1069
- fluidDataStoreId: {
1070
- value: this.id,
1071
- tag: TelemetryDataTag.CodeArtifact,
1072
- },
1073
- fluidDataStorePackagePath: packagePathToTelemetryProperty(
1074
- this.dataStoreContext.packagePath,
1075
- ),
1076
- stack: generateStack(),
1077
- });
1078
- this.localChangesTelemetryCount--;
1079
- }
607
+ is local channel contexts: ${this.contexts.get(id) instanceof LocalChannelContextBase}` */,
608
+ );
609
+
610
+ const flatBlobs = new Map<string, ArrayBufferLike>();
611
+ const snapshotTree = buildSnapshotTree(
612
+ attachMessage.snapshot.entries,
613
+ flatBlobs,
614
+ );
615
+
616
+ const remoteChannelContext = new RemoteChannelContext(
617
+ this,
618
+ this.dataStoreContext,
619
+ this.dataStoreContext.storage,
620
+ (content, localContentMetadata) =>
621
+ this.submitChannelOp(id, content, localContentMetadata),
622
+ (address: string) => this.setChannelDirty(address),
623
+ (srcHandle: IFluidHandle, outboundHandle: IFluidHandle) =>
624
+ this.addedGCOutboundReference(srcHandle, outboundHandle),
625
+ id,
626
+ snapshotTree,
627
+ this.sharedObjectRegistry,
628
+ flatBlobs,
629
+ this.dataStoreContext.getCreateChildSummarizerNodeFn(id, {
630
+ type: CreateSummarizerNodeSource.FromAttach,
631
+ sequenceNumber: message.sequenceNumber,
632
+ snapshot: attachMessage.snapshot,
633
+ }),
634
+ async () => this.getChannelBaseGCDetails(id),
635
+ attachMessage.type,
636
+ );
637
+
638
+ this.contexts.set(id, remoteChannelContext);
639
+ if (this.contextsDeferred.has(id)) {
640
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
641
+ this.contextsDeferred.get(id)!.resolve(remoteChannelContext);
642
+ } else {
643
+ const deferred = new Deferred<IChannelContext>();
644
+ deferred.resolve(remoteChannelContext);
645
+ this.contextsDeferred.set(id, deferred);
646
+ }
647
+ }
648
+ break;
649
+ }
650
+
651
+ case DataStoreMessageType.ChannelOp:
652
+ this.processChannelOp(message, local, localOpMetadata);
653
+ break;
654
+ default:
655
+ }
656
+
657
+ this.emit("op", message);
658
+ } catch (error) {
659
+ throw DataProcessingError.wrapIfUnrecognized(
660
+ error,
661
+ "fluidDataStoreRuntimeFailedToProcessMessage",
662
+ message,
663
+ );
664
+ }
665
+ }
666
+
667
+ public processSignal(message: IInboundSignalMessage, local: boolean) {
668
+ this.emit("signal", message, local);
669
+ }
670
+
671
+ private isChannelAttached(id: string): boolean {
672
+ return (
673
+ // Added in createChannel
674
+ // Removed when bindChannel is called
675
+ !this.notBoundedChannelContextSet.has(id) &&
676
+ // Added in bindChannel only if this is not attached yet
677
+ // Removed when this is attached by calling attachGraph
678
+ !this.localChannelContextQueue.has(id) &&
679
+ // Added in attachChannel called by bindChannel
680
+ // Removed when attach op is broadcast
681
+ !this.pendingAttach.has(id)
682
+ );
683
+ }
684
+
685
+ /**
686
+ * Returns the outbound routes of this channel. Currently, all contexts in this channel are considered
687
+ * referenced and are hence outbound. This will change when we have root and non-root channel contexts.
688
+ * The only root contexts will be considered as referenced.
689
+ */
690
+ private getOutboundRoutes(): string[] {
691
+ const outboundRoutes: string[] = [];
692
+ for (const [contextId] of this.contexts) {
693
+ outboundRoutes.push(`${this.absolutePath}/${contextId}`);
694
+ }
695
+ return outboundRoutes;
696
+ }
697
+
698
+ /**
699
+ * Updates the GC nodes of this channel. It does the following:
700
+ * - Adds a back route to self to all its child GC nodes.
701
+ * - Adds a node for this channel.
702
+ * @param builder - The builder that contains the GC nodes for this channel's children.
703
+ */
704
+ private updateGCNodes(builder: GCDataBuilder) {
705
+ // Add a back route to self in each child's GC nodes. If any child is referenced, then its parent should
706
+ // be considered referenced as well.
707
+ builder.addRouteToAllNodes(this.absolutePath);
708
+
709
+ // Get the outbound routes and add a GC node for this channel.
710
+ builder.addNode("/", this.getOutboundRoutes());
711
+ }
712
+
713
+ /**
714
+ * Generates data used for garbage collection. This includes a list of GC nodes that represent this channel
715
+ * including any of its child channel contexts. Each node has a set of outbound routes to other GC nodes in the
716
+ * document. It does the following:
717
+ *
718
+ * 1. Calls into each child context to get its GC data.
719
+ *
720
+ * 2. Prefixes the child context's id to the GC nodes in the child's GC data. This makes sure that the node can be
721
+ * identified as belonging to the child.
722
+ *
723
+ * 3. Adds a GC node for this channel to the nodes received from the children. All these nodes together represent
724
+ * the GC data of this channel.
725
+ *
726
+ * @param fullGC - true to bypass optimizations and force full generation of GC data.
727
+ */
728
+ public async getGCData(fullGC: boolean = false): Promise<IGarbageCollectionData> {
729
+ const builder = new GCDataBuilder();
730
+ // Iterate over each channel context and get their GC data.
731
+ await Promise.all(
732
+ Array.from(this.contexts)
733
+ .filter(([contextId, _]) => {
734
+ // Get GC data only for attached contexts. Detached contexts are not connected in the GC reference
735
+ // graph so any references they might have won't be connected as well.
736
+ return this.isChannelAttached(contextId);
737
+ })
738
+ .map(async ([contextId, context]) => {
739
+ const contextGCData = await context.getGCData(fullGC);
740
+ // Prefix the child's id to the ids of its GC nodes so they can be identified as belonging to the child.
741
+ // This also gradually builds the id of each node to be a path from the root.
742
+ builder.prefixAndAddNodes(contextId, contextGCData.gcNodes);
743
+ }),
744
+ );
745
+
746
+ this.updateGCNodes(builder);
747
+ return builder.getGCData();
748
+ }
749
+
750
+ /**
751
+ * After GC has run, called to notify this channel of routes that are used in it. It calls the child contexts to
752
+ * update their used routes.
753
+ * @param usedRoutes - The routes that are used in all contexts in this channel.
754
+ */
755
+ public updateUsedRoutes(usedRoutes: string[]) {
756
+ // Get a map of channel ids to routes used in it.
757
+ const usedContextRoutes = unpackChildNodesUsedRoutes(usedRoutes);
758
+
759
+ // Verify that the used routes are correct.
760
+ for (const [id] of usedContextRoutes) {
761
+ assert(
762
+ this.contexts.has(id),
763
+ 0x17e /* "Used route does not belong to any known context" */,
764
+ );
765
+ }
766
+
767
+ // Update the used routes in each context. Used routes is empty for unused context.
768
+ for (const [contextId, context] of this.contexts) {
769
+ context.updateUsedRoutes(usedContextRoutes.get(contextId) ?? []);
770
+ }
771
+ }
772
+
773
+ /**
774
+ * Called when a new outbound reference is added to another node. This is used by garbage collection to identify
775
+ * all references added in the system.
776
+ * @param srcHandle - The handle of the node that added the reference.
777
+ * @param outboundHandle - The handle of the outbound node that is referenced.
778
+ */
779
+ private addedGCOutboundReference(srcHandle: IFluidHandle, outboundHandle: IFluidHandle) {
780
+ this.dataStoreContext.addedGCOutboundReference?.(srcHandle, outboundHandle);
781
+ }
782
+
783
+ /**
784
+ * Returns the base GC details for the channel with the given id. This is used to initialize its GC state.
785
+ * @param channelId - The id of the channel context that is asked for the initial GC details.
786
+ * @returns the requested channel's base GC details.
787
+ */
788
+ private async getChannelBaseGCDetails(
789
+ channelId: string,
790
+ ): Promise<IGarbageCollectionDetailsBase> {
791
+ let channelBaseGCDetails = (await this.channelsBaseGCDetails).get(channelId);
792
+ if (channelBaseGCDetails === undefined) {
793
+ channelBaseGCDetails = {};
794
+ } else if (channelBaseGCDetails.gcData?.gcNodes !== undefined) {
795
+ // Note: if the child channel has an explicit handle route to its parent, it will be removed here and
796
+ // expected to be added back by the parent when getGCData is called.
797
+ removeRouteFromAllNodes(channelBaseGCDetails.gcData.gcNodes, this.absolutePath);
798
+ }
799
+
800
+ // Currently, channel context's are always considered used. So, it there are no used routes for it, we still
801
+ // need to mark it as used. Add self-route (empty string) to the channel context's used routes.
802
+ if (
803
+ channelBaseGCDetails.usedRoutes === undefined ||
804
+ channelBaseGCDetails.usedRoutes.length === 0
805
+ ) {
806
+ channelBaseGCDetails.usedRoutes = [""];
807
+ }
808
+ return channelBaseGCDetails;
809
+ }
810
+
811
+ /**
812
+ * Returns a summary at the current sequence number.
813
+ * @param fullTree - true to bypass optimizations and force a full summary tree
814
+ * @param trackState - This tells whether we should track state from this summary.
815
+ * @param telemetryContext - summary data passed through the layers for telemetry purposes
816
+ */
817
+ public async summarize(
818
+ fullTree: boolean = false,
819
+ trackState: boolean = true,
820
+ telemetryContext?: ITelemetryContext,
821
+ ): Promise<ISummaryTreeWithStats> {
822
+ const summaryBuilder = new SummaryTreeBuilder();
823
+
824
+ // Iterate over each data store and ask it to summarize
825
+ await Promise.all(
826
+ Array.from(this.contexts)
827
+ .filter(([contextId, _]) => {
828
+ const isAttached = this.isChannelAttached(contextId);
829
+ // We are not expecting local dds! Summary may not capture local state.
830
+ assert(
831
+ isAttached,
832
+ 0x17f /* "Not expecting detached channels during summarize" */,
833
+ );
834
+ // If the object is registered - and we have received the sequenced op creating the object
835
+ // (i.e. it has a base mapping) - then we go ahead and summarize
836
+ return isAttached;
837
+ })
838
+ .map(async ([contextId, context]) => {
839
+ const contextSummary = await context.summarize(
840
+ fullTree,
841
+ trackState,
842
+ telemetryContext,
843
+ );
844
+ summaryBuilder.addWithStats(contextId, contextSummary);
845
+ }),
846
+ );
847
+
848
+ return summaryBuilder.getSummaryTree();
849
+ }
850
+
851
+ public getAttachSummary(telemetryContext?: ITelemetryContext): ISummaryTreeWithStats {
852
+ /**
853
+ * back-compat 0.59.1000 - getAttachSummary() is called when making a data store globally visible (previously
854
+ * attaching state). Ideally, attachGraph() should have already be called making it locally visible. However,
855
+ * before visibility state was added, this may not have been the case and getAttachSummary() could be called:
856
+ *
857
+ * 1. Before attaching the data store - When a detached container is attached.
858
+ *
859
+ * 2. After attaching the data store - When a data store is created and bound in an attached container.
860
+ *
861
+ * The basic idea is that all local object should become locally visible before they are globally visible.
862
+ */
863
+ this.attachGraph();
864
+
865
+ // This assert cannot be added now due to back-compat. To be uncommented when the following issue is fixed -
866
+ // https://github.com/microsoft/FluidFramework/issues/9688.
867
+ //
868
+ // assert(this.visibilityState === VisibilityState.LocallyVisible,
869
+ // "The data store should be locally visible when generating attach summary",
870
+ // );
871
+
872
+ const summaryBuilder = new SummaryTreeBuilder();
873
+
874
+ // Craft the .attributes file for each shared object
875
+ for (const [contextId, context] of this.contexts) {
876
+ if (!(context instanceof LocalChannelContextBase)) {
877
+ throw new LoggingError("Should only be called with local channel handles");
878
+ }
879
+
880
+ if (!this.notBoundedChannelContextSet.has(contextId)) {
881
+ let summaryTree: ISummaryTreeWithStats;
882
+ if (context.isLoaded) {
883
+ const contextSummary = context.getAttachSummary(telemetryContext);
884
+ assert(
885
+ contextSummary.summary.type === SummaryType.Tree,
886
+ 0x180 /* "getAttachSummary should always return a tree" */,
887
+ );
888
+ summaryTree = { stats: contextSummary.stats, summary: contextSummary.summary };
889
+ } else {
890
+ // If this channel is not yet loaded, then there should be no changes in the snapshot from which
891
+ // it was created as it is detached container. So just use the previous snapshot.
892
+ assert(
893
+ !!this.dataStoreContext.baseSnapshot,
894
+ 0x181 /* "BaseSnapshot should be there as detached container loaded from snapshot" */,
895
+ );
896
+ summaryTree = convertSnapshotTreeToSummaryTree(
897
+ this.dataStoreContext.baseSnapshot.trees[contextId],
898
+ );
899
+ }
900
+ summaryBuilder.addWithStats(contextId, summaryTree);
901
+ }
902
+ }
903
+
904
+ return summaryBuilder.getSummaryTree();
905
+ }
906
+
907
+ public submitMessage(type: DataStoreMessageType, content: any, localOpMetadata: unknown) {
908
+ this.submit(type, content, localOpMetadata);
909
+ }
910
+
911
+ public submitSignal(type: string, content: any) {
912
+ this.verifyNotClosed();
913
+ return this.dataStoreContext.submitSignal(type, content);
914
+ }
915
+
916
+ /**
917
+ * Will return when the data store is attached.
918
+ */
919
+ public async waitAttached(): Promise<void> {
920
+ return this.deferredAttached.promise;
921
+ }
922
+
923
+ /**
924
+ * Attach channel should only be called after the data store has been attached
925
+ */
926
+ private attachChannel(channel: IChannel): void {
927
+ this.verifyNotClosed();
928
+ // If this handle is already attached no need to attach again.
929
+ if (channel.handle.isAttached) {
930
+ return;
931
+ }
932
+
933
+ channel.handle.attachGraph();
934
+
935
+ assert(this.isAttached, 0x182 /* "Data store should be attached to attach the channel." */);
936
+ assert(
937
+ this.visibilityState === VisibilityState.GloballyVisible,
938
+ 0x2d0 /* "Data store should be globally visible to attach channels." */,
939
+ );
940
+
941
+ const summarizeResult = summarizeChannel(
942
+ channel,
943
+ true /* fullTree */,
944
+ false /* trackState */,
945
+ );
946
+ // Attach message needs the summary in ITree format. Convert the ISummaryTree into an ITree.
947
+ const snapshot = convertSummaryTreeToITree(summarizeResult.summary);
948
+
949
+ const message: IAttachMessage = {
950
+ id: channel.id,
951
+ snapshot,
952
+ type: channel.attributes.type,
953
+ };
954
+ this.pendingAttach.set(channel.id, message);
955
+ this.submit(DataStoreMessageType.Attach, message);
956
+
957
+ const context = this.contexts.get(channel.id) as LocalChannelContextBase;
958
+ context.makeVisible();
959
+ }
960
+
961
+ private submitChannelOp(address: string, contents: any, localOpMetadata: unknown) {
962
+ const envelope: IEnvelope = { address, contents };
963
+ this.submit(DataStoreMessageType.ChannelOp, envelope, localOpMetadata);
964
+ }
965
+
966
+ private submit(
967
+ type: DataStoreMessageType,
968
+ content: any,
969
+ localOpMetadata: unknown = undefined,
970
+ ): void {
971
+ this.verifyNotClosed();
972
+ this.dataStoreContext.submitMessage(type, content, localOpMetadata);
973
+ }
974
+
975
+ /**
976
+ * For messages of type MessageType.Operation, finds the right channel and asks it to resubmit the message.
977
+ * For all other messages, just submit it again.
978
+ * This typically happens when we reconnect and there are unacked messages.
979
+ * @param content - The content of the original message.
980
+ * @param localOpMetadata - The local metadata associated with the original message.
981
+ */
982
+ public reSubmit(type: DataStoreMessageType, content: any, localOpMetadata: unknown) {
983
+ this.verifyNotClosed();
984
+
985
+ switch (type) {
986
+ case DataStoreMessageType.ChannelOp: {
987
+ // For Operations, find the right channel and trigger resubmission on it.
988
+ const envelope = content as IEnvelope;
989
+ const channelContext = this.contexts.get(envelope.address);
990
+ assert(
991
+ !!channelContext,
992
+ 0x183 /* "There should be a channel context for the op" */,
993
+ );
994
+ channelContext.reSubmit(envelope.contents, localOpMetadata);
995
+ break;
996
+ }
997
+ case DataStoreMessageType.Attach:
998
+ // For Attach messages, just submit them again.
999
+ this.submit(type, content, localOpMetadata);
1000
+ break;
1001
+ default:
1002
+ unreachableCase(type);
1003
+ }
1004
+ }
1005
+
1006
+ /**
1007
+ * Revert a local op.
1008
+ * @param content - The content of the original message.
1009
+ * @param localOpMetadata - The local metadata associated with the original message.
1010
+ */
1011
+ public rollback?(type: DataStoreMessageType, content: any, localOpMetadata: unknown) {
1012
+ this.verifyNotClosed();
1013
+
1014
+ switch (type) {
1015
+ case DataStoreMessageType.ChannelOp: {
1016
+ // For Operations, find the right channel and trigger resubmission on it.
1017
+ const envelope = content as IEnvelope;
1018
+ const channelContext = this.contexts.get(envelope.address);
1019
+ assert(
1020
+ !!channelContext,
1021
+ 0x2ed /* "There should be a channel context for the op" */,
1022
+ );
1023
+ channelContext.rollback(envelope.contents, localOpMetadata);
1024
+ break;
1025
+ }
1026
+ default:
1027
+ throw new LoggingError(`Can't rollback ${type} message`);
1028
+ }
1029
+ }
1030
+
1031
+ public async applyStashedOp(content: any): Promise<unknown> {
1032
+ const envelope = content as IEnvelope;
1033
+ const channelContext = this.contexts.get(envelope.address);
1034
+ assert(!!channelContext, 0x184 /* "There should be a channel context for the op" */);
1035
+ await channelContext.getChannel();
1036
+ return channelContext.applyStashedOp(envelope.contents);
1037
+ }
1038
+
1039
+ private setChannelDirty(address: string): void {
1040
+ this.verifyNotClosed();
1041
+ this.dataStoreContext.setChannelDirty(address);
1042
+ }
1043
+
1044
+ private processChannelOp(
1045
+ message: ISequencedDocumentMessage,
1046
+ local: boolean,
1047
+ localOpMetadata: unknown,
1048
+ ) {
1049
+ this.verifyNotClosed();
1050
+
1051
+ const envelope = message.contents as IEnvelope;
1052
+
1053
+ const transformed: ISequencedDocumentMessage = {
1054
+ ...message,
1055
+ contents: envelope.contents,
1056
+ };
1057
+
1058
+ const channelContext = this.contexts.get(envelope.address);
1059
+ assert(!!channelContext, 0x185 /* "Channel not found" */);
1060
+ channelContext.processOp(transformed, local, localOpMetadata);
1061
+
1062
+ return channelContext;
1063
+ }
1064
+
1065
+ private attachListener() {
1066
+ this.setMaxListeners(Number.MAX_SAFE_INTEGER);
1067
+ this.dataStoreContext.once("attaching", () => {
1068
+ /**
1069
+ * back-compat 0.59.1000 - Ideally, attachGraph() should have already been called making the data store
1070
+ * locally visible. However, before visibility state was added, this may not have been the case and data
1071
+ * store can move to "attaching" state in 2 scenarios:
1072
+ * 1) Before attachGraph() is called - When a data store is created and bound in an attached container.
1073
+ * 2) After attachGraph() is called - When a detached container is attached.
1074
+ *
1075
+ * The basic idea is that all local object should become locally visible before they are globally visible.
1076
+ */
1077
+ this.attachGraph();
1078
+
1079
+ this._attachState = AttachState.Attaching;
1080
+
1081
+ assert(
1082
+ this.visibilityState === VisibilityState.LocallyVisible,
1083
+ 0x2d1 /* "Data store should be locally visible before it can become globally visible." */,
1084
+ );
1085
+
1086
+ // Mark the data store globally visible and make its child channels visible as well.
1087
+ this.visibilityState = VisibilityState.GloballyVisible;
1088
+ this.localChannelContextQueue.forEach((channel) => {
1089
+ channel.makeVisible();
1090
+ });
1091
+ this.localChannelContextQueue.clear();
1092
+
1093
+ // This promise resolution will be moved to attached event once we fix the scheduler.
1094
+ this.deferredAttached.resolve();
1095
+ this.emit("attaching");
1096
+ });
1097
+ this.dataStoreContext.once("attached", () => {
1098
+ assert(
1099
+ this.visibilityState === VisibilityState.GloballyVisible,
1100
+ 0x2d2 /* "Data store should be globally visible when its attached." */,
1101
+ );
1102
+ this._attachState = AttachState.Attached;
1103
+ this.emit("attached");
1104
+ });
1105
+ }
1106
+
1107
+ private verifyNotClosed() {
1108
+ if (this._disposed) {
1109
+ throw new LoggingError("Runtime is closed");
1110
+ }
1111
+ }
1080
1112
  }
1081
1113
 
1082
1114
  /**
@@ -1086,17 +1118,18 @@ export class FluidDataStoreRuntime extends
1086
1118
  * @param requestHandler - request handler to mix in
1087
1119
  */
1088
1120
  export const mixinRequestHandler = (
1089
- requestHandler: (request: IRequest, runtime: FluidDataStoreRuntime) => Promise<IResponse>,
1090
- Base: typeof FluidDataStoreRuntime = FluidDataStoreRuntime,
1091
- ) => class RuntimeWithRequestHandler extends Base {
1092
- public async request(request: IRequest) {
1093
- const response = await super.request(request);
1094
- if (response.status === 404) {
1095
- return requestHandler(request, this);
1096
- }
1097
- return response;
1098
- }
1099
- } as typeof FluidDataStoreRuntime;
1121
+ requestHandler: (request: IRequest, runtime: FluidDataStoreRuntime) => Promise<IResponse>,
1122
+ Base: typeof FluidDataStoreRuntime = FluidDataStoreRuntime,
1123
+ ) =>
1124
+ class RuntimeWithRequestHandler extends Base {
1125
+ public async request(request: IRequest) {
1126
+ const response = await super.request(request);
1127
+ if (response.status === 404) {
1128
+ return requestHandler(request, this);
1129
+ }
1130
+ return response;
1131
+ }
1132
+ } as typeof FluidDataStoreRuntime;
1100
1133
 
1101
1134
  /**
1102
1135
  * Mixin class that adds await for DataObject to finish initialization before we proceed to summary.
@@ -1105,38 +1138,41 @@ export const mixinRequestHandler = (
1105
1138
  * @param Base - base class, inherits from FluidDataStoreRuntime
1106
1139
  */
1107
1140
  export const mixinSummaryHandler = (
1108
- handler: (runtime: FluidDataStoreRuntime) => Promise<{ path: string[]; content: string; } | undefined>,
1109
- Base: typeof FluidDataStoreRuntime = FluidDataStoreRuntime,
1110
- ) => class RuntimeWithSummarizerHandler extends Base {
1111
- private addBlob(summary: ISummaryTreeWithStats, path: string[], content: string) {
1112
- const firstName = path.shift();
1113
- if (firstName === undefined) {
1114
- throw new LoggingError("Path can't be empty");
1115
- }
1116
-
1117
- let blob: ISummaryTree | ISummaryBlob = {
1118
- type: SummaryType.Blob,
1119
- content,
1120
- };
1121
- summary.stats.blobNodeCount++;
1122
- summary.stats.totalBlobSize += content.length;
1123
-
1124
- for (const name of path.reverse()) {
1125
- blob = {
1126
- type: SummaryType.Tree,
1127
- tree: { [name]: blob },
1128
- };
1129
- summary.stats.treeNodeCount++;
1130
- }
1131
- summary.summary.tree[firstName] = blob;
1132
- }
1133
-
1134
- async summarize(...args: any[]) {
1135
- const summary = await super.summarize(...args);
1136
- const content = await handler(this);
1137
- if (content !== undefined) {
1138
- this.addBlob(summary, content.path, content.content);
1139
- }
1140
- return summary;
1141
- }
1142
- } as typeof FluidDataStoreRuntime;
1141
+ handler: (
1142
+ runtime: FluidDataStoreRuntime,
1143
+ ) => Promise<{ path: string[]; content: string } | undefined>,
1144
+ Base: typeof FluidDataStoreRuntime = FluidDataStoreRuntime,
1145
+ ) =>
1146
+ class RuntimeWithSummarizerHandler extends Base {
1147
+ private addBlob(summary: ISummaryTreeWithStats, path: string[], content: string) {
1148
+ const firstName = path.shift();
1149
+ if (firstName === undefined) {
1150
+ throw new LoggingError("Path can't be empty");
1151
+ }
1152
+
1153
+ let blob: ISummaryTree | ISummaryBlob = {
1154
+ type: SummaryType.Blob,
1155
+ content,
1156
+ };
1157
+ summary.stats.blobNodeCount++;
1158
+ summary.stats.totalBlobSize += content.length;
1159
+
1160
+ for (const name of path.reverse()) {
1161
+ blob = {
1162
+ type: SummaryType.Tree,
1163
+ tree: { [name]: blob },
1164
+ };
1165
+ summary.stats.treeNodeCount++;
1166
+ }
1167
+ summary.summary.tree[firstName] = blob;
1168
+ }
1169
+
1170
+ async summarize(...args: any[]) {
1171
+ const summary = await super.summarize(...args);
1172
+ const content = await handler(this);
1173
+ if (content !== undefined) {
1174
+ this.addBlob(summary, content.path, content.content);
1175
+ }
1176
+ return summary;
1177
+ }
1178
+ } as typeof FluidDataStoreRuntime;