refactor: cleanup worker choice strategies code
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 10 Jul 2023 16:10:03 +0000 (18:10 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 10 Jul 2023 16:10:03 +0000 (18:10 +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/fair-share-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/selection-strategies-types.ts
src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts
src/pools/worker.ts

index c4f1e2c65483ccceb1c3bb2f3343caac2c7530cf..01beb340dad7c59ac0b361f576382a66e42e1b9a 100644 (file)
@@ -24,11 +24,6 @@ export abstract class AbstractWorkerChoiceStrategy<
   Data = unknown,
   Response = unknown
 > implements IWorkerChoiceStrategy {
-  // /**
-  //  * Toggles finding the last free worker node key.
-  //  */
-  // private toggleFindLastFreeWorkerNodeKey: boolean = false
-
   /**
    * The next worker node key.
    */
@@ -132,20 +127,6 @@ export abstract class AbstractWorkerChoiceStrategy<
     return this.pool.workerNodes[workerNodeKey].info.ready
   }
 
-  // /**
-  //  * Finds a free worker node key.
-  //  *
-  //  * @returns The free worker node key or `-1` if there is no free worker node.
-  //  */
-  // protected findFreeWorkerNodeKey (): number {
-  //   if (this.toggleFindLastFreeWorkerNodeKey) {
-  //     this.toggleFindLastFreeWorkerNodeKey = false
-  //     return this.findLastFreeWorkerNodeKey()
-  //   }
-  //   this.toggleFindLastFreeWorkerNodeKey = true
-  //   return this.findFirstFreeWorkerNodeKey()
-  // }
-
   /**
    * Gets the worker task runtime.
    * If the task statistics require the average runtime, the average runtime is returned.
@@ -198,45 +179,4 @@ export abstract class AbstractWorkerChoiceStrategy<
     }
     return Math.round(cpusCycleTimeWeight / cpus().length)
   }
-
-  // /**
-  //  * Finds the first free worker node key based on the number of tasks the worker has applied.
-  //  *
-  //  * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
-  //  *
-  //  * If no free worker is found, `-1` is returned.
-  //  *
-  //  * @returns A worker node key if there is one, `-1` otherwise.
-  //  */
-  // private findFirstFreeWorkerNodeKey (): number {
-  //   return this.pool.workerNodes.findIndex(workerNode => {
-  //     return workerNode.usage.tasks.executing === 0
-  //   })
-  // }
-
-  // /**
-  //  * Finds the last free worker node key based on the number of tasks the worker has applied.
-  //  *
-  //  * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
-  //  *
-  //  * If no free worker is found, `-1` is returned.
-  //  *
-  //  * @returns A worker node key if there is one, `-1` otherwise.
-  //  */
-  // private findLastFreeWorkerNodeKey (): number {
-  //   // It requires node >= 18.0.0:
-  //   // return this.pool.workerNodes.findLastIndex(workerNode => {
-  //   //   return workerNode.usage.tasks.executing === 0
-  //   // })
-  //   for (
-  //     let workerNodeKey = this.pool.workerNodes.length - 1;
-  //     workerNodeKey >= 0;
-  //     workerNodeKey--
-  //   ) {
-  //     if (this.pool.workerNodes[workerNodeKey].usage.tasks.executing === 0) {
-  //       return workerNodeKey
-  //     }
-  //   }
-  //   return -1
-  // }
 }
index 00c56dad8f555f64afc0a4d21a693d6a97b956f5..bf30a4764d4ffdd5287fcbbad588baddca42b193 100644 (file)
@@ -70,8 +70,7 @@ export class FairShareWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
-    this.fairShareNextWorkerNodeKey()
-    return this.nextWorkerNodeKey
+    return this.fairShareNextWorkerNodeKey()
   }
 
   /** @inheritDoc */
@@ -80,7 +79,7 @@ export class FairShareWorkerChoiceStrategy<
     return true
   }
 
-  private fairShareNextWorkerNodeKey (): void {
+  private fairShareNextWorkerNodeKey (): number {
     let minWorkerVirtualTaskEndTimestamp = Infinity
     for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
       if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
@@ -96,6 +95,7 @@ export class FairShareWorkerChoiceStrategy<
         this.nextWorkerNodeKey = workerNodeKey
       }
     }
+    return this.nextWorkerNodeKey
   }
 
   /**
index dabad6826568c38621c73d8fa4f6ac4efb28418f..9a1550e06f60dfecc4339f56ac2c01ad3a7344cf 100644 (file)
@@ -61,8 +61,7 @@ export class LeastBusyWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
-    this.leastBusyNextWorkerNodeKey()
-    return this.nextWorkerNodeKey
+    return this.leastBusyNextWorkerNodeKey()
   }
 
   /** @inheritDoc */
@@ -70,7 +69,7 @@ export class LeastBusyWorkerChoiceStrategy<
     return true
   }
 
-  private leastBusyNextWorkerNodeKey (): void {
+  private leastBusyNextWorkerNodeKey (): number {
     let minTime = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerTime =
@@ -87,5 +86,6 @@ export class LeastBusyWorkerChoiceStrategy<
         this.nextWorkerNodeKey = workerNodeKey
       }
     }
+    return this.nextWorkerNodeKey
   }
 }
index 24c612e073806e1800bbe4113515354dfbdd0334..7c837ceff3d7b7ca45a5bd9da29feaa70784ef0c 100644 (file)
@@ -57,8 +57,7 @@ export class LeastEluWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
-    this.leastEluNextWorkerNodeKey()
-    return this.nextWorkerNodeKey
+    return this.leastEluNextWorkerNodeKey()
   }
 
   /** @inheritDoc */
@@ -66,7 +65,7 @@ export class LeastEluWorkerChoiceStrategy<
     return true
   }
 
-  private leastEluNextWorkerNodeKey (): void {
+  private leastEluNextWorkerNodeKey (): number {
     let minWorkerElu = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerUsage = workerNode.usage
@@ -82,5 +81,6 @@ export class LeastEluWorkerChoiceStrategy<
         this.nextWorkerNodeKey = workerNodeKey
       }
     }
+    return this.nextWorkerNodeKey
   }
 }
index 910d10a3ac320db8f3feac42eb43293181a806d1..e8a7218e160879bcaa97399d78a3b39ff7857bc1 100644 (file)
@@ -42,8 +42,7 @@ export class LeastUsedWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
-    this.leastUsedNextWorkerNodeKey()
-    return this.nextWorkerNodeKey
+    return this.leastUsedNextWorkerNodeKey()
   }
 
   /** @inheritDoc */
@@ -51,7 +50,7 @@ export class LeastUsedWorkerChoiceStrategy<
     return true
   }
 
-  private leastUsedNextWorkerNodeKey (): void {
+  private leastUsedNextWorkerNodeKey (): number {
     let minNumberOfTasks = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
       const workerTaskStatistics = workerNode.usage.tasks
@@ -70,5 +69,6 @@ export class LeastUsedWorkerChoiceStrategy<
         this.nextWorkerNodeKey = workerNodeKey
       }
     }
+    return this.nextWorkerNodeKey
   }
 }
index fdf5ac84fddf87235b1d7732e81f2ccff3aae53b..222a03495db53a789a0038e3ce9e0e41f0e17518 100644 (file)
@@ -66,10 +66,11 @@ export class RoundRobinWorkerChoiceStrategy<
     return true
   }
 
-  private roundRobinNextWorkerNodeKey (): void {
+  private roundRobinNextWorkerNodeKey (): number {
     this.nextWorkerNodeKey =
       this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
         ? 0
         : this.nextWorkerNodeKey + 1
+    return this.nextWorkerNodeKey
   }
 }
index e53f3e822967b43a7d6f11ca0b34e6e3a9e7ed4e..563b04b00086a7a31a68f7cb25ca4bde6b513c92 100644 (file)
@@ -147,7 +147,7 @@ export interface TaskStatisticsRequirements {
  */
 export interface StrategyPolicy {
   /**
-   * Expects direct usage of dynamic worker.
+   * Expects direct usage of the newly created dynamic worker.
    */
   readonly useDynamicWorker: boolean
 }
index ed38aa1497176ba88751e83a850e1794b3aaa9fd..4921df6dd115899261bd05f6fa2f223f09605e91 100644 (file)
@@ -94,7 +94,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     return true
   }
 
-  private weightedRoundRobinNextWorkerNodeKey (): void {
+  private weightedRoundRobinNextWorkerNodeKey (): number {
     const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
     const workerWeight =
       this.opts.weights?.[this.nextWorkerNodeKey] ?? this.defaultWorkerWeight
@@ -109,5 +109,6 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
           : this.nextWorkerNodeKey + 1
       this.workerVirtualTaskRunTime = 0
     }
+    return this.nextWorkerNodeKey
   }
 }
index 58610eaaddacc2beea93809f80675bb8530010c0..507396dea078ac806c5dda839336f6046df11d5c 100644 (file)
@@ -238,7 +238,7 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    */
   readonly resetUsage: () => void
   /**
-   * Gets task usage statistics.
+   * Gets task worker usage statistics.
    */
   readonly getTaskWorkerUsage: (name: string) => WorkerUsage | undefined
 }