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
= {
43 * Constructs a worker choice strategy bound to the pool.
45 * @param pool - The pool instance.
46 * @param opts - The worker choice strategy options.
49 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
50 protected opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
52 this.choose
= this.choose
.bind(this)
55 protected setTaskStatisticsRequirements (
56 opts
: WorkerChoiceStrategyOptions
59 this.taskStatisticsRequirements
.runTime
.average
&&
60 opts
.runTime
?.median
=== true
62 this.taskStatisticsRequirements
.runTime
.average
= false
63 this.taskStatisticsRequirements
.runTime
.median
= opts
.runTime
67 this.taskStatisticsRequirements
.runTime
.median
&&
68 opts
.runTime
?.median
=== false
70 this.taskStatisticsRequirements
.runTime
.average
= true
71 this.taskStatisticsRequirements
.runTime
.median
= opts
.runTime
75 this.taskStatisticsRequirements
.waitTime
.average
&&
76 opts
.waitTime
?.median
=== true
78 this.taskStatisticsRequirements
.waitTime
.average
= false
79 this.taskStatisticsRequirements
.waitTime
.median
= opts
.waitTime
83 this.taskStatisticsRequirements
.waitTime
.median
&&
84 opts
.waitTime
?.median
=== false
86 this.taskStatisticsRequirements
.waitTime
.average
= true
87 this.taskStatisticsRequirements
.waitTime
.median
= opts
.waitTime
93 public abstract reset (): boolean
96 public abstract update (workerNodeKey
: number): boolean
99 public abstract choose (): number
102 public abstract remove (workerNodeKey
: number): boolean
105 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
106 opts
= opts
?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
107 this.setTaskStatisticsRequirements(opts
)
112 * Finds a free worker node key.
114 * @returns The free worker node key or `-1` if there is no free worker node.
116 protected findFreeWorkerNodeKey (): number {
117 if (this.toggleFindLastFreeWorkerNodeKey
) {
118 this.toggleFindLastFreeWorkerNodeKey
= false
119 return this.findLastFreeWorkerNodeKey()
121 this.toggleFindLastFreeWorkerNodeKey
= true
122 return this.findFirstFreeWorkerNodeKey()
126 * Gets the worker task runtime.
127 * If the task statistics require the average runtime, the average runtime is returned.
128 * If the task statistics require the median runtime , the median runtime is returned.
130 * @param workerNodeKey - The worker node key.
131 * @returns The worker task runtime.
133 protected getWorkerTaskRunTime (workerNodeKey
: number): number {
134 return this.taskStatisticsRequirements
.runTime
.median
135 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.median
136 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.average
140 * Gets the worker task wait time.
141 * If the task statistics require the average wait time, the average wait time is returned.
142 * If the task statistics require the median wait time, the median wait time is returned.
144 * @param workerNodeKey - The worker node key.
145 * @returns The worker task wait time.
147 protected getWorkerWaitTime (workerNodeKey
: number): number {
148 return this.taskStatisticsRequirements
.waitTime
.median
149 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.median
150 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.average
153 protected computeDefaultWorkerWeight (): number {
154 let cpusCycleTimeWeight
= 0
155 for (const cpu
of cpus()) {
156 // CPU estimated cycle time
157 const numberOfDigits
= cpu
.speed
.toString().length
- 1
158 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
159 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
161 return Math.round(cpusCycleTimeWeight
/ cpus().length
)
165 * Finds the first free worker node key based on the number of tasks the worker has applied.
167 * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
169 * If no free worker is found, `-1` is returned.
171 * @returns A worker node key if there is one, `-1` otherwise.
173 private findFirstFreeWorkerNodeKey (): number {
174 return this.pool
.workerNodes
.findIndex(workerNode
=> {
175 return workerNode
.workerUsage
.tasks
.executing
=== 0
180 * Finds the last free worker node key based on the number of tasks the worker has applied.
182 * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
184 * If no free worker is found, `-1` is returned.
186 * @returns A worker node key if there is one, `-1` otherwise.
188 private findLastFreeWorkerNodeKey (): number {
189 // It requires node >= 18.0.0:
190 // return this.workerNodes.findLastIndex(workerNode => {
191 // return workerNode.workerUsage.tasks.executing === 0
194 let workerNodeKey
= this.pool
.workerNodes
.length
- 1;
199 this.pool
.workerNodes
[workerNodeKey
].workerUsage
.tasks
.executing
=== 0