Merge branch 'master' into interleaved-weighted-round-robin-worker-choice-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 /**
737c6d97 10 * Less used worker selection strategy.
bdaf31cd 11 */
737c6d97 12 LESS_USED: 'LESS_USED',
168c526f
JB
13 /**
14 * Less busy worker selection strategy.
15 */
16 LESS_BUSY: 'LESS_BUSY',
23ff945a
JB
17 /**
18 * Fair share worker selection strategy.
19 */
20 FAIR_SHARE: 'FAIR_SHARE',
b3432a63
JB
21 /**
22 * Weighted round robin worker selection strategy.
23 */
feec6e8c
JB
24 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN',
25 /**
26 * Interleaved weighted round robin worker selection strategy.
27 */
28 INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
bdaf31cd
JB
29} as const)
30
31/**
32 * Worker choice strategy.
33 */
34export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
35
ff733df7
JB
36/**
37 * Worker choice strategy options.
38 */
39export interface WorkerChoiceStrategyOptions {
b0623665
JB
40 /**
41 * Use tasks median run time instead of average run time.
d29bce7c
JB
42 *
43 * @defaultValue false
b0623665 44 */
ff733df7 45 medRunTime?: boolean
08f3f44c
JB
46 /**
47 * Worker weights to use for weighted round robin worker selection strategy.
48 * Weight is the tasks maximum average or median runtime in milliseconds.
49 *
50 * @defaultValue Computed worker weights automatically given the CPU performance.
51 */
52 weights?: Record<number, number>
ff733df7
JB
53}
54
10fcfaf4 55/**
9cd39dd4 56 * Pool worker tasks usage statistics requirements.
71ebe93b
JB
57 *
58 * @internal
10fcfaf4 59 */
78cea37e 60export interface RequiredStatistics {
243a550a
JB
61 /**
62 * Require tasks run time.
63 */
10fcfaf4 64 runTime: boolean
243a550a
JB
65 /**
66 * Require tasks average run time.
67 */
c6bd2650 68 avgRunTime: boolean
243a550a
JB
69 /**
70 * Require tasks median run time.
71 */
78099a15 72 medRunTime: boolean
10fcfaf4
JB
73}
74
bdaf31cd
JB
75/**
76 * Worker choice strategy interface.
bdaf31cd 77 */
17393ac8 78export interface IWorkerChoiceStrategy {
10fcfaf4 79 /**
f06e48d8 80 * Required tasks usage statistics.
10fcfaf4 81 */
ea7a90d3
JB
82 readonly requiredStatistics: RequiredStatistics
83 /**
138d29a8 84 * Resets strategy internals.
a4958de2
JB
85 *
86 * @returns `true` if the reset is successful, `false` otherwise.
ea7a90d3 87 */
78cea37e 88 reset: () => boolean
138d29a8 89 /**
c7e196ba 90 * Updates the worker node key strategy internals.
138d29a8
JB
91 *
92 * @returns `true` if the update is successful, `false` otherwise.
93 */
a4958de2 94 update: (workerNodeKey: number) => boolean
bdaf31cd 95 /**
f06e48d8 96 * Chooses a worker node in the pool and returns its key.
a4958de2
JB
97 *
98 * @returns The worker node key.
bdaf31cd 99 */
c923ce56 100 choose: () => number
97a2abc3 101 /**
c7e196ba 102 * Removes the worker node key from strategy internals.
97a2abc3 103 *
f06e48d8 104 * @param workerNodeKey - The worker node key.
138d29a8 105 * @returns `true` if the worker node key is removed, `false` otherwise.
97a2abc3 106 */
f06e48d8 107 remove: (workerNodeKey: number) => boolean
a20f0ba5
JB
108 /**
109 * Sets the worker choice strategy options.
110 *
111 * @param opts - The worker choice strategy options.
112 */
113 setOptions: (opts: WorkerChoiceStrategyOptions) => void
bdaf31cd 114}