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'
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types'
12 * Selects the next worker with an interleaved weighted round robin scheduling algorithm.
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.
18 export class InterleavedWeightedRoundRobinWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 * Worker node id where the current task will be submitted.
28 private currentWorkerNodeId
: number = 0
31 * This is used to determine the current round weight.
33 private currentRoundId
: number = 0
37 private roundWeights
: number[]
39 * Default worker weight.
41 private readonly defaultWorkerWeight
: number
45 pool
: IPool
<Worker
, Data
, Response
>,
46 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
49 this.checkOptions(this.opts
)
50 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
51 this.roundWeights
= this.getRoundWeights()
55 public reset (): boolean {
56 this.currentWorkerNodeId
= 0
57 this.currentRoundId
= 0
62 public update (): boolean {
67 public choose (): number {
68 let roundId
: number | undefined
69 let workerNodeId
: number | undefined
71 let roundIndex
= this.currentRoundId
;
72 roundIndex
< this.roundWeights
.length
;
76 let workerNodeKey
= this.currentWorkerNodeId
;
77 workerNodeKey
< this.pool
.workerNodes
.length
;
81 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
82 if (workerWeight
>= this.roundWeights
[roundIndex
]) {
84 workerNodeId
= workerNodeKey
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
95 this.currentRoundId
=== this.roundWeights
.length
- 1
97 : this.currentRoundId
+ 1
99 this.currentWorkerNodeId
= this.currentWorkerNodeId
+ 1
101 return chosenWorkerNodeKey
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
114 : this.currentRoundId
+ 1
121 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
122 super.setOptions(opts
)
123 this.roundWeights
= this.getRoundWeights()
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
)
134 return Math.round(cpusCycleTimeWeight
/ cpus().length
)
137 private getRoundWeights (): number[] {
138 if (this.opts
.weights
== null) {
139 return [this.defaultWorkerWeight
]
143 Object.values(this.opts
.weights
)
145 .sort((a
, b
) => a
- b
)