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 roundId
: number | undefined
77 let workerNodeId
: number | undefined
79 let round
= this.currentRoundId
;
80 round
< this.roundWeights
.length
;
84 let workerNodeKey
= this.currentWorkerNodeId
;
85 workerNodeKey
< this.pool
.workerNodes
.length
;
89 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
90 if (workerWeight
>= this.roundWeights
[round
]) {
92 workerNodeId
= workerNodeKey
97 this.currentRoundId
= roundId
?? 0
98 this.currentWorkerNodeId
= workerNodeId
?? 0
99 const chosenWorkerNodeKey
= this.currentWorkerNodeId
100 if (this.currentWorkerNodeId
=== this.pool
.workerNodes
.length
- 1) {
101 this.currentWorkerNodeId
= 0
102 this.currentRoundId
=
103 this.currentRoundId
=== this.roundWeights
.length
- 1
105 : this.currentRoundId
+ 1
107 this.currentWorkerNodeId
= this.currentWorkerNodeId
+ 1
109 return chosenWorkerNodeKey
113 public remove (workerNodeKey
: number): boolean {
114 if (this.currentWorkerNodeId
=== workerNodeKey
) {
115 if (this.pool
.workerNodes
.length
=== 0) {
116 this.currentWorkerNodeId
= 0
118 this.currentWorkerNodeId
=
119 this.currentWorkerNodeId
> this.pool
.workerNodes
.length
- 1
120 ? this.pool
.workerNodes
.length
- 1
121 : this.currentWorkerNodeId
128 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
129 super.setOptions(opts
)
130 this.roundWeights
= this.getRoundWeights()
133 private computeDefaultWorkerWeight (): number {
134 let cpusCycleTimeWeight
= 0
135 for (const cpu
of cpus()) {
136 // CPU estimated cycle time
137 const numberOfDigits
= cpu
.speed
.toString().length
- 1
138 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
139 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
141 return Math.round(cpusCycleTimeWeight
/ cpus().length
)
144 private getRoundWeights (): number[] {
145 if (this.opts
.weights
== null) {
146 return [this.defaultWorkerWeight
]
150 Object.values(this.opts
.weights
)
152 .sort((a
, b
) => a
- b
)