1 import { cpus
} from
'node:os'
2 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
3 import type { IPool
} from
'../pool'
4 import type { IWorker
} from
'../worker'
7 TaskStatisticsRequirements
,
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types'
12 * Worker choice strategy abstract base class.
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
16 * @typeParam Response - Type of execution response. This can only be serializable data.
18 export abstract class AbstractWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
22 > implements IWorkerChoiceStrategy
{
24 * Toggles finding the last free worker node key.
26 private toggleFindLastFreeWorkerNodeKey
: boolean = false
28 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
47 * Constructs a worker choice strategy bound to the pool.
49 * @param pool - The pool instance.
50 * @param opts - The worker choice strategy options.
53 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
54 protected opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
56 this.choose
= this.choose
.bind(this)
59 protected setTaskStatisticsRequirements (
60 opts
: WorkerChoiceStrategyOptions
63 this.taskStatisticsRequirements
.runTime
.average
&&
64 opts
.runTime
?.median
=== true
66 this.taskStatisticsRequirements
.runTime
.average
= false
67 this.taskStatisticsRequirements
.runTime
.median
= opts
.runTime
71 this.taskStatisticsRequirements
.runTime
.median
&&
72 opts
.runTime
?.median
=== false
74 this.taskStatisticsRequirements
.runTime
.average
= true
75 this.taskStatisticsRequirements
.runTime
.median
= opts
.runTime
79 this.taskStatisticsRequirements
.waitTime
.average
&&
80 opts
.waitTime
?.median
=== true
82 this.taskStatisticsRequirements
.waitTime
.average
= false
83 this.taskStatisticsRequirements
.waitTime
.median
= opts
.waitTime
87 this.taskStatisticsRequirements
.waitTime
.median
&&
88 opts
.waitTime
?.median
=== false
90 this.taskStatisticsRequirements
.waitTime
.average
= true
91 this.taskStatisticsRequirements
.waitTime
.median
= opts
.waitTime
95 this.taskStatisticsRequirements
.elu
.average
&&
96 opts
.elu
?.median
=== true
98 this.taskStatisticsRequirements
.elu
.average
= false
99 this.taskStatisticsRequirements
.elu
.median
= opts
.elu
.median
as boolean
102 this.taskStatisticsRequirements
.elu
.median
&&
103 opts
.elu
?.median
=== false
105 this.taskStatisticsRequirements
.elu
.average
= true
106 this.taskStatisticsRequirements
.elu
.median
= opts
.elu
.median
as boolean
111 public abstract reset (): boolean
114 public abstract update (workerNodeKey
: number): boolean
117 public abstract choose (): number
120 public abstract remove (workerNodeKey
: number): boolean
123 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
124 opts
= opts
?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
125 this.setTaskStatisticsRequirements(opts
)
130 * Finds a free worker node key.
132 * @returns The free worker node key or `-1` if there is no free worker node.
134 protected findFreeWorkerNodeKey (): number {
135 if (this.toggleFindLastFreeWorkerNodeKey
) {
136 this.toggleFindLastFreeWorkerNodeKey
= false
137 return this.findLastFreeWorkerNodeKey()
139 this.toggleFindLastFreeWorkerNodeKey
= true
140 return this.findFirstFreeWorkerNodeKey()
144 * Gets the worker task runtime.
145 * If the task statistics require the average runtime, the average runtime is returned.
146 * If the task statistics require the median runtime , the median runtime is returned.
148 * @param workerNodeKey - The worker node key.
149 * @returns The worker task runtime.
151 protected getWorkerTaskRunTime (workerNodeKey
: number): number {
152 return this.taskStatisticsRequirements
.runTime
.median
153 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.median
154 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.average
158 * Gets the worker task wait time.
159 * If the task statistics require the average wait time, the average wait time is returned.
160 * If the task statistics require the median wait time, the median wait time is returned.
162 * @param workerNodeKey - The worker node key.
163 * @returns The worker task wait time.
165 protected getWorkerTaskWaitTime (workerNodeKey
: number): number {
166 return this.taskStatisticsRequirements
.waitTime
.median
167 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.median
168 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.average
172 * Gets the worker task ELU.
173 * If the task statistics require the average ELU, the average ELU is returned.
174 * If the task statistics require the median ELU, the median ELU is returned.
176 * @param workerNodeKey - The worker node key.
177 * @returns The worker task ELU.
179 protected getWorkerTaskElu (workerNodeKey
: number): number {
180 return this.taskStatisticsRequirements
.elu
.median
181 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.elu
.active
.median
182 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.elu
.active
.average
185 protected computeDefaultWorkerWeight (): number {
186 let cpusCycleTimeWeight
= 0
187 for (const cpu
of cpus()) {
188 // CPU estimated cycle time
189 const numberOfDigits
= cpu
.speed
.toString().length
- 1
190 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
191 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
193 return Math.round(cpusCycleTimeWeight
/ cpus().length
)
197 * Finds the first free worker node key based on the number of tasks the worker has applied.
199 * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
201 * If no free worker is found, `-1` is returned.
203 * @returns A worker node key if there is one, `-1` otherwise.
205 private findFirstFreeWorkerNodeKey (): number {
206 return this.pool
.workerNodes
.findIndex(workerNode
=> {
207 return workerNode
.workerUsage
.tasks
.executing
=== 0
212 * Finds the last free worker node key based on the number of tasks the worker has applied.
214 * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
216 * If no free worker is found, `-1` is returned.
218 * @returns A worker node key if there is one, `-1` otherwise.
220 private findLastFreeWorkerNodeKey (): number {
221 // It requires node >= 18.0.0:
222 // return this.workerNodes.findLastIndex(workerNode => {
223 // return workerNode.workerUsage.tasks.executing === 0
226 let workerNodeKey
= this.pool
.workerNodes
.length
- 1;
231 this.pool
.workerNodes
[workerNodeKey
].workerUsage
.tasks
.executing
=== 0