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. |
457dd3dc | 167 | * @throws {@link https://nodejs.org/api/errors.html#class-rangeerror} If the maximum consecutive worker choice strategy executions has been reached. |
bdaf31cd | 168 | */ |
c923ce56 | 169 | public execute (): number { |
fb5a7307 JB |
170 | const workerChoiceStrategy = this.workerChoiceStrategies.get( |
171 | this.workerChoiceStrategy | |
172 | ) as IWorkerChoiceStrategy | |
173 | let workerNodeKey: number | undefined | |
174 | const maxExecutionCount = 10000 | |
175 | let executionCount = 0 | |
176 | let chooseCount = 0 | |
177 | let retriesCount = 0 | |
178 | do { | |
179 | if (workerChoiceStrategy.hasPoolWorkerNodesReady()) { | |
180 | workerNodeKey = workerChoiceStrategy.choose() | |
181 | if (chooseCount > 0) { | |
182 | retriesCount++ | |
183 | } | |
184 | chooseCount++ | |
185 | } | |
186 | executionCount++ | |
187 | } while ( | |
188 | executionCount < maxExecutionCount && | |
189 | (!workerChoiceStrategy.hasPoolWorkerNodesReady() || | |
190 | (workerNodeKey == null && retriesCount < (this.opts.retries as number))) | |
191 | ) | |
192 | if (executionCount >= maxExecutionCount) { | |
193 | throw new RangeError( | |
194 | `Worker choice strategy consecutive executions has exceeded the maximum of ${maxExecutionCount}` | |
195 | ) | |
196 | } | |
197 | if (workerNodeKey == null) { | |
e695d66f | 198 | throw new Error( |
fb5a7307 | 199 | `Worker node key chosen is null or undefined after ${retriesCount} retries` |
4ba8492f | 200 | ) |
b0d6ed8f JB |
201 | } |
202 | return workerNodeKey | |
bdaf31cd | 203 | } |
97a2abc3 JB |
204 | |
205 | /** | |
c7e196ba | 206 | * Removes the worker node key from the worker choice strategy in the context. |
97a2abc3 | 207 | * |
501aea93 | 208 | * @param workerNodeKey - The worker node key. |
97a2abc3 JB |
209 | * @returns `true` if the removal is successful, `false` otherwise. |
210 | */ | |
f06e48d8 | 211 | public remove (workerNodeKey: number): boolean { |
95c83464 JB |
212 | return ( |
213 | this.workerChoiceStrategies.get( | |
d710242d | 214 | this.workerChoiceStrategy |
17393ac8 | 215 | ) as IWorkerChoiceStrategy |
f06e48d8 | 216 | ).remove(workerNodeKey) |
95c83464 | 217 | } |
a20f0ba5 JB |
218 | |
219 | /** | |
220 | * Sets the worker choice strategies in the context options. | |
221 | * | |
222 | * @param opts - The worker choice strategy options. | |
223 | */ | |
224 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
8990357d | 225 | this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts } |
0509fd43 | 226 | for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) { |
a20f0ba5 | 227 | workerChoiceStrategy.setOptions(opts) |
0509fd43 | 228 | } |
a20f0ba5 | 229 | } |
bdaf31cd | 230 | } |