fix: do not pre-choose in WRR strategy
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 31 Aug 2023 12:15:18 +0000 (14:15 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 31 Aug 2023 12:15:18 +0000 (14:15 +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/fair-share-worker-choice-strategy.ts
src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/least-busy-worker-choice-strategy.ts
src/pools/selection-strategies/least-elu-worker-choice-strategy.ts
src/pools/selection-strategies/least-used-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 cd8736c25e771470871497be2ef2f6933105aaa4..ca3d9b9d640fc6bf0faedfe3105d5751802281e1 100644 (file)
@@ -194,18 +194,26 @@ export abstract class AbstractWorkerChoiceStrategy<
       : this.pool.workerNodes[workerNodeKey].usage.elu.active?.average ?? 0
   }
 
+  /**
+   * Sets safely the previous worker node key.
+   *
+   * @param workerNodeKey - The worker node key.
+   */
+  protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
+    this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey
+  }
+
   /**
    * Check the next worker node eligibility.
    *
-   * @param chosenNextWorkerNodeKey - The chosen next worker node key.
+   * @param previousWorkerNodeKey - The previous worker node key.
    */
   protected checkNextWorkerNodeEligibility (
-    chosenNextWorkerNodeKey: number | undefined
+    previousWorkerNodeKey: number | undefined
   ): void {
     if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
       this.nextWorkerNodeKey = undefined
-      this.previousWorkerNodeKey =
-        chosenNextWorkerNodeKey ?? this.previousWorkerNodeKey
+      this.setPreviousWorkerNodeKey(previousWorkerNodeKey)
     }
   }
 
index f293bbb7cbe00eacb8c5e1e0d30b0e670da5a3ed..c4a0d30beb7fe827d614ca9f434c7470aeb9d018 100644 (file)
@@ -70,6 +70,7 @@ export class FairShareWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
     this.nextWorkerNodeKey = this.fairShareNextWorkerNodeKey()
     return this.nextWorkerNodeKey
   }
@@ -81,8 +82,8 @@ export class FairShareWorkerChoiceStrategy<
   }
 
   private fairShareNextWorkerNodeKey (): number | undefined {
-    let minWorkerVirtualTaskEndTimestamp = Infinity
     let chosenWorkerNodeKey: number | undefined
+    let minWorkerVirtualTaskEndTimestamp = Infinity
     for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
       if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
         this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
index 2e82ac3d9caaaf0146ba9f3f8e9b6f45df1089da..0b32bd457af259810459fe6148aacf6dd95e06bd 100644 (file)
@@ -111,8 +111,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
           this.workerVirtualTaskRunTime =
             this.workerVirtualTaskRunTime +
             this.getWorkerTaskRunTime(workerNodeKey)
-          this.previousWorkerNodeKey =
-            this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+          this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
           this.nextWorkerNodeKey = workerNodeKey
           return this.nextWorkerNodeKey
         }
index de71167ee8d307bf8c9b20f5f2df6fca7d9374c6..0e8fdef31f27cdad48ba105554296f50df07f386 100644 (file)
@@ -61,6 +61,7 @@ export class LeastBusyWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
     this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
     return this.nextWorkerNodeKey
   }
@@ -71,8 +72,8 @@ export class LeastBusyWorkerChoiceStrategy<
   }
 
   private leastBusyNextWorkerNodeKey (): number | undefined {
-    let minTime = Infinity
     let chosenWorkerNodeKey: number | undefined
+    let minTime = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerTime =
         (workerNode.usage.runTime?.aggregate ?? 0) +
index 3ec23a1c532d14bcf65ac8f9d46d29fa5a8e9c72..2624e5b2fdc68444d1e085452fe117655b45f282 100644 (file)
@@ -57,6 +57,7 @@ export class LeastEluWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
     this.nextWorkerNodeKey = this.leastEluNextWorkerNodeKey()
     return this.nextWorkerNodeKey
   }
@@ -67,8 +68,8 @@ export class LeastEluWorkerChoiceStrategy<
   }
 
   private leastEluNextWorkerNodeKey (): number | undefined {
-    let minWorkerElu = Infinity
     let chosenWorkerNodeKey: number | undefined
+    let minWorkerElu = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerUsage = workerNode.usage
       const workerElu = workerUsage.elu?.active?.aggregate ?? 0
index c750c290d1d7f3c5039698a9be04a2971b623781..d20c32174cc553c01b120fdd055217218fc4c314 100644 (file)
@@ -42,6 +42,7 @@ export class LeastUsedWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
     this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey()
     return this.nextWorkerNodeKey
   }
@@ -52,8 +53,8 @@ export class LeastUsedWorkerChoiceStrategy<
   }
 
   private leastUsedNextWorkerNodeKey (): number | undefined {
-    let minNumberOfTasks = Infinity
     let chosenWorkerNodeKey: number | undefined
+    let minNumberOfTasks = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerTaskStatistics = workerNode.usage.tasks
       const workerTasks =
index 8a5656b80701fdf0f943b8e651ce0b00a5349f9f..6884a5d277b87892daeb4d8acaba99bc510c671a 100644 (file)
@@ -44,6 +44,7 @@ export class RoundRobinWorkerChoiceStrategy<
   /** @inheritDoc */
   public choose (): number | undefined {
     const chosenWorkerNodeKey = this.nextWorkerNodeKey
+    this.setPreviousWorkerNodeKey(chosenWorkerNodeKey)
     this.roundRobinNextWorkerNodeKey()
     return chosenWorkerNodeKey
   }
index 3ac941f08a96ec9963cf06041c118c80e9d56d7d..42952fa2c3d08534ea08595080cfa2860693e6b6 100644 (file)
@@ -70,9 +70,8 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number | undefined {
-    const chosenWorkerNodeKey = this.nextWorkerNodeKey
-    this.weightedRoundRobinNextWorkerNodeKey()
-    return chosenWorkerNodeKey
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
+    return this.weightedRoundRobinNextWorkerNodeKey()
   }
 
   /** @inheritDoc */