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'
8 } from
'./selection-strategies-types'
11 * Virtual task runtime.
13 interface TaskRunTime
{
19 * Selects the next worker with a weighted round robin scheduling algorithm.
20 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
22 * @typeParam Worker - Type of worker which manages the strategy.
23 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
24 * @typeParam Response - Type of response of execution. This can only be serializable data.
26 export class WeightedRoundRobinWorkerChoiceStrategy
<
27 Worker
extends IPoolWorker
,
31 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
32 implements IWorkerChoiceStrategy
{
34 public readonly requiredStatistics
: RequiredStatistics
= {
39 * Worker id where the current task will be submitted.
41 private currentWorkerId
: number = 0
43 * Default worker weight.
45 private readonly defaultWorkerWeight
: number
47 * Per worker virtual task runtime map.
49 private readonly workersTaskRunTime
: Map
<number, TaskRunTime
> = new Map
<
55 * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
57 * @param pool - The pool instance.
59 public constructor (pool
: IPoolInternal
<Worker
, Data
, Response
>) {
61 this.defaultWorkerWeight
= this.computeWorkerWeight()
62 this.initWorkersTaskRunTime()
66 public reset (): boolean {
67 this.currentWorkerId
= 0
68 this.workersTaskRunTime
.clear()
69 this.initWorkersTaskRunTime()
74 public choose (): number {
75 const chosenWorkerKey
= this.currentWorkerId
76 if (this.isDynamicPool
&& !this.workersTaskRunTime
.has(chosenWorkerKey
)) {
77 this.initWorkerTaskRunTime(chosenWorkerKey
)
79 const workerTaskRunTime
=
80 this.workersTaskRunTime
.get(chosenWorkerKey
)?.runTime
?? 0
81 const workerTaskWeight
=
82 this.workersTaskRunTime
.get(chosenWorkerKey
)?.weight
??
83 this.defaultWorkerWeight
84 if (workerTaskRunTime
< workerTaskWeight
) {
85 this.setWorkerTaskRunTime(
89 (this.getWorkerVirtualTaskRunTime(chosenWorkerKey
) ?? 0)
92 this.currentWorkerId
=
93 this.currentWorkerId
=== this.pool
.workers
.length
- 1
95 : this.currentWorkerId
+ 1
96 this.setWorkerTaskRunTime(this.currentWorkerId
, workerTaskWeight
, 0)
98 return chosenWorkerKey
102 public remove (workerKey
: number): boolean {
103 if (this.currentWorkerId
=== workerKey
) {
104 this.currentWorkerId
=
105 this.currentWorkerId
> this.pool
.workers
.length
- 1
106 ? this.pool
.workers
.length
- 1
107 : this.currentWorkerId
109 const workerDeleted
= this.workersTaskRunTime
.delete(workerKey
)
110 for (const [key
, value
] of this.workersTaskRunTime
) {
111 if (key
> workerKey
) {
112 this.workersTaskRunTime
.set(key
- 1, value
)
118 private initWorkersTaskRunTime (): void {
119 for (const [index
] of this.pool
.workers
.entries()) {
120 this.initWorkerTaskRunTime(index
)
124 private initWorkerTaskRunTime (workerKey
: number): void {
125 this.setWorkerTaskRunTime(workerKey
, this.defaultWorkerWeight
, 0)
128 private setWorkerTaskRunTime (
133 this.workersTaskRunTime
.set(workerKey
, {
139 private getWorkerVirtualTaskRunTime (workerKey
: number): number {
140 return this.pool
.workers
[workerKey
].tasksUsage
.avgRunTime
143 private computeWorkerWeight (): number {
144 let cpusCycleTimeWeight
= 0
145 for (const cpu
of cpus()) {
146 // CPU estimated cycle time
147 const numberOfDigits
= cpu
.speed
.toString().length
- 1
148 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
149 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
151 return Math.round(cpusCycleTimeWeight
/ cpus().length
)