refactor: conditionally reset strategy internals
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Apr 2023 17:36:58 +0000 (19:36 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Apr 2023 17:36:58 +0000 (19:36 +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/fair-share-worker-choice-strategy.ts
src/pools/selection-strategies/less-busy-worker-choice-strategy.ts
src/pools/selection-strategies/less-used-worker-choice-strategy.ts
src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/selection-strategies-utils.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/worker-choice-strategy-context.test.js

index 44e7e3f8b5ea01b7c707e0754f580b9181ea9a4b..5b4a2fb5f777b4f8b5238a0be7b578dd9b3924ce 100644 (file)
@@ -15,8 +15,8 @@ import type {
  */
 export abstract class AbstractWorkerChoiceStrategy<
   Worker extends IPoolWorker,
-  Data,
-  Response
+  Data = unknown,
+  Response = unknown
 > implements IWorkerChoiceStrategy<Worker, Data, Response> {
   /** {@inheritDoc} */
   public readonly isDynamicPool: boolean
index 1fe68ffae7a92b2397b22050bf12266143168540..60c22a299df6ee2825dc63a79e9c405fe72d3312 100644 (file)
@@ -23,8 +23,8 @@ interface WorkerVirtualTaskTimestamp {
  */
 export class FairShareWorkerChoiceStrategy<
     Worker extends IPoolWorker,
-    Data,
-    Response
+    Data = unknown,
+    Response = unknown
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy<Worker, Data, Response> {
index bddae82de6f7c6f4e4dbed953b5496430e2f071a..9952a41df80fc191fcbcc829d5389f7f720ba02c 100644 (file)
@@ -14,8 +14,8 @@ import type {
  */
 export class LessBusyWorkerChoiceStrategy<
     Worker extends IPoolWorker,
-    Data,
-    Response
+    Data = unknown,
+    Response = unknown
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy<Worker, Data, Response> {
index f0632db86bbda11a8945d6670a965d621f04a673..01332caf7f41cf086596c325173ac639f7b7cb39 100644 (file)
@@ -11,8 +11,8 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types'
  */
 export class LessUsedWorkerChoiceStrategy<
     Worker extends IPoolWorker,
-    Data,
-    Response
+    Data = unknown,
+    Response = unknown
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy<Worker, Data, Response> {
index 5ee219202aff6c7ec44f1af67b1264cdd16e8b40..9b49d7643581d8a4991bc1b25239b2053e0265d2 100644 (file)
@@ -11,8 +11,8 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types'
  */
 export class RoundRobinWorkerChoiceStrategy<
     Worker extends IPoolWorker,
-    Data,
-    Response
+    Data = unknown,
+    Response = unknown
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy<Worker, Data, Response> {
index 3db6456603b44b7bf3508a2bd375687f4a4d7e04..b0967d094ad9b5f1fb2957552e6628742fad2935 100644 (file)
@@ -20,8 +20,8 @@ import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-w
  */
 export function getWorkerChoiceStrategy<
   Worker extends IPoolWorker,
-  Data,
-  Response
+  Data = unknown,
+  Response = unknown
 > (
   pool: IPoolInternal<Worker, Data, Response>,
   workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
index a8848f14ee80570f7c2d9e294d9d3552cfec3757..c700c42a857a94962c40e6311f580781cb94fc30 100644 (file)
@@ -25,8 +25,8 @@ interface TaskRunTime {
  */
 export class WeightedRoundRobinWorkerChoiceStrategy<
     Worker extends IPoolWorker,
-    Data,
-    Response
+    Data = unknown,
+    Response = unknown
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
   implements IWorkerChoiceStrategy<Worker, Data, Response> {
index 41b515b816d00dc991c27d8143efe80ec7c6116a..605cce38707bb47754d5dabec4fbca72ceaa95a0 100644 (file)
@@ -17,8 +17,8 @@ import { getWorkerChoiceStrategy } from './selection-strategies-utils'
  */
 export class WorkerChoiceStrategyContext<
   Worker extends IPoolWorker,
-  Data,
-  Response
+  Data = unknown,
+  Response = unknown
 > {
   private workerChoiceStrategy: IWorkerChoiceStrategy<Worker, Data, Response>
 
@@ -32,12 +32,12 @@ export class WorkerChoiceStrategyContext<
   public constructor (
     pool: IPoolInternal<Worker, Data, Response>,
     private readonly createWorkerCallback: () => number,
-    workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
+    private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
   ) {
     this.execute.bind(this)
     this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
       pool,
-      workerChoiceStrategy
+      workerChoiceStrategyType
     )
   }
 
@@ -59,11 +59,16 @@ export class WorkerChoiceStrategyContext<
     pool: IPoolInternal<Worker, Data, Response>,
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
-    this.workerChoiceStrategy?.reset()
-    this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
-      pool,
-      workerChoiceStrategy
-    )
+    if (this.workerChoiceStrategyType === workerChoiceStrategy) {
+      this.workerChoiceStrategy?.reset()
+    } else {
+      this.workerChoiceStrategyType = workerChoiceStrategy
+      this.workerChoiceStrategy = getWorkerChoiceStrategy<
+      Worker,
+      Data,
+      Response
+      >(pool, this.workerChoiceStrategyType)
+    }
   }
 
   /**
index d1cad6a95ee8977c0fbeff958e2ab6f7a1d10c94..bcd84208974d2a6e837851e5f774cb5833b38bce 100644 (file)
@@ -90,6 +90,12 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
+      RoundRobinWorkerChoiceStrategy
+    )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
       fixedPool,
       WorkerChoiceStrategies.ROUND_ROBIN
@@ -97,12 +103,21 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       RoundRobinWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
+      RoundRobinWorkerChoiceStrategy
+    )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
       dynamicPool,
       WorkerChoiceStrategies.ROUND_ROBIN
@@ -110,6 +125,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       RoundRobinWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
@@ -123,6 +141,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       LessUsedWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.LESS_USED
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
@@ -136,6 +157,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       LessUsedWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.LESS_USED
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
@@ -149,6 +173,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       LessBusyWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.LESS_BUSY
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
@@ -162,6 +189,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       LessBusyWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.LESS_BUSY
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
@@ -175,6 +205,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       FairShareWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.FAIR_SHARE
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
@@ -188,6 +221,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       FairShareWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.FAIR_SHARE
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
@@ -201,6 +237,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       WeightedRoundRobinWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+    )
   })
 
   it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
@@ -214,5 +253,8 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       WeightedRoundRobinWorkerChoiceStrategy
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+    )
   })
 })