@fluidframework/test-utils 2.0.0-internal.3.0.2 → 2.0.0-internal.3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/.eslintrc.js +8 -10
  2. package/README.md +41 -11
  3. package/api-extractor.json +2 -2
  4. package/dist/DriverWrappers.d.ts.map +1 -1
  5. package/dist/DriverWrappers.js.map +1 -1
  6. package/dist/TestConfigs.d.ts.map +1 -1
  7. package/dist/TestConfigs.js +3 -2
  8. package/dist/TestConfigs.js.map +1 -1
  9. package/dist/TestSummaryUtils.d.ts +15 -4
  10. package/dist/TestSummaryUtils.d.ts.map +1 -1
  11. package/dist/TestSummaryUtils.js +23 -17
  12. package/dist/TestSummaryUtils.js.map +1 -1
  13. package/dist/containerUtils.d.ts +1 -2
  14. package/dist/containerUtils.d.ts.map +1 -1
  15. package/dist/containerUtils.js +4 -2
  16. package/dist/containerUtils.js.map +1 -1
  17. package/dist/index.d.ts +4 -4
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/interfaces.d.ts.map +1 -1
  22. package/dist/interfaces.js.map +1 -1
  23. package/dist/loaderContainerTracker.d.ts.map +1 -1
  24. package/dist/loaderContainerTracker.js +37 -27
  25. package/dist/loaderContainerTracker.js.map +1 -1
  26. package/dist/localCodeLoader.d.ts.map +1 -1
  27. package/dist/localCodeLoader.js.map +1 -1
  28. package/dist/localLoader.d.ts.map +1 -1
  29. package/dist/localLoader.js.map +1 -1
  30. package/dist/packageVersion.d.ts +1 -1
  31. package/dist/packageVersion.js +1 -1
  32. package/dist/packageVersion.js.map +1 -1
  33. package/dist/retry.d.ts.map +1 -1
  34. package/dist/retry.js.map +1 -1
  35. package/dist/testContainerRuntimeFactory.d.ts.map +1 -1
  36. package/dist/testContainerRuntimeFactory.js +2 -1
  37. package/dist/testContainerRuntimeFactory.js.map +1 -1
  38. package/dist/testFluidObject.d.ts.map +1 -1
  39. package/dist/testFluidObject.js +7 -3
  40. package/dist/testFluidObject.js.map +1 -1
  41. package/dist/testObjectProvider.d.ts +4 -1
  42. package/dist/testObjectProvider.d.ts.map +1 -1
  43. package/dist/testObjectProvider.js +28 -11
  44. package/dist/testObjectProvider.js.map +1 -1
  45. package/dist/timeoutUtils.d.ts.map +1 -1
  46. package/dist/timeoutUtils.js +4 -3
  47. package/dist/timeoutUtils.js.map +1 -1
  48. package/package.json +66 -55
  49. package/prettier.config.cjs +1 -1
  50. package/src/DriverWrappers.ts +40 -37
  51. package/src/TestConfigs.ts +9 -7
  52. package/src/TestSummaryUtils.ts +113 -119
  53. package/src/containerUtils.ts +20 -18
  54. package/src/index.ts +24 -25
  55. package/src/interfaces.ts +10 -7
  56. package/src/loaderContainerTracker.ts +627 -565
  57. package/src/localCodeLoader.ts +85 -77
  58. package/src/localLoader.ts +24 -24
  59. package/src/packageVersion.ts +1 -1
  60. package/src/retry.ts +31 -25
  61. package/src/testContainerRuntimeFactory.ts +59 -56
  62. package/src/testFluidObject.ts +168 -152
  63. package/src/testObjectProvider.ts +445 -384
  64. package/src/timeoutUtils.ts +174 -154
  65. package/tsconfig.json +9 -16
@@ -6,7 +6,11 @@ import { assert } from "@fluidframework/common-utils";
6
6
  import { IContainer, IDeltaQueue, IHostLoader } from "@fluidframework/container-definitions";
7
7
  import { Container } from "@fluidframework/container-loader";
8
8
  import { canBeCoalescedByService } from "@fluidframework/driver-utils";
9
- import { IDocumentMessage, ISequencedDocumentMessage, MessageType } from "@fluidframework/protocol-definitions";
9
+ import {
10
+ IDocumentMessage,
11
+ ISequencedDocumentMessage,
12
+ MessageType,
13
+ } from "@fluidframework/protocol-definitions";
10
14
  import { debug } from "./debug";
11
15
  import { IOpProcessingController } from "./testObjectProvider";
12
16
  import { timeoutAwait, timeoutPromise } from "./timeoutUtils";
@@ -15,574 +19,632 @@ const debugOp = debug.extend("ops");
15
19
  const debugWait = debug.extend("wait");
16
20
 
17
21
  interface ContainerRecord {
18
- // A short number for debug output
19
- index: number;
22
+ // A short number for debug output
23
+ index: number;
20
24
 
21
- // LoaderContainerTracker paused state
22
- paused: boolean;
25
+ // LoaderContainerTracker paused state
26
+ paused: boolean;
23
27
 
24
- // Tracking trailing no-op that may or may be acked by the server so we can discount them
25
- // See issue #5629
26
- startTrailingNoOps: number;
27
- trailingNoOps: number;
28
+ // Tracking trailing no-op that may or may be acked by the server so we can discount them
29
+ // See issue #5629
30
+ startTrailingNoOps: number;
31
+ trailingNoOps: number;
28
32
 
29
- // Track last proposal to ensure no unresolved proposal
30
- lastProposal: number;
33
+ // Track last proposal to ensure no unresolved proposal
34
+ lastProposal: number;
31
35
  }
32
36
 
33
37
  export class LoaderContainerTracker implements IOpProcessingController {
34
- private readonly containers = new Map<IContainer, ContainerRecord>();
35
- private lastProposalSeqNum: number = 0;
36
-
37
- constructor(private readonly syncSummarizerClients: boolean = false) {}
38
-
39
- /**
40
- * Add a loader to start to track any container created from them
41
- * @param loader - loader to start tracking any container created.
42
- */
43
- public add<LoaderType extends IHostLoader>(loader: LoaderType) {
44
- // TODO: Expose Loader API to able to intercept container creation (See issue #5114)
45
- const patch = <T, C extends IContainer>(fn: (...args) => Promise<C>) => {
46
- const boundFn = fn.bind(loader);
47
- return async (...args: T[]) => {
48
- const container = await boundFn(...args);
49
- this.addContainer(container);
50
- return container;
51
- };
52
- };
53
- /* eslint-disable @typescript-eslint/unbound-method */
54
- loader.resolve = patch(loader.resolve);
55
- loader.createDetachedContainer = patch(loader.createDetachedContainer);
56
- loader.rehydrateDetachedContainerFromSnapshot = patch(loader.rehydrateDetachedContainerFromSnapshot);
57
- /* eslint-enable @typescript-eslint/unbound-method */
58
- }
59
-
60
- /**
61
- * Utility function to add container to be tracked.
62
- *
63
- * @param container - container to add
64
- */
65
- private addContainer(container: IContainer) {
66
- // ignore summarizer
67
- if (!container.deltaManager.clientDetails.capabilities.interactive && !this.syncSummarizerClients) { return; }
68
-
69
- // don't add container that is already tracked
70
- if (this.containers.has(container)) { return; }
71
-
72
- const record = {
73
- index: this.containers.size,
74
- paused: false,
75
- startTrailingNoOps: 0,
76
- trailingNoOps: 0,
77
- lastProposal: 0,
78
- };
79
- this.containers.set(container, record);
80
- this.trackTrailingNoOps(container, record);
81
- this.trackLastProposal(container);
82
- this.setupTrace(container, record.index);
83
- }
84
-
85
- /**
86
- * Keep track of the trailing NoOp that was sent so we can discount them in the clientSequenceNumber tracking.
87
- * The server might coalesce them with other ops, or a single NoOp, or delay it if it don't think it is necessary.
88
- *
89
- * @param container - the container to track
90
- * @param record - the record to update the trailing op information
91
- */
92
- private trackTrailingNoOps(container: IContainer, record: ContainerRecord) {
93
- container.deltaManager.outbound.on("op", (messages) => {
94
- for (const msg of messages) {
95
- if (canBeCoalescedByService(msg)) {
96
- // Track the NoOp that was sent.
97
- if (record.trailingNoOps === 0) {
98
- // record the starting sequence number of the trailing no ops if we haven't been tracking yet.
99
- record.startTrailingNoOps = msg.clientSequenceNumber;
100
- }
101
- record.trailingNoOps++;
102
- } else {
103
- // Other ops has been sent. We would like to see those ack'ed, so no more need to track NoOps
104
- record.trailingNoOps = 0;
105
- }
106
- }
107
- });
108
-
109
- container.deltaManager.inbound.on("push", (message) => {
110
- // Received the no op back, update the record if we are tracking
111
- if (canBeCoalescedByService(message)
112
- && message.clientId === (container as Container).clientId
113
- && record.trailingNoOps !== 0
114
- && record.startTrailingNoOps <= message.clientSequenceNumber
115
- ) {
116
- // NoOp might have coalesced and skipped ahead some sequence number
117
- // update the record and skip ahead as well
118
- const oldStartTrailingNoOps = record.startTrailingNoOps;
119
- record.startTrailingNoOps = message.clientSequenceNumber + 1;
120
- record.trailingNoOps -= (record.startTrailingNoOps - oldStartTrailingNoOps);
121
- }
122
- });
123
-
124
- container.on("disconnected", () => {
125
- // reset on disconnect.
126
- record.trailingNoOps = 0;
127
- });
128
- }
129
-
130
- private trackLastProposal(container: IContainer) {
131
- container.on("codeDetailsProposed", (value, proposal) => {
132
- if (proposal.sequenceNumber > this.lastProposalSeqNum) {
133
- this.lastProposalSeqNum = proposal.sequenceNumber;
134
- }
135
- });
136
- }
137
-
138
- /**
139
- * Reset the tracker, closing all containers and stop tracking them.
140
- */
141
- public reset() {
142
- this.lastProposalSeqNum = 0;
143
- for (const container of this.containers.keys()) {
144
- container.close();
145
- }
146
- this.containers.clear();
147
-
148
- // REVIEW: do we need to unpatch the loaders?
149
- }
150
-
151
- /**
152
- * Ensure all tracked containers are synchronized
153
- */
154
- public async ensureSynchronized(...containers: IContainer[]): Promise<void> {
155
- await this.processSynchronized(undefined, ...containers);
156
- }
157
-
158
- /**
159
- * Ensure all tracked containers are synchronized with a time limit
160
- */
161
- public async ensureSynchronizedWithTimeout?(timeoutDuration: number | undefined, ...containers: IContainer[]) {
162
- await this.processSynchronized(timeoutDuration, ...containers);
163
- }
164
-
165
- /**
166
- * Make sure all the tracked containers are synchronized.
167
- *
168
- * No isDirty (non-readonly) containers
169
- *
170
- * No extra clientId in quorum of any container that is not tracked and still opened.
171
- *
172
- * - i.e. no pending Join/Leave message.
173
- *
174
- * No unresolved proposal (minSeqNum \>= lastProposalSeqNum)
175
- *
176
- * lastSequenceNumber of all container is the same
177
- *
178
- * clientSequenceNumberObserved is the same as clientSequenceNumber sent
179
- *
180
- * - this overlaps with !isDirty, but include task scheduler ops.
181
- *
182
- * - Trailing NoOp is tracked and don't count as pending ops.
183
- */
184
- private async processSynchronized(timeoutDuration: number | undefined, ...containers: IContainer[]) {
185
- const resumed = this.resumeProcessing(...containers);
186
-
187
- let waitingSequenceNumberSynchronized = false;
188
- // eslint-disable-next-line no-constant-condition
189
- while (true) {
190
- const containersToApply = this.getContainers(containers);
191
- if (containersToApply.length === 0) { break; }
192
-
193
- // Ignore readonly dirty containers, because it can't sent up and nothing can be done about it being dirty
194
- const dirtyContainers = containersToApply.filter((c) => {
195
- const { deltaManager, isDirty } = c;
196
- return deltaManager.readOnlyInfo.readonly !== true && isDirty;
197
- });
198
- if (dirtyContainers.length === 0) {
199
- // Wait for all the leave messages
200
- const pendingClients = this.getPendingClients(containersToApply);
201
- if (pendingClients.length === 0) {
202
- if (this.isSequenceNumberSynchronized(containersToApply)) {
203
- // done, we are in sync
204
- break;
205
- }
206
- if (!waitingSequenceNumberSynchronized) {
207
- // Only write it out once
208
- waitingSequenceNumberSynchronized = true;
209
- debugWait("Waiting for sequence number synchronized");
210
- await timeoutAwait(this.waitForAnyInboundOps(containersToApply), {
211
- errorMsg: "Timeout on waiting for sequence number synchronized",
212
- });
213
- }
214
- } else {
215
- waitingSequenceNumberSynchronized = false;
216
- await timeoutAwait(this.waitForPendingClients(pendingClients), {
217
- errorMsg: "Timeout on waiting for pending join or leave op",
218
- });
219
- }
220
- } else {
221
- // Wait for all the containers to be saved
222
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
223
- debugWait(`Waiting container to be saved ${dirtyContainers.map((c) => this.containers.get(c)!.index)}`);
224
- waitingSequenceNumberSynchronized = false;
225
- await Promise.all(dirtyContainers.map(async (c) => Promise.race(
226
- [timeoutPromise(
227
- (resolve) => c.once("saved", () => resolve()),
228
- {
229
- errorMsg: "Timeout on waiting a container to be saved",
230
- },
231
- ),
232
- new Promise((resolve) => c.once("closed", resolve)),
233
- ],
234
- )));
235
- }
236
-
237
- // yield a turn to allow side effect of the ops we just processed execute before we check again
238
- await new Promise<void>((resolve) => { setTimeout(resolve, 0); });
239
- }
240
-
241
- // Pause all container that was resumed
242
- // don't call pause if resumed is empty and pause everything, which is not what we want
243
- if (resumed.length !== 0) {
244
- await timeoutAwait(this.pauseProcessing(...resumed), {
245
- errorMsg: "Timeout on waiting for pausing all resumed containers",
246
- });
247
- }
248
-
249
- debugWait("Synchronized");
250
- }
251
-
252
- /**
253
- * Utility to calculate the set of clientId per container in quorum that is NOT associated with
254
- * any container we tracked, indicating there is a pending join or leave op that we need to wait.
255
- *
256
- * @param containersToApply - the set of containers to check
257
- */
258
- private getPendingClients(containersToApply: IContainer[]) {
259
- // All the clientId we track should be a superset of the quorum, otherwise, we are missing
260
- // leave messages
261
- const openedDocuments = Array.from(this.containers.keys()).filter((c) => !c.closed);
262
- const openedClientId = openedDocuments.map((container) => (container as Container).clientId);
263
-
264
- const pendingClients: [IContainer, Set<string>][] = [];
265
- containersToApply.forEach((container) => {
266
- const pendingClientId = new Set<string>();
267
- const quorum = container.getQuorum();
268
- quorum.getMembers().forEach((client, clientId) => {
269
- // ignore summarizer
270
- if (!client.client.details.capabilities.interactive && !this.syncSummarizerClients) { return; }
271
- if (!openedClientId.includes(clientId)) {
272
- pendingClientId.add(clientId);
273
- }
274
- });
275
-
276
- if (pendingClientId.size !== 0) {
277
- pendingClients.push([container, pendingClientId]);
278
- }
279
- });
280
- return pendingClients;
281
- }
282
-
283
- /**
284
- * Utility to check synchronization based on sequence number
285
- * See ensureSynchronized for more detail
286
- *
287
- * @param containersToApply - the set of containers to check
288
- */
289
- private isSequenceNumberSynchronized(containersToApply: IContainer[]) {
290
- // clientSequenceNumber check detects ops in flight, both on the wire and in the outbound queue
291
- // We need both client sequence number and isDirty check because:
292
- // - Currently isDirty flag ignores ops for task scheduler, so we need the client sequence number check
293
- // - But isDirty flags include ops during forceReadonly and disconnected, because we don't submit
294
- // the ops in the first place, clientSequenceNumber is not assigned
295
-
296
- const isClientSequenceNumberSynchronized = containersToApply.every((container) => {
297
- if (container.deltaManager.readOnlyInfo.readonly === true) {
298
- // Ignore readonly container. the clientSeqNum and clientSeqNumObserved might be out of sync
299
- // because we transition to readonly when outbound is not empty or the in transit op got lost
300
- return true;
301
- }
302
- // Note that in read only mode, the op won't be submitted
303
- let deltaManager = (container.deltaManager as any);
304
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
305
- const { trailingNoOps } = this.containers.get(container)!;
306
- // Back-compat: clientSequenceNumber & clientSequenceNumberObserved moved to ConnectionManager in 0.53
307
- if (!("clientSequenceNumber" in deltaManager)) {
308
- deltaManager = deltaManager.connectionManager;
309
- }
310
- assert("clientSequenceNumber" in deltaManager, "no clientSequenceNumber");
311
- assert("clientSequenceNumberObserved" in deltaManager, "no clientSequenceNumber");
312
- return deltaManager.clientSequenceNumber ===
313
- (deltaManager.clientSequenceNumberObserved as number) + trailingNoOps;
314
- });
315
-
316
- if (!isClientSequenceNumberSynchronized) {
317
- return false;
318
- }
319
-
320
- const minSeqNum = containersToApply[0].deltaManager.minimumSequenceNumber;
321
- if (minSeqNum < this.lastProposalSeqNum) {
322
- // There is an unresolved proposal
323
- return false;
324
- }
325
-
326
- // Check to see if all the container has process the same number of ops.
327
- const seqNum = containersToApply[0].deltaManager.lastSequenceNumber;
328
- return containersToApply.every((c) => c.deltaManager.lastSequenceNumber === seqNum);
329
- }
330
-
331
- /**
332
- * Utility to wait for any clientId in quorum that is NOT associated with any container we
333
- * tracked, indicating there is a pending join or leave op that we need to wait.
334
- *
335
- * Note that this function doesn't account for container that got added after we started waiting
336
- *
337
- * @param containersToApply - the set of containers to wait for any inbound ops for
338
- */
339
- private async waitForPendingClients(pendingClients: [IContainer, Set<string>][]) {
340
- const unconnectedClients =
341
- Array.from(this.containers.keys()).filter((c) => !c.closed && !(c as Container).connected);
342
- return Promise.all(pendingClients.map(async ([container, pendingClientId]) => {
343
- return new Promise<void>((resolve) => {
344
- const cleanup = () => {
345
- unconnectedClients.forEach((c) => c.off("connected", handler));
346
- container.getQuorum().off("removeMember", handler);
347
- };
348
- const handler = (clientId: string) => {
349
- pendingClientId.delete(clientId);
350
- if (pendingClientId.size === 0) {
351
- cleanup();
352
- resolve();
353
- }
354
- };
355
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
356
- const index = this.containers.get(container)!.index;
357
- debugWait(`${index}: Waiting for pending clients ${Array.from(pendingClientId.keys())}`);
358
- unconnectedClients.forEach((c) => c.on("connected", handler));
359
- container.getQuorum().on("removeMember", handler);
360
- container.on("closed", () => {
361
- cleanup();
362
- resolve();
363
- });
364
- });
365
- }));
366
- }
367
-
368
- /**
369
- * Utility to wait for any inbound ops from a set of containers
370
- * @param containersToApply - the set of containers to wait for any inbound ops for
371
- */
372
- private async waitForAnyInboundOps(containersToApply: IContainer[]) {
373
- return new Promise<void>((resolve) => {
374
- const handler = () => {
375
- containersToApply.map((c) => {
376
- c.deltaManager.inbound.off("push", handler);
377
- });
378
- resolve();
379
- };
380
- containersToApply.map((c) => {
381
- c.deltaManager.inbound.on("push", handler);
382
- });
383
- });
384
- }
385
-
386
- /**
387
- * Resume all queue activities on all paused tracked containers and return them
388
- */
389
- public resumeProcessing(...containers: IContainer[]) {
390
- const resumed: IContainer[] = [];
391
- const containersToApply = this.getContainers(containers);
392
- for (const container of containersToApply) {
393
- const record = this.containers.get(container);
394
- if (record?.paused === true) {
395
- debugWait(`${record.index}: container resumed`);
396
- container.deltaManager.inbound.resume();
397
- container.deltaManager.outbound.resume();
398
- resumed.push(container);
399
- record.paused = false;
400
- }
401
- }
402
- return resumed;
403
- }
404
-
405
- /**
406
- * Pause all queue activities on the containers given, or all tracked containers
407
- * Any containers given that is not tracked will be ignored.
408
- */
409
- public async pauseProcessing(...containers: IContainer[]) {
410
- const pauseP: Promise<void>[] = [];
411
- const containersToApply = this.getContainers(containers);
412
- for (const container of containersToApply) {
413
- const record = this.containers.get(container);
414
- if (record !== undefined && !record.paused) {
415
- debugWait(`${record.index}: container paused`);
416
- pauseP.push(container.deltaManager.inbound.pause());
417
- pauseP.push(container.deltaManager.outbound.pause());
418
- record.paused = true;
419
- }
420
- }
421
- await Promise.all(pauseP);
422
- }
423
-
424
- /**
425
- * Pause all queue activities on all tracked containers, and resume only
426
- * inbound to process ops until it is idle. All queues are left in the paused state
427
- * after the function
428
- */
429
- public async processIncoming(...containers: IContainer[]) {
430
- return this.processQueue(containers, (container) => container.deltaManager.inbound);
431
- }
432
-
433
- /**
434
- * Pause all queue activities on all tracked containers, and resume only
435
- * outbound to process ops until it is idle. All queues are left in the paused state
436
- * after the function
437
- */
438
- public async processOutgoing(...containers: IContainer[]) {
439
- return this.processQueue(containers, (container) => container.deltaManager.outbound);
440
- }
441
-
442
- /**
443
- * Implementation of processIncoming and processOutgoing
444
- */
445
- private async processQueue<U>(containers: IContainer[], getQueue: (container: IContainer) => IDeltaQueue<U>) {
446
- await this.pauseProcessing(...containers);
447
- const resumed: IDeltaQueue<U>[] = [];
448
-
449
- const containersToApply = this.getContainers(containers);
450
- const inflightTracker = new Map<IContainer, number>();
451
- const cleanup: (() => void)[] = [];
452
- for (const container of containersToApply) {
453
- const queue = getQueue(container);
454
-
455
- // track the outgoing ops (if any) to make sure they make the round trip to at least to the same client
456
- // to make sure they are sequenced.
457
- cleanup.push(this.setupInOutTracker(container, inflightTracker));
458
- queue.resume();
459
- resumed.push(queue);
460
- }
461
-
462
- while (resumed.some((queue) => !queue.idle)) {
463
- debugWait("Wait until queue is idle");
464
- await new Promise<void>((resolve) => { setTimeout(resolve, 0); });
465
- }
466
-
467
- // Make sure all the op that we sent out are acked first
468
- // This is no op if we are processing incoming
469
- if (inflightTracker.size) {
470
- debugWait("Wait for inflight ops");
471
- do {
472
- await this.waitForAnyInboundOps(containersToApply);
473
- } while (inflightTracker.size);
474
- }
475
-
476
- // remove the handlers
477
- cleanup.forEach((clean) => clean());
478
-
479
- await Promise.all(resumed.map(async (queue) => queue.pause()));
480
- }
481
-
482
- /**
483
- * Utility to set up listener to track the outbound ops until it round trip back
484
- * Returns a function to remove the handler after it is done.
485
- *
486
- * @param container - the container to setup
487
- * @param inflightTracker - a map to track the clientSequenceNumber per container it expect to get ops back
488
- */
489
- private setupInOutTracker(container: IContainer, inflightTracker: Map<IContainer, number>) {
490
- const outHandler = (messages: IDocumentMessage[]) => {
491
- for (const message of messages) {
492
- if (!canBeCoalescedByService(message)) {
493
- inflightTracker.set(container, message.clientSequenceNumber);
494
- }
495
- }
496
- };
497
- const inHandler = (message: ISequencedDocumentMessage) => {
498
- if (!canBeCoalescedByService(message)
499
- && message.clientId === (container as Container).clientId
500
- && inflightTracker.get(container) === message.clientSequenceNumber) {
501
- inflightTracker.delete(container);
502
- }
503
- };
504
-
505
- container.deltaManager.outbound.on("op", outHandler);
506
- container.deltaManager.inbound.on("push", inHandler);
507
-
508
- return () => {
509
- container.deltaManager.outbound.off("op", outHandler);
510
- container.deltaManager.inbound.off("push", inHandler);
511
- };
512
- }
513
-
514
- /**
515
- * Setup debug traces for connection and ops
516
- */
517
- private setupTrace(container: IContainer, index: number) {
518
- if (debugOp.enabled) {
519
- const getContentsString = (type: string, msgContents: any) => {
520
- try {
521
- if (type !== MessageType.Operation) {
522
- if (typeof msgContents === "string") { return msgContents; }
523
- return JSON.stringify(msgContents);
524
- }
525
- let address = "";
526
-
527
- // contents comes in the wire as JSON string ("push" event)
528
- // But already parsed when apply ("op" event)
529
- let contents = typeof msgContents === "string" ?
530
- JSON.parse(msgContents) : msgContents;
531
- while (contents !== undefined && contents !== null) {
532
- if (contents.contents?.address !== undefined) {
533
- address += `/${contents.contents.address}`;
534
- contents = contents.contents.contents;
535
- } else if (contents.content?.address !== undefined) {
536
- address += `/${contents.content.address}`;
537
- contents = contents.content.contents;
538
- } else {
539
- break;
540
- }
541
- }
542
- if (address) {
543
- return `${address} ${JSON.stringify(contents)}`;
544
- }
545
- return JSON.stringify(contents);
546
- } catch (e: any) {
547
- return `${e.message}: ${e.stack}`;
548
- }
549
- };
550
- debugOp(`${index}: ADD: clientId: ${(container as Container).clientId}`);
551
- container.deltaManager.outbound.on("op", (messages) => {
552
- for (const msg of messages) {
553
- debugOp(`${index}: OUT: `
554
- + `cli: ${msg.clientSequenceNumber.toString().padStart(3)} `
555
- + `rsq: ${msg.referenceSequenceNumber.toString().padStart(3)} `
556
- + `${msg.type} ${getContentsString(msg.type, msg.contents)}`);
557
- }
558
- });
559
- const getInboundHandler = (type: string) => {
560
- return (msg: ISequencedDocumentMessage) => {
561
- const clientSeq = msg.clientId === (container as Container).clientId ?
562
- `cli: ${msg.clientSequenceNumber.toString().padStart(3)}` : " ";
563
- debugOp(`${index}: ${type}: seq: ${msg.sequenceNumber.toString().padStart(3)} `
564
- + `${clientSeq} min: ${msg.minimumSequenceNumber.toString().padStart(3)} `
565
- + `${msg.type} ${getContentsString(msg.type, msg.contents)}`);
566
- };
567
- };
568
- container.deltaManager.inbound.on("push", getInboundHandler("IN "));
569
- container.deltaManager.inbound.on("op", getInboundHandler("OP "));
570
- container.deltaManager.on("connect", (details) => {
571
- debugOp(`${index}: CON: clientId: ${details.clientId}`);
572
- });
573
- container.deltaManager.on("disconnect", (reason) => {
574
- debugOp(`${index}: DIS: ${reason}`);
575
- });
576
- }
577
- }
578
-
579
- /**
580
- * Filter out the opened containers based on param.
581
- * @param containers - The container to filter to. If the array is empty, it means don't filter and return
582
- * all open containers.
583
- */
584
- private getContainers(containers: IContainer[]) {
585
- const containersToApply = containers.length === 0 ? Array.from(this.containers.keys()) : containers;
586
- return containersToApply.filter((container) => !container.closed);
587
- }
38
+ private readonly containers = new Map<IContainer, ContainerRecord>();
39
+ private lastProposalSeqNum: number = 0;
40
+
41
+ constructor(private readonly syncSummarizerClients: boolean = false) {}
42
+
43
+ /**
44
+ * Add a loader to start to track any container created from them
45
+ * @param loader - loader to start tracking any container created.
46
+ */
47
+ public add<LoaderType extends IHostLoader>(loader: LoaderType) {
48
+ // TODO: Expose Loader API to able to intercept container creation (See issue #5114)
49
+ const patch = <T, C extends IContainer>(fn: (...args) => Promise<C>) => {
50
+ const boundFn = fn.bind(loader);
51
+ return async (...args: T[]) => {
52
+ const container = await boundFn(...args);
53
+ this.addContainer(container);
54
+ return container;
55
+ };
56
+ };
57
+ /* eslint-disable @typescript-eslint/unbound-method */
58
+ loader.resolve = patch(loader.resolve);
59
+ loader.createDetachedContainer = patch(loader.createDetachedContainer);
60
+ loader.rehydrateDetachedContainerFromSnapshot = patch(
61
+ loader.rehydrateDetachedContainerFromSnapshot,
62
+ );
63
+ /* eslint-enable @typescript-eslint/unbound-method */
64
+ }
65
+
66
+ /**
67
+ * Utility function to add container to be tracked.
68
+ *
69
+ * @param container - container to add
70
+ */
71
+ private addContainer(container: IContainer) {
72
+ // ignore summarizer
73
+ if (
74
+ !container.deltaManager.clientDetails.capabilities.interactive &&
75
+ !this.syncSummarizerClients
76
+ ) {
77
+ return;
78
+ }
79
+
80
+ // don't add container that is already tracked
81
+ if (this.containers.has(container)) {
82
+ return;
83
+ }
84
+
85
+ const record = {
86
+ index: this.containers.size,
87
+ paused: false,
88
+ startTrailingNoOps: 0,
89
+ trailingNoOps: 0,
90
+ lastProposal: 0,
91
+ };
92
+ this.containers.set(container, record);
93
+ this.trackTrailingNoOps(container, record);
94
+ this.trackLastProposal(container);
95
+ this.setupTrace(container, record.index);
96
+ }
97
+
98
+ /**
99
+ * Keep track of the trailing NoOp that was sent so we can discount them in the clientSequenceNumber tracking.
100
+ * The server might coalesce them with other ops, or a single NoOp, or delay it if it don't think it is necessary.
101
+ *
102
+ * @param container - the container to track
103
+ * @param record - the record to update the trailing op information
104
+ */
105
+ private trackTrailingNoOps(container: IContainer, record: ContainerRecord) {
106
+ container.deltaManager.outbound.on("op", (messages) => {
107
+ for (const msg of messages) {
108
+ if (canBeCoalescedByService(msg)) {
109
+ // Track the NoOp that was sent.
110
+ if (record.trailingNoOps === 0) {
111
+ // record the starting sequence number of the trailing no ops if we haven't been tracking yet.
112
+ record.startTrailingNoOps = msg.clientSequenceNumber;
113
+ }
114
+ record.trailingNoOps++;
115
+ } else {
116
+ // Other ops has been sent. We would like to see those ack'ed, so no more need to track NoOps
117
+ record.trailingNoOps = 0;
118
+ }
119
+ }
120
+ });
121
+
122
+ container.deltaManager.inbound.on("push", (message) => {
123
+ // Received the no op back, update the record if we are tracking
124
+ if (
125
+ canBeCoalescedByService(message) &&
126
+ message.clientId === (container as Container).clientId &&
127
+ record.trailingNoOps !== 0 &&
128
+ record.startTrailingNoOps <= message.clientSequenceNumber
129
+ ) {
130
+ // NoOp might have coalesced and skipped ahead some sequence number
131
+ // update the record and skip ahead as well
132
+ const oldStartTrailingNoOps = record.startTrailingNoOps;
133
+ record.startTrailingNoOps = message.clientSequenceNumber + 1;
134
+ record.trailingNoOps -= record.startTrailingNoOps - oldStartTrailingNoOps;
135
+ }
136
+ });
137
+
138
+ container.on("disconnected", () => {
139
+ // reset on disconnect.
140
+ record.trailingNoOps = 0;
141
+ });
142
+ }
143
+
144
+ private trackLastProposal(container: IContainer) {
145
+ container.on("codeDetailsProposed", (value, proposal) => {
146
+ if (proposal.sequenceNumber > this.lastProposalSeqNum) {
147
+ this.lastProposalSeqNum = proposal.sequenceNumber;
148
+ }
149
+ });
150
+ }
151
+
152
+ /**
153
+ * Reset the tracker, closing all containers and stop tracking them.
154
+ */
155
+ public reset() {
156
+ this.lastProposalSeqNum = 0;
157
+ for (const container of this.containers.keys()) {
158
+ container.close();
159
+ }
160
+ this.containers.clear();
161
+
162
+ // REVIEW: do we need to unpatch the loaders?
163
+ }
164
+
165
+ /**
166
+ * Ensure all tracked containers are synchronized
167
+ */
168
+ public async ensureSynchronized(...containers: IContainer[]): Promise<void> {
169
+ await this.processSynchronized(undefined, ...containers);
170
+ }
171
+
172
+ /**
173
+ * Ensure all tracked containers are synchronized with a time limit
174
+ */
175
+ public async ensureSynchronizedWithTimeout?(
176
+ timeoutDuration: number | undefined,
177
+ ...containers: IContainer[]
178
+ ) {
179
+ await this.processSynchronized(timeoutDuration, ...containers);
180
+ }
181
+
182
+ /**
183
+ * Make sure all the tracked containers are synchronized.
184
+ *
185
+ * No isDirty (non-readonly) containers
186
+ *
187
+ * No extra clientId in quorum of any container that is not tracked and still opened.
188
+ *
189
+ * - i.e. no pending Join/Leave message.
190
+ *
191
+ * No unresolved proposal (minSeqNum \>= lastProposalSeqNum)
192
+ *
193
+ * lastSequenceNumber of all container is the same
194
+ *
195
+ * clientSequenceNumberObserved is the same as clientSequenceNumber sent
196
+ *
197
+ * - this overlaps with !isDirty, but include task scheduler ops.
198
+ *
199
+ * - Trailing NoOp is tracked and don't count as pending ops.
200
+ */
201
+ private async processSynchronized(
202
+ timeoutDuration: number | undefined,
203
+ ...containers: IContainer[]
204
+ ) {
205
+ const resumed = this.resumeProcessing(...containers);
206
+
207
+ let waitingSequenceNumberSynchronized = false;
208
+ // eslint-disable-next-line no-constant-condition
209
+ while (true) {
210
+ const containersToApply = this.getContainers(containers);
211
+ if (containersToApply.length === 0) {
212
+ break;
213
+ }
214
+
215
+ // Ignore readonly dirty containers, because it can't sent up and nothing can be done about it being dirty
216
+ const dirtyContainers = containersToApply.filter((c) => {
217
+ const { deltaManager, isDirty } = c;
218
+ return deltaManager.readOnlyInfo.readonly !== true && isDirty;
219
+ });
220
+ if (dirtyContainers.length === 0) {
221
+ // Wait for all the leave messages
222
+ const pendingClients = this.getPendingClients(containersToApply);
223
+ if (pendingClients.length === 0) {
224
+ if (this.isSequenceNumberSynchronized(containersToApply)) {
225
+ // done, we are in sync
226
+ break;
227
+ }
228
+ if (!waitingSequenceNumberSynchronized) {
229
+ // Only write it out once
230
+ waitingSequenceNumberSynchronized = true;
231
+ debugWait("Waiting for sequence number synchronized");
232
+ await timeoutAwait(this.waitForAnyInboundOps(containersToApply), {
233
+ errorMsg: "Timeout on waiting for sequence number synchronized",
234
+ });
235
+ }
236
+ } else {
237
+ waitingSequenceNumberSynchronized = false;
238
+ await timeoutAwait(this.waitForPendingClients(pendingClients), {
239
+ errorMsg: "Timeout on waiting for pending join or leave op",
240
+ });
241
+ }
242
+ } else {
243
+ // Wait for all the containers to be saved
244
+ debugWait(
245
+ `Waiting container to be saved ${dirtyContainers.map(
246
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
247
+ (c) => this.containers.get(c)!.index,
248
+ )}`,
249
+ );
250
+ waitingSequenceNumberSynchronized = false;
251
+ await Promise.all(
252
+ dirtyContainers.map(async (c) =>
253
+ Promise.race([
254
+ timeoutPromise((resolve) => c.once("saved", () => resolve()), {
255
+ errorMsg: "Timeout on waiting a container to be saved",
256
+ }),
257
+ new Promise((resolve) => c.once("closed", resolve)),
258
+ ]),
259
+ ),
260
+ );
261
+ }
262
+
263
+ // yield a turn to allow side effect of the ops we just processed execute before we check again
264
+ await new Promise<void>((resolve) => {
265
+ setTimeout(resolve, 0);
266
+ });
267
+ }
268
+
269
+ // Pause all container that was resumed
270
+ // don't call pause if resumed is empty and pause everything, which is not what we want
271
+ if (resumed.length !== 0) {
272
+ await timeoutAwait(this.pauseProcessing(...resumed), {
273
+ errorMsg: "Timeout on waiting for pausing all resumed containers",
274
+ });
275
+ }
276
+
277
+ debugWait("Synchronized");
278
+ }
279
+
280
+ /**
281
+ * Utility to calculate the set of clientId per container in quorum that is NOT associated with
282
+ * any container we tracked, indicating there is a pending join or leave op that we need to wait.
283
+ *
284
+ * @param containersToApply - the set of containers to check
285
+ */
286
+ private getPendingClients(containersToApply: IContainer[]) {
287
+ // All the clientId we track should be a superset of the quorum, otherwise, we are missing
288
+ // leave messages
289
+ const openedDocuments = Array.from(this.containers.keys()).filter((c) => !c.closed);
290
+ const openedClientId = openedDocuments.map(
291
+ (container) => (container as Container).clientId,
292
+ );
293
+
294
+ const pendingClients: [IContainer, Set<string>][] = [];
295
+ containersToApply.forEach((container) => {
296
+ const pendingClientId = new Set<string>();
297
+ const quorum = container.getQuorum();
298
+ quorum.getMembers().forEach((client, clientId) => {
299
+ // ignore summarizer
300
+ if (
301
+ !client.client.details.capabilities.interactive &&
302
+ !this.syncSummarizerClients
303
+ ) {
304
+ return;
305
+ }
306
+ if (!openedClientId.includes(clientId)) {
307
+ pendingClientId.add(clientId);
308
+ }
309
+ });
310
+
311
+ if (pendingClientId.size !== 0) {
312
+ pendingClients.push([container, pendingClientId]);
313
+ }
314
+ });
315
+ return pendingClients;
316
+ }
317
+
318
+ /**
319
+ * Utility to check synchronization based on sequence number
320
+ * See ensureSynchronized for more detail
321
+ *
322
+ * @param containersToApply - the set of containers to check
323
+ */
324
+ private isSequenceNumberSynchronized(containersToApply: IContainer[]) {
325
+ // clientSequenceNumber check detects ops in flight, both on the wire and in the outbound queue
326
+ // We need both client sequence number and isDirty check because:
327
+ // - Currently isDirty flag ignores ops for task scheduler, so we need the client sequence number check
328
+ // - But isDirty flags include ops during forceReadonly and disconnected, because we don't submit
329
+ // the ops in the first place, clientSequenceNumber is not assigned
330
+
331
+ const isClientSequenceNumberSynchronized = containersToApply.every((container) => {
332
+ if (container.deltaManager.readOnlyInfo.readonly === true) {
333
+ // Ignore readonly container. the clientSeqNum and clientSeqNumObserved might be out of sync
334
+ // because we transition to readonly when outbound is not empty or the in transit op got lost
335
+ return true;
336
+ }
337
+ // Note that in read only mode, the op won't be submitted
338
+ let deltaManager = container.deltaManager as any;
339
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
340
+ const { trailingNoOps } = this.containers.get(container)!;
341
+ // Back-compat: clientSequenceNumber & clientSequenceNumberObserved moved to ConnectionManager in 0.53
342
+ if (!("clientSequenceNumber" in deltaManager)) {
343
+ deltaManager = deltaManager.connectionManager;
344
+ }
345
+ assert("clientSequenceNumber" in deltaManager, "no clientSequenceNumber");
346
+ assert("clientSequenceNumberObserved" in deltaManager, "no clientSequenceNumber");
347
+ return (
348
+ deltaManager.clientSequenceNumber ===
349
+ (deltaManager.clientSequenceNumberObserved as number) + trailingNoOps
350
+ );
351
+ });
352
+
353
+ if (!isClientSequenceNumberSynchronized) {
354
+ return false;
355
+ }
356
+
357
+ const minSeqNum = containersToApply[0].deltaManager.minimumSequenceNumber;
358
+ if (minSeqNum < this.lastProposalSeqNum) {
359
+ // There is an unresolved proposal
360
+ return false;
361
+ }
362
+
363
+ // Check to see if all the container has process the same number of ops.
364
+ const seqNum = containersToApply[0].deltaManager.lastSequenceNumber;
365
+ return containersToApply.every((c) => c.deltaManager.lastSequenceNumber === seqNum);
366
+ }
367
+
368
+ /**
369
+ * Utility to wait for any clientId in quorum that is NOT associated with any container we
370
+ * tracked, indicating there is a pending join or leave op that we need to wait.
371
+ *
372
+ * Note that this function doesn't account for container that got added after we started waiting
373
+ *
374
+ * @param containersToApply - the set of containers to wait for any inbound ops for
375
+ */
376
+ private async waitForPendingClients(pendingClients: [IContainer, Set<string>][]) {
377
+ const unconnectedClients = Array.from(this.containers.keys()).filter(
378
+ (c) => !c.closed && !(c as Container).connected,
379
+ );
380
+ return Promise.all(
381
+ pendingClients.map(async ([container, pendingClientId]) => {
382
+ return new Promise<void>((resolve) => {
383
+ const cleanup = () => {
384
+ unconnectedClients.forEach((c) => c.off("connected", handler));
385
+ container.getQuorum().off("removeMember", handler);
386
+ };
387
+ const handler = (clientId: string) => {
388
+ pendingClientId.delete(clientId);
389
+ if (pendingClientId.size === 0) {
390
+ cleanup();
391
+ resolve();
392
+ }
393
+ };
394
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
395
+ const index = this.containers.get(container)!.index;
396
+ debugWait(
397
+ `${index}: Waiting for pending clients ${Array.from(
398
+ pendingClientId.keys(),
399
+ )}`,
400
+ );
401
+ unconnectedClients.forEach((c) => c.on("connected", handler));
402
+ container.getQuorum().on("removeMember", handler);
403
+ container.on("closed", () => {
404
+ cleanup();
405
+ resolve();
406
+ });
407
+ });
408
+ }),
409
+ );
410
+ }
411
+
412
+ /**
413
+ * Utility to wait for any inbound ops from a set of containers
414
+ * @param containersToApply - the set of containers to wait for any inbound ops for
415
+ */
416
+ private async waitForAnyInboundOps(containersToApply: IContainer[]) {
417
+ return new Promise<void>((resolve) => {
418
+ const handler = () => {
419
+ containersToApply.map((c) => {
420
+ c.deltaManager.inbound.off("push", handler);
421
+ });
422
+ resolve();
423
+ };
424
+ containersToApply.map((c) => {
425
+ c.deltaManager.inbound.on("push", handler);
426
+ });
427
+ });
428
+ }
429
+
430
+ /**
431
+ * Resume all queue activities on all paused tracked containers and return them
432
+ */
433
+ public resumeProcessing(...containers: IContainer[]) {
434
+ const resumed: IContainer[] = [];
435
+ const containersToApply = this.getContainers(containers);
436
+ for (const container of containersToApply) {
437
+ const record = this.containers.get(container);
438
+ if (record?.paused === true) {
439
+ debugWait(`${record.index}: container resumed`);
440
+ container.deltaManager.inbound.resume();
441
+ container.deltaManager.outbound.resume();
442
+ resumed.push(container);
443
+ record.paused = false;
444
+ }
445
+ }
446
+ return resumed;
447
+ }
448
+
449
+ /**
450
+ * Pause all queue activities on the containers given, or all tracked containers
451
+ * Any containers given that is not tracked will be ignored.
452
+ */
453
+ public async pauseProcessing(...containers: IContainer[]) {
454
+ const pauseP: Promise<void>[] = [];
455
+ const containersToApply = this.getContainers(containers);
456
+ for (const container of containersToApply) {
457
+ const record = this.containers.get(container);
458
+ if (record !== undefined && !record.paused) {
459
+ debugWait(`${record.index}: container paused`);
460
+ pauseP.push(container.deltaManager.inbound.pause());
461
+ pauseP.push(container.deltaManager.outbound.pause());
462
+ record.paused = true;
463
+ }
464
+ }
465
+ await Promise.all(pauseP);
466
+ }
467
+
468
+ /**
469
+ * Pause all queue activities on all tracked containers, and resume only
470
+ * inbound to process ops until it is idle. All queues are left in the paused state
471
+ * after the function
472
+ */
473
+ public async processIncoming(...containers: IContainer[]) {
474
+ return this.processQueue(containers, (container) => container.deltaManager.inbound);
475
+ }
476
+
477
+ /**
478
+ * Pause all queue activities on all tracked containers, and resume only
479
+ * outbound to process ops until it is idle. All queues are left in the paused state
480
+ * after the function
481
+ */
482
+ public async processOutgoing(...containers: IContainer[]) {
483
+ return this.processQueue(containers, (container) => container.deltaManager.outbound);
484
+ }
485
+
486
+ /**
487
+ * Implementation of processIncoming and processOutgoing
488
+ */
489
+ private async processQueue<U>(
490
+ containers: IContainer[],
491
+ getQueue: (container: IContainer) => IDeltaQueue<U>,
492
+ ) {
493
+ await this.pauseProcessing(...containers);
494
+ const resumed: IDeltaQueue<U>[] = [];
495
+
496
+ const containersToApply = this.getContainers(containers);
497
+ const inflightTracker = new Map<IContainer, number>();
498
+ const cleanup: (() => void)[] = [];
499
+ for (const container of containersToApply) {
500
+ const queue = getQueue(container);
501
+
502
+ // track the outgoing ops (if any) to make sure they make the round trip to at least to the same client
503
+ // to make sure they are sequenced.
504
+ cleanup.push(this.setupInOutTracker(container, inflightTracker));
505
+ queue.resume();
506
+ resumed.push(queue);
507
+ }
508
+
509
+ while (resumed.some((queue) => !queue.idle)) {
510
+ debugWait("Wait until queue is idle");
511
+ await new Promise<void>((resolve) => {
512
+ setTimeout(resolve, 0);
513
+ });
514
+ }
515
+
516
+ // Make sure all the op that we sent out are acked first
517
+ // This is no op if we are processing incoming
518
+ if (inflightTracker.size) {
519
+ debugWait("Wait for inflight ops");
520
+ do {
521
+ await this.waitForAnyInboundOps(containersToApply);
522
+ } while (inflightTracker.size);
523
+ }
524
+
525
+ // remove the handlers
526
+ cleanup.forEach((clean) => clean());
527
+
528
+ await Promise.all(resumed.map(async (queue) => queue.pause()));
529
+ }
530
+
531
+ /**
532
+ * Utility to set up listener to track the outbound ops until it round trip back
533
+ * Returns a function to remove the handler after it is done.
534
+ *
535
+ * @param container - the container to setup
536
+ * @param inflightTracker - a map to track the clientSequenceNumber per container it expect to get ops back
537
+ */
538
+ private setupInOutTracker(container: IContainer, inflightTracker: Map<IContainer, number>) {
539
+ const outHandler = (messages: IDocumentMessage[]) => {
540
+ for (const message of messages) {
541
+ if (!canBeCoalescedByService(message)) {
542
+ inflightTracker.set(container, message.clientSequenceNumber);
543
+ }
544
+ }
545
+ };
546
+ const inHandler = (message: ISequencedDocumentMessage) => {
547
+ if (
548
+ !canBeCoalescedByService(message) &&
549
+ message.clientId === (container as Container).clientId &&
550
+ inflightTracker.get(container) === message.clientSequenceNumber
551
+ ) {
552
+ inflightTracker.delete(container);
553
+ }
554
+ };
555
+
556
+ container.deltaManager.outbound.on("op", outHandler);
557
+ container.deltaManager.inbound.on("push", inHandler);
558
+
559
+ return () => {
560
+ container.deltaManager.outbound.off("op", outHandler);
561
+ container.deltaManager.inbound.off("push", inHandler);
562
+ };
563
+ }
564
+
565
+ /**
566
+ * Setup debug traces for connection and ops
567
+ */
568
+ private setupTrace(container: IContainer, index: number) {
569
+ if (debugOp.enabled) {
570
+ const getContentsString = (type: string, msgContents: any) => {
571
+ try {
572
+ if (type !== MessageType.Operation) {
573
+ if (typeof msgContents === "string") {
574
+ return msgContents;
575
+ }
576
+ return JSON.stringify(msgContents);
577
+ }
578
+ let address = "";
579
+
580
+ // contents comes in the wire as JSON string ("push" event)
581
+ // But already parsed when apply ("op" event)
582
+ let contents =
583
+ typeof msgContents === "string" ? JSON.parse(msgContents) : msgContents;
584
+ while (contents !== undefined && contents !== null) {
585
+ if (contents.contents?.address !== undefined) {
586
+ address += `/${contents.contents.address}`;
587
+ contents = contents.contents.contents;
588
+ } else if (contents.content?.address !== undefined) {
589
+ address += `/${contents.content.address}`;
590
+ contents = contents.content.contents;
591
+ } else {
592
+ break;
593
+ }
594
+ }
595
+ if (address) {
596
+ return `${address} ${JSON.stringify(contents)}`;
597
+ }
598
+ return JSON.stringify(contents);
599
+ } catch (e: any) {
600
+ return `${e.message}: ${e.stack}`;
601
+ }
602
+ };
603
+ debugOp(`${index}: ADD: clientId: ${(container as Container).clientId}`);
604
+ container.deltaManager.outbound.on("op", (messages) => {
605
+ for (const msg of messages) {
606
+ debugOp(
607
+ `${index}: OUT: ` +
608
+ `cli: ${msg.clientSequenceNumber.toString().padStart(3)} ` +
609
+ `rsq: ${msg.referenceSequenceNumber.toString().padStart(3)} ` +
610
+ `${msg.type} ${getContentsString(msg.type, msg.contents)}`,
611
+ );
612
+ }
613
+ });
614
+ const getInboundHandler = (type: string) => {
615
+ return (msg: ISequencedDocumentMessage) => {
616
+ const clientSeq =
617
+ msg.clientId === (container as Container).clientId
618
+ ? `cli: ${msg.clientSequenceNumber.toString().padStart(3)}`
619
+ : " ";
620
+ debugOp(
621
+ `${index}: ${type}: seq: ${msg.sequenceNumber.toString().padStart(3)} ` +
622
+ `${clientSeq} min: ${msg.minimumSequenceNumber
623
+ .toString()
624
+ .padStart(3)} ` +
625
+ `${msg.type} ${getContentsString(msg.type, msg.contents)}`,
626
+ );
627
+ };
628
+ };
629
+ container.deltaManager.inbound.on("push", getInboundHandler("IN "));
630
+ container.deltaManager.inbound.on("op", getInboundHandler("OP "));
631
+ container.deltaManager.on("connect", (details) => {
632
+ debugOp(`${index}: CON: clientId: ${details.clientId}`);
633
+ });
634
+ container.deltaManager.on("disconnect", (reason) => {
635
+ debugOp(`${index}: DIS: ${reason}`);
636
+ });
637
+ }
638
+ }
639
+
640
+ /**
641
+ * Filter out the opened containers based on param.
642
+ * @param containers - The container to filter to. If the array is empty, it means don't filter and return
643
+ * all open containers.
644
+ */
645
+ private getContainers(containers: IContainer[]) {
646
+ const containersToApply =
647
+ containers.length === 0 ? Array.from(this.containers.keys()) : containers;
648
+ return containersToApply.filter((container) => !container.closed);
649
+ }
588
650
  }