build: switch default to ESM
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
1 import {
2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
3 buildInternalWorkerChoiceStrategyOptions
4 } from '../../utils.js'
5 import type { IPool } from '../pool.js'
6 import type { IWorker } from '../worker.js'
7 import type {
8 IWorkerChoiceStrategy,
9 InternalWorkerChoiceStrategyOptions,
10 MeasurementStatisticsRequirements,
11 StrategyPolicy,
12 TaskStatisticsRequirements
13 } from './selection-strategies-types.js'
14
15 /**
16 * Worker choice strategy abstract base class.
17 *
18 * @typeParam Worker - Type of worker which manages the strategy.
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.
21 */
22 export abstract class AbstractWorkerChoiceStrategy<
23 Worker extends IWorker,
24 Data = unknown,
25 Response = unknown
26 > implements IWorkerChoiceStrategy {
27 /**
28 * The next worker node key.
29 */
30 protected nextWorkerNodeKey: number | undefined = 0
31
32 /**
33 * The previous worker node key.
34 */
35 protected previousWorkerNodeKey: number = 0
36
37 /** @inheritDoc */
38 public readonly strategyPolicy: StrategyPolicy = {
39 dynamicWorkerUsage: false,
40 dynamicWorkerReady: true
41 }
42
43 /** @inheritDoc */
44 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
45 runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
46 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
47 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
48 }
49
50 /**
51 * Constructs a worker choice strategy bound to the pool.
52 *
53 * @param pool - The pool instance.
54 * @param opts - The worker choice strategy options.
55 */
56 public constructor (
57 protected readonly pool: IPool<Worker, Data, Response>,
58 protected opts: InternalWorkerChoiceStrategyOptions
59 ) {
60 this.opts = buildInternalWorkerChoiceStrategyOptions(
61 this.pool.info.maxSize,
62 this.opts
63 )
64 this.setTaskStatisticsRequirements(this.opts)
65 this.choose = this.choose.bind(this)
66 }
67
68 protected setTaskStatisticsRequirements (
69 opts: InternalWorkerChoiceStrategyOptions
70 ): void {
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
92 }
93 if (measurementStatisticsRequirements.median && !toggleMedian) {
94 measurementStatisticsRequirements.average = true
95 measurementStatisticsRequirements.median = toggleMedian
96 }
97 }
98
99 protected resetWorkerNodeKeyProperties (): void {
100 this.nextWorkerNodeKey = 0
101 this.previousWorkerNodeKey = 0
102 }
103
104 /** @inheritDoc */
105 public abstract reset (): boolean
106
107 /** @inheritDoc */
108 public abstract update (workerNodeKey: number): boolean
109
110 /** @inheritDoc */
111 public abstract choose (): number | undefined
112
113 /** @inheritDoc */
114 public abstract remove (workerNodeKey: number): boolean
115
116 /** @inheritDoc */
117 public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
118 this.opts = buildInternalWorkerChoiceStrategyOptions(
119 this.pool.info.maxSize,
120 opts
121 )
122 this.setTaskStatisticsRequirements(this.opts)
123 }
124
125 /** @inheritDoc */
126 public hasPoolWorkerNodesReady (): boolean {
127 return this.pool.workerNodes.some(workerNode => workerNode.info.ready)
128 }
129
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 */
136 protected isWorkerNodeReady (workerNodeKey: number): boolean {
137 return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false
138 }
139
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
149 /**
150 * Gets the worker node task runtime.
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.
153 *
154 * @param workerNodeKey - The worker node key.
155 * @returns The worker node task runtime.
156 */
157 protected getWorkerNodeTaskRunTime (workerNodeKey: number): number {
158 return this.taskStatisticsRequirements.runTime.median
159 ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0
160 : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0
161 }
162
163 /**
164 * Gets the worker node task wait time.
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.
167 *
168 * @param workerNodeKey - The worker node key.
169 * @returns The worker node task wait time.
170 */
171 protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number {
172 return this.taskStatisticsRequirements.waitTime.median
173 ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0
174 : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0
175 }
176
177 /**
178 * Gets the worker node task ELU.
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.
181 *
182 * @param workerNodeKey - The worker node key.
183 * @returns The worker node task ELU.
184 */
185 protected getWorkerNodeTaskElu (workerNodeKey: number): number {
186 return this.taskStatisticsRequirements.elu.median
187 ? this.pool.workerNodes[workerNodeKey].usage.elu.active.median ?? 0
188 : this.pool.workerNodes[workerNodeKey].usage.elu.active.average ?? 0
189 }
190
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 }
199 }