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