1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
6 TaskStatisticsRequirements
,
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types.js'
10 import { WorkerChoiceStrategies
} from
'./selection-strategies-types.js'
12 getWorkerChoiceStrategiesRetries
,
13 getWorkerChoiceStrategy
14 } from
'./selection-strategies-utils.js'
17 * The worker choice strategies context.
19 * @typeParam Worker - Type of worker.
20 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
23 export class WorkerChoiceStrategiesContext
<
24 Worker
extends IWorker
,
29 * The number of worker choice strategies execution retries.
31 public retriesCount
: number
34 * The default worker choice strategy in the context.
36 private defaultWorkerChoiceStrategy
: WorkerChoiceStrategy
39 * The worker choice strategies registered in the context.
41 private readonly workerChoiceStrategies
: Map
<
47 * The maximum number of worker choice strategies execution retries.
49 private readonly retries
: number
52 * Worker choice strategies context constructor.
54 * @param pool - The pool instance.
55 * @param workerChoiceStrategies - The worker choice strategies. @defaultValue [WorkerChoiceStrategies.ROUND_ROBIN]
56 * @param opts - The worker choice strategy options.
59 private readonly pool
: IPool
<Worker
, Data
, Response
>,
60 workerChoiceStrategies
: WorkerChoiceStrategy
[] = [
61 WorkerChoiceStrategies
.ROUND_ROBIN
63 opts
?: WorkerChoiceStrategyOptions
65 this.execute
= this.execute
.bind(this)
66 this.defaultWorkerChoiceStrategy
= workerChoiceStrategies
[0]
67 this.workerChoiceStrategies
= new Map
<
71 for (const workerChoiceStrategy
of workerChoiceStrategies
) {
72 this.addWorkerChoiceStrategy(workerChoiceStrategy
, this.pool
, opts
)
75 this.retries
= getWorkerChoiceStrategiesRetries(this.pool
, opts
)
79 * Gets the active worker choice strategies policy in the context.
81 * @returns The strategies policy.
83 public getPolicy (): StrategyPolicy
{
84 const policies
: StrategyPolicy
[] = []
85 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
86 policies
.push(workerChoiceStrategy
.strategyPolicy
)
89 dynamicWorkerUsage
: policies
.some(p
=> p
.dynamicWorkerUsage
),
90 dynamicWorkerReady
: policies
.some(p
=> p
.dynamicWorkerReady
)
95 * Gets the active worker choice strategies in the context task statistics requirements.
97 * @returns The task statistics requirements.
99 public getTaskStatisticsRequirements (): TaskStatisticsRequirements
{
100 const taskStatisticsRequirements
: TaskStatisticsRequirements
[] = []
101 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
102 taskStatisticsRequirements
.push(
103 workerChoiceStrategy
.taskStatisticsRequirements
108 aggregate
: taskStatisticsRequirements
.some(r
=> r
.runTime
.aggregate
),
109 average
: taskStatisticsRequirements
.some(r
=> r
.runTime
.average
),
110 median
: taskStatisticsRequirements
.some(r
=> r
.runTime
.median
)
113 aggregate
: taskStatisticsRequirements
.some(r
=> r
.waitTime
.aggregate
),
114 average
: taskStatisticsRequirements
.some(r
=> r
.waitTime
.average
),
115 median
: taskStatisticsRequirements
.some(r
=> r
.waitTime
.median
)
118 aggregate
: taskStatisticsRequirements
.some(r
=> r
.elu
.aggregate
),
119 average
: taskStatisticsRequirements
.some(r
=> r
.elu
.average
),
120 median
: taskStatisticsRequirements
.some(r
=> r
.elu
.median
)
126 * Sets the default worker choice strategy to use in the context.
128 * @param workerChoiceStrategy - The default worker choice strategy to set.
129 * @param opts - The worker choice strategy options.
131 public setDefaultWorkerChoiceStrategy (
132 workerChoiceStrategy
: WorkerChoiceStrategy
,
133 opts
?: WorkerChoiceStrategyOptions
135 this.defaultWorkerChoiceStrategy
= workerChoiceStrategy
136 this.addWorkerChoiceStrategy(workerChoiceStrategy
, this.pool
, opts
)
140 * Updates the worker node key in the active worker choice strategies in the context internals.
142 * @returns `true` if the update is successful, `false` otherwise.
144 public update (workerNodeKey
: number): boolean {
145 const res
: boolean[] = []
146 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
147 res
.push(workerChoiceStrategy
.update(workerNodeKey
))
149 return res
.every(r
=> r
)
153 * Executes the worker choice strategy in the context algorithm.
155 * @param workerChoiceStrategy - The worker choice strategy algorithm to execute. @defaultValue this.defaultWorkerChoiceStrategy
156 * @returns The key of the worker node.
157 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
160 workerChoiceStrategy
: WorkerChoiceStrategy
= this
161 .defaultWorkerChoiceStrategy
163 return this.executeStrategy(
164 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165 this.workerChoiceStrategies
.get(workerChoiceStrategy
)!
170 * Executes the given worker choice strategy.
172 * @param workerChoiceStrategy - The worker choice strategy.
173 * @returns The key of the worker node.
174 * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined.
176 private executeStrategy (workerChoiceStrategy
: IWorkerChoiceStrategy
): number {
177 let workerNodeKey
: number | undefined
181 workerNodeKey
= workerChoiceStrategy
.choose()
182 if (workerNodeKey
== null && chooseCount
> 0) {
187 } while (workerNodeKey
== null && retriesCount
< this.retries
)
188 if (workerNodeKey
== null) {
190 `Worker node key chosen is null or undefined after ${retriesCount} retries`
197 * Removes the worker node key from the active worker choice strategies in the context.
199 * @param workerNodeKey - The worker node key.
200 * @returns `true` if the removal is successful, `false` otherwise.
202 public remove (workerNodeKey
: number): boolean {
203 const res
: boolean[] = []
204 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
205 res
.push(workerChoiceStrategy
.remove(workerNodeKey
))
207 return res
.every(r
=> r
)
211 * Sets the active worker choice strategies in the context options.
213 * @param opts - The worker choice strategy options.
215 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
216 for (const workerChoiceStrategy
of this.workerChoiceStrategies
.values()) {
217 workerChoiceStrategy
.setOptions(opts
)
221 private addWorkerChoiceStrategy (
222 workerChoiceStrategy
: WorkerChoiceStrategy
,
223 pool
: IPool
<Worker
, Data
, Response
>,
224 opts
?: WorkerChoiceStrategyOptions
225 ): Map
<WorkerChoiceStrategy
, IWorkerChoiceStrategy
> {
226 if (!this.workerChoiceStrategies
.has(workerChoiceStrategy
)) {
227 return this.workerChoiceStrategies
.set(
228 workerChoiceStrategy
,
229 getWorkerChoiceStrategy
<Worker
, Data
, Response
>(
230 workerChoiceStrategy
,
237 return this.workerChoiceStrategies
240 // private removeWorkerChoiceStrategy (
241 // workerChoiceStrategy: WorkerChoiceStrategy
243 // return this.workerChoiceStrategies.delete(workerChoiceStrategy)