X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-busy-worker-choice-strategy.ts;h=9a1550e06f60dfecc4339f56ac2c01ad3a7344cf;hb=3d76750ac8dacd95003ca89cf9542c3b2a014b8c;hp=de747089b8fc49d989ef232c442a6caf695a3e6d;hpb=1dfe8965df029a366c040227fb61ae47b3d59a3a;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 de747089..9a1550e0 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, - TaskStatistics, + 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,14 +26,18 @@ export class LeastBusyWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly taskStatistics: TaskStatistics = { - 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: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } /** @inheritDoc */ @@ -39,7 +46,7 @@ export class LeastBusyWorkerChoiceStrategy< opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -54,22 +61,31 @@ export class LeastBusyWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - let minRunTime = Infinity - let leastBusyWorkerNodeKey!: number - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerRunTime = workerNode.workerUsage.runTime.aggregation - if (workerRunTime === 0) { - return workerNodeKey - } else if (workerRunTime < minRunTime) { - minRunTime = workerRunTime - leastBusyWorkerNodeKey = workerNodeKey - } - } - return leastBusyWorkerNodeKey + return this.leastBusyNextWorkerNodeKey() } /** @inheritDoc */ public remove (): boolean { return true } + + private leastBusyNextWorkerNodeKey (): number { + 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 + } + } + return this.nextWorkerNodeKey + } }