refactor: dedupe worker choice strategy options handling code
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
CommitLineData
3c93feb9
JB
1import {
2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
39618ede 3 buildWorkerChoiceStrategyOptions
d35e5717
JB
4} from '../../utils.js'
5import type { IPool } from '../pool.js'
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 */
35 protected previousWorkerNodeKey: number = 0
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 ) {
ce63d9e2 60 this.setOptions(this.opts)
7254e419 61 this.choose = this.choose.bind(this)
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
fb5a7307
JB
124 /** @inheritDoc */
125 public hasPoolWorkerNodesReady (): boolean {
126 return this.pool.workerNodes.some(workerNode => workerNode.info.ready)
127 }
128
d4ddb68b
JB
129 /**
130 * Whether the worker node is ready or not.
131 *
132 * @param workerNodeKey - The worker node key.
133 * @returns Whether the worker node is ready or not.
134 */
ae3ab61d 135 protected isWorkerNodeReady (workerNodeKey: number): boolean {
535fd8d5 136 return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false
19dbc45b
JB
137 }
138
a38b62f1
JB
139 /**
140 * Check the next worker node readiness.
141 */
142 protected checkNextWorkerNodeReadiness (): void {
67f3f2d6
JB
143 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
144 if (!this.isWorkerNodeReady(this.nextWorkerNodeKey!)) {
a38b62f1
JB
145 delete this.nextWorkerNodeKey
146 }
147 }
148
f6b641d6 149 /**
f3a91bac 150 * Gets the worker node task runtime.
932fc8be
JB
151 * If the task statistics require the average runtime, the average runtime is returned.
152 * If the task statistics require the median runtime , the median runtime is returned.
f6b641d6
JB
153 *
154 * @param workerNodeKey - The worker node key.
f3a91bac 155 * @returns The worker node task runtime.
f6b641d6 156 */
f3a91bac 157 protected getWorkerNodeTaskRunTime (workerNodeKey: number): number {
932fc8be 158 return this.taskStatisticsRequirements.runTime.median
46b0bb09
JB
159 ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0
160 : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0
f6b641d6
JB
161 }
162
ef680bb8 163 /**
f3a91bac 164 * Gets the worker node task wait time.
932fc8be
JB
165 * If the task statistics require the average wait time, the average wait time is returned.
166 * If the task statistics require the median wait time, the median wait time is returned.
ef680bb8
JB
167 *
168 * @param workerNodeKey - The worker node key.
f3a91bac 169 * @returns The worker node task wait time.
ef680bb8 170 */
f3a91bac 171 protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number {
932fc8be 172 return this.taskStatisticsRequirements.waitTime.median
46b0bb09
JB
173 ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0
174 : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0
ef680bb8
JB
175 }
176
5df69fab 177 /**
f3a91bac 178 * Gets the worker node task ELU.
9adcefab
JB
179 * If the task statistics require the average ELU, the average ELU is returned.
180 * If the task statistics require the median ELU, the median ELU is returned.
5df69fab
JB
181 *
182 * @param workerNodeKey - The worker node key.
f3a91bac 183 * @returns The worker node task ELU.
5df69fab 184 */
f3a91bac 185 protected getWorkerNodeTaskElu (workerNodeKey: number): number {
5df69fab 186 return this.taskStatisticsRequirements.elu.median
46b0bb09
JB
187 ? this.pool.workerNodes[workerNodeKey].usage.elu.active.median ?? 0
188 : this.pool.workerNodes[workerNodeKey].usage.elu.active.average ?? 0
5df69fab
JB
189 }
190
baca80f7
JB
191 /**
192 * Sets safely the previous worker node key.
193 *
194 * @param workerNodeKey - The worker node key.
195 */
196 protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
197 this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey
198 }
bdaf31cd 199}