| 1 | import { cpus } from 'node:os' |
| 2 | import type { IWorker } from '../worker' |
| 3 | import type { IPool } from '../pool' |
| 4 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
| 5 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
| 6 | import type { |
| 7 | IWorkerChoiceStrategy, |
| 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 | /** |
| 26 | * Worker node id where the current task will be submitted. |
| 27 | */ |
| 28 | private currentWorkerNodeId: number = 0 |
| 29 | /** |
| 30 | * Current round id. |
| 31 | * This is used to determine the current round weight. |
| 32 | */ |
| 33 | private currentRoundId: number = 0 |
| 34 | /** |
| 35 | * Round weights. |
| 36 | */ |
| 37 | private roundWeights: number[] |
| 38 | /** |
| 39 | * Default worker weight. |
| 40 | */ |
| 41 | private readonly defaultWorkerWeight: number |
| 42 | |
| 43 | /** @inheritDoc */ |
| 44 | public constructor ( |
| 45 | pool: IPool<Worker, Data, Response>, |
| 46 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
| 47 | ) { |
| 48 | super(pool, opts) |
| 49 | this.setRequiredStatistics(this.opts) |
| 50 | this.defaultWorkerWeight = this.computeDefaultWorkerWeight() |
| 51 | this.roundWeights = this.getRoundWeights() |
| 52 | } |
| 53 | |
| 54 | /** @inheritDoc */ |
| 55 | public reset (): boolean { |
| 56 | this.currentWorkerNodeId = 0 |
| 57 | this.currentRoundId = 0 |
| 58 | return true |
| 59 | } |
| 60 | |
| 61 | /** @inheritDoc */ |
| 62 | public update (): boolean { |
| 63 | return true |
| 64 | } |
| 65 | |
| 66 | /** @inheritDoc */ |
| 67 | public choose (): number { |
| 68 | let roundId: number | undefined |
| 69 | let workerNodeId: number | undefined |
| 70 | for ( |
| 71 | let roundIndex = this.currentRoundId; |
| 72 | roundIndex < this.roundWeights.length; |
| 73 | roundIndex++ |
| 74 | ) { |
| 75 | for ( |
| 76 | let workerNodeKey = this.currentWorkerNodeId; |
| 77 | workerNodeKey < this.pool.workerNodes.length; |
| 78 | workerNodeKey++ |
| 79 | ) { |
| 80 | const workerWeight = |
| 81 | this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight |
| 82 | if (workerWeight >= this.roundWeights[roundIndex]) { |
| 83 | roundId = roundIndex |
| 84 | workerNodeId = workerNodeKey |
| 85 | break |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | this.currentRoundId = roundId ?? 0 |
| 90 | this.currentWorkerNodeId = workerNodeId ?? 0 |
| 91 | const chosenWorkerNodeKey = this.currentWorkerNodeId |
| 92 | if (this.currentWorkerNodeId === this.pool.workerNodes.length - 1) { |
| 93 | this.currentWorkerNodeId = 0 |
| 94 | this.currentRoundId = |
| 95 | this.currentRoundId === this.roundWeights.length - 1 |
| 96 | ? 0 |
| 97 | : this.currentRoundId + 1 |
| 98 | } else { |
| 99 | this.currentWorkerNodeId = this.currentWorkerNodeId + 1 |
| 100 | } |
| 101 | return chosenWorkerNodeKey |
| 102 | } |
| 103 | |
| 104 | /** @inheritDoc */ |
| 105 | public remove (workerNodeKey: number): boolean { |
| 106 | if (this.currentWorkerNodeId === workerNodeKey) { |
| 107 | if (this.pool.workerNodes.length === 0) { |
| 108 | this.currentWorkerNodeId = 0 |
| 109 | } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) { |
| 110 | this.currentWorkerNodeId = this.pool.workerNodes.length - 1 |
| 111 | this.currentRoundId = |
| 112 | this.currentRoundId === this.roundWeights.length - 1 |
| 113 | ? 0 |
| 114 | : this.currentRoundId + 1 |
| 115 | } |
| 116 | } |
| 117 | return true |
| 118 | } |
| 119 | |
| 120 | /** @inheritDoc */ |
| 121 | public setOptions (opts: WorkerChoiceStrategyOptions): void { |
| 122 | super.setOptions(opts) |
| 123 | this.roundWeights = this.getRoundWeights() |
| 124 | } |
| 125 | |
| 126 | private computeDefaultWorkerWeight (): number { |
| 127 | let cpusCycleTimeWeight = 0 |
| 128 | for (const cpu of cpus()) { |
| 129 | // CPU estimated cycle time |
| 130 | const numberOfDigits = cpu.speed.toString().length - 1 |
| 131 | const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits)) |
| 132 | cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits) |
| 133 | } |
| 134 | return Math.round(cpusCycleTimeWeight / cpus().length) |
| 135 | } |
| 136 | |
| 137 | private getRoundWeights (): number[] { |
| 138 | if (this.opts.weights == null) { |
| 139 | return [this.defaultWorkerWeight] |
| 140 | } |
| 141 | return [ |
| 142 | ...new Set( |
| 143 | Object.values(this.opts.weights) |
| 144 | .slice() |
| 145 | .sort((a, b) => a - b) |
| 146 | ) |
| 147 | ] |
| 148 | } |
| 149 | } |