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'
9 WorkerChoiceStrategyOptions
10 } from
'./selection-strategies-types'
13 * Selects the next worker with an interleaved weighted round robin scheduling algorithm.
15 * @typeParam Worker - Type of worker which manages the strategy.
16 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
17 * @typeParam Response - Type of execution response. This can only be serializable data.
19 export class InterleavedWeightedRoundRobinWorkerChoiceStrategy
<
20 Worker
extends IWorker
,
24 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
25 implements IWorkerChoiceStrategy
{
27 public readonly requiredStatistics
: RequiredStatistics
= {
34 * Worker node id where the current task will be submitted.
36 private currentWorkerNodeId
: number = 0
39 * This is used to determine the current round weight.
41 private currentRoundId
: number = 0
45 private roundWeights
: number[]
47 * Default worker weight.
49 private readonly defaultWorkerWeight
: number
53 pool
: IPool
<Worker
, Data
, Response
>,
54 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
57 this.checkOptions(this.opts
)
58 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
59 this.roundWeights
= this.getRoundWeights()
63 public reset (): boolean {
64 this.currentWorkerNodeId
= 0
65 this.currentRoundId
= 0
70 public update (): boolean {
75 public choose (): number {
76 let chosenWorkerNodeKey
: number
78 this.opts
.weights
?.[this.currentWorkerNodeId
] ?? this.defaultWorkerWeight
79 if (workerWeight
>= this.roundWeights
[this.currentRoundId
]) {
80 chosenWorkerNodeKey
= this.currentWorkerNodeId
81 this.currentWorkerNodeId
=
82 this.currentWorkerNodeId
=== this.pool
.workerNodes
.length
- 1
84 : this.currentWorkerNodeId
+ 1
85 if (this.currentWorkerNodeId
=== this.pool
.workerNodes
.length
- 1) {
87 this.currentRoundId
=== this.roundWeights
.length
- 1
89 : this.currentRoundId
+ 1
92 let roundId
: number | undefined
93 let workerNodeId
: number | undefined
95 let round
= this.currentRoundId
;
96 round
< this.roundWeights
.length
;
100 let workerNodeKey
= this.currentWorkerNodeId
+ 1;
101 workerNodeKey
< this.pool
.workerNodes
.length
;
105 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
106 if (workerWeight
>= this.roundWeights
[round
]) {
108 workerNodeId
= workerNodeKey
113 this.currentRoundId
= roundId
?? 0
114 this.currentWorkerNodeId
= workerNodeId
?? 0
115 chosenWorkerNodeKey
= this.currentWorkerNodeId
117 return chosenWorkerNodeKey
121 public remove (workerNodeKey
: number): boolean {
122 if (this.currentWorkerNodeId
=== workerNodeKey
) {
123 if (this.pool
.workerNodes
.length
=== 0) {
124 this.currentWorkerNodeId
= 0
126 this.currentWorkerNodeId
=
127 this.currentWorkerNodeId
> this.pool
.workerNodes
.length
- 1
128 ? this.pool
.workerNodes
.length
- 1
129 : this.currentWorkerNodeId
136 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
137 super.setOptions(opts
)
138 this.roundWeights
= this.getRoundWeights()
141 private computeDefaultWorkerWeight (): number {
142 let cpusCycleTimeWeight
= 0
143 for (const cpu
of cpus()) {
144 // CPU estimated cycle time
145 const numberOfDigits
= cpu
.speed
.toString().length
- 1
146 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
147 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
149 return Math.round(cpusCycleTimeWeight
/ cpus().length
)
152 private getRoundWeights (): number[] {
153 if (this.opts
.weights
== null) {
154 return [this.defaultWorkerWeight
]
158 Object.values(this.opts
.weights
)
160 .sort((a
, b
) => a
- b
)