feat: add statistics accounting to ELU fields
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
1 import type { IWorker } from '../worker'
2 import type { IPool } from '../pool'
3 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
4 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5 import type {
6 IWorkerChoiceStrategy,
7 TaskStatisticsRequirements,
8 WorkerChoiceStrategyOptions
9 } from './selection-strategies-types'
10
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 *
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.
17 * @typeParam Response - Type of execution response. This can only be serializable data.
18 */
19 export class WeightedRoundRobinWorkerChoiceStrategy<
20 Worker extends IWorker,
21 Data = unknown,
22 Response = unknown
23 >
24 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
25 implements IWorkerChoiceStrategy {
26 /** @inheritDoc */
27 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
28 runTime: {
29 aggregate: true,
30 average: true,
31 median: false
32 },
33 waitTime: {
34 aggregate: false,
35 average: false,
36 median: false
37 },
38 elu: {
39 aggregate: false,
40 average: false,
41 median: false
42 }
43 }
44
45 /**
46 * Worker node id where the current task will be submitted.
47 */
48 private currentWorkerNodeId: number = 0
49 /**
50 * Default worker weight.
51 */
52 private readonly defaultWorkerWeight: number
53 /**
54 * Worker virtual task runtime.
55 */
56 private workerVirtualTaskRunTime: number = 0
57
58 /** @inheritDoc */
59 public constructor (
60 pool: IPool<Worker, Data, Response>,
61 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
62 ) {
63 super(pool, opts)
64 this.setTaskStatisticsRequirements(this.opts)
65 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
66 }
67
68 /** @inheritDoc */
69 public reset (): boolean {
70 this.currentWorkerNodeId = 0
71 this.workerVirtualTaskRunTime = 0
72 return true
73 }
74
75 /** @inheritDoc */
76 public update (): boolean {
77 return true
78 }
79
80 /** @inheritDoc */
81 public choose (): number {
82 const chosenWorkerNodeKey = this.currentWorkerNodeId
83 const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
84 const workerWeight =
85 this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
86 if (workerVirtualTaskRunTime < workerWeight) {
87 this.workerVirtualTaskRunTime =
88 workerVirtualTaskRunTime +
89 this.getWorkerTaskRunTime(chosenWorkerNodeKey)
90 } else {
91 this.currentWorkerNodeId =
92 this.currentWorkerNodeId === this.pool.workerNodes.length - 1
93 ? 0
94 : this.currentWorkerNodeId + 1
95 this.workerVirtualTaskRunTime = 0
96 }
97 return chosenWorkerNodeKey
98 }
99
100 /** @inheritDoc */
101 public remove (workerNodeKey: number): boolean {
102 if (this.currentWorkerNodeId === workerNodeKey) {
103 if (this.pool.workerNodes.length === 0) {
104 this.currentWorkerNodeId = 0
105 } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
106 this.currentWorkerNodeId = this.pool.workerNodes.length - 1
107 }
108 this.workerVirtualTaskRunTime = 0
109 }
110 return true
111 }
112 }