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