fix: fix fair share algorithm implementation
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index 5b526331824be72af5048eb8c76fe26b001a373a..e2dd626de76106991b11d1567d70c9e6f3d21ab7 100644 (file)
-import type { IPoolInternal } from '../pool-internal'
-import { PoolType } from '../pool-internal'
-import type { IPoolWorker } from '../pool-worker'
+import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
+import type { IPool } from '../pool'
+import type { IWorker } from '../worker'
 import type {
   IWorkerChoiceStrategy,
-  RequiredStatistics
+  RequiredStatistics,
+  WorkerChoiceStrategyOptions
 } from './selection-strategies-types'
 
 /**
- * Abstract worker choice strategy class.
+ * Worker choice strategy abstract base class.
  *
  * @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 Response - Type of execution response. This can only be serializable data.
  */
 export abstract class AbstractWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
+  Worker extends IWorker,
+  Data = unknown,
+  Response = unknown
 > implements IWorkerChoiceStrategy {
-  /** {@inheritDoc} */
-  public readonly isDynamicPool: boolean
-  /** {@inheritDoc} */
-  public requiredStatistics: RequiredStatistics = {
-    runTime: false
+  /**
+   * Toggles finding the last free worker node key.
+   */
+  private toggleFindLastFreeWorkerNodeKey: boolean = false
+  /** @inheritDoc */
+  public readonly requiredStatistics: RequiredStatistics = {
+    runTime: false,
+    avgRunTime: false,
+    medRunTime: false
   }
 
   /**
-   * Constructs a worker choice strategy attached to the pool.
+   * Constructs a worker choice strategy bound to the pool.
    *
    * @param pool - The pool instance.
+   * @param opts - The worker choice strategy options.
    */
   public constructor (
-    protected readonly pool: IPoolInternal<Worker, Data, Response>
+    protected readonly pool: IPool<Worker, Data, Response>,
+    protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
-    this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
+    this.choose = this.choose.bind(this)
+  }
+
+  protected checkOptions (opts: WorkerChoiceStrategyOptions): void {
+    if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) {
+      this.requiredStatistics.avgRunTime = false
+      this.requiredStatistics.medRunTime = opts.medRunTime as boolean
+    }
+    if (this.requiredStatistics.medRunTime && opts.medRunTime === false) {
+      this.requiredStatistics.avgRunTime = true
+      this.requiredStatistics.medRunTime = opts.medRunTime as boolean
+    }
+    if (
+      opts.weights != null &&
+      Object.keys(opts.weights).length < this.pool.size
+    ) {
+      throw new Error(
+        'Worker choice strategy options must have a weight for each worker node.'
+      )
+    }
   }
 
-  /** {@inheritDoc} */
+  /** @inheritDoc */
   public abstract reset (): boolean
 
-  /** {@inheritDoc} */
+  /** @inheritDoc */
+  public abstract update (workerNodeKey: number): boolean
+
+  /** @inheritDoc */
   public abstract choose (): number
+
+  /** @inheritDoc */
+  public abstract remove (workerNodeKey: number): boolean
+
+  /** @inheritDoc */
+  public setOptions (opts: WorkerChoiceStrategyOptions): void {
+    opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+    this.checkOptions(opts)
+    this.opts = opts
+  }
+
+  /**
+   * Finds a free worker node key.
+   *
+   * @returns The free worker node key or `-1` if there is no free worker node.
+   */
+  protected findFreeWorkerNodeKey (): number {
+    if (this.toggleFindLastFreeWorkerNodeKey) {
+      this.toggleFindLastFreeWorkerNodeKey = false
+      return this.findLastFreeWorkerNodeKey()
+    }
+    this.toggleFindLastFreeWorkerNodeKey = true
+    return this.findFirstFreeWorkerNodeKey()
+  }
+
+  /**
+   * Finds the first free worker node key based on the number of tasks the worker has applied.
+   *
+   * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
+   *
+   * If no free worker is found, `-1` is returned.
+   *
+   * @returns A worker node key if there is one, `-1` otherwise.
+   */
+  private findFirstFreeWorkerNodeKey (): number {
+    return this.pool.workerNodes.findIndex(workerNode => {
+      return workerNode.tasksUsage.running === 0
+    })
+  }
+
+  /**
+   * Finds the last free worker node key based on the number of tasks the worker has applied.
+   *
+   * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
+   *
+   * If no free worker is found, `-1` is returned.
+   *
+   * @returns A worker node key if there is one, `-1` otherwise.
+   */
+  private findLastFreeWorkerNodeKey (): number {
+    // It requires node >= 18.0.0:
+    // return this.workerNodes.findLastIndex(workerNode => {
+    //   return workerNode.tasksUsage.running === 0
+    // })
+    for (let i = this.pool.workerNodes.length - 1; i >= 0; i--) {
+      if (this.pool.workerNodes[i].tasksUsage.running === 0) {
+        return i
+      }
+    }
+    return -1
+  }
 }