Commit | Line | Data |
---|---|---|
3c93feb9 JB |
1 | import { |
2 | DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
3 | DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
4 | } from '../../utils' | |
058a9457 JB |
5 | import type { IPool } from '../pool' |
6 | import type { IWorker } from '../worker' | |
7 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' | |
8 | import type { | |
9 | IWorkerChoiceStrategy, | |
05302647 | 10 | TaskStatisticsRequirements, |
058a9457 JB |
11 | WorkerChoiceStrategyOptions |
12 | } from './selection-strategies-types' | |
13 | ||
14 | /** | |
15 | * Selects the worker with the least ELU. | |
16 | * | |
17 | * @typeParam Worker - Type of worker which manages the strategy. | |
e102732c JB |
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. | |
058a9457 JB |
20 | */ |
21 | export class LeastEluWorkerChoiceStrategy< | |
22 | Worker extends IWorker, | |
23 | Data = unknown, | |
24 | Response = unknown | |
25 | > | |
26 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
27 | implements IWorkerChoiceStrategy { | |
28 | /** @inheritDoc */ | |
05302647 | 29 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
3c93feb9 JB |
30 | runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
31 | waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
5df69fab JB |
32 | elu: { |
33 | aggregate: true, | |
34 | average: false, | |
35 | median: false | |
36 | } | |
058a9457 JB |
37 | } |
38 | ||
39 | /** @inheritDoc */ | |
40 | public constructor ( | |
41 | pool: IPool<Worker, Data, Response>, | |
42 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
43 | ) { | |
44 | super(pool, opts) | |
e460940e | 45 | this.setTaskStatisticsRequirements(this.opts) |
058a9457 JB |
46 | } |
47 | ||
48 | /** @inheritDoc */ | |
49 | public reset (): boolean { | |
50 | return true | |
51 | } | |
52 | ||
53 | /** @inheritDoc */ | |
54 | public update (): boolean { | |
db703c75 JB |
55 | return true |
56 | } | |
57 | ||
58 | /** @inheritDoc */ | |
59 | public choose (): number { | |
9b106837 JB |
60 | this.leastEluNextWorkerNodeKey() |
61 | return this.nextWorkerNodeKey | |
62 | } | |
63 | ||
64 | /** @inheritDoc */ | |
65 | public remove (): boolean { | |
66 | return true | |
67 | } | |
68 | ||
69 | private leastEluNextWorkerNodeKey (): void { | |
cdb517b3 | 70 | let minWorkerElu = Infinity |
058a9457 | 71 | for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { |
465b2940 | 72 | const workerUsage = workerNode.usage |
98e72cda | 73 | const workerElu = workerUsage.elu?.active?.aggregate ?? 0 |
9b106837 JB |
74 | if (this.isWorkerNodeReady(workerNodeKey) && workerElu === 0) { |
75 | this.nextWorkerNodeKey = workerNodeKey | |
5ea80606 | 76 | break |
19dbc45b | 77 | } else if ( |
9b106837 | 78 | this.isWorkerNodeReady(workerNodeKey) && |
19dbc45b JB |
79 | workerElu < minWorkerElu |
80 | ) { | |
cdb517b3 | 81 | minWorkerElu = workerElu |
9b106837 | 82 | this.nextWorkerNodeKey = workerNodeKey |
058a9457 JB |
83 | } |
84 | } | |
058a9457 JB |
85 | } |
86 | } |