refactor: remove unneeded type casting
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
CommitLineData
bdaf31cd
JB
1/**
2 * Enumeration of worker choice strategies.
3 */
4export const WorkerChoiceStrategies = Object.freeze({
5 /**
6 * Round robin worker selection strategy.
7 */
8 ROUND_ROBIN: 'ROUND_ROBIN',
9 /**
e4543b14 10 * Least used worker selection strategy.
bdaf31cd 11 */
e4543b14 12 LEAST_USED: 'LEAST_USED',
168c526f 13 /**
e4543b14 14 * Least busy worker selection strategy.
168c526f 15 */
e4543b14 16 LEAST_BUSY: 'LEAST_BUSY',
058a9457
JB
17 /**
18 * Least ELU worker selection strategy.
058a9457
JB
19 */
20 LEAST_ELU: 'LEAST_ELU',
23ff945a
JB
21 /**
22 * Fair share worker selection strategy.
23 */
24 FAIR_SHARE: 'FAIR_SHARE',
b3432a63
JB
25 /**
26 * Weighted round robin worker selection strategy.
27 */
feec6e8c
JB
28 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
29 /**
30 * Interleaved weighted round robin worker selection strategy.
d3127e84
JB
31 *
32 * @experimental
feec6e8c
JB
33 */
34 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
bdaf31cd
JB
35} as const)
36
37/**
38 * Worker choice strategy.
39 */
40export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
41
9adcefab
JB
42/**
43 * Enumeration of measurements.
44 */
45export const Measurements = Object.freeze({
46 runTime: 'runTime',
47 waitTime: 'waitTime',
48 elu: 'elu'
49} as const)
50
51/**
52 * Measurement.
53 */
54export type Measurement = keyof typeof Measurements
55
932fc8be
JB
56/**
57 * Measurement options.
58 */
9adcefab 59export interface MeasurementOptions {
932fc8be
JB
60 /**
61 * Set measurement median.
62 */
eb7bf744 63 readonly median: boolean
932fc8be
JB
64}
65
ff733df7
JB
66/**
67 * Worker choice strategy options.
68 */
69export interface WorkerChoiceStrategyOptions {
8990357d
JB
70 /**
71 * Measurement to use in worker choice strategy supporting it.
9adcefab 72 */
eb7bf744 73 readonly measurement?: Measurement
b0623665 74 /**
932fc8be 75 * Runtime options.
d29bce7c 76 *
932fc8be 77 * @defaultValue \{ median: false \}
b0623665 78 */
eb7bf744 79 readonly runTime?: MeasurementOptions
0567595a 80 /**
932fc8be 81 * Wait time options.
0567595a 82 *
932fc8be 83 * @defaultValue \{ median: false \}
0567595a 84 */
eb7bf744 85 readonly waitTime?: MeasurementOptions
5df69fab
JB
86 /**
87 * Event loop utilization options.
88 *
89 * @defaultValue \{ median: false \}
90 */
eb7bf744 91 readonly elu?: MeasurementOptions
08f3f44c 92 /**
313f98ac 93 * Worker weights to use for weighted round robin worker selection strategies.
98446b39 94 * A weight is tasks maximum execution time in milliseconds for a worker node.
08f3f44c 95 *
313f98ac 96 * @defaultValue Weights computed automatically given the CPU performance.
08f3f44c 97 */
eb7bf744 98 readonly weights?: Record<number, number>
ff733df7
JB
99}
100
26ce26ca
JB
101/**
102 * Worker choice strategy internal options.
103 *
104 * @internal
105 */
106export interface InternalWorkerChoiceStrategyOptions
107 extends WorkerChoiceStrategyOptions {
108 /**
109 * Number of worker choice retries to perform if no worker is eligible.
110 *
111 * @defaultValue pool maximum size
112 */
113 readonly retries?: number
114}
115
10fcfaf4 116/**
932fc8be 117 * Measurement statistics requirements.
71ebe93b
JB
118 *
119 * @internal
10fcfaf4 120 */
9adcefab 121export interface MeasurementStatisticsRequirements {
243a550a 122 /**
64383951 123 * Requires measurement aggregate.
243a550a 124 */
932fc8be 125 aggregate: boolean
243a550a 126 /**
64383951 127 * Requires measurement average.
243a550a 128 */
932fc8be 129 average: boolean
0567595a 130 /**
64383951 131 * Requires measurement median.
0567595a 132 */
932fc8be
JB
133 median: boolean
134}
135
136/**
137 * Pool worker node worker usage statistics requirements.
138 *
139 * @internal
140 */
141export interface TaskStatisticsRequirements {
0567595a 142 /**
932fc8be 143 * Tasks runtime requirements.
0567595a 144 */
eb7bf744 145 readonly runTime: MeasurementStatisticsRequirements
0567595a 146 /**
932fc8be 147 * Tasks wait time requirements.
0567595a 148 */
eb7bf744 149 readonly waitTime: MeasurementStatisticsRequirements
62c15a68 150 /**
5df69fab 151 * Tasks event loop utilization requirements.
62c15a68 152 */
eb7bf744 153 readonly elu: MeasurementStatisticsRequirements
10fcfaf4
JB
154}
155
6c6afb84
JB
156/**
157 * Strategy policy.
247ce01e
JB
158 *
159 * @internal
6c6afb84
JB
160 */
161export interface StrategyPolicy {
162 /**
b1aae695 163 * Expects tasks execution on the newly created dynamic worker.
6c6afb84 164 */
b1aae695
JB
165 readonly dynamicWorkerUsage: boolean
166 /**
167 * Expects the newly created dynamic worker to be flagged as ready.
168 */
169 readonly dynamicWorkerReady: boolean
6c6afb84
JB
170}
171
bdaf31cd
JB
172/**
173 * Worker choice strategy interface.
922a7350
JB
174 *
175 * @internal
bdaf31cd 176 */
17393ac8 177export interface IWorkerChoiceStrategy {
6c6afb84
JB
178 /**
179 * Strategy policy.
180 */
181 readonly strategyPolicy: StrategyPolicy
10fcfaf4 182 /**
87de9ff5 183 * Tasks statistics requirements.
10fcfaf4 184 */
87de9ff5 185 readonly taskStatisticsRequirements: TaskStatisticsRequirements
ea7a90d3 186 /**
138d29a8 187 * Resets strategy internals.
a4958de2
JB
188 *
189 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 190 */
4b628b48 191 readonly reset: () => boolean
138d29a8 192 /**
c7e196ba 193 * Updates the worker node key strategy internals.
549ee69b 194 * This is called after a task has been executed on a worker node.
138d29a8
JB
195 *
196 * @returns `true` if the update is successful, `false` otherwise.
197 */
4b628b48 198 readonly update: (workerNodeKey: number) => boolean
bdaf31cd 199 /**
f06e48d8 200 * Chooses a worker node in the pool and returns its key.
f6ccb7db
JB
201 * If no worker nodes are not eligible, `undefined` is returned.
202 * If `undefined` is returned, the caller retry.
a4958de2 203 *
b1aae695 204 * @returns The worker node key or `undefined`.
bdaf31cd 205 */
b1aae695 206 readonly choose: () => number | undefined
97a2abc3 207 /**
c7e196ba 208 * Removes the worker node key from strategy internals.
97a2abc3 209 *
f06e48d8 210 * @param workerNodeKey - The worker node key.
138d29a8 211 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 212 */
4b628b48 213 readonly remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
214 /**
215 * Sets the worker choice strategy options.
216 *
217 * @param opts - The worker choice strategy options.
218 */
4b628b48 219 readonly setOptions: (opts: WorkerChoiceStrategyOptions) => void
fb5a7307
JB
220 /**
221 * Whether the pool has worker nodes ready or not.
222 *
223 * @returns Whether the pool has worker nodes ready or not.
224 */
225 readonly hasPoolWorkerNodesReady: () => boolean
bdaf31cd 226}