X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=a37f91d44fa67276542b8d70b3969a04046f4e72;hb=7b7d96315331635fa3c8a12b03b6893fe91bf8ec;hp=dabad6826568c38621c73d8fa4f6ac4efb28418f;hpb=9b1068374b1a52479b07e1e22b692289d5579237;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 dabad682..a37f91d4 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -1,14 +1,11 @@ -import { - DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, - DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS -} from '../../utils' +import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils' import type { IPool } from '../pool' import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, - TaskStatisticsRequirements, - WorkerChoiceStrategyOptions + InternalWorkerChoiceStrategyOptions, + TaskStatisticsRequirements } from './selection-strategies-types' /** @@ -43,7 +40,7 @@ export class LeastBusyWorkerChoiceStrategy< /** @inheritDoc */ public constructor ( pool: IPool, - opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + opts: InternalWorkerChoiceStrategyOptions ) { super(pool, opts) this.setTaskStatisticsRequirements(this.opts) @@ -60,8 +57,9 @@ export class LeastBusyWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - this.leastBusyNextWorkerNodeKey() + public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) + this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey() return this.nextWorkerNodeKey } @@ -70,22 +68,18 @@ export class LeastBusyWorkerChoiceStrategy< return true } - private leastBusyNextWorkerNodeKey (): void { - let minTime = Infinity - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerTime = - (workerNode.usage.runTime?.aggregate ?? 0) + - (workerNode.usage.waitTime?.aggregate ?? 0) - if (this.isWorkerNodeReady(workerNodeKey) && workerTime === 0) { - this.nextWorkerNodeKey = workerNodeKey - break - } else if ( - this.isWorkerNodeReady(workerNodeKey) && - workerTime < minTime - ) { - minTime = workerTime - this.nextWorkerNodeKey = workerNodeKey - } - } + private leastBusyNextWorkerNodeKey (): number | undefined { + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + return this.isWorkerNodeReady(workerNodeKey) && + (workerNode.usage.runTime.aggregate ?? 0) + + (workerNode.usage.waitTime.aggregate ?? 0) < + (workerNodes[minWorkerNodeKey].usage.runTime.aggregate ?? 0) + + (workerNodes[minWorkerNodeKey].usage.waitTime.aggregate ?? 0) + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) } }