refactor: sensible defaults for worker choice strategy policy
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
index a0553fcb81ace9c910b1ecd755b9c123b640f362..78ad7485dcc58ddb8c65ebb946fcdc6a34b92f5d 100644 (file)
@@ -1,11 +1,13 @@
-import { cpus } from 'node:os'
 import type { IWorker } from '../worker'
 import type { IPool } from '../pool'
-import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
+import {
+  DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+  DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+} from '../../utils'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
 import type {
   IWorkerChoiceStrategy,
-  RequiredStatistics,
+  TaskStatisticsRequirements,
   WorkerChoiceStrategyOptions
 } from './selection-strategies-types'
 
@@ -14,8 +16,8 @@ import type {
  * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
  *
  * @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 execution response. 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 WeightedRoundRobinWorkerChoiceStrategy<
     Worker extends IWorker,
@@ -25,16 +27,16 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy {
   /** @inheritDoc */
-  public readonly requiredStatistics: RequiredStatistics = {
-    runTime: true,
-    avgRunTime: true,
-    medRunTime: false
+  public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
+    runTime: {
+      aggregate: true,
+      average: true,
+      median: false
+    },
+    waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+    elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
   }
 
-  /**
-   * Worker node id where the current task will be submitted.
-   */
-  private currentWorkerNodeId: number = 0
   /**
    * Default worker weight.
    */
@@ -50,13 +52,13 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     super(pool, opts)
-    this.setRequiredStatistics(this.opts)
+    this.setTaskStatisticsRequirements(this.opts)
     this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
   }
 
   /** @inheritDoc */
   public reset (): boolean {
-    this.currentWorkerNodeId = 0
+    this.nextWorkerNodeKey = 0
     this.workerVirtualTaskRunTime = 0
     return true
   }
@@ -67,46 +69,49 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public choose (): number {
-    const chosenWorkerNodeKey = this.currentWorkerNodeId
-    const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
-    const workerWeight =
-      this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
-    if (workerVirtualTaskRunTime < workerWeight) {
-      this.workerVirtualTaskRunTime =
-        workerVirtualTaskRunTime +
-        this.getWorkerTaskRunTime(chosenWorkerNodeKey)
-    } else {
-      this.currentWorkerNodeId =
-        this.currentWorkerNodeId === this.pool.workerNodes.length - 1
-          ? 0
-          : this.currentWorkerNodeId + 1
-      this.workerVirtualTaskRunTime = 0
+  public choose (): number | undefined {
+    const chosenWorkerNodeKey = this.nextWorkerNodeKey
+    this.weightedRoundRobinNextWorkerNodeKey()
+    if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
+      this.nextWorkerNodeKey = undefined
+      this.previousWorkerNodeKey =
+        chosenWorkerNodeKey ?? this.previousWorkerNodeKey
     }
     return chosenWorkerNodeKey
   }
 
   /** @inheritDoc */
   public remove (workerNodeKey: number): boolean {
-    if (this.currentWorkerNodeId === workerNodeKey) {
+    if (this.nextWorkerNodeKey === workerNodeKey) {
       if (this.pool.workerNodes.length === 0) {
-        this.currentWorkerNodeId = 0
-      } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
-        this.currentWorkerNodeId = this.pool.workerNodes.length - 1
+        this.nextWorkerNodeKey = 0
+      } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
+        this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
       }
       this.workerVirtualTaskRunTime = 0
     }
     return true
   }
 
-  private computeDefaultWorkerWeight (): number {
-    let cpusCycleTimeWeight = 0
-    for (const cpu of cpus()) {
-      // CPU estimated cycle time
-      const numberOfDigits = cpu.speed.toString().length - 1
-      const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
-      cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
+  private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
+    const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
+    const workerWeight =
+      this.opts.weights?.[
+        this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+      ] ?? this.defaultWorkerWeight
+    if (workerVirtualTaskRunTime < workerWeight) {
+      this.workerVirtualTaskRunTime =
+        workerVirtualTaskRunTime +
+        this.getWorkerTaskRunTime(
+          this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+        )
+    } else {
+      this.nextWorkerNodeKey =
+        this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
+          ? 0
+          : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
+      this.workerVirtualTaskRunTime = 0
     }
-    return Math.round(cpusCycleTimeWeight / cpus().length)
+    return this.nextWorkerNodeKey
   }
 }