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