X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=aafc07dd6f8ac8f8faa47742bdda2e096449fd55;hb=5ea80606520ed06f815f4f7485ec9057ac23b176;hp=05caa42a0149ebc7dc29ef76edc3a025e8b24c2c;hpb=e4543b1428fd6b52f5832ea75f21ac082b52684e;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 05caa42a..aafc07dd 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,10 +23,22 @@ export class LeastBusyWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly requiredStatistics: RequiredStatistics = { - runTime: true, - avgRunTime: false, - medRunTime: 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 */ @@ -35,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 */ @@ -45,27 +57,25 @@ export class LeastBusyWorkerChoiceStrategy< /** @inheritDoc */ public update (): boolean { + let minTime = Infinity + for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { + 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 true } /** @inheritDoc */ public choose (): number { - const freeWorkerNodeKey = this.findFreeWorkerNodeKey() - if (freeWorkerNodeKey !== -1) { - return freeWorkerNodeKey - } - let minRunTime = Infinity - let leastBusyWorkerNodeKey!: number - 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 - } - } - return leastBusyWorkerNodeKey + return this.nextWorkerNodeId } /** @inheritDoc */