feat: add ELU tasks accounting
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
1 import { cpus } from 'node:os'
2 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
3 import type { IPool } from '../pool'
4 import type { IWorker } from '../worker'
5 import type {
6 IWorkerChoiceStrategy,
7 RequiredStatistics,
8 WorkerChoiceStrategyOptions
9 } from './selection-strategies-types'
10
11 /**
12 * Worker choice strategy abstract base class.
13 *
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
16 * @typeParam Response - Type of execution response. This can only be serializable data.
17 */
18 export abstract class AbstractWorkerChoiceStrategy<
19 Worker extends IWorker,
20 Data = unknown,
21 Response = unknown
22 > implements IWorkerChoiceStrategy {
23 /**
24 * Toggles finding the last free worker node key.
25 */
26 private toggleFindLastFreeWorkerNodeKey: boolean = false
27 /** @inheritDoc */
28 public readonly requiredStatistics: RequiredStatistics = {
29 runTime: false,
30 avgRunTime: false,
31 medRunTime: false,
32 waitTime: false,
33 avgWaitTime: false,
34 medWaitTime: false,
35 elu: false
36 }
37
38 /**
39 * Constructs a worker choice strategy bound to the pool.
40 *
41 * @param pool - The pool instance.
42 * @param opts - The worker choice strategy options.
43 */
44 public constructor (
45 protected readonly pool: IPool<Worker, Data, Response>,
46 protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
47 ) {
48 this.choose = this.choose.bind(this)
49 }
50
51 protected setRequiredStatistics (opts: WorkerChoiceStrategyOptions): void {
52 if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) {
53 this.requiredStatistics.avgRunTime = false
54 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
55 }
56 if (this.requiredStatistics.medRunTime && opts.medRunTime === false) {
57 this.requiredStatistics.avgRunTime = true
58 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
59 }
60 if (this.requiredStatistics.avgWaitTime && opts.medWaitTime === true) {
61 this.requiredStatistics.avgWaitTime = false
62 this.requiredStatistics.medWaitTime = opts.medWaitTime as boolean
63 }
64 if (this.requiredStatistics.medWaitTime && opts.medWaitTime === false) {
65 this.requiredStatistics.avgWaitTime = true
66 this.requiredStatistics.medWaitTime = opts.medWaitTime as boolean
67 }
68 }
69
70 /** @inheritDoc */
71 public abstract reset (): boolean
72
73 /** @inheritDoc */
74 public abstract update (workerNodeKey: number): boolean
75
76 /** @inheritDoc */
77 public abstract choose (): number
78
79 /** @inheritDoc */
80 public abstract remove (workerNodeKey: number): boolean
81
82 /** @inheritDoc */
83 public setOptions (opts: WorkerChoiceStrategyOptions): void {
84 opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
85 this.setRequiredStatistics(opts)
86 this.opts = opts
87 }
88
89 /**
90 * Finds a free worker node key.
91 *
92 * @returns The free worker node key or `-1` if there is no free worker node.
93 */
94 protected findFreeWorkerNodeKey (): number {
95 if (this.toggleFindLastFreeWorkerNodeKey) {
96 this.toggleFindLastFreeWorkerNodeKey = false
97 return this.findLastFreeWorkerNodeKey()
98 }
99 this.toggleFindLastFreeWorkerNodeKey = true
100 return this.findFirstFreeWorkerNodeKey()
101 }
102
103 /**
104 * Gets the worker task runtime.
105 * If the required statistics are `avgRunTime`, the average runtime is returned.
106 * If the required statistics are `medRunTime`, the median runtime is returned.
107 *
108 * @param workerNodeKey - The worker node key.
109 * @returns The worker task runtime.
110 */
111 protected getWorkerTaskRunTime (workerNodeKey: number): number {
112 return this.requiredStatistics.medRunTime
113 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
114 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
115 }
116
117 /**
118 * Gets the worker task wait time.
119 * If the required statistics are `avgWaitTime`, the average wait time is returned.
120 * If the required statistics are `medWaitTime`, the median wait time is returned.
121 *
122 * @param workerNodeKey - The worker node key.
123 * @returns The worker task wait time.
124 */
125 protected getWorkerWaitTime (workerNodeKey: number): number {
126 return this.requiredStatistics.medWaitTime
127 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medWaitTime
128 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgWaitTime
129 }
130
131 protected computeDefaultWorkerWeight (): number {
132 let cpusCycleTimeWeight = 0
133 for (const cpu of cpus()) {
134 // CPU estimated cycle time
135 const numberOfDigits = cpu.speed.toString().length - 1
136 const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
137 cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
138 }
139 return Math.round(cpusCycleTimeWeight / cpus().length)
140 }
141
142 /**
143 * Finds the first free worker node key based on the number of tasks the worker has applied.
144 *
145 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
146 *
147 * If no free worker is found, `-1` is returned.
148 *
149 * @returns A worker node key if there is one, `-1` otherwise.
150 */
151 private findFirstFreeWorkerNodeKey (): number {
152 return this.pool.workerNodes.findIndex(workerNode => {
153 return workerNode.tasksUsage.running === 0
154 })
155 }
156
157 /**
158 * Finds the last free worker node key based on the number of tasks the worker has applied.
159 *
160 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
161 *
162 * If no free worker is found, `-1` is returned.
163 *
164 * @returns A worker node key if there is one, `-1` otherwise.
165 */
166 private findLastFreeWorkerNodeKey (): number {
167 // It requires node >= 18.0.0:
168 // return this.workerNodes.findLastIndex(workerNode => {
169 // return workerNode.tasksUsage.running === 0
170 // })
171 for (
172 let workerNodeKey = this.pool.workerNodes.length - 1;
173 workerNodeKey >= 0;
174 workerNodeKey--
175 ) {
176 if (this.pool.workerNodes[workerNodeKey].tasksUsage.running === 0) {
177 return workerNodeKey
178 }
179 }
180 return -1
181 }
182 }