Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
CommitLineData
e9ed6eee 1import type { IPool } from '../pool.js'
3c93feb9 2import {
ded253e2
JB
3 buildWorkerChoiceStrategyOptions,
4 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
e9ed6eee 5} from '../utils.js'
d35e5717 6import type { IWorker } from '../worker.js'
10fcfaf4
JB
7import type {
8 IWorkerChoiceStrategy,
57441b79 9 MeasurementStatisticsRequirements,
6c6afb84 10 StrategyPolicy,
39618ede
JB
11 TaskStatisticsRequirements,
12 WorkerChoiceStrategyOptions
d35e5717 13} from './selection-strategies-types.js'
bdaf31cd
JB
14
15/**
9cd39dd4 16 * Worker choice strategy abstract base class.
bdaf31cd 17 *
38e795c1 18 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
bdaf31cd
JB
21 */
22export abstract class AbstractWorkerChoiceStrategy<
f06e48d8 23 Worker extends IWorker,
b2b1d84e
JB
24 Data = unknown,
25 Response = unknown
17393ac8 26> implements IWorkerChoiceStrategy {
d33be430 27 /**
9b106837 28 * The next worker node key.
d33be430 29 */
b1aae695 30 protected nextWorkerNodeKey: number | undefined = 0
d33be430 31
7c7bb289
JB
32 /**
33 * The previous worker node key.
34 */
c63a35a0 35 protected previousWorkerNodeKey = 0
7c7bb289 36
6c6afb84
JB
37 /** @inheritDoc */
38 public readonly strategyPolicy: StrategyPolicy = {
b1aae695 39 dynamicWorkerUsage: false,
f6bc9f26 40 dynamicWorkerReady: true
6c6afb84
JB
41 }
42
afc003b2 43 /** @inheritDoc */
87de9ff5 44 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
3c93feb9
JB
45 runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
46 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
47 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
10fcfaf4 48 }
bdaf31cd
JB
49
50 /**
6533c3e6 51 * Constructs a worker choice strategy bound to the pool.
bdaf31cd 52 *
38e795c1 53 * @param pool - The pool instance.
da309861 54 * @param opts - The worker choice strategy options.
bdaf31cd
JB
55 */
56 public constructor (
c4855468 57 protected readonly pool: IPool<Worker, Data, Response>,
39618ede 58 protected opts?: WorkerChoiceStrategyOptions
b8f3418c 59 ) {
7254e419 60 this.choose = this.choose.bind(this)
04d875a3 61 this.setOptions(this.opts)
b8f3418c 62 }
bdaf31cd 63
932fc8be 64 protected setTaskStatisticsRequirements (
39618ede 65 opts: WorkerChoiceStrategyOptions | undefined
932fc8be 66 ): void {
57441b79
JB
67 this.toggleMedianMeasurementStatisticsRequirements(
68 this.taskStatisticsRequirements.runTime,
67f3f2d6 69 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
39618ede 70 opts!.runTime!.median
57441b79
JB
71 )
72 this.toggleMedianMeasurementStatisticsRequirements(
73 this.taskStatisticsRequirements.waitTime,
67f3f2d6 74 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
39618ede 75 opts!.waitTime!.median
57441b79
JB
76 )
77 this.toggleMedianMeasurementStatisticsRequirements(
78 this.taskStatisticsRequirements.elu,
67f3f2d6 79 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
39618ede 80 opts!.elu!.median
57441b79
JB
81 )
82 }
83
84 private toggleMedianMeasurementStatisticsRequirements (
85 measurementStatisticsRequirements: MeasurementStatisticsRequirements,
86 toggleMedian: boolean
87 ): void {
88 if (measurementStatisticsRequirements.average && toggleMedian) {
89 measurementStatisticsRequirements.average = false
90 measurementStatisticsRequirements.median = toggleMedian
5df69fab 91 }
57441b79
JB
92 if (measurementStatisticsRequirements.median && !toggleMedian) {
93 measurementStatisticsRequirements.average = true
94 measurementStatisticsRequirements.median = toggleMedian
5df69fab 95 }
da309861
JB
96 }
97
39a43af7
JB
98 protected resetWorkerNodeKeyProperties (): void {
99 this.nextWorkerNodeKey = 0
100 this.previousWorkerNodeKey = 0
101 }
102
afc003b2 103 /** @inheritDoc */
a6f7f1b4 104 public abstract reset (): boolean
ea7a90d3 105
138d29a8 106 /** @inheritDoc */
a4958de2 107 public abstract update (workerNodeKey: number): boolean
138d29a8 108
afc003b2 109 /** @inheritDoc */
b1aae695 110 public abstract choose (): number | undefined
97a2abc3 111
afc003b2 112 /** @inheritDoc */
f06e48d8 113 public abstract remove (workerNodeKey: number): boolean
a20f0ba5
JB
114
115 /** @inheritDoc */
39618ede
JB
116 public setOptions (opts: WorkerChoiceStrategyOptions | undefined): void {
117 this.opts = buildWorkerChoiceStrategyOptions<Worker, Data, Response>(
118 this.pool,
00e1bdeb
JB
119 opts
120 )
65ab8dcc 121 this.setTaskStatisticsRequirements(this.opts)
a20f0ba5 122 }
cb70b19d 123
d4ddb68b
JB
124 /**
125 * Whether the worker node is ready or not.
126 *
127 * @param workerNodeKey - The worker node key.
128 * @returns Whether the worker node is ready or not.
129 */
ae3ab61d 130 protected isWorkerNodeReady (workerNodeKey: number): boolean {
535fd8d5 131 return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false
19dbc45b
JB
132 }
133
a38b62f1 134 /**
8e8d9101 135 * Check the next worker node key.
a38b62f1 136 */
8e8d9101
JB
137 protected checkNextWorkerNodeKey (): void {
138 if (
139 this.nextWorkerNodeKey != null &&
140 (this.nextWorkerNodeKey < 0 ||
141 !this.isWorkerNodeReady(this.nextWorkerNodeKey))
142 ) {
a38b62f1
JB
143 delete this.nextWorkerNodeKey
144 }
145 }
146
f6b641d6 147 /**
f3a91bac 148 * Gets the worker node task runtime.
932fc8be
JB
149 * If the task statistics require the average runtime, the average runtime is returned.
150 * If the task statistics require the median runtime , the median runtime is returned.
f6b641d6
JB
151 *
152 * @param workerNodeKey - The worker node key.
f3a91bac 153 * @returns The worker node task runtime.
f6b641d6 154 */
f3a91bac 155 protected getWorkerNodeTaskRunTime (workerNodeKey: number): number {
932fc8be 156 return this.taskStatisticsRequirements.runTime.median
46b0bb09
JB
157 ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0
158 : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0
f6b641d6
JB
159 }
160
ef680bb8 161 /**
f3a91bac 162 * Gets the worker node task wait time.
932fc8be
JB
163 * If the task statistics require the average wait time, the average wait time is returned.
164 * If the task statistics require the median wait time, the median wait time is returned.
ef680bb8
JB
165 *
166 * @param workerNodeKey - The worker node key.
f3a91bac 167 * @returns The worker node task wait time.
ef680bb8 168 */
f3a91bac 169 protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number {
932fc8be 170 return this.taskStatisticsRequirements.waitTime.median
46b0bb09
JB
171 ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0
172 : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0
ef680bb8
JB
173 }
174
5df69fab 175 /**
f3a91bac 176 * Gets the worker node task ELU.
9adcefab
JB
177 * If the task statistics require the average ELU, the average ELU is returned.
178 * If the task statistics require the median ELU, the median ELU is returned.
5df69fab
JB
179 *
180 * @param workerNodeKey - The worker node key.
f3a91bac 181 * @returns The worker node task ELU.
5df69fab 182 */
f3a91bac 183 protected getWorkerNodeTaskElu (workerNodeKey: number): number {
5df69fab 184 return this.taskStatisticsRequirements.elu.median
46b0bb09
JB
185 ? this.pool.workerNodes[workerNodeKey].usage.elu.active.median ?? 0
186 : this.pool.workerNodes[workerNodeKey].usage.elu.active.average ?? 0
5df69fab
JB
187 }
188
baca80f7
JB
189 /**
190 * Sets safely the previous worker node key.
191 *
192 * @param workerNodeKey - The worker node key.
193 */
194 protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
8e8d9101
JB
195 this.previousWorkerNodeKey =
196 workerNodeKey != null && workerNodeKey >= 0
197 ? workerNodeKey
198 : this.previousWorkerNodeKey
baca80f7 199 }
bdaf31cd 200}