fix: ensure the number of worker choice retries is enough for WRR
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 19 Dec 2023 22:47:23 +0000 (23:47 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 19 Dec 2023 22:47:23 +0000 (23:47 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/worker-choice-strategy-context.ts
src/utils.ts
tests/pools/abstract-pool.test.mjs

index 34473ab4e2a3849bc96c6ac02bbe39bbbce3a47a..7324c511cca18f76b89c254f1deb777ae06c2dbe 100644 (file)
@@ -113,7 +113,10 @@ export abstract class AbstractWorkerChoiceStrategy<
   /** @inheritDoc */
   public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
     this.opts = {
-      ...getDefaultInternalWorkerChoiceStrategyOptions(this.pool.info.maxSize),
+      ...getDefaultInternalWorkerChoiceStrategyOptions(
+        this.pool.info.maxSize +
+          Object.keys((opts?.weights as Record<number, number>) ?? {}).length
+      ),
       ...opts
     }
     this.setTaskStatisticsRequirements(this.opts)
index 1de78aba9f15f1ca2cddb1cdac474c8470e7991f..cf1b005169d8fbaa04fb074adb4f3f2966cbe335 100644 (file)
@@ -47,7 +47,11 @@ export class WorkerChoiceStrategyContext<
     private opts?: InternalWorkerChoiceStrategyOptions
   ) {
     this.opts = {
-      ...getDefaultInternalWorkerChoiceStrategyOptions(pool.info.maxSize),
+      ...getDefaultInternalWorkerChoiceStrategyOptions(
+        pool.info.maxSize +
+          Object.keys((this.opts?.weights as Record<number, number>) ?? {})
+            .length
+      ),
       ...this.opts
     }
     this.execute = this.execute.bind(this)
@@ -232,7 +236,10 @@ export class WorkerChoiceStrategyContext<
     opts?: InternalWorkerChoiceStrategyOptions
   ): void {
     this.opts = {
-      ...getDefaultInternalWorkerChoiceStrategyOptions(pool.info.maxSize),
+      ...getDefaultInternalWorkerChoiceStrategyOptions(
+        pool.info.maxSize +
+          Object.keys((opts?.weights as Record<number, number>) ?? {}).length
+      ),
       ...opts
     }
     for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) {
index 25233a022aac69f9db858907261ad6720e5e495e..2dd3947c2f42bf081473dc08ccb34bf4cf30e4b1 100644 (file)
@@ -22,16 +22,16 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => {
 })
 
 /**
- * Default worker choice strategy options.
+ * Gets default worker choice strategy options.
  *
- * @param poolMaxSize - The pool maximum size.
+ * @param retries - The number of worker choice retries.
  * @returns The default worker choice strategy options.
  */
 export const getDefaultInternalWorkerChoiceStrategyOptions = (
-  poolMaxSize: number
+  retries: number
 ): InternalWorkerChoiceStrategyOptions => {
   return {
-    retries: poolMaxSize,
+    retries,
     runTime: { median: false },
     waitTime: { median: false },
     elu: { median: false }
index 44aca3993c7d94d9f52185e553333db2316a8a71..29f038c77b1e6e614b9ab9f15ee9cdbc1511cf7f 100644 (file)
@@ -288,7 +288,9 @@ describe('Abstract pool test suite', () => {
       exitHandler: testHandler
     })
     expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
-      retries: pool.info.maxSize,
+      retries:
+        pool.info.maxSize +
+        Object.keys(pool.opts.workerChoiceStrategyOptions.weights).length,
       runTime: { median: true },
       waitTime: { median: false },
       elu: { median: false },
@@ -297,7 +299,9 @@ describe('Abstract pool test suite', () => {
     for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({
-        retries: pool.info.maxSize,
+        retries:
+          pool.info.maxSize +
+          Object.keys(pool.opts.workerChoiceStrategyOptions.weights).length,
         runTime: { median: true },
         waitTime: { median: false },
         elu: { median: false },