refactor: silence sonar
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index 01beb340dad7c59ac0b361f576382a66e42e1b9a..cc38d196941e4611af2235a84899cd82ab55c9f0 100644 (file)
@@ -7,6 +7,7 @@ import type { IPool } from '../pool'
 import type { IWorker } from '../worker'
 import type {
   IWorkerChoiceStrategy,
+  MeasurementStatisticsRequirements,
   StrategyPolicy,
   TaskStatisticsRequirements,
   WorkerChoiceStrategyOptions
@@ -27,11 +28,17 @@ export abstract class AbstractWorkerChoiceStrategy<
   /**
    * The next worker node key.
    */
-  protected nextWorkerNodeKey: number = 0
+  protected nextWorkerNodeKey: number | undefined = 0
+
+  /**
+   * The previous worker node key.
+   */
+  protected previousWorkerNodeKey: number = 0
 
   /** @inheritDoc */
   public readonly strategyPolicy: StrategyPolicy = {
-    useDynamicWorker: false
+    dynamicWorkerUsage: false,
+    dynamicWorkerReady: true
   }
 
   /** @inheritDoc */
@@ -51,57 +58,38 @@ export abstract class AbstractWorkerChoiceStrategy<
     protected readonly pool: IPool<Worker, Data, Response>,
     protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
+    this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts }
     this.choose = this.choose.bind(this)
   }
 
   protected setTaskStatisticsRequirements (
     opts: WorkerChoiceStrategyOptions
   ): void {
-    if (
-      this.taskStatisticsRequirements.runTime.average &&
-      opts.runTime?.median === true
-    ) {
-      this.taskStatisticsRequirements.runTime.average = false
-      this.taskStatisticsRequirements.runTime.median = opts.runTime
-        .median as boolean
-    }
-    if (
-      this.taskStatisticsRequirements.runTime.median &&
-      opts.runTime?.median === false
-    ) {
-      this.taskStatisticsRequirements.runTime.average = true
-      this.taskStatisticsRequirements.runTime.median = opts.runTime
-        .median as boolean
-    }
-    if (
-      this.taskStatisticsRequirements.waitTime.average &&
-      opts.waitTime?.median === true
-    ) {
-      this.taskStatisticsRequirements.waitTime.average = false
-      this.taskStatisticsRequirements.waitTime.median = opts.waitTime
-        .median as boolean
-    }
-    if (
-      this.taskStatisticsRequirements.waitTime.median &&
-      opts.waitTime?.median === false
-    ) {
-      this.taskStatisticsRequirements.waitTime.average = true
-      this.taskStatisticsRequirements.waitTime.median = opts.waitTime
-        .median as boolean
-    }
-    if (
-      this.taskStatisticsRequirements.elu.average &&
-      opts.elu?.median === true
-    ) {
-      this.taskStatisticsRequirements.elu.average = false
-      this.taskStatisticsRequirements.elu.median = opts.elu.median as boolean
+    this.toggleMedianMeasurementStatisticsRequirements(
+      this.taskStatisticsRequirements.runTime,
+      opts.runTime?.median as boolean
+    )
+    this.toggleMedianMeasurementStatisticsRequirements(
+      this.taskStatisticsRequirements.waitTime,
+      opts.waitTime?.median as boolean
+    )
+    this.toggleMedianMeasurementStatisticsRequirements(
+      this.taskStatisticsRequirements.elu,
+      opts.elu?.median as boolean
+    )
+  }
+
+  private toggleMedianMeasurementStatisticsRequirements (
+    measurementStatisticsRequirements: MeasurementStatisticsRequirements,
+    toggleMedian: boolean
+  ): void {
+    if (measurementStatisticsRequirements.average && toggleMedian) {
+      measurementStatisticsRequirements.average = false
+      measurementStatisticsRequirements.median = toggleMedian
     }
-    if (
-      this.taskStatisticsRequirements.elu.median &&
-      opts.elu?.median === false
-    ) {
-      this.taskStatisticsRequirements.elu.average = true
-      this.taskStatisticsRequirements.elu.median = opts.elu.median as boolean
+    if (measurementStatisticsRequirements.median && !toggleMedian) {
+      measurementStatisticsRequirements.average = true
+      measurementStatisticsRequirements.median = toggleMedian
     }
   }
 
@@ -112,21 +100,53 @@ export abstract class AbstractWorkerChoiceStrategy<
   public abstract update (workerNodeKey: number): boolean
 
   /** @inheritDoc */
-  public abstract choose (): number
+  public abstract choose (): number | undefined
 
   /** @inheritDoc */
   public abstract remove (workerNodeKey: number): boolean
 
   /** @inheritDoc */
   public setOptions (opts: WorkerChoiceStrategyOptions): void {
-    this.opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+    this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts }
     this.setTaskStatisticsRequirements(this.opts)
   }
 
-  protected isWorkerNodeReady (workerNodeKey: number): boolean {
+  /**
+   * Whether the worker node is ready or not.
+   *
+   * @param workerNodeKey - The worker node key.
+   * @returns Whether the worker node is ready or not.
+   */
+  private isWorkerNodeReady (workerNodeKey: number): boolean {
     return this.pool.workerNodes[workerNodeKey].info.ready
   }
 
+  /**
+   * Whether the worker node has back pressure or not (i.e. its tasks queue is full).
+   *
+   * @param workerNodeKey - The worker node key.
+   * @returns `true` if the worker node has back pressure, `false` otherwise.
+   */
+  private hasWorkerNodeBackPressure (workerNodeKey: number): boolean {
+    return this.pool.hasWorkerNodeBackPressure(workerNodeKey)
+  }
+
+  /**
+   * Whether the worker node is eligible or not.
+   * A worker node is eligible if it is ready and does not have back pressure.
+   *
+   * @param workerNodeKey - The worker node key.
+   * @returns `true` if the worker node is eligible, `false` otherwise.
+   * @see {@link isWorkerNodeReady}
+   * @see {@link hasWorkerNodeBackPressure}
+   */
+  protected isWorkerNodeEligible (workerNodeKey: number): boolean {
+    return (
+      this.isWorkerNodeReady(workerNodeKey) &&
+      !this.hasWorkerNodeBackPressure(workerNodeKey)
+    )
+  }
+
   /**
    * Gets the worker task runtime.
    * If the task statistics require the average runtime, the average runtime is returned.
@@ -169,6 +189,31 @@ export abstract class AbstractWorkerChoiceStrategy<
       : this.pool.workerNodes[workerNodeKey].usage.elu.active?.average ?? 0
   }
 
+  /**
+   * Assign to nextWorkerNodeKey property the chosen worker node key.
+   *
+   * @param chosenWorkerNodeKey - The chosen worker node key.
+   */
+  protected assignChosenWorkerNodeKey (
+    chosenWorkerNodeKey: number | undefined
+  ): void {
+    if (chosenWorkerNodeKey != null) {
+      this.nextWorkerNodeKey = chosenWorkerNodeKey
+    } else {
+      this.nextWorkerNodeKey = undefined
+    }
+  }
+
+  protected checkNextWorkerNodeEligibility (
+    chosenWorkerNodeKey: number | undefined
+  ): void {
+    if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
+      this.nextWorkerNodeKey = undefined
+      this.previousWorkerNodeKey =
+        chosenWorkerNodeKey ?? this.previousWorkerNodeKey
+    }
+  }
+
   protected computeDefaultWorkerWeight (): number {
     let cpusCycleTimeWeight = 0
     for (const cpu of cpus()) {