X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=84476eedaf0c434f5bcce23aa7ca694f1bf9d267;hb=d262e04c193ce648ddd6c0cb1dfaeb9264928c8c;hp=2dfb6083d4d85e6a4fe9b58d5a2eddca07e55819;hpb=a3ae469eebfcc990a251133beb01378599313233;p=poolifier.git 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 2dfb6083..84476eed 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -4,7 +4,7 @@ import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - RequiredStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -23,14 +23,22 @@ export class LeastBusyWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly requiredStatistics: RequiredStatistics = { - runTime: true, - avgRunTime: false, - medRunTime: false, - waitTime: false, - avgWaitTime: false, - medWaitTime: false, - elu: false + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { + runTime: { + aggregate: true, + average: false, + median: false + }, + waitTime: { + aggregate: true, + average: false, + median: false + }, + elu: { + aggregate: false, + average: false, + median: false + } } /** @inheritDoc */ @@ -39,7 +47,7 @@ export class LeastBusyWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setRequiredStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -54,22 +62,20 @@ export class LeastBusyWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - const freeWorkerNodeKey = this.findFreeWorkerNodeKey() - if (freeWorkerNodeKey !== -1) { - return freeWorkerNodeKey - } - let minRunTime = Infinity - let leastBusyWorkerNodeKey!: number + let minTime = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerRunTime = workerNode.tasksUsage.runTime - if (workerRunTime === 0) { - return workerNodeKey - } else if (workerRunTime < minRunTime) { - minRunTime = workerRunTime - leastBusyWorkerNodeKey = workerNodeKey + const workerTime = + workerNode.workerUsage.runTime.aggregate + + workerNode.workerUsage.waitTime.aggregate + if (workerTime === 0) { + this.nextWorkerNodeId = workerNodeKey + break + } else if (workerTime < minTime) { + minTime = workerTime + this.nextWorkerNodeId = workerNodeKey } } - return leastBusyWorkerNodeKey + return this.nextWorkerNodeId } /** @inheritDoc */