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