refactor: factor out worker runtime getter
[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)
49be33fe 53 this.setRequiredStatistics(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
138d29a8
JB
64 /** @inheritDoc */
65 public update (): boolean {
66 return true
67 }
68
afc003b2 69 /** @inheritDoc */
c923ce56 70 public choose (): number {
f06e48d8 71 const chosenWorkerNodeKey = this.currentWorkerNodeId
138d29a8
JB
72 const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
73 const workerWeight =
08f3f44c 74 this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
138d29a8 75 if (workerVirtualTaskRunTime < workerWeight) {
08f3f44c 76 this.workerVirtualTaskRunTime =
0d80593b 77 workerVirtualTaskRunTime +
f6b641d6 78 this.getWorkerTaskRunTime(chosenWorkerNodeKey)
b3432a63 79 } else {
f06e48d8
JB
80 this.currentWorkerNodeId =
81 this.currentWorkerNodeId === this.pool.workerNodes.length - 1
b3432a63 82 ? 0
f06e48d8 83 : this.currentWorkerNodeId + 1
08f3f44c 84 this.workerVirtualTaskRunTime = 0
b3432a63 85 }
f06e48d8 86 return chosenWorkerNodeKey
b3432a63
JB
87 }
88
afc003b2 89 /** @inheritDoc */
f06e48d8
JB
90 public remove (workerNodeKey: number): boolean {
91 if (this.currentWorkerNodeId === workerNodeKey) {
92 if (this.pool.workerNodes.length === 0) {
93 this.currentWorkerNodeId = 0
97f4fd90
JB
94 } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
95 this.currentWorkerNodeId = this.pool.workerNodes.length - 1
78ab2555 96 }
08f3f44c 97 this.workerVirtualTaskRunTime = 0
97a2abc3 98 }
08f3f44c 99 return true
2377984d
JB
100 }
101
08f3f44c 102 private computeDefaultWorkerWeight (): number {
b3432a63 103 let cpusCycleTimeWeight = 0
a59e741b 104 for (const cpu of cpus()) {
b3432a63 105 // CPU estimated cycle time
d8a610ca
JB
106 const numberOfDigits = cpu.speed.toString().length - 1
107 const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
108 cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
b3432a63 109 }
7b0d35b8 110 return Math.round(cpusCycleTimeWeight / cpus().length)
b3432a63 111 }
b3432a63 112}