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 strategy instances 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 computed retries the worker node key is null or undefined.
171 public execute (): number {
172 return this.executeStrategy(
173 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
174 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)!
179 * Executes the given worker choice strategy.
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 computed retries the worker node key is null or undefined.
185 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
186 let workerNodeKey
: number | undefined
190 workerNodeKey
= workerChoiceStrategy
.choose()
191 if (workerNodeKey
!= null && workerNodeKey
< 0) {
192 workerNodeKey
= undefined
194 if (workerNodeKey
== null && chooseCount
> 0) {
198 } while (workerNodeKey
== null && retriesCount
< this.retries
)
199 if (workerNodeKey
== null) {
201 `Worker node key chosen is null or undefined after ${retriesCount} retries`
208 * Removes the worker node key from the worker choice strategy in the context.
210 * @param workerNodeKey - The worker node key.
211 * @returns `true` if the removal is successful, `false` otherwise.
213 public remove (workerNodeKey
: number): boolean {
214 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
215 return this.workerChoiceStrategies
216 .get(this.workerChoiceStrategy
)!
217 .remove(workerNodeKey
)
221 * Sets the worker choice strategies in the context options.
223 * @param opts - The worker choice strategy options.
225 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
226 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
227 workerChoiceStrategy
.setOptions(opts
)