refactor: cleanup dead code
[poolifier.git] / src / pools / selection-strategies / least-busy-worker-choice-strategy.ts
index 05caa42a0149ebc7dc29ef76edc3a025e8b24c2c..5ef438f6084a1d3fb0d26326870bf351d5a5972d 100644 (file)
@@ -1,19 +1,19 @@
-import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
-import type { IPool } from '../pool'
-import type { IWorker } from '../worker'
-import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
+import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils.js'
+import type { IPool } from '../pool.js'
+import type { IWorker } from '../worker.js'
+import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
 import type {
   IWorkerChoiceStrategy,
-  RequiredStatistics,
+  TaskStatisticsRequirements,
   WorkerChoiceStrategyOptions
-} from './selection-strategies-types'
+} from './selection-strategies-types.js'
 
 /**
  * Selects the least busy worker.
  *
  * @typeParam Worker - Type of worker which manages the strategy.
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of execution response. This can only be serializable data.
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
  */
 export class LeastBusyWorkerChoiceStrategy<
     Worker extends IWorker,
@@ -23,19 +23,27 @@ export class LeastBusyWorkerChoiceStrategy<
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy {
   /** @inheritDoc */
-  public readonly requiredStatistics: RequiredStatistics = {
-    runTime: true,
-    avgRunTime: false,
-    medRunTime: false
+  public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
+    runTime: {
+      aggregate: true,
+      average: false,
+      median: false
+    },
+    waitTime: {
+      aggregate: true,
+      average: false,
+      median: false
+    },
+    elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
   }
 
   /** @inheritDoc */
   public constructor (
     pool: IPool<Worker, Data, Response>,
-    opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+    opts?: WorkerChoiceStrategyOptions
   ) {
     super(pool, opts)
-    this.setRequiredStatistics(this.opts)
+    this.setTaskStatisticsRequirements(this.opts)
   }
 
   /** @inheritDoc */
@@ -49,27 +57,32 @@ export class LeastBusyWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public choose (): number {
-    const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
-    if (freeWorkerNodeKey !== -1) {
-      return freeWorkerNodeKey
-    }
-    let minRunTime = Infinity
-    let leastBusyWorkerNodeKey!: number
-    for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
-      const workerRunTime = workerNode.tasksUsage.runTime
-      if (workerRunTime === 0) {
-        return workerNodeKey
-      } else if (workerRunTime < minRunTime) {
-        minRunTime = workerRunTime
-        leastBusyWorkerNodeKey = workerNodeKey
-      }
-    }
-    return leastBusyWorkerNodeKey
+  public choose (): number | undefined {
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
+    this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
+    return this.nextWorkerNodeKey
   }
 
   /** @inheritDoc */
   public remove (): boolean {
     return true
   }
+
+  private leastBusyNextWorkerNodeKey (): number | undefined {
+    if (this.pool.workerNodes.length === 0) {
+      return undefined
+    }
+    return this.pool.workerNodes.reduce(
+      (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+        return this.isWorkerNodeReady(workerNodeKey) &&
+          (workerNode.usage.runTime.aggregate ?? 0) +
+            (workerNode.usage.waitTime.aggregate ?? 0) <
+            (workerNodes[minWorkerNodeKey].usage.runTime.aggregate ?? 0) +
+              (workerNodes[minWorkerNodeKey].usage.waitTime.aggregate ?? 0)
+          ? workerNodeKey
+          : minWorkerNodeKey
+      },
+      0
+    )
+  }
 }