Merge branch 'master' into elu-strategy
[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 * Worker weights to use for weighted round robin worker selection strategy.
72 * Weight is the tasks maximum average or median runtime in milliseconds.
73 *
74 * @defaultValue Computed worker weights automatically given the CPU performance.
75 */
76 weights?: Record<number, number>
77 }
78
79 /**
80 * Measurement statistics requirements.
81 *
82 * @internal
83 */
84 interface MeasurementStatisticsRequirements {
85 /**
86 * Require measurement aggregate.
87 */
88 aggregate: boolean
89 /**
90 * Require measurement average.
91 */
92 average: boolean
93 /**
94 * Require measurement median.
95 */
96 median: boolean
97 }
98
99 /**
100 * Pool worker node worker usage statistics requirements.
101 *
102 * @internal
103 */
104 export interface TaskStatisticsRequirements {
105 /**
106 * Tasks runtime requirements.
107 */
108 runTime: MeasurementStatisticsRequirements
109 /**
110 * Tasks wait time requirements.
111 */
112 waitTime: MeasurementStatisticsRequirements
113 /**
114 * Event loop utilization.
115 */
116 elu: boolean
117 }
118
119 /**
120 * Worker choice strategy interface.
121 */
122 export interface IWorkerChoiceStrategy {
123 /**
124 * Tasks statistics requirements.
125 */
126 readonly taskStatisticsRequirements: TaskStatisticsRequirements
127 /**
128 * Resets strategy internals.
129 *
130 * @returns `true` if the reset is successful, `false` otherwise.
131 */
132 reset: () => boolean
133 /**
134 * Updates the worker node key strategy internals.
135 *
136 * @returns `true` if the update is successful, `false` otherwise.
137 */
138 update: (workerNodeKey: number) => boolean
139 /**
140 * Chooses a worker node in the pool and returns its key.
141 *
142 * @returns The worker node key.
143 */
144 choose: () => number
145 /**
146 * Removes the worker node key from strategy internals.
147 *
148 * @param workerNodeKey - The worker node key.
149 * @returns `true` if the worker node key is removed, `false` otherwise.
150 */
151 remove: (workerNodeKey: number) => boolean
152 /**
153 * Sets the worker choice strategy options.
154 *
155 * @param opts - The worker choice strategy options.
156 */
157 setOptions: (opts: WorkerChoiceStrategyOptions) => void
158 }