Commit | Line | Data |
---|---|---|
3c93feb9 JB |
1 | import { |
2 | DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
00e1bdeb | 3 | buildInternalWorkerChoiceStrategyOptions |
3c93feb9 | 4 | } from '../../utils' |
08f3f44c | 5 | import type { IPool } from '../pool' |
f06e48d8 | 6 | import type { IWorker } from '../worker' |
10fcfaf4 JB |
7 | import type { |
8 | IWorkerChoiceStrategy, | |
26ce26ca | 9 | InternalWorkerChoiceStrategyOptions, |
57441b79 | 10 | MeasurementStatisticsRequirements, |
6c6afb84 | 11 | StrategyPolicy, |
26ce26ca | 12 | TaskStatisticsRequirements |
10fcfaf4 | 13 | } from './selection-strategies-types' |
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.setOptions(this.opts) |
61 | this.opts = buildInternalWorkerChoiceStrategyOptions( | |
62 | this.pool.info.maxSize, | |
63 | this.opts | |
64 | ) | |
65 | this.setTaskStatisticsRequirements(this.opts) | |
7254e419 | 66 | this.choose = this.choose.bind(this) |
b8f3418c | 67 | } |
bdaf31cd | 68 | |
932fc8be | 69 | protected setTaskStatisticsRequirements ( |
26ce26ca | 70 | opts: InternalWorkerChoiceStrategyOptions |
932fc8be | 71 | ): void { |
57441b79 JB |
72 | this.toggleMedianMeasurementStatisticsRequirements( |
73 | this.taskStatisticsRequirements.runTime, | |
74 | opts.runTime?.median as boolean | |
75 | ) | |
76 | this.toggleMedianMeasurementStatisticsRequirements( | |
77 | this.taskStatisticsRequirements.waitTime, | |
78 | opts.waitTime?.median as boolean | |
79 | ) | |
80 | this.toggleMedianMeasurementStatisticsRequirements( | |
81 | this.taskStatisticsRequirements.elu, | |
82 | opts.elu?.median as boolean | |
83 | ) | |
84 | } | |
85 | ||
86 | private toggleMedianMeasurementStatisticsRequirements ( | |
87 | measurementStatisticsRequirements: MeasurementStatisticsRequirements, | |
88 | toggleMedian: boolean | |
89 | ): void { | |
90 | if (measurementStatisticsRequirements.average && toggleMedian) { | |
91 | measurementStatisticsRequirements.average = false | |
92 | measurementStatisticsRequirements.median = toggleMedian | |
5df69fab | 93 | } |
57441b79 JB |
94 | if (measurementStatisticsRequirements.median && !toggleMedian) { |
95 | measurementStatisticsRequirements.average = true | |
96 | measurementStatisticsRequirements.median = toggleMedian | |
5df69fab | 97 | } |
da309861 JB |
98 | } |
99 | ||
39a43af7 JB |
100 | protected resetWorkerNodeKeyProperties (): void { |
101 | this.nextWorkerNodeKey = 0 | |
102 | this.previousWorkerNodeKey = 0 | |
103 | } | |
104 | ||
afc003b2 | 105 | /** @inheritDoc */ |
a6f7f1b4 | 106 | public abstract reset (): boolean |
ea7a90d3 | 107 | |
138d29a8 | 108 | /** @inheritDoc */ |
a4958de2 | 109 | public abstract update (workerNodeKey: number): boolean |
138d29a8 | 110 | |
afc003b2 | 111 | /** @inheritDoc */ |
b1aae695 | 112 | public abstract choose (): number | undefined |
97a2abc3 | 113 | |
afc003b2 | 114 | /** @inheritDoc */ |
f06e48d8 | 115 | public abstract remove (workerNodeKey: number): boolean |
a20f0ba5 JB |
116 | |
117 | /** @inheritDoc */ | |
26ce26ca | 118 | public setOptions (opts: InternalWorkerChoiceStrategyOptions): void { |
00e1bdeb JB |
119 | this.opts = buildInternalWorkerChoiceStrategyOptions( |
120 | this.pool.info.maxSize, | |
121 | opts | |
122 | ) | |
65ab8dcc | 123 | this.setTaskStatisticsRequirements(this.opts) |
a20f0ba5 | 124 | } |
cb70b19d | 125 | |
fb5a7307 JB |
126 | /** @inheritDoc */ |
127 | public hasPoolWorkerNodesReady (): boolean { | |
128 | return this.pool.workerNodes.some(workerNode => workerNode.info.ready) | |
129 | } | |
130 | ||
d4ddb68b JB |
131 | /** |
132 | * Whether the worker node is ready or not. | |
133 | * | |
134 | * @param workerNodeKey - The worker node key. | |
135 | * @returns Whether the worker node is ready or not. | |
136 | */ | |
ae3ab61d | 137 | protected isWorkerNodeReady (workerNodeKey: number): boolean { |
535fd8d5 | 138 | return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false |
19dbc45b JB |
139 | } |
140 | ||
a38b62f1 JB |
141 | /** |
142 | * Check the next worker node readiness. | |
143 | */ | |
144 | protected checkNextWorkerNodeReadiness (): void { | |
145 | if (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number)) { | |
146 | delete this.nextWorkerNodeKey | |
147 | } | |
148 | } | |
149 | ||
f6b641d6 | 150 | /** |
f3a91bac | 151 | * Gets the worker node 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. | |
f3a91bac | 156 | * @returns The worker node task runtime. |
f6b641d6 | 157 | */ |
f3a91bac | 158 | protected getWorkerNodeTaskRunTime (workerNodeKey: number): number { |
932fc8be | 159 | return this.taskStatisticsRequirements.runTime.median |
46b0bb09 JB |
160 | ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0 |
161 | : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0 | |
f6b641d6 JB |
162 | } |
163 | ||
ef680bb8 | 164 | /** |
f3a91bac | 165 | * Gets the worker node 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. | |
f3a91bac | 170 | * @returns The worker node task wait time. |
ef680bb8 | 171 | */ |
f3a91bac | 172 | protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number { |
932fc8be | 173 | return this.taskStatisticsRequirements.waitTime.median |
46b0bb09 JB |
174 | ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0 |
175 | : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0 | |
ef680bb8 JB |
176 | } |
177 | ||
5df69fab | 178 | /** |
f3a91bac | 179 | * Gets the worker node 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. | |
f3a91bac | 184 | * @returns The worker node task ELU. |
5df69fab | 185 | */ |
f3a91bac | 186 | protected getWorkerNodeTaskElu (workerNodeKey: number): number { |
5df69fab | 187 | return this.taskStatisticsRequirements.elu.median |
46b0bb09 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 | ||
baca80f7 JB |
192 | /** |
193 | * Sets safely the previous worker node key. | |
194 | * | |
195 | * @param workerNodeKey - The worker node key. | |
196 | */ | |
197 | protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void { | |
198 | this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey | |
199 | } | |
bdaf31cd | 200 | } |