X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fabstract-worker-choice-strategy.ts;h=34473ab4e2a3849bc96c6ac02bbe39bbbce3a47a;hb=c004ececb19646937d2bf9e6431f51fd4108b782;hp=5ec7cedc0f9dc0f2f4a1079302a94d515c5d4281;hpb=b7e141c40bccfd7a4ec0ff98b7829f7d296f048b;p=poolifier.git diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 5ec7cedc..34473ab4 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -1,16 +1,16 @@ import { cpus } from 'node:os' import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, - DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + getDefaultInternalWorkerChoiceStrategyOptions } from '../../utils' import type { IPool } from '../pool' import type { IWorker } from '../worker' import type { IWorkerChoiceStrategy, + InternalWorkerChoiceStrategyOptions, MeasurementStatisticsRequirements, StrategyPolicy, - TaskStatisticsRequirements, - WorkerChoiceStrategyOptions + TaskStatisticsRequirements } from './selection-strategies-types' /** @@ -56,14 +56,14 @@ export abstract class AbstractWorkerChoiceStrategy< */ public constructor ( protected readonly pool: IPool, - protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + protected opts: InternalWorkerChoiceStrategyOptions ) { - this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts } + this.setOptions(this.opts) this.choose = this.choose.bind(this) } protected setTaskStatisticsRequirements ( - opts: WorkerChoiceStrategyOptions + opts: InternalWorkerChoiceStrategyOptions ): void { this.toggleMedianMeasurementStatisticsRequirements( this.taskStatisticsRequirements.runTime, @@ -111,45 +111,36 @@ export abstract class AbstractWorkerChoiceStrategy< public abstract remove (workerNodeKey: number): boolean /** @inheritDoc */ - public setOptions (opts: WorkerChoiceStrategyOptions): void { - this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts } + public setOptions (opts: InternalWorkerChoiceStrategyOptions): void { + this.opts = { + ...getDefaultInternalWorkerChoiceStrategyOptions(this.pool.info.maxSize), + ...opts + } this.setTaskStatisticsRequirements(this.opts) } + /** @inheritDoc */ + public hasPoolWorkerNodesReady (): boolean { + return this.pool.workerNodes.some(workerNode => workerNode.info.ready) + } + /** * Whether the worker node is ready or not. * * @param workerNodeKey - The worker node key. * @returns Whether the worker node is ready or not. */ - private isWorkerNodeReady (workerNodeKey: number): boolean { - return this.pool.workerNodes[workerNodeKey].info.ready + protected isWorkerNodeReady (workerNodeKey: number): boolean { + return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false } /** - * Whether the worker node has back pressure or not (i.e. its tasks queue is full). - * - * @param workerNodeKey - The worker node key. - * @returns `true` if the worker node has back pressure, `false` otherwise. + * Check the next worker node readiness. */ - private hasWorkerNodeBackPressure (workerNodeKey: number): boolean { - return this.pool.hasWorkerNodeBackPressure(workerNodeKey) - } - - /** - * Whether the worker node is eligible or not. - * A worker node is eligible if it is ready and does not have back pressure. - * - * @param workerNodeKey - The worker node key. - * @returns `true` if the worker node is eligible, `false` otherwise. - * @see {@link isWorkerNodeReady} - * @see {@link hasWorkerNodeBackPressure} - */ - protected isWorkerNodeEligible (workerNodeKey: number): boolean { - return ( - this.isWorkerNodeReady(workerNodeKey) && - !this.hasWorkerNodeBackPressure(workerNodeKey) - ) + protected checkNextWorkerNodeReadiness (): void { + if (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number)) { + delete this.nextWorkerNodeKey + } } /** @@ -203,15 +194,6 @@ export abstract class AbstractWorkerChoiceStrategy< this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey } - /** - * Check the next worker node eligibility. - */ - protected checkNextWorkerNodeEligibility (): void { - if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) { - this.nextWorkerNodeKey = undefined - } - } - protected computeDefaultWorkerWeight (): number { let cpusCycleTimeWeight = 0 for (const cpu of cpus()) {