X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=6fad8939e75aa0f2a3dca8e332c08ba529ba3a83;hb=fce028d66b18a0e46571eb82457055fe3177d702;hp=05caa42a0149ebc7dc29ef76edc3a025e8b24c2c;hpb=5a5fc090d6f7eb9248df1ba5c0ff4d001461b6d4;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..6fad8939 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -1,10 +1,13 @@ -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' import type { IWorkerChoiceStrategy, - RequiredStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -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, @@ -23,10 +26,18 @@ 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: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } /** @inheritDoc */ @@ -35,7 +46,7 @@ export class LeastBusyWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setRequiredStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -49,27 +60,34 @@ export class LeastBusyWorkerChoiceStrategy< } /** @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 + public choose (): number | undefined { + this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey() + 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 + } }