fix: ensure worker choice is retried at least the pool max size
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
index 16d5a7be1dd60ca213a873c1571f8335948ac826..aff4e73dd3a6e5a641a626e153f39bd796b32108 100644 (file)
-import type { IPoolWorker } from '../pool-worker'
+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 {
-  IWorkerChoiceStrategy,
-  RequiredStatistics
+import {
+  type IWorkerChoiceStrategy,
+  type InternalWorkerChoiceStrategyOptions,
+  Measurements,
+  type TaskStatisticsRequirements
 } from './selection-strategies-types'
 
-/**
- * Worker virtual task timestamp.
- */
-interface WorkerVirtualTaskTimestamp {
-  start: number
-  end: number
-}
-
 /**
  * Selects the next worker with a fair share scheduling algorithm.
  * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
  *
  * @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 response of execution. 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 FairShareWorkerChoiceStrategy<
-    Worker extends IPoolWorker,
+    Worker extends IWorker,
     Data = unknown,
     Response = unknown
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy {
   /** @inheritDoc */
-  public readonly requiredStatistics: RequiredStatistics = {
-    runTime: true,
-    avgRunTime: true
+  public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
+    runTime: {
+      aggregate: true,
+      average: true,
+      median: false
+    },
+    waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+    elu: {
+      aggregate: true,
+      average: true,
+      median: false
+    }
   }
 
-  /**
-   *  Worker last virtual task execution timestamp.
-   */
-  private readonly workerLastVirtualTaskTimestamp: Map<
-  number,
-  WorkerVirtualTaskTimestamp
-  > = new Map<number, WorkerVirtualTaskTimestamp>()
+  /** @inheritDoc */
+  public constructor (
+    pool: IPool<Worker, Data, Response>,
+    opts: InternalWorkerChoiceStrategyOptions
+  ) {
+    super(pool, opts)
+    this.setTaskStatisticsRequirements(this.opts)
+  }
 
   /** @inheritDoc */
   public reset (): boolean {
-    this.workerLastVirtualTaskTimestamp.clear()
+    for (const workerNode of this.pool.workerNodes) {
+      delete workerNode.strategyData?.virtualTaskEndTimestamp
+    }
     return true
   }
 
   /** @inheritDoc */
-  public choose (): number {
-    let minWorkerVirtualTaskEndTimestamp = Infinity
-    let chosenWorkerKey!: number
-    for (const [index] of this.pool.workers.entries()) {
-      this.computeWorkerLastVirtualTaskTimestamp(index)
-      const workerLastVirtualTaskEndTimestamp =
-        this.workerLastVirtualTaskTimestamp.get(index)?.end ?? 0
-      if (
-        workerLastVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
-      ) {
-        minWorkerVirtualTaskEndTimestamp = workerLastVirtualTaskEndTimestamp
-        chosenWorkerKey = index
-      }
+  public update (workerNodeKey: number): boolean {
+    this.pool.workerNodes[workerNodeKey].strategyData = {
+      virtualTaskEndTimestamp:
+        this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey)
     }
-    return chosenWorkerKey
+    return true
   }
 
   /** @inheritDoc */
-  public remove (workerKey: number): boolean {
-    const workerDeleted = this.workerLastVirtualTaskTimestamp.delete(workerKey)
-    for (const [key, value] of this.workerLastVirtualTaskTimestamp.entries()) {
-      if (key > workerKey) {
-        this.workerLastVirtualTaskTimestamp.set(key - 1, value)
-      }
-    }
-    return workerDeleted
+  public choose (): number | undefined {
+    this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
+    this.nextWorkerNodeKey = this.fairShareNextWorkerNodeKey()
+    return this.nextWorkerNodeKey
+  }
+
+  /** @inheritDoc */
+  public remove (): boolean {
+    return true
+  }
+
+  private fairShareNextWorkerNodeKey (): number | undefined {
+    return this.pool.workerNodes.reduce(
+      (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+        if (workerNode.strategyData?.virtualTaskEndTimestamp == null) {
+          workerNode.strategyData = {
+            virtualTaskEndTimestamp:
+              this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey)
+          }
+        }
+        return this.isWorkerNodeReady(workerNodeKey) &&
+          (workerNode.strategyData.virtualTaskEndTimestamp as number) <
+            ((workerNodes[minWorkerNodeKey].strategyData as StrategyData)
+              .virtualTaskEndTimestamp as number)
+          ? workerNodeKey
+          : minWorkerNodeKey
+      },
+      0
+    )
   }
 
   /**
-   * Computes worker last virtual task timestamp.
+   * Computes the worker node key virtual task end timestamp.
    *
-   * @param workerKey - The worker key.
+   * @param workerNodeKey - The worker node key.
+   * @returns The worker node key virtual task end timestamp.
    */
-  private computeWorkerLastVirtualTaskTimestamp (workerKey: number): void {
-    const workerVirtualTaskStartTimestamp = Math.max(
-      Date.now(),
-      this.workerLastVirtualTaskTimestamp.get(workerKey)?.end ?? -Infinity
+  private computeWorkerNodeVirtualTaskEndTimestamp (
+    workerNodeKey: number
+  ): number {
+    return this.getWorkerNodeVirtualTaskEndTimestamp(
+      workerNodeKey,
+      this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey)
     )
-    this.workerLastVirtualTaskTimestamp.set(workerKey, {
-      start: workerVirtualTaskStartTimestamp,
-      end:
-        workerVirtualTaskStartTimestamp +
-        (this.pool.workers[workerKey].tasksUsage.avgRunTime ?? 0)
-    })
+  }
+
+  private getWorkerNodeVirtualTaskEndTimestamp (
+    workerNodeKey: number,
+    workerNodeVirtualTaskStartTimestamp: number
+  ): number {
+    const workerNodeTaskRunTime =
+      this.opts.measurement === Measurements.elu
+        ? this.getWorkerNodeTaskElu(workerNodeKey)
+        : this.getWorkerNodeTaskRunTime(workerNodeKey)
+    return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime
+  }
+
+  private getWorkerNodeVirtualTaskStartTimestamp (
+    workerNodeKey: number
+  ): number {
+    const virtualTaskEndTimestamp =
+      this.pool.workerNodes[workerNodeKey]?.strategyData
+        ?.virtualTaskEndTimestamp
+    const now = performance.now()
+    return now < (virtualTaskEndTimestamp ?? -Infinity)
+      ? (virtualTaskEndTimestamp as number)
+      : now
   }
 }