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