@fluidframework/task-manager 2.60.0 → 2.61.0-355516

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.
@@ -3,10 +3,19 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
  import { EventEmitter } from "@fluid-internal/client-utils";
6
- import { assert, unreachableCase } from "@fluidframework/core-utils/internal";
6
+ import { AttachState, } from "@fluidframework/container-definitions/internal";
7
+ import { assert } from "@fluidframework/core-utils/internal";
7
8
  import { MessageType } from "@fluidframework/driver-definitions/internal";
8
9
  import { readAndParse } from "@fluidframework/driver-utils/internal";
9
10
  import { SharedObject, createSingleBlobSummary, } from "@fluidframework/shared-object-base/internal";
11
+ function assertIsTaskManagerOperation(op) {
12
+ assert(typeof op === "object" &&
13
+ op !== null &&
14
+ "taskId" in op &&
15
+ typeof op.taskId === "string" &&
16
+ "type" in op &&
17
+ (op.type === "volunteer" || op.type === "abandon" || op.type === "complete"), "Not a TaskManager operation");
18
+ }
10
19
  const snapshotFileName = "header";
11
20
  /**
12
21
  * Placeholder clientId for detached scenarios.
@@ -55,7 +64,9 @@ export class TaskManagerClass extends SharedObject {
55
64
  this.connectionWatcher = new EventEmitter();
56
65
  // completedWatcher emits an event whenever the local client receives a completed op.
57
66
  this.completedWatcher = new EventEmitter();
58
- this.messageId = -1;
67
+ // rollbackWatcher emits an event whenever a pending op is rolled back.
68
+ this.rollbackWatcher = new EventEmitter();
69
+ this.nextPendingMessageId = 0;
59
70
  /**
60
71
  * Tracks the most recent pending op for a given task
61
72
  */
@@ -64,60 +75,47 @@ export class TaskManagerClass extends SharedObject {
64
75
  * Tracks tasks that are this client is currently subscribed to.
65
76
  */
66
77
  this.subscribedTasks = new Set();
67
- /**
68
- * Map to track tasks that have pending complete ops.
69
- */
70
- this.pendingCompletedTasks = new Map();
71
78
  this.opWatcher.on("volunteer", (taskId, clientId, local, messageId) => {
72
- // We're tracking local ops from this connection. Filter out local ops during "connecting"
73
- // state since these were sent on the prior connection and were already cleared from the latestPendingOps.
74
- if (runtime.connected && local) {
75
- const pendingOp = this.latestPendingOps.get(taskId);
76
- assert(pendingOp !== undefined, 0x07b /* "Unexpected op" */);
77
- // Need to check the id, since it's possible to volunteer and abandon multiple times before the acks
78
- if (messageId === pendingOp.messageId) {
79
- assert(pendingOp.type === "volunteer", 0x07c /* "Unexpected op type" */);
80
- // Delete the pending, because we no longer have an outstanding op
79
+ if (local) {
80
+ const latestPendingOps = this.latestPendingOps.get(taskId);
81
+ assert(latestPendingOps !== undefined, "No pending ops for task");
82
+ const pendingOp = latestPendingOps.shift();
83
+ assert(pendingOp !== undefined && pendingOp.messageId === messageId, "Unexpected op");
84
+ assert(pendingOp.type === "volunteer", 0x07c /* "Unexpected op type" */);
85
+ if (latestPendingOps.length === 0) {
81
86
  this.latestPendingOps.delete(taskId);
82
87
  }
83
88
  }
84
89
  this.addClientToQueue(taskId, clientId);
85
90
  });
86
91
  this.opWatcher.on("abandon", (taskId, clientId, local, messageId) => {
87
- if (runtime.connected && local) {
88
- const pendingOp = this.latestPendingOps.get(taskId);
89
- assert(pendingOp !== undefined, 0x07d /* "Unexpected op" */);
90
- // Need to check the id, since it's possible to abandon and volunteer multiple times before the acks
91
- if (messageId === pendingOp.messageId) {
92
- assert(pendingOp.type === "abandon", 0x07e /* "Unexpected op type" */);
93
- // Delete the pending, because we no longer have an outstanding op
92
+ if (local) {
93
+ const latestPendingOps = this.latestPendingOps.get(taskId);
94
+ assert(latestPendingOps !== undefined, "No pending ops for task");
95
+ const pendingOp = latestPendingOps.shift();
96
+ assert(pendingOp !== undefined && pendingOp.messageId === messageId, "Unexpected op");
97
+ assert(pendingOp.type === "abandon", 0x07e /* "Unexpected op type" */);
98
+ if (latestPendingOps.length === 0) {
94
99
  this.latestPendingOps.delete(taskId);
95
100
  }
101
+ this.abandonWatcher.emit("abandon", taskId, messageId);
96
102
  }
97
103
  this.removeClientFromQueue(taskId, clientId);
98
104
  });
99
105
  this.opWatcher.on("complete", (taskId, clientId, local, messageId) => {
100
- if (runtime.connected && local) {
101
- const pendingOp = this.latestPendingOps.get(taskId);
102
- assert(pendingOp !== undefined, 0x400 /* Unexpected op */);
103
- // Need to check the id, since it's possible to complete multiple times before the acks
104
- if (messageId === pendingOp.messageId) {
105
- assert(pendingOp.type === "complete", 0x401 /* Unexpected op type */);
106
- // Delete the pending, because we no longer have an outstanding op
106
+ if (local) {
107
+ const latestPendingOps = this.latestPendingOps.get(taskId);
108
+ assert(latestPendingOps !== undefined, "No pending ops for task");
109
+ const pendingOp = latestPendingOps.shift();
110
+ assert(pendingOp !== undefined && pendingOp.messageId === messageId, "Unexpected op");
111
+ assert(pendingOp.type === "complete", 0x401 /* Unexpected op type */);
112
+ if (latestPendingOps.length === 0) {
107
113
  this.latestPendingOps.delete(taskId);
108
114
  }
109
- // Remove complete op from this.pendingCompletedTasks
110
- const pendingIds = this.pendingCompletedTasks.get(taskId);
111
- assert(pendingIds !== undefined && pendingIds.length > 0, 0x402 /* pendingIds is empty */);
112
- const removed = pendingIds.shift();
113
- assert(removed === messageId, 0x403 /* Removed complete op id does not match */);
114
- }
115
- // For clients in queue, we need to remove them from the queue and raise the proper events.
116
- if (!local) {
117
- this.taskQueues.delete(taskId);
118
- this.completedWatcher.emit("completed", taskId);
119
- this.emit("completed", taskId);
120
115
  }
116
+ this.taskQueues.delete(taskId);
117
+ this.completedWatcher.emit("completed", taskId, messageId);
118
+ this.emit("completed", taskId);
121
119
  });
122
120
  runtime.getQuorum().on("removeMember", (clientId) => {
123
121
  this.removeClientFromAllQueues(clientId);
@@ -141,17 +139,15 @@ export class TaskManagerClass extends SharedObject {
141
139
  });
142
140
  this.connectionWatcher.on("disconnect", () => {
143
141
  assert(this.clientId !== undefined, 0x1d3 /* "Missing client id on disconnect" */);
144
- // We don't modify the taskQueues on disconnect (they still reflect the latest known consensus state).
145
- // After reconnect these will get cleaned up by observing the clientLeaves.
146
- // However we do need to recognize that we lost the lock if we had it. Calls to .queued() and
147
- // .assigned() are also connection-state-aware to be consistent.
142
+ // Emit "lost" for any tasks we were assigned to.
148
143
  for (const [taskId, clientQueue] of this.taskQueues.entries()) {
149
144
  if (this.isAttached() && clientQueue[0] === this.clientId) {
150
145
  this.emit("lost", taskId);
151
146
  }
152
147
  }
153
- // All of our outstanding ops will be for the old clientId even if they get ack'd
154
- this.latestPendingOps.clear();
148
+ // Remove this client from all queues to reflect the new state, since being disconnected automatically removes
149
+ // this client from all queues.
150
+ this.removeClientFromAllQueues(this.clientId);
155
151
  });
156
152
  }
157
153
  submitVolunteerOp(taskId) {
@@ -161,10 +157,16 @@ export class TaskManagerClass extends SharedObject {
161
157
  };
162
158
  const pendingOp = {
163
159
  type: "volunteer",
164
- messageId: ++this.messageId,
160
+ messageId: this.nextPendingMessageId++,
165
161
  };
166
162
  this.submitLocalMessage(op, pendingOp.messageId);
167
- this.latestPendingOps.set(taskId, pendingOp);
163
+ const latestPendingOps = this.latestPendingOps.get(taskId);
164
+ if (latestPendingOps === undefined) {
165
+ this.latestPendingOps.set(taskId, [pendingOp]);
166
+ }
167
+ else {
168
+ latestPendingOps.push(pendingOp);
169
+ }
168
170
  }
169
171
  submitAbandonOp(taskId) {
170
172
  const op = {
@@ -173,10 +175,16 @@ export class TaskManagerClass extends SharedObject {
173
175
  };
174
176
  const pendingOp = {
175
177
  type: "abandon",
176
- messageId: ++this.messageId,
178
+ messageId: this.nextPendingMessageId++,
177
179
  };
178
180
  this.submitLocalMessage(op, pendingOp.messageId);
179
- this.latestPendingOps.set(taskId, pendingOp);
181
+ const latestPendingOps = this.latestPendingOps.get(taskId);
182
+ if (latestPendingOps === undefined) {
183
+ this.latestPendingOps.set(taskId, [pendingOp]);
184
+ }
185
+ else {
186
+ latestPendingOps.push(pendingOp);
187
+ }
180
188
  }
181
189
  submitCompleteOp(taskId) {
182
190
  const op = {
@@ -185,23 +193,25 @@ export class TaskManagerClass extends SharedObject {
185
193
  };
186
194
  const pendingOp = {
187
195
  type: "complete",
188
- messageId: ++this.messageId,
196
+ messageId: this.nextPendingMessageId++,
189
197
  };
190
- if (this.pendingCompletedTasks.has(taskId)) {
191
- this.pendingCompletedTasks.get(taskId)?.push(pendingOp.messageId);
198
+ this.submitLocalMessage(op, pendingOp.messageId);
199
+ const latestPendingOps = this.latestPendingOps.get(taskId);
200
+ if (latestPendingOps === undefined) {
201
+ this.latestPendingOps.set(taskId, [pendingOp]);
192
202
  }
193
203
  else {
194
- this.pendingCompletedTasks.set(taskId, [pendingOp.messageId]);
204
+ latestPendingOps.push(pendingOp);
195
205
  }
196
- this.submitLocalMessage(op, pendingOp.messageId);
197
- this.latestPendingOps.set(taskId, pendingOp);
198
206
  }
199
207
  /**
200
208
  * {@inheritDoc ITaskManager.volunteerForTask}
201
209
  */
202
210
  async volunteerForTask(taskId) {
203
- // If we have the lock, resolve immediately
204
- if (this.assigned(taskId)) {
211
+ // If we are both queued and assigned, then we have the lock and do not
212
+ // have any pending abandon/complete ops. In this case we can resolve
213
+ // true immediately.
214
+ if (this.queuedOptimistically(taskId) && this.assigned(taskId)) {
205
215
  return true;
206
216
  }
207
217
  if (this.readOnlyInfo.readonly === true) {
@@ -210,7 +220,7 @@ export class TaskManagerClass extends SharedObject {
210
220
  : new Error("Attempted to volunteer in read-only state");
211
221
  throw error;
212
222
  }
213
- if (!this.isAttached()) {
223
+ if (this.isDetached()) {
214
224
  // Simulate auto-ack in detached scenario
215
225
  assert(this.clientId !== undefined, 0x472 /* clientId should not be undefined */);
216
226
  this.addClientToQueue(taskId, this.clientId);
@@ -221,6 +231,25 @@ export class TaskManagerClass extends SharedObject {
221
231
  }
222
232
  // This promise works even if we already have an outstanding volunteer op.
223
233
  const lockAcquireP = new Promise((resolve, reject) => {
234
+ // If we don't send an op (meaning the latest pending op is "volunteer"), nextPendingMessageId
235
+ // will be greater than that prior "volunteer" op's messageId. This is OK because
236
+ // we only use it to filter stale abandon/complete, and not when determining if we
237
+ // acquired the lock.
238
+ const nextPendingMessageId = this.nextPendingMessageId;
239
+ const setupListeners = () => {
240
+ this.queueWatcher.on("queueChange", checkIfAcquiredLock);
241
+ this.abandonWatcher.on("abandon", checkIfAbandoned);
242
+ this.connectionWatcher.on("disconnect", rejectOnDisconnect);
243
+ this.completedWatcher.on("completed", checkIfCompleted);
244
+ this.rollbackWatcher.on("rollback", checkIfRolledBack);
245
+ };
246
+ const removeListeners = () => {
247
+ this.queueWatcher.off("queueChange", checkIfAcquiredLock);
248
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
249
+ this.connectionWatcher.off("disconnect", rejectOnDisconnect);
250
+ this.completedWatcher.off("completed", checkIfCompleted);
251
+ this.rollbackWatcher.off("rollback", checkIfRolledBack);
252
+ };
224
253
  const checkIfAcquiredLock = (eventTaskId) => {
225
254
  if (eventTaskId !== taskId) {
226
255
  return;
@@ -228,47 +257,48 @@ export class TaskManagerClass extends SharedObject {
228
257
  // Also check pending ops here because it's possible we are currently in the queue from a previous
229
258
  // lock attempt, but have an outstanding abandon AND the outstanding volunteer for this lock attempt.
230
259
  // If we reach the head of the queue based on the previous lock attempt, we don't want to resolve.
231
- if (this.assigned(taskId) && !this.latestPendingOps.has(taskId)) {
232
- this.queueWatcher.off("queueChange", checkIfAcquiredLock);
233
- this.abandonWatcher.off("abandon", checkIfAbandoned);
234
- this.connectionWatcher.off("disconnect", rejectOnDisconnect);
235
- this.completedWatcher.off("completed", checkIfCompleted);
260
+ if (this.assigned(taskId)) {
261
+ removeListeners();
236
262
  resolve(true);
237
263
  }
238
264
  };
239
- const checkIfAbandoned = (eventTaskId) => {
265
+ const checkIfAbandoned = (eventTaskId, messageId) => {
240
266
  if (eventTaskId !== taskId) {
241
267
  return;
242
268
  }
243
- this.queueWatcher.off("queueChange", checkIfAcquiredLock);
244
- this.abandonWatcher.off("abandon", checkIfAbandoned);
245
- this.connectionWatcher.off("disconnect", rejectOnDisconnect);
246
- this.completedWatcher.off("completed", checkIfCompleted);
269
+ if (messageId !== undefined && messageId <= nextPendingMessageId) {
270
+ // Ignore abandon events that were for abandon ops that were sent prior to our current volunteer attempt.
271
+ return;
272
+ }
273
+ removeListeners();
247
274
  reject(new Error("Abandoned before acquiring task assignment"));
248
275
  };
249
276
  const rejectOnDisconnect = () => {
250
- this.queueWatcher.off("queueChange", checkIfAcquiredLock);
251
- this.abandonWatcher.off("abandon", checkIfAbandoned);
252
- this.connectionWatcher.off("disconnect", rejectOnDisconnect);
253
- this.completedWatcher.off("completed", checkIfCompleted);
277
+ removeListeners();
254
278
  reject(new Error("Disconnected before acquiring task assignment"));
255
279
  };
256
- const checkIfCompleted = (eventTaskId) => {
280
+ const checkIfCompleted = (eventTaskId, messageId) => {
257
281
  if (eventTaskId !== taskId) {
258
282
  return;
259
283
  }
260
- this.queueWatcher.off("queueChange", checkIfAcquiredLock);
261
- this.abandonWatcher.off("abandon", checkIfAbandoned);
262
- this.connectionWatcher.off("disconnect", rejectOnDisconnect);
263
- this.completedWatcher.off("completed", checkIfCompleted);
284
+ if (messageId !== undefined && messageId <= nextPendingMessageId) {
285
+ // Ignore abandon events that were for abandon ops that were sent prior to our current volunteer attempt.
286
+ return;
287
+ }
288
+ removeListeners();
264
289
  resolve(false);
265
290
  };
266
- this.queueWatcher.on("queueChange", checkIfAcquiredLock);
267
- this.abandonWatcher.on("abandon", checkIfAbandoned);
268
- this.connectionWatcher.on("disconnect", rejectOnDisconnect);
269
- this.completedWatcher.on("completed", checkIfCompleted);
291
+ const checkIfRolledBack = (eventTaskId) => {
292
+ if (eventTaskId !== taskId) {
293
+ return;
294
+ }
295
+ removeListeners();
296
+ resolve(false);
297
+ };
298
+ setupListeners();
270
299
  });
271
- if (!this.queued(taskId)) {
300
+ if (!this.queuedOptimistically(taskId)) {
301
+ // Only send the volunteer op if we are not already queued.
272
302
  this.submitVolunteerOp(taskId);
273
303
  }
274
304
  return lockAcquireP;
@@ -283,37 +313,69 @@ export class TaskManagerClass extends SharedObject {
283
313
  if (this.readOnlyInfo.readonly === true && this.readOnlyInfo.permissions === true) {
284
314
  throw new Error("Attempted to subscribe with read-only permissions");
285
315
  }
316
+ let volunteerOpMessageId;
286
317
  const submitVolunteerOp = () => {
318
+ volunteerOpMessageId = this.nextPendingMessageId;
287
319
  this.submitVolunteerOp(taskId);
288
320
  };
321
+ const setupListeners = () => {
322
+ this.abandonWatcher.on("abandon", checkIfAbandoned);
323
+ this.connectionWatcher.on("disconnect", disconnectHandler);
324
+ this.completedWatcher.on("completed", checkIfCompleted);
325
+ this.rollbackWatcher.on("rollback", checkIfRolledBack);
326
+ };
327
+ const removeListeners = () => {
328
+ this.abandonWatcher.off("abandon", checkIfAbandoned);
329
+ this.connectionWatcher.off("disconnect", disconnectHandler);
330
+ this.connectionWatcher.off("connect", submitVolunteerOp);
331
+ this.completedWatcher.off("completed", checkIfCompleted);
332
+ this.rollbackWatcher.off("rollback", checkIfRolledBack);
333
+ };
289
334
  const disconnectHandler = () => {
290
335
  // Wait to be connected again and then re-submit volunteer op
291
336
  this.connectionWatcher.once("connect", submitVolunteerOp);
292
337
  };
293
- const checkIfAbandoned = (eventTaskId) => {
338
+ const checkIfAbandoned = (eventTaskId, messageId) => {
294
339
  if (eventTaskId !== taskId) {
295
340
  return;
296
341
  }
297
- this.abandonWatcher.off("abandon", checkIfAbandoned);
298
- this.connectionWatcher.off("disconnect", disconnectHandler);
299
- this.connectionWatcher.off("connect", submitVolunteerOp);
300
- this.completedWatcher.off("completed", checkIfCompleted);
342
+ // abandonWatcher emits twice for a local abandon() call. When initially called it
343
+ // will emit with undefined messageId. It will emit a second time when the op is
344
+ // ack'd and processed, this time with the messageId for the ack.
345
+ // This condition accounts ensures we don't ignore the initial abandon() emit and
346
+ // only ignore emits associated with ack'd abandon ops that were sent prior to the
347
+ // current volunteer attempt.
348
+ if (messageId !== undefined &&
349
+ volunteerOpMessageId !== undefined &&
350
+ messageId <= volunteerOpMessageId) {
351
+ // Ignore abandon events that were for abandon ops that were sent prior to our current volunteer attempt.
352
+ return;
353
+ }
354
+ removeListeners();
301
355
  this.subscribedTasks.delete(taskId);
302
356
  };
303
- const checkIfCompleted = (eventTaskId) => {
357
+ const checkIfCompleted = (eventTaskId, messageId) => {
304
358
  if (eventTaskId !== taskId) {
305
359
  return;
306
360
  }
307
- this.abandonWatcher.off("abandon", checkIfAbandoned);
308
- this.connectionWatcher.off("disconnect", disconnectHandler);
309
- this.connectionWatcher.off("connect", submitVolunteerOp);
310
- this.completedWatcher.off("completed", checkIfCompleted);
361
+ if (messageId !== undefined &&
362
+ volunteerOpMessageId !== undefined &&
363
+ messageId <= volunteerOpMessageId) {
364
+ // Ignore abandon events that were for abandon ops that were sent prior to our current volunteer attempt.
365
+ return;
366
+ }
367
+ removeListeners();
368
+ this.subscribedTasks.delete(taskId);
369
+ };
370
+ const checkIfRolledBack = (eventTaskId) => {
371
+ if (eventTaskId !== taskId) {
372
+ return;
373
+ }
374
+ removeListeners();
311
375
  this.subscribedTasks.delete(taskId);
312
376
  };
313
- this.abandonWatcher.on("abandon", checkIfAbandoned);
314
- this.connectionWatcher.on("disconnect", disconnectHandler);
315
- this.completedWatcher.on("completed", checkIfCompleted);
316
- if (!this.isAttached()) {
377
+ setupListeners();
378
+ if (this.isDetached()) {
317
379
  // Simulate auto-ack in detached scenario
318
380
  assert(this.clientId !== undefined, 0x473 /* clientId should not be undefined */);
319
381
  this.addClientToQueue(taskId, this.clientId);
@@ -321,18 +383,13 @@ export class TaskManagerClass extends SharedObject {
321
383
  // a real clientId. At that point we should re-enter the queue with a real volunteer op (assuming we are
322
384
  // connected).
323
385
  this.runtime.once("attached", () => {
324
- if (this.queued(taskId)) {
325
- // If we are already queued, then we were able to replace the placeholderClientId with our real
326
- // clientId and no action is required.
327
- return;
328
- }
329
- else if (this.connected) {
386
+ // We call scrubClientsNotInQuorum() in case our clientId changed during the attach process.
387
+ this.scrubClientsNotInQuorum();
388
+ if (this.connected) {
330
389
  submitVolunteerOp();
331
390
  }
332
391
  else {
333
- this.connectionWatcher.once("connect", () => {
334
- submitVolunteerOp();
335
- });
392
+ this.connectionWatcher.once("connect", submitVolunteerOp);
336
393
  }
337
394
  });
338
395
  }
@@ -340,7 +397,8 @@ export class TaskManagerClass extends SharedObject {
340
397
  // If we are disconnected (and attached), wait to be connected and submit volunteer op
341
398
  disconnectHandler();
342
399
  }
343
- else if (!this.assigned(taskId) && !this.queued(taskId)) {
400
+ else if (!this.queuedOptimistically(taskId)) {
401
+ // We don't need to send a second volunteer op if we just sent one.
344
402
  submitVolunteerOp();
345
403
  }
346
404
  this.subscribedTasks.add(taskId);
@@ -350,23 +408,19 @@ export class TaskManagerClass extends SharedObject {
350
408
  */
351
409
  abandon(taskId) {
352
410
  // Always allow abandon if the client is subscribed to allow clients to unsubscribe while disconnected.
353
- // Otherwise, we should check to make sure the client is both connected queued for the task before sending an
354
- // abandon op.
355
- if (!this.subscribed(taskId) && !this.queued(taskId)) {
411
+ // Otherwise, we should check to make sure the client is optimistically queued for the task before trying to abandon.
412
+ if (!this.queuedOptimistically(taskId) && !this.subscribed(taskId)) {
356
413
  // Nothing to do
357
414
  return;
358
415
  }
359
- if (!this.isAttached()) {
416
+ if (this.isDetached()) {
360
417
  // Simulate auto-ack in detached scenario
361
418
  assert(this.clientId !== undefined, 0x474 /* clientId is undefined */);
362
419
  this.removeClientFromQueue(taskId, this.clientId);
363
420
  this.abandonWatcher.emit("abandon", taskId);
364
421
  return;
365
422
  }
366
- // If we're subscribed but not queued, we don't need to submit an abandon op (probably offline)
367
- if (this.queued(taskId)) {
368
- this.submitAbandonOp(taskId);
369
- }
423
+ this.submitAbandonOp(taskId);
370
424
  this.abandonWatcher.emit("abandon", taskId);
371
425
  }
372
426
  /**
@@ -377,9 +431,7 @@ export class TaskManagerClass extends SharedObject {
377
431
  return false;
378
432
  }
379
433
  const currentAssignee = this.taskQueues.get(taskId)?.[0];
380
- return (currentAssignee !== undefined &&
381
- currentAssignee === this.clientId &&
382
- !this.latestPendingOps.has(taskId));
434
+ return currentAssignee !== undefined && currentAssignee === this.clientId;
383
435
  }
384
436
  /**
385
437
  * {@inheritDoc ITaskManager.queued}
@@ -389,11 +441,7 @@ export class TaskManagerClass extends SharedObject {
389
441
  return false;
390
442
  }
391
443
  assert(this.clientId !== undefined, 0x07f /* "clientId undefined" */);
392
- const clientQueue = this.taskQueues.get(taskId);
393
- // If we have no queue for the taskId, then no one has signed up for it.
394
- return (((clientQueue?.includes(this.clientId) ?? false) &&
395
- !this.latestPendingOps.has(taskId)) ||
396
- this.latestPendingOps.get(taskId)?.type === "volunteer");
444
+ return this.taskQueues.get(taskId)?.includes(this.clientId) ?? false;
397
445
  }
398
446
  /**
399
447
  * {@inheritDoc ITaskManager.subscribed}
@@ -410,15 +458,16 @@ export class TaskManagerClass extends SharedObject {
410
458
  }
411
459
  // If we are detached we will simulate auto-ack for the complete op. Therefore we only need to send the op if
412
460
  // we are attached. Additionally, we don't need to check if we are connected while detached.
413
- if (this.isAttached()) {
414
- if (!this.connected) {
415
- throw new Error("Attempted to complete task in disconnected state");
416
- }
417
- this.submitCompleteOp(taskId);
461
+ if (this.isDetached()) {
462
+ this.taskQueues.delete(taskId);
463
+ this.completedWatcher.emit("completed", taskId);
464
+ this.emit("completed", taskId);
465
+ return;
466
+ }
467
+ if (!this.connected) {
468
+ throw new Error("Attempted to complete task in disconnected state");
418
469
  }
419
- this.taskQueues.delete(taskId);
420
- this.completedWatcher.emit("completed", taskId);
421
- this.emit("completed", taskId);
470
+ this.submitCompleteOp(taskId);
422
471
  }
423
472
  /**
424
473
  * {@inheritDoc ITaskManager.canVolunteer}
@@ -481,12 +530,22 @@ export class TaskManagerClass extends SharedObject {
481
530
  onConnect() {
482
531
  this.connectionWatcher.emit("connect");
483
532
  }
484
- //
485
533
  /**
486
534
  * Override resubmit core to avoid resubmission on reconnect. On disconnect we accept our removal from the
487
535
  * queues, and leave it up to the user to decide whether they want to attempt to re-enter a queue on reconnect.
536
+ * However, we do need to update latestPendingOps to account for the ops we will no longer be processing.
488
537
  */
489
- reSubmitCore() { }
538
+ reSubmitCore(content, localOpMetadata) {
539
+ assertIsTaskManagerOperation(content);
540
+ const pendingOps = this.latestPendingOps.get(content.taskId);
541
+ assert(pendingOps !== undefined, "No pending ops for task on resubmit attempt");
542
+ const pendingOpIndex = pendingOps.findIndex((op) => op.messageId === localOpMetadata && op.type === content.type);
543
+ assert(pendingOpIndex !== -1, "Could not match pending op on resubmit attempt");
544
+ pendingOps.splice(pendingOpIndex, 1);
545
+ if (pendingOps.length === 0) {
546
+ this.latestPendingOps.delete(content.taskId);
547
+ }
548
+ }
490
549
  /**
491
550
  * Process a task manager operation
492
551
  *
@@ -520,11 +579,6 @@ export class TaskManagerClass extends SharedObject {
520
579
  }
521
580
  }
522
581
  addClientToQueue(taskId, clientId) {
523
- const pendingIds = this.pendingCompletedTasks.get(taskId);
524
- if (pendingIds !== undefined && pendingIds.length > 0) {
525
- // Ignore the volunteer op if we know this task is about to be completed
526
- return;
527
- }
528
582
  // Ensure that the clientId exists in the quorum, or it is placeholderClientId (detached scenario)
529
583
  if (this.runtime.getQuorum().getMembers().has(clientId) ||
530
584
  this.clientId === placeholderClientId) {
@@ -534,6 +588,12 @@ export class TaskManagerClass extends SharedObject {
534
588
  clientQueue = [];
535
589
  this.taskQueues.set(taskId, clientQueue);
536
590
  }
591
+ if (clientQueue.includes(clientId)) {
592
+ // We shouldn't re-add the client if it's already in the queue.
593
+ // This may be possible in scenarios where a client was added in
594
+ // while detached.
595
+ return;
596
+ }
537
597
  const oldLockHolder = clientQueue[0];
538
598
  clientQueue.push(clientId);
539
599
  const newLockHolder = clientQueue[0];
@@ -575,7 +635,13 @@ export class TaskManagerClass extends SharedObject {
575
635
  for (const clientQueue of this.taskQueues.values()) {
576
636
  const clientIdIndex = clientQueue.indexOf(placeholderClientId);
577
637
  if (clientIdIndex !== -1) {
578
- clientQueue[clientIdIndex] = this.runtime.clientId;
638
+ if (clientQueue.includes(this.runtime.clientId)) {
639
+ // If the real clientId is already in the queue, just remove the placeholder.
640
+ clientQueue.splice(clientIdIndex, 1);
641
+ }
642
+ else {
643
+ clientQueue[clientIdIndex] = this.runtime.clientId;
644
+ }
579
645
  }
580
646
  }
581
647
  }
@@ -596,25 +662,55 @@ export class TaskManagerClass extends SharedObject {
596
662
  }
597
663
  }
598
664
  }
599
- applyStashedOp(content) {
600
- const taskOp = content;
601
- switch (taskOp.type) {
602
- case "abandon": {
603
- this.abandon(taskOp.taskId);
604
- break;
605
- }
606
- case "complete": {
607
- this.complete(taskOp.taskId);
608
- break;
609
- }
610
- case "volunteer": {
611
- this.subscribeToTask(taskOp.taskId);
612
- break;
613
- }
614
- default: {
615
- unreachableCase(taskOp);
616
- }
665
+ /**
666
+ * Checks whether this client is currently assigned or in queue to become assigned, while also accounting
667
+ * for the latest pending ops.
668
+ */
669
+ queuedOptimistically(taskId) {
670
+ if (this.isAttached() && !this.connected) {
671
+ return false;
617
672
  }
673
+ assert(this.clientId !== undefined, 0x07f /* "clientId undefined" */);
674
+ const inQueue = this.taskQueues.get(taskId)?.includes(this.clientId) ?? false;
675
+ const latestPendingOps = this.latestPendingOps.get(taskId);
676
+ const latestPendingOp = latestPendingOps !== undefined && latestPendingOps.length > 0
677
+ ? latestPendingOps[latestPendingOps.length - 1]
678
+ : undefined;
679
+ const isPendingVolunteer = latestPendingOp?.type === "volunteer";
680
+ const isPendingAbandonOrComplete = latestPendingOp?.type === "abandon" || latestPendingOp?.type === "complete";
681
+ // We return true if the client is either in queue already or the latest pending op for this task is a volunteer op.
682
+ // But we should always return false if the latest pending op is an abandon or complete op.
683
+ return (inQueue && !isPendingAbandonOrComplete) || isPendingVolunteer;
684
+ }
685
+ /**
686
+ * Returns true if the client is detached.
687
+ * This is distinct from !this.isAttached() because `isAttached()` also checks if `this._isBoundToContext`
688
+ * is true. We use `isDetached()` to determine if we should simulate auto-ack behavior for ops, which is
689
+ * mainly concerned with if we have been assigned a real clientId yet.
690
+ */
691
+ isDetached() {
692
+ return this.runtime.attachState === AttachState.Detached;
693
+ }
694
+ applyStashedOp(content) {
695
+ // We don't apply any stashed ops since during the rehydration process. Since we lose any assigned tasks
696
+ // during rehydration we cannot be assigned any tasks. Additionally, without the in-memory state of the
697
+ // previous dds, we also cannot re-volunteer based on a previous subscribeToTask() call. Since we are
698
+ // unable to be assigned to any tasks, there is no reason to process abandon/complete ops either.
699
+ }
700
+ /**
701
+ * {@inheritDoc @fluidframework/shared-object-base#SharedObject.rollback}
702
+ */
703
+ rollback(content, localOpMetadata) {
704
+ assert(typeof localOpMetadata === "number", "Expect localOpMetadata to be a number");
705
+ assertIsTaskManagerOperation(content);
706
+ const latestPendingOps = this.latestPendingOps.get(content.taskId);
707
+ assert(latestPendingOps !== undefined, "No pending ops when trying to rollback");
708
+ const pendingOpToRollback = latestPendingOps.pop();
709
+ assert(pendingOpToRollback !== undefined && pendingOpToRollback.messageId === localOpMetadata, "pending op mismatch");
710
+ if (latestPendingOps.length === 0) {
711
+ this.latestPendingOps.delete(content.taskId);
712
+ }
713
+ this.rollbackWatcher.emit("rollback", content.taskId);
618
714
  }
619
715
  }
620
716
  //# sourceMappingURL=taskManager.js.map