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(
51 pool
.info
.maxSize
+ Object.keys(this.opts
?.weights
?? {}).length
55 this.execute
= this.execute
.bind(this)
56 this.workerChoiceStrategies
= new Map
<
61 WorkerChoiceStrategies
.ROUND_ROBIN
,
62 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
68 WorkerChoiceStrategies
.LEAST_USED
,
69 new (LeastUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
75 WorkerChoiceStrategies
.LEAST_BUSY
,
76 new (LeastBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
82 WorkerChoiceStrategies
.LEAST_ELU
,
83 new (LeastEluWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
89 WorkerChoiceStrategies
.FAIR_SHARE
,
90 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
96 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
97 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
104 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
,
105 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
115 * Gets the strategy policy in the context.
117 * @returns The strategy policy.
119 public getStrategyPolicy (): StrategyPolicy
{
121 this.workerChoiceStrategies
.get(
122 this.workerChoiceStrategy
123 ) as IWorkerChoiceStrategy
128 * Gets the worker choice strategy in the context task statistics requirements.
130 * @returns The task statistics requirements.
132 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
134 this.workerChoiceStrategies
.get(
135 this.workerChoiceStrategy
136 ) as IWorkerChoiceStrategy
137 ).taskStatisticsRequirements
141 * Sets the worker choice strategy to use in the context.
143 * @param workerChoiceStrategy - The worker choice strategy to set.
145 public setWorkerChoiceStrategy (
146 workerChoiceStrategy
: WorkerChoiceStrategy
148 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
149 this.workerChoiceStrategy
= workerChoiceStrategy
151 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
155 * Updates the worker node key in the worker choice strategy in the context internals.
157 * @returns `true` if the update is successful, `false` otherwise.
159 public update (workerNodeKey
: number): boolean {
161 this.workerChoiceStrategies
.get(
162 this.workerChoiceStrategy
163 ) as IWorkerChoiceStrategy
164 ).update(workerNodeKey
)
168 * Executes the worker choice strategy in the context algorithm.
170 * @returns The key of the worker node.
171 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
173 public execute (): number {
174 const workerChoiceStrategy
= this.workerChoiceStrategies
.get(
175 this.workerChoiceStrategy
176 ) as IWorkerChoiceStrategy
177 if (!workerChoiceStrategy
.hasPoolWorkerNodesReady()) {
178 return this.execute()
180 return this.executeStrategy(workerChoiceStrategy
)
184 * Executes the given worker choice strategy.
186 * @param workerChoiceStrategy - The worker choice strategy.
187 * @returns The key of the worker node.
188 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
190 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
191 let workerNodeKey
: number | undefined
195 workerNodeKey
= workerChoiceStrategy
.choose()
196 if (workerNodeKey
== null && chooseCount
> 0) {
201 workerNodeKey
== null &&
202 retriesCount
< (this.opts
?.retries
as number)
204 if (workerNodeKey
== null) {
206 `Worker node key chosen is null or undefined after ${retriesCount} retries`
213 * Removes the worker node key from the worker choice strategy in the context.
215 * @param workerNodeKey - The worker node key.
216 * @returns `true` if the removal is successful, `false` otherwise.
218 public remove (workerNodeKey
: number): boolean {
220 this.workerChoiceStrategies
.get(
221 this.workerChoiceStrategy
222 ) as IWorkerChoiceStrategy
223 ).remove(workerNodeKey
)
227 * Sets the worker choice strategies in the context options.
229 * @param pool - The pool instance.
230 * @param opts - The worker choice strategy options.
233 pool
: IPool
<Worker
, Data
, Response
>,
234 opts
?: InternalWorkerChoiceStrategyOptions
237 ...getDefaultInternalWorkerChoiceStrategyOptions(
238 pool
.info
.maxSize
+ Object.keys(opts
?.weights
?? {}).length
242 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
243 workerChoiceStrategy
.setOptions(this.opts
)