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 least busy worker.
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 LeastBusyWorkerChoiceStrategy
<
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
= {
47 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
52 pool
: IPool
<Worker
, Data
, Response
>,
53 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
56 this.setTaskStatisticsRequirements(this.opts
)
60 public reset (): boolean {
65 public update (): boolean {
70 public choose (): number | undefined {
71 const chosenWorkerNodeKey
= this.leastBusyNextWorkerNodeKey()
72 this.assignChosenWorkerNodeKey(chosenWorkerNodeKey
)
73 return this.nextWorkerNodeKey
77 public remove (): boolean {
81 private leastBusyNextWorkerNodeKey (): number | undefined {
82 let minTime
= Infinity
83 let chosenWorkerNodeKey
: number | undefined
84 for (const [workerNodeKey
, workerNode
] of this.pool
.workerNodes
.entries()) {
86 (workerNode
.usage
.runTime
?.aggregate
?? 0) +
87 (workerNode
.usage
.waitTime
?.aggregate
?? 0)
88 if (this.isWorkerNodeEligible(workerNodeKey
) && workerTime
=== 0) {
89 chosenWorkerNodeKey
= workerNodeKey
92 this.isWorkerNodeEligible(workerNodeKey
) &&
96 chosenWorkerNodeKey
= workerNodeKey
99 return chosenWorkerNodeKey