X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=1f7033214d6a4735c3064df290acc2ce48cb3e72;hb=5623b8d539e054f846e86b1f8b3d2e60be748330;hp=d9e3c20e1d56e951238cdbfc47ab2c4698ca999a;hpb=a788de39a01b042deb0707c2e92af99181b933d2;p=poolifier.git diff --git a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts index d9e3c20e..1f703321 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -4,6 +4,7 @@ import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' import type { IWorkerChoiceStrategy, + StrategyPolicy, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -11,8 +12,8 @@ import type { * Selects the least used 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 LeastUsedWorkerChoiceStrategy< Worker extends IWorker, @@ -21,13 +22,19 @@ export class LeastUsedWorkerChoiceStrategy< > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { + /** @inheritDoc */ + public readonly strategyPolicy: StrategyPolicy = { + dynamicWorkerUsage: false, + dynamicWorkerReady: true + } + /** @inheritDoc */ public constructor ( pool: IPool, opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.setTaskStatistics(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ @@ -41,28 +48,37 @@ export class LeastUsedWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - const freeWorkerNodeKey = this.findFreeWorkerNodeKey() - if (freeWorkerNodeKey !== -1) { - return freeWorkerNodeKey - } - let minNumberOfTasks = Infinity - let leastUsedWorkerNodeKey!: number - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const tasksUsage = workerNode.tasksUsage - const workerTasks = tasksUsage.ran + tasksUsage.running - if (workerTasks === 0) { - return workerNodeKey - } else if (workerTasks < minNumberOfTasks) { - minNumberOfTasks = workerTasks - leastUsedWorkerNodeKey = workerNodeKey - } - } - return leastUsedWorkerNodeKey + public choose (): number | undefined { + const chosenWorkerNodeKey = this.leastUsedNextWorkerNodeKey() + this.assignChosenWorkerNodeKey(chosenWorkerNodeKey) + return this.nextWorkerNodeKey } /** @inheritDoc */ public remove (): boolean { return true } + + private leastUsedNextWorkerNodeKey (): number | undefined { + let minNumberOfTasks = Infinity + let chosenWorkerNodeKey: number | undefined + for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { + const workerTaskStatistics = workerNode.usage.tasks + const workerTasks = + workerTaskStatistics.executed + + workerTaskStatistics.executing + + workerTaskStatistics.queued + if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) { + chosenWorkerNodeKey = workerNodeKey + break + } else if ( + this.isWorkerNodeEligible(workerNodeKey) && + workerTasks < minNumberOfTasks + ) { + minNumberOfTasks = workerTasks + chosenWorkerNodeKey = workerNodeKey + } + } + return chosenWorkerNodeKey + } }