refactor: untangle worker choosing code from worker creation code
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
index 925f318c6cd24b124068c20791ca81c3a494e6f1..ea598b207f2e1b20358ce0cd6c5da134077ae8f1 100644 (file)
@@ -1,13 +1,16 @@
-import { cpus } from 'os'
+import { cpus } from 'node:os'
 import type { IPoolInternal } from '../pool-internal'
 import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
-import type { RequiredStatistics } from './selection-strategies-types'
+import type {
+  IWorkerChoiceStrategy,
+  RequiredStatistics
+} from './selection-strategies-types'
 
 /**
  * Virtual task runtime.
  */
-type TaskRunTime = {
+interface TaskRunTime {
   weight: number
   runTime: number
 }
@@ -16,44 +19,43 @@ type TaskRunTime = {
  * Selects the next worker with a weighted round robin scheduling algorithm.
  * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
  *
- * @template Worker Type of worker which manages the strategy.
- * @template Data Type of data sent to the worker. This can only be serializable data.
- * @template Response Type of response of execution. This can only be serializable data.
+ * @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.
  */
 export class WeightedRoundRobinWorkerChoiceStrategy<
-  Worker extends IPoolWorker,
-  Data,
-  Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
+    Worker extends IPoolWorker,
+    Data = unknown,
+    Response = unknown
+  >
+  extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+  implements IWorkerChoiceStrategy {
   /** @inheritDoc */
   public readonly requiredStatistics: RequiredStatistics = {
-    runTime: true
+    runTime: true,
+    avgRunTime: true
   }
 
   /**
-   * Worker index where the previous task was submitted.
+   * Worker id where the current task will be submitted.
    */
-  private previousWorkerIndex: number = 0
-  /**
-   * Worker index where the current task will be submitted.
-   */
-  private currentWorkerIndex: number = 0
+  private currentWorkerId: number = 0
   /**
    * Default worker weight.
    */
-  private defaultWorkerWeight: number
+  private readonly defaultWorkerWeight: number
   /**
    * Per worker virtual task runtime map.
    */
-  private readonly workersTaskRunTime: Map<Worker, TaskRunTime> = new Map<
-    Worker,
-    TaskRunTime
+  private readonly workersTaskRunTime: Map<number, TaskRunTime> = new Map<
+  number,
+  TaskRunTime
   >()
 
   /**
    * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
    *
-   * @param pool The pool instance.
+   * @param pool The pool instance.
    */
   public constructor (pool: IPoolInternal<Worker, Data, Response>) {
     super(pool)
@@ -62,82 +64,94 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public resetStatistics (): boolean {
+  public reset (): boolean {
+    this.currentWorkerId = 0
     this.workersTaskRunTime.clear()
     this.initWorkersTaskRunTime()
     return true
   }
 
   /** @inheritDoc */
-  public choose (): Worker {
-    const currentWorker = this.pool.workers[this.currentWorkerIndex]
-    if (
-      this.isDynamicPool === true &&
-      this.workersTaskRunTime.has(currentWorker) === false
-    ) {
-      this.initWorkerTaskRunTime(currentWorker)
+  public choose (): number {
+    const chosenWorkerKey = this.currentWorkerId
+    if (this.isDynamicPool && !this.workersTaskRunTime.has(chosenWorkerKey)) {
+      this.initWorkerTaskRunTime(chosenWorkerKey)
     }
-    const workerVirtualTaskRunTime =
-      this.getWorkerVirtualTaskRunTime(currentWorker) ?? 0
+    const workerTaskRunTime =
+      this.workersTaskRunTime.get(chosenWorkerKey)?.runTime ?? 0
     const workerTaskWeight =
-      this.workersTaskRunTime.get(currentWorker)?.weight ??
+      this.workersTaskRunTime.get(chosenWorkerKey)?.weight ??
       this.defaultWorkerWeight
-    if (this.currentWorkerIndex === this.previousWorkerIndex) {
-      const workerTaskRunTime =
-        (this.workersTaskRunTime.get(currentWorker)?.runTime ?? 0) +
-        workerVirtualTaskRunTime
+    if (workerTaskRunTime < workerTaskWeight) {
       this.setWorkerTaskRunTime(
-        currentWorker,
+        chosenWorkerKey,
         workerTaskWeight,
-        workerTaskRunTime
+        workerTaskRunTime +
+          (this.getWorkerVirtualTaskRunTime(chosenWorkerKey) ?? 0)
       )
     } else {
-      this.setWorkerTaskRunTime(currentWorker, workerTaskWeight, 0)
-    }
-    if (workerVirtualTaskRunTime < workerTaskWeight) {
-      this.previousWorkerIndex = this.currentWorkerIndex
-    } else {
-      this.previousWorkerIndex = this.currentWorkerIndex
-      this.currentWorkerIndex =
-        this.pool.workers.length - 1 === this.currentWorkerIndex
+      this.currentWorkerId =
+        this.currentWorkerId === this.pool.workers.length - 1
           ? 0
-          : this.currentWorkerIndex + 1
+          : this.currentWorkerId + 1
+      this.setWorkerTaskRunTime(this.currentWorkerId, workerTaskWeight, 0)
+    }
+    return chosenWorkerKey
+  }
+
+  /** @inheritDoc */
+  public remove (workerKey: number): boolean {
+    if (this.currentWorkerId === workerKey) {
+      if (this.pool.workers.length === 0) {
+        this.currentWorkerId = 0
+      } else {
+        this.currentWorkerId =
+          this.currentWorkerId > this.pool.workers.length - 1
+            ? this.pool.workers.length - 1
+            : this.currentWorkerId
+      }
+    }
+    const workerDeleted = this.workersTaskRunTime.delete(workerKey)
+    for (const [key, value] of this.workersTaskRunTime) {
+      if (key > workerKey) {
+        this.workersTaskRunTime.set(key - 1, value)
+      }
     }
-    return this.pool.workers[this.currentWorkerIndex]
+    return workerDeleted
   }
 
   private initWorkersTaskRunTime (): void {
-    for (const worker of this.pool.workers) {
-      this.initWorkerTaskRunTime(worker)
+    for (const [index] of this.pool.workers.entries()) {
+      this.initWorkerTaskRunTime(index)
     }
   }
 
-  private initWorkerTaskRunTime (worker: Worker): void {
-    this.setWorkerTaskRunTime(worker, this.defaultWorkerWeight, 0)
+  private initWorkerTaskRunTime (workerKey: number): void {
+    this.setWorkerTaskRunTime(workerKey, this.defaultWorkerWeight, 0)
   }
 
   private setWorkerTaskRunTime (
-    worker: Worker,
+    workerKey: number,
     weight: number,
     runTime: number
   ): void {
-    this.workersTaskRunTime.set(worker, {
+    this.workersTaskRunTime.set(workerKey, {
       weight,
       runTime
     })
   }
 
-  private getWorkerVirtualTaskRunTime (worker: Worker): number | undefined {
-    return this.pool.getWorkerAverageTasksRunTime(worker)
+  private getWorkerVirtualTaskRunTime (workerKey: number): number {
+    return this.pool.workers[workerKey].tasksUsage.avgRunTime
   }
 
   private computeWorkerWeight (): number {
     let cpusCycleTimeWeight = 0
     for (const cpu of cpus()) {
       // CPU estimated cycle time
-      const numberOfDigit = cpu.speed.toString().length - 1
-      const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigit))
-      cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigit)
+      const numberOfDigits = cpu.speed.toString().length - 1
+      const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
+      cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
     }
     return Math.round(cpusCycleTimeWeight / cpus().length)
   }