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'
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 requiredStatistics
: RequiredStatistics
= {
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 setRequiredStatistics (opts
: WorkerChoiceStrategyOptions
): void {
52 if (this.requiredStatistics
.avgRunTime
&& opts
.medRunTime
=== true) {
53 this.requiredStatistics
.avgRunTime
= false
54 this.requiredStatistics
.medRunTime
= opts
.medRunTime
as boolean
56 if (this.requiredStatistics
.medRunTime
&& opts
.medRunTime
=== false) {
57 this.requiredStatistics
.avgRunTime
= true
58 this.requiredStatistics
.medRunTime
= opts
.medRunTime
as boolean
60 if (this.requiredStatistics
.avgWaitTime
&& opts
.medWaitTime
=== true) {
61 this.requiredStatistics
.avgWaitTime
= false
62 this.requiredStatistics
.medWaitTime
= opts
.medWaitTime
as boolean
64 if (this.requiredStatistics
.medWaitTime
&& opts
.medWaitTime
=== false) {
65 this.requiredStatistics
.avgWaitTime
= true
66 this.requiredStatistics
.medWaitTime
= opts
.medWaitTime
as boolean
71 public abstract reset (): boolean
74 public abstract update (workerNodeKey
: number): boolean
77 public abstract choose (): number
80 public abstract remove (workerNodeKey
: number): boolean
83 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
84 opts
= opts
?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
85 this.setRequiredStatistics(opts
)
90 * Finds a free worker node key.
92 * @returns The free worker node key or `-1` if there is no free worker node.
94 protected findFreeWorkerNodeKey (): number {
95 if (this.toggleFindLastFreeWorkerNodeKey
) {
96 this.toggleFindLastFreeWorkerNodeKey
= false
97 return this.findLastFreeWorkerNodeKey()
99 this.toggleFindLastFreeWorkerNodeKey
= true
100 return this.findFirstFreeWorkerNodeKey()
104 * Gets the worker task runtime.
105 * If the required statistics are `avgRunTime`, the average runtime is returned.
106 * If the required statistics are `medRunTime`, the median runtime is returned.
108 * @param workerNodeKey - The worker node key.
109 * @returns The worker task runtime.
111 protected getWorkerTaskRunTime (workerNodeKey
: number): number {
112 return this.requiredStatistics
.medRunTime
113 ? this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.medRunTime
114 : this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.avgRunTime
118 * Gets the worker task wait time.
119 * If the required statistics are `avgWaitTime`, the average wait time is returned.
120 * If the required statistics are `medWaitTime`, the median wait time is returned.
122 * @param workerNodeKey - The worker node key.
123 * @returns The worker task wait time.
125 protected getWorkerWaitTime (workerNodeKey
: number): number {
126 return this.requiredStatistics
.medWaitTime
127 ? this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.medWaitTime
128 : this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.avgWaitTime
131 protected computeDefaultWorkerWeight (): number {
132 let cpusCycleTimeWeight
= 0
133 for (const cpu
of cpus()) {
134 // CPU estimated cycle time
135 const numberOfDigits
= cpu
.speed
.toString().length
- 1
136 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
137 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
139 return Math.round(cpusCycleTimeWeight
/ cpus().length
)
143 * Finds the first free worker node key based on the number of tasks the worker has applied.
145 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
147 * If no free worker is found, `-1` is returned.
149 * @returns A worker node key if there is one, `-1` otherwise.
151 private findFirstFreeWorkerNodeKey (): number {
152 return this.pool
.workerNodes
.findIndex(workerNode
=> {
153 return workerNode
.tasksUsage
.running
=== 0
158 * Finds the last free worker node key based on the number of tasks the worker has applied.
160 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
162 * If no free worker is found, `-1` is returned.
164 * @returns A worker node key if there is one, `-1` otherwise.
166 private findLastFreeWorkerNodeKey (): number {
167 // It requires node >= 18.0.0:
168 // return this.workerNodes.findLastIndex(workerNode => {
169 // return workerNode.tasksUsage.running === 0
172 let workerNodeKey
= this.pool
.workerNodes
.length
- 1;
176 if (this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.running
=== 0) {