-import { cpus } from 'node:os'
import {
DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
- DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ buildInternalWorkerChoiceStrategyOptions
} 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'
/**
*/
public constructor (
protected readonly pool: IPool<Worker, Data, Response>,
- protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ protected opts: InternalWorkerChoiceStrategyOptions
) {
- this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts }
+ this.opts = buildInternalWorkerChoiceStrategyOptions(
+ this.pool.info.maxSize,
+ this.opts
+ )
+ this.setTaskStatisticsRequirements(this.opts)
this.choose = this.choose.bind(this)
}
protected setTaskStatisticsRequirements (
- opts: WorkerChoiceStrategyOptions
+ opts: InternalWorkerChoiceStrategyOptions
): void {
this.toggleMedianMeasurementStatisticsRequirements(
this.taskStatisticsRequirements.runTime,
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 = buildInternalWorkerChoiceStrategyOptions(
+ this.pool.info.maxSize,
+ opts
+ )
this.setTaskStatisticsRequirements(this.opts)
}
- /**
- * 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
+ /** @inheritDoc */
+ public hasPoolWorkerNodesReady (): boolean {
+ return this.pool.workerNodes.some(workerNode => workerNode.info.ready)
}
/**
- * Whether the worker node has back pressure or not (i.e. its tasks queue is full).
+ * Whether the worker node is ready or not.
*
* @param workerNodeKey - The worker node key.
- * @returns `true` if the worker node has back pressure, `false` otherwise.
+ * @returns Whether the worker node is ready or not.
*/
- private hasWorkerNodeBackPressure (workerNodeKey: number): boolean {
- return this.pool.hasWorkerNodeBackPressure(workerNodeKey)
+ protected isWorkerNodeReady (workerNodeKey: number): boolean {
+ return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false
}
/**
- * 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}
+ * Check the next worker node readiness.
*/
- 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
+ }
}
/**
*/
protected getWorkerNodeTaskRunTime (workerNodeKey: number): number {
return this.taskStatisticsRequirements.runTime.median
- ? this.pool.workerNodes[workerNodeKey]?.usage?.runTime?.median ?? 0
- : this.pool.workerNodes[workerNodeKey]?.usage?.runTime?.average ?? 0
+ ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0
+ : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0
}
/**
*/
protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number {
return this.taskStatisticsRequirements.waitTime.median
- ? this.pool.workerNodes[workerNodeKey]?.usage?.waitTime?.median ?? 0
- : this.pool.workerNodes[workerNodeKey]?.usage?.waitTime?.average ?? 0
+ ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0
+ : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0
}
/**
*/
protected getWorkerNodeTaskElu (workerNodeKey: number): number {
return this.taskStatisticsRequirements.elu.median
- ? this.pool.workerNodes[workerNodeKey]?.usage?.elu?.active?.median ?? 0
- : this.pool.workerNodes[workerNodeKey]?.usage?.elu?.active?.average ?? 0
+ ? this.pool.workerNodes[workerNodeKey].usage.elu.active.median ?? 0
+ : this.pool.workerNodes[workerNodeKey].usage.elu.active.average ?? 0
}
/**
protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
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()) {
- // CPU estimated cycle time
- const numberOfDigits = cpu.speed.toString().length - 1
- const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
- cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
- }
- return Math.round(cpusCycleTimeWeight / cpus().length)
- }
}