From: Jérôme Benoit Date: Sat, 19 Aug 2023 19:04:40 +0000 (+0200) Subject: fix: some more edges cases in the worker choice strategy retries code X-Git-Tag: v2.6.31~19 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7c7bb28936c5de8829dba0794760498077d3ea90;p=poolifier.git fix: some more edges cases in the worker choice strategy retries code Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 4ed7dcae..d3e7e385 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -30,6 +30,11 @@ export abstract class AbstractWorkerChoiceStrategy< */ protected nextWorkerNodeKey: number | undefined = 0 + /** + * The previous worker node key. + */ + protected previousWorkerNodeKey: number = 0 + /** @inheritDoc */ public readonly strategyPolicy: StrategyPolicy = { dynamicWorkerUsage: false, diff --git a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts index cdbfbee5..e5370428 100644 --- a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts @@ -67,15 +67,17 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { - let roundId: number | undefined + let roundId: number = this.roundId let workerNodeId: number | undefined for ( let roundIndex = this.roundId; roundIndex < this.roundWeights.length; roundIndex++ ) { + roundId = roundIndex for ( - let workerNodeKey = this.nextWorkerNodeKey ?? 0; + let workerNodeKey = + this.nextWorkerNodeKey ?? this.previousWorkerNodeKey; workerNodeKey < this.pool.workerNodes.length; workerNodeKey++ ) { @@ -85,13 +87,16 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< this.isWorkerNodeEligible(workerNodeKey) && workerWeight >= this.roundWeights[roundIndex] ) { - roundId = roundIndex workerNodeId = workerNodeKey break } } } - this.roundId = roundId as number + this.roundId = roundId + if (workerNodeId == null) { + this.previousWorkerNodeKey = + this.nextWorkerNodeKey ?? this.previousWorkerNodeKey + } this.nextWorkerNodeKey = workerNodeId const chosenWorkerNodeKey = this.nextWorkerNodeKey if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) { @@ -99,7 +104,8 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< this.roundId = this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1 } else { - this.nextWorkerNodeKey = (this.nextWorkerNodeKey ?? 0) + 1 + this.nextWorkerNodeKey = + (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 } return chosenWorkerNodeKey } diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index 41bc4085..b6d222f5 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -54,6 +54,8 @@ export class RoundRobinWorkerChoiceStrategy< this.roundRobinNextWorkerNodeKey() if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) { this.nextWorkerNodeKey = undefined + this.previousWorkerNodeKey = + chosenWorkerNodeKey ?? this.previousWorkerNodeKey } return chosenWorkerNodeKey } @@ -74,7 +76,7 @@ export class RoundRobinWorkerChoiceStrategy< this.nextWorkerNodeKey = this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 ? 0 - : (this.nextWorkerNodeKey ?? 0) + 1 + : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 return this.nextWorkerNodeKey } } diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index 1aab3d1c..701e5165 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -81,6 +81,8 @@ export class WeightedRoundRobinWorkerChoiceStrategy< this.weightedRoundRobinNextWorkerNodeKey() if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) { this.nextWorkerNodeKey = undefined + this.previousWorkerNodeKey = + chosenWorkerNodeKey ?? this.previousWorkerNodeKey } return chosenWorkerNodeKey } @@ -101,17 +103,20 @@ export class WeightedRoundRobinWorkerChoiceStrategy< private weightedRoundRobinNextWorkerNodeKey (): number | undefined { const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime const workerWeight = - this.opts.weights?.[this.nextWorkerNodeKey ?? 0] ?? - this.defaultWorkerWeight + this.opts.weights?.[ + this.nextWorkerNodeKey ?? this.previousWorkerNodeKey + ] ?? this.defaultWorkerWeight if (workerVirtualTaskRunTime < workerWeight) { this.workerVirtualTaskRunTime = workerVirtualTaskRunTime + - this.getWorkerTaskRunTime(this.nextWorkerNodeKey ?? 0) + this.getWorkerTaskRunTime( + this.nextWorkerNodeKey ?? this.previousWorkerNodeKey + ) } else { this.nextWorkerNodeKey = this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 ? 0 - : (this.nextWorkerNodeKey ?? 0) + 1 + : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 this.workerVirtualTaskRunTime = 0 } return this.nextWorkerNodeKey