WRR: Fix worker choice initial runtime value on each round
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
index f8f1821269144ed59f7b33b4cb21ab27e2ea7eeb..48f213ffebb1a4577debff4e701ef45f53e2ebed 100644 (file)
@@ -1,4 +1,4 @@
-import type { AbstractPoolWorker } from '../abstract-pool-worker'
+import type { IPoolWorker } from '../pool-worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
 import type { RequiredStatistics } from './selection-strategies-types'
 
@@ -19,29 +19,35 @@ type WorkerVirtualTaskTimestamp = {
  * @template Response Type of response of execution. This can only be serializable data.
  */
 export class FairShareWorkerChoiceStrategy<
-  Worker extends AbstractPoolWorker,
+  Worker extends IPoolWorker,
   Data,
   Response
 > extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
   /** @inheritDoc */
-  public requiredStatistics: RequiredStatistics = {
+  public readonly requiredStatistics: RequiredStatistics = {
     runTime: true
   }
 
   /**
    *  Worker last virtual task execution timestamp.
    */
-  private workerLastVirtualTaskTimestamp: Map<
+  private readonly workerLastVirtualTaskTimestamp: Map<
     Worker,
     WorkerVirtualTaskTimestamp
   > = new Map<Worker, WorkerVirtualTaskTimestamp>()
 
+  /** @inheritDoc */
+  public reset (): boolean {
+    this.workerLastVirtualTaskTimestamp.clear()
+    return true
+  }
+
   /** @inheritDoc */
   public choose (): Worker {
-    this.updateWorkerLastVirtualTaskTimestamp()
     let minWorkerVirtualTaskEndTimestamp = Infinity
     let chosenWorker!: Worker
     for (const worker of this.pool.workers) {
+      this.computeWorkerLastVirtualTaskTimestamp(worker)
       const workerLastVirtualTaskEndTimestamp =
         this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? 0
       if (
@@ -55,21 +61,21 @@ export class FairShareWorkerChoiceStrategy<
   }
 
   /**
-   * Compute workers last virtual task timestamp.
+   * Computes worker last virtual task timestamp.
+   *
+   * @param worker The worker.
    */
-  private updateWorkerLastVirtualTaskTimestamp () {
-    for (const worker of this.pool.workers) {
-      const workerVirtualTaskStartTimestamp = Math.max(
-        Date.now(),
-        this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? 0
-      )
-      const workerVirtualTaskEndTimestamp =
-        workerVirtualTaskStartTimestamp +
-        (this.pool.getWorkerAverageTasksRunTime(worker) ?? 0)
-      this.workerLastVirtualTaskTimestamp.set(worker, {
-        start: workerVirtualTaskStartTimestamp,
-        end: workerVirtualTaskEndTimestamp
-      })
-    }
+  private computeWorkerLastVirtualTaskTimestamp (worker: Worker): void {
+    const workerVirtualTaskStartTimestamp = Math.max(
+      Date.now(),
+      this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? -Infinity
+    )
+    const workerVirtualTaskEndTimestamp =
+      workerVirtualTaskStartTimestamp +
+      (this.pool.getWorkerAverageTasksRunTime(worker) ?? 0)
+    this.workerLastVirtualTaskTimestamp.set(worker, {
+      start: workerVirtualTaskStartTimestamp,
+      end: workerVirtualTaskEndTimestamp
+    })
   }
 }