refactor: add `isWorkerNodeStealing()` helper
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 31 Aug 2024 11:51:09 +0000 (13:51 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 31 Aug 2024 11:51:09 +0000 (13:51 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts

index 098ed23391c8a101953459e4d458df0b289753c2..1a2c8bf3125687b1e7bb834ce1bc2a067936c66c 100644 (file)
@@ -641,15 +641,12 @@ export abstract class AbstractPool<
       const localWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(
         message.workerId
       )
-      const workerInfo = this.getWorkerInfo(localWorkerNodeKey)
       // Kill message received from worker
       if (
         isKillBehavior(KillBehaviors.HARD, message.kill) ||
         (isKillBehavior(KillBehaviors.SOFT, message.kill) &&
           this.isWorkerNodeIdle(localWorkerNodeKey) &&
-          workerInfo != null &&
-          !workerInfo.continuousStealing &&
-          !workerInfo.backPressureStealing)
+          !this.isWorkerNodeStealing(localWorkerNodeKey))
       ) {
         // Flag the worker node as not ready immediately
         this.flagWorkerNodeAsNotReady(localWorkerNodeKey)
@@ -1363,6 +1360,15 @@ export abstract class AbstractPool<
     return workerNode.info.ready && workerNode.usage.tasks.executing === 0
   }
 
+  private isWorkerNodeStealing (workerNodeKey: number): boolean {
+    const workerNode = this.workerNodes[workerNodeKey]
+    return (
+      workerNode.info.ready &&
+      (workerNode.info.continuousStealing ||
+        workerNode.info.backPressureStealing)
+    )
+  }
+
   private redistributeQueuedTasks (sourceWorkerNodeKey: number): void {
     if (sourceWorkerNodeKey === -1 || this.cannotStealTask()) {
       return
@@ -2078,9 +2084,8 @@ export abstract class AbstractPool<
           0
         ),
         stealingWorkerNodes: this.workerNodes.reduce(
-          (accumulator, workerNode) =>
-            workerNode.info.continuousStealing ||
-            workerNode.info.backPressureStealing
+          (accumulator, _, workerNodeKey) =>
+            this.isWorkerNodeStealing(workerNodeKey)
               ? accumulator + 1
               : accumulator,
           0