@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,715 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.TaskManager = void 0;
8
+ const events_1 = require("events");
9
+ const common_utils_1 = require("@fluidframework/common-utils");
10
+ const protocol_definitions_1 = require("@fluidframework/protocol-definitions");
11
+ const driver_utils_1 = require("@fluidframework/driver-utils");
12
+ const shared_object_base_1 = require("@fluidframework/shared-object-base");
13
+ const taskManagerFactory_1 = require("./taskManagerFactory");
14
+ const snapshotFileName = "header";
15
+ /**
16
+ * Placeholder clientId for detached scenarios.
17
+ */
18
+ const placeholderClientId = "placeholder";
19
+ /**
20
+ * The TaskManager distributed data structure tracks queues of clients that want to exclusively run a task.
21
+ *
22
+ * @remarks
23
+ *
24
+ * For an in-depth overview, see [TaskManager](https://fluidframework.com/docs/data-structures/task-manager/).
25
+ *
26
+ * ### Creation
27
+ *
28
+ * To create a `TaskManager`, call the static create method:
29
+ *
30
+ * ```typescript
31
+ * const taskManager = TaskManager.create(this.runtime, id);
32
+ * ```
33
+ *
34
+ * ### Usage
35
+ *
36
+ * To volunteer for a task, use the `volunteerForTask()` method. This returns a Promise that will resolve once the
37
+ * client has acquired exclusive rights to run the task, or reject if the client is removed from the queue without
38
+ * acquiring the rights.
39
+ *
40
+ * ```typescript
41
+ * taskManager.volunteerForTask("NameOfTask")
42
+ * .then(() => { doTheTask(); })
43
+ * .catch((err) => { console.error(err); });
44
+ * ```
45
+ *
46
+ * Alternatively, you can indefinitely volunteer for a task with the synchronous `subscribeToTask()` method. This
47
+ * method does not return a value, therefore you need to rely on eventing to know when you have acquired the rights
48
+ * to run the task (see below).
49
+ *
50
+ * ```typescript
51
+ * taskManager.subscribeToTask("NameOfTask");
52
+ * ```
53
+ *
54
+ * To check if the local client is currently subscribed to a task, use the `subscribed()` method.
55
+ * ```typescript
56
+ * if (taskManager.subscribed("NameOfTask")) {
57
+ * console.log("This client is currently subscribed to the task.");
58
+ * }
59
+ * ```
60
+ *
61
+ * To release the rights to the task, use the `abandon()` method. The next client in the queue will then get the
62
+ * rights to run the task.
63
+ *
64
+ * ```typescript
65
+ * taskManager.abandon("NameOfTask");
66
+ * ```
67
+ *
68
+ * To inspect your state in the queue, you can use the `queued()` and `assigned()` methods.
69
+ *
70
+ * ```typescript
71
+ * if (taskManager.queued("NameOfTask")) {
72
+ * console.log("This client is somewhere in the queue, potentially even having the task assignment.");
73
+ * }
74
+ *
75
+ * if (taskManager.assigned("NameOfTask")) {
76
+ * console.log("This client currently has the rights to run the task");
77
+ * }
78
+ * ```
79
+ *
80
+ * To signal to other connected clients that a task is completed, use the `complete()` method. This will release all
81
+ * clients from the queue and emit the "completed" event.
82
+ *
83
+ * ```typescript
84
+ * taskManager.complete("NameOfTask");
85
+ * ```
86
+ *
87
+ * ### Eventing
88
+ *
89
+ * `TaskManager` is an `EventEmitter`, and will emit events when a task is assigned to the client, when the task
90
+ * assignment is lost, and when a task was completed by another client.
91
+ *
92
+ * ```typescript
93
+ * taskManager.on("assigned", (taskId: string) => {
94
+ * console.log(`Client was assigned task: ${taskId}`);
95
+ * });
96
+ *
97
+ * taskManager.on("lost", (taskId: string) => {
98
+ * console.log(`Client released task: ${taskId}`);
99
+ * });
100
+ *
101
+ * taskManager.on("completed", (taskId: string) => {
102
+ * console.log(`Another client completed task: ${taskId}`);
103
+ * });
104
+ * ```
105
+ *
106
+ * These can be useful if the logic to volunteer for a task is separated from the logic to perform the task, such as
107
+ * when using the `subscribeToTask()` method.
108
+ */
109
+ class TaskManager extends shared_object_base_1.SharedObject {
110
+ /**
111
+ * Constructs a new task manager. If the object is non-local an id and service interfaces will
112
+ * be provided
113
+ *
114
+ * @param runtime - data store runtime the task queue belongs to
115
+ * @param id - optional name of the task queue
116
+ */
117
+ constructor(id, runtime, attributes) {
118
+ super(id, runtime, attributes, "fluid_taskManager_");
119
+ /**
120
+ * Mapping of taskId to a queue of clientIds that are waiting on the task. Maintains the consensus state of the
121
+ * queue, even if we know we've submitted an op that should eventually modify the queue.
122
+ */
123
+ this.taskQueues = new Map();
124
+ // opWatcher emits for every op on this data store. This is just a repackaging of processCore into events.
125
+ this.opWatcher = new events_1.EventEmitter();
126
+ // queueWatcher emits an event whenever the consensus state of the task queues changes
127
+ this.queueWatcher = new events_1.EventEmitter();
128
+ // abandonWatcher emits an event whenever the local client calls abandon() on a task.
129
+ this.abandonWatcher = new events_1.EventEmitter();
130
+ // connectionWatcher emits an event whenever we get connected or disconnected.
131
+ this.connectionWatcher = new events_1.EventEmitter();
132
+ // completedWatcher emits an event whenever the local client receives a completed op.
133
+ this.completedWatcher = new events_1.EventEmitter();
134
+ this.messageId = -1;
135
+ /**
136
+ * Tracks the most recent pending op for a given task
137
+ */
138
+ this.latestPendingOps = new Map();
139
+ /**
140
+ * Tracks tasks that are this client is currently subscribed to.
141
+ */
142
+ this.subscribedTasks = new Set();
143
+ /**
144
+ * Map to track tasks that have pending complete ops.
145
+ */
146
+ this.pendingCompletedTasks = new Map();
147
+ this.opWatcher.on("volunteer", (taskId, clientId, local, messageId) => {
148
+ // We're tracking local ops from this connection. Filter out local ops during "connecting"
149
+ // state since these were sent on the prior connection and were already cleared from the latestPendingOps.
150
+ if (runtime.connected && local) {
151
+ const pendingOp = this.latestPendingOps.get(taskId);
152
+ (0, common_utils_1.assert)(pendingOp !== undefined, 0x07b /* "Unexpected op" */);
153
+ // Need to check the id, since it's possible to volunteer and abandon multiple times before the acks
154
+ if (messageId === pendingOp.messageId) {
155
+ (0, common_utils_1.assert)(pendingOp.type === "volunteer", 0x07c /* "Unexpected op type" */);
156
+ // Delete the pending, because we no longer have an outstanding op
157
+ this.latestPendingOps.delete(taskId);
158
+ }
159
+ }
160
+ this.addClientToQueue(taskId, clientId);
161
+ });
162
+ this.opWatcher.on("abandon", (taskId, clientId, local, messageId) => {
163
+ if (runtime.connected && local) {
164
+ const pendingOp = this.latestPendingOps.get(taskId);
165
+ (0, common_utils_1.assert)(pendingOp !== undefined, 0x07d /* "Unexpected op" */);
166
+ // Need to check the id, since it's possible to abandon and volunteer multiple times before the acks
167
+ if (messageId === pendingOp.messageId) {
168
+ (0, common_utils_1.assert)(pendingOp.type === "abandon", 0x07e /* "Unexpected op type" */);
169
+ // Delete the pending, because we no longer have an outstanding op
170
+ this.latestPendingOps.delete(taskId);
171
+ }
172
+ }
173
+ this.removeClientFromQueue(taskId, clientId);
174
+ });
175
+ this.opWatcher.on("complete", (taskId, clientId, local, messageId) => {
176
+ if (runtime.connected && local) {
177
+ const pendingOp = this.latestPendingOps.get(taskId);
178
+ (0, common_utils_1.assert)(pendingOp !== undefined, 0x400 /* Unexpected op */);
179
+ // Need to check the id, since it's possible to complete multiple times before the acks
180
+ if (messageId === pendingOp.messageId) {
181
+ (0, common_utils_1.assert)(pendingOp.type === "complete", 0x401 /* Unexpected op type */);
182
+ // Delete the pending, because we no longer have an outstanding op
183
+ this.latestPendingOps.delete(taskId);
184
+ }
185
+ // Remove complete op from this.pendingCompletedTasks
186
+ const pendingIds = this.pendingCompletedTasks.get(taskId);
187
+ (0, common_utils_1.assert)(pendingIds !== undefined && pendingIds.length > 0, 0x402 /* pendingIds is empty */);
188
+ const removed = pendingIds.shift();
189
+ (0, common_utils_1.assert)(removed === messageId, 0x403 /* Removed complete op id does not match */);
190
+ }
191
+ // For clients in queue, we need to remove them from the queue and raise the proper events.
192
+ if (!local) {
193
+ this.taskQueues.delete(taskId);
194
+ this.completedWatcher.emit("completed", taskId);
195
+ this.emit("completed", taskId);
196
+ }
197
+ });
198
+ runtime.getQuorum().on("removeMember", (clientId) => {
199
+ this.removeClientFromAllQueues(clientId);
200
+ });
201
+ this.queueWatcher.on("queueChange", (taskId, oldLockHolder, newLockHolder) => {
202
+ // If oldLockHolder is placeholderClientId we need to emit the task was lost during the attach process
203
+ if (oldLockHolder === placeholderClientId) {
204
+ this.emit("lost", taskId);
205
+ return;
206
+ }
207
+ // Exit early if we are still catching up on reconnect -- we can't be the leader yet anyway.
208
+ if (this.clientId === undefined) {
209
+ return;
210
+ }
211
+ if (oldLockHolder !== this.clientId && newLockHolder === this.clientId) {
212
+ this.emit("assigned", taskId);
213
+ }
214
+ else if (oldLockHolder === this.clientId && newLockHolder !== this.clientId) {
215
+ this.emit("lost", taskId);
216
+ }
217
+ });
218
+ this.connectionWatcher.on("disconnect", () => {
219
+ (0, common_utils_1.assert)(this.clientId !== undefined, 0x1d3 /* "Missing client id on disconnect" */);
220
+ // We don't modify the taskQueues on disconnect (they still reflect the latest known consensus state).
221
+ // After reconnect these will get cleaned up by observing the clientLeaves.
222
+ // However we do need to recognize that we lost the lock if we had it. Calls to .queued() and
223
+ // .assigned() are also connection-state-aware to be consistent.
224
+ for (const [taskId, clientQueue] of this.taskQueues.entries()) {
225
+ if (this.isAttached() && clientQueue[0] === this.clientId) {
226
+ this.emit("lost", taskId);
227
+ }
228
+ }
229
+ // All of our outstanding ops will be for the old clientId even if they get ack'd
230
+ this.latestPendingOps.clear();
231
+ });
232
+ }
233
+ /**
234
+ * Create a new TaskManager
235
+ *
236
+ * @param runtime - data store runtime the new task queue belongs to
237
+ * @param id - optional name of the task queue
238
+ * @returns newly create task queue (but not attached yet)
239
+ */
240
+ static create(runtime, id) {
241
+ return runtime.createChannel(id, taskManagerFactory_1.TaskManagerFactory.Type);
242
+ }
243
+ /**
244
+ * Get a factory for TaskManager to register with the data store.
245
+ *
246
+ * @returns a factory that creates and load TaskManager
247
+ */
248
+ static getFactory() {
249
+ return new taskManagerFactory_1.TaskManagerFactory();
250
+ }
251
+ /**
252
+ * Returns the clientId. Will return a placeholder if the runtime is detached and not yet assigned a clientId.
253
+ */
254
+ get clientId() {
255
+ return this.isAttached() ? this.runtime.clientId : placeholderClientId;
256
+ }
257
+ /**
258
+ * Returns a ReadOnlyInfo object to determine current read/write permissions.
259
+ */
260
+ get readOnlyInfo() {
261
+ return this.runtime.deltaManager.readOnlyInfo;
262
+ }
263
+ submitVolunteerOp(taskId) {
264
+ const op = {
265
+ type: "volunteer",
266
+ taskId,
267
+ };
268
+ const pendingOp = {
269
+ type: "volunteer",
270
+ messageId: ++this.messageId,
271
+ };
272
+ this.submitLocalMessage(op, pendingOp.messageId);
273
+ this.latestPendingOps.set(taskId, pendingOp);
274
+ }
275
+ submitAbandonOp(taskId) {
276
+ const op = {
277
+ type: "abandon",
278
+ taskId,
279
+ };
280
+ const pendingOp = {
281
+ type: "abandon",
282
+ messageId: ++this.messageId,
283
+ };
284
+ this.submitLocalMessage(op, pendingOp.messageId);
285
+ this.latestPendingOps.set(taskId, pendingOp);
286
+ }
287
+ submitCompleteOp(taskId) {
288
+ var _a;
289
+ const op = {
290
+ type: "complete",
291
+ taskId,
292
+ };
293
+ const pendingOp = {
294
+ type: "complete",
295
+ messageId: ++this.messageId,
296
+ };
297
+ if (this.pendingCompletedTasks.has(taskId)) {
298
+ (_a = this.pendingCompletedTasks.get(taskId)) === null || _a === void 0 ? void 0 : _a.push(pendingOp.messageId);
299
+ }
300
+ else {
301
+ this.pendingCompletedTasks.set(taskId, [pendingOp.messageId]);
302
+ }
303
+ this.submitLocalMessage(op, pendingOp.messageId);
304
+ this.latestPendingOps.set(taskId, pendingOp);
305
+ }
306
+ /**
307
+ * {@inheritDoc ITaskManager.volunteerForTask}
308
+ */
309
+ async volunteerForTask(taskId) {
310
+ // If we have the lock, resolve immediately
311
+ if (this.assigned(taskId)) {
312
+ return true;
313
+ }
314
+ if (this.readOnlyInfo.readonly === true) {
315
+ const error = this.readOnlyInfo.permissions === true ?
316
+ new Error(`Attempted to volunteer with read-only permissions: ${taskId}`) :
317
+ new Error(`Attempted to volunteer in read-only state: ${taskId}`);
318
+ throw error;
319
+ }
320
+ if (!this.isAttached()) {
321
+ // Simulate auto-ack in detached scenario
322
+ (0, common_utils_1.assert)(this.clientId !== undefined, 0x472 /* clientId should not be undefined */);
323
+ this.addClientToQueue(taskId, this.clientId);
324
+ return true;
325
+ }
326
+ if (!this.connected) {
327
+ throw new Error(`Attempted to volunteer in disconnected state: ${taskId}`);
328
+ }
329
+ // This promise works even if we already have an outstanding volunteer op.
330
+ const lockAcquireP = new Promise((resolve, reject) => {
331
+ const checkIfAcquiredLock = (eventTaskId) => {
332
+ if (eventTaskId !== taskId) {
333
+ return;
334
+ }
335
+ // Also check pending ops here because it's possible we are currently in the queue from a previous
336
+ // lock attempt, but have an outstanding abandon AND the outstanding volunteer for this lock attempt.
337
+ // If we reach the head of the queue based on the previous lock attempt, we don't want to resolve.
338
+ if (this.assigned(taskId) && !this.latestPendingOps.has(taskId)) {
339
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
340
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
341
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
342
+ this.completedWatcher.off("completed", checkIfCompleted);
343
+ resolve(true);
344
+ }
345
+ };
346
+ const checkIfAbandoned = (eventTaskId) => {
347
+ if (eventTaskId !== taskId) {
348
+ return;
349
+ }
350
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
351
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
352
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
353
+ this.completedWatcher.off("completed", checkIfCompleted);
354
+ reject(new Error(`Abandoned before acquiring task assignment: ${taskId}`));
355
+ };
356
+ const rejectOnDisconnect = () => {
357
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
358
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
359
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
360
+ this.completedWatcher.off("completed", checkIfCompleted);
361
+ reject(new Error(`Disconnected before acquiring task assignment: ${taskId}`));
362
+ };
363
+ const checkIfCompleted = (eventTaskId) => {
364
+ if (eventTaskId !== taskId) {
365
+ return;
366
+ }
367
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
368
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
369
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
370
+ this.completedWatcher.off("completed", checkIfCompleted);
371
+ resolve(false);
372
+ };
373
+ this.queueWatcher.on("queueChange", checkIfAcquiredLock);
374
+ this.abandonWatcher.on("abandon", checkIfAbandoned);
375
+ this.connectionWatcher.on("disconnect", rejectOnDisconnect);
376
+ this.completedWatcher.on("completed", checkIfCompleted);
377
+ });
378
+ if (!this.queued(taskId)) {
379
+ this.submitVolunteerOp(taskId);
380
+ }
381
+ return lockAcquireP;
382
+ }
383
+ /**
384
+ * {@inheritDoc ITaskManager.subscribeToTask}
385
+ */
386
+ subscribeToTask(taskId) {
387
+ if (this.subscribed(taskId)) {
388
+ return;
389
+ }
390
+ if (this.readOnlyInfo.readonly === true && this.readOnlyInfo.permissions === true) {
391
+ throw new Error(`Attempted to subscribe with read-only permissions: ${taskId}`);
392
+ }
393
+ const submitVolunteerOp = () => {
394
+ this.submitVolunteerOp(taskId);
395
+ };
396
+ const disconnectHandler = () => {
397
+ // Wait to be connected again and then re-submit volunteer op
398
+ this.connectionWatcher.once("connect", submitVolunteerOp);
399
+ };
400
+ const checkIfAbandoned = (eventTaskId) => {
401
+ if (eventTaskId !== taskId) {
402
+ return;
403
+ }
404
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
405
+ this.connectionWatcher.off("disconnect", disconnectHandler);
406
+ this.connectionWatcher.off("connect", submitVolunteerOp);
407
+ this.completedWatcher.off("completed", checkIfCompleted);
408
+ this.subscribedTasks.delete(taskId);
409
+ };
410
+ const checkIfCompleted = (eventTaskId) => {
411
+ if (eventTaskId !== taskId) {
412
+ return;
413
+ }
414
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
415
+ this.connectionWatcher.off("disconnect", disconnectHandler);
416
+ this.connectionWatcher.off("connect", submitVolunteerOp);
417
+ this.completedWatcher.off("completed", checkIfCompleted);
418
+ this.subscribedTasks.delete(taskId);
419
+ };
420
+ this.abandonWatcher.on("abandon", checkIfAbandoned);
421
+ this.connectionWatcher.on("disconnect", disconnectHandler);
422
+ this.completedWatcher.on("completed", checkIfCompleted);
423
+ if (!this.isAttached()) {
424
+ // Simulate auto-ack in detached scenario
425
+ (0, common_utils_1.assert)(this.clientId !== undefined, 0x473 /* clientId should not be undefined */);
426
+ this.addClientToQueue(taskId, this.clientId);
427
+ // Because we volunteered with placeholderClientId, we need to wait for when we attach and are assigned
428
+ // a real clientId. At that point we should re-enter the queue with a real volunteer op (assuming we are
429
+ // connected).
430
+ this.runtime.once("attached", () => {
431
+ if (this.queued(taskId)) {
432
+ // If we are already queued, then we were able to replace the placeholderClientId with our real
433
+ // clientId and no action is required.
434
+ return;
435
+ }
436
+ else if (this.connected) {
437
+ submitVolunteerOp();
438
+ }
439
+ else {
440
+ this.connectionWatcher.once("connect", () => {
441
+ submitVolunteerOp();
442
+ });
443
+ }
444
+ });
445
+ }
446
+ else if (!this.connected) {
447
+ // If we are disconnected (and attached), wait to be connected and submit volunteer op
448
+ disconnectHandler();
449
+ }
450
+ else if (!this.assigned(taskId) && !this.queued(taskId)) {
451
+ submitVolunteerOp();
452
+ }
453
+ this.subscribedTasks.add(taskId);
454
+ }
455
+ /**
456
+ * {@inheritDoc ITaskManager.abandon}
457
+ */
458
+ abandon(taskId) {
459
+ // Always allow abandon if the client is subscribed to allow clients to unsubscribe while disconnected.
460
+ // Otherwise, we should check to make sure the client is both connected queued for the task before sending an
461
+ // abandon op.
462
+ if (!this.subscribed(taskId) && !this.queued(taskId)) {
463
+ // Nothing to do
464
+ return;
465
+ }
466
+ if (!this.isAttached()) {
467
+ // Simulate auto-ack in detached scenario
468
+ (0, common_utils_1.assert)(this.clientId !== undefined, 0x474 /* clientId is undefined */);
469
+ this.removeClientFromQueue(taskId, this.clientId);
470
+ this.abandonWatcher.emit("abandon", taskId);
471
+ return;
472
+ }
473
+ // If we're subscribed but not queued, we don't need to submit an abandon op (probably offline)
474
+ if (this.queued(taskId)) {
475
+ this.submitAbandonOp(taskId);
476
+ }
477
+ this.abandonWatcher.emit("abandon", taskId);
478
+ }
479
+ /**
480
+ * {@inheritDoc ITaskManager.assigned}
481
+ */
482
+ assigned(taskId) {
483
+ var _a;
484
+ if (this.isAttached() && !this.connected) {
485
+ return false;
486
+ }
487
+ const currentAssignee = (_a = this.taskQueues.get(taskId)) === null || _a === void 0 ? void 0 : _a[0];
488
+ return currentAssignee !== undefined
489
+ && currentAssignee === this.clientId
490
+ && !this.latestPendingOps.has(taskId);
491
+ }
492
+ /**
493
+ * {@inheritDoc ITaskManager.queued}
494
+ */
495
+ queued(taskId) {
496
+ var _a, _b;
497
+ if (this.isAttached() && !this.connected) {
498
+ return false;
499
+ }
500
+ (0, common_utils_1.assert)(this.clientId !== undefined, 0x07f /* "clientId undefined" */);
501
+ const clientQueue = this.taskQueues.get(taskId);
502
+ // If we have no queue for the taskId, then no one has signed up for it.
503
+ return (((_a = clientQueue === null || clientQueue === void 0 ? void 0 : clientQueue.includes(this.clientId)) !== null && _a !== void 0 ? _a : false)
504
+ && !this.latestPendingOps.has(taskId))
505
+ || ((_b = this.latestPendingOps.get(taskId)) === null || _b === void 0 ? void 0 : _b.type) === "volunteer";
506
+ }
507
+ /**
508
+ * {@inheritDoc ITaskManager.subscribed}
509
+ */
510
+ subscribed(taskId) {
511
+ return this.subscribedTasks.has(taskId);
512
+ }
513
+ /**
514
+ * {@inheritDoc ITaskManager.complete}
515
+ */
516
+ complete(taskId) {
517
+ if (!this.assigned(taskId)) {
518
+ throw new Error(`Attempted to mark task as complete while not being assigned: ${taskId}`);
519
+ }
520
+ // If we are detached we will simulate auto-ack for the complete op. Therefore we only need to send the op if
521
+ // we are attached. Additionally, we don't need to check if we are connected while detached.
522
+ if (this.isAttached()) {
523
+ if (!this.connected) {
524
+ throw new Error(`Attempted to complete task in disconnected state: ${taskId}`);
525
+ }
526
+ this.submitCompleteOp(taskId);
527
+ }
528
+ this.taskQueues.delete(taskId);
529
+ this.completedWatcher.emit("completed", taskId);
530
+ this.emit("completed", taskId);
531
+ }
532
+ /**
533
+ * {@inheritDoc ITaskManager.canVolunteer}
534
+ */
535
+ canVolunteer() {
536
+ // A client can volunteer for a task if it's both connected to the delta stream and in write mode.
537
+ // this.connected reflects that condition, but is unintuitive and may be changed in the future. This API allows
538
+ // us to make changes to this.connected without affecting our guidance on how to check if a client is eligible
539
+ // to volunteer for a task.
540
+ return this.connected;
541
+ }
542
+ /**
543
+ * Create a summary for the task manager
544
+ *
545
+ * @returns the summary of the current state of the task manager
546
+ * @internal
547
+ */
548
+ summarizeCore(serializer) {
549
+ if (this.runtime.clientId !== undefined) {
550
+ // If the runtime has been assigned an actual clientId by now, we can replace the placeholder clientIds
551
+ // and maintain the task assignment.
552
+ this.replacePlaceholderInAllQueues();
553
+ }
554
+ else {
555
+ // If the runtime has still not been assigned a clientId, we should not summarize with the placeholder
556
+ // clientIds and instead remove them from the queues and require the client to re-volunteer when assigned
557
+ // a new clientId.
558
+ this.removeClientFromAllQueues(placeholderClientId);
559
+ }
560
+ // Only include tasks if there are clients in the queue.
561
+ const filteredMap = new Map();
562
+ this.taskQueues.forEach((queue, taskId) => {
563
+ if (queue.length > 0) {
564
+ filteredMap.set(taskId, queue);
565
+ }
566
+ });
567
+ const content = [...filteredMap.entries()];
568
+ return (0, shared_object_base_1.createSingleBlobSummary)(snapshotFileName, JSON.stringify(content));
569
+ }
570
+ /**
571
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.loadCore}
572
+ * @internal
573
+ */
574
+ async loadCore(storage) {
575
+ const content = await (0, driver_utils_1.readAndParse)(storage, snapshotFileName);
576
+ content.forEach(([taskId, clientIdQueue]) => {
577
+ this.taskQueues.set(taskId, clientIdQueue);
578
+ });
579
+ this.scrubClientsNotInQuorum();
580
+ }
581
+ /**
582
+ * @internal
583
+ */
584
+ initializeLocalCore() { }
585
+ /**
586
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.onDisconnect}
587
+ * @internal
588
+ */
589
+ onDisconnect() {
590
+ this.connectionWatcher.emit("disconnect");
591
+ }
592
+ /**
593
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.onConnect}
594
+ * @internal
595
+ */
596
+ onConnect() {
597
+ this.connectionWatcher.emit("connect");
598
+ }
599
+ //
600
+ /**
601
+ * Override resubmit core to avoid resubmission on reconnect. On disconnect we accept our removal from the
602
+ * queues, and leave it up to the user to decide whether they want to attempt to re-enter a queue on reconnect.
603
+ * @internal
604
+ */
605
+ reSubmitCore() { }
606
+ /**
607
+ * Process a task manager operation
608
+ *
609
+ * @param message - the message to prepare
610
+ * @param local - whether the message was sent by the local client
611
+ * @param localOpMetadata - For local client messages, this is the metadata that was submitted with the message.
612
+ * For messages from a remote client, this will be undefined.
613
+ * @internal
614
+ */
615
+ processCore(message, local, localOpMetadata) {
616
+ if (message.type === protocol_definitions_1.MessageType.Operation) {
617
+ const op = message.contents;
618
+ const messageId = localOpMetadata;
619
+ switch (op.type) {
620
+ case "volunteer":
621
+ this.opWatcher.emit("volunteer", op.taskId, message.clientId, local, messageId);
622
+ break;
623
+ case "abandon":
624
+ this.opWatcher.emit("abandon", op.taskId, message.clientId, local, messageId);
625
+ break;
626
+ case "complete":
627
+ this.opWatcher.emit("complete", op.taskId, message.clientId, local, messageId);
628
+ break;
629
+ default:
630
+ throw new Error("Unknown operation");
631
+ }
632
+ }
633
+ }
634
+ addClientToQueue(taskId, clientId) {
635
+ const pendingIds = this.pendingCompletedTasks.get(taskId);
636
+ if (pendingIds !== undefined && pendingIds.length > 0) {
637
+ // Ignore the volunteer op if we know this task is about to be completed
638
+ return;
639
+ }
640
+ // Ensure that the clientId exists in the quorum, or it is placeholderClientId (detached scenario)
641
+ if (this.runtime.getQuorum().getMembers().has(clientId) || this.clientId === placeholderClientId) {
642
+ // Create the queue if it doesn't exist, and push the client on the back.
643
+ let clientQueue = this.taskQueues.get(taskId);
644
+ if (clientQueue === undefined) {
645
+ clientQueue = [];
646
+ this.taskQueues.set(taskId, clientQueue);
647
+ }
648
+ const oldLockHolder = clientQueue[0];
649
+ clientQueue.push(clientId);
650
+ const newLockHolder = clientQueue[0];
651
+ if (newLockHolder !== oldLockHolder) {
652
+ this.queueWatcher.emit("queueChange", taskId, oldLockHolder, newLockHolder);
653
+ }
654
+ }
655
+ }
656
+ removeClientFromQueue(taskId, clientId) {
657
+ const clientQueue = this.taskQueues.get(taskId);
658
+ if (clientQueue === undefined) {
659
+ return;
660
+ }
661
+ const oldLockHolder = clientId === placeholderClientId ? placeholderClientId : clientQueue[0];
662
+ const clientIdIndex = clientQueue.indexOf(clientId);
663
+ if (clientIdIndex !== -1) {
664
+ clientQueue.splice(clientIdIndex, 1);
665
+ // Clean up the queue if there are no more clients in it.
666
+ if (clientQueue.length === 0) {
667
+ this.taskQueues.delete(taskId);
668
+ }
669
+ }
670
+ const newLockHolder = clientQueue[0];
671
+ if (newLockHolder !== oldLockHolder) {
672
+ this.queueWatcher.emit("queueChange", taskId, oldLockHolder, newLockHolder);
673
+ }
674
+ }
675
+ removeClientFromAllQueues(clientId) {
676
+ for (const taskId of this.taskQueues.keys()) {
677
+ this.removeClientFromQueue(taskId, clientId);
678
+ }
679
+ }
680
+ /**
681
+ * Will replace all instances of the placeholderClientId with the current clientId. This should only be called when
682
+ * transitioning from detached to attached and this.runtime.clientId is defined.
683
+ */
684
+ replacePlaceholderInAllQueues() {
685
+ (0, common_utils_1.assert)(this.runtime.clientId !== undefined, 0x475 /* this.runtime.clientId should be defined */);
686
+ for (const clientQueue of this.taskQueues.values()) {
687
+ const clientIdIndex = clientQueue.indexOf(placeholderClientId);
688
+ if (clientIdIndex !== -1) {
689
+ clientQueue[clientIdIndex] = this.runtime.clientId;
690
+ }
691
+ }
692
+ }
693
+ // This seems like it should be unnecessary if we can trust to receive the join/leave messages and
694
+ // also have an accurate snapshot.
695
+ scrubClientsNotInQuorum() {
696
+ const quorum = this.runtime.getQuorum();
697
+ for (const [taskId, clientQueue] of this.taskQueues) {
698
+ const filteredClientQueue = clientQueue.filter((clientId) => quorum.getMember(clientId) !== undefined);
699
+ if (clientQueue.length !== filteredClientQueue.length) {
700
+ if (filteredClientQueue.length === 0) {
701
+ this.taskQueues.delete(taskId);
702
+ }
703
+ else {
704
+ this.taskQueues.set(taskId, filteredClientQueue);
705
+ }
706
+ this.queueWatcher.emit("queueChange", taskId);
707
+ }
708
+ }
709
+ }
710
+ applyStashedOp() {
711
+ throw new Error("not implemented");
712
+ }
713
+ }
714
+ exports.TaskManager = TaskManager;
715
+ //# sourceMappingURL=taskManager.js.map