1 import { cpus
} from
'node:os'
3 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
4 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
6 import type { IPool
} from
'../pool'
7 import type { IWorker
} from
'../worker'
10 MeasurementStatisticsRequirements
,
12 TaskStatisticsRequirements
,
13 WorkerChoiceStrategyOptions
14 } from
'./selection-strategies-types'
17 * Worker choice strategy abstract base class.
19 * @typeParam Worker - Type of worker which manages the strategy.
20 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
23 export abstract class AbstractWorkerChoiceStrategy
<
24 Worker
extends IWorker
,
27 > implements IWorkerChoiceStrategy
{
29 * The next worker node key.
31 protected nextWorkerNodeKey
: number = 0
34 public readonly strategyPolicy
: StrategyPolicy
= {
35 useDynamicWorker
: false
39 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
40 runTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
41 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
42 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
46 * Constructs a worker choice strategy bound to the pool.
48 * @param pool - The pool instance.
49 * @param opts - The worker choice strategy options.
52 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
53 protected opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
55 this.opts
= { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
, ...opts
}
56 this.choose
= this.choose
.bind(this)
59 protected setTaskStatisticsRequirements (
60 opts
: WorkerChoiceStrategyOptions
62 this.toggleMedianMeasurementStatisticsRequirements(
63 this.taskStatisticsRequirements
.runTime
,
64 opts
.runTime
?.median
as boolean
66 this.toggleMedianMeasurementStatisticsRequirements(
67 this.taskStatisticsRequirements
.waitTime
,
68 opts
.waitTime
?.median
as boolean
70 this.toggleMedianMeasurementStatisticsRequirements(
71 this.taskStatisticsRequirements
.elu
,
72 opts
.elu
?.median
as boolean
76 private toggleMedianMeasurementStatisticsRequirements (
77 measurementStatisticsRequirements
: MeasurementStatisticsRequirements
,
80 if (measurementStatisticsRequirements
.average
&& toggleMedian
) {
81 measurementStatisticsRequirements
.average
= false
82 measurementStatisticsRequirements
.median
= toggleMedian
84 if (measurementStatisticsRequirements
.median
&& !toggleMedian
) {
85 measurementStatisticsRequirements
.average
= true
86 measurementStatisticsRequirements
.median
= toggleMedian
91 public abstract reset (): boolean
94 public abstract update (workerNodeKey
: number): boolean
97 public abstract choose (): number
100 public abstract remove (workerNodeKey
: number): boolean
103 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
104 this.opts
= { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
, ...opts
}
105 this.setTaskStatisticsRequirements(this.opts
)
109 * Whether the worker node is ready or not.
111 * @param workerNodeKey - The worker node key.
112 * @returns Whether the worker node is ready or not.
114 private isWorkerNodeReady (workerNodeKey
: number): boolean {
115 return this.pool
.workerNodes
[workerNodeKey
].info
.ready
119 * Whether the worker node has back pressure or not (i.e. its tasks queue is full).
121 * @param workerNodeKey - The worker node key.
122 * @returns `true` if the worker node has back pressure, `false` otherwise.
124 private hasWorkerNodeBackPressure (workerNodeKey
: number): boolean {
125 return this.pool
.hasWorkerNodeBackPressure(workerNodeKey
)
129 * Whether the worker node is eligible or not.
130 * A worker node is eligible if it is ready and does not have back pressure.
132 * @param workerNodeKey - The worker node key.
133 * @returns `true` if the worker node is eligible, `false` otherwise.
134 * @see {@link isWorkerNodeReady}
135 * @see {@link hasWorkerNodeBackPressure}
137 protected isWorkerNodeEligible (workerNodeKey
: number): boolean {
139 this.isWorkerNodeReady(workerNodeKey
) &&
140 !this.hasWorkerNodeBackPressure(workerNodeKey
)
145 * Gets the worker task runtime.
146 * If the task statistics require the average runtime, the average runtime is returned.
147 * If the task statistics require the median runtime , the median runtime is returned.
149 * @param workerNodeKey - The worker node key.
150 * @returns The worker task runtime.
152 protected getWorkerTaskRunTime (workerNodeKey
: number): number {
153 return this.taskStatisticsRequirements
.runTime
.median
154 ? this.pool
.workerNodes
[workerNodeKey
].usage
.runTime
?.median
?? 0
155 : this.pool
.workerNodes
[workerNodeKey
].usage
.runTime
?.average
?? 0
159 * Gets the worker task wait time.
160 * If the task statistics require the average wait time, the average wait time is returned.
161 * If the task statistics require the median wait time, the median wait time is returned.
163 * @param workerNodeKey - The worker node key.
164 * @returns The worker task wait time.
166 protected getWorkerTaskWaitTime (workerNodeKey
: number): number {
167 return this.taskStatisticsRequirements
.waitTime
.median
168 ? this.pool
.workerNodes
[workerNodeKey
].usage
.waitTime
?.median
?? 0
169 : this.pool
.workerNodes
[workerNodeKey
].usage
.waitTime
?.average
?? 0
173 * Gets the worker task ELU.
174 * If the task statistics require the average ELU, the average ELU is returned.
175 * If the task statistics require the median ELU, the median ELU is returned.
177 * @param workerNodeKey - The worker node key.
178 * @returns The worker task ELU.
180 protected getWorkerTaskElu (workerNodeKey
: number): number {
181 return this.taskStatisticsRequirements
.elu
.median
182 ? this.pool
.workerNodes
[workerNodeKey
].usage
.elu
.active
?.median
?? 0
183 : this.pool
.workerNodes
[workerNodeKey
].usage
.elu
.active
?.average
?? 0
186 protected computeDefaultWorkerWeight (): number {
187 let cpusCycleTimeWeight
= 0
188 for (const cpu
of cpus()) {
189 // CPU estimated cycle time
190 const numberOfDigits
= cpu
.speed
.toString().length
- 1
191 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
192 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
194 return Math.round(cpusCycleTimeWeight
/ cpus().length
)