refactor: use Array.from to build Array() from Map()
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
index aff4e73dd3a6e5a641a626e153f39bd796b32108..2f2686e9bb0f5f6a6c18aa5ce51c913e9d3af7c5 100644 (file)
@@ -1,13 +1,12 @@
-import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils'
-import type { IPool } from '../pool'
-import type { IWorker, StrategyData } from '../worker'
-import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
+import type { IPool } from '../pool.js'
+import type { IWorker } from '../worker.js'
+import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
 import {
   type IWorkerChoiceStrategy,
-  type InternalWorkerChoiceStrategyOptions,
   Measurements,
-  type TaskStatisticsRequirements
-} from './selection-strategies-types'
+  type TaskStatisticsRequirements,
+  type WorkerChoiceStrategyOptions
+} from './selection-strategies-types.js'
 
 /**
  * Selects the next worker with a fair share scheduling algorithm.
@@ -31,7 +30,11 @@ export class FairShareWorkerChoiceStrategy<
       average: true,
       median: false
     },
-    waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+    waitTime: {
+      aggregate: true,
+      average: true,
+      median: false
+    },
     elu: {
       aggregate: true,
       average: true,
@@ -42,7 +45,7 @@ export class FairShareWorkerChoiceStrategy<
   /** @inheritDoc */
   public constructor (
     pool: IPool<Worker, Data, Response>,
-    opts: InternalWorkerChoiceStrategyOptions
+    opts?: WorkerChoiceStrategyOptions
   ) {
     super(pool, opts)
     this.setTaskStatisticsRequirements(this.opts)
@@ -78,6 +81,9 @@ export class FairShareWorkerChoiceStrategy<
   }
 
   private fairShareNextWorkerNodeKey (): number | undefined {
+    if (this.pool.workerNodes.length === 0) {
+      return undefined
+    }
     return this.pool.workerNodes.reduce(
       (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
         if (workerNode.strategyData?.virtualTaskEndTimestamp == null) {
@@ -87,9 +93,10 @@ export class FairShareWorkerChoiceStrategy<
           }
         }
         return this.isWorkerNodeReady(workerNodeKey) &&
-          (workerNode.strategyData.virtualTaskEndTimestamp as number) <
-            ((workerNodes[minWorkerNodeKey].strategyData as StrategyData)
-              .virtualTaskEndTimestamp as number)
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          workerNode.strategyData.virtualTaskEndTimestamp! <
+            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+            workerNodes[minWorkerNodeKey].strategyData!.virtualTaskEndTimestamp!
           ? workerNodeKey
           : minWorkerNodeKey
       },
@@ -116,11 +123,13 @@ export class FairShareWorkerChoiceStrategy<
     workerNodeKey: number,
     workerNodeVirtualTaskStartTimestamp: number
   ): number {
-    const workerNodeTaskRunTime =
-      this.opts.measurement === Measurements.elu
+    const workerNodeTaskExecutionTime =
+      this.getWorkerNodeTaskWaitTime(workerNodeKey) +
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      (this.opts!.measurement === Measurements.elu
         ? this.getWorkerNodeTaskElu(workerNodeKey)
-        : this.getWorkerNodeTaskRunTime(workerNodeKey)
-    return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime
+        : this.getWorkerNodeTaskRunTime(workerNodeKey))
+    return workerNodeVirtualTaskStartTimestamp + workerNodeTaskExecutionTime
   }
 
   private getWorkerNodeVirtualTaskStartTimestamp (
@@ -131,7 +140,8 @@ export class FairShareWorkerChoiceStrategy<
         ?.virtualTaskEndTimestamp
     const now = performance.now()
     return now < (virtualTaskEndTimestamp ?? -Infinity)
-      ? (virtualTaskEndTimestamp as number)
+      ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      virtualTaskEndTimestamp!
       : now
   }
 }