refactor: cleanups
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
CommitLineData
3c93feb9
JB
1import {
2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
00e1bdeb 3 buildInternalWorkerChoiceStrategyOptions
3c93feb9 4} from '../../utils'
08f3f44c 5import type { IPool } from '../pool'
f06e48d8 6import type { IWorker } from '../worker'
10fcfaf4
JB
7import type {
8 IWorkerChoiceStrategy,
26ce26ca 9 InternalWorkerChoiceStrategyOptions,
57441b79 10 MeasurementStatisticsRequirements,
6c6afb84 11 StrategyPolicy,
26ce26ca 12 TaskStatisticsRequirements
10fcfaf4 13} from './selection-strategies-types'
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>,
26ce26ca 58 protected opts: InternalWorkerChoiceStrategyOptions
b8f3418c 59 ) {
3d6f0f73
JB
60 this.opts = buildInternalWorkerChoiceStrategyOptions(
61 this.pool.info.maxSize,
62 this.opts
63 )
64 this.setTaskStatisticsRequirements(this.opts)
7254e419 65 this.choose = this.choose.bind(this)
b8f3418c 66 }
bdaf31cd 67
932fc8be 68 protected setTaskStatisticsRequirements (
26ce26ca 69 opts: InternalWorkerChoiceStrategyOptions
932fc8be 70 ): void {
57441b79
JB
71 this.toggleMedianMeasurementStatisticsRequirements(
72 this.taskStatisticsRequirements.runTime,
73 opts.runTime?.median as boolean
74 )
75 this.toggleMedianMeasurementStatisticsRequirements(
76 this.taskStatisticsRequirements.waitTime,
77 opts.waitTime?.median as boolean
78 )
79 this.toggleMedianMeasurementStatisticsRequirements(
80 this.taskStatisticsRequirements.elu,
81 opts.elu?.median as boolean
82 )
83 }
84
85 private toggleMedianMeasurementStatisticsRequirements (
86 measurementStatisticsRequirements: MeasurementStatisticsRequirements,
87 toggleMedian: boolean
88 ): void {
89 if (measurementStatisticsRequirements.average && toggleMedian) {
90 measurementStatisticsRequirements.average = false
91 measurementStatisticsRequirements.median = toggleMedian
5df69fab 92 }
57441b79
JB
93 if (measurementStatisticsRequirements.median && !toggleMedian) {
94 measurementStatisticsRequirements.average = true
95 measurementStatisticsRequirements.median = toggleMedian
5df69fab 96 }
da309861
JB
97 }
98
39a43af7
JB
99 protected resetWorkerNodeKeyProperties (): void {
100 this.nextWorkerNodeKey = 0
101 this.previousWorkerNodeKey = 0
102 }
103
afc003b2 104 /** @inheritDoc */
a6f7f1b4 105 public abstract reset (): boolean
ea7a90d3 106
138d29a8 107 /** @inheritDoc */
a4958de2 108 public abstract update (workerNodeKey: number): boolean
138d29a8 109
afc003b2 110 /** @inheritDoc */
b1aae695 111 public abstract choose (): number | undefined
97a2abc3 112
afc003b2 113 /** @inheritDoc */
f06e48d8 114 public abstract remove (workerNodeKey: number): boolean
a20f0ba5
JB
115
116 /** @inheritDoc */
26ce26ca 117 public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
00e1bdeb
JB
118 this.opts = buildInternalWorkerChoiceStrategyOptions(
119 this.pool.info.maxSize,
120 opts
121 )
65ab8dcc 122 this.setTaskStatisticsRequirements(this.opts)
a20f0ba5 123 }
cb70b19d 124
fb5a7307
JB
125 /** @inheritDoc */
126 public hasPoolWorkerNodesReady (): boolean {
127 return this.pool.workerNodes.some(workerNode => workerNode.info.ready)
128 }
129
d4ddb68b
JB
130 /**
131 * Whether the worker node is ready or not.
132 *
133 * @param workerNodeKey - The worker node key.
134 * @returns Whether the worker node is ready or not.
135 */
ae3ab61d 136 protected isWorkerNodeReady (workerNodeKey: number): boolean {
535fd8d5 137 return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false
19dbc45b
JB
138 }
139
a38b62f1
JB
140 /**
141 * Check the next worker node readiness.
142 */
143 protected checkNextWorkerNodeReadiness (): void {
144 if (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number)) {
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}