fix: add missing crypto import
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
CommitLineData
fc3e6586 1import { cpus } from 'node:os'
b3432a63 2import type { IPoolInternal } from '../pool-internal'
ea7a90d3 3import type { IPoolWorker } from '../pool-worker'
b3432a63 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 9 */
78cea37e 10interface TaskRunTime {
b3432a63
JB
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 *
38e795c1
JB
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.
b3432a63
JB
22 */
23export class WeightedRoundRobinWorkerChoiceStrategy<
ea7a90d3 24 Worker extends IPoolWorker,
b3432a63
JB
25 Data,
26 Response
27> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
38e795c1 28 /** {@inheritDoc} */
ea7a90d3 29 public readonly requiredStatistics: RequiredStatistics = {
10fcfaf4
JB
30 runTime: true
31 }
32
b3432a63 33 /**
ffcbbad8 34 * Worker id where the current task will be submitted.
b3432a63 35 */
ffcbbad8 36 private currentWorkerId: number = 0
b3432a63
JB
37 /**
38 * Default worker weight.
39 */
777af0ac 40 private readonly defaultWorkerWeight: number
b3432a63 41 /**
2377984d 42 * Per worker virtual task runtime map.
b3432a63 43 */
ea7a90d3 44 private readonly workersTaskRunTime: Map<Worker, TaskRunTime> = new Map<
78cea37e
JB
45 Worker,
46 TaskRunTime
b3432a63
JB
47 >()
48
49 /**
23ff945a 50 * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
b3432a63 51 *
38e795c1 52 * @param pool - The pool instance.
b3432a63
JB
53 */
54 public constructor (pool: IPoolInternal<Worker, Data, Response>) {
55 super(pool)
56 this.defaultWorkerWeight = this.computeWorkerWeight()
2377984d 57 this.initWorkersTaskRunTime()
b3432a63
JB
58 }
59
38e795c1 60 /** {@inheritDoc} */
a6f7f1b4 61 public reset (): boolean {
ffcbbad8 62 this.currentWorkerId = 0
ea7a90d3
JB
63 this.workersTaskRunTime.clear()
64 this.initWorkersTaskRunTime()
65 return true
66 }
67
38e795c1 68 /** {@inheritDoc} */
b3432a63 69 public choose (): Worker {
ffcbbad8
JB
70 const chosenWorker = this.pool.workers.get(this.currentWorkerId)
71 ?.worker as Worker
78cea37e 72 if (this.isDynamicPool && !this.workersTaskRunTime.has(chosenWorker)) {
11df3590 73 this.initWorkerTaskRunTime(chosenWorker)
b3432a63 74 }
d8a610ca
JB
75 const workerTaskRunTime =
76 this.workersTaskRunTime.get(chosenWorker)?.runTime ?? 0
b3432a63 77 const workerTaskWeight =
11df3590 78 this.workersTaskRunTime.get(chosenWorker)?.weight ??
b3432a63 79 this.defaultWorkerWeight
553ad720 80 if (workerTaskRunTime < workerTaskWeight) {
2377984d 81 this.setWorkerTaskRunTime(
11df3590 82 chosenWorker,
2377984d 83 workerTaskWeight,
553ad720 84 workerTaskRunTime +
11df3590 85 (this.getWorkerVirtualTaskRunTime(chosenWorker) ?? 0)
2377984d 86 )
b3432a63 87 } else {
ffcbbad8
JB
88 this.currentWorkerId =
89 this.currentWorkerId === this.pool.workers.size - 1
b3432a63 90 ? 0
ffcbbad8 91 : this.currentWorkerId + 1
d354916a 92 this.setWorkerTaskRunTime(
ffcbbad8 93 this.pool.workers.get(this.currentWorkerId)?.worker as Worker,
d354916a
JB
94 workerTaskWeight,
95 0
96 )
b3432a63 97 }
11df3590 98 return chosenWorker
b3432a63
JB
99 }
100
2377984d 101 private initWorkersTaskRunTime (): void {
ffcbbad8
JB
102 for (const value of this.pool.workers.values()) {
103 this.initWorkerTaskRunTime(value.worker)
2377984d
JB
104 }
105 }
106
107 private initWorkerTaskRunTime (worker: Worker): void {
108 this.setWorkerTaskRunTime(worker, this.defaultWorkerWeight, 0)
109 }
110
111 private setWorkerTaskRunTime (
112 worker: Worker,
113 weight: number,
114 runTime: number
115 ): void {
116 this.workersTaskRunTime.set(worker, {
117 weight,
118 runTime
119 })
120 }
121
122 private getWorkerVirtualTaskRunTime (worker: Worker): number | undefined {
123 return this.pool.getWorkerAverageTasksRunTime(worker)
124 }
125
126 private computeWorkerWeight (): number {
b3432a63 127 let cpusCycleTimeWeight = 0
a59e741b 128 for (const cpu of cpus()) {
b3432a63 129 // CPU estimated cycle time
d8a610ca
JB
130 const numberOfDigits = cpu.speed.toString().length - 1
131 const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
132 cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
b3432a63 133 }
7b0d35b8 134 return Math.round(cpusCycleTimeWeight / cpus().length)
b3432a63 135 }
b3432a63 136}