Merge pull request #748 from poolifier/interleaved-weighted-round-robin-worker-choice...
[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 * Fair share worker selection strategy.
19 */
20 FAIR_SHARE: 'FAIR_SHARE',
21 /**
22 * Weighted round robin worker selection strategy.
23 */
24 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
25 /**
26 * Interleaved weighted round robin worker selection strategy.
27 *
28 * @experimental
29 */
30 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
31 } as const)
32
33 /**
34 * Worker choice strategy.
35 */
36 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
37
38 /**
39 * Worker choice strategy options.
40 */
41 export interface WorkerChoiceStrategyOptions {
42 /**
43 * Use tasks median runtime instead of average runtime.
44 *
45 * @defaultValue false
46 */
47 medRunTime?: boolean
48 /**
49 * Use tasks median wait time instead of average runtime.
50 *
51 * @defaultValue false
52 */
53 medWaitTime?: boolean
54 /**
55 * Worker weights to use for weighted round robin worker selection strategy.
56 * Weight is the tasks maximum average or median runtime in milliseconds.
57 *
58 * @defaultValue Computed worker weights automatically given the CPU performance.
59 */
60 weights?: Record<number, number>
61 }
62
63 /**
64 * Pool worker tasks usage statistics requirements.
65 *
66 * @internal
67 */
68 export interface RequiredStatistics {
69 /**
70 * Require tasks runtime.
71 */
72 runTime: boolean
73 /**
74 * Require tasks average runtime.
75 */
76 avgRunTime: boolean
77 /**
78 * Require tasks median runtime.
79 */
80 medRunTime: boolean
81 /**
82 * Require tasks wait time.
83 */
84 waitTime: boolean
85 /**
86 * Require tasks average wait time.
87 */
88 avgWaitTime: boolean
89 /**
90 * Require tasks median wait time.
91 */
92 medWaitTime: boolean
93 }
94
95 /**
96 * Worker choice strategy interface.
97 */
98 export interface IWorkerChoiceStrategy {
99 /**
100 * Required tasks usage statistics.
101 */
102 readonly requiredStatistics: RequiredStatistics
103 /**
104 * Resets strategy internals.
105 *
106 * @returns `true` if the reset is successful, `false` otherwise.
107 */
108 reset: () => boolean
109 /**
110 * Updates the worker node key strategy internals.
111 *
112 * @returns `true` if the update is successful, `false` otherwise.
113 */
114 update: (workerNodeKey: number) => boolean
115 /**
116 * Chooses a worker node in the pool and returns its key.
117 *
118 * @returns The worker node key.
119 */
120 choose: () => number
121 /**
122 * Removes the worker node key from strategy internals.
123 *
124 * @param workerNodeKey - The worker node key.
125 * @returns `true` if the worker node key is removed, `false` otherwise.
126 */
127 remove: (workerNodeKey: number) => boolean
128 /**
129 * Sets the worker choice strategy options.
130 *
131 * @param opts - The worker choice strategy options.
132 */
133 setOptions: (opts: WorkerChoiceStrategyOptions) => void
134 }