2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
3 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
5 import type { IPool
} from
'../pool'
6 import type { IWorker
} from
'../worker'
7 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
10 TaskStatisticsRequirements
,
11 WorkerChoiceStrategyOptions
12 } from
'./selection-strategies-types'
15 * Selects the worker with the least ELU.
17 * @typeParam Worker - Type of worker which manages the strategy.
18 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
19 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
21 export class LeastEluWorkerChoiceStrategy
<
22 Worker
extends IWorker
,
26 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
27 implements IWorkerChoiceStrategy
{
29 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
30 runTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
31 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
41 pool
: IPool
<Worker
, Data
, Response
>,
42 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
45 this.setTaskStatisticsRequirements(this.opts
)
49 public reset (): boolean {
54 public update (): boolean {
59 public choose (): number | undefined {
60 const chosenWorkerNodeKey
= this.leastEluNextWorkerNodeKey()
61 this.assignChosenWorkerNodeKey(chosenWorkerNodeKey
)
62 return this.nextWorkerNodeKey
66 public remove (): boolean {
70 private leastEluNextWorkerNodeKey (): number | undefined {
71 let minWorkerElu
= Infinity
72 let chosenWorkerNodeKey
: number | undefined
73 for (const [workerNodeKey
, workerNode
] of this.pool
.workerNodes
.entries()) {
74 const workerUsage
= workerNode
.usage
75 const workerElu
= workerUsage
.elu
?.active
?.aggregate
?? 0
76 if (this.isWorkerNodeEligible(workerNodeKey
) && workerElu
=== 0) {
77 chosenWorkerNodeKey
= workerNodeKey
80 this.isWorkerNodeEligible(workerNodeKey
) &&
81 workerElu
< minWorkerElu
83 minWorkerElu
= workerElu
84 chosenWorkerNodeKey
= workerNodeKey
87 return chosenWorkerNodeKey