X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fselection-strategies-types.ts;h=c578ddc3f9fb24e289f5caf8fa542ec402d63834;hb=5df69fabd77b3ec4137a6382e2d84791bf0fe85d;hp=4fb2f3584aadffcc0f5224fdfb8ce6521a2979a8;hpb=3300e7bcb155358c2cc1eed6e4ac11581457616f;p=poolifier.git diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index 4fb2f358..c578ddc3 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -1,6 +1,3 @@ -import type { IPoolInternal } from '../pool-internal' -import type { IPoolWorker } from '../pool-worker' - /** * Enumeration of worker choice strategies. */ @@ -10,13 +7,19 @@ export const WorkerChoiceStrategies = Object.freeze({ */ ROUND_ROBIN: 'ROUND_ROBIN', /** - * Less used worker selection strategy. + * Least used worker selection strategy. + */ + LEAST_USED: 'LEAST_USED', + /** + * Least busy worker selection strategy. */ - LESS_USED: 'LESS_USED', + LEAST_BUSY: 'LEAST_BUSY', /** - * Less busy worker selection strategy. + * Least ELU worker selection strategy. + * + * @experimental */ - LESS_BUSY: 'LESS_BUSY', + LEAST_ELU: 'LEAST_ELU', /** * Fair share worker selection strategy. */ @@ -24,7 +27,13 @@ export const WorkerChoiceStrategies = Object.freeze({ /** * Weighted round robin worker selection strategy. */ - WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN' + WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN', + /** + * Interleaved weighted round robin worker selection strategy. + * + * @experimental + */ + INTERLEAVED_WEIGHTED_ROUND_ROBIN: 'INTERLEAVED_WEIGHTED_ROUND_ROBIN' } as const) /** @@ -33,48 +42,123 @@ export const WorkerChoiceStrategies = Object.freeze({ export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies /** - * Pool worker tasks usage statistics requirements. + * Measurement options. */ -export interface RequiredStatistics { - runTime: boolean - avgRunTime: boolean +interface MeasurementOptions { + /** + * Set measurement median. + */ + median: boolean } /** - * Worker choice strategy interface. + * Worker choice strategy options. */ -export interface IWorkerChoiceStrategy< - Worker extends IPoolWorker, - Data = unknown, - Response = unknown -> { +export interface WorkerChoiceStrategyOptions { + /** + * Runtime options. + * + * @defaultValue \{ median: false \} + */ + runTime?: MeasurementOptions /** - * The pool instance. - * @readonly + * Wait time options. + * + * @defaultValue \{ median: false \} */ - readonly pool: IPoolInternal + waitTime?: MeasurementOptions /** - * Is the pool attached to the strategy dynamic?. - * @readonly + * Event loop utilization options. + * + * @defaultValue \{ median: false \} */ - readonly isDynamicPool: boolean + elu?: MeasurementOptions /** - * Required pool tasks usage statistics. - * @readonly + * Worker weights to use for weighted round robin worker selection strategy. + * Weight is the tasks maximum average or median runtime in milliseconds. + * + * @defaultValue Computed worker weights automatically given the CPU performance. */ - readonly requiredStatistics: RequiredStatistics + weights?: Record +} + +/** + * Measurement statistics requirements. + * + * @internal + */ +interface MeasurementStatisticsRequirements { /** - * Resets strategy internals (counters, statistics, etc.). + * Require measurement aggregate. + */ + aggregate: boolean + /** + * Require measurement average. + */ + average: boolean + /** + * Require measurement median. + */ + median: boolean +} + +/** + * Pool worker node worker usage statistics requirements. + * + * @internal + */ +export interface TaskStatisticsRequirements { + /** + * Tasks runtime requirements. + */ + runTime: MeasurementStatisticsRequirements + /** + * Tasks wait time requirements. + */ + waitTime: MeasurementStatisticsRequirements + /** + * Tasks event loop utilization requirements. + */ + elu: MeasurementStatisticsRequirements +} + +/** + * Worker choice strategy interface. + */ +export interface IWorkerChoiceStrategy { + /** + * Tasks statistics requirements. + */ + readonly taskStatisticsRequirements: TaskStatisticsRequirements + /** + * Resets strategy internals. + * + * @returns `true` if the reset is successful, `false` otherwise. */ reset: () => boolean /** - * Chooses a worker in the pool and returns its key. + * Updates the worker node key strategy internals. + * + * @returns `true` if the update is successful, `false` otherwise. + */ + update: (workerNodeKey: number) => boolean + /** + * Chooses a worker node in the pool and returns its key. + * + * @returns The worker node key. */ choose: () => number /** - * Removes a worker reference from strategy internals. + * Removes the worker node key from strategy internals. + * + * @param workerNodeKey - The worker node key. + * @returns `true` if the worker node key is removed, `false` otherwise. + */ + remove: (workerNodeKey: number) => boolean + /** + * Sets the worker choice strategy options. * - * @param workerKey - The worker key. + * @param opts - The worker choice strategy options. */ - remove: (workerKey: number) => boolean + setOptions: (opts: WorkerChoiceStrategyOptions) => void }