docs: refine README.md
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
CommitLineData
fc3e6586 1import { cpus } from 'node:os'
f06e48d8 2import type { IWorker } from '../worker'
65d7a1c9
JB
3import type { IPool } from '../pool'
4import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
b3432a63 5import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
bf90656c
JB
6import type {
7 IWorkerChoiceStrategy,
da309861
JB
8 RequiredStatistics,
9 WorkerChoiceStrategyOptions
bf90656c 10} from './selection-strategies-types'
b3432a63 11
b3432a63
JB
12/**
13 * Selects the next worker with a weighted round robin scheduling algorithm.
14 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
15 *
38e795c1
JB
16 * @typeParam Worker - Type of worker which manages the strategy.
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
02706357 18 * @typeParam Response - Type of execution response. This can only be serializable data.
b3432a63
JB
19 */
20export class WeightedRoundRobinWorkerChoiceStrategy<
f06e48d8 21 Worker extends IWorker,
b2b1d84e
JB
22 Data = unknown,
23 Response = unknown
bf90656c
JB
24 >
25 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 26 implements IWorkerChoiceStrategy {
afc003b2 27 /** @inheritDoc */
ea7a90d3 28 public readonly requiredStatistics: RequiredStatistics = {
c6bd2650 29 runTime: true,
78099a15
JB
30 avgRunTime: true,
31 medRunTime: false
10fcfaf4
JB
32 }
33
b3432a63 34 /**
f06e48d8 35 * Worker node id where the current task will be submitted.
b3432a63 36 */
f06e48d8 37 private currentWorkerNodeId: number = 0
b3432a63
JB
38 /**
39 * Default worker weight.
40 */
777af0ac 41 private readonly defaultWorkerWeight: number
b3432a63 42 /**
08f3f44c 43 * Worker virtual task runtime.
b3432a63 44 */
08f3f44c 45 private workerVirtualTaskRunTime: number = 0
b3432a63 46
2fc5cae3 47 /** @inheritDoc */
da309861 48 public constructor (
c4855468 49 pool: IPool<Worker, Data, Response>,
2fc5cae3 50 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
da309861
JB
51 ) {
52 super(pool, opts)
a20f0ba5 53 this.checkOptions(this.opts)
08f3f44c 54 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
b3432a63
JB
55 }
56
afc003b2 57 /** @inheritDoc */
a6f7f1b4 58 public reset (): boolean {
f06e48d8 59 this.currentWorkerNodeId = 0
08f3f44c 60 this.workerVirtualTaskRunTime = 0
ea7a90d3
JB
61 return true
62 }
63
afc003b2 64 /** @inheritDoc */
c923ce56 65 public choose (): number {
f06e48d8 66 const chosenWorkerNodeKey = this.currentWorkerNodeId
0d80593b 67 const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime ?? 0
b3432a63 68 const workerTaskWeight =
08f3f44c 69 this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
0d80593b 70 if (workerVirtualTaskRunTime < workerTaskWeight) {
08f3f44c 71 this.workerVirtualTaskRunTime =
0d80593b 72 workerVirtualTaskRunTime +
08f3f44c 73 (this.getWorkerVirtualTaskRunTime(chosenWorkerNodeKey) ?? 0)
b3432a63 74 } else {
f06e48d8
JB
75 this.currentWorkerNodeId =
76 this.currentWorkerNodeId === this.pool.workerNodes.length - 1
b3432a63 77 ? 0
f06e48d8 78 : this.currentWorkerNodeId + 1
08f3f44c 79 this.workerVirtualTaskRunTime = 0
b3432a63 80 }
f06e48d8 81 return chosenWorkerNodeKey
b3432a63
JB
82 }
83
afc003b2 84 /** @inheritDoc */
f06e48d8
JB
85 public remove (workerNodeKey: number): boolean {
86 if (this.currentWorkerNodeId === workerNodeKey) {
87 if (this.pool.workerNodes.length === 0) {
88 this.currentWorkerNodeId = 0
78ab2555 89 } else {
f06e48d8
JB
90 this.currentWorkerNodeId =
91 this.currentWorkerNodeId > this.pool.workerNodes.length - 1
92 ? this.pool.workerNodes.length - 1
93 : this.currentWorkerNodeId
78ab2555 94 }
08f3f44c 95 this.workerVirtualTaskRunTime = 0
97a2abc3 96 }
08f3f44c 97 return true
2377984d
JB
98 }
99
f06e48d8 100 private getWorkerVirtualTaskRunTime (workerNodeKey: number): number {
da309861
JB
101 return this.requiredStatistics.medRunTime
102 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
103 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
2377984d
JB
104 }
105
08f3f44c 106 private computeDefaultWorkerWeight (): number {
b3432a63 107 let cpusCycleTimeWeight = 0
a59e741b 108 for (const cpu of cpus()) {
b3432a63 109 // CPU estimated cycle time
d8a610ca
JB
110 const numberOfDigits = cpu.speed.toString().length - 1
111 const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
112 cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
b3432a63 113 }
7b0d35b8 114 return Math.round(cpusCycleTimeWeight / cpus().length)
b3432a63 115 }
b3432a63 116}