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