fix: some more edges cases in the worker choice strategy retries code
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 19 Aug 2023 19:04:40 +0000 (21:04 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 19 Aug 2023 19:04:40 +0000 (21:04 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
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 4ed7dcae2b0dea882d1eb15a1cb0f88310435e7f..d3e7e385192f23e502a6fb5ce9afb1a414b07ddf 100644 (file)
@@ -30,6 +30,11 @@ export abstract class AbstractWorkerChoiceStrategy<
    */
   protected nextWorkerNodeKey: number | undefined = 0
 
+  /**
+   * The previous worker node key.
+   */
+  protected previousWorkerNodeKey: number = 0
+
   /** @inheritDoc */
   public readonly strategyPolicy: StrategyPolicy = {
     dynamicWorkerUsage: false,
index cdbfbee5bb0b73e6436f5aad3d94b45e764d3713..e5370428409c337be5b81772eacf5f80a03576f1 100644 (file)
@@ -67,15 +67,17 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
-    let roundId: number | undefined
+    let roundId: number = this.roundId
     let workerNodeId: number | undefined
     for (
       let roundIndex = this.roundId;
       roundIndex < this.roundWeights.length;
       roundIndex++
     ) {
+      roundId = roundIndex
       for (
-        let workerNodeKey = this.nextWorkerNodeKey ?? 0;
+        let workerNodeKey =
+          this.nextWorkerNodeKey ?? this.previousWorkerNodeKey;
         workerNodeKey < this.pool.workerNodes.length;
         workerNodeKey++
       ) {
@@ -85,13 +87,16 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
           this.isWorkerNodeEligible(workerNodeKey) &&
           workerWeight >= this.roundWeights[roundIndex]
         ) {
-          roundId = roundIndex
           workerNodeId = workerNodeKey
           break
         }
       }
     }
-    this.roundId = roundId as number
+    this.roundId = roundId
+    if (workerNodeId == null) {
+      this.previousWorkerNodeKey =
+        this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+    }
     this.nextWorkerNodeKey = workerNodeId
     const chosenWorkerNodeKey = this.nextWorkerNodeKey
     if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) {
@@ -99,7 +104,8 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
       this.roundId =
         this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
     } else {
-      this.nextWorkerNodeKey = (this.nextWorkerNodeKey ?? 0) + 1
+      this.nextWorkerNodeKey =
+        (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
     }
     return chosenWorkerNodeKey
   }
index 41bc4085ebd5a1399975e48e873fc0612c6a611c..b6d222f59995c523770120d515571bdb8c129b54 100644 (file)
@@ -54,6 +54,8 @@ export class RoundRobinWorkerChoiceStrategy<
     this.roundRobinNextWorkerNodeKey()
     if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
       this.nextWorkerNodeKey = undefined
+      this.previousWorkerNodeKey =
+        chosenWorkerNodeKey ?? this.previousWorkerNodeKey
     }
     return chosenWorkerNodeKey
   }
@@ -74,7 +76,7 @@ export class RoundRobinWorkerChoiceStrategy<
     this.nextWorkerNodeKey =
       this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
         ? 0
-        : (this.nextWorkerNodeKey ?? 0) + 1
+        : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
     return this.nextWorkerNodeKey
   }
 }
index 1aab3d1c6fdfd82ace0147aa27a6ce67be7b2cf8..701e51656366369abadb22edcadca5c3e03c90fe 100644 (file)
@@ -81,6 +81,8 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     this.weightedRoundRobinNextWorkerNodeKey()
     if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
       this.nextWorkerNodeKey = undefined
+      this.previousWorkerNodeKey =
+        chosenWorkerNodeKey ?? this.previousWorkerNodeKey
     }
     return chosenWorkerNodeKey
   }
@@ -101,17 +103,20 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
     const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
     const workerWeight =
-      this.opts.weights?.[this.nextWorkerNodeKey ?? 0] ??
-      this.defaultWorkerWeight
+      this.opts.weights?.[
+        this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+      ] ?? this.defaultWorkerWeight
     if (workerVirtualTaskRunTime < workerWeight) {
       this.workerVirtualTaskRunTime =
         workerVirtualTaskRunTime +
-        this.getWorkerTaskRunTime(this.nextWorkerNodeKey ?? 0)
+        this.getWorkerTaskRunTime(
+          this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+        )
     } else {
       this.nextWorkerNodeKey =
         this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
           ? 0
-          : (this.nextWorkerNodeKey ?? 0) + 1
+          : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
       this.workerVirtualTaskRunTime = 0
     }
     return this.nextWorkerNodeKey