| 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 | * Enumeration of measurements. |
| 46 | */ |
| 47 | export const Measurements = Object.freeze({ |
| 48 | runTime: 'runTime', |
| 49 | waitTime: 'waitTime', |
| 50 | elu: 'elu' |
| 51 | } as const) |
| 52 | |
| 53 | /** |
| 54 | * Measurement. |
| 55 | */ |
| 56 | export type Measurement = keyof typeof Measurements |
| 57 | |
| 58 | /** |
| 59 | * Measurement options. |
| 60 | */ |
| 61 | export interface MeasurementOptions { |
| 62 | /** |
| 63 | * Set measurement median. |
| 64 | */ |
| 65 | median: boolean |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Worker choice strategy options. |
| 70 | */ |
| 71 | export interface WorkerChoiceStrategyOptions { |
| 72 | /** |
| 73 | * Measurement to use for worker choice strategy. |
| 74 | */ |
| 75 | measurement?: Measurement |
| 76 | /** |
| 77 | * Runtime options. |
| 78 | * |
| 79 | * @defaultValue \{ median: false \} |
| 80 | */ |
| 81 | runTime?: MeasurementOptions |
| 82 | /** |
| 83 | * Wait time options. |
| 84 | * |
| 85 | * @defaultValue \{ median: false \} |
| 86 | */ |
| 87 | waitTime?: MeasurementOptions |
| 88 | /** |
| 89 | * Event loop utilization options. |
| 90 | * |
| 91 | * @defaultValue \{ median: false \} |
| 92 | */ |
| 93 | elu?: MeasurementOptions |
| 94 | /** |
| 95 | * Worker weights to use for weighted round robin worker selection strategy. |
| 96 | * Weight is the tasks maximum average or median runtime in milliseconds. |
| 97 | * |
| 98 | * @defaultValue Computed worker weights automatically given the CPU performance. |
| 99 | */ |
| 100 | weights?: Record<number, number> |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Measurement statistics requirements. |
| 105 | * |
| 106 | * @internal |
| 107 | */ |
| 108 | export interface MeasurementStatisticsRequirements { |
| 109 | /** |
| 110 | * Require measurement aggregate. |
| 111 | */ |
| 112 | aggregate: boolean |
| 113 | /** |
| 114 | * Require measurement average. |
| 115 | */ |
| 116 | average: boolean |
| 117 | /** |
| 118 | * Require measurement median. |
| 119 | */ |
| 120 | median: boolean |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Pool worker node worker usage statistics requirements. |
| 125 | * |
| 126 | * @internal |
| 127 | */ |
| 128 | export interface TaskStatisticsRequirements { |
| 129 | /** |
| 130 | * Tasks runtime requirements. |
| 131 | */ |
| 132 | runTime: MeasurementStatisticsRequirements |
| 133 | /** |
| 134 | * Tasks wait time requirements. |
| 135 | */ |
| 136 | waitTime: MeasurementStatisticsRequirements |
| 137 | /** |
| 138 | * Tasks event loop utilization requirements. |
| 139 | */ |
| 140 | elu: MeasurementStatisticsRequirements |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Worker choice strategy interface. |
| 145 | */ |
| 146 | export interface IWorkerChoiceStrategy { |
| 147 | /** |
| 148 | * Tasks statistics requirements. |
| 149 | */ |
| 150 | readonly taskStatisticsRequirements: TaskStatisticsRequirements |
| 151 | /** |
| 152 | * Resets strategy internals. |
| 153 | * |
| 154 | * @returns `true` if the reset is successful, `false` otherwise. |
| 155 | */ |
| 156 | reset: () => boolean |
| 157 | /** |
| 158 | * Updates the worker node key strategy internals. |
| 159 | * |
| 160 | * @returns `true` if the update is successful, `false` otherwise. |
| 161 | */ |
| 162 | update: (workerNodeKey: number) => boolean |
| 163 | /** |
| 164 | * Chooses a worker node in the pool and returns its key. |
| 165 | * |
| 166 | * @returns The worker node key. |
| 167 | */ |
| 168 | choose: () => number |
| 169 | /** |
| 170 | * Removes the worker node key from strategy internals. |
| 171 | * |
| 172 | * @param workerNodeKey - The worker node key. |
| 173 | * @returns `true` if the worker node key is removed, `false` otherwise. |
| 174 | */ |
| 175 | remove: (workerNodeKey: number) => boolean |
| 176 | /** |
| 177 | * Sets the worker choice strategy options. |
| 178 | * |
| 179 | * @param opts - The worker choice strategy options. |
| 180 | */ |
| 181 | setOptions: (opts: WorkerChoiceStrategyOptions) => void |
| 182 | } |