fix: avoid out of bound at worker node removal
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 31 Aug 2023 23:16:57 +0000 (01:16 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 31 Aug 2023 23:16:57 +0000 (01:16 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
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 0b32bd457af259810459fe6148aacf6dd95e06bd..7fba0f1b5dad0972af4ae9630b3c8565301a7863 100644 (file)
@@ -137,18 +137,20 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public remove (workerNodeKey: number): boolean {
-    if (this.nextWorkerNodeKey === workerNodeKey) {
-      if (this.pool.workerNodes.length === 0) {
-        this.roundId = 0
-        this.workerNodeId = 0
-        this.nextWorkerNodeKey = 0
-      } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
-        if (this.workerNodeId === this.nextWorkerNodeKey) {
-          this.workerNodeId = this.pool.workerNodes.length - 1
-        }
-        this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
-      }
-      this.workerVirtualTaskRunTime = 0
+    if (this.pool.workerNodes.length === 0) {
+      this.reset()
+    }
+    if (
+      this.workerNodeId === workerNodeKey &&
+      this.workerNodeId > this.pool.workerNodes.length - 1
+    ) {
+      this.workerNodeId = this.pool.workerNodes.length - 1
+    }
+    if (
+      this.previousWorkerNodeKey === workerNodeKey &&
+      this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
+    ) {
+      this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
     }
     return true
   }
index 6884a5d277b87892daeb4d8acaba99bc510c671a..aa3ee108ced7d528f1d5ea90204c4306f841226c 100644 (file)
@@ -51,12 +51,20 @@ export class RoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public remove (workerNodeKey: number): boolean {
-    if (this.nextWorkerNodeKey === workerNodeKey) {
-      if (this.pool.workerNodes.length === 0) {
-        this.nextWorkerNodeKey = 0
-      } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
-        this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
-      }
+    if (this.pool.workerNodes.length === 0) {
+      this.reset()
+    }
+    if (
+      this.nextWorkerNodeKey === workerNodeKey &&
+      this.nextWorkerNodeKey > this.pool.workerNodes.length - 1
+    ) {
+      this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
+    }
+    if (
+      this.previousWorkerNodeKey === workerNodeKey &&
+      this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
+    ) {
+      this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
     }
     return true
   }
index 42952fa2c3d08534ea08595080cfa2860693e6b6..c87081a6161c82ab612d5aca2b37a49e6b1089b0 100644 (file)
@@ -76,14 +76,18 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public remove (workerNodeKey: number): boolean {
+    if (this.pool.workerNodes.length === 0) {
+      this.reset()
+    }
     if (this.nextWorkerNodeKey === workerNodeKey) {
-      if (this.pool.workerNodes.length === 0) {
-        this.nextWorkerNodeKey = 0
-      } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
-        this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
-      }
       this.workerVirtualTaskRunTime = 0
     }
+    if (
+      this.previousWorkerNodeKey === workerNodeKey &&
+      this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
+    ) {
+      this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
+    }
     return true
   }