Commit | Line | Data |
---|---|---|
0bbf65c3 | 1 | import { cpus } from 'node:os' |
bbeadd16 | 2 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
08f3f44c | 3 | import type { IPool } from '../pool' |
f06e48d8 | 4 | import type { IWorker } from '../worker' |
10fcfaf4 JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
b6b32453 | 7 | TaskStatistics, |
da309861 | 8 | WorkerChoiceStrategyOptions |
10fcfaf4 | 9 | } from './selection-strategies-types' |
bdaf31cd JB |
10 | |
11 | /** | |
9cd39dd4 | 12 | * Worker choice strategy abstract base class. |
bdaf31cd | 13 | * |
38e795c1 JB |
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. | |
02706357 | 16 | * @typeParam Response - Type of execution response. This can only be serializable data. |
bdaf31cd JB |
17 | */ |
18 | export abstract class AbstractWorkerChoiceStrategy< | |
f06e48d8 | 19 | Worker extends IWorker, |
b2b1d84e JB |
20 | Data = unknown, |
21 | Response = unknown | |
17393ac8 | 22 | > implements IWorkerChoiceStrategy { |
cb70b19d JB |
23 | /** |
24 | * Toggles finding the last free worker node key. | |
25 | */ | |
26 | private toggleFindLastFreeWorkerNodeKey: boolean = false | |
afc003b2 | 27 | /** @inheritDoc */ |
b6b32453 | 28 | public readonly taskStatistics: TaskStatistics = { |
c6bd2650 | 29 | runTime: false, |
78099a15 | 30 | avgRunTime: false, |
0567595a JB |
31 | medRunTime: false, |
32 | waitTime: false, | |
33 | avgWaitTime: false, | |
62c15a68 JB |
34 | medWaitTime: false, |
35 | elu: false | |
10fcfaf4 | 36 | } |
bdaf31cd JB |
37 | |
38 | /** | |
6533c3e6 | 39 | * Constructs a worker choice strategy bound to the pool. |
bdaf31cd | 40 | * |
38e795c1 | 41 | * @param pool - The pool instance. |
da309861 | 42 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
43 | */ |
44 | public constructor ( | |
c4855468 | 45 | protected readonly pool: IPool<Worker, Data, Response>, |
a20f0ba5 | 46 | protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
b8f3418c | 47 | ) { |
7254e419 | 48 | this.choose = this.choose.bind(this) |
b8f3418c | 49 | } |
bdaf31cd | 50 | |
b6b32453 JB |
51 | protected setTaskStatistics (opts: WorkerChoiceStrategyOptions): void { |
52 | if (this.taskStatistics.avgRunTime && opts.medRunTime === true) { | |
53 | this.taskStatistics.avgRunTime = false | |
54 | this.taskStatistics.medRunTime = opts.medRunTime as boolean | |
da309861 | 55 | } |
b6b32453 JB |
56 | if (this.taskStatistics.medRunTime && opts.medRunTime === false) { |
57 | this.taskStatistics.avgRunTime = true | |
58 | this.taskStatistics.medRunTime = opts.medRunTime as boolean | |
a20f0ba5 | 59 | } |
b6b32453 JB |
60 | if (this.taskStatistics.avgWaitTime && opts.medWaitTime === true) { |
61 | this.taskStatistics.avgWaitTime = false | |
62 | this.taskStatistics.medWaitTime = opts.medWaitTime as boolean | |
0567595a | 63 | } |
b6b32453 JB |
64 | if (this.taskStatistics.medWaitTime && opts.medWaitTime === false) { |
65 | this.taskStatistics.avgWaitTime = true | |
66 | this.taskStatistics.medWaitTime = opts.medWaitTime as boolean | |
0567595a | 67 | } |
da309861 JB |
68 | } |
69 | ||
afc003b2 | 70 | /** @inheritDoc */ |
a6f7f1b4 | 71 | public abstract reset (): boolean |
ea7a90d3 | 72 | |
138d29a8 | 73 | /** @inheritDoc */ |
a4958de2 | 74 | public abstract update (workerNodeKey: number): boolean |
138d29a8 | 75 | |
afc003b2 | 76 | /** @inheritDoc */ |
c923ce56 | 77 | public abstract choose (): number |
97a2abc3 | 78 | |
afc003b2 | 79 | /** @inheritDoc */ |
f06e48d8 | 80 | public abstract remove (workerNodeKey: number): boolean |
a20f0ba5 JB |
81 | |
82 | /** @inheritDoc */ | |
83 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
d01e4d67 | 84 | opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
b6b32453 | 85 | this.setTaskStatistics(opts) |
a20f0ba5 JB |
86 | this.opts = opts |
87 | } | |
cb70b19d JB |
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 | |
e0ae6100 | 97 | return this.findLastFreeWorkerNodeKey() |
cb70b19d JB |
98 | } |
99 | this.toggleFindLastFreeWorkerNodeKey = true | |
e0ae6100 JB |
100 | return this.findFirstFreeWorkerNodeKey() |
101 | } | |
102 | ||
f6b641d6 | 103 | /** |
e6606302 JB |
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. | |
f6b641d6 JB |
107 | * |
108 | * @param workerNodeKey - The worker node key. | |
e6606302 | 109 | * @returns The worker task runtime. |
f6b641d6 JB |
110 | */ |
111 | protected getWorkerTaskRunTime (workerNodeKey: number): number { | |
b6b32453 | 112 | return this.taskStatistics.medRunTime |
f6b641d6 JB |
113 | ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime |
114 | : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime | |
115 | } | |
116 | ||
ef680bb8 JB |
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 { | |
b6b32453 | 126 | return this.taskStatistics.medWaitTime |
ef680bb8 JB |
127 | ? this.pool.workerNodes[workerNodeKey].tasksUsage.medWaitTime |
128 | : this.pool.workerNodes[workerNodeKey].tasksUsage.avgWaitTime | |
129 | } | |
130 | ||
0bbf65c3 JB |
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 | ||
e0ae6100 JB |
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 => { | |
a4958de2 | 153 | return workerNode.tasksUsage.running === 0 |
e0ae6100 JB |
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 { | |
0e7c56b0 | 167 | // It requires node >= 18.0.0: |
e0ae6100 | 168 | // return this.workerNodes.findLastIndex(workerNode => { |
a4958de2 | 169 | // return workerNode.tasksUsage.running === 0 |
e0ae6100 | 170 | // }) |
0682ba15 JB |
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 | |
e0ae6100 JB |
178 | } |
179 | } | |
180 | return -1 | |
cb70b19d | 181 | } |
bdaf31cd | 182 | } |