fix: ensure worker choice is retried at least the pool max size
[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,
26ce26ca 4 getDefaultInternalWorkerChoiceStrategyOptions
3c93feb9 5} from '../../utils'
08f3f44c 6import type { IPool } from '../pool'
f06e48d8 7import type { IWorker } from '../worker'
10fcfaf4
JB
8import type {
9 IWorkerChoiceStrategy,
26ce26ca 10 InternalWorkerChoiceStrategyOptions,
57441b79 11 MeasurementStatisticsRequirements,
6c6afb84 12 StrategyPolicy,
26ce26ca 13 TaskStatisticsRequirements
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 */
b1aae695 31 protected nextWorkerNodeKey: number | undefined = 0
d33be430 32
7c7bb289
JB
33 /**
34 * The previous worker node key.
35 */
36 protected previousWorkerNodeKey: number = 0
37
6c6afb84
JB
38 /** @inheritDoc */
39 public readonly strategyPolicy: StrategyPolicy = {
b1aae695 40 dynamicWorkerUsage: false,
f6bc9f26 41 dynamicWorkerReady: true
6c6afb84
JB
42 }
43
afc003b2 44 /** @inheritDoc */
87de9ff5 45 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
3c93feb9
JB
46 runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
47 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
48 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
10fcfaf4 49 }
bdaf31cd
JB
50
51 /**
6533c3e6 52 * Constructs a worker choice strategy bound to the pool.
bdaf31cd 53 *
38e795c1 54 * @param pool - The pool instance.
da309861 55 * @param opts - The worker choice strategy options.
bdaf31cd
JB
56 */
57 public constructor (
c4855468 58 protected readonly pool: IPool<Worker, Data, Response>,
26ce26ca 59 protected opts: InternalWorkerChoiceStrategyOptions
b8f3418c 60 ) {
26ce26ca 61 this.setOptions(this.opts)
7254e419 62 this.choose = this.choose.bind(this)
b8f3418c 63 }
bdaf31cd 64
932fc8be 65 protected setTaskStatisticsRequirements (
26ce26ca 66 opts: InternalWorkerChoiceStrategyOptions
932fc8be 67 ): void {
57441b79
JB
68 this.toggleMedianMeasurementStatisticsRequirements(
69 this.taskStatisticsRequirements.runTime,
70 opts.runTime?.median as boolean
71 )
72 this.toggleMedianMeasurementStatisticsRequirements(
73 this.taskStatisticsRequirements.waitTime,
74 opts.waitTime?.median as boolean
75 )
76 this.toggleMedianMeasurementStatisticsRequirements(
77 this.taskStatisticsRequirements.elu,
78 opts.elu?.median as boolean
79 )
80 }
81
82 private toggleMedianMeasurementStatisticsRequirements (
83 measurementStatisticsRequirements: MeasurementStatisticsRequirements,
84 toggleMedian: boolean
85 ): void {
86 if (measurementStatisticsRequirements.average && toggleMedian) {
87 measurementStatisticsRequirements.average = false
88 measurementStatisticsRequirements.median = toggleMedian
5df69fab 89 }
57441b79
JB
90 if (measurementStatisticsRequirements.median && !toggleMedian) {
91 measurementStatisticsRequirements.average = true
92 measurementStatisticsRequirements.median = toggleMedian
5df69fab 93 }
da309861
JB
94 }
95
39a43af7
JB
96 protected resetWorkerNodeKeyProperties (): void {
97 this.nextWorkerNodeKey = 0
98 this.previousWorkerNodeKey = 0
99 }
100
afc003b2 101 /** @inheritDoc */
a6f7f1b4 102 public abstract reset (): boolean
ea7a90d3 103
138d29a8 104 /** @inheritDoc */
a4958de2 105 public abstract update (workerNodeKey: number): boolean
138d29a8 106
afc003b2 107 /** @inheritDoc */
b1aae695 108 public abstract choose (): number | undefined
97a2abc3 109
afc003b2 110 /** @inheritDoc */
f06e48d8 111 public abstract remove (workerNodeKey: number): boolean
a20f0ba5
JB
112
113 /** @inheritDoc */
26ce26ca
JB
114 public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
115 this.opts = {
116 ...getDefaultInternalWorkerChoiceStrategyOptions(this.pool.info.maxSize),
117 ...opts
118 }
65ab8dcc 119 this.setTaskStatisticsRequirements(this.opts)
a20f0ba5 120 }
cb70b19d 121
fb5a7307
JB
122 /** @inheritDoc */
123 public hasPoolWorkerNodesReady (): boolean {
124 return this.pool.workerNodes.some(workerNode => workerNode.info.ready)
125 }
126
d4ddb68b
JB
127 /**
128 * Whether the worker node is ready or not.
129 *
130 * @param workerNodeKey - The worker node key.
131 * @returns Whether the worker node is ready or not.
132 */
ae3ab61d 133 protected isWorkerNodeReady (workerNodeKey: number): boolean {
535fd8d5 134 return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false
19dbc45b
JB
135 }
136
a38b62f1
JB
137 /**
138 * Check the next worker node readiness.
139 */
140 protected checkNextWorkerNodeReadiness (): void {
141 if (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number)) {
142 delete this.nextWorkerNodeKey
143 }
144 }
145
f6b641d6 146 /**
f3a91bac 147 * Gets the worker node task runtime.
932fc8be
JB
148 * If the task statistics require the average runtime, the average runtime is returned.
149 * If the task statistics require the median runtime , the median runtime is returned.
f6b641d6
JB
150 *
151 * @param workerNodeKey - The worker node key.
f3a91bac 152 * @returns The worker node task runtime.
f6b641d6 153 */
f3a91bac 154 protected getWorkerNodeTaskRunTime (workerNodeKey: number): number {
932fc8be 155 return this.taskStatisticsRequirements.runTime.median
46b0bb09
JB
156 ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0
157 : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0
f6b641d6
JB
158 }
159
ef680bb8 160 /**
f3a91bac 161 * Gets the worker node task wait time.
932fc8be
JB
162 * If the task statistics require the average wait time, the average wait time is returned.
163 * If the task statistics require the median wait time, the median wait time is returned.
ef680bb8
JB
164 *
165 * @param workerNodeKey - The worker node key.
f3a91bac 166 * @returns The worker node task wait time.
ef680bb8 167 */
f3a91bac 168 protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number {
932fc8be 169 return this.taskStatisticsRequirements.waitTime.median
46b0bb09
JB
170 ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0
171 : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0
ef680bb8
JB
172 }
173
5df69fab 174 /**
f3a91bac 175 * Gets the worker node task ELU.
9adcefab
JB
176 * If the task statistics require the average ELU, the average ELU is returned.
177 * If the task statistics require the median ELU, the median ELU is returned.
5df69fab
JB
178 *
179 * @param workerNodeKey - The worker node key.
f3a91bac 180 * @returns The worker node task ELU.
5df69fab 181 */
f3a91bac 182 protected getWorkerNodeTaskElu (workerNodeKey: number): number {
5df69fab 183 return this.taskStatisticsRequirements.elu.median
46b0bb09
JB
184 ? this.pool.workerNodes[workerNodeKey].usage.elu.active.median ?? 0
185 : this.pool.workerNodes[workerNodeKey].usage.elu.active.average ?? 0
5df69fab
JB
186 }
187
baca80f7
JB
188 /**
189 * Sets safely the previous worker node key.
190 *
191 * @param workerNodeKey - The worker node key.
192 */
193 protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
194 this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey
195 }
196
0bbf65c3
JB
197 protected computeDefaultWorkerWeight (): number {
198 let cpusCycleTimeWeight = 0
199 for (const cpu of cpus()) {
200 // CPU estimated cycle time
201 const numberOfDigits = cpu.speed.toString().length - 1
202 const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
203 cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
204 }
205 return Math.round(cpusCycleTimeWeight / cpus().length)
206 }
bdaf31cd 207}