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