X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Finterleaved-weighted-round-robin-worker-choice-strategy.ts;h=3942bb6ed99961f986e1286ab20b31baf2a00eb5;hb=8166b5b31ebb2d15bbdc112f1a86d3774fe21294;hp=15a6c7f48eaac7f0a81e5a30bfd74762a426435d;hpb=1ed7fb6c99f2fcb5f319d9c1fa2aefc5434f82e4;p=poolifier.git 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 15a6c7f4..3942bb6e 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 @@ -1,15 +1,12 @@ -import type { IWorker } from '../worker' -import type { IPool } from '../pool' -import { - DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, - DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS -} from '../../utils' -import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' +import type { IWorker } from '../worker.js' +import type { IPool } from '../pool.js' +import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils.js' +import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' import type { IWorkerChoiceStrategy, TaskStatisticsRequirements, WorkerChoiceStrategyOptions -} from './selection-strategies-types' +} from './selection-strategies-types.js' /** * Selects the next worker with an interleaved weighted round robin scheduling algorithm. @@ -40,10 +37,6 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< * Round id. */ private roundId: number = 0 - /** - * Default worker weight. - */ - private readonly defaultWorkerWeight: number /** * Round weights. */ @@ -60,11 +53,10 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< /** @inheritDoc */ public constructor ( pool: IPool, - opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + opts?: WorkerChoiceStrategyOptions ) { super(pool, opts) this.setTaskStatisticsRequirements(this.opts) - this.defaultWorkerWeight = this.computeDefaultWorkerWeight() this.roundWeights = this.getRoundWeights() } @@ -102,8 +94,8 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< ) { this.workerNodeVirtualTaskRunTime = 0 } - const workerWeight = - this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const workerWeight = this.opts!.weights![workerNodeKey]! if ( this.isWorkerNodeReady(workerNodeKey) && workerWeight >= this.roundWeights[roundIndex] && @@ -122,20 +114,18 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< } private interleavedWeightedRoundRobinNextWorkerNodeId (): void { - do { - if ( - this.roundId === this.roundWeights.length - 1 && - this.workerNodeId === this.pool.workerNodes.length - 1 - ) { - this.roundId = 0 - this.workerNodeId = 0 - } else if (this.workerNodeId === this.pool.workerNodes.length - 1) { - this.roundId = this.roundId + 1 - this.workerNodeId = 0 - } else { - this.workerNodeId = this.workerNodeId + 1 - } - } while (!this.isWorkerNodeReady(this.workerNodeId)) + if ( + this.roundId === this.roundWeights.length - 1 && + this.workerNodeId === this.pool.workerNodes.length - 1 + ) { + this.roundId = 0 + this.workerNodeId = 0 + } else if (this.workerNodeId === this.pool.workerNodes.length - 1) { + this.roundId = this.roundId + 1 + this.workerNodeId = 0 + } else { + this.workerNodeId = this.workerNodeId + 1 + } } /** @inheritDoc */ @@ -159,18 +149,16 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< } /** @inheritDoc */ - public setOptions (opts: WorkerChoiceStrategyOptions): void { + public setOptions (opts: WorkerChoiceStrategyOptions | undefined): void { super.setOptions(opts) this.roundWeights = this.getRoundWeights() } private getRoundWeights (): number[] { - if (this.opts.weights == null) { - return [this.defaultWorkerWeight] - } return [ ...new Set( - Object.values(this.opts.weights) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + Object.values(this.opts!.weights!) .slice() .sort((a, b) => a - b) )