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