1 import type { IPool
} from
'../pool.js'
3 buildWorkerChoiceStrategyOptions
,
4 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
6 import type { IWorker
} from
'../worker.js'
9 MeasurementStatisticsRequirements
,
11 TaskStatisticsRequirements
,
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies-types.js'
16 * Worker choice strategy abstract base class.
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.
22 export abstract class AbstractWorkerChoiceStrategy
<
23 Worker
extends IWorker
,
26 > implements IWorkerChoiceStrategy
{
28 * The next worker node key.
30 protected nextWorkerNodeKey
: number | undefined = 0
33 * The previous worker node key.
35 protected previousWorkerNodeKey
= 0
38 public readonly strategyPolicy
: StrategyPolicy
= {
39 dynamicWorkerUsage
: false,
40 dynamicWorkerReady
: true
44 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
45 runTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
46 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
47 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
51 * Constructs a worker choice strategy bound to the pool.
53 * @param pool - The pool instance.
54 * @param opts - The worker choice strategy options.
57 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
58 protected opts
?: WorkerChoiceStrategyOptions
60 this.choose
= this.choose
.bind(this)
61 this.setOptions(this.opts
)
64 protected setTaskStatisticsRequirements (
65 opts
: WorkerChoiceStrategyOptions
| undefined
67 this.toggleMedianMeasurementStatisticsRequirements(
68 this.taskStatisticsRequirements
.runTime
,
69 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
72 this.toggleMedianMeasurementStatisticsRequirements(
73 this.taskStatisticsRequirements
.waitTime
,
74 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
75 opts
!.waitTime
!.median
77 this.toggleMedianMeasurementStatisticsRequirements(
78 this.taskStatisticsRequirements
.elu
,
79 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
84 private toggleMedianMeasurementStatisticsRequirements (
85 measurementStatisticsRequirements
: MeasurementStatisticsRequirements
,
88 if (measurementStatisticsRequirements
.average
&& toggleMedian
) {
89 measurementStatisticsRequirements
.average
= false
90 measurementStatisticsRequirements
.median
= toggleMedian
92 if (measurementStatisticsRequirements
.median
&& !toggleMedian
) {
93 measurementStatisticsRequirements
.average
= true
94 measurementStatisticsRequirements
.median
= toggleMedian
98 protected resetWorkerNodeKeyProperties (): void {
99 this.nextWorkerNodeKey
= 0
100 this.previousWorkerNodeKey
= 0
104 public abstract reset (): boolean
107 public abstract update (workerNodeKey
: number): boolean
110 public abstract choose (): number | undefined
113 public abstract remove (workerNodeKey
: number): boolean
116 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
117 this.opts
= buildWorkerChoiceStrategyOptions
<Worker
, Data
, Response
>(
121 this.setTaskStatisticsRequirements(this.opts
)
125 * Whether the worker node is ready or not.
127 * @param workerNodeKey - The worker node key.
128 * @returns Whether the worker node is ready or not.
130 protected isWorkerNodeReady (workerNodeKey
: number): boolean {
131 return this.pool
.workerNodes
[workerNodeKey
]?.info
?.ready
?? false
135 * Check the next worker node key.
137 protected checkNextWorkerNodeKey (): void {
139 this.nextWorkerNodeKey
!= null &&
140 (this.nextWorkerNodeKey
< 0 ||
141 !this.isWorkerNodeReady(this.nextWorkerNodeKey
))
143 delete this.nextWorkerNodeKey
148 * Gets the worker node task runtime.
149 * If the task statistics require the average runtime, the average runtime is returned.
150 * If the task statistics require the median runtime , the median runtime is returned.
152 * @param workerNodeKey - The worker node key.
153 * @returns The worker node task runtime.
155 protected getWorkerNodeTaskRunTime (workerNodeKey
: number): number {
156 return this.taskStatisticsRequirements
.runTime
.median
157 ? this.pool
.workerNodes
[workerNodeKey
].usage
.runTime
.median
?? 0
158 : this.pool
.workerNodes
[workerNodeKey
].usage
.runTime
.average
?? 0
162 * Gets the worker node task wait time.
163 * If the task statistics require the average wait time, the average wait time is returned.
164 * If the task statistics require the median wait time, the median wait time is returned.
166 * @param workerNodeKey - The worker node key.
167 * @returns The worker node task wait time.
169 protected getWorkerNodeTaskWaitTime (workerNodeKey
: number): number {
170 return this.taskStatisticsRequirements
.waitTime
.median
171 ? this.pool
.workerNodes
[workerNodeKey
].usage
.waitTime
.median
?? 0
172 : this.pool
.workerNodes
[workerNodeKey
].usage
.waitTime
.average
?? 0
176 * Gets the worker node task ELU.
177 * If the task statistics require the average ELU, the average ELU is returned.
178 * If the task statistics require the median ELU, the median ELU is returned.
180 * @param workerNodeKey - The worker node key.
181 * @returns The worker node task ELU.
183 protected getWorkerNodeTaskElu (workerNodeKey
: number): number {
184 return this.taskStatisticsRequirements
.elu
.median
185 ? this.pool
.workerNodes
[workerNodeKey
].usage
.elu
.active
.median
?? 0
186 : this.pool
.workerNodes
[workerNodeKey
].usage
.elu
.active
.average
?? 0
190 * Sets safely the previous worker node key.
192 * @param workerNodeKey - The worker node key.
194 protected setPreviousWorkerNodeKey (workerNodeKey
: number | undefined): void {
195 this.previousWorkerNodeKey
=
196 workerNodeKey
!= null && workerNodeKey
>= 0
198 : this.previousWorkerNodeKey