1 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
7 InternalWorkerChoiceStrategyOptions
,
8 TaskStatisticsRequirements
9 } from
'./selection-strategies-types'
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
: InternalWorkerChoiceStrategyOptions
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 return this.pool
.workerNodes
.reduce(
69 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
70 return this.isWorkerNodeReady(workerNodeKey
) &&
71 (workerNode
.usage
.elu
.active
.aggregate
?? 0) <
72 (workerNodes
[minWorkerNodeKey
].usage
.elu
.active
.aggregate
?? 0)