@fluidframework/task-manager 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.
@@ -8,14 +8,18 @@ import { EventEmitter } from "events";
8
8
  import { assert } from "@fluidframework/common-utils";
9
9
  import { ISequencedDocumentMessage, MessageType } from "@fluidframework/protocol-definitions";
10
10
  import {
11
- IChannelAttributes,
12
- IFluidDataStoreRuntime,
13
- IChannelStorageService,
14
- IChannelFactory,
11
+ IChannelAttributes,
12
+ IFluidDataStoreRuntime,
13
+ IChannelStorageService,
14
+ IChannelFactory,
15
15
  } from "@fluidframework/datastore-definitions";
16
16
  import { ISummaryTreeWithStats } from "@fluidframework/runtime-definitions";
17
17
  import { readAndParse } from "@fluidframework/driver-utils";
18
- import { createSingleBlobSummary, IFluidSerializer, SharedObject } from "@fluidframework/shared-object-base";
18
+ import {
19
+ createSingleBlobSummary,
20
+ IFluidSerializer,
21
+ SharedObject,
22
+ } from "@fluidframework/shared-object-base";
19
23
  import { ReadOnlyInfo } from "@fluidframework/container-definitions";
20
24
  import { TaskManagerFactory } from "./taskManagerFactory";
21
25
  import { ITaskManager, ITaskManagerEvents } from "./interfaces";
@@ -24,28 +28,28 @@ import { ITaskManager, ITaskManagerEvents } from "./interfaces";
24
28
  * Description of a task manager operation
25
29
  */
26
30
  type ITaskManagerOperation =
27
- ITaskManagerVolunteerOperation |
28
- ITaskManagerAbandonOperation |
29
- ITaskManagerCompletedOperation;
31
+ | ITaskManagerVolunteerOperation
32
+ | ITaskManagerAbandonOperation
33
+ | ITaskManagerCompletedOperation;
30
34
 
31
35
  interface ITaskManagerVolunteerOperation {
32
- type: "volunteer";
33
- taskId: string;
36
+ type: "volunteer";
37
+ taskId: string;
34
38
  }
35
39
 
36
40
  interface ITaskManagerAbandonOperation {
37
- type: "abandon";
38
- taskId: string;
41
+ type: "abandon";
42
+ taskId: string;
39
43
  }
40
44
 
41
45
  interface ITaskManagerCompletedOperation {
42
- type: "complete";
43
- taskId: string;
46
+ type: "complete";
47
+ taskId: string;
44
48
  }
45
49
 
46
50
  interface IPendingOp {
47
- type: "volunteer" | "abandon" | "complete";
48
- messageId: number;
51
+ type: "volunteer" | "abandon" | "complete";
52
+ messageId: number;
49
53
  }
50
54
 
51
55
  const snapshotFileName = "header";
@@ -55,14 +59,11 @@ const snapshotFileName = "header";
55
59
  */
56
60
  const placeholderClientId = "placeholder";
57
61
 
58
-
59
62
  /**
60
63
  * The TaskManager distributed data structure tracks queues of clients that want to exclusively run a task.
61
64
  *
62
65
  * @remarks
63
66
  *
64
- * For an in-depth overview, see [TaskManager](https://fluidframework.com/docs/data-structures/task-manager/).
65
- *
66
67
  * ### Creation
67
68
  *
68
69
  * To create a `TaskManager`, call the static create method:
@@ -147,686 +148,719 @@ const placeholderClientId = "placeholder";
147
148
  * when using the `subscribeToTask()` method.
148
149
  */
149
150
  export class TaskManager extends SharedObject<ITaskManagerEvents> implements ITaskManager {
150
- /**
151
- * Create a new TaskManager
152
- *
153
- * @param runtime - data store runtime the new task queue belongs to
154
- * @param id - optional name of the task queue
155
- * @returns newly create task queue (but not attached yet)
156
- */
157
- public static create(runtime: IFluidDataStoreRuntime, id?: string) {
158
- return runtime.createChannel(id, TaskManagerFactory.Type) as TaskManager;
159
- }
160
-
161
- /**
162
- * Get a factory for TaskManager to register with the data store.
163
- *
164
- * @returns a factory that creates and load TaskManager
165
- */
166
- public static getFactory(): IChannelFactory {
167
- return new TaskManagerFactory();
168
- }
169
-
170
- /**
171
- * Mapping of taskId to a queue of clientIds that are waiting on the task. Maintains the consensus state of the
172
- * queue, even if we know we've submitted an op that should eventually modify the queue.
173
- */
174
- private readonly taskQueues: Map<string, string[]> = new Map();
175
-
176
- // opWatcher emits for every op on this data store. This is just a repackaging of processCore into events.
177
- private readonly opWatcher: EventEmitter = new EventEmitter();
178
- // queueWatcher emits an event whenever the consensus state of the task queues changes
179
- private readonly queueWatcher: EventEmitter = new EventEmitter();
180
- // abandonWatcher emits an event whenever the local client calls abandon() on a task.
181
- private readonly abandonWatcher: EventEmitter = new EventEmitter();
182
- // connectionWatcher emits an event whenever we get connected or disconnected.
183
- private readonly connectionWatcher: EventEmitter = new EventEmitter();
184
- // completedWatcher emits an event whenever the local client receives a completed op.
185
- private readonly completedWatcher: EventEmitter = new EventEmitter();
186
-
187
- private messageId: number = -1;
188
- /**
189
- * Tracks the most recent pending op for a given task
190
- */
191
- private readonly latestPendingOps: Map<string, IPendingOp> = new Map();
192
-
193
- /**
194
- * Tracks tasks that are this client is currently subscribed to.
195
- */
196
- private readonly subscribedTasks: Set<string> = new Set();
197
-
198
- /**
199
- * Map to track tasks that have pending complete ops.
200
- */
201
- private readonly pendingCompletedTasks: Map<string, number[]> = new Map();
202
-
203
- /**
204
- * Returns the clientId. Will return a placeholder if the runtime is detached and not yet assigned a clientId.
205
- */
206
- private get clientId(): string | undefined {
207
- return this.isAttached() ? this.runtime.clientId : placeholderClientId;
208
- }
209
-
210
- /**
211
- * Returns a ReadOnlyInfo object to determine current read/write permissions.
212
- */
213
- private get readOnlyInfo(): ReadOnlyInfo {
214
- return this.runtime.deltaManager.readOnlyInfo;
215
- }
216
-
217
- /**
218
- * Constructs a new task manager. If the object is non-local an id and service interfaces will
219
- * be provided
220
- *
221
- * @param runtime - data store runtime the task queue belongs to
222
- * @param id - optional name of the task queue
223
- */
224
- constructor(id: string, runtime: IFluidDataStoreRuntime, attributes: IChannelAttributes) {
225
- super(id, runtime, attributes, "fluid_taskManager_");
226
-
227
- this.opWatcher.on("volunteer", (taskId: string, clientId: string, local: boolean, messageId: number) => {
228
- // We're tracking local ops from this connection. Filter out local ops during "connecting"
229
- // state since these were sent on the prior connection and were already cleared from the latestPendingOps.
230
- if (runtime.connected && local) {
231
- const pendingOp = this.latestPendingOps.get(taskId);
232
- assert(pendingOp !== undefined, 0x07b /* "Unexpected op" */);
233
- // Need to check the id, since it's possible to volunteer and abandon multiple times before the acks
234
- if (messageId === pendingOp.messageId) {
235
- assert(pendingOp.type === "volunteer", 0x07c /* "Unexpected op type" */);
236
- // Delete the pending, because we no longer have an outstanding op
237
- this.latestPendingOps.delete(taskId);
238
- }
239
- }
240
-
241
- this.addClientToQueue(taskId, clientId);
242
- });
243
-
244
- this.opWatcher.on("abandon", (taskId: string, clientId: string, local: boolean, messageId: number) => {
245
- if (runtime.connected && local) {
246
- const pendingOp = this.latestPendingOps.get(taskId);
247
- assert(pendingOp !== undefined, 0x07d /* "Unexpected op" */);
248
- // Need to check the id, since it's possible to abandon and volunteer multiple times before the acks
249
- if (messageId === pendingOp.messageId) {
250
- assert(pendingOp.type === "abandon", 0x07e /* "Unexpected op type" */);
251
- // Delete the pending, because we no longer have an outstanding op
252
- this.latestPendingOps.delete(taskId);
253
- }
254
- }
255
-
256
- this.removeClientFromQueue(taskId, clientId);
257
- });
258
-
259
- this.opWatcher.on("complete", (taskId: string, clientId: string, local: boolean, messageId: number) => {
260
- if (runtime.connected && local) {
261
- const pendingOp = this.latestPendingOps.get(taskId);
262
- assert(pendingOp !== undefined, 0x400 /* Unexpected op */);
263
- // Need to check the id, since it's possible to complete multiple times before the acks
264
- if (messageId === pendingOp.messageId) {
265
- assert(pendingOp.type === "complete", 0x401 /* Unexpected op type */);
266
- // Delete the pending, because we no longer have an outstanding op
267
- this.latestPendingOps.delete(taskId);
268
- }
269
-
270
- // Remove complete op from this.pendingCompletedTasks
271
- const pendingIds = this.pendingCompletedTasks.get(taskId);
272
- assert(pendingIds !== undefined && pendingIds.length > 0, 0x402 /* pendingIds is empty */);
273
- const removed = pendingIds.shift();
274
- assert(removed === messageId, 0x403 /* Removed complete op id does not match */);
275
- }
276
-
277
- // For clients in queue, we need to remove them from the queue and raise the proper events.
278
- if (!local) {
279
- this.taskQueues.delete(taskId);
280
- this.completedWatcher.emit("completed", taskId);
281
- this.emit("completed", taskId);
282
- }
283
- });
284
-
285
- runtime.getQuorum().on("removeMember", (clientId: string) => {
286
- this.removeClientFromAllQueues(clientId);
287
- });
288
-
289
- this.queueWatcher.on("queueChange", (taskId: string, oldLockHolder: string, newLockHolder: string) => {
290
- // If oldLockHolder is placeholderClientId we need to emit the task was lost during the attach process
291
- if (oldLockHolder === placeholderClientId) {
292
- this.emit("lost", taskId);
293
- return;
294
- }
295
-
296
- // Exit early if we are still catching up on reconnect -- we can't be the leader yet anyway.
297
- if (this.clientId === undefined) {
298
- return;
299
- }
300
-
301
- if (oldLockHolder !== this.clientId && newLockHolder === this.clientId) {
302
- this.emit("assigned", taskId);
303
- } else if (oldLockHolder === this.clientId && newLockHolder !== this.clientId) {
304
- this.emit("lost", taskId);
305
- }
306
- });
307
-
308
- this.connectionWatcher.on("disconnect", () => {
309
- assert(this.clientId !== undefined, 0x1d3 /* "Missing client id on disconnect" */);
310
-
311
- // We don't modify the taskQueues on disconnect (they still reflect the latest known consensus state).
312
- // After reconnect these will get cleaned up by observing the clientLeaves.
313
- // However we do need to recognize that we lost the lock if we had it. Calls to .queued() and
314
- // .assigned() are also connection-state-aware to be consistent.
315
- for (const [taskId, clientQueue] of this.taskQueues.entries()) {
316
- if (this.isAttached() && clientQueue[0] === this.clientId) {
317
- this.emit("lost", taskId);
318
- }
319
- }
320
-
321
- // All of our outstanding ops will be for the old clientId even if they get ack'd
322
- this.latestPendingOps.clear();
323
- });
324
- }
325
-
326
- private submitVolunteerOp(taskId: string) {
327
- const op: ITaskManagerVolunteerOperation = {
328
- type: "volunteer",
329
- taskId,
330
- };
331
- const pendingOp: IPendingOp = {
332
- type: "volunteer",
333
- messageId: ++this.messageId,
334
- };
335
- this.submitLocalMessage(op, pendingOp.messageId);
336
- this.latestPendingOps.set(taskId, pendingOp);
337
- }
338
-
339
- private submitAbandonOp(taskId: string) {
340
- const op: ITaskManagerAbandonOperation = {
341
- type: "abandon",
342
- taskId,
343
- };
344
- const pendingOp: IPendingOp = {
345
- type: "abandon",
346
- messageId: ++this.messageId,
347
- };
348
- this.submitLocalMessage(op, pendingOp.messageId);
349
- this.latestPendingOps.set(taskId, pendingOp);
350
- }
351
-
352
- private submitCompleteOp(taskId: string) {
353
- const op: ITaskManagerCompletedOperation = {
354
- type: "complete",
355
- taskId,
356
- };
357
- const pendingOp: IPendingOp = {
358
- type: "complete",
359
- messageId: ++this.messageId,
360
- };
361
-
362
- if (this.pendingCompletedTasks.has(taskId)) {
363
- this.pendingCompletedTasks.get(taskId)?.push(pendingOp.messageId);
364
- } else {
365
- this.pendingCompletedTasks.set(taskId, [pendingOp.messageId]);
366
- }
367
-
368
- this.submitLocalMessage(op, pendingOp.messageId);
369
- this.latestPendingOps.set(taskId, pendingOp);
370
- }
371
-
372
- /**
373
- * {@inheritDoc ITaskManager.volunteerForTask}
374
- */
375
- public async volunteerForTask(taskId: string) {
376
- // If we have the lock, resolve immediately
377
- if (this.assigned(taskId)) {
378
- return true;
379
- }
380
-
381
- if (this.readOnlyInfo.readonly === true) {
382
- const error = this.readOnlyInfo.permissions === true ?
383
- new Error("Attempted to volunteer with read-only permissions") :
384
- new Error("Attempted to volunteer in read-only state");
385
- throw error;
386
- }
387
-
388
- if (!this.isAttached()) {
389
- // Simulate auto-ack in detached scenario
390
- assert(this.clientId !== undefined, 0x472 /* clientId should not be undefined */);
391
- this.addClientToQueue(taskId, this.clientId);
392
- return true;
393
- }
394
-
395
- if (!this.connected) {
396
- throw new Error("Attempted to volunteer in disconnected state");
397
- }
398
-
399
- // This promise works even if we already have an outstanding volunteer op.
400
- const lockAcquireP = new Promise<boolean>((resolve, reject) => {
401
- const checkIfAcquiredLock = (eventTaskId: string) => {
402
- if (eventTaskId !== taskId) {
403
- return;
404
- }
405
-
406
- // Also check pending ops here because it's possible we are currently in the queue from a previous
407
- // lock attempt, but have an outstanding abandon AND the outstanding volunteer for this lock attempt.
408
- // If we reach the head of the queue based on the previous lock attempt, we don't want to resolve.
409
- if (this.assigned(taskId) && !this.latestPendingOps.has(taskId)) {
410
- this.queueWatcher.off("queueChange", checkIfAcquiredLock);
411
- this.abandonWatcher.off("abandon", checkIfAbandoned);
412
- this.connectionWatcher.off("disconnect", rejectOnDisconnect);
413
- this.completedWatcher.off("completed", checkIfCompleted);
414
- resolve(true);
415
- }
416
- };
417
-
418
- const checkIfAbandoned = (eventTaskId: string) => {
419
- if (eventTaskId !== taskId) {
420
- return;
421
- }
422
-
423
- this.queueWatcher.off("queueChange", checkIfAcquiredLock);
424
- this.abandonWatcher.off("abandon", checkIfAbandoned);
425
- this.connectionWatcher.off("disconnect", rejectOnDisconnect);
426
- this.completedWatcher.off("completed", checkIfCompleted);
427
- reject(new Error("Abandoned before acquiring task assignment"));
428
- };
429
-
430
- const rejectOnDisconnect = () => {
431
- this.queueWatcher.off("queueChange", checkIfAcquiredLock);
432
- this.abandonWatcher.off("abandon", checkIfAbandoned);
433
- this.connectionWatcher.off("disconnect", rejectOnDisconnect);
434
- this.completedWatcher.off("completed", checkIfCompleted);
435
- reject(new Error("Disconnected before acquiring task assignment"));
436
- };
437
-
438
- const checkIfCompleted = (eventTaskId: string) => {
439
- if (eventTaskId !== taskId) {
440
- return;
441
- }
442
-
443
- this.queueWatcher.off("queueChange", checkIfAcquiredLock);
444
- this.abandonWatcher.off("abandon", checkIfAbandoned);
445
- this.connectionWatcher.off("disconnect", rejectOnDisconnect);
446
- this.completedWatcher.off("completed", checkIfCompleted);
447
- resolve(false);
448
- };
449
-
450
- this.queueWatcher.on("queueChange", checkIfAcquiredLock);
451
- this.abandonWatcher.on("abandon", checkIfAbandoned);
452
- this.connectionWatcher.on("disconnect", rejectOnDisconnect);
453
- this.completedWatcher.on("completed", checkIfCompleted);
454
- });
455
-
456
- if (!this.queued(taskId)) {
457
- this.submitVolunteerOp(taskId);
458
- }
459
- return lockAcquireP;
460
- }
461
-
462
- /**
463
- * {@inheritDoc ITaskManager.subscribeToTask}
464
- */
465
- public subscribeToTask(taskId: string) {
466
- if (this.subscribed(taskId)) {
467
- return;
468
- }
469
-
470
- if (this.readOnlyInfo.readonly === true && this.readOnlyInfo.permissions === true) {
471
- throw new Error("Attempted to subscribe with read-only permissions");
472
- }
473
-
474
- const submitVolunteerOp = () => {
475
- this.submitVolunteerOp(taskId);
476
- };
477
-
478
- const disconnectHandler = () => {
479
- // Wait to be connected again and then re-submit volunteer op
480
- this.connectionWatcher.once("connect", submitVolunteerOp);
481
- };
482
-
483
- const checkIfAbandoned = (eventTaskId: string) => {
484
- if (eventTaskId !== taskId) {
485
- return;
486
- }
487
-
488
- this.abandonWatcher.off("abandon", checkIfAbandoned);
489
- this.connectionWatcher.off("disconnect", disconnectHandler);
490
- this.connectionWatcher.off("connect", submitVolunteerOp);
491
- this.completedWatcher.off("completed", checkIfCompleted);
492
-
493
- this.subscribedTasks.delete(taskId);
494
- };
495
-
496
- const checkIfCompleted = (eventTaskId: string) => {
497
- if (eventTaskId !== taskId) {
498
- return;
499
- }
500
-
501
- this.abandonWatcher.off("abandon", checkIfAbandoned);
502
- this.connectionWatcher.off("disconnect", disconnectHandler);
503
- this.connectionWatcher.off("connect", submitVolunteerOp);
504
- this.completedWatcher.off("completed", checkIfCompleted);
505
-
506
- this.subscribedTasks.delete(taskId);
507
- };
508
-
509
- this.abandonWatcher.on("abandon", checkIfAbandoned);
510
- this.connectionWatcher.on("disconnect", disconnectHandler);
511
- this.completedWatcher.on("completed", checkIfCompleted);
512
-
513
- if (!this.isAttached()) {
514
- // Simulate auto-ack in detached scenario
515
- assert(this.clientId !== undefined, 0x473 /* clientId should not be undefined */);
516
- this.addClientToQueue(taskId, this.clientId);
517
- // Because we volunteered with placeholderClientId, we need to wait for when we attach and are assigned
518
- // a real clientId. At that point we should re-enter the queue with a real volunteer op (assuming we are
519
- // connected).
520
- this.runtime.once("attached", () => {
521
- if (this.queued(taskId)) {
522
- // If we are already queued, then we were able to replace the placeholderClientId with our real
523
- // clientId and no action is required.
524
- return;
525
- } else if (this.connected) {
526
- submitVolunteerOp();
527
- } else {
528
- this.connectionWatcher.once("connect", () => {
529
- submitVolunteerOp();
530
- });
531
- }
532
- });
533
- } else if (!this.connected) {
534
- // If we are disconnected (and attached), wait to be connected and submit volunteer op
535
- disconnectHandler();
536
- } else if (!this.assigned(taskId) && !this.queued(taskId)) {
537
- submitVolunteerOp();
538
- }
539
- this.subscribedTasks.add(taskId);
540
- }
541
-
542
- /**
543
- * {@inheritDoc ITaskManager.abandon}
544
- */
545
- public abandon(taskId: string) {
546
- // Always allow abandon if the client is subscribed to allow clients to unsubscribe while disconnected.
547
- // Otherwise, we should check to make sure the client is both connected queued for the task before sending an
548
- // abandon op.
549
- if (!this.subscribed(taskId) && !this.queued(taskId)) {
550
- // Nothing to do
551
- return;
552
- }
553
-
554
- if (!this.isAttached()) {
555
- // Simulate auto-ack in detached scenario
556
- assert(this.clientId !== undefined, 0x474 /* clientId is undefined */);
557
- this.removeClientFromQueue(taskId, this.clientId);
558
- this.abandonWatcher.emit("abandon", taskId);
559
- return;
560
- }
561
-
562
- // If we're subscribed but not queued, we don't need to submit an abandon op (probably offline)
563
- if (this.queued(taskId)) {
564
- this.submitAbandonOp(taskId);
565
- }
566
- this.abandonWatcher.emit("abandon", taskId);
567
- }
568
-
569
- /**
570
- * {@inheritDoc ITaskManager.assigned}
571
- */
572
- public assigned(taskId: string) {
573
- if (this.isAttached() && !this.connected) {
574
- return false;
575
- }
576
-
577
- const currentAssignee = this.taskQueues.get(taskId)?.[0];
578
- return currentAssignee !== undefined
579
- && currentAssignee === this.clientId
580
- && !this.latestPendingOps.has(taskId);
581
- }
582
-
583
- /**
584
- * {@inheritDoc ITaskManager.queued}
585
- */
586
- public queued(taskId: string) {
587
- if (this.isAttached() && !this.connected) {
588
- return false;
589
- }
590
-
591
- assert(this.clientId !== undefined, 0x07f /* "clientId undefined" */);
592
-
593
- const clientQueue = this.taskQueues.get(taskId);
594
- // If we have no queue for the taskId, then no one has signed up for it.
595
- return (
596
- (clientQueue?.includes(this.clientId) ?? false)
597
- && !this.latestPendingOps.has(taskId)
598
- )
599
- || this.latestPendingOps.get(taskId)?.type === "volunteer";
600
- }
601
-
602
- /**
603
- * {@inheritDoc ITaskManager.subscribed}
604
- */
605
- public subscribed(taskId: string): boolean {
606
- return this.subscribedTasks.has(taskId);
607
- }
608
-
609
- /**
610
- * {@inheritDoc ITaskManager.complete}
611
- */
612
- public complete(taskId: string): void {
613
- if (!this.assigned(taskId)) {
614
- throw new Error("Attempted to mark task as complete while not being assigned");
615
- }
616
-
617
- // If we are detached we will simulate auto-ack for the complete op. Therefore we only need to send the op if
618
- // we are attached. Additionally, we don't need to check if we are connected while detached.
619
- if (this.isAttached()) {
620
- if (!this.connected) {
621
- throw new Error("Attempted to complete task in disconnected state");
622
- }
623
- this.submitCompleteOp(taskId);
624
- }
625
-
626
- this.taskQueues.delete(taskId);
627
- this.completedWatcher.emit("completed", taskId);
628
- this.emit("completed", taskId);
629
- }
630
-
631
- /**
632
- * {@inheritDoc ITaskManager.canVolunteer}
633
- */
634
- public canVolunteer(): boolean {
635
- // A client can volunteer for a task if it's both connected to the delta stream and in write mode.
636
- // this.connected reflects that condition, but is unintuitive and may be changed in the future. This API allows
637
- // us to make changes to this.connected without affecting our guidance on how to check if a client is eligible
638
- // to volunteer for a task.
639
- return this.connected;
640
- }
641
-
642
- /**
643
- * Create a summary for the task manager
644
- *
645
- * @returns the summary of the current state of the task manager
646
- * @internal
647
- */
648
- protected summarizeCore(serializer: IFluidSerializer): ISummaryTreeWithStats {
649
- if (this.runtime.clientId !== undefined) {
650
- // If the runtime has been assigned an actual clientId by now, we can replace the placeholder clientIds
651
- // and maintain the task assignment.
652
- this.replacePlaceholderInAllQueues();
653
- } else {
654
- // If the runtime has still not been assigned a clientId, we should not summarize with the placeholder
655
- // clientIds and instead remove them from the queues and require the client to re-volunteer when assigned
656
- // a new clientId.
657
- this.removeClientFromAllQueues(placeholderClientId);
658
- }
659
-
660
- // Only include tasks if there are clients in the queue.
661
- const filteredMap = new Map<string, string[]>();
662
- this.taskQueues.forEach((queue: string[], taskId: string) => {
663
- if (queue.length > 0) {
664
- filteredMap.set(taskId, queue);
665
- }
666
- });
667
- const content = [...filteredMap.entries()];
668
- return createSingleBlobSummary(snapshotFileName, JSON.stringify(content));
669
- }
670
-
671
- /**
672
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.loadCore}
673
- * @internal
674
- */
675
- protected async loadCore(storage: IChannelStorageService): Promise<void> {
676
- const content = await readAndParse<[string, string[]][]>(storage, snapshotFileName);
677
- content.forEach(([taskId, clientIdQueue]) => {
678
- this.taskQueues.set(taskId, clientIdQueue);
679
- });
680
- this.scrubClientsNotInQuorum();
681
- }
682
-
683
- /**
684
- * @internal
685
- */
686
- protected initializeLocalCore() { }
687
-
688
- /**
689
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.onDisconnect}
690
- * @internal
691
- */
692
- protected onDisconnect() {
693
- this.connectionWatcher.emit("disconnect");
694
- }
695
-
696
- /**
697
- * {@inheritDoc @fluidframework/shared-object-base#SharedObject.onConnect}
698
- * @internal
699
- */
700
- protected onConnect() {
701
- this.connectionWatcher.emit("connect");
702
- }
703
-
704
- //
705
- /**
706
- * Override resubmit core to avoid resubmission on reconnect. On disconnect we accept our removal from the
707
- * queues, and leave it up to the user to decide whether they want to attempt to re-enter a queue on reconnect.
708
- * @internal
709
- */
710
- protected reSubmitCore() { }
711
-
712
- /**
713
- * Process a task manager operation
714
- *
715
- * @param message - the message to prepare
716
- * @param local - whether the message was sent by the local client
717
- * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
718
- * For messages from a remote client, this will be undefined.
719
- * @internal
720
- */
721
- protected processCore(message: ISequencedDocumentMessage, local: boolean, localOpMetadata: unknown) {
722
- if (message.type === MessageType.Operation) {
723
- const op = message.contents as ITaskManagerOperation;
724
- const messageId = localOpMetadata as number;
725
-
726
- switch (op.type) {
727
- case "volunteer":
728
- this.opWatcher.emit("volunteer", op.taskId, message.clientId, local, messageId);
729
- break;
730
-
731
- case "abandon":
732
- this.opWatcher.emit("abandon", op.taskId, message.clientId, local, messageId);
733
- break;
734
-
735
- case "complete":
736
- this.opWatcher.emit("complete", op.taskId, message.clientId, local, messageId);
737
- break;
738
-
739
- default:
740
- throw new Error("Unknown operation");
741
- }
742
- }
743
- }
744
-
745
- private addClientToQueue(taskId: string, clientId: string) {
746
- const pendingIds = this.pendingCompletedTasks.get(taskId);
747
- if (pendingIds !== undefined && pendingIds.length > 0) {
748
- // Ignore the volunteer op if we know this task is about to be completed
749
- return;
750
- }
751
-
752
- // Ensure that the clientId exists in the quorum, or it is placeholderClientId (detached scenario)
753
- if (this.runtime.getQuorum().getMembers().has(clientId) || this.clientId === placeholderClientId) {
754
- // Create the queue if it doesn't exist, and push the client on the back.
755
- let clientQueue = this.taskQueues.get(taskId);
756
- if (clientQueue === undefined) {
757
- clientQueue = [];
758
- this.taskQueues.set(taskId, clientQueue);
759
- }
760
-
761
- const oldLockHolder = clientQueue[0];
762
- clientQueue.push(clientId);
763
- const newLockHolder = clientQueue[0];
764
- if (newLockHolder !== oldLockHolder) {
765
- this.queueWatcher.emit("queueChange", taskId, oldLockHolder, newLockHolder);
766
- }
767
-
768
- }
769
- }
770
-
771
- private removeClientFromQueue(taskId: string, clientId: string) {
772
- const clientQueue = this.taskQueues.get(taskId);
773
- if (clientQueue === undefined) {
774
- return;
775
- }
776
-
777
- const oldLockHolder = clientId === placeholderClientId ? placeholderClientId : clientQueue[0];
778
- const clientIdIndex = clientQueue.indexOf(clientId);
779
- if (clientIdIndex !== -1) {
780
- clientQueue.splice(clientIdIndex, 1);
781
- // Clean up the queue if there are no more clients in it.
782
- if (clientQueue.length === 0) {
783
- this.taskQueues.delete(taskId);
784
- }
785
- }
786
- const newLockHolder = clientQueue[0];
787
- if (newLockHolder !== oldLockHolder) {
788
- this.queueWatcher.emit("queueChange", taskId, oldLockHolder, newLockHolder);
789
- }
790
- }
791
-
792
- private removeClientFromAllQueues(clientId: string) {
793
- for (const taskId of this.taskQueues.keys()) {
794
- this.removeClientFromQueue(taskId, clientId);
795
- }
796
- }
797
-
798
- /**
799
- * Will replace all instances of the placeholderClientId with the current clientId. This should only be called when
800
- * transitioning from detached to attached and this.runtime.clientId is defined.
801
- */
802
- private replacePlaceholderInAllQueues() {
803
- assert(this.runtime.clientId !== undefined, 0x475 /* this.runtime.clientId should be defined */);
804
- for (const clientQueue of this.taskQueues.values()) {
805
- const clientIdIndex = clientQueue.indexOf(placeholderClientId);
806
- if (clientIdIndex !== -1) {
807
- clientQueue[clientIdIndex] = this.runtime.clientId;
808
- }
809
- }
810
- }
811
-
812
- // This seems like it should be unnecessary if we can trust to receive the join/leave messages and
813
- // also have an accurate snapshot.
814
- private scrubClientsNotInQuorum() {
815
- const quorum = this.runtime.getQuorum();
816
- for (const [taskId, clientQueue] of this.taskQueues) {
817
- const filteredClientQueue = clientQueue.filter((clientId) => quorum.getMember(clientId) !== undefined);
818
- if (clientQueue.length !== filteredClientQueue.length) {
819
- if (filteredClientQueue.length === 0) {
820
- this.taskQueues.delete(taskId);
821
- } else {
822
- this.taskQueues.set(taskId, filteredClientQueue);
823
- }
824
- this.queueWatcher.emit("queueChange", taskId);
825
- }
826
- }
827
- }
828
-
829
- public applyStashedOp() {
830
- throw new Error("not implemented");
831
- }
151
+ /**
152
+ * Create a new TaskManager
153
+ *
154
+ * @param runtime - data store runtime the new task queue belongs to
155
+ * @param id - optional name of the task queue
156
+ * @returns newly create task queue (but not attached yet)
157
+ */
158
+ public static create(runtime: IFluidDataStoreRuntime, id?: string) {
159
+ return runtime.createChannel(id, TaskManagerFactory.Type) as TaskManager;
160
+ }
161
+
162
+ /**
163
+ * Get a factory for TaskManager to register with the data store.
164
+ *
165
+ * @returns a factory that creates and load TaskManager
166
+ */
167
+ public static getFactory(): IChannelFactory {
168
+ return new TaskManagerFactory();
169
+ }
170
+
171
+ /**
172
+ * Mapping of taskId to a queue of clientIds that are waiting on the task. Maintains the consensus state of the
173
+ * queue, even if we know we've submitted an op that should eventually modify the queue.
174
+ */
175
+ private readonly taskQueues: Map<string, string[]> = new Map();
176
+
177
+ // opWatcher emits for every op on this data store. This is just a repackaging of processCore into events.
178
+ private readonly opWatcher: EventEmitter = new EventEmitter();
179
+ // queueWatcher emits an event whenever the consensus state of the task queues changes
180
+ private readonly queueWatcher: EventEmitter = new EventEmitter();
181
+ // abandonWatcher emits an event whenever the local client calls abandon() on a task.
182
+ private readonly abandonWatcher: EventEmitter = new EventEmitter();
183
+ // connectionWatcher emits an event whenever we get connected or disconnected.
184
+ private readonly connectionWatcher: EventEmitter = new EventEmitter();
185
+ // completedWatcher emits an event whenever the local client receives a completed op.
186
+ private readonly completedWatcher: EventEmitter = new EventEmitter();
187
+
188
+ private messageId: number = -1;
189
+ /**
190
+ * Tracks the most recent pending op for a given task
191
+ */
192
+ private readonly latestPendingOps: Map<string, IPendingOp> = new Map();
193
+
194
+ /**
195
+ * Tracks tasks that are this client is currently subscribed to.
196
+ */
197
+ private readonly subscribedTasks: Set<string> = new Set();
198
+
199
+ /**
200
+ * Map to track tasks that have pending complete ops.
201
+ */
202
+ private readonly pendingCompletedTasks: Map<string, number[]> = new Map();
203
+
204
+ /**
205
+ * Returns the clientId. Will return a placeholder if the runtime is detached and not yet assigned a clientId.
206
+ */
207
+ private get clientId(): string | undefined {
208
+ return this.isAttached() ? this.runtime.clientId : placeholderClientId;
209
+ }
210
+
211
+ /**
212
+ * Returns a ReadOnlyInfo object to determine current read/write permissions.
213
+ */
214
+ private get readOnlyInfo(): ReadOnlyInfo {
215
+ return this.runtime.deltaManager.readOnlyInfo;
216
+ }
217
+
218
+ /**
219
+ * Constructs a new task manager. If the object is non-local an id and service interfaces will
220
+ * be provided
221
+ *
222
+ * @param runtime - data store runtime the task queue belongs to
223
+ * @param id - optional name of the task queue
224
+ */
225
+ constructor(id: string, runtime: IFluidDataStoreRuntime, attributes: IChannelAttributes) {
226
+ super(id, runtime, attributes, "fluid_taskManager_");
227
+
228
+ this.opWatcher.on(
229
+ "volunteer",
230
+ (taskId: string, clientId: string, local: boolean, messageId: number) => {
231
+ // We're tracking local ops from this connection. Filter out local ops during "connecting"
232
+ // state since these were sent on the prior connection and were already cleared from the latestPendingOps.
233
+ if (runtime.connected && local) {
234
+ const pendingOp = this.latestPendingOps.get(taskId);
235
+ assert(pendingOp !== undefined, 0x07b /* "Unexpected op" */);
236
+ // Need to check the id, since it's possible to volunteer and abandon multiple times before the acks
237
+ if (messageId === pendingOp.messageId) {
238
+ assert(pendingOp.type === "volunteer", 0x07c /* "Unexpected op type" */);
239
+ // Delete the pending, because we no longer have an outstanding op
240
+ this.latestPendingOps.delete(taskId);
241
+ }
242
+ }
243
+
244
+ this.addClientToQueue(taskId, clientId);
245
+ },
246
+ );
247
+
248
+ this.opWatcher.on(
249
+ "abandon",
250
+ (taskId: string, clientId: string, local: boolean, messageId: number) => {
251
+ if (runtime.connected && local) {
252
+ const pendingOp = this.latestPendingOps.get(taskId);
253
+ assert(pendingOp !== undefined, 0x07d /* "Unexpected op" */);
254
+ // Need to check the id, since it's possible to abandon and volunteer multiple times before the acks
255
+ if (messageId === pendingOp.messageId) {
256
+ assert(pendingOp.type === "abandon", 0x07e /* "Unexpected op type" */);
257
+ // Delete the pending, because we no longer have an outstanding op
258
+ this.latestPendingOps.delete(taskId);
259
+ }
260
+ }
261
+
262
+ this.removeClientFromQueue(taskId, clientId);
263
+ },
264
+ );
265
+
266
+ this.opWatcher.on(
267
+ "complete",
268
+ (taskId: string, clientId: string, local: boolean, messageId: number) => {
269
+ if (runtime.connected && local) {
270
+ const pendingOp = this.latestPendingOps.get(taskId);
271
+ assert(pendingOp !== undefined, 0x400 /* Unexpected op */);
272
+ // Need to check the id, since it's possible to complete multiple times before the acks
273
+ if (messageId === pendingOp.messageId) {
274
+ assert(pendingOp.type === "complete", 0x401 /* Unexpected op type */);
275
+ // Delete the pending, because we no longer have an outstanding op
276
+ this.latestPendingOps.delete(taskId);
277
+ }
278
+
279
+ // Remove complete op from this.pendingCompletedTasks
280
+ const pendingIds = this.pendingCompletedTasks.get(taskId);
281
+ assert(
282
+ pendingIds !== undefined && pendingIds.length > 0,
283
+ 0x402 /* pendingIds is empty */,
284
+ );
285
+ const removed = pendingIds.shift();
286
+ assert(
287
+ removed === messageId,
288
+ 0x403 /* Removed complete op id does not match */,
289
+ );
290
+ }
291
+
292
+ // For clients in queue, we need to remove them from the queue and raise the proper events.
293
+ if (!local) {
294
+ this.taskQueues.delete(taskId);
295
+ this.completedWatcher.emit("completed", taskId);
296
+ this.emit("completed", taskId);
297
+ }
298
+ },
299
+ );
300
+
301
+ runtime.getQuorum().on("removeMember", (clientId: string) => {
302
+ this.removeClientFromAllQueues(clientId);
303
+ });
304
+
305
+ this.queueWatcher.on(
306
+ "queueChange",
307
+ (taskId: string, oldLockHolder: string, newLockHolder: string) => {
308
+ // If oldLockHolder is placeholderClientId we need to emit the task was lost during the attach process
309
+ if (oldLockHolder === placeholderClientId) {
310
+ this.emit("lost", taskId);
311
+ return;
312
+ }
313
+
314
+ // Exit early if we are still catching up on reconnect -- we can't be the leader yet anyway.
315
+ if (this.clientId === undefined) {
316
+ return;
317
+ }
318
+
319
+ if (oldLockHolder !== this.clientId && newLockHolder === this.clientId) {
320
+ this.emit("assigned", taskId);
321
+ } else if (oldLockHolder === this.clientId && newLockHolder !== this.clientId) {
322
+ this.emit("lost", taskId);
323
+ }
324
+ },
325
+ );
326
+
327
+ this.connectionWatcher.on("disconnect", () => {
328
+ assert(this.clientId !== undefined, 0x1d3 /* "Missing client id on disconnect" */);
329
+
330
+ // We don't modify the taskQueues on disconnect (they still reflect the latest known consensus state).
331
+ // After reconnect these will get cleaned up by observing the clientLeaves.
332
+ // However we do need to recognize that we lost the lock if we had it. Calls to .queued() and
333
+ // .assigned() are also connection-state-aware to be consistent.
334
+ for (const [taskId, clientQueue] of this.taskQueues.entries()) {
335
+ if (this.isAttached() && clientQueue[0] === this.clientId) {
336
+ this.emit("lost", taskId);
337
+ }
338
+ }
339
+
340
+ // All of our outstanding ops will be for the old clientId even if they get ack'd
341
+ this.latestPendingOps.clear();
342
+ });
343
+ }
344
+
345
+ private submitVolunteerOp(taskId: string) {
346
+ const op: ITaskManagerVolunteerOperation = {
347
+ type: "volunteer",
348
+ taskId,
349
+ };
350
+ const pendingOp: IPendingOp = {
351
+ type: "volunteer",
352
+ messageId: ++this.messageId,
353
+ };
354
+ this.submitLocalMessage(op, pendingOp.messageId);
355
+ this.latestPendingOps.set(taskId, pendingOp);
356
+ }
357
+
358
+ private submitAbandonOp(taskId: string) {
359
+ const op: ITaskManagerAbandonOperation = {
360
+ type: "abandon",
361
+ taskId,
362
+ };
363
+ const pendingOp: IPendingOp = {
364
+ type: "abandon",
365
+ messageId: ++this.messageId,
366
+ };
367
+ this.submitLocalMessage(op, pendingOp.messageId);
368
+ this.latestPendingOps.set(taskId, pendingOp);
369
+ }
370
+
371
+ private submitCompleteOp(taskId: string) {
372
+ const op: ITaskManagerCompletedOperation = {
373
+ type: "complete",
374
+ taskId,
375
+ };
376
+ const pendingOp: IPendingOp = {
377
+ type: "complete",
378
+ messageId: ++this.messageId,
379
+ };
380
+
381
+ if (this.pendingCompletedTasks.has(taskId)) {
382
+ this.pendingCompletedTasks.get(taskId)?.push(pendingOp.messageId);
383
+ } else {
384
+ this.pendingCompletedTasks.set(taskId, [pendingOp.messageId]);
385
+ }
386
+
387
+ this.submitLocalMessage(op, pendingOp.messageId);
388
+ this.latestPendingOps.set(taskId, pendingOp);
389
+ }
390
+
391
+ /**
392
+ * {@inheritDoc ITaskManager.volunteerForTask}
393
+ */
394
+ public async volunteerForTask(taskId: string) {
395
+ // If we have the lock, resolve immediately
396
+ if (this.assigned(taskId)) {
397
+ return true;
398
+ }
399
+
400
+ if (this.readOnlyInfo.readonly === true) {
401
+ const error =
402
+ this.readOnlyInfo.permissions === true
403
+ ? new Error("Attempted to volunteer with read-only permissions")
404
+ : new Error("Attempted to volunteer in read-only state");
405
+ throw error;
406
+ }
407
+
408
+ if (!this.isAttached()) {
409
+ // Simulate auto-ack in detached scenario
410
+ assert(this.clientId !== undefined, 0x472 /* clientId should not be undefined */);
411
+ this.addClientToQueue(taskId, this.clientId);
412
+ return true;
413
+ }
414
+
415
+ if (!this.connected) {
416
+ throw new Error("Attempted to volunteer in disconnected state");
417
+ }
418
+
419
+ // This promise works even if we already have an outstanding volunteer op.
420
+ const lockAcquireP = new Promise<boolean>((resolve, reject) => {
421
+ const checkIfAcquiredLock = (eventTaskId: string) => {
422
+ if (eventTaskId !== taskId) {
423
+ return;
424
+ }
425
+
426
+ // Also check pending ops here because it's possible we are currently in the queue from a previous
427
+ // lock attempt, but have an outstanding abandon AND the outstanding volunteer for this lock attempt.
428
+ // If we reach the head of the queue based on the previous lock attempt, we don't want to resolve.
429
+ if (this.assigned(taskId) && !this.latestPendingOps.has(taskId)) {
430
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
431
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
432
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
433
+ this.completedWatcher.off("completed", checkIfCompleted);
434
+ resolve(true);
435
+ }
436
+ };
437
+
438
+ const checkIfAbandoned = (eventTaskId: string) => {
439
+ if (eventTaskId !== taskId) {
440
+ return;
441
+ }
442
+
443
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
444
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
445
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
446
+ this.completedWatcher.off("completed", checkIfCompleted);
447
+ reject(new Error("Abandoned before acquiring task assignment"));
448
+ };
449
+
450
+ const rejectOnDisconnect = () => {
451
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
452
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
453
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
454
+ this.completedWatcher.off("completed", checkIfCompleted);
455
+ reject(new Error("Disconnected before acquiring task assignment"));
456
+ };
457
+
458
+ const checkIfCompleted = (eventTaskId: string) => {
459
+ if (eventTaskId !== taskId) {
460
+ return;
461
+ }
462
+
463
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
464
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
465
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
466
+ this.completedWatcher.off("completed", checkIfCompleted);
467
+ resolve(false);
468
+ };
469
+
470
+ this.queueWatcher.on("queueChange", checkIfAcquiredLock);
471
+ this.abandonWatcher.on("abandon", checkIfAbandoned);
472
+ this.connectionWatcher.on("disconnect", rejectOnDisconnect);
473
+ this.completedWatcher.on("completed", checkIfCompleted);
474
+ });
475
+
476
+ if (!this.queued(taskId)) {
477
+ this.submitVolunteerOp(taskId);
478
+ }
479
+ return lockAcquireP;
480
+ }
481
+
482
+ /**
483
+ * {@inheritDoc ITaskManager.subscribeToTask}
484
+ */
485
+ public subscribeToTask(taskId: string) {
486
+ if (this.subscribed(taskId)) {
487
+ return;
488
+ }
489
+
490
+ if (this.readOnlyInfo.readonly === true && this.readOnlyInfo.permissions === true) {
491
+ throw new Error("Attempted to subscribe with read-only permissions");
492
+ }
493
+
494
+ const submitVolunteerOp = () => {
495
+ this.submitVolunteerOp(taskId);
496
+ };
497
+
498
+ const disconnectHandler = () => {
499
+ // Wait to be connected again and then re-submit volunteer op
500
+ this.connectionWatcher.once("connect", submitVolunteerOp);
501
+ };
502
+
503
+ const checkIfAbandoned = (eventTaskId: string) => {
504
+ if (eventTaskId !== taskId) {
505
+ return;
506
+ }
507
+
508
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
509
+ this.connectionWatcher.off("disconnect", disconnectHandler);
510
+ this.connectionWatcher.off("connect", submitVolunteerOp);
511
+ this.completedWatcher.off("completed", checkIfCompleted);
512
+
513
+ this.subscribedTasks.delete(taskId);
514
+ };
515
+
516
+ const checkIfCompleted = (eventTaskId: string) => {
517
+ if (eventTaskId !== taskId) {
518
+ return;
519
+ }
520
+
521
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
522
+ this.connectionWatcher.off("disconnect", disconnectHandler);
523
+ this.connectionWatcher.off("connect", submitVolunteerOp);
524
+ this.completedWatcher.off("completed", checkIfCompleted);
525
+
526
+ this.subscribedTasks.delete(taskId);
527
+ };
528
+
529
+ this.abandonWatcher.on("abandon", checkIfAbandoned);
530
+ this.connectionWatcher.on("disconnect", disconnectHandler);
531
+ this.completedWatcher.on("completed", checkIfCompleted);
532
+
533
+ if (!this.isAttached()) {
534
+ // Simulate auto-ack in detached scenario
535
+ assert(this.clientId !== undefined, 0x473 /* clientId should not be undefined */);
536
+ this.addClientToQueue(taskId, this.clientId);
537
+ // Because we volunteered with placeholderClientId, we need to wait for when we attach and are assigned
538
+ // a real clientId. At that point we should re-enter the queue with a real volunteer op (assuming we are
539
+ // connected).
540
+ this.runtime.once("attached", () => {
541
+ if (this.queued(taskId)) {
542
+ // If we are already queued, then we were able to replace the placeholderClientId with our real
543
+ // clientId and no action is required.
544
+ return;
545
+ } else if (this.connected) {
546
+ submitVolunteerOp();
547
+ } else {
548
+ this.connectionWatcher.once("connect", () => {
549
+ submitVolunteerOp();
550
+ });
551
+ }
552
+ });
553
+ } else if (!this.connected) {
554
+ // If we are disconnected (and attached), wait to be connected and submit volunteer op
555
+ disconnectHandler();
556
+ } else if (!this.assigned(taskId) && !this.queued(taskId)) {
557
+ submitVolunteerOp();
558
+ }
559
+ this.subscribedTasks.add(taskId);
560
+ }
561
+
562
+ /**
563
+ * {@inheritDoc ITaskManager.abandon}
564
+ */
565
+ public abandon(taskId: string) {
566
+ // Always allow abandon if the client is subscribed to allow clients to unsubscribe while disconnected.
567
+ // Otherwise, we should check to make sure the client is both connected queued for the task before sending an
568
+ // abandon op.
569
+ if (!this.subscribed(taskId) && !this.queued(taskId)) {
570
+ // Nothing to do
571
+ return;
572
+ }
573
+
574
+ if (!this.isAttached()) {
575
+ // Simulate auto-ack in detached scenario
576
+ assert(this.clientId !== undefined, 0x474 /* clientId is undefined */);
577
+ this.removeClientFromQueue(taskId, this.clientId);
578
+ this.abandonWatcher.emit("abandon", taskId);
579
+ return;
580
+ }
581
+
582
+ // If we're subscribed but not queued, we don't need to submit an abandon op (probably offline)
583
+ if (this.queued(taskId)) {
584
+ this.submitAbandonOp(taskId);
585
+ }
586
+ this.abandonWatcher.emit("abandon", taskId);
587
+ }
588
+
589
+ /**
590
+ * {@inheritDoc ITaskManager.assigned}
591
+ */
592
+ public assigned(taskId: string) {
593
+ if (this.isAttached() && !this.connected) {
594
+ return false;
595
+ }
596
+
597
+ const currentAssignee = this.taskQueues.get(taskId)?.[0];
598
+ return (
599
+ currentAssignee !== undefined &&
600
+ currentAssignee === this.clientId &&
601
+ !this.latestPendingOps.has(taskId)
602
+ );
603
+ }
604
+
605
+ /**
606
+ * {@inheritDoc ITaskManager.queued}
607
+ */
608
+ public queued(taskId: string) {
609
+ if (this.isAttached() && !this.connected) {
610
+ return false;
611
+ }
612
+
613
+ assert(this.clientId !== undefined, 0x07f /* "clientId undefined" */);
614
+
615
+ const clientQueue = this.taskQueues.get(taskId);
616
+ // If we have no queue for the taskId, then no one has signed up for it.
617
+ return (
618
+ ((clientQueue?.includes(this.clientId) ?? false) &&
619
+ !this.latestPendingOps.has(taskId)) ||
620
+ this.latestPendingOps.get(taskId)?.type === "volunteer"
621
+ );
622
+ }
623
+
624
+ /**
625
+ * {@inheritDoc ITaskManager.subscribed}
626
+ */
627
+ public subscribed(taskId: string): boolean {
628
+ return this.subscribedTasks.has(taskId);
629
+ }
630
+
631
+ /**
632
+ * {@inheritDoc ITaskManager.complete}
633
+ */
634
+ public complete(taskId: string): void {
635
+ if (!this.assigned(taskId)) {
636
+ throw new Error("Attempted to mark task as complete while not being assigned");
637
+ }
638
+
639
+ // If we are detached we will simulate auto-ack for the complete op. Therefore we only need to send the op if
640
+ // we are attached. Additionally, we don't need to check if we are connected while detached.
641
+ if (this.isAttached()) {
642
+ if (!this.connected) {
643
+ throw new Error("Attempted to complete task in disconnected state");
644
+ }
645
+ this.submitCompleteOp(taskId);
646
+ }
647
+
648
+ this.taskQueues.delete(taskId);
649
+ this.completedWatcher.emit("completed", taskId);
650
+ this.emit("completed", taskId);
651
+ }
652
+
653
+ /**
654
+ * {@inheritDoc ITaskManager.canVolunteer}
655
+ */
656
+ public canVolunteer(): boolean {
657
+ // A client can volunteer for a task if it's both connected to the delta stream and in write mode.
658
+ // this.connected reflects that condition, but is unintuitive and may be changed in the future. This API allows
659
+ // us to make changes to this.connected without affecting our guidance on how to check if a client is eligible
660
+ // to volunteer for a task.
661
+ return this.connected;
662
+ }
663
+
664
+ /**
665
+ * Create a summary for the task manager
666
+ *
667
+ * @returns the summary of the current state of the task manager
668
+ * @internal
669
+ */
670
+ protected summarizeCore(serializer: IFluidSerializer): ISummaryTreeWithStats {
671
+ if (this.runtime.clientId !== undefined) {
672
+ // If the runtime has been assigned an actual clientId by now, we can replace the placeholder clientIds
673
+ // and maintain the task assignment.
674
+ this.replacePlaceholderInAllQueues();
675
+ } else {
676
+ // If the runtime has still not been assigned a clientId, we should not summarize with the placeholder
677
+ // clientIds and instead remove them from the queues and require the client to re-volunteer when assigned
678
+ // a new clientId.
679
+ this.removeClientFromAllQueues(placeholderClientId);
680
+ }
681
+
682
+ // Only include tasks if there are clients in the queue.
683
+ const filteredMap = new Map<string, string[]>();
684
+ this.taskQueues.forEach((queue: string[], taskId: string) => {
685
+ if (queue.length > 0) {
686
+ filteredMap.set(taskId, queue);
687
+ }
688
+ });
689
+ const content = [...filteredMap.entries()];
690
+ return createSingleBlobSummary(snapshotFileName, JSON.stringify(content));
691
+ }
692
+
693
+ /**
694
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.loadCore}
695
+ * @internal
696
+ */
697
+ protected async loadCore(storage: IChannelStorageService): Promise<void> {
698
+ const content = await readAndParse<[string, string[]][]>(storage, snapshotFileName);
699
+ content.forEach(([taskId, clientIdQueue]) => {
700
+ this.taskQueues.set(taskId, clientIdQueue);
701
+ });
702
+ this.scrubClientsNotInQuorum();
703
+ }
704
+
705
+ /**
706
+ * @internal
707
+ */
708
+ protected initializeLocalCore() {}
709
+
710
+ /**
711
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.onDisconnect}
712
+ * @internal
713
+ */
714
+ protected onDisconnect() {
715
+ this.connectionWatcher.emit("disconnect");
716
+ }
717
+
718
+ /**
719
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.onConnect}
720
+ * @internal
721
+ */
722
+ protected onConnect() {
723
+ this.connectionWatcher.emit("connect");
724
+ }
725
+
726
+ //
727
+ /**
728
+ * Override resubmit core to avoid resubmission on reconnect. On disconnect we accept our removal from the
729
+ * queues, and leave it up to the user to decide whether they want to attempt to re-enter a queue on reconnect.
730
+ * @internal
731
+ */
732
+ protected reSubmitCore() {}
733
+
734
+ /**
735
+ * Process a task manager operation
736
+ *
737
+ * @param message - the message to prepare
738
+ * @param local - whether the message was sent by the local client
739
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
740
+ * For messages from a remote client, this will be undefined.
741
+ * @internal
742
+ */
743
+ protected processCore(
744
+ message: ISequencedDocumentMessage,
745
+ local: boolean,
746
+ localOpMetadata: unknown,
747
+ ) {
748
+ if (message.type === MessageType.Operation) {
749
+ const op = message.contents as ITaskManagerOperation;
750
+ const messageId = localOpMetadata as number;
751
+
752
+ switch (op.type) {
753
+ case "volunteer":
754
+ this.opWatcher.emit("volunteer", op.taskId, message.clientId, local, messageId);
755
+ break;
756
+
757
+ case "abandon":
758
+ this.opWatcher.emit("abandon", op.taskId, message.clientId, local, messageId);
759
+ break;
760
+
761
+ case "complete":
762
+ this.opWatcher.emit("complete", op.taskId, message.clientId, local, messageId);
763
+ break;
764
+
765
+ default:
766
+ throw new Error("Unknown operation");
767
+ }
768
+ }
769
+ }
770
+
771
+ private addClientToQueue(taskId: string, clientId: string) {
772
+ const pendingIds = this.pendingCompletedTasks.get(taskId);
773
+ if (pendingIds !== undefined && pendingIds.length > 0) {
774
+ // Ignore the volunteer op if we know this task is about to be completed
775
+ return;
776
+ }
777
+
778
+ // Ensure that the clientId exists in the quorum, or it is placeholderClientId (detached scenario)
779
+ if (
780
+ this.runtime.getQuorum().getMembers().has(clientId) ||
781
+ this.clientId === placeholderClientId
782
+ ) {
783
+ // Create the queue if it doesn't exist, and push the client on the back.
784
+ let clientQueue = this.taskQueues.get(taskId);
785
+ if (clientQueue === undefined) {
786
+ clientQueue = [];
787
+ this.taskQueues.set(taskId, clientQueue);
788
+ }
789
+
790
+ const oldLockHolder = clientQueue[0];
791
+ clientQueue.push(clientId);
792
+ const newLockHolder = clientQueue[0];
793
+ if (newLockHolder !== oldLockHolder) {
794
+ this.queueWatcher.emit("queueChange", taskId, oldLockHolder, newLockHolder);
795
+ }
796
+ }
797
+ }
798
+
799
+ private removeClientFromQueue(taskId: string, clientId: string) {
800
+ const clientQueue = this.taskQueues.get(taskId);
801
+ if (clientQueue === undefined) {
802
+ return;
803
+ }
804
+
805
+ const oldLockHolder =
806
+ clientId === placeholderClientId ? placeholderClientId : clientQueue[0];
807
+ const clientIdIndex = clientQueue.indexOf(clientId);
808
+ if (clientIdIndex !== -1) {
809
+ clientQueue.splice(clientIdIndex, 1);
810
+ // Clean up the queue if there are no more clients in it.
811
+ if (clientQueue.length === 0) {
812
+ this.taskQueues.delete(taskId);
813
+ }
814
+ }
815
+ const newLockHolder = clientQueue[0];
816
+ if (newLockHolder !== oldLockHolder) {
817
+ this.queueWatcher.emit("queueChange", taskId, oldLockHolder, newLockHolder);
818
+ }
819
+ }
820
+
821
+ private removeClientFromAllQueues(clientId: string) {
822
+ for (const taskId of this.taskQueues.keys()) {
823
+ this.removeClientFromQueue(taskId, clientId);
824
+ }
825
+ }
826
+
827
+ /**
828
+ * Will replace all instances of the placeholderClientId with the current clientId. This should only be called when
829
+ * transitioning from detached to attached and this.runtime.clientId is defined.
830
+ */
831
+ private replacePlaceholderInAllQueues() {
832
+ assert(
833
+ this.runtime.clientId !== undefined,
834
+ 0x475 /* this.runtime.clientId should be defined */,
835
+ );
836
+ for (const clientQueue of this.taskQueues.values()) {
837
+ const clientIdIndex = clientQueue.indexOf(placeholderClientId);
838
+ if (clientIdIndex !== -1) {
839
+ clientQueue[clientIdIndex] = this.runtime.clientId;
840
+ }
841
+ }
842
+ }
843
+
844
+ // This seems like it should be unnecessary if we can trust to receive the join/leave messages and
845
+ // also have an accurate snapshot.
846
+ private scrubClientsNotInQuorum() {
847
+ const quorum = this.runtime.getQuorum();
848
+ for (const [taskId, clientQueue] of this.taskQueues) {
849
+ const filteredClientQueue = clientQueue.filter(
850
+ (clientId) => quorum.getMember(clientId) !== undefined,
851
+ );
852
+ if (clientQueue.length !== filteredClientQueue.length) {
853
+ if (filteredClientQueue.length === 0) {
854
+ this.taskQueues.delete(taskId);
855
+ } else {
856
+ this.taskQueues.set(taskId, filteredClientQueue);
857
+ }
858
+ this.queueWatcher.emit("queueChange", taskId);
859
+ }
860
+ }
861
+ }
862
+
863
+ public applyStashedOp() {
864
+ throw new Error("not implemented");
865
+ }
832
866
  }