| 1 | import type { IWorker } from '../worker' |
| 2 | import type { IPool } from '../pool' |
| 3 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
| 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
| 5 | import type { |
| 6 | IWorkerChoiceStrategy, |
| 7 | WorkerChoiceStrategyOptions |
| 8 | } from './selection-strategies-types' |
| 9 | |
| 10 | /** |
| 11 | * Selects the next worker with an interleaved weighted round robin scheduling algorithm. |
| 12 | * |
| 13 | * @typeParam Worker - Type of worker which manages the strategy. |
| 14 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
| 15 | * @typeParam Response - Type of execution response. This can only be serializable data. |
| 16 | */ |
| 17 | export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< |
| 18 | Worker extends IWorker, |
| 19 | Data = unknown, |
| 20 | Response = unknown |
| 21 | > |
| 22 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> |
| 23 | implements IWorkerChoiceStrategy { |
| 24 | /** |
| 25 | * Worker node id where the current task will be submitted. |
| 26 | */ |
| 27 | private currentWorkerNodeId: number = 0 |
| 28 | /** |
| 29 | * Current round id. |
| 30 | * This is used to determine the current round weight. |
| 31 | */ |
| 32 | private currentRoundId: number = 0 |
| 33 | /** |
| 34 | * Round weights. |
| 35 | */ |
| 36 | private roundWeights: number[] |
| 37 | /** |
| 38 | * Default worker weight. |
| 39 | */ |
| 40 | private readonly defaultWorkerWeight: number |
| 41 | |
| 42 | /** @inheritDoc */ |
| 43 | public constructor ( |
| 44 | pool: IPool<Worker, Data, Response>, |
| 45 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
| 46 | ) { |
| 47 | super(pool, opts) |
| 48 | this.setRequiredStatistics(this.opts) |
| 49 | this.defaultWorkerWeight = this.computeDefaultWorkerWeight() |
| 50 | this.roundWeights = this.getRoundWeights() |
| 51 | } |
| 52 | |
| 53 | /** @inheritDoc */ |
| 54 | public reset (): boolean { |
| 55 | this.currentWorkerNodeId = 0 |
| 56 | this.currentRoundId = 0 |
| 57 | return true |
| 58 | } |
| 59 | |
| 60 | /** @inheritDoc */ |
| 61 | public update (): boolean { |
| 62 | return true |
| 63 | } |
| 64 | |
| 65 | /** @inheritDoc */ |
| 66 | public choose (): number { |
| 67 | let roundId: number | undefined |
| 68 | let workerNodeId: number | undefined |
| 69 | for ( |
| 70 | let roundIndex = this.currentRoundId; |
| 71 | roundIndex < this.roundWeights.length; |
| 72 | roundIndex++ |
| 73 | ) { |
| 74 | for ( |
| 75 | let workerNodeKey = this.currentWorkerNodeId; |
| 76 | workerNodeKey < this.pool.workerNodes.length; |
| 77 | workerNodeKey++ |
| 78 | ) { |
| 79 | const workerWeight = |
| 80 | this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight |
| 81 | if (workerWeight >= this.roundWeights[roundIndex]) { |
| 82 | roundId = roundIndex |
| 83 | workerNodeId = workerNodeKey |
| 84 | break |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | this.currentRoundId = roundId ?? 0 |
| 89 | this.currentWorkerNodeId = workerNodeId ?? 0 |
| 90 | const chosenWorkerNodeKey = this.currentWorkerNodeId |
| 91 | if (this.currentWorkerNodeId === this.pool.workerNodes.length - 1) { |
| 92 | this.currentWorkerNodeId = 0 |
| 93 | this.currentRoundId = |
| 94 | this.currentRoundId === this.roundWeights.length - 1 |
| 95 | ? 0 |
| 96 | : this.currentRoundId + 1 |
| 97 | } else { |
| 98 | this.currentWorkerNodeId = this.currentWorkerNodeId + 1 |
| 99 | } |
| 100 | return chosenWorkerNodeKey |
| 101 | } |
| 102 | |
| 103 | /** @inheritDoc */ |
| 104 | public remove (workerNodeKey: number): boolean { |
| 105 | if (this.currentWorkerNodeId === workerNodeKey) { |
| 106 | if (this.pool.workerNodes.length === 0) { |
| 107 | this.currentWorkerNodeId = 0 |
| 108 | } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) { |
| 109 | this.currentWorkerNodeId = this.pool.workerNodes.length - 1 |
| 110 | this.currentRoundId = |
| 111 | this.currentRoundId === this.roundWeights.length - 1 |
| 112 | ? 0 |
| 113 | : this.currentRoundId + 1 |
| 114 | } |
| 115 | } |
| 116 | return true |
| 117 | } |
| 118 | |
| 119 | /** @inheritDoc */ |
| 120 | public setOptions (opts: WorkerChoiceStrategyOptions): void { |
| 121 | super.setOptions(opts) |
| 122 | this.roundWeights = this.getRoundWeights() |
| 123 | } |
| 124 | |
| 125 | private getRoundWeights (): number[] { |
| 126 | if (this.opts.weights == null) { |
| 127 | return [this.defaultWorkerWeight] |
| 128 | } |
| 129 | return [ |
| 130 | ...new Set( |
| 131 | Object.values(this.opts.weights) |
| 132 | .slice() |
| 133 | .sort((a, b) => a - b) |
| 134 | ) |
| 135 | ] |
| 136 | } |
| 137 | } |