1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
5 TaskStatisticsRequirements
,
6 WorkerChoiceStrategyOptions
,
7 } from
'./selection-strategies-types.js'
9 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../utils.js'
10 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
13 * 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 override
readonly taskStatisticsRequirements
: TaskStatisticsRequirements
=
33 runTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
34 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
39 pool
: IPool
<Worker
, Data
, Response
>,
40 opts
?: WorkerChoiceStrategyOptions
43 this.setTaskStatisticsRequirements(this.opts
)
46 private leastEluNextWorkerNodeKey (): number | undefined {
47 return this.pool
.workerNodes
.reduce(
48 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
49 return this.isWorkerNodeReady(workerNodeKey
) &&
50 (workerNode
.usage
.elu
.active
.aggregate
?? 0) <
51 (workerNodes
[minWorkerNodeKey
].usage
.elu
.active
.aggregate
?? 0)
60 public choose (): number | undefined {
61 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
62 this.nextWorkerNodeKey
= this.leastEluNextWorkerNodeKey()
63 return this.nextWorkerNodeKey
67 public remove (): boolean {
72 public reset (): boolean {
77 public update (): boolean {