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'
11 TaskStatisticsRequirements
,
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies-types'
16 * Selects the worker with the least ELU.
18 * @typeParam Worker - Type of worker which manages the strategy.
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
22 export class LeastEluWorkerChoiceStrategy
<
23 Worker
extends IWorker
,
27 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
28 implements IWorkerChoiceStrategy
{
30 public readonly strategyPolicy
: StrategyPolicy
= {
31 dynamicWorkerUsage
: false,
32 dynamicWorkerReady
: true
36 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
37 runTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
38 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
48 pool
: IPool
<Worker
, Data
, Response
>,
49 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
52 this.setTaskStatisticsRequirements(this.opts
)
56 public reset (): boolean {
61 public update (): boolean {
66 public choose (): number | undefined {
67 const chosenWorkerNodeKey
= this.leastEluNextWorkerNodeKey()
68 this.assignChosenWorkerNodeKey(chosenWorkerNodeKey
)
69 return this.nextWorkerNodeKey
73 public remove (): boolean {
77 private leastEluNextWorkerNodeKey (): number | undefined {
78 let minWorkerElu
= Infinity
79 let chosenWorkerNodeKey
: number | undefined
80 for (const [workerNodeKey
, workerNode
] of this.pool
.workerNodes
.entries()) {
81 const workerUsage
= workerNode
.usage
82 const workerElu
= workerUsage
.elu
?.active
?.aggregate
?? 0
83 if (this.isWorkerNodeEligible(workerNodeKey
) && workerElu
=== 0) {
84 chosenWorkerNodeKey
= workerNodeKey
87 this.isWorkerNodeEligible(workerNodeKey
) &&
88 workerElu
< minWorkerElu
90 minWorkerElu
= workerElu
91 chosenWorkerNodeKey
= workerNodeKey
94 return chosenWorkerNodeKey