1 import { buildInternalWorkerChoiceStrategyOptions
} from
'../../utils.js'
2 import type { IPool
} from
'../pool.js'
3 import type { IWorker
} from
'../worker.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
,
12 InternalWorkerChoiceStrategyOptions
,
14 TaskStatisticsRequirements
,
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
,
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
49 this.opts
= buildInternalWorkerChoiceStrategyOptions(
53 this.execute
= this.execute
.bind(this)
54 this.workerChoiceStrategies
= new Map
<
59 WorkerChoiceStrategies
.ROUND_ROBIN
,
60 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
66 WorkerChoiceStrategies
.LEAST_USED
,
67 new (LeastUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
73 WorkerChoiceStrategies
.LEAST_BUSY
,
74 new (LeastBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
80 WorkerChoiceStrategies
.LEAST_ELU
,
81 new (LeastEluWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
87 WorkerChoiceStrategies
.FAIR_SHARE
,
88 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
94 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
95 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
102 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
,
103 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
113 * Gets the strategy policy in the context.
115 * @returns The strategy policy.
117 public getStrategyPolicy (): StrategyPolicy
{
118 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
119 return this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)!
124 * Gets the worker choice strategy in the context task statistics requirements.
126 * @returns The task statistics requirements.
128 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
129 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
130 return this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)!
131 .taskStatisticsRequirements
135 * Sets the worker choice strategy to use in the context.
137 * @param workerChoiceStrategy - The worker choice strategy to set.
139 public setWorkerChoiceStrategy (
140 workerChoiceStrategy
: WorkerChoiceStrategy
142 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
143 this.workerChoiceStrategy
= workerChoiceStrategy
145 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
149 * Updates the worker node key in the worker choice strategy in the context internals.
151 * @returns `true` if the update is successful, `false` otherwise.
153 public update (workerNodeKey
: number): boolean {
154 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
155 return this.workerChoiceStrategies
156 .get(this.workerChoiceStrategy
)!
157 .update(workerNodeKey
)
161 * Executes the worker choice strategy in the context algorithm.
163 * @returns The key of the worker node.
164 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
166 public execute (): number {
167 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
168 const workerChoiceStrategy
= this.workerChoiceStrategies
.get(
169 this.workerChoiceStrategy
171 if (!workerChoiceStrategy
.hasPoolWorkerNodesReady()) {
172 return this.execute()
174 return this.executeStrategy(workerChoiceStrategy
)
178 * Executes the given worker choice strategy.
180 * @param workerChoiceStrategy - The worker choice strategy.
181 * @returns The key of the worker node.
182 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
184 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
185 let workerNodeKey
: number | undefined
189 workerNodeKey
= workerChoiceStrategy
.choose()
190 if (workerNodeKey
== null && chooseCount
> 0) {
194 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
195 } while (workerNodeKey
== null && retriesCount
< this.opts
!.retries
!)
196 if (workerNodeKey
== null) {
198 `Worker node key chosen is null or undefined after ${retriesCount} retries`
205 * Removes the worker node key from the worker choice strategy in the context.
207 * @param workerNodeKey - The worker node key.
208 * @returns `true` if the removal is successful, `false` otherwise.
210 public remove (workerNodeKey
: number): boolean {
211 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
212 return this.workerChoiceStrategies
213 .get(this.workerChoiceStrategy
)!
214 .remove(workerNodeKey
)
218 * Sets the worker choice strategies in the context options.
220 * @param pool - The pool instance.
221 * @param opts - The worker choice strategy options.
224 pool
: IPool
<Worker
, Data
, Response
>,
225 opts
?: InternalWorkerChoiceStrategyOptions
227 this.opts
= buildInternalWorkerChoiceStrategyOptions(
231 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
232 workerChoiceStrategy
.setOptions(this.opts
)