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
= {
39 * Constructs a worker choice strategy bound to the pool.
41 * @param pool - The pool instance.
42 * @param opts - The worker choice strategy options.
45 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
46 protected opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
48 this.choose
= this.choose
.bind(this)
51 protected setTaskStatistics (opts
: WorkerChoiceStrategyOptions
): void {
53 this.taskStatisticsRequirements
.avgRunTime
&&
54 opts
.medRunTime
=== true
56 this.taskStatisticsRequirements
.avgRunTime
= false
57 this.taskStatisticsRequirements
.medRunTime
= opts
.medRunTime
as boolean
60 this.taskStatisticsRequirements
.medRunTime
&&
61 opts
.medRunTime
=== false
63 this.taskStatisticsRequirements
.avgRunTime
= true
64 this.taskStatisticsRequirements
.medRunTime
= opts
.medRunTime
as boolean
67 this.taskStatisticsRequirements
.avgWaitTime
&&
68 opts
.medWaitTime
=== true
70 this.taskStatisticsRequirements
.avgWaitTime
= false
71 this.taskStatisticsRequirements
.medWaitTime
= opts
.medWaitTime
as boolean
74 this.taskStatisticsRequirements
.medWaitTime
&&
75 opts
.medWaitTime
=== false
77 this.taskStatisticsRequirements
.avgWaitTime
= true
78 this.taskStatisticsRequirements
.medWaitTime
= opts
.medWaitTime
as boolean
83 public abstract reset (): boolean
86 public abstract update (workerNodeKey
: number): boolean
89 public abstract choose (): number
92 public abstract remove (workerNodeKey
: number): boolean
95 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
96 opts
= opts
?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
97 this.setTaskStatistics(opts
)
102 * Finds a free worker node key.
104 * @returns The free worker node key or `-1` if there is no free worker node.
106 protected findFreeWorkerNodeKey (): number {
107 if (this.toggleFindLastFreeWorkerNodeKey
) {
108 this.toggleFindLastFreeWorkerNodeKey
= false
109 return this.findLastFreeWorkerNodeKey()
111 this.toggleFindLastFreeWorkerNodeKey
= true
112 return this.findFirstFreeWorkerNodeKey()
116 * Gets the worker task runtime.
117 * If the task statistics require `avgRunTime`, the average runtime is returned.
118 * If the task statistics require `medRunTime`, the median runtime is returned.
120 * @param workerNodeKey - The worker node key.
121 * @returns The worker task runtime.
123 protected getWorkerTaskRunTime (workerNodeKey
: number): number {
124 return this.taskStatisticsRequirements
.medRunTime
125 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.median
126 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.average
130 * Gets the worker task wait time.
131 * If the task statistics require `avgWaitTime`, the average wait time is returned.
132 * If the task statistics require `medWaitTime`, the median wait time is returned.
134 * @param workerNodeKey - The worker node key.
135 * @returns The worker task wait time.
137 protected getWorkerWaitTime (workerNodeKey
: number): number {
138 return this.taskStatisticsRequirements
.medWaitTime
139 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.median
140 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.average
143 protected computeDefaultWorkerWeight (): number {
144 let cpusCycleTimeWeight
= 0
145 for (const cpu
of cpus()) {
146 // CPU estimated cycle time
147 const numberOfDigits
= cpu
.speed
.toString().length
- 1
148 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
149 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
151 return Math.round(cpusCycleTimeWeight
/ cpus().length
)
155 * Finds the first free worker node key based on the number of tasks the worker has applied.
157 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
159 * If no free worker is found, `-1` is returned.
161 * @returns A worker node key if there is one, `-1` otherwise.
163 private findFirstFreeWorkerNodeKey (): number {
164 return this.pool
.workerNodes
.findIndex(workerNode
=> {
165 return workerNode
.workerUsage
.tasks
.executing
=== 0
170 * Finds the last free worker node key based on the number of tasks the worker has applied.
172 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
174 * If no free worker is found, `-1` is returned.
176 * @returns A worker node key if there is one, `-1` otherwise.
178 private findLastFreeWorkerNodeKey (): number {
179 // It requires node >= 18.0.0:
180 // return this.workerNodes.findLastIndex(workerNode => {
181 // return workerNode.workerUsage.tasks.executing === 0
184 let workerNodeKey
= this.pool
.workerNodes
.length
- 1;
189 this.pool
.workerNodes
[workerNodeKey
].workerUsage
.tasks
.executing
=== 0