2 * Enumeration of worker choice strategies.
4 export const WorkerChoiceStrategies
= Object.freeze({
6 * Round robin worker selection strategy.
8 ROUND_ROBIN
: 'ROUND_ROBIN',
10 * Least used worker selection strategy.
12 LEAST_USED
: 'LEAST_USED',
14 * Least busy worker selection strategy.
16 LEAST_BUSY
: 'LEAST_BUSY',
18 * Least ELU worker selection strategy.
20 LEAST_ELU
: 'LEAST_ELU',
22 * Fair share worker selection strategy.
24 FAIR_SHARE
: 'FAIR_SHARE',
26 * Weighted round robin worker selection strategy.
28 WEIGHTED_ROUND_ROBIN
: 'WEIGHTED_ROUND_ROBIN',
30 * Interleaved weighted round robin worker selection strategy.
34 INTERLEAVED_WEIGHTED_ROUND_ROBIN
: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
38 * Worker choice strategy.
40 export type WorkerChoiceStrategy
= keyof
typeof WorkerChoiceStrategies
43 * Enumeration of measurements.
45 export const Measurements
= Object.freeze({
54 export type Measurement
= keyof
typeof Measurements
57 * Measurement options.
59 export interface MeasurementOptions
{
61 * Set measurement median.
63 readonly median
: boolean
67 * Worker choice strategy options.
69 export interface WorkerChoiceStrategyOptions
{
71 * Measurement to use in worker choice strategy supporting it.
73 readonly measurement
?: Measurement
77 * @defaultValue \{ median: false \}
79 readonly runTime
?: MeasurementOptions
83 * @defaultValue \{ median: false \}
85 readonly waitTime
?: MeasurementOptions
87 * Event loop utilization options.
89 * @defaultValue \{ median: false \}
91 readonly elu
?: MeasurementOptions
93 * Worker weights to use for weighted round robin worker selection strategies.
94 * A weight is tasks maximum execution time in milliseconds for a worker node.
96 * @defaultValue Weights computed automatically given the CPU performance.
98 weights
?: Record
<number, number>
102 * Worker choice strategy internal options.
106 export interface InternalWorkerChoiceStrategyOptions
107 extends WorkerChoiceStrategyOptions
{
109 * Number of worker choice retries to perform if no worker is eligible.
111 * @defaultValue pool maximum size
113 readonly retries
?: number
117 * Measurement statistics requirements.
121 export interface MeasurementStatisticsRequirements
{
123 * Requires measurement aggregate.
127 * Requires measurement average.
131 * Requires measurement median.
137 * Pool worker node worker usage statistics requirements.
141 export interface TaskStatisticsRequirements
{
143 * Tasks runtime requirements.
145 readonly runTime
: MeasurementStatisticsRequirements
147 * Tasks wait time requirements.
149 readonly waitTime
: MeasurementStatisticsRequirements
151 * Tasks event loop utilization requirements.
153 readonly elu
: MeasurementStatisticsRequirements
161 export interface StrategyPolicy
{
163 * Expects tasks execution on the newly created dynamic worker.
165 readonly dynamicWorkerUsage
: boolean
167 * Expects the newly created dynamic worker to be flagged as ready.
169 readonly dynamicWorkerReady
: boolean
173 * Worker choice strategy interface.
177 export interface IWorkerChoiceStrategy
{
181 readonly strategyPolicy
: StrategyPolicy
183 * Tasks statistics requirements.
185 readonly taskStatisticsRequirements
: TaskStatisticsRequirements
187 * Resets strategy internals.
189 * @returns `true` if the reset is successful, `false` otherwise.
191 readonly reset
: () => boolean
193 * Updates the worker node key strategy internals.
194 * This is called after a task has been executed on a worker node.
196 * @returns `true` if the update is successful, `false` otherwise.
198 readonly update
: (workerNodeKey
: number) => boolean
200 * Chooses a worker node in the pool and returns its key.
201 * If no worker nodes are not eligible, `undefined` is returned.
202 * If `undefined` is returned, the caller retry.
204 * @returns The worker node key or `undefined`.
206 readonly choose
: () => number | undefined
208 * Removes the worker node key from strategy internals.
210 * @param workerNodeKey - The worker node key.
211 * @returns `true` if the worker node key is removed, `false` otherwise.
213 readonly remove
: (workerNodeKey
: number) => boolean
215 * Sets the worker choice strategy options.
217 * @param opts - The worker choice strategy options.
219 readonly setOptions
: (opts
: WorkerChoiceStrategyOptions
) => void
221 * Whether the pool has worker nodes ready or not.
223 * @returns Whether the pool has worker nodes ready or not.
225 readonly hasPoolWorkerNodesReady
: () => boolean