Commit | Line | Data |
---|---|---|
0bbf65c3 | 1 | import { cpus } from 'node:os' |
3c93feb9 JB |
2 | import { |
3 | DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
4 | DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
5 | } from '../../utils' | |
08f3f44c | 6 | import type { IPool } from '../pool' |
f06e48d8 | 7 | import type { IWorker } from '../worker' |
10fcfaf4 JB |
8 | import type { |
9 | IWorkerChoiceStrategy, | |
57441b79 | 10 | MeasurementStatisticsRequirements, |
6c6afb84 | 11 | StrategyPolicy, |
87de9ff5 | 12 | TaskStatisticsRequirements, |
da309861 | 13 | WorkerChoiceStrategyOptions |
10fcfaf4 | 14 | } from './selection-strategies-types' |
bdaf31cd JB |
15 | |
16 | /** | |
9cd39dd4 | 17 | * Worker choice strategy abstract base class. |
bdaf31cd | 18 | * |
38e795c1 | 19 | * @typeParam Worker - Type of worker which manages the strategy. |
e102732c JB |
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. | |
bdaf31cd JB |
22 | */ |
23 | export abstract class AbstractWorkerChoiceStrategy< | |
f06e48d8 | 24 | Worker extends IWorker, |
b2b1d84e JB |
25 | Data = unknown, |
26 | Response = unknown | |
17393ac8 | 27 | > implements IWorkerChoiceStrategy { |
d33be430 | 28 | /** |
9b106837 | 29 | * The next worker node key. |
d33be430 | 30 | */ |
b1aae695 | 31 | protected nextWorkerNodeKey: number | undefined = 0 |
d33be430 | 32 | |
7c7bb289 JB |
33 | /** |
34 | * The previous worker node key. | |
35 | */ | |
36 | protected previousWorkerNodeKey: number = 0 | |
37 | ||
6c6afb84 JB |
38 | /** @inheritDoc */ |
39 | public readonly strategyPolicy: StrategyPolicy = { | |
b1aae695 | 40 | dynamicWorkerUsage: false, |
f6bc9f26 | 41 | dynamicWorkerReady: true |
6c6afb84 JB |
42 | } |
43 | ||
afc003b2 | 44 | /** @inheritDoc */ |
87de9ff5 | 45 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
3c93feb9 JB |
46 | runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
47 | waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
48 | elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS | |
10fcfaf4 | 49 | } |
bdaf31cd JB |
50 | |
51 | /** | |
6533c3e6 | 52 | * Constructs a worker choice strategy bound to the pool. |
bdaf31cd | 53 | * |
38e795c1 | 54 | * @param pool - The pool instance. |
da309861 | 55 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
56 | */ |
57 | public constructor ( | |
c4855468 | 58 | protected readonly pool: IPool<Worker, Data, Response>, |
a20f0ba5 | 59 | protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
b8f3418c | 60 | ) { |
8990357d | 61 | this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts } |
7254e419 | 62 | this.choose = this.choose.bind(this) |
b8f3418c | 63 | } |
bdaf31cd | 64 | |
932fc8be JB |
65 | protected setTaskStatisticsRequirements ( |
66 | opts: WorkerChoiceStrategyOptions | |
67 | ): void { | |
57441b79 JB |
68 | this.toggleMedianMeasurementStatisticsRequirements( |
69 | this.taskStatisticsRequirements.runTime, | |
70 | opts.runTime?.median as boolean | |
71 | ) | |
72 | this.toggleMedianMeasurementStatisticsRequirements( | |
73 | this.taskStatisticsRequirements.waitTime, | |
74 | opts.waitTime?.median as boolean | |
75 | ) | |
76 | this.toggleMedianMeasurementStatisticsRequirements( | |
77 | this.taskStatisticsRequirements.elu, | |
78 | opts.elu?.median as boolean | |
79 | ) | |
80 | } | |
81 | ||
82 | private toggleMedianMeasurementStatisticsRequirements ( | |
83 | measurementStatisticsRequirements: MeasurementStatisticsRequirements, | |
84 | toggleMedian: boolean | |
85 | ): void { | |
86 | if (measurementStatisticsRequirements.average && toggleMedian) { | |
87 | measurementStatisticsRequirements.average = false | |
88 | measurementStatisticsRequirements.median = toggleMedian | |
5df69fab | 89 | } |
57441b79 JB |
90 | if (measurementStatisticsRequirements.median && !toggleMedian) { |
91 | measurementStatisticsRequirements.average = true | |
92 | measurementStatisticsRequirements.median = toggleMedian | |
5df69fab | 93 | } |
da309861 JB |
94 | } |
95 | ||
afc003b2 | 96 | /** @inheritDoc */ |
a6f7f1b4 | 97 | public abstract reset (): boolean |
ea7a90d3 | 98 | |
138d29a8 | 99 | /** @inheritDoc */ |
a4958de2 | 100 | public abstract update (workerNodeKey: number): boolean |
138d29a8 | 101 | |
afc003b2 | 102 | /** @inheritDoc */ |
b1aae695 | 103 | public abstract choose (): number | undefined |
97a2abc3 | 104 | |
afc003b2 | 105 | /** @inheritDoc */ |
f06e48d8 | 106 | public abstract remove (workerNodeKey: number): boolean |
a20f0ba5 JB |
107 | |
108 | /** @inheritDoc */ | |
109 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
8990357d | 110 | this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts } |
65ab8dcc | 111 | this.setTaskStatisticsRequirements(this.opts) |
a20f0ba5 | 112 | } |
cb70b19d | 113 | |
d4ddb68b JB |
114 | /** |
115 | * Whether the worker node is ready or not. | |
116 | * | |
117 | * @param workerNodeKey - The worker node key. | |
118 | * @returns Whether the worker node is ready or not. | |
119 | */ | |
8990357d | 120 | private isWorkerNodeReady (workerNodeKey: number): boolean { |
19dbc45b JB |
121 | return this.pool.workerNodes[workerNodeKey].info.ready |
122 | } | |
123 | ||
e2b31e32 JB |
124 | /** |
125 | * Whether the worker node has back pressure or not (i.e. its tasks queue is full). | |
126 | * | |
127 | * @param workerNodeKey - The worker node key. | |
128 | * @returns `true` if the worker node has back pressure, `false` otherwise. | |
129 | */ | |
8990357d | 130 | private hasWorkerNodeBackPressure (workerNodeKey: number): boolean { |
e2b31e32 JB |
131 | return this.pool.hasWorkerNodeBackPressure(workerNodeKey) |
132 | } | |
133 | ||
8990357d JB |
134 | /** |
135 | * Whether the worker node is eligible or not. | |
136 | * A worker node is eligible if it is ready and does not have back pressure. | |
137 | * | |
138 | * @param workerNodeKey - The worker node key. | |
139 | * @returns `true` if the worker node is eligible, `false` otherwise. | |
140 | * @see {@link isWorkerNodeReady} | |
141 | * @see {@link hasWorkerNodeBackPressure} | |
142 | */ | |
143 | protected isWorkerNodeEligible (workerNodeKey: number): boolean { | |
144 | return ( | |
145 | this.isWorkerNodeReady(workerNodeKey) && | |
146 | !this.hasWorkerNodeBackPressure(workerNodeKey) | |
147 | ) | |
148 | } | |
149 | ||
f6b641d6 | 150 | /** |
e6606302 | 151 | * Gets the worker task runtime. |
932fc8be JB |
152 | * If the task statistics require the average runtime, the average runtime is returned. |
153 | * If the task statistics require the median runtime , the median runtime is returned. | |
f6b641d6 JB |
154 | * |
155 | * @param workerNodeKey - The worker node key. | |
e6606302 | 156 | * @returns The worker task runtime. |
f6b641d6 JB |
157 | */ |
158 | protected getWorkerTaskRunTime (workerNodeKey: number): number { | |
932fc8be | 159 | return this.taskStatisticsRequirements.runTime.median |
71514351 JB |
160 | ? this.pool.workerNodes[workerNodeKey].usage.runTime?.median ?? 0 |
161 | : this.pool.workerNodes[workerNodeKey].usage.runTime?.average ?? 0 | |
f6b641d6 JB |
162 | } |
163 | ||
ef680bb8 JB |
164 | /** |
165 | * Gets the worker task wait time. | |
932fc8be JB |
166 | * If the task statistics require the average wait time, the average wait time is returned. |
167 | * If the task statistics require the median wait time, the median wait time is returned. | |
ef680bb8 JB |
168 | * |
169 | * @param workerNodeKey - The worker node key. | |
170 | * @returns The worker task wait time. | |
171 | */ | |
5df69fab | 172 | protected getWorkerTaskWaitTime (workerNodeKey: number): number { |
932fc8be | 173 | return this.taskStatisticsRequirements.waitTime.median |
71514351 JB |
174 | ? this.pool.workerNodes[workerNodeKey].usage.waitTime?.median ?? 0 |
175 | : this.pool.workerNodes[workerNodeKey].usage.waitTime?.average ?? 0 | |
ef680bb8 JB |
176 | } |
177 | ||
5df69fab JB |
178 | /** |
179 | * Gets the worker task ELU. | |
9adcefab JB |
180 | * If the task statistics require the average ELU, the average ELU is returned. |
181 | * If the task statistics require the median ELU, the median ELU is returned. | |
5df69fab JB |
182 | * |
183 | * @param workerNodeKey - The worker node key. | |
184 | * @returns The worker task ELU. | |
185 | */ | |
186 | protected getWorkerTaskElu (workerNodeKey: number): number { | |
187 | return this.taskStatisticsRequirements.elu.median | |
71514351 JB |
188 | ? this.pool.workerNodes[workerNodeKey].usage.elu.active?.median ?? 0 |
189 | : this.pool.workerNodes[workerNodeKey].usage.elu.active?.average ?? 0 | |
5df69fab JB |
190 | } |
191 | ||
b1aae695 JB |
192 | /** |
193 | * Assign to nextWorkerNodeKey property the chosen worker node key. | |
194 | * | |
195 | * @param chosenWorkerNodeKey - The chosen worker node key. | |
196 | */ | |
197 | protected assignChosenWorkerNodeKey ( | |
198 | chosenWorkerNodeKey: number | undefined | |
199 | ): void { | |
200 | if (chosenWorkerNodeKey != null) { | |
201 | this.nextWorkerNodeKey = chosenWorkerNodeKey | |
202 | } else { | |
203 | this.nextWorkerNodeKey = undefined | |
204 | } | |
205 | } | |
206 | ||
48e6ef5a JB |
207 | protected checkNextWorkerNodeEligibility ( |
208 | chosenWorkerNodeKey: number | undefined | |
209 | ): void { | |
210 | if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) { | |
211 | this.nextWorkerNodeKey = undefined | |
212 | this.previousWorkerNodeKey = | |
213 | chosenWorkerNodeKey ?? this.previousWorkerNodeKey | |
214 | } | |
215 | } | |
216 | ||
0bbf65c3 JB |
217 | protected computeDefaultWorkerWeight (): number { |
218 | let cpusCycleTimeWeight = 0 | |
219 | for (const cpu of cpus()) { | |
220 | // CPU estimated cycle time | |
221 | const numberOfDigits = cpu.speed.toString().length - 1 | |
222 | const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits)) | |
223 | cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits) | |
224 | } | |
225 | return Math.round(cpusCycleTimeWeight / cpus().length) | |
226 | } | |
bdaf31cd | 227 | } |