feat: add worker choice strategies retry mechanism
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
CommitLineData
0bbf65c3 1import { cpus } from 'node:os'
3c93feb9
JB
2import {
3 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
4 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
5} from '../../utils'
08f3f44c 6import type { IPool } from '../pool'
f06e48d8 7import type { IWorker } from '../worker'
10fcfaf4
JB
8import type {
9 IWorkerChoiceStrategy,
57441b79 10 MeasurementStatisticsRequirements,
6c6afb84 11 StrategyPolicy,
87de9ff5 12 TaskStatisticsRequirements,
da309861 13 WorkerChoiceStrategyOptions
10fcfaf4 14} from './selection-strategies-types'
bdaf31cd
JB
15
16/**
9cd39dd4 17 * Worker choice strategy abstract base class.
bdaf31cd 18 *
38e795c1 19 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
20 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
bdaf31cd
JB
22 */
23export abstract class AbstractWorkerChoiceStrategy<
f06e48d8 24 Worker extends IWorker,
b2b1d84e
JB
25 Data = unknown,
26 Response = unknown
17393ac8 27> implements IWorkerChoiceStrategy {
d33be430 28 /**
9b106837 29 * The next worker node key.
d33be430 30 */
9b106837 31 protected nextWorkerNodeKey: number = 0
d33be430 32
6c6afb84
JB
33 /** @inheritDoc */
34 public readonly strategyPolicy: StrategyPolicy = {
35 useDynamicWorker: false
36 }
37
afc003b2 38 /** @inheritDoc */
87de9ff5 39 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
3c93feb9
JB
40 runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
41 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
42 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
10fcfaf4 43 }
bdaf31cd
JB
44
45 /**
6533c3e6 46 * Constructs a worker choice strategy bound to the pool.
bdaf31cd 47 *
38e795c1 48 * @param pool - The pool instance.
da309861 49 * @param opts - The worker choice strategy options.
bdaf31cd
JB
50 */
51 public constructor (
c4855468 52 protected readonly pool: IPool<Worker, Data, Response>,
a20f0ba5 53 protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
b8f3418c 54 ) {
8990357d 55 this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts }
7254e419 56 this.choose = this.choose.bind(this)
b8f3418c 57 }
bdaf31cd 58
932fc8be
JB
59 protected setTaskStatisticsRequirements (
60 opts: WorkerChoiceStrategyOptions
61 ): void {
57441b79
JB
62 this.toggleMedianMeasurementStatisticsRequirements(
63 this.taskStatisticsRequirements.runTime,
64 opts.runTime?.median as boolean
65 )
66 this.toggleMedianMeasurementStatisticsRequirements(
67 this.taskStatisticsRequirements.waitTime,
68 opts.waitTime?.median as boolean
69 )
70 this.toggleMedianMeasurementStatisticsRequirements(
71 this.taskStatisticsRequirements.elu,
72 opts.elu?.median as boolean
73 )
74 }
75
76 private toggleMedianMeasurementStatisticsRequirements (
77 measurementStatisticsRequirements: MeasurementStatisticsRequirements,
78 toggleMedian: boolean
79 ): void {
80 if (measurementStatisticsRequirements.average && toggleMedian) {
81 measurementStatisticsRequirements.average = false
82 measurementStatisticsRequirements.median = toggleMedian
5df69fab 83 }
57441b79
JB
84 if (measurementStatisticsRequirements.median && !toggleMedian) {
85 measurementStatisticsRequirements.average = true
86 measurementStatisticsRequirements.median = toggleMedian
5df69fab 87 }
da309861
JB
88 }
89
afc003b2 90 /** @inheritDoc */
a6f7f1b4 91 public abstract reset (): boolean
ea7a90d3 92
138d29a8 93 /** @inheritDoc */
a4958de2 94 public abstract update (workerNodeKey: number): boolean
138d29a8 95
afc003b2 96 /** @inheritDoc */
c923ce56 97 public abstract choose (): number
97a2abc3 98
afc003b2 99 /** @inheritDoc */
f06e48d8 100 public abstract remove (workerNodeKey: number): boolean
a20f0ba5
JB
101
102 /** @inheritDoc */
103 public setOptions (opts: WorkerChoiceStrategyOptions): void {
8990357d 104 this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts }
65ab8dcc 105 this.setTaskStatisticsRequirements(this.opts)
a20f0ba5 106 }
cb70b19d 107
d4ddb68b
JB
108 /**
109 * Whether the worker node is ready or not.
110 *
111 * @param workerNodeKey - The worker node key.
112 * @returns Whether the worker node is ready or not.
113 */
8990357d 114 private isWorkerNodeReady (workerNodeKey: number): boolean {
19dbc45b
JB
115 return this.pool.workerNodes[workerNodeKey].info.ready
116 }
117
e2b31e32
JB
118 /**
119 * Whether the worker node has back pressure or not (i.e. its tasks queue is full).
120 *
121 * @param workerNodeKey - The worker node key.
122 * @returns `true` if the worker node has back pressure, `false` otherwise.
123 */
8990357d 124 private hasWorkerNodeBackPressure (workerNodeKey: number): boolean {
e2b31e32
JB
125 return this.pool.hasWorkerNodeBackPressure(workerNodeKey)
126 }
127
8990357d
JB
128 /**
129 * Whether the worker node is eligible or not.
130 * A worker node is eligible if it is ready and does not have back pressure.
131 *
132 * @param workerNodeKey - The worker node key.
133 * @returns `true` if the worker node is eligible, `false` otherwise.
134 * @see {@link isWorkerNodeReady}
135 * @see {@link hasWorkerNodeBackPressure}
136 */
137 protected isWorkerNodeEligible (workerNodeKey: number): boolean {
138 return (
139 this.isWorkerNodeReady(workerNodeKey) &&
140 !this.hasWorkerNodeBackPressure(workerNodeKey)
141 )
142 }
143
f6b641d6 144 /**
e6606302 145 * Gets the worker task runtime.
932fc8be
JB
146 * If the task statistics require the average runtime, the average runtime is returned.
147 * If the task statistics require the median runtime , the median runtime is returned.
f6b641d6
JB
148 *
149 * @param workerNodeKey - The worker node key.
e6606302 150 * @returns The worker task runtime.
f6b641d6
JB
151 */
152 protected getWorkerTaskRunTime (workerNodeKey: number): number {
932fc8be 153 return this.taskStatisticsRequirements.runTime.median
71514351
JB
154 ? this.pool.workerNodes[workerNodeKey].usage.runTime?.median ?? 0
155 : this.pool.workerNodes[workerNodeKey].usage.runTime?.average ?? 0
f6b641d6
JB
156 }
157
ef680bb8
JB
158 /**
159 * Gets the worker task wait time.
932fc8be
JB
160 * If the task statistics require the average wait time, the average wait time is returned.
161 * If the task statistics require the median wait time, the median wait time is returned.
ef680bb8
JB
162 *
163 * @param workerNodeKey - The worker node key.
164 * @returns The worker task wait time.
165 */
5df69fab 166 protected getWorkerTaskWaitTime (workerNodeKey: number): number {
932fc8be 167 return this.taskStatisticsRequirements.waitTime.median
71514351
JB
168 ? this.pool.workerNodes[workerNodeKey].usage.waitTime?.median ?? 0
169 : this.pool.workerNodes[workerNodeKey].usage.waitTime?.average ?? 0
ef680bb8
JB
170 }
171
5df69fab
JB
172 /**
173 * Gets the worker task ELU.
9adcefab
JB
174 * If the task statistics require the average ELU, the average ELU is returned.
175 * If the task statistics require the median ELU, the median ELU is returned.
5df69fab
JB
176 *
177 * @param workerNodeKey - The worker node key.
178 * @returns The worker task ELU.
179 */
180 protected getWorkerTaskElu (workerNodeKey: number): number {
181 return this.taskStatisticsRequirements.elu.median
71514351
JB
182 ? this.pool.workerNodes[workerNodeKey].usage.elu.active?.median ?? 0
183 : this.pool.workerNodes[workerNodeKey].usage.elu.active?.average ?? 0
5df69fab
JB
184 }
185
0bbf65c3
JB
186 protected computeDefaultWorkerWeight (): number {
187 let cpusCycleTimeWeight = 0
188 for (const cpu of cpus()) {
189 // CPU estimated cycle time
190 const numberOfDigits = cpu.speed.toString().length - 1
191 const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
192 cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
193 }
194 return Math.round(cpusCycleTimeWeight / cpus().length)
195 }
bdaf31cd 196}