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=1ea091c6f712be7be7472534692581fd3b56de0f;hpb=a3ae469eebfcc990a251133beb01378599313233;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 1ea091c6..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,7 +4,8 @@ import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - RequiredStatistics, + StrategyPolicy, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -24,20 +25,29 @@ export class WeightedRoundRobinWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly requiredStatistics: RequiredStatistics = { - runTime: true, - avgRunTime: true, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, - elu: false + public readonly strategyPolicy: StrategyPolicy = { + useDynamicWorker: true + } + + /** @inheritDoc */ + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: { + aggregate: false, + average: false, + median: false + }, + elu: { + aggregate: false, + average: false, + median: false + } } - /** - * Worker node id where the current task will be submitted. - */ - private currentWorkerNodeId: number = 0 /** * Default worker weight. */ @@ -53,13 +63,13 @@ export class WeightedRoundRobinWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setRequiredStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) this.defaultWorkerWeight = this.computeDefaultWorkerWeight() } /** @inheritDoc */ public reset (): boolean { - this.currentWorkerNodeId = 0 + this.nextWorkerNodeId = 0 this.workerVirtualTaskRunTime = 0 return true } @@ -71,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 @@ -80,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 @@ -91,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 }