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=bda206a7d7e1521ac8a6056b0485ad18e7698375;hpb=e34f0ebcd2536e0890242ab414467c0e68ed65e6;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 bda206a7..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 @@ -1,11 +1,11 @@ -import { cpus } from 'node:os' import type { IWorker } from '../worker' import type { IPool } from '../pool' 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' @@ -25,16 +25,29 @@ export class WeightedRoundRobinWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly requiredStatistics: RequiredStatistics = { - runTime: true, - avgRunTime: true, - medRunTime: 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. */ @@ -50,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 } @@ -68,19 +81,19 @@ 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 if (workerVirtualTaskRunTime < workerWeight) { this.workerVirtualTaskRunTime = workerVirtualTaskRunTime + - this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey) + 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 @@ -88,31 +101,14 @@ 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 } return true } - - private getWorkerVirtualTaskRunTime (workerNodeKey: number): number { - return this.requiredStatistics.medRunTime - ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime - : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime - } - - private computeDefaultWorkerWeight (): number { - let cpusCycleTimeWeight = 0 - for (const cpu of cpus()) { - // CPU estimated cycle time - const numberOfDigits = cpu.speed.toString().length - 1 - const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits)) - cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits) - } - return Math.round(cpusCycleTimeWeight / cpus().length) - } }