Commit | Line | Data |
---|---|---|
bbeadd16 | 1 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
c4855468 | 2 | import type { IPool } from '../pool' |
f06e48d8 | 3 | import type { IWorker } from '../worker' |
51fe3d3c | 4 | import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy' |
feec6e8c | 5 | import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from './interleaved-weighted-round-robin-worker-choice-strategy' |
e4543b14 JB |
6 | import { LeastBusyWorkerChoiceStrategy } from './least-busy-worker-choice-strategy' |
7 | import { LeastUsedWorkerChoiceStrategy } from './least-used-worker-choice-strategy' | |
058a9457 | 8 | import { LeastEluWorkerChoiceStrategy } from './least-elu-worker-choice-strategy' |
51fe3d3c | 9 | import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy' |
bdaf31cd JB |
10 | import type { |
11 | IWorkerChoiceStrategy, | |
6c6afb84 | 12 | StrategyPolicy, |
87de9ff5 | 13 | TaskStatisticsRequirements, |
da309861 JB |
14 | WorkerChoiceStrategy, |
15 | WorkerChoiceStrategyOptions | |
bdaf31cd JB |
16 | } from './selection-strategies-types' |
17 | import { WorkerChoiceStrategies } from './selection-strategies-types' | |
51fe3d3c | 18 | import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy' |
bdaf31cd JB |
19 | |
20 | /** | |
21 | * The worker choice strategy context. | |
22 | * | |
38e795c1 | 23 | * @typeParam Worker - Type of worker. |
e102732c JB |
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. | |
bdaf31cd JB |
26 | */ |
27 | export class WorkerChoiceStrategyContext< | |
f06e48d8 | 28 | Worker extends IWorker, |
b2b1d84e JB |
29 | Data = unknown, |
30 | Response = unknown | |
bdaf31cd | 31 | > { |
b529c323 | 32 | private readonly workerChoiceStrategies: Map< |
95c83464 | 33 | WorkerChoiceStrategy, |
17393ac8 | 34 | IWorkerChoiceStrategy |
b529c323 | 35 | > |
bdaf31cd JB |
36 | |
37 | /** | |
38 | * Worker choice strategy context constructor. | |
39 | * | |
38e795c1 | 40 | * @param pool - The pool instance. |
d710242d | 41 | * @param workerChoiceStrategy - The worker choice strategy. |
da309861 | 42 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
43 | */ |
44 | public constructor ( | |
c4855468 | 45 | pool: IPool<Worker, Data, Response>, |
d710242d | 46 | private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN, |
bbeadd16 | 47 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
bdaf31cd | 48 | ) { |
7254e419 | 49 | this.execute = this.execute.bind(this) |
b529c323 JB |
50 | this.workerChoiceStrategies = new Map< |
51 | WorkerChoiceStrategy, | |
17393ac8 | 52 | IWorkerChoiceStrategy |
b529c323 JB |
53 | >([ |
54 | [ | |
55 | WorkerChoiceStrategies.ROUND_ROBIN, | |
7254e419 JB |
56 | new (RoundRobinWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( |
57 | pool, | |
58 | opts | |
59 | ) | |
b529c323 JB |
60 | ], |
61 | [ | |
e4543b14 JB |
62 | WorkerChoiceStrategies.LEAST_USED, |
63 | new (LeastUsedWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( | |
7254e419 JB |
64 | pool, |
65 | opts | |
66 | ) | |
b529c323 JB |
67 | ], |
68 | [ | |
e4543b14 JB |
69 | WorkerChoiceStrategies.LEAST_BUSY, |
70 | new (LeastBusyWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( | |
7254e419 JB |
71 | pool, |
72 | opts | |
73 | ) | |
b529c323 JB |
74 | ], |
75 | [ | |
058a9457 JB |
76 | WorkerChoiceStrategies.LEAST_ELU, |
77 | new (LeastEluWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( | |
78 | pool, | |
79 | opts | |
80 | ) | |
81 | ], | |
82 | [ | |
b529c323 | 83 | WorkerChoiceStrategies.FAIR_SHARE, |
7254e419 | 84 | new (FairShareWorkerChoiceStrategy.bind(this))<Worker, Data, Response>( |
da309861 JB |
85 | pool, |
86 | opts | |
87 | ) | |
7254e419 JB |
88 | ], |
89 | [ | |
90 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, | |
91 | new (WeightedRoundRobinWorkerChoiceStrategy.bind(this))< | |
92 | Worker, | |
93 | Data, | |
94 | Response | |
95 | >(pool, opts) | |
feec6e8c JB |
96 | ], |
97 | [ | |
98 | WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN, | |
99 | new (InterleavedWeightedRoundRobinWorkerChoiceStrategy.bind(this))< | |
100 | Worker, | |
101 | Data, | |
102 | Response | |
103 | >(pool, opts) | |
b529c323 JB |
104 | ] |
105 | ]) | |
bdaf31cd JB |
106 | } |
107 | ||
6c6afb84 JB |
108 | /** |
109 | * Gets the strategy policy in the context. | |
110 | * | |
111 | * @returns The strategy policy. | |
112 | */ | |
113 | public getStrategyPolicy (): StrategyPolicy { | |
114 | return ( | |
115 | this.workerChoiceStrategies.get( | |
116 | this.workerChoiceStrategy | |
117 | ) as IWorkerChoiceStrategy | |
118 | ).strategyPolicy | |
119 | } | |
120 | ||
97a2abc3 | 121 | /** |
87de9ff5 | 122 | * Gets the worker choice strategy task statistics requirements in the context. |
97a2abc3 | 123 | * |
87de9ff5 | 124 | * @returns The task statistics requirements. |
97a2abc3 | 125 | */ |
87de9ff5 | 126 | public getTaskStatisticsRequirements (): TaskStatisticsRequirements { |
95c83464 JB |
127 | return ( |
128 | this.workerChoiceStrategies.get( | |
d710242d | 129 | this.workerChoiceStrategy |
17393ac8 | 130 | ) as IWorkerChoiceStrategy |
87de9ff5 | 131 | ).taskStatisticsRequirements |
97a2abc3 JB |
132 | } |
133 | ||
bdaf31cd | 134 | /** |
bdede008 | 135 | * Sets the worker choice strategy to use in the context. |
bdaf31cd | 136 | * |
38e795c1 | 137 | * @param workerChoiceStrategy - The worker choice strategy to set. |
bdaf31cd JB |
138 | */ |
139 | public setWorkerChoiceStrategy ( | |
140 | workerChoiceStrategy: WorkerChoiceStrategy | |
141 | ): void { | |
d710242d JB |
142 | if (this.workerChoiceStrategy !== workerChoiceStrategy) { |
143 | this.workerChoiceStrategy = workerChoiceStrategy | |
b2b1d84e | 144 | } |
d710242d | 145 | this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset() |
bdaf31cd JB |
146 | } |
147 | ||
138d29a8 | 148 | /** |
c7e196ba | 149 | * Updates the worker node key in the worker choice strategy internals in the context. |
138d29a8 JB |
150 | * |
151 | * @returns `true` if the update is successful, `false` otherwise. | |
152 | */ | |
a4958de2 | 153 | public update (workerNodeKey: number): boolean { |
138d29a8 JB |
154 | return ( |
155 | this.workerChoiceStrategies.get( | |
156 | this.workerChoiceStrategy | |
157 | ) as IWorkerChoiceStrategy | |
a4958de2 | 158 | ).update(workerNodeKey) |
138d29a8 JB |
159 | } |
160 | ||
bdaf31cd | 161 | /** |
51fe3d3c | 162 | * Executes the worker choice strategy algorithm in the context. |
bdaf31cd | 163 | * |
f06e48d8 | 164 | * @returns The key of the worker node. |
13a332e6 | 165 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker node key is null or undefined. |
bdaf31cd | 166 | */ |
c923ce56 | 167 | public execute (): number { |
b0d6ed8f | 168 | const workerNodeKey = ( |
17393ac8 | 169 | this.workerChoiceStrategies.get( |
d710242d | 170 | this.workerChoiceStrategy |
17393ac8 JB |
171 | ) as IWorkerChoiceStrategy |
172 | ).choose() | |
b0d6ed8f | 173 | if (workerNodeKey == null) { |
aa9eede8 | 174 | throw new TypeError('Worker node key chosen is null or undefined') |
b0d6ed8f JB |
175 | } |
176 | return workerNodeKey | |
bdaf31cd | 177 | } |
97a2abc3 JB |
178 | |
179 | /** | |
c7e196ba | 180 | * Removes the worker node key from the worker choice strategy in the context. |
97a2abc3 | 181 | * |
501aea93 | 182 | * @param workerNodeKey - The worker node key. |
97a2abc3 JB |
183 | * @returns `true` if the removal is successful, `false` otherwise. |
184 | */ | |
f06e48d8 | 185 | public remove (workerNodeKey: number): boolean { |
95c83464 JB |
186 | return ( |
187 | this.workerChoiceStrategies.get( | |
d710242d | 188 | this.workerChoiceStrategy |
17393ac8 | 189 | ) as IWorkerChoiceStrategy |
f06e48d8 | 190 | ).remove(workerNodeKey) |
95c83464 | 191 | } |
a20f0ba5 JB |
192 | |
193 | /** | |
194 | * Sets the worker choice strategies in the context options. | |
195 | * | |
196 | * @param opts - The worker choice strategy options. | |
197 | */ | |
198 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
0509fd43 | 199 | for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) { |
a20f0ba5 | 200 | workerChoiceStrategy.setOptions(opts) |
0509fd43 | 201 | } |
a20f0ba5 | 202 | } |
bdaf31cd | 203 | } |