1 import type { IPool
} from
'../pool.js'
2 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../utils.js'
3 import type { IWorker
} from
'../worker.js'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
7 TaskStatisticsRequirements
,
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types.js'
12 * Selects the worker with the least ELU.
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
16 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
18 export class LeastEluWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
27 runTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
28 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
38 pool
: IPool
<Worker
, Data
, Response
>,
39 opts
?: WorkerChoiceStrategyOptions
42 this.setTaskStatisticsRequirements(this.opts
)
46 public reset (): boolean {
51 public update (): boolean {
56 public choose (): number | undefined {
57 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
58 this.nextWorkerNodeKey
= this.leastEluNextWorkerNodeKey()
59 return this.nextWorkerNodeKey
63 public remove (): boolean {
67 private leastEluNextWorkerNodeKey (): number | undefined {
68 if (this.pool
.workerNodes
.length
=== 0) {
71 return this.pool
.workerNodes
.reduce(
72 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
73 return this.isWorkerNodeReady(workerNodeKey
) &&
74 (workerNode
.usage
.elu
.active
.aggregate
?? 0) <
75 (workerNodes
[minWorkerNodeKey
].usage
.elu
.active
.aggregate
?? 0)