Uniformize some comments
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
CommitLineData
b3432a63
JB
1import { cpus } from 'os'
2import type { AbstractPoolWorker } from '../abstract-pool-worker'
3import type { IPoolInternal } from '../pool-internal'
4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
10fcfaf4 5import type { RequiredStatistics } from './selection-strategies-types'
b3432a63
JB
6
7/**
23135a89 8 * Virtual task runtime.
b3432a63
JB
9 */
10type TaskRunTime = {
11 weight: number
12 runTime: number
13}
14
15/**
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.
18 *
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.
22 */
23export class WeightedRoundRobinWorkerChoiceStrategy<
24 Worker extends AbstractPoolWorker,
25 Data,
26 Response
27> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
10fcfaf4
JB
28 /** @inheritDoc */
29 public requiredStatistics: RequiredStatistics = {
30 runTime: true
31 }
32
b3432a63
JB
33 /**
34 * Worker index where the previous task was submitted.
35 */
36 private previousWorkerIndex: number = 0
37 /**
38 * Worker index where the current task will be submitted.
39 */
40 private currentWorkerIndex: number = 0
41 /**
42 * Default worker weight.
43 */
44 private defaultWorkerWeight: number
45 /**
2377984d 46 * Per worker virtual task runtime map.
b3432a63 47 */
2377984d 48 private workersTaskRunTime: Map<Worker, TaskRunTime> = new Map<
b3432a63
JB
49 Worker,
50 TaskRunTime
51 >()
52
53 /**
23ff945a 54 * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
b3432a63
JB
55 *
56 * @param pool The pool instance.
57 */
58 public constructor (pool: IPoolInternal<Worker, Data, Response>) {
59 super(pool)
60 this.defaultWorkerWeight = this.computeWorkerWeight()
2377984d 61 this.initWorkersTaskRunTime()
b3432a63
JB
62 }
63
64 /** @inheritDoc */
65 public choose (): Worker {
66 const currentWorker = this.pool.workers[this.currentWorkerIndex]
2377984d
JB
67 if (
68 this.isDynamicPool === true &&
69 this.workersTaskRunTime.has(currentWorker) === false
70 ) {
71 this.initWorkerTaskRunTime(currentWorker)
b3432a63
JB
72 }
73 const workerVirtualTaskRunTime =
74 this.getWorkerVirtualTaskRunTime(currentWorker) ?? 0
75 const workerTaskWeight =
2377984d 76 this.workersTaskRunTime.get(currentWorker)?.weight ??
b3432a63
JB
77 this.defaultWorkerWeight
78 if (this.currentWorkerIndex === this.previousWorkerIndex) {
79 const workerTaskRunTime =
2377984d 80 (this.workersTaskRunTime.get(currentWorker)?.runTime ?? 0) +
b3432a63 81 workerVirtualTaskRunTime
2377984d
JB
82 this.setWorkerTaskRunTime(
83 currentWorker,
84 workerTaskWeight,
85 workerTaskRunTime
86 )
b3432a63 87 } else {
2377984d 88 this.setWorkerTaskRunTime(currentWorker, workerTaskWeight, 0)
b3432a63
JB
89 }
90 if (
91 workerVirtualTaskRunTime <
2377984d
JB
92 (this.workersTaskRunTime.get(currentWorker)?.weight ??
93 this.defaultWorkerWeight)
b3432a63
JB
94 ) {
95 this.previousWorkerIndex = this.currentWorkerIndex
96 } else {
97 this.previousWorkerIndex = this.currentWorkerIndex
98 this.currentWorkerIndex =
99 this.pool.workers.length - 1 === this.currentWorkerIndex
100 ? 0
101 : this.currentWorkerIndex + 1
102 }
103 return this.pool.workers[this.currentWorkerIndex]
104 }
105
2377984d
JB
106 private initWorkersTaskRunTime (): void {
107 for (const worker of this.pool.workers) {
108 this.initWorkerTaskRunTime(worker)
109 }
110 }
111
112 private initWorkerTaskRunTime (worker: Worker): void {
113 this.setWorkerTaskRunTime(worker, this.defaultWorkerWeight, 0)
114 }
115
116 private setWorkerTaskRunTime (
117 worker: Worker,
118 weight: number,
119 runTime: number
120 ): void {
121 this.workersTaskRunTime.set(worker, {
122 weight,
123 runTime
124 })
125 }
126
127 private getWorkerVirtualTaskRunTime (worker: Worker): number | undefined {
128 return this.pool.getWorkerAverageTasksRunTime(worker)
129 }
130
131 private computeWorkerWeight (): number {
b3432a63 132 let cpusCycleTimeWeight = 0
a59e741b 133 for (const cpu of cpus()) {
b3432a63 134 // CPU estimated cycle time
a59e741b
JB
135 const numberOfDigit = cpu.speed.toString().length - 1
136 const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigit))
b3432a63
JB
137 cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigit)
138 }
139 return cpusCycleTimeWeight / cpus().length
140 }
b3432a63 141}