refactor: ensure stolen tasks account is done safely
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 25 Aug 2023 15:42:56 +0000 (17:42 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 25 Aug 2023 15:42:56 +0000 (17:42 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts

index 4abbd615efe13869feaff7b9e1b19ee584b17990..3cc8b2204cefd18af104b670a30eaea2a39bad1a 100644 (file)
@@ -1238,6 +1238,17 @@ export abstract class AbstractPool<
     }
   }
 
+  // private incrementTasksStolen (workerNodeKey: number, workerNode: WorkerNode): void {
+  //   ++workerNode.usage.tasks.stolen
+  //   if (this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey)) {
+  //     const taskFunctionWorkerUsage =
+  //       workerNode.getTaskFunctionWorkerUsage(
+  //         task.name as string
+  //       ) as WorkerUsage
+  //     ++taskFunctionWorkerUsage.tasks.stolen
+  //   }
+  // }
+
   private taskStealingOnEmptyQueue (workerId: number): void {
     const destinationWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(workerId)
     const destinationWorkerNode = this.workerNodes[destinationWorkerNodeKey]
@@ -1269,8 +1280,15 @@ export abstract class AbstractPool<
         } else {
           this.enqueueTask(destinationWorkerNodeKey, task)
         }
-        ++destinationWorkerNode.usage.tasks.stolen
-        if (this.shallUpdateTaskFunctionWorkerUsage(destinationWorkerNodeKey)) {
+        if (destinationWorkerNode?.usage != null) {
+          ++destinationWorkerNode.usage.tasks.stolen
+        }
+        if (
+          this.shallUpdateTaskFunctionWorkerUsage(destinationWorkerNodeKey) &&
+          destinationWorkerNode.getTaskFunctionWorkerUsage(
+            task.name as string
+          ) != null
+        ) {
           const taskFunctionWorkerUsage =
             destinationWorkerNode.getTaskFunctionWorkerUsage(
               task.name as string
@@ -1312,8 +1330,13 @@ export abstract class AbstractPool<
         } else {
           this.enqueueTask(workerNodeKey, task)
         }
-        ++workerNode.usage.tasks.stolen
-        if (this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey)) {
+        if (workerNode?.usage != null) {
+          ++workerNode.usage.tasks.stolen
+        }
+        if (
+          this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) &&
+          workerNode.getTaskFunctionWorkerUsage(task.name as string) != null
+        ) {
           const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage(
             task.name as string
           ) as WorkerUsage