1 import type { IPool
} from
'../pool.js'
2 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../utils.js'
3 import type { IWorker
} from
'../worker.js'
6 MeasurementStatisticsRequirements
,
8 TaskStatisticsRequirements
,
9 WorkerChoiceStrategyOptions
10 } from
'./selection-strategies-types.js'
11 import { buildWorkerChoiceStrategyOptions
} from
'./selection-strategies-utils.js'
14 * Worker choice strategy abstract base class.
16 * @typeParam Worker - Type of worker which manages the strategy.
17 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
18 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
20 export abstract class AbstractWorkerChoiceStrategy
<
21 Worker
extends IWorker
,
24 > implements IWorkerChoiceStrategy
{
26 * The next worker node key.
28 protected nextWorkerNodeKey
: number | undefined = 0
31 * The previous worker node key.
33 protected previousWorkerNodeKey
= 0
36 public readonly strategyPolicy
: StrategyPolicy
= {
37 dynamicWorkerUsage
: false,
38 dynamicWorkerReady
: true
42 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
43 runTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
44 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
45 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
49 * Constructs a worker choice strategy bound to the pool.
51 * @param pool - The pool instance.
52 * @param opts - The worker choice strategy options.
55 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
56 protected opts
?: WorkerChoiceStrategyOptions
58 this.choose
= this.choose
.bind(this)
59 this.setOptions(this.opts
)
62 protected setTaskStatisticsRequirements (
63 opts
: WorkerChoiceStrategyOptions
| undefined
65 this.toggleMedianMeasurementStatisticsRequirements(
66 this.taskStatisticsRequirements
.runTime
,
67 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70 this.toggleMedianMeasurementStatisticsRequirements(
71 this.taskStatisticsRequirements
.waitTime
,
72 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
73 opts
!.waitTime
!.median
75 this.toggleMedianMeasurementStatisticsRequirements(
76 this.taskStatisticsRequirements
.elu
,
77 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
82 private toggleMedianMeasurementStatisticsRequirements (
83 measurementStatisticsRequirements
: MeasurementStatisticsRequirements
,
86 if (measurementStatisticsRequirements
.average
&& toggleMedian
) {
87 measurementStatisticsRequirements
.average
= false
88 measurementStatisticsRequirements
.median
= toggleMedian
90 if (measurementStatisticsRequirements
.median
&& !toggleMedian
) {
91 measurementStatisticsRequirements
.average
= true
92 measurementStatisticsRequirements
.median
= toggleMedian
96 protected resetWorkerNodeKeyProperties (): void {
97 this.nextWorkerNodeKey
= 0
98 this.previousWorkerNodeKey
= 0
102 public abstract reset (): boolean
105 public abstract update (workerNodeKey
: number): boolean
108 public abstract choose (): number | undefined
111 public abstract remove (workerNodeKey
: number): boolean
114 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
115 this.opts
= buildWorkerChoiceStrategyOptions
<Worker
, Data
, Response
>(
119 this.setTaskStatisticsRequirements(this.opts
)
123 * Whether the worker node is ready or not.
125 * @param workerNodeKey - The worker node key.
126 * @returns Whether the worker node is ready or not.
128 protected isWorkerNodeReady (workerNodeKey
: number): boolean {
129 return this.pool
.workerNodes
[workerNodeKey
]?.info
?.ready
?? false
133 * Check the next worker node key.
135 protected checkNextWorkerNodeKey (): void {
137 this.nextWorkerNodeKey
!= null &&
138 (this.nextWorkerNodeKey
< 0 ||
139 !this.isWorkerNodeReady(this.nextWorkerNodeKey
))
141 delete this.nextWorkerNodeKey
146 * Gets the worker node task runtime.
147 * If the task statistics require the average runtime, the average runtime is returned.
148 * If the task statistics require the median runtime , the median runtime is returned.
150 * @param workerNodeKey - The worker node key.
151 * @returns The worker node task runtime.
153 protected getWorkerNodeTaskRunTime (workerNodeKey
: number): number {
154 return this.taskStatisticsRequirements
.runTime
.median
155 ? this.pool
.workerNodes
[workerNodeKey
].usage
.runTime
.median
?? 0
156 : this.pool
.workerNodes
[workerNodeKey
].usage
.runTime
.average
?? 0
160 * Gets the worker node task wait time.
161 * If the task statistics require the average wait time, the average wait time is returned.
162 * If the task statistics require the median wait time, the median wait time is returned.
164 * @param workerNodeKey - The worker node key.
165 * @returns The worker node task wait time.
167 protected getWorkerNodeTaskWaitTime (workerNodeKey
: number): number {
168 return this.taskStatisticsRequirements
.waitTime
.median
169 ? this.pool
.workerNodes
[workerNodeKey
].usage
.waitTime
.median
?? 0
170 : this.pool
.workerNodes
[workerNodeKey
].usage
.waitTime
.average
?? 0
174 * Gets the worker node task ELU.
175 * If the task statistics require the average ELU, the average ELU is returned.
176 * If the task statistics require the median ELU, the median ELU is returned.
178 * @param workerNodeKey - The worker node key.
179 * @returns The worker node task ELU.
181 protected getWorkerNodeTaskElu (workerNodeKey
: number): number {
182 return this.taskStatisticsRequirements
.elu
.median
183 ? this.pool
.workerNodes
[workerNodeKey
].usage
.elu
.active
.median
?? 0
184 : this.pool
.workerNodes
[workerNodeKey
].usage
.elu
.active
.average
?? 0
188 * Sets safely the previous worker node key.
190 * @param workerNodeKey - The worker node key.
192 protected setPreviousWorkerNodeKey (workerNodeKey
: number | undefined): void {
193 this.previousWorkerNodeKey
=
194 workerNodeKey
!= null && workerNodeKey
>= 0
196 : this.previousWorkerNodeKey