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, | |
da309861 JB |
7 | RequiredStatistics, |
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 */ |
da309861 | 28 | public readonly requiredStatistics: RequiredStatistics = { |
c6bd2650 | 29 | runTime: false, |
78099a15 | 30 | avgRunTime: false, |
0567595a JB |
31 | medRunTime: false, |
32 | waitTime: false, | |
33 | avgWaitTime: false, | |
34 | medWaitTime: false | |
10fcfaf4 | 35 | } |
bdaf31cd JB |
36 | |
37 | /** | |
6533c3e6 | 38 | * Constructs a worker choice strategy bound to the pool. |
bdaf31cd | 39 | * |
38e795c1 | 40 | * @param pool - The pool instance. |
da309861 | 41 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
42 | */ |
43 | public constructor ( | |
c4855468 | 44 | protected readonly pool: IPool<Worker, Data, Response>, |
a20f0ba5 | 45 | protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
b8f3418c | 46 | ) { |
7254e419 | 47 | this.choose = this.choose.bind(this) |
b8f3418c | 48 | } |
bdaf31cd | 49 | |
49be33fe | 50 | protected setRequiredStatistics (opts: WorkerChoiceStrategyOptions): void { |
f9f00b5f | 51 | if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) { |
2fc5cae3 JB |
52 | this.requiredStatistics.avgRunTime = false |
53 | this.requiredStatistics.medRunTime = opts.medRunTime as boolean | |
da309861 | 54 | } |
a20f0ba5 JB |
55 | if (this.requiredStatistics.medRunTime && opts.medRunTime === false) { |
56 | this.requiredStatistics.avgRunTime = true | |
57 | this.requiredStatistics.medRunTime = opts.medRunTime as boolean | |
58 | } | |
0567595a JB |
59 | if (this.requiredStatistics.avgWaitTime && opts.medWaitTime === true) { |
60 | this.requiredStatistics.avgWaitTime = false | |
61 | this.requiredStatistics.medWaitTime = opts.medWaitTime as boolean | |
62 | } | |
63 | if (this.requiredStatistics.medWaitTime && opts.medWaitTime === false) { | |
64 | this.requiredStatistics.avgWaitTime = true | |
65 | this.requiredStatistics.medWaitTime = opts.medWaitTime as boolean | |
66 | } | |
da309861 JB |
67 | } |
68 | ||
afc003b2 | 69 | /** @inheritDoc */ |
a6f7f1b4 | 70 | public abstract reset (): boolean |
ea7a90d3 | 71 | |
138d29a8 | 72 | /** @inheritDoc */ |
a4958de2 | 73 | public abstract update (workerNodeKey: number): boolean |
138d29a8 | 74 | |
afc003b2 | 75 | /** @inheritDoc */ |
c923ce56 | 76 | public abstract choose (): number |
97a2abc3 | 77 | |
afc003b2 | 78 | /** @inheritDoc */ |
f06e48d8 | 79 | public abstract remove (workerNodeKey: number): boolean |
a20f0ba5 JB |
80 | |
81 | /** @inheritDoc */ | |
82 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
d01e4d67 | 83 | opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
49be33fe | 84 | this.setRequiredStatistics(opts) |
a20f0ba5 JB |
85 | this.opts = opts |
86 | } | |
cb70b19d JB |
87 | |
88 | /** | |
89 | * Finds a free worker node key. | |
90 | * | |
91 | * @returns The free worker node key or `-1` if there is no free worker node. | |
92 | */ | |
93 | protected findFreeWorkerNodeKey (): number { | |
94 | if (this.toggleFindLastFreeWorkerNodeKey) { | |
95 | this.toggleFindLastFreeWorkerNodeKey = false | |
e0ae6100 | 96 | return this.findLastFreeWorkerNodeKey() |
cb70b19d JB |
97 | } |
98 | this.toggleFindLastFreeWorkerNodeKey = true | |
e0ae6100 JB |
99 | return this.findFirstFreeWorkerNodeKey() |
100 | } | |
101 | ||
f6b641d6 | 102 | /** |
e6606302 JB |
103 | * Gets the worker task runtime. |
104 | * If the required statistics are `avgRunTime`, the average runtime is returned. | |
105 | * If the required statistics are `medRunTime`, the median runtime is returned. | |
f6b641d6 JB |
106 | * |
107 | * @param workerNodeKey - The worker node key. | |
e6606302 | 108 | * @returns The worker task runtime. |
f6b641d6 JB |
109 | */ |
110 | protected getWorkerTaskRunTime (workerNodeKey: number): number { | |
111 | return this.requiredStatistics.medRunTime | |
112 | ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime | |
113 | : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime | |
114 | } | |
115 | ||
ef680bb8 JB |
116 | /** |
117 | * Gets the worker task wait time. | |
118 | * If the required statistics are `avgWaitTime`, the average wait time is returned. | |
119 | * If the required statistics are `medWaitTime`, the median wait time is returned. | |
120 | * | |
121 | * @param workerNodeKey - The worker node key. | |
122 | * @returns The worker task wait time. | |
123 | */ | |
124 | protected getWorkerWaitTime (workerNodeKey: number): number { | |
125 | return this.requiredStatistics.medWaitTime | |
126 | ? this.pool.workerNodes[workerNodeKey].tasksUsage.medWaitTime | |
127 | : this.pool.workerNodes[workerNodeKey].tasksUsage.avgWaitTime | |
128 | } | |
129 | ||
0bbf65c3 JB |
130 | protected computeDefaultWorkerWeight (): number { |
131 | let cpusCycleTimeWeight = 0 | |
132 | for (const cpu of cpus()) { | |
133 | // CPU estimated cycle time | |
134 | const numberOfDigits = cpu.speed.toString().length - 1 | |
135 | const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits)) | |
136 | cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits) | |
137 | } | |
138 | return Math.round(cpusCycleTimeWeight / cpus().length) | |
139 | } | |
140 | ||
e0ae6100 JB |
141 | /** |
142 | * Finds the first free worker node key based on the number of tasks the worker has applied. | |
143 | * | |
144 | * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. | |
145 | * | |
146 | * If no free worker is found, `-1` is returned. | |
147 | * | |
148 | * @returns A worker node key if there is one, `-1` otherwise. | |
149 | */ | |
150 | private findFirstFreeWorkerNodeKey (): number { | |
151 | return this.pool.workerNodes.findIndex(workerNode => { | |
a4958de2 | 152 | return workerNode.tasksUsage.running === 0 |
e0ae6100 JB |
153 | }) |
154 | } | |
155 | ||
156 | /** | |
157 | * Finds the last free worker node key based on the number of tasks the worker has applied. | |
158 | * | |
159 | * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. | |
160 | * | |
161 | * If no free worker is found, `-1` is returned. | |
162 | * | |
163 | * @returns A worker node key if there is one, `-1` otherwise. | |
164 | */ | |
165 | private findLastFreeWorkerNodeKey (): number { | |
0e7c56b0 | 166 | // It requires node >= 18.0.0: |
e0ae6100 | 167 | // return this.workerNodes.findLastIndex(workerNode => { |
a4958de2 | 168 | // return workerNode.tasksUsage.running === 0 |
e0ae6100 | 169 | // }) |
0682ba15 JB |
170 | for ( |
171 | let workerNodeKey = this.pool.workerNodes.length - 1; | |
172 | workerNodeKey >= 0; | |
173 | workerNodeKey-- | |
174 | ) { | |
175 | if (this.pool.workerNodes[workerNodeKey].tasksUsage.running === 0) { | |
176 | return workerNodeKey | |
e0ae6100 JB |
177 | } |
178 | } | |
179 | return -1 | |
cb70b19d | 180 | } |
bdaf31cd | 181 | } |