| 1 | import type { IPool } from '../pool.js' |
| 2 | import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js' |
| 3 | import type { IWorker } from '../worker.js' |
| 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' |
| 5 | import type { |
| 6 | IWorkerChoiceStrategy, |
| 7 | TaskStatisticsRequirements, |
| 8 | WorkerChoiceStrategyOptions, |
| 9 | } from './selection-strategies-types.js' |
| 10 | |
| 11 | /** |
| 12 | * Selects the next worker with a weighted round robin scheduling algorithm. |
| 13 | * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin. |
| 14 | * @typeParam Worker - Type of worker which manages the strategy. |
| 15 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
| 16 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 17 | */ |
| 18 | export class WeightedRoundRobinWorkerChoiceStrategy< |
| 19 | Worker extends IWorker, |
| 20 | Data = unknown, |
| 21 | Response = unknown |
| 22 | > |
| 23 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> |
| 24 | implements IWorkerChoiceStrategy { |
| 25 | /** @inheritDoc */ |
| 26 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
| 27 | runTime: { |
| 28 | aggregate: true, |
| 29 | average: true, |
| 30 | median: false, |
| 31 | }, |
| 32 | waitTime: { |
| 33 | aggregate: true, |
| 34 | average: true, |
| 35 | median: false, |
| 36 | }, |
| 37 | elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Worker node virtual execution time. |
| 42 | */ |
| 43 | private workerNodeVirtualTaskExecutionTime = 0 |
| 44 | |
| 45 | /** @inheritDoc */ |
| 46 | public constructor ( |
| 47 | pool: IPool<Worker, Data, Response>, |
| 48 | opts?: WorkerChoiceStrategyOptions |
| 49 | ) { |
| 50 | super(pool, opts) |
| 51 | this.setTaskStatisticsRequirements(this.opts) |
| 52 | } |
| 53 | |
| 54 | /** @inheritDoc */ |
| 55 | public reset (): boolean { |
| 56 | this.resetWorkerNodeKeyProperties() |
| 57 | this.workerNodeVirtualTaskExecutionTime = 0 |
| 58 | return true |
| 59 | } |
| 60 | |
| 61 | /** @inheritDoc */ |
| 62 | public update (): boolean { |
| 63 | return true |
| 64 | } |
| 65 | |
| 66 | /** @inheritDoc */ |
| 67 | public choose (): number | undefined { |
| 68 | this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) |
| 69 | this.weightedRoundRobinNextWorkerNodeKey() |
| 70 | this.checkNextWorkerNodeKey() |
| 71 | return this.nextWorkerNodeKey |
| 72 | } |
| 73 | |
| 74 | /** @inheritDoc */ |
| 75 | public remove (workerNodeKey: number): boolean { |
| 76 | if (this.pool.workerNodes.length === 0) { |
| 77 | this.reset() |
| 78 | return true |
| 79 | } |
| 80 | if (this.nextWorkerNodeKey === workerNodeKey) { |
| 81 | this.workerNodeVirtualTaskExecutionTime = 0 |
| 82 | if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { |
| 83 | this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 |
| 84 | } |
| 85 | } |
| 86 | if ( |
| 87 | this.previousWorkerNodeKey === workerNodeKey && |
| 88 | this.previousWorkerNodeKey > this.pool.workerNodes.length - 1 |
| 89 | ) { |
| 90 | this.previousWorkerNodeKey = this.pool.workerNodes.length - 1 |
| 91 | } |
| 92 | return true |
| 93 | } |
| 94 | |
| 95 | private weightedRoundRobinNextWorkerNodeKey (): number | undefined { |
| 96 | const workerWeight = |
| 97 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 98 | this.opts!.weights![this.nextWorkerNodeKey ?? this.previousWorkerNodeKey] |
| 99 | if (this.workerNodeVirtualTaskExecutionTime < workerWeight) { |
| 100 | this.workerNodeVirtualTaskExecutionTime += |
| 101 | this.getWorkerNodeTaskWaitTime( |
| 102 | this.nextWorkerNodeKey ?? this.previousWorkerNodeKey |
| 103 | ) + |
| 104 | this.getWorkerNodeTaskRunTime( |
| 105 | this.nextWorkerNodeKey ?? this.previousWorkerNodeKey |
| 106 | ) |
| 107 | } else { |
| 108 | this.nextWorkerNodeKey = |
| 109 | this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 |
| 110 | ? 0 |
| 111 | : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 |
| 112 | this.workerNodeVirtualTaskExecutionTime = 0 |
| 113 | } |
| 114 | return this.nextWorkerNodeKey |
| 115 | } |
| 116 | } |