1 import { cpus
} from
'node:os'
2 import type { IPoolInternal
} from
'../pool-internal'
3 import type { IPoolWorker
} from
'../pool-worker'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
5 import type { RequiredStatistics
} from
'./selection-strategies-types'
8 * Virtual task runtime.
10 interface TaskRunTime
{
16 * Selects the next worker with a weighted round robin scheduling algorithm.
17 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
19 * @typeParam Worker - Type of worker which manages the strategy.
20 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
21 * @typeParam Response - Type of response of execution. This can only be serializable data.
23 export class WeightedRoundRobinWorkerChoiceStrategy
<
24 Worker
extends IPoolWorker
,
27 > extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
> {
29 public readonly requiredStatistics
: RequiredStatistics
= {
34 * Worker id where the current task will be submitted.
36 private currentWorkerId
: number = 0
38 * Default worker weight.
40 private readonly defaultWorkerWeight
: number
42 * Per worker virtual task runtime map.
44 private readonly workersTaskRunTime
: Map
<number, TaskRunTime
> = new Map
<
50 * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
52 * @param pool - The pool instance.
54 public constructor (pool
: IPoolInternal
<Worker
, Data
, Response
>) {
56 this.defaultWorkerWeight
= this.computeWorkerWeight()
57 this.initWorkersTaskRunTime()
61 public reset (): boolean {
62 this.currentWorkerId
= 0
63 this.workersTaskRunTime
.clear()
64 this.initWorkersTaskRunTime()
69 public choose (): number {
70 const chosenWorkerKey
= this.currentWorkerId
71 if (this.isDynamicPool
&& !this.workersTaskRunTime
.has(chosenWorkerKey
)) {
72 this.initWorkerTaskRunTime(chosenWorkerKey
)
74 const workerTaskRunTime
=
75 this.workersTaskRunTime
.get(chosenWorkerKey
)?.runTime
?? 0
76 const workerTaskWeight
=
77 this.workersTaskRunTime
.get(chosenWorkerKey
)?.weight
??
78 this.defaultWorkerWeight
79 if (workerTaskRunTime
< workerTaskWeight
) {
80 this.setWorkerTaskRunTime(
84 (this.getWorkerVirtualTaskRunTime(chosenWorkerKey
) ?? 0)
87 this.currentWorkerId
=
88 this.currentWorkerId
=== this.pool
.workers
.length
- 1
90 : this.currentWorkerId
+ 1
91 this.setWorkerTaskRunTime(this.currentWorkerId
, workerTaskWeight
, 0)
93 return chosenWorkerKey
97 public remove (workerKey
: number): boolean {
98 if (this.currentWorkerId
=== workerKey
) {
99 this.currentWorkerId
=
100 this.currentWorkerId
> this.pool
.workers
.length
- 1
101 ? this.pool
.workers
.length
- 1
102 : this.currentWorkerId
104 const workerDeleted
= this.workersTaskRunTime
.delete(workerKey
)
105 for (const [key
, value
] of this.workersTaskRunTime
) {
106 if (key
> workerKey
) {
107 this.workersTaskRunTime
.set(key
- 1, value
)
113 private initWorkersTaskRunTime (): void {
114 for (const [index
] of this.pool
.workers
.entries()) {
115 this.initWorkerTaskRunTime(index
)
119 private initWorkerTaskRunTime (workerKey
: number): void {
120 this.setWorkerTaskRunTime(workerKey
, this.defaultWorkerWeight
, 0)
123 private setWorkerTaskRunTime (
128 this.workersTaskRunTime
.set(workerKey
, {
134 private getWorkerVirtualTaskRunTime (workerKey
: number): number {
135 return this.pool
.workers
[workerKey
].tasksUsage
.avgRunTime
138 private computeWorkerWeight (): number {
139 let cpusCycleTimeWeight
= 0
140 for (const cpu
of cpus()) {
141 // CPU estimated cycle time
142 const numberOfDigits
= cpu
.speed
.toString().length
- 1
143 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
144 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
146 return Math.round(cpusCycleTimeWeight
/ cpus().length
)