feat: add statistics accounting to ELU fields
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
1 /**
2 * Enumeration of worker choice strategies.
3 */
4 export const WorkerChoiceStrategies = Object.freeze({
5 /**
6 * Round robin worker selection strategy.
7 */
8 ROUND_ROBIN: 'ROUND_ROBIN',
9 /**
10 * Least used worker selection strategy.
11 */
12 LEAST_USED: 'LEAST_USED',
13 /**
14 * Least busy worker selection strategy.
15 */
16 LEAST_BUSY: 'LEAST_BUSY',
17 /**
18 * Least ELU worker selection strategy.
19 *
20 * @experimental
21 */
22 LEAST_ELU: 'LEAST_ELU',
23 /**
24 * Fair share worker selection strategy.
25 */
26 FAIR_SHARE: 'FAIR_SHARE',
27 /**
28 * Weighted round robin worker selection strategy.
29 */
30 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
31 /**
32 * Interleaved weighted round robin worker selection strategy.
33 *
34 * @experimental
35 */
36 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
37 } as const)
38
39 /**
40 * Worker choice strategy.
41 */
42 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
43
44 /**
45 * Measurement options.
46 */
47 interface MeasurementOptions {
48 /**
49 * Set measurement median.
50 */
51 median: boolean
52 }
53
54 /**
55 * Worker choice strategy options.
56 */
57 export interface WorkerChoiceStrategyOptions {
58 /**
59 * Runtime options.
60 *
61 * @defaultValue \{ median: false \}
62 */
63 runTime?: MeasurementOptions
64 /**
65 * Wait time options.
66 *
67 * @defaultValue \{ median: false \}
68 */
69 waitTime?: MeasurementOptions
70 /**
71 * Event loop utilization options.
72 *
73 * @defaultValue \{ median: false \}
74 */
75 elu?: MeasurementOptions
76 /**
77 * Worker weights to use for weighted round robin worker selection strategy.
78 * Weight is the tasks maximum average or median runtime in milliseconds.
79 *
80 * @defaultValue Computed worker weights automatically given the CPU performance.
81 */
82 weights?: Record<number, number>
83 }
84
85 /**
86 * Measurement statistics requirements.
87 *
88 * @internal
89 */
90 interface MeasurementStatisticsRequirements {
91 /**
92 * Require measurement aggregate.
93 */
94 aggregate: boolean
95 /**
96 * Require measurement average.
97 */
98 average: boolean
99 /**
100 * Require measurement median.
101 */
102 median: boolean
103 }
104
105 /**
106 * Pool worker node worker usage statistics requirements.
107 *
108 * @internal
109 */
110 export interface TaskStatisticsRequirements {
111 /**
112 * Tasks runtime requirements.
113 */
114 runTime: MeasurementStatisticsRequirements
115 /**
116 * Tasks wait time requirements.
117 */
118 waitTime: MeasurementStatisticsRequirements
119 /**
120 * Tasks event loop utilization requirements.
121 */
122 elu: MeasurementStatisticsRequirements
123 }
124
125 /**
126 * Worker choice strategy interface.
127 */
128 export interface IWorkerChoiceStrategy {
129 /**
130 * Tasks statistics requirements.
131 */
132 readonly taskStatisticsRequirements: TaskStatisticsRequirements
133 /**
134 * Resets strategy internals.
135 *
136 * @returns `true` if the reset is successful, `false` otherwise.
137 */
138 reset: () => boolean
139 /**
140 * Updates the worker node key strategy internals.
141 *
142 * @returns `true` if the update is successful, `false` otherwise.
143 */
144 update: (workerNodeKey: number) => boolean
145 /**
146 * Chooses a worker node in the pool and returns its key.
147 *
148 * @returns The worker node key.
149 */
150 choose: () => number
151 /**
152 * Removes the worker node key from strategy internals.
153 *
154 * @param workerNodeKey - The worker node key.
155 * @returns `true` if the worker node key is removed, `false` otherwise.
156 */
157 remove: (workerNodeKey: number) => boolean
158 /**
159 * Sets the worker choice strategy options.
160 *
161 * @param opts - The worker choice strategy options.
162 */
163 setOptions: (opts: WorkerChoiceStrategyOptions) => void
164 }