@fluidframework/task-manager 2.0.0-internal.2.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 (54) hide show
  1. package/.eslintrc.js +14 -0
  2. package/.mocharc.js +12 -0
  3. package/LICENSE +21 -0
  4. package/README.md +36 -0
  5. package/api-extractor.json +4 -0
  6. package/dist/index.d.ts +7 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +10 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/interfaces.d.ts +65 -0
  11. package/dist/interfaces.d.ts.map +1 -0
  12. package/dist/interfaces.js +7 -0
  13. package/dist/interfaces.js.map +1 -0
  14. package/dist/packageVersion.d.ts +9 -0
  15. package/dist/packageVersion.d.ts.map +1 -0
  16. package/dist/packageVersion.js +12 -0
  17. package/dist/packageVersion.js.map +1 -0
  18. package/dist/taskManager.d.ts +242 -0
  19. package/dist/taskManager.d.ts.map +1 -0
  20. package/dist/taskManager.js +715 -0
  21. package/dist/taskManager.js.map +1 -0
  22. package/dist/taskManagerFactory.d.ts +21 -0
  23. package/dist/taskManagerFactory.d.ts.map +1 -0
  24. package/dist/taskManagerFactory.js +41 -0
  25. package/dist/taskManagerFactory.js.map +1 -0
  26. package/lib/index.d.ts +7 -0
  27. package/lib/index.d.ts.map +1 -0
  28. package/lib/index.js +6 -0
  29. package/lib/index.js.map +1 -0
  30. package/lib/interfaces.d.ts +65 -0
  31. package/lib/interfaces.d.ts.map +1 -0
  32. package/lib/interfaces.js +6 -0
  33. package/lib/interfaces.js.map +1 -0
  34. package/lib/packageVersion.d.ts +9 -0
  35. package/lib/packageVersion.d.ts.map +1 -0
  36. package/lib/packageVersion.js +9 -0
  37. package/lib/packageVersion.js.map +1 -0
  38. package/lib/taskManager.d.ts +242 -0
  39. package/lib/taskManager.d.ts.map +1 -0
  40. package/lib/taskManager.js +711 -0
  41. package/lib/taskManager.js.map +1 -0
  42. package/lib/taskManagerFactory.d.ts +21 -0
  43. package/lib/taskManagerFactory.d.ts.map +1 -0
  44. package/lib/taskManagerFactory.js +37 -0
  45. package/lib/taskManagerFactory.js.map +1 -0
  46. package/package.json +116 -0
  47. package/prettier.config.cjs +8 -0
  48. package/src/index.ts +7 -0
  49. package/src/interfaces.ts +74 -0
  50. package/src/packageVersion.ts +9 -0
  51. package/src/taskManager.ts +832 -0
  52. package/src/taskManagerFactory.ts +54 -0
  53. package/tsconfig.esnext.json +7 -0
  54. package/tsconfig.json +14 -0
@@ -0,0 +1,832 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { EventEmitter } from "events";
7
+
8
+ import { assert } from "@fluidframework/common-utils";
9
+ import { ISequencedDocumentMessage, MessageType } from "@fluidframework/protocol-definitions";
10
+ import {
11
+ IChannelAttributes,
12
+ IFluidDataStoreRuntime,
13
+ IChannelStorageService,
14
+ IChannelFactory,
15
+ } from "@fluidframework/datastore-definitions";
16
+ import { ISummaryTreeWithStats } from "@fluidframework/runtime-definitions";
17
+ import { readAndParse } from "@fluidframework/driver-utils";
18
+ import { createSingleBlobSummary, IFluidSerializer, SharedObject } from "@fluidframework/shared-object-base";
19
+ import { ReadOnlyInfo } from "@fluidframework/container-definitions";
20
+ import { TaskManagerFactory } from "./taskManagerFactory";
21
+ import { ITaskManager, ITaskManagerEvents } from "./interfaces";
22
+
23
+ /**
24
+ * Description of a task manager operation
25
+ */
26
+ type ITaskManagerOperation =
27
+ ITaskManagerVolunteerOperation |
28
+ ITaskManagerAbandonOperation |
29
+ ITaskManagerCompletedOperation;
30
+
31
+ interface ITaskManagerVolunteerOperation {
32
+ type: "volunteer";
33
+ taskId: string;
34
+ }
35
+
36
+ interface ITaskManagerAbandonOperation {
37
+ type: "abandon";
38
+ taskId: string;
39
+ }
40
+
41
+ interface ITaskManagerCompletedOperation {
42
+ type: "complete";
43
+ taskId: string;
44
+ }
45
+
46
+ interface IPendingOp {
47
+ type: "volunteer" | "abandon" | "complete";
48
+ messageId: number;
49
+ }
50
+
51
+ const snapshotFileName = "header";
52
+
53
+ /**
54
+ * Placeholder clientId for detached scenarios.
55
+ */
56
+ const placeholderClientId = "placeholder";
57
+
58
+
59
+ /**
60
+ * The TaskManager distributed data structure tracks queues of clients that want to exclusively run a task.
61
+ *
62
+ * @remarks
63
+ *
64
+ * For an in-depth overview, see [TaskManager](https://fluidframework.com/docs/data-structures/task-manager/).
65
+ *
66
+ * ### Creation
67
+ *
68
+ * To create a `TaskManager`, call the static create method:
69
+ *
70
+ * ```typescript
71
+ * const taskManager = TaskManager.create(this.runtime, id);
72
+ * ```
73
+ *
74
+ * ### Usage
75
+ *
76
+ * To volunteer for a task, use the `volunteerForTask()` method. This returns a Promise that will resolve once the
77
+ * client has acquired exclusive rights to run the task, or reject if the client is removed from the queue without
78
+ * acquiring the rights.
79
+ *
80
+ * ```typescript
81
+ * taskManager.volunteerForTask("NameOfTask")
82
+ * .then(() => { doTheTask(); })
83
+ * .catch((err) => { console.error(err); });
84
+ * ```
85
+ *
86
+ * Alternatively, you can indefinitely volunteer for a task with the synchronous `subscribeToTask()` method. This
87
+ * method does not return a value, therefore you need to rely on eventing to know when you have acquired the rights
88
+ * to run the task (see below).
89
+ *
90
+ * ```typescript
91
+ * taskManager.subscribeToTask("NameOfTask");
92
+ * ```
93
+ *
94
+ * To check if the local client is currently subscribed to a task, use the `subscribed()` method.
95
+ * ```typescript
96
+ * if (taskManager.subscribed("NameOfTask")) {
97
+ * console.log("This client is currently subscribed to the task.");
98
+ * }
99
+ * ```
100
+ *
101
+ * To release the rights to the task, use the `abandon()` method. The next client in the queue will then get the
102
+ * rights to run the task.
103
+ *
104
+ * ```typescript
105
+ * taskManager.abandon("NameOfTask");
106
+ * ```
107
+ *
108
+ * To inspect your state in the queue, you can use the `queued()` and `assigned()` methods.
109
+ *
110
+ * ```typescript
111
+ * if (taskManager.queued("NameOfTask")) {
112
+ * console.log("This client is somewhere in the queue, potentially even having the task assignment.");
113
+ * }
114
+ *
115
+ * if (taskManager.assigned("NameOfTask")) {
116
+ * console.log("This client currently has the rights to run the task");
117
+ * }
118
+ * ```
119
+ *
120
+ * To signal to other connected clients that a task is completed, use the `complete()` method. This will release all
121
+ * clients from the queue and emit the "completed" event.
122
+ *
123
+ * ```typescript
124
+ * taskManager.complete("NameOfTask");
125
+ * ```
126
+ *
127
+ * ### Eventing
128
+ *
129
+ * `TaskManager` is an `EventEmitter`, and will emit events when a task is assigned to the client, when the task
130
+ * assignment is lost, and when a task was completed by another client.
131
+ *
132
+ * ```typescript
133
+ * taskManager.on("assigned", (taskId: string) => {
134
+ * console.log(`Client was assigned task: ${taskId}`);
135
+ * });
136
+ *
137
+ * taskManager.on("lost", (taskId: string) => {
138
+ * console.log(`Client released task: ${taskId}`);
139
+ * });
140
+ *
141
+ * taskManager.on("completed", (taskId: string) => {
142
+ * console.log(`Another client completed task: ${taskId}`);
143
+ * });
144
+ * ```
145
+ *
146
+ * These can be useful if the logic to volunteer for a task is separated from the logic to perform the task, such as
147
+ * when using the `subscribeToTask()` method.
148
+ */
149
+ 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: ${taskId}`) :
384
+ new Error(`Attempted to volunteer in read-only state: ${taskId}`);
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: ${taskId}`);
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: ${taskId}`));
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: ${taskId}`));
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: ${taskId}`);
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: ${taskId}`);
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: ${taskId}`);
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
+ }
832
+ }