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