Merge branch 'master' into interleaved-weighted-round-robin-worker-choice-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 * 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 * Worker weights to use for weighted round robin worker selection strategy.
50 * Weight is the tasks maximum average or median runtime in milliseconds.
51 *
52 * @defaultValue Computed worker weights automatically given the CPU performance.
53 */
54 weights?: Record<number, number>
55 }
56
57 /**
58 * Pool worker tasks usage statistics requirements.
59 *
60 * @internal
61 */
62 export interface RequiredStatistics {
63 /**
64 * Require tasks runtime.
65 */
66 runTime: boolean
67 /**
68 * Require tasks average runtime.
69 */
70 avgRunTime: boolean
71 /**
72 * Require tasks median runtime.
73 */
74 medRunTime: boolean
75 }
76
77 /**
78 * Worker choice strategy interface.
79 */
80 export interface IWorkerChoiceStrategy {
81 /**
82 * Required tasks usage statistics.
83 */
84 readonly requiredStatistics: RequiredStatistics
85 /**
86 * Resets strategy internals.
87 *
88 * @returns `true` if the reset is successful, `false` otherwise.
89 */
90 reset: () => boolean
91 /**
92 * Updates the worker node key strategy internals.
93 *
94 * @returns `true` if the update is successful, `false` otherwise.
95 */
96 update: (workerNodeKey: number) => boolean
97 /**
98 * Chooses a worker node in the pool and returns its key.
99 *
100 * @returns The worker node key.
101 */
102 choose: () => number
103 /**
104 * Removes the worker node key from strategy internals.
105 *
106 * @param workerNodeKey - The worker node key.
107 * @returns `true` if the worker node key is removed, `false` otherwise.
108 */
109 remove: (workerNodeKey: number) => boolean
110 /**
111 * Sets the worker choice strategy options.
112 *
113 * @param opts - The worker choice strategy options.
114 */
115 setOptions: (opts: WorkerChoiceStrategyOptions) => void
116 }