From baca80f7921f47ead05d194fb782a3e310f4ea41 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 31 Aug 2023 14:15:18 +0200 Subject: [PATCH] fix: do not pre-choose in WRR strategy MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../abstract-worker-choice-strategy.ts | 16 ++++++++++++---- .../fair-share-worker-choice-strategy.ts | 3 ++- ...eighted-round-robin-worker-choice-strategy.ts | 3 +-- .../least-busy-worker-choice-strategy.ts | 3 ++- .../least-elu-worker-choice-strategy.ts | 3 ++- .../least-used-worker-choice-strategy.ts | 3 ++- .../round-robin-worker-choice-strategy.ts | 1 + ...eighted-round-robin-worker-choice-strategy.ts | 5 ++--- 8 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index cd8736c2..ca3d9b9d 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -194,18 +194,26 @@ export abstract class AbstractWorkerChoiceStrategy< : this.pool.workerNodes[workerNodeKey].usage.elu.active?.average ?? 0 } + /** + * Sets safely the previous worker node key. + * + * @param workerNodeKey - The worker node key. + */ + protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void { + this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey + } + /** * Check the next worker node eligibility. * - * @param chosenNextWorkerNodeKey - The chosen next worker node key. + * @param previousWorkerNodeKey - The previous worker node key. */ protected checkNextWorkerNodeEligibility ( - chosenNextWorkerNodeKey: number | undefined + previousWorkerNodeKey: number | undefined ): void { if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) { this.nextWorkerNodeKey = undefined - this.previousWorkerNodeKey = - chosenNextWorkerNodeKey ?? this.previousWorkerNodeKey + this.setPreviousWorkerNodeKey(previousWorkerNodeKey) } } diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index f293bbb7..c4a0d30b 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -70,6 +70,7 @@ export class FairShareWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) this.nextWorkerNodeKey = this.fairShareNextWorkerNodeKey() return this.nextWorkerNodeKey } @@ -81,8 +82,8 @@ export class FairShareWorkerChoiceStrategy< } private fairShareNextWorkerNodeKey (): number | undefined { - let minWorkerVirtualTaskEndTimestamp = Infinity let chosenWorkerNodeKey: number | undefined + let minWorkerVirtualTaskEndTimestamp = Infinity for (const [workerNodeKey] of this.pool.workerNodes.entries()) { if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) { this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey) 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 2e82ac3d..0b32bd45 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 @@ -111,8 +111,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< this.workerVirtualTaskRunTime = this.workerVirtualTaskRunTime + this.getWorkerTaskRunTime(workerNodeKey) - this.previousWorkerNodeKey = - this.nextWorkerNodeKey ?? this.previousWorkerNodeKey + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) this.nextWorkerNodeKey = workerNodeKey return this.nextWorkerNodeKey } diff --git a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts index de71167e..0e8fdef3 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -61,6 +61,7 @@ export class LeastBusyWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey() return this.nextWorkerNodeKey } @@ -71,8 +72,8 @@ export class LeastBusyWorkerChoiceStrategy< } private leastBusyNextWorkerNodeKey (): number | undefined { - let minTime = Infinity let chosenWorkerNodeKey: number | undefined + let minTime = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerTime = (workerNode.usage.runTime?.aggregate ?? 0) + diff --git a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts index 3ec23a1c..2624e5b2 100644 --- a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts @@ -57,6 +57,7 @@ export class LeastEluWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) this.nextWorkerNodeKey = this.leastEluNextWorkerNodeKey() return this.nextWorkerNodeKey } @@ -67,8 +68,8 @@ export class LeastEluWorkerChoiceStrategy< } private leastEluNextWorkerNodeKey (): number | undefined { - let minWorkerElu = Infinity let chosenWorkerNodeKey: number | undefined + let minWorkerElu = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerUsage = workerNode.usage const workerElu = workerUsage.elu?.active?.aggregate ?? 0 diff --git a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts index c750c290..d20c3217 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -42,6 +42,7 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey() return this.nextWorkerNodeKey } @@ -52,8 +53,8 @@ export class LeastUsedWorkerChoiceStrategy< } private leastUsedNextWorkerNodeKey (): number | undefined { - let minNumberOfTasks = Infinity let chosenWorkerNodeKey: number | undefined + let minNumberOfTasks = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerTaskStatistics = workerNode.usage.tasks const workerTasks = 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 8a5656b8..6884a5d2 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -44,6 +44,7 @@ export class RoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { const chosenWorkerNodeKey = this.nextWorkerNodeKey + this.setPreviousWorkerNodeKey(chosenWorkerNodeKey) this.roundRobinNextWorkerNodeKey() return chosenWorkerNodeKey } 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 3ac941f0..42952fa2 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 @@ -70,9 +70,8 @@ export class WeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number | undefined { - const chosenWorkerNodeKey = this.nextWorkerNodeKey - this.weightedRoundRobinNextWorkerNodeKey() - return chosenWorkerNodeKey + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) + return this.weightedRoundRobinNextWorkerNodeKey() } /** @inheritDoc */ -- 2.34.1