X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=224ca540130f49c37e6b28d5de471e5237dddb14;hb=f6bc9f26d8a0246bbee14b2b03d0bcc41b8aeb52;hp=dc304c89fd1e5abecb680aa5d519d9a59f6f18ac;hpb=d33be4309c69e39da5e81479e40b1a5ec7078bd5;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 dc304c89..224ca540 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -1,4 +1,7 @@ -import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' +import { + DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, + DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS +} from '../../utils' import type { IPool } from '../pool' import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' @@ -12,8 +15,8 @@ import type { * Selects the least busy worker. * * @typeParam Worker - Type of worker which manages the strategy. - * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of execution response. This can only be serializable data. + * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. + * @typeParam Response - Type of execution response. This can only be structured-cloneable data. */ export class LeastBusyWorkerChoiceStrategy< Worker extends IWorker, @@ -34,11 +37,7 @@ export class LeastBusyWorkerChoiceStrategy< average: false, median: false }, - elu: { - aggregate: false, - average: false, - median: false - } + elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } /** @inheritDoc */ @@ -57,29 +56,39 @@ 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 - return true - } else if (workerTime < minTime) { - minTime = workerTime - this.nextWorkerNodeId = workerNodeKey - } - } return true } /** @inheritDoc */ - public choose (): number { - return this.nextWorkerNodeId + public choose (): number | undefined { + const chosenWorkerNodeKey = this.leastBusyNextWorkerNodeKey() + this.assignChosenWorkerNodeKey(chosenWorkerNodeKey) + return this.nextWorkerNodeKey } /** @inheritDoc */ public remove (): boolean { return true } + + private leastBusyNextWorkerNodeKey (): number | undefined { + let minTime = Infinity + let chosenWorkerNodeKey: number | undefined + for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { + const workerTime = + (workerNode.usage.runTime?.aggregate ?? 0) + + (workerNode.usage.waitTime?.aggregate ?? 0) + if (this.isWorkerNodeEligible(workerNodeKey) && workerTime === 0) { + chosenWorkerNodeKey = workerNodeKey + break + } else if ( + this.isWorkerNodeEligible(workerNodeKey) && + workerTime < minTime + ) { + minTime = workerTime + chosenWorkerNodeKey = workerNodeKey + } + } + return chosenWorkerNodeKey + } }