Commit | Line | Data |
---|---|---|
bbeadd16 | 1 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
c4855468 | 2 | import type { IPool } from '../pool' |
f06e48d8 | 3 | import type { IWorker } from '../worker' |
51fe3d3c | 4 | import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy' |
feec6e8c | 5 | import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from './interleaved-weighted-round-robin-worker-choice-strategy' |
e4543b14 JB |
6 | import { LeastBusyWorkerChoiceStrategy } from './least-busy-worker-choice-strategy' |
7 | import { LeastUsedWorkerChoiceStrategy } from './least-used-worker-choice-strategy' | |
058a9457 | 8 | import { LeastEluWorkerChoiceStrategy } from './least-elu-worker-choice-strategy' |
51fe3d3c | 9 | import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy' |
bdaf31cd JB |
10 | import type { |
11 | IWorkerChoiceStrategy, | |
6c6afb84 | 12 | StrategyPolicy, |
87de9ff5 | 13 | TaskStatisticsRequirements, |
da309861 JB |
14 | WorkerChoiceStrategy, |
15 | WorkerChoiceStrategyOptions | |
bdaf31cd JB |
16 | } from './selection-strategies-types' |
17 | import { WorkerChoiceStrategies } from './selection-strategies-types' | |
51fe3d3c | 18 | import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy' |
bdaf31cd JB |
19 | |
20 | /** | |
21 | * The worker choice strategy context. | |
22 | * | |
38e795c1 | 23 | * @typeParam Worker - Type of worker. |
e102732c JB |
24 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
25 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
bdaf31cd JB |
26 | */ |
27 | export class WorkerChoiceStrategyContext< | |
f06e48d8 | 28 | Worker extends IWorker, |
b2b1d84e JB |
29 | Data = unknown, |
30 | Response = unknown | |
bdaf31cd | 31 | > { |
b529c323 | 32 | private readonly workerChoiceStrategies: Map< |
95c83464 | 33 | WorkerChoiceStrategy, |
17393ac8 | 34 | IWorkerChoiceStrategy |
b529c323 | 35 | > |
bdaf31cd JB |
36 | |
37 | /** | |
38 | * Worker choice strategy context constructor. | |
39 | * | |
38e795c1 | 40 | * @param pool - The pool instance. |
d710242d | 41 | * @param workerChoiceStrategy - The worker choice strategy. |
da309861 | 42 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
43 | */ |
44 | public constructor ( | |
c4855468 | 45 | pool: IPool<Worker, Data, Response>, |
d710242d | 46 | private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN, |
8990357d | 47 | private opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
bdaf31cd | 48 | ) { |
8990357d | 49 | this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts } |
7254e419 | 50 | this.execute = this.execute.bind(this) |
b529c323 JB |
51 | this.workerChoiceStrategies = new Map< |
52 | WorkerChoiceStrategy, | |
17393ac8 | 53 | IWorkerChoiceStrategy |
b529c323 JB |
54 | >([ |
55 | [ | |
56 | WorkerChoiceStrategies.ROUND_ROBIN, | |
7254e419 JB |
57 | new (RoundRobinWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( |
58 | pool, | |
59 | opts | |
60 | ) | |
b529c323 JB |
61 | ], |
62 | [ | |
e4543b14 JB |
63 | WorkerChoiceStrategies.LEAST_USED, |
64 | new (LeastUsedWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( | |
7254e419 JB |
65 | pool, |
66 | opts | |
67 | ) | |
b529c323 JB |
68 | ], |
69 | [ | |
e4543b14 JB |
70 | WorkerChoiceStrategies.LEAST_BUSY, |
71 | new (LeastBusyWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( | |
7254e419 JB |
72 | pool, |
73 | opts | |
74 | ) | |
b529c323 JB |
75 | ], |
76 | [ | |
058a9457 JB |
77 | WorkerChoiceStrategies.LEAST_ELU, |
78 | new (LeastEluWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( | |
79 | pool, | |
80 | opts | |
81 | ) | |
82 | ], | |
83 | [ | |
b529c323 | 84 | WorkerChoiceStrategies.FAIR_SHARE, |
7254e419 | 85 | new (FairShareWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( |
da309861 JB |
86 | pool, |
87 | opts | |
88 | ) | |
7254e419 JB |
89 | ], |
90 | [ | |
91 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, | |
92 | new (WeightedRoundRobinWorkerChoiceStrategy.bind(this))< | |
93 | Worker, | |
94 | Data, | |
95 | Response | |
96 | >(pool, opts) | |
feec6e8c JB |
97 | ], |
98 | [ | |
99 | WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN, | |
100 | new (InterleavedWeightedRoundRobinWorkerChoiceStrategy.bind(this))< | |
101 | Worker, | |
102 | Data, | |
103 | Response | |
104 | >(pool, opts) | |
b529c323 JB |
105 | ] |
106 | ]) | |
bdaf31cd JB |
107 | } |
108 | ||
6c6afb84 JB |
109 | /** |
110 | * Gets the strategy policy in the context. | |
111 | * | |
112 | * @returns The strategy policy. | |
113 | */ | |
114 | public getStrategyPolicy (): StrategyPolicy { | |
115 | return ( | |
116 | this.workerChoiceStrategies.get( | |
117 | this.workerChoiceStrategy | |
118 | ) as IWorkerChoiceStrategy | |
119 | ).strategyPolicy | |
120 | } | |
121 | ||
97a2abc3 | 122 | /** |
8990357d | 123 | * Gets the worker choice strategy in the context task statistics requirements. |
97a2abc3 | 124 | * |
87de9ff5 | 125 | * @returns The task statistics requirements. |
97a2abc3 | 126 | */ |
87de9ff5 | 127 | public getTaskStatisticsRequirements (): TaskStatisticsRequirements { |
95c83464 JB |
128 | return ( |
129 | this.workerChoiceStrategies.get( | |
d710242d | 130 | this.workerChoiceStrategy |
17393ac8 | 131 | ) as IWorkerChoiceStrategy |
87de9ff5 | 132 | ).taskStatisticsRequirements |
97a2abc3 JB |
133 | } |
134 | ||
bdaf31cd | 135 | /** |
bdede008 | 136 | * Sets the worker choice strategy to use in the context. |
bdaf31cd | 137 | * |
38e795c1 | 138 | * @param workerChoiceStrategy - The worker choice strategy to set. |
bdaf31cd JB |
139 | */ |
140 | public setWorkerChoiceStrategy ( | |
141 | workerChoiceStrategy: WorkerChoiceStrategy | |
142 | ): void { | |
d710242d JB |
143 | if (this.workerChoiceStrategy !== workerChoiceStrategy) { |
144 | this.workerChoiceStrategy = workerChoiceStrategy | |
b2b1d84e | 145 | } |
d710242d | 146 | this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset() |
bdaf31cd JB |
147 | } |
148 | ||
138d29a8 | 149 | /** |
8990357d | 150 | * Updates the worker node key in the worker choice strategy in the context internals. |
138d29a8 JB |
151 | * |
152 | * @returns `true` if the update is successful, `false` otherwise. | |
153 | */ | |
a4958de2 | 154 | public update (workerNodeKey: number): boolean { |
138d29a8 JB |
155 | return ( |
156 | this.workerChoiceStrategies.get( | |
157 | this.workerChoiceStrategy | |
158 | ) as IWorkerChoiceStrategy | |
a4958de2 | 159 | ).update(workerNodeKey) |
138d29a8 JB |
160 | } |
161 | ||
bdaf31cd | 162 | /** |
8990357d | 163 | * Executes the worker choice strategy in the context algorithm. |
bdaf31cd | 164 | * |
f06e48d8 | 165 | * @returns The key of the worker node. |
fb5a7307 | 166 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined. |
bdaf31cd | 167 | */ |
c923ce56 | 168 | public execute (): number { |
fb5a7307 JB |
169 | const workerChoiceStrategy = this.workerChoiceStrategies.get( |
170 | this.workerChoiceStrategy | |
171 | ) as IWorkerChoiceStrategy | |
613a91dc | 172 | if (!workerChoiceStrategy.hasPoolWorkerNodesReady()) { |
722d55a0 | 173 | return this.execute() |
613a91dc JB |
174 | } |
175 | return this.executeStrategy(workerChoiceStrategy) | |
176 | } | |
177 | ||
178 | /** | |
179 | * Executes the given worker choice strategy. | |
180 | * | |
181 | * @param workerChoiceStrategy - The worker choice strategy. | |
182 | * @returns The key of the worker node. | |
183 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined. | |
184 | */ | |
185 | private executeStrategy (workerChoiceStrategy: IWorkerChoiceStrategy): number { | |
fb5a7307 | 186 | let workerNodeKey: number | undefined |
fb5a7307 JB |
187 | let chooseCount = 0 |
188 | let retriesCount = 0 | |
189 | do { | |
613a91dc JB |
190 | workerNodeKey = workerChoiceStrategy.choose() |
191 | if (workerNodeKey == null && chooseCount > 0) { | |
192 | retriesCount++ | |
fb5a7307 | 193 | } |
613a91dc | 194 | chooseCount++ |
fb5a7307 | 195 | } while ( |
613a91dc JB |
196 | workerNodeKey == null && |
197 | retriesCount < (this.opts.retries as number) | |
fb5a7307 | 198 | ) |
fb5a7307 | 199 | if (workerNodeKey == null) { |
e695d66f | 200 | throw new Error( |
fb5a7307 | 201 | `Worker node key chosen is null or undefined after ${retriesCount} retries` |
4ba8492f | 202 | ) |
b0d6ed8f JB |
203 | } |
204 | return workerNodeKey | |
bdaf31cd | 205 | } |
97a2abc3 JB |
206 | |
207 | /** | |
c7e196ba | 208 | * Removes the worker node key from the worker choice strategy in the context. |
97a2abc3 | 209 | * |
501aea93 | 210 | * @param workerNodeKey - The worker node key. |
97a2abc3 JB |
211 | * @returns `true` if the removal is successful, `false` otherwise. |
212 | */ | |
f06e48d8 | 213 | public remove (workerNodeKey: number): boolean { |
95c83464 JB |
214 | return ( |
215 | this.workerChoiceStrategies.get( | |
d710242d | 216 | this.workerChoiceStrategy |
17393ac8 | 217 | ) as IWorkerChoiceStrategy |
f06e48d8 | 218 | ).remove(workerNodeKey) |
95c83464 | 219 | } |
a20f0ba5 JB |
220 | |
221 | /** | |
222 | * Sets the worker choice strategies in the context options. | |
223 | * | |
224 | * @param opts - The worker choice strategy options. | |
225 | */ | |
226 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
8990357d | 227 | this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts } |
0509fd43 | 228 | for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) { |
a20f0ba5 | 229 | workerChoiceStrategy.setOptions(opts) |
0509fd43 | 230 | } |
a20f0ba5 | 231 | } |
bdaf31cd | 232 | } |