Commit | Line | Data |
---|---|---|
3c93feb9 JB |
1 | import { |
2 | DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
00e1bdeb | 3 | buildInternalWorkerChoiceStrategyOptions |
d35e5717 JB |
4 | } from '../../utils.js' |
5 | import type { IPool } from '../pool.js' | |
6 | import type { IWorker } from '../worker.js' | |
10fcfaf4 JB |
7 | import type { |
8 | IWorkerChoiceStrategy, | |
26ce26ca | 9 | InternalWorkerChoiceStrategyOptions, |
57441b79 | 10 | MeasurementStatisticsRequirements, |
6c6afb84 | 11 | StrategyPolicy, |
26ce26ca | 12 | TaskStatisticsRequirements |
d35e5717 | 13 | } from './selection-strategies-types.js' |
bdaf31cd JB |
14 | |
15 | /** | |
9cd39dd4 | 16 | * Worker choice strategy abstract base class. |
bdaf31cd | 17 | * |
38e795c1 | 18 | * @typeParam Worker - Type of worker which manages the strategy. |
e102732c JB |
19 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
20 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
bdaf31cd JB |
21 | */ |
22 | export abstract class AbstractWorkerChoiceStrategy< | |
f06e48d8 | 23 | Worker extends IWorker, |
b2b1d84e JB |
24 | Data = unknown, |
25 | Response = unknown | |
17393ac8 | 26 | > implements IWorkerChoiceStrategy { |
d33be430 | 27 | /** |
9b106837 | 28 | * The next worker node key. |
d33be430 | 29 | */ |
b1aae695 | 30 | protected nextWorkerNodeKey: number | undefined = 0 |
d33be430 | 31 | |
7c7bb289 JB |
32 | /** |
33 | * The previous worker node key. | |
34 | */ | |
35 | protected previousWorkerNodeKey: number = 0 | |
36 | ||
6c6afb84 JB |
37 | /** @inheritDoc */ |
38 | public readonly strategyPolicy: StrategyPolicy = { | |
b1aae695 | 39 | dynamicWorkerUsage: false, |
f6bc9f26 | 40 | dynamicWorkerReady: true |
6c6afb84 JB |
41 | } |
42 | ||
afc003b2 | 43 | /** @inheritDoc */ |
87de9ff5 | 44 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
3c93feb9 JB |
45 | runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
46 | waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
47 | elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS | |
10fcfaf4 | 48 | } |
bdaf31cd JB |
49 | |
50 | /** | |
6533c3e6 | 51 | * Constructs a worker choice strategy bound to the pool. |
bdaf31cd | 52 | * |
38e795c1 | 53 | * @param pool - The pool instance. |
da309861 | 54 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
55 | */ |
56 | public constructor ( | |
c4855468 | 57 | protected readonly pool: IPool<Worker, Data, Response>, |
26ce26ca | 58 | protected opts: InternalWorkerChoiceStrategyOptions |
b8f3418c | 59 | ) { |
3d6f0f73 JB |
60 | this.opts = buildInternalWorkerChoiceStrategyOptions( |
61 | this.pool.info.maxSize, | |
62 | this.opts | |
63 | ) | |
64 | this.setTaskStatisticsRequirements(this.opts) | |
7254e419 | 65 | this.choose = this.choose.bind(this) |
b8f3418c | 66 | } |
bdaf31cd | 67 | |
932fc8be | 68 | protected setTaskStatisticsRequirements ( |
26ce26ca | 69 | opts: InternalWorkerChoiceStrategyOptions |
932fc8be | 70 | ): void { |
57441b79 JB |
71 | this.toggleMedianMeasurementStatisticsRequirements( |
72 | this.taskStatisticsRequirements.runTime, | |
67f3f2d6 JB |
73 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
74 | opts.runTime!.median | |
57441b79 JB |
75 | ) |
76 | this.toggleMedianMeasurementStatisticsRequirements( | |
77 | this.taskStatisticsRequirements.waitTime, | |
67f3f2d6 JB |
78 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
79 | opts.waitTime!.median | |
57441b79 JB |
80 | ) |
81 | this.toggleMedianMeasurementStatisticsRequirements( | |
82 | this.taskStatisticsRequirements.elu, | |
67f3f2d6 JB |
83 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
84 | opts.elu!.median | |
57441b79 JB |
85 | ) |
86 | } | |
87 | ||
88 | private toggleMedianMeasurementStatisticsRequirements ( | |
89 | measurementStatisticsRequirements: MeasurementStatisticsRequirements, | |
90 | toggleMedian: boolean | |
91 | ): void { | |
92 | if (measurementStatisticsRequirements.average && toggleMedian) { | |
93 | measurementStatisticsRequirements.average = false | |
94 | measurementStatisticsRequirements.median = toggleMedian | |
5df69fab | 95 | } |
57441b79 JB |
96 | if (measurementStatisticsRequirements.median && !toggleMedian) { |
97 | measurementStatisticsRequirements.average = true | |
98 | measurementStatisticsRequirements.median = toggleMedian | |
5df69fab | 99 | } |
da309861 JB |
100 | } |
101 | ||
39a43af7 JB |
102 | protected resetWorkerNodeKeyProperties (): void { |
103 | this.nextWorkerNodeKey = 0 | |
104 | this.previousWorkerNodeKey = 0 | |
105 | } | |
106 | ||
afc003b2 | 107 | /** @inheritDoc */ |
a6f7f1b4 | 108 | public abstract reset (): boolean |
ea7a90d3 | 109 | |
138d29a8 | 110 | /** @inheritDoc */ |
a4958de2 | 111 | public abstract update (workerNodeKey: number): boolean |
138d29a8 | 112 | |
afc003b2 | 113 | /** @inheritDoc */ |
b1aae695 | 114 | public abstract choose (): number | undefined |
97a2abc3 | 115 | |
afc003b2 | 116 | /** @inheritDoc */ |
f06e48d8 | 117 | public abstract remove (workerNodeKey: number): boolean |
a20f0ba5 JB |
118 | |
119 | /** @inheritDoc */ | |
26ce26ca | 120 | public setOptions (opts: InternalWorkerChoiceStrategyOptions): void { |
00e1bdeb JB |
121 | this.opts = buildInternalWorkerChoiceStrategyOptions( |
122 | this.pool.info.maxSize, | |
123 | opts | |
124 | ) | |
65ab8dcc | 125 | this.setTaskStatisticsRequirements(this.opts) |
a20f0ba5 | 126 | } |
cb70b19d | 127 | |
fb5a7307 JB |
128 | /** @inheritDoc */ |
129 | public hasPoolWorkerNodesReady (): boolean { | |
130 | return this.pool.workerNodes.some(workerNode => workerNode.info.ready) | |
131 | } | |
132 | ||
d4ddb68b JB |
133 | /** |
134 | * Whether the worker node is ready or not. | |
135 | * | |
136 | * @param workerNodeKey - The worker node key. | |
137 | * @returns Whether the worker node is ready or not. | |
138 | */ | |
ae3ab61d | 139 | protected isWorkerNodeReady (workerNodeKey: number): boolean { |
535fd8d5 | 140 | return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false |
19dbc45b JB |
141 | } |
142 | ||
a38b62f1 JB |
143 | /** |
144 | * Check the next worker node readiness. | |
145 | */ | |
146 | protected checkNextWorkerNodeReadiness (): void { | |
67f3f2d6 JB |
147 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
148 | if (!this.isWorkerNodeReady(this.nextWorkerNodeKey!)) { | |
a38b62f1 JB |
149 | delete this.nextWorkerNodeKey |
150 | } | |
151 | } | |
152 | ||
f6b641d6 | 153 | /** |
f3a91bac | 154 | * Gets the worker node task runtime. |
932fc8be JB |
155 | * If the task statistics require the average runtime, the average runtime is returned. |
156 | * If the task statistics require the median runtime , the median runtime is returned. | |
f6b641d6 JB |
157 | * |
158 | * @param workerNodeKey - The worker node key. | |
f3a91bac | 159 | * @returns The worker node task runtime. |
f6b641d6 | 160 | */ |
f3a91bac | 161 | protected getWorkerNodeTaskRunTime (workerNodeKey: number): number { |
932fc8be | 162 | return this.taskStatisticsRequirements.runTime.median |
46b0bb09 JB |
163 | ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0 |
164 | : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0 | |
f6b641d6 JB |
165 | } |
166 | ||
ef680bb8 | 167 | /** |
f3a91bac | 168 | * Gets the worker node task wait time. |
932fc8be JB |
169 | * If the task statistics require the average wait time, the average wait time is returned. |
170 | * If the task statistics require the median wait time, the median wait time is returned. | |
ef680bb8 JB |
171 | * |
172 | * @param workerNodeKey - The worker node key. | |
f3a91bac | 173 | * @returns The worker node task wait time. |
ef680bb8 | 174 | */ |
f3a91bac | 175 | protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number { |
932fc8be | 176 | return this.taskStatisticsRequirements.waitTime.median |
46b0bb09 JB |
177 | ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0 |
178 | : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0 | |
ef680bb8 JB |
179 | } |
180 | ||
5df69fab | 181 | /** |
f3a91bac | 182 | * Gets the worker node task ELU. |
9adcefab JB |
183 | * If the task statistics require the average ELU, the average ELU is returned. |
184 | * If the task statistics require the median ELU, the median ELU is returned. | |
5df69fab JB |
185 | * |
186 | * @param workerNodeKey - The worker node key. | |
f3a91bac | 187 | * @returns The worker node task ELU. |
5df69fab | 188 | */ |
f3a91bac | 189 | protected getWorkerNodeTaskElu (workerNodeKey: number): number { |
5df69fab | 190 | return this.taskStatisticsRequirements.elu.median |
46b0bb09 JB |
191 | ? this.pool.workerNodes[workerNodeKey].usage.elu.active.median ?? 0 |
192 | : this.pool.workerNodes[workerNodeKey].usage.elu.active.average ?? 0 | |
5df69fab JB |
193 | } |
194 | ||
baca80f7 JB |
195 | /** |
196 | * Sets safely the previous worker node key. | |
197 | * | |
198 | * @param workerNodeKey - The worker node key. | |
199 | */ | |
200 | protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void { | |
201 | this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey | |
202 | } | |
bdaf31cd | 203 | } |