Commit | Line | Data |
---|---|---|
d35e5717 | 1 | import type { IPool } from '../pool.js' |
e9ed6eee | 2 | import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js' |
d35e5717 JB |
3 | import type { IWorker } from '../worker.js' |
4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' | |
058a9457 JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
39618ede | 7 | TaskStatisticsRequirements, |
3a502712 | 8 | WorkerChoiceStrategyOptions, |
d35e5717 | 9 | } from './selection-strategies-types.js' |
058a9457 JB |
10 | |
11 | /** | |
12 | * Selects the worker with the least ELU. | |
058a9457 | 13 | * @typeParam Worker - Type of worker which manages the strategy. |
e102732c JB |
14 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
15 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
058a9457 JB |
16 | */ |
17 | export class LeastEluWorkerChoiceStrategy< | |
18 | Worker extends IWorker, | |
19 | Data = unknown, | |
20 | Response = unknown | |
21 | > | |
22 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
23 | implements IWorkerChoiceStrategy { | |
24 | /** @inheritDoc */ | |
05302647 | 25 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
3c93feb9 JB |
26 | runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
27 | waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
5df69fab JB |
28 | elu: { |
29 | aggregate: true, | |
30 | average: false, | |
3a502712 JB |
31 | median: false, |
32 | }, | |
058a9457 JB |
33 | } |
34 | ||
35 | /** @inheritDoc */ | |
36 | public constructor ( | |
37 | pool: IPool<Worker, Data, Response>, | |
39618ede | 38 | opts?: WorkerChoiceStrategyOptions |
058a9457 JB |
39 | ) { |
40 | super(pool, opts) | |
e460940e | 41 | this.setTaskStatisticsRequirements(this.opts) |
058a9457 JB |
42 | } |
43 | ||
44 | /** @inheritDoc */ | |
45 | public reset (): boolean { | |
46 | return true | |
47 | } | |
48 | ||
49 | /** @inheritDoc */ | |
50 | public update (): boolean { | |
db703c75 JB |
51 | return true |
52 | } | |
53 | ||
54 | /** @inheritDoc */ | |
b1aae695 | 55 | public choose (): number | undefined { |
baca80f7 | 56 | this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) |
fce028d6 | 57 | this.nextWorkerNodeKey = this.leastEluNextWorkerNodeKey() |
b1aae695 | 58 | return this.nextWorkerNodeKey |
9b106837 JB |
59 | } |
60 | ||
61 | /** @inheritDoc */ | |
62 | public remove (): boolean { | |
63 | return true | |
64 | } | |
65 | ||
b1aae695 | 66 | private leastEluNextWorkerNodeKey (): number | undefined { |
f3a91bac JB |
67 | return this.pool.workerNodes.reduce( |
68 | (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { | |
ae3ab61d JB |
69 | return this.isWorkerNodeReady(workerNodeKey) && |
70 | (workerNode.usage.elu.active.aggregate ?? 0) < | |
71 | (workerNodes[minWorkerNodeKey].usage.elu.active.aggregate ?? 0) | |
f3a91bac JB |
72 | ? workerNodeKey |
73 | : minWorkerNodeKey | |
74 | }, | |
75 | 0 | |
76 | ) | |
058a9457 JB |
77 | } |
78 | } |