refactor: factor out worker runtime getter
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 13 May 2023 13:40:46 +0000 (15:40 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 13 May 2023 13:40:46 +0000 (15:40 +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/weighted-round-robin-worker-choice-strategy.ts

index c3fb28c4f1e162185f65e08029c886e8df826d99..a4455d28a8e33c5bac1bceaf679b8feab5de533b 100644 (file)
@@ -87,6 +87,20 @@ export abstract class AbstractWorkerChoiceStrategy<
     return this.findFirstFreeWorkerNodeKey()
   }
 
+  /**
+   * Gets the worker task run time.
+   * If the required statistics are `avgRunTime`, the average run time is returned.
+   * If the required statistics are `medRunTime`, the median run time is returned.
+   *
+   * @param workerNodeKey - The worker node key.
+   * @returns The worker task run time.
+   */
+  protected getWorkerTaskRunTime (workerNodeKey: number): number {
+    return this.requiredStatistics.medRunTime
+      ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
+      : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
+  }
+
   /**
    * Finds the first free worker node key based on the number of tasks the worker has applied.
    *
index 0ae939949b1c115918db6bb1f33dfab2e253191a..e26f6b8f5953dff0b3d6dc1d59469977ab264494 100644 (file)
@@ -97,10 +97,9 @@ export class FairShareWorkerChoiceStrategy<
     workerNodeKey: number,
     workerVirtualTaskStartTimestamp: number
   ): number {
-    const workerVirtualTaskRunTime = this.requiredStatistics.medRunTime
-      ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
-      : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
-    return workerVirtualTaskStartTimestamp + workerVirtualTaskRunTime
+    return (
+      workerVirtualTaskStartTimestamp + this.getWorkerTaskRunTime(workerNodeKey)
+    )
   }
 
   private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number {
index bda206a7d7e1521ac8a6056b0485ad18e7698375..a0553fcb81ace9c910b1ecd755b9c123b640f362 100644 (file)
@@ -75,7 +75,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     if (workerVirtualTaskRunTime < workerWeight) {
       this.workerVirtualTaskRunTime =
         workerVirtualTaskRunTime +
-        this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey)
+        this.getWorkerTaskRunTime(chosenWorkerNodeKey)
     } else {
       this.currentWorkerNodeId =
         this.currentWorkerNodeId === this.pool.workerNodes.length - 1
@@ -99,12 +99,6 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     return true
   }
 
-  private getWorkerVirtualTaskRunTime (workerNodeKey: number): number {
-    return this.requiredStatistics.medRunTime
-      ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
-      : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
-  }
-
   private computeDefaultWorkerWeight (): number {
     let cpusCycleTimeWeight = 0
     for (const cpu of cpus()) {