1 import { getDefaultInternalWorkerChoiceStrategyOptions
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
4 import { FairShareWorkerChoiceStrategy
} from
'./fair-share-worker-choice-strategy'
5 import { InterleavedWeightedRoundRobinWorkerChoiceStrategy
} from
'./interleaved-weighted-round-robin-worker-choice-strategy'
6 import { LeastBusyWorkerChoiceStrategy
} from
'./least-busy-worker-choice-strategy'
7 import { LeastUsedWorkerChoiceStrategy
} from
'./least-used-worker-choice-strategy'
8 import { LeastEluWorkerChoiceStrategy
} from
'./least-elu-worker-choice-strategy'
9 import { RoundRobinWorkerChoiceStrategy
} from
'./round-robin-worker-choice-strategy'
11 IWorkerChoiceStrategy
,
12 InternalWorkerChoiceStrategyOptions
,
14 TaskStatisticsRequirements
,
16 } from
'./selection-strategies-types'
17 import { WorkerChoiceStrategies
} from
'./selection-strategies-types'
18 import { WeightedRoundRobinWorkerChoiceStrategy
} from
'./weighted-round-robin-worker-choice-strategy'
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
,
32 private readonly workerChoiceStrategies
: Map
<
38 * Worker choice strategy context constructor.
40 * @param pool - The pool instance.
41 * @param workerChoiceStrategy - The worker choice strategy.
42 * @param opts - The worker choice strategy options.
45 pool
: IPool
<Worker
, Data
, Response
>,
46 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
47 private opts
?: InternalWorkerChoiceStrategyOptions
50 ...getDefaultInternalWorkerChoiceStrategyOptions(
52 Object.keys((this.opts
?.weights
as Record
<number, number>) ?? {})
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))<
117 * Gets the strategy policy in the context.
119 * @returns The strategy policy.
121 public getStrategyPolicy (): StrategyPolicy
{
123 this.workerChoiceStrategies
.get(
124 this.workerChoiceStrategy
125 ) as IWorkerChoiceStrategy
130 * Gets the worker choice strategy in the context task statistics requirements.
132 * @returns The task statistics requirements.
134 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
136 this.workerChoiceStrategies
.get(
137 this.workerChoiceStrategy
138 ) as IWorkerChoiceStrategy
139 ).taskStatisticsRequirements
143 * Sets the worker choice strategy to use in the context.
145 * @param workerChoiceStrategy - The worker choice strategy to set.
147 public setWorkerChoiceStrategy (
148 workerChoiceStrategy
: WorkerChoiceStrategy
150 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
151 this.workerChoiceStrategy
= workerChoiceStrategy
153 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
157 * Updates the worker node key in the worker choice strategy in the context internals.
159 * @returns `true` if the update is successful, `false` otherwise.
161 public update (workerNodeKey
: number): boolean {
163 this.workerChoiceStrategies
.get(
164 this.workerChoiceStrategy
165 ) as IWorkerChoiceStrategy
166 ).update(workerNodeKey
)
170 * Executes the worker choice strategy in the context algorithm.
172 * @returns The key of the worker node.
173 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
175 public execute (): number {
176 const workerChoiceStrategy
= this.workerChoiceStrategies
.get(
177 this.workerChoiceStrategy
178 ) as IWorkerChoiceStrategy
179 if (!workerChoiceStrategy
.hasPoolWorkerNodesReady()) {
180 return this.execute()
182 return this.executeStrategy(workerChoiceStrategy
)
186 * Executes the given worker choice strategy.
188 * @param workerChoiceStrategy - The worker choice strategy.
189 * @returns The key of the worker node.
190 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
192 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
193 let workerNodeKey
: number | undefined
197 workerNodeKey
= workerChoiceStrategy
.choose()
198 if (workerNodeKey
== null && chooseCount
> 0) {
203 workerNodeKey
== null &&
204 retriesCount
< (this.opts
?.retries
as number)
206 if (workerNodeKey
== null) {
208 `Worker node key chosen is null or undefined after ${retriesCount} retries`
215 * Removes the worker node key from the worker choice strategy in the context.
217 * @param workerNodeKey - The worker node key.
218 * @returns `true` if the removal is successful, `false` otherwise.
220 public remove (workerNodeKey
: number): boolean {
222 this.workerChoiceStrategies
.get(
223 this.workerChoiceStrategy
224 ) as IWorkerChoiceStrategy
225 ).remove(workerNodeKey
)
229 * Sets the worker choice strategies in the context options.
231 * @param pool - The pool instance.
232 * @param opts - The worker choice strategy options.
235 pool
: IPool
<Worker
, Data
, Response
>,
236 opts
?: InternalWorkerChoiceStrategyOptions
239 ...getDefaultInternalWorkerChoiceStrategyOptions(
241 Object.keys((opts
?.weights
as Record
<number, number>) ?? {}).length
245 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
246 workerChoiceStrategy
.setOptions(this.opts
)