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 a weighted round robin scheduling algorithm.
14 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
16 * @typeParam Worker - Type of worker which manages the strategy.
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
18 * @typeParam Response - Type of execution response. This can only be serializable data.
20 export class WeightedRoundRobinWorkerChoiceStrategy
<
21 Worker
extends IWorker
,
25 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
26 implements IWorkerChoiceStrategy
{
28 public readonly requiredStatistics
: RequiredStatistics
= {
35 * Worker node id where the current task will be submitted.
37 private currentWorkerNodeId
: number = 0
39 * Default worker weight.
41 private readonly defaultWorkerWeight
: number
43 * Worker virtual task runtime.
45 private workerVirtualTaskRunTime
: number = 0
49 pool
: IPool
<Worker
, Data
, Response
>,
50 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
53 this.checkOptions(this.opts
)
54 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
58 public reset (): boolean {
59 this.currentWorkerNodeId
= 0
60 this.workerVirtualTaskRunTime
= 0
65 public update (): boolean {
70 public choose (): number {
71 const chosenWorkerNodeKey
= this.currentWorkerNodeId
72 const workerVirtualTaskRunTime
= this.workerVirtualTaskRunTime
74 this.opts
.weights
?.[chosenWorkerNodeKey
] ?? this.defaultWorkerWeight
75 if (workerVirtualTaskRunTime
< workerWeight
) {
76 this.workerVirtualTaskRunTime
=
77 workerVirtualTaskRunTime
+
78 this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey
)
80 this.currentWorkerNodeId
=
81 this.currentWorkerNodeId
=== this.pool
.workerNodes
.length
- 1
83 : this.currentWorkerNodeId
+ 1
84 this.workerVirtualTaskRunTime
= 0
86 return chosenWorkerNodeKey
90 public remove (workerNodeKey
: number): boolean {
91 if (this.currentWorkerNodeId
=== workerNodeKey
) {
92 if (this.pool
.workerNodes
.length
=== 0) {
93 this.currentWorkerNodeId
= 0
95 this.currentWorkerNodeId
=
96 this.currentWorkerNodeId
> this.pool
.workerNodes
.length
- 1
97 ? this.pool
.workerNodes
.length
- 1
98 : this.currentWorkerNodeId
100 this.workerVirtualTaskRunTime
= 0
105 private getWorkerVirtualTaskRunTime (workerNodeKey
: number): number {
106 return this.requiredStatistics
.medRunTime
107 ? this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.medRunTime
108 : this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.avgRunTime
111 private computeDefaultWorkerWeight (): number {
112 let cpusCycleTimeWeight
= 0
113 for (const cpu
of cpus()) {
114 // CPU estimated cycle time
115 const numberOfDigits
= cpu
.speed
.toString().length
- 1
116 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
117 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
119 return Math.round(cpusCycleTimeWeight
/ cpus().length
)