refactor: use builtin retry mechanism in worker choice strategies
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 20 Oct 2023 04:18:30 +0000 (06:18 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 20 Oct 2023 04:18:30 +0000 (06:18 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts

index 86692de92f50c6cc9b1c1c7bcb3e1e960d5693e5..c364933358528ef3ade54a3cc3dd474cf2c2d9d3 100644 (file)
@@ -126,6 +126,15 @@ export abstract class AbstractWorkerChoiceStrategy<
     return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false
   }
 
+  /**
+   * Check the next worker node readiness.
+   */
+  protected checkNextWorkerNodeReadiness (): void {
+    if (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number)) {
+      delete this.nextWorkerNodeKey
+    }
+  }
+
   /**
    * Gets the worker node task runtime.
    * If the task statistics require the average runtime, the average runtime is returned.
index 15a6c7f48eaac7f0a81e5a30bfd74762a426435d..eee39ec8d646da13324c7050e86afc33882fa22c 100644 (file)
@@ -122,20 +122,18 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
   }
 
   private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
-    do {
-      if (
-        this.roundId === this.roundWeights.length - 1 &&
-        this.workerNodeId === this.pool.workerNodes.length - 1
-      ) {
-        this.roundId = 0
-        this.workerNodeId = 0
-      } else if (this.workerNodeId === this.pool.workerNodes.length - 1) {
-        this.roundId = this.roundId + 1
-        this.workerNodeId = 0
-      } else {
-        this.workerNodeId = this.workerNodeId + 1
-      }
-    } while (!this.isWorkerNodeReady(this.workerNodeId))
+    if (
+      this.roundId === this.roundWeights.length - 1 &&
+      this.workerNodeId === this.pool.workerNodes.length - 1
+    ) {
+      this.roundId = 0
+      this.workerNodeId = 0
+    } else if (this.workerNodeId === this.pool.workerNodes.length - 1) {
+      this.roundId = this.roundId + 1
+      this.workerNodeId = 0
+    } else {
+      this.workerNodeId = this.workerNodeId + 1
+    }
   }
 
   /** @inheritDoc */
index 19dd3872c116723756fb146a4a57506b10523ab0..7c49cec75689a8ca64aa101b51ec8f86890f1409 100644 (file)
@@ -46,6 +46,7 @@ export class RoundRobinWorkerChoiceStrategy<
     const chosenWorkerNodeKey = this.nextWorkerNodeKey
     this.setPreviousWorkerNodeKey(chosenWorkerNodeKey)
     this.roundRobinNextWorkerNodeKey()
+    this.checkNextWorkerNodeReadiness()
     return chosenWorkerNodeKey
   }
 
@@ -70,12 +71,10 @@ export class RoundRobinWorkerChoiceStrategy<
   }
 
   private roundRobinNextWorkerNodeKey (): number | undefined {
-    do {
-      this.nextWorkerNodeKey =
-        this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
-          ? 0
-          : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
-    } while (!this.isWorkerNodeReady(this.nextWorkerNodeKey))
+    this.nextWorkerNodeKey =
+      this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
+        ? 0
+        : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
     return this.nextWorkerNodeKey
   }
 }
index 22bf458febd3734a7f147c27172b486acdde5169..5148494ed93f478862504515175708ecdd633f04 100644 (file)
@@ -71,7 +71,9 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   /** @inheritDoc */
   public choose (): number | undefined {
     this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
-    return this.weightedRoundRobinNextWorkerNodeKey()
+    this.weightedRoundRobinNextWorkerNodeKey()
+    this.checkNextWorkerNodeReadiness()
+    return this.nextWorkerNodeKey
   }
 
   /** @inheritDoc */
@@ -95,25 +97,23 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   }
 
   private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
-    do {
-      const workerWeight =
-        this.opts.weights?.[
+    const workerWeight =
+      this.opts.weights?.[
+        this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+      ] ?? this.defaultWorkerWeight
+    if (this.workerNodeVirtualTaskRunTime < workerWeight) {
+      this.workerNodeVirtualTaskRunTime =
+        this.workerNodeVirtualTaskRunTime +
+        this.getWorkerNodeTaskRunTime(
           this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
-        ] ?? this.defaultWorkerWeight
-      if (this.workerNodeVirtualTaskRunTime < workerWeight) {
-        this.workerNodeVirtualTaskRunTime =
-          this.workerNodeVirtualTaskRunTime +
-          this.getWorkerNodeTaskRunTime(
-            this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
-          )
-      } else {
-        this.nextWorkerNodeKey =
-          this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
-            ? 0
-            : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
-        this.workerNodeVirtualTaskRunTime = 0
-      }
-    } while (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number))
+        )
+    } else {
+      this.nextWorkerNodeKey =
+        this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
+          ? 0
+          : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
+      this.workerNodeVirtualTaskRunTime = 0
+    }
     return this.nextWorkerNodeKey
   }
 }