Merge pull request #773 from poolifier/elu-strategy
[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.
19 *
20 * @experimental
21 */
22 LEAST_ELU: 'LEAST_ELU',
23ff945a
JB
23 /**
24 * Fair share worker selection strategy.
25 */
26 FAIR_SHARE: 'FAIR_SHARE',
b3432a63
JB
27 /**
28 * Weighted round robin worker selection strategy.
29 */
feec6e8c
JB
30 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
31 /**
32 * Interleaved weighted round robin worker selection strategy.
d3127e84
JB
33 *
34 * @experimental
feec6e8c
JB
35 */
36 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
bdaf31cd
JB
37} as const)
38
39/**
40 * Worker choice strategy.
41 */
42export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
43
932fc8be
JB
44/**
45 * Measurement options.
46 */
47interface MeasurementOptions {
48 /**
49 * Set measurement median.
50 */
51 median: boolean
52}
53
ff733df7
JB
54/**
55 * Worker choice strategy options.
56 */
57export interface WorkerChoiceStrategyOptions {
b0623665 58 /**
932fc8be 59 * Runtime options.
d29bce7c 60 *
932fc8be 61 * @defaultValue \{ median: false \}
b0623665 62 */
932fc8be 63 runTime?: MeasurementOptions
0567595a 64 /**
932fc8be 65 * Wait time options.
0567595a 66 *
932fc8be 67 * @defaultValue \{ median: false \}
0567595a 68 */
932fc8be 69 waitTime?: MeasurementOptions
08f3f44c
JB
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>
ff733df7
JB
77}
78
10fcfaf4 79/**
932fc8be 80 * Measurement statistics requirements.
71ebe93b
JB
81 *
82 * @internal
10fcfaf4 83 */
932fc8be 84interface MeasurementStatisticsRequirements {
243a550a 85 /**
932fc8be 86 * Require measurement aggregate.
243a550a 87 */
932fc8be 88 aggregate: boolean
243a550a 89 /**
932fc8be 90 * Require measurement average.
243a550a 91 */
932fc8be 92 average: boolean
0567595a 93 /**
932fc8be 94 * Require measurement median.
0567595a 95 */
932fc8be
JB
96 median: boolean
97}
98
99/**
100 * Pool worker node worker usage statistics requirements.
101 *
102 * @internal
103 */
104export interface TaskStatisticsRequirements {
0567595a 105 /**
932fc8be 106 * Tasks runtime requirements.
0567595a 107 */
932fc8be 108 runTime: MeasurementStatisticsRequirements
0567595a 109 /**
932fc8be 110 * Tasks wait time requirements.
0567595a 111 */
932fc8be 112 waitTime: MeasurementStatisticsRequirements
62c15a68
JB
113 /**
114 * Event loop utilization.
115 */
116 elu: boolean
10fcfaf4
JB
117}
118
bdaf31cd
JB
119/**
120 * Worker choice strategy interface.
bdaf31cd 121 */
17393ac8 122export interface IWorkerChoiceStrategy {
10fcfaf4 123 /**
87de9ff5 124 * Tasks statistics requirements.
10fcfaf4 125 */
87de9ff5 126 readonly taskStatisticsRequirements: TaskStatisticsRequirements
ea7a90d3 127 /**
138d29a8 128 * Resets strategy internals.
a4958de2
JB
129 *
130 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 131 */
78cea37e 132 reset: () => boolean
138d29a8 133 /**
c7e196ba 134 * Updates the worker node key strategy internals.
138d29a8
JB
135 *
136 * @returns `true` if the update is successful, `false` otherwise.
137 */
a4958de2 138 update: (workerNodeKey: number) => boolean
bdaf31cd 139 /**
f06e48d8 140 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
141 *
142 * @returns The worker node key.
bdaf31cd 143 */
c923ce56 144 choose: () => number
97a2abc3 145 /**
c7e196ba 146 * Removes the worker node key from strategy internals.
97a2abc3 147 *
f06e48d8 148 * @param workerNodeKey - The worker node key.
138d29a8 149 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 150 */
f06e48d8 151 remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
152 /**
153 * Sets the worker choice strategy options.
154 *
155 * @param opts - The worker choice strategy options.
156 */
157 setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 158}