X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fweighted-round-robin-worker-choice-strategy.ts;h=fc6f48cf2632a89e0fa1de1afe198ff37eb2cdb7;hb=2c039e4373e86714cdf27e77440b12ee8eb2e4db;hp=1ce6041920cfa634071093561eabc32e405272d2;hpb=5df69fabd77b3ec4137a6382e2d84791bf0fe85d;p=poolifier.git 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 1ce60419..fc6f48cf 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 @@ -4,6 +4,7 @@ import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, + StrategyPolicy, TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -23,6 +24,11 @@ export class WeightedRoundRobinWorkerChoiceStrategy< > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { + /** @inheritDoc */ + public readonly strategyPolicy: StrategyPolicy = { + useDynamicWorker: true + } + /** @inheritDoc */ public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { runTime: { @@ -42,10 +48,6 @@ export class WeightedRoundRobinWorkerChoiceStrategy< } } - /** - * Worker node id where the current task will be submitted. - */ - private currentWorkerNodeId: number = 0 /** * Default worker weight. */ @@ -67,7 +69,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public reset (): boolean { - this.currentWorkerNodeId = 0 + this.nextWorkerNodeId = 0 this.workerVirtualTaskRunTime = 0 return true } @@ -79,7 +81,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - const chosenWorkerNodeKey = this.currentWorkerNodeId + const chosenWorkerNodeKey = this.nextWorkerNodeId const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime const workerWeight = this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight @@ -88,10 +90,10 @@ export class WeightedRoundRobinWorkerChoiceStrategy< workerVirtualTaskRunTime + this.getWorkerTaskRunTime(chosenWorkerNodeKey) } else { - this.currentWorkerNodeId = - this.currentWorkerNodeId === this.pool.workerNodes.length - 1 + this.nextWorkerNodeId = + this.nextWorkerNodeId === this.pool.workerNodes.length - 1 ? 0 - : this.currentWorkerNodeId + 1 + : this.nextWorkerNodeId + 1 this.workerVirtualTaskRunTime = 0 } return chosenWorkerNodeKey @@ -99,11 +101,11 @@ export class WeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public remove (workerNodeKey: number): boolean { - if (this.currentWorkerNodeId === workerNodeKey) { + if (this.nextWorkerNodeId === workerNodeKey) { if (this.pool.workerNodes.length === 0) { - this.currentWorkerNodeId = 0 - } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) { - this.currentWorkerNodeId = this.pool.workerNodes.length - 1 + this.nextWorkerNodeId = 0 + } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) { + this.nextWorkerNodeId = this.pool.workerNodes.length - 1 } this.workerVirtualTaskRunTime = 0 }