1 import { cpus
} from
'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.
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 * @template Worker Type of worker which manages the strategy.
20 * @template Data Type of data sent to the worker. This can only be serializable data.
21 * @template 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 index where the previous task was submitted.
36 private previousWorkerIndex
: number = 0
38 * Worker index where the current task will be submitted.
40 private currentWorkerIndex
: number = 0
42 * Default worker weight.
44 private readonly defaultWorkerWeight
: number
46 * Per worker virtual task runtime map.
48 private readonly workersTaskRunTime
: Map
<Worker
, TaskRunTime
> = new Map
<
54 * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
56 * @param pool The pool instance.
58 public constructor (pool
: IPoolInternal
<Worker
, Data
, Response
>) {
60 this.defaultWorkerWeight
= this.computeWorkerWeight()
61 this.initWorkersTaskRunTime()
65 public reset (): boolean {
66 this.previousWorkerIndex
= 0
67 this.currentWorkerIndex
= 0
68 this.workersTaskRunTime
.clear()
69 this.initWorkersTaskRunTime()
74 public choose (): Worker
{
75 const currentWorker
= this.pool
.workers
[this.currentWorkerIndex
]
77 this.isDynamicPool
=== true &&
78 this.workersTaskRunTime
.has(currentWorker
) === false
80 this.initWorkerTaskRunTime(currentWorker
)
82 const workerVirtualTaskRunTime
=
83 this.getWorkerVirtualTaskRunTime(currentWorker
) ?? 0
84 const workerTaskWeight
=
85 this.workersTaskRunTime
.get(currentWorker
)?.weight
??
86 this.defaultWorkerWeight
87 if (this.currentWorkerIndex
=== this.previousWorkerIndex
) {
88 const workerTaskRunTime
=
89 (this.workersTaskRunTime
.get(currentWorker
)?.runTime
?? 0) +
90 workerVirtualTaskRunTime
91 this.setWorkerTaskRunTime(
97 this.setWorkerTaskRunTime(currentWorker
, workerTaskWeight
, 0)
99 if (workerVirtualTaskRunTime
< workerTaskWeight
) {
100 this.previousWorkerIndex
= this.currentWorkerIndex
102 this.previousWorkerIndex
= this.currentWorkerIndex
103 this.currentWorkerIndex
=
104 this.pool
.workers
.length
- 1 === this.currentWorkerIndex
106 : this.currentWorkerIndex
+ 1
108 return this.pool
.workers
[this.currentWorkerIndex
]
111 private initWorkersTaskRunTime (): void {
112 for (const worker
of this.pool
.workers
) {
113 this.initWorkerTaskRunTime(worker
)
117 private initWorkerTaskRunTime (worker
: Worker
): void {
118 this.setWorkerTaskRunTime(worker
, this.defaultWorkerWeight
, 0)
121 private setWorkerTaskRunTime (
126 this.workersTaskRunTime
.set(worker
, {
132 private getWorkerVirtualTaskRunTime (worker
: Worker
): number | undefined {
133 return this.pool
.getWorkerAverageTasksRunTime(worker
)
136 private computeWorkerWeight (): number {
137 let cpusCycleTimeWeight
= 0
138 for (const cpu
of cpus()) {
139 // CPU estimated cycle time
140 const numberOfDigit
= cpu
.speed
.toString().length
- 1
141 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigit
))
142 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigit
)
144 return Math.round(cpusCycleTimeWeight
/ cpus().length
)