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 * The number of times the worker choice strategy in the context has been retried.
40 private choiceRetriesCount
= 0
43 * Worker choice strategy context constructor.
45 * @param pool - The pool instance.
46 * @param workerChoiceStrategy - The worker choice strategy.
47 * @param opts - The worker choice strategy options.
50 pool
: IPool
<Worker
, Data
, Response
>,
51 private workerChoiceStrategy
: WorkerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
,
52 private opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
54 this.opts
= { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
, ...opts
}
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 the worker node key is null or undefined.
173 public execute (): number {
174 const workerNodeKey
= (
175 this.workerChoiceStrategies
.get(
176 this.workerChoiceStrategy
177 ) as IWorkerChoiceStrategy
180 workerNodeKey
== null &&
181 this.choiceRetriesCount
< (this.opts
.choiceRetries
as number)
183 this.choiceRetriesCount
++
184 return this.execute()
185 } else if (workerNodeKey
== null) {
187 `Worker node key chosen is null or undefined after ${this.choiceRetriesCount} retries`
190 this.choiceRetriesCount
= 0
195 * Removes the worker node key from the worker choice strategy in the context.
197 * @param workerNodeKey - The worker node key.
198 * @returns `true` if the removal is successful, `false` otherwise.
200 public remove (workerNodeKey
: number): boolean {
202 this.workerChoiceStrategies
.get(
203 this.workerChoiceStrategy
204 ) as IWorkerChoiceStrategy
205 ).remove(workerNodeKey
)
209 * Sets the worker choice strategies in the context options.
211 * @param opts - The worker choice strategy options.
213 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
214 this.opts
= { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
, ...opts
}
215 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
216 workerChoiceStrategy
.setOptions(opts
)