1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} 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
,
13 TaskStatisticsRequirements
,
15 WorkerChoiceStrategyOptions
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
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
49 this.opts
= { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
, ...opts
}
50 this.execute
= this.execute
.bind(this)
51 this.workerChoiceStrategies
= new Map
<
56 WorkerChoiceStrategies
.ROUND_ROBIN
,
57 new (RoundRobinWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
63 WorkerChoiceStrategies
.LEAST_USED
,
64 new (LeastUsedWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
70 WorkerChoiceStrategies
.LEAST_BUSY
,
71 new (LeastBusyWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
77 WorkerChoiceStrategies
.LEAST_ELU
,
78 new (LeastEluWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
84 WorkerChoiceStrategies
.FAIR_SHARE
,
85 new (FairShareWorkerChoiceStrategy
.bind(this))<Worker
, Data
, Response
>(
91 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
92 new (WeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
99 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
,
100 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy
.bind(this))<
110 * Gets the strategy policy in the context.
112 * @returns The strategy policy.
114 public getStrategyPolicy (): StrategyPolicy
{
116 this.workerChoiceStrategies
.get(
117 this.workerChoiceStrategy
118 ) as IWorkerChoiceStrategy
123 * Gets the worker choice strategy in the context task statistics requirements.
125 * @returns The task statistics requirements.
127 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
129 this.workerChoiceStrategies
.get(
130 this.workerChoiceStrategy
131 ) as IWorkerChoiceStrategy
132 ).taskStatisticsRequirements
136 * Sets the worker choice strategy to use in the context.
138 * @param workerChoiceStrategy - The worker choice strategy to set.
140 public setWorkerChoiceStrategy (
141 workerChoiceStrategy
: WorkerChoiceStrategy
143 if (this.workerChoiceStrategy
!== workerChoiceStrategy
) {
144 this.workerChoiceStrategy
= workerChoiceStrategy
146 this.workerChoiceStrategies
.get(this.workerChoiceStrategy
)?.reset()
150 * Updates the worker node key in the worker choice strategy in the context internals.
152 * @returns `true` if the update is successful, `false` otherwise.
154 public update (workerNodeKey
: number): boolean {
156 this.workerChoiceStrategies
.get(
157 this.workerChoiceStrategy
158 ) as IWorkerChoiceStrategy
159 ).update(workerNodeKey
)
163 * Executes the worker choice strategy in the context algorithm.
165 * @returns The key of the worker node.
166 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
167 * @throws {@link https://nodejs.org/api/errors.html#class-rangeerror} If the maximum consecutive worker choice strategy executions has been reached.
169 public execute (): number {
170 const workerChoiceStrategy
= this.workerChoiceStrategies
.get(
171 this.workerChoiceStrategy
172 ) as IWorkerChoiceStrategy
173 let workerNodeKey
: number | undefined
174 const maxExecutionCount
= 10000
175 let executionCount
= 0
179 if (workerChoiceStrategy
.hasPoolWorkerNodesReady()) {
180 workerNodeKey
= workerChoiceStrategy
.choose()
181 if (chooseCount
> 0) {
188 executionCount
< maxExecutionCount
&&
189 (!workerChoiceStrategy
.hasPoolWorkerNodesReady() ||
190 (workerNodeKey
== null && retriesCount
< (this.opts
.retries
as number)))
192 if (executionCount
>= maxExecutionCount
) {
193 throw new RangeError(
194 `Worker choice strategy consecutive executions has exceeded the maximum of ${maxExecutionCount}`
197 if (workerNodeKey
== null) {
199 `Worker node key chosen is null or undefined after ${retriesCount} retries`
206 * Removes the worker node key from the worker choice strategy in the context.
208 * @param workerNodeKey - The worker node key.
209 * @returns `true` if the removal is successful, `false` otherwise.
211 public remove (workerNodeKey
: number): boolean {
213 this.workerChoiceStrategies
.get(
214 this.workerChoiceStrategy
215 ) as IWorkerChoiceStrategy
216 ).remove(workerNodeKey
)
220 * Sets the worker choice strategies in the context options.
222 * @param opts - The worker choice strategy options.
224 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
225 this.opts
= { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
, ...opts
}
226 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
227 workerChoiceStrategy
.setOptions(opts
)