Fix strategies internals reset
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 13 Oct 2022 18:57:25 +0000 (20:57 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 13 Oct 2022 18:57:25 +0000 (20:57 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts
src/pools/selection-strategies/fair-share-worker-choice-strategy.ts
src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts
src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/selection-strategies-types.ts
src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/worker-choice-strategy-context.ts
tests/pools/selection-strategies/selection-strategies.test.js

index d624bdd2f0128079d68eef6ee16d888d8373031f..3ba59b42fb0b4b8233edcca1139fc4c6b3f69350 100644 (file)
@@ -35,7 +35,7 @@ export abstract class AbstractWorkerChoiceStrategy<
   ) {}
 
   /** @inheritDoc */
-  public abstract resetStatistics (): boolean
+  public abstract reset (): boolean
 
   /** @inheritDoc */
   public abstract choose (): Worker
index 8d8b8518a4bd110d6453261d83ac8dd932254308..81f3df519fecffcd7fa5aaec04a7e3e4cdc14689 100644 (file)
@@ -43,8 +43,8 @@ export class DynamicPoolWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public resetStatistics (): boolean {
-    return this.workerChoiceStrategy.resetStatistics()
+  public reset (): boolean {
+    return this.workerChoiceStrategy.reset()
   }
 
   /** @inheritDoc */
index ee9351ec0b1011615a2f7489d83b5931a8ff8673..3735f9722319c34656d3498389962859ffd2f060 100644 (file)
@@ -37,7 +37,7 @@ export class FairShareWorkerChoiceStrategy<
   > = new Map<Worker, WorkerVirtualTaskTimestamp>()
 
   /** @inheritDoc */
-  public resetStatistics (): boolean {
+  public reset (): boolean {
     this.workerLastVirtualTaskTimestamp.clear()
     return true
   }
index 0e2a2bf473b64cafa25051fbf13e603e5ab71701..342ca6ebf10fd46274b8f2a78deecd3f1c81be2b 100644 (file)
@@ -14,7 +14,7 @@ export class LessRecentlyUsedWorkerChoiceStrategy<
   Response
 > extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
   /** @inheritDoc */
-  public resetStatistics (): boolean {
+  public reset (): boolean {
     return true
   }
 
index 082c7d21a6b988f18ff8768fdd0868c357205d9e..0a9fbb7b0a386e1f2ec2f7a78c8f0bf61a9d8572 100644 (file)
@@ -19,7 +19,8 @@ export class RoundRobinWorkerChoiceStrategy<
   private nextWorkerIndex: number = 0
 
   /** @inheritDoc */
-  public resetStatistics (): boolean {
+  public reset (): boolean {
+    this.nextWorkerIndex = 0
     return true
   }
 
index f875c2906ee3a173ffab6790d93aece511b2a0ef..499b8e489e744a3fad453391b8e4e664c90eac94 100644 (file)
@@ -49,9 +49,9 @@ export interface IWorkerChoiceStrategy<Worker extends IPoolWorker> {
    */
   readonly requiredStatistics: RequiredStatistics
   /**
-   * Resets strategy internal statistics.
+   * Resets strategy internals (counters, statistics, etc.).
    */
-  resetStatistics(): boolean
+  reset(): boolean
   /**
    * Chooses a worker in the pool.
    */
index 925f318c6cd24b124068c20791ca81c3a494e6f1..752ae93317ae436dfaab13a55bee86930c0e130a 100644 (file)
@@ -62,7 +62,9 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
   }
 
   /** @inheritDoc */
-  public resetStatistics (): boolean {
+  public reset (): boolean {
+    this.previousWorkerIndex = 0
+    this.currentWorkerIndex = 0
     this.workersTaskRunTime.clear()
     this.initWorkersTaskRunTime()
     return true
index 491444755aafd5076663d14b0c22f6cd79eaf716..836a1cb5052c5f2219e9e872b3a979684b0336af 100644 (file)
@@ -77,7 +77,7 @@ export class WorkerChoiceStrategyContext<
   public setWorkerChoiceStrategy (
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
-    this.workerChoiceStrategy?.resetStatistics()
+    this.workerChoiceStrategy?.reset()
     this.workerChoiceStrategy = this.getPoolWorkerChoiceStrategy(
       workerChoiceStrategy
     )
index faa28a296b844c050b96706f090165f9676609e6..d9a01684e9b5b54c85675d3eb9f7573df0b1ea0d 100644 (file)
@@ -125,6 +125,32 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
+    let pool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex
+    ).toBe(0)
+    await pool.destroy()
+    pool = new DynamicThreadPool(
+      min,
+      max,
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .workerChoiceStrategy.nextWorkerIndex
+    ).toBe(0)
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
+
   it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
     const pool = new FixedThreadPool(
       max,
@@ -307,7 +333,7 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify FAIR_SHARE strategy statistics are resets after setting it', async () => {
+  it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
     let pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js'
@@ -470,7 +496,7 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify WEIGHTED_ROUND_ROBIN strategy statistics are resets after setting it', async () => {
+  it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
     let pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js'
@@ -480,6 +506,18 @@ describe('Selection strategies test suite', () => {
         .workersTaskRunTime
     ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .previousWorkerIndex
+    ).toBe(0)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .currentWorkerIndex
+    ).toBe(0)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .defaultWorkerWeight
+    ).toBeGreaterThan(0)
     for (const worker of pool.workerChoiceStrategyContext
       .getWorkerChoiceStrategy()
       .workersTaskRunTime.keys()) {
@@ -500,6 +538,18 @@ describe('Selection strategies test suite', () => {
         .workerChoiceStrategy.workersTaskRunTime
     ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .workerChoiceStrategy.previousWorkerIndex
+    ).toBe(0)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .workerChoiceStrategy.currentWorkerIndex
+    ).toBe(0)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .workerChoiceStrategy.defaultWorkerWeight
+    ).toBeGreaterThan(0)
     for (const worker of pool.workerChoiceStrategyContext
       .getWorkerChoiceStrategy()
       .workerChoiceStrategy.workersTaskRunTime.keys()) {