refactor: cleanup IWRR properties
[poolifier.git] / src / pools / selection-strategies / interleaved-weighted-round-robin-worker-choice-strategy.ts
CommitLineData
feec6e8c
JB
1import { cpus } from 'node:os'
2import type { IWorker } from '../worker'
3import type { IPool } from '../pool'
4import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
5import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
6import type {
7 IWorkerChoiceStrategy,
feec6e8c
JB
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 */
18export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
19 Worker extends IWorker,
20 Data = unknown,
21 Response = unknown
22 >
23 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
24 implements IWorkerChoiceStrategy {
feec6e8c
JB
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 */
e4854a4e 37 private roundWeights: number[]
feec6e8c
JB
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.checkOptions(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 {
297f3bbe
JB
68 let roundId: number | undefined
69 let workerNodeId: number | undefined
70 for (
d3127e84
JB
71 let roundIndex = this.currentRoundId;
72 roundIndex < this.roundWeights.length;
73 roundIndex++
297f3bbe 74 ) {
feec6e8c 75 for (
297f3bbe
JB
76 let workerNodeKey = this.currentWorkerNodeId;
77 workerNodeKey < this.pool.workerNodes.length;
78 workerNodeKey++
feec6e8c 79 ) {
297f3bbe
JB
80 const workerWeight =
81 this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
d3127e84
JB
82 if (workerWeight >= this.roundWeights[roundIndex]) {
83 roundId = roundIndex
297f3bbe
JB
84 workerNodeId = workerNodeKey
85 break
feec6e8c
JB
86 }
87 }
297f3bbe
JB
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
feec6e8c
JB
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
cac70359
JB
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
feec6e8c
JB
115 }
116 }
117 return true
118 }
119
e4854a4e
JB
120 /** @inheritDoc */
121 public setOptions (opts: WorkerChoiceStrategyOptions): void {
122 super.setOptions(opts)
123 this.roundWeights = this.getRoundWeights()
124 }
125
feec6e8c
JB
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}