Merge pull request #2365 from poolifier/map-execute
[poolifier.git] / src / pools / selection-strategies / least-busy-worker-choice-strategy.ts
index dabad6826568c38621c73d8fa4f6ac4efb28418f..33199368c67b176a23920f407973aaf290e3a9be 100644 (file)
@@ -1,19 +1,15 @@
-import {
-  DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-  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 type { IPool } from '../pool.js'
+import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js'
+import type { IWorker } from '../worker.js'
+import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
 import type {
   IWorkerChoiceStrategy,
   TaskStatisticsRequirements,
-  WorkerChoiceStrategyOptions
-} from './selection-strategies-types'
+  WorkerChoiceStrategyOptions,
+} 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 structured-cloneable data.
  * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
@@ -30,20 +26,20 @@ export class LeastBusyWorkerChoiceStrategy<
     runTime: {
       aggregate: true,
       average: false,
-      median: false
+      median: false,
     },
     waitTime: {
       aggregate: true,
       average: false,
-      median: false
+      median: false,
     },
-    elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
+    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.setTaskStatisticsRequirements(this.opts)
@@ -60,8 +56,9 @@ export class LeastBusyWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public choose (): number {
-    this.leastBusyNextWorkerNodeKey()
+  public choose (): number | undefined {
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
+    this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
     return this.nextWorkerNodeKey
   }
 
@@ -70,22 +67,18 @@ export class LeastBusyWorkerChoiceStrategy<
     return true
   }
 
-  private leastBusyNextWorkerNodeKey (): void {
-    let minTime = Infinity
-    for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
-      const workerTime =
-        (workerNode.usage.runTime?.aggregate ?? 0) +
-        (workerNode.usage.waitTime?.aggregate ?? 0)
-      if (this.isWorkerNodeReady(workerNodeKey) && workerTime === 0) {
-        this.nextWorkerNodeKey = workerNodeKey
-        break
-      } else if (
-        this.isWorkerNodeReady(workerNodeKey) &&
-        workerTime < minTime
-      ) {
-        minTime = workerTime
-        this.nextWorkerNodeKey = workerNodeKey
-      }
-    }
+  private leastBusyNextWorkerNodeKey (): number | undefined {
+    return this.pool.workerNodes.reduce(
+      (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+        return this.isWorkerNodeReady(workerNodeKey) &&
+          (workerNode.usage.waitTime.aggregate ?? 0) +
+            (workerNode.usage.runTime.aggregate ?? 0) <
+            (workerNodes[minWorkerNodeKey].usage.waitTime.aggregate ?? 0) +
+              (workerNodes[minWorkerNodeKey].usage.runTime.aggregate ?? 0)
+          ? workerNodeKey
+          : minWorkerNodeKey
+      },
+      0
+    )
   }
 }