Commit | Line | Data |
---|---|---|
bbeadd16 | 1 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
08f3f44c | 2 | import type { IPool } from '../pool' |
f06e48d8 | 3 | import type { IWorker } from '../worker' |
10fcfaf4 JB |
4 | import type { |
5 | IWorkerChoiceStrategy, | |
da309861 JB |
6 | RequiredStatistics, |
7 | WorkerChoiceStrategyOptions | |
10fcfaf4 | 8 | } from './selection-strategies-types' |
bdaf31cd JB |
9 | |
10 | /** | |
9cd39dd4 | 11 | * Worker choice strategy abstract base class. |
bdaf31cd | 12 | * |
38e795c1 JB |
13 | * @typeParam Worker - Type of worker which manages the strategy. |
14 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
02706357 | 15 | * @typeParam Response - Type of execution response. This can only be serializable data. |
bdaf31cd JB |
16 | */ |
17 | export abstract class AbstractWorkerChoiceStrategy< | |
f06e48d8 | 18 | Worker extends IWorker, |
b2b1d84e JB |
19 | Data = unknown, |
20 | Response = unknown | |
17393ac8 | 21 | > implements IWorkerChoiceStrategy { |
cb70b19d JB |
22 | /** |
23 | * Toggles finding the last free worker node key. | |
24 | */ | |
25 | private toggleFindLastFreeWorkerNodeKey: boolean = false | |
afc003b2 | 26 | /** @inheritDoc */ |
da309861 | 27 | public readonly requiredStatistics: RequiredStatistics = { |
c6bd2650 | 28 | runTime: false, |
78099a15 | 29 | avgRunTime: false, |
0567595a JB |
30 | medRunTime: false, |
31 | waitTime: false, | |
32 | avgWaitTime: false, | |
33 | medWaitTime: false | |
10fcfaf4 | 34 | } |
bdaf31cd JB |
35 | |
36 | /** | |
6533c3e6 | 37 | * Constructs a worker choice strategy bound to the pool. |
bdaf31cd | 38 | * |
38e795c1 | 39 | * @param pool - The pool instance. |
da309861 | 40 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
41 | */ |
42 | public constructor ( | |
c4855468 | 43 | protected readonly pool: IPool<Worker, Data, Response>, |
a20f0ba5 | 44 | protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
b8f3418c | 45 | ) { |
7254e419 | 46 | this.choose = this.choose.bind(this) |
b8f3418c | 47 | } |
bdaf31cd | 48 | |
49be33fe | 49 | protected setRequiredStatistics (opts: WorkerChoiceStrategyOptions): void { |
f9f00b5f | 50 | if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) { |
2fc5cae3 JB |
51 | this.requiredStatistics.avgRunTime = false |
52 | this.requiredStatistics.medRunTime = opts.medRunTime as boolean | |
da309861 | 53 | } |
a20f0ba5 JB |
54 | if (this.requiredStatistics.medRunTime && opts.medRunTime === false) { |
55 | this.requiredStatistics.avgRunTime = true | |
56 | this.requiredStatistics.medRunTime = opts.medRunTime as boolean | |
57 | } | |
0567595a JB |
58 | if (this.requiredStatistics.avgWaitTime && opts.medWaitTime === true) { |
59 | this.requiredStatistics.avgWaitTime = false | |
60 | this.requiredStatistics.medWaitTime = opts.medWaitTime as boolean | |
61 | } | |
62 | if (this.requiredStatistics.medWaitTime && opts.medWaitTime === false) { | |
63 | this.requiredStatistics.avgWaitTime = true | |
64 | this.requiredStatistics.medWaitTime = opts.medWaitTime as boolean | |
65 | } | |
da309861 JB |
66 | } |
67 | ||
afc003b2 | 68 | /** @inheritDoc */ |
a6f7f1b4 | 69 | public abstract reset (): boolean |
ea7a90d3 | 70 | |
138d29a8 | 71 | /** @inheritDoc */ |
a4958de2 | 72 | public abstract update (workerNodeKey: number): boolean |
138d29a8 | 73 | |
afc003b2 | 74 | /** @inheritDoc */ |
c923ce56 | 75 | public abstract choose (): number |
97a2abc3 | 76 | |
afc003b2 | 77 | /** @inheritDoc */ |
f06e48d8 | 78 | public abstract remove (workerNodeKey: number): boolean |
a20f0ba5 JB |
79 | |
80 | /** @inheritDoc */ | |
81 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
d01e4d67 | 82 | opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
49be33fe | 83 | this.setRequiredStatistics(opts) |
a20f0ba5 JB |
84 | this.opts = opts |
85 | } | |
cb70b19d JB |
86 | |
87 | /** | |
88 | * Finds a free worker node key. | |
89 | * | |
90 | * @returns The free worker node key or `-1` if there is no free worker node. | |
91 | */ | |
92 | protected findFreeWorkerNodeKey (): number { | |
93 | if (this.toggleFindLastFreeWorkerNodeKey) { | |
94 | this.toggleFindLastFreeWorkerNodeKey = false | |
e0ae6100 | 95 | return this.findLastFreeWorkerNodeKey() |
cb70b19d JB |
96 | } |
97 | this.toggleFindLastFreeWorkerNodeKey = true | |
e0ae6100 JB |
98 | return this.findFirstFreeWorkerNodeKey() |
99 | } | |
100 | ||
f6b641d6 | 101 | /** |
e6606302 JB |
102 | * Gets the worker task runtime. |
103 | * If the required statistics are `avgRunTime`, the average runtime is returned. | |
104 | * If the required statistics are `medRunTime`, the median runtime is returned. | |
f6b641d6 JB |
105 | * |
106 | * @param workerNodeKey - The worker node key. | |
e6606302 | 107 | * @returns The worker task runtime. |
f6b641d6 JB |
108 | */ |
109 | protected getWorkerTaskRunTime (workerNodeKey: number): number { | |
110 | return this.requiredStatistics.medRunTime | |
111 | ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime | |
112 | : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime | |
113 | } | |
114 | ||
ef680bb8 JB |
115 | /** |
116 | * Gets the worker task wait time. | |
117 | * If the required statistics are `avgWaitTime`, the average wait time is returned. | |
118 | * If the required statistics are `medWaitTime`, the median wait time is returned. | |
119 | * | |
120 | * @param workerNodeKey - The worker node key. | |
121 | * @returns The worker task wait time. | |
122 | */ | |
123 | protected getWorkerWaitTime (workerNodeKey: number): number { | |
124 | return this.requiredStatistics.medWaitTime | |
125 | ? this.pool.workerNodes[workerNodeKey].tasksUsage.medWaitTime | |
126 | : this.pool.workerNodes[workerNodeKey].tasksUsage.avgWaitTime | |
127 | } | |
128 | ||
e0ae6100 JB |
129 | /** |
130 | * Finds the first free worker node key based on the number of tasks the worker has applied. | |
131 | * | |
132 | * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. | |
133 | * | |
134 | * If no free worker is found, `-1` is returned. | |
135 | * | |
136 | * @returns A worker node key if there is one, `-1` otherwise. | |
137 | */ | |
138 | private findFirstFreeWorkerNodeKey (): number { | |
139 | return this.pool.workerNodes.findIndex(workerNode => { | |
a4958de2 | 140 | return workerNode.tasksUsage.running === 0 |
e0ae6100 JB |
141 | }) |
142 | } | |
143 | ||
144 | /** | |
145 | * Finds the last free worker node key based on the number of tasks the worker has applied. | |
146 | * | |
147 | * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. | |
148 | * | |
149 | * If no free worker is found, `-1` is returned. | |
150 | * | |
151 | * @returns A worker node key if there is one, `-1` otherwise. | |
152 | */ | |
153 | private findLastFreeWorkerNodeKey (): number { | |
0e7c56b0 | 154 | // It requires node >= 18.0.0: |
e0ae6100 | 155 | // return this.workerNodes.findLastIndex(workerNode => { |
a4958de2 | 156 | // return workerNode.tasksUsage.running === 0 |
e0ae6100 | 157 | // }) |
0682ba15 JB |
158 | for ( |
159 | let workerNodeKey = this.pool.workerNodes.length - 1; | |
160 | workerNodeKey >= 0; | |
161 | workerNodeKey-- | |
162 | ) { | |
163 | if (this.pool.workerNodes[workerNodeKey].tasksUsage.running === 0) { | |
164 | return workerNodeKey | |
e0ae6100 JB |
165 | } |
166 | } | |
167 | return -1 | |
cb70b19d | 168 | } |
bdaf31cd | 169 | } |