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'
11 IWorkerChoiceStrategy
,
13 TaskStatisticsRequirements
,
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'
21 * The worker choice strategy context.
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.
27 export class WorkerChoiceStrategyContext
<
28 Worker
extends IWorker
,
33 * The worker choice strategies registered in the context.
35 private readonly workerChoiceStrategies
: Map
<
41 * The number of worker choice strategy execution retries.
43 private readonly retries
: number
46 * Worker choice strategy context constructor.
48 * @param pool - The pool instance.
49 * @param workerChoiceStrategy - The worker choice strategy.
50 * @param opts - The worker choice strategy options.
53 pool
: IPool
<Worker
, Data
, Response
>,
54 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
55 opts
?: WorkerChoiceStrategyOptions
57 this.execute
= this.execute
.bind(this)
58 this.workerChoiceStrategies
= new Map
<
63 WorkerChoiceStrategies
.ROUND_ROBIN
,
64 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
70 WorkerChoiceStrategies
.LEAST_USED
,
71 new (LeastUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
77 WorkerChoiceStrategies
.LEAST_BUSY
,
78 new (LeastBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
84 WorkerChoiceStrategies
.LEAST_ELU
,
85 new (LeastEluWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
91 WorkerChoiceStrategies
.FAIR_SHARE
,
92 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
98 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
99 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
106 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
,
107 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
114 this.retries
= getWorkerChoiceStrategyRetries(pool
, opts
)
118 * Gets the strategy policy in the context.
120 * @returns The strategy policy.
122 public getStrategyPolicy (): StrategyPolicy
{
123 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
124 return this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)!
129 * Gets the worker choice strategy in the context task statistics requirements.
131 * @returns The task statistics requirements.
133 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
134 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
135 return this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)!
136 .taskStatisticsRequirements
140 * Sets the worker choice strategy to use in the context.
142 * @param workerChoiceStrategy - The worker choice strategy to set.
144 public setWorkerChoiceStrategy (
145 workerChoiceStrategy
: WorkerChoiceStrategy
147 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
148 this.workerChoiceStrategy
= workerChoiceStrategy
150 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
154 * Updates the worker node key in the worker choice strategy in the context internals.
156 * @returns `true` if the update is successful, `false` otherwise.
158 public update (workerNodeKey
: number): boolean {
159 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
160 return this.workerChoiceStrategies
161 .get(this.workerChoiceStrategy
)!
162 .update(workerNodeKey
)
166 * Executes the worker choice strategy in the context algorithm.
168 * @returns The key of the worker node.
169 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
171 public execute (): number {
172 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
173 const workerChoiceStrategy
= this.workerChoiceStrategies
.get(
174 this.workerChoiceStrategy
176 if (!workerChoiceStrategy
.hasPoolWorkerNodesReady()) {
177 return this.execute()
179 return this.executeStrategy(workerChoiceStrategy
)
183 * Executes the given worker choice strategy.
185 * @param workerChoiceStrategy - The worker choice strategy.
186 * @returns The key of the worker node.
187 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
189 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
190 let workerNodeKey
: number | undefined
194 workerNodeKey
= workerChoiceStrategy
.choose()
195 if (workerNodeKey
== null && chooseCount
> 0) {
199 } while (workerNodeKey
== null && retriesCount
< this.retries
)
200 if (workerNodeKey
== null) {
202 `Worker node key chosen is null or undefined after ${retriesCount} retries`
209 * Removes the worker node key from the worker choice strategy in the context.
211 * @param workerNodeKey - The worker node key.
212 * @returns `true` if the removal is successful, `false` otherwise.
214 public remove (workerNodeKey
: number): boolean {
215 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
216 return this.workerChoiceStrategies
217 .get(this.workerChoiceStrategy
)!
218 .remove(workerNodeKey
)
222 * Sets the worker choice strategies in the context options.
224 * @param opts - The worker choice strategy options.
226 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
227 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
228 workerChoiceStrategy
.setOptions(opts
)