Commit | Line | Data |
---|---|---|
3c93feb9 JB |
1 | import { |
2 | DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
3 | DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
4 | } from '../../utils' | |
2fc5cae3 | 5 | import type { IPool } from '../pool' |
f06e48d8 | 6 | import type { IWorker } from '../worker' |
168c526f | 7 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
bf90656c JB |
8 | import type { |
9 | IWorkerChoiceStrategy, | |
87de9ff5 | 10 | TaskStatisticsRequirements, |
2fc5cae3 | 11 | WorkerChoiceStrategyOptions |
bf90656c | 12 | } from './selection-strategies-types' |
168c526f JB |
13 | |
14 | /** | |
e4543b14 | 15 | * Selects the least busy worker. |
168c526f JB |
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. | |
168c526f | 20 | */ |
e4543b14 | 21 | export class LeastBusyWorkerChoiceStrategy< |
f06e48d8 | 22 | Worker extends IWorker, |
b2b1d84e JB |
23 | Data = unknown, |
24 | Response = unknown | |
bf90656c JB |
25 | > |
26 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
17393ac8 | 27 | implements IWorkerChoiceStrategy { |
afc003b2 | 28 | /** @inheritDoc */ |
87de9ff5 | 29 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
932fc8be JB |
30 | runTime: { |
31 | aggregate: true, | |
32 | average: false, | |
33 | median: false | |
34 | }, | |
35 | waitTime: { | |
36 | aggregate: true, | |
37 | average: false, | |
38 | median: false | |
39 | }, | |
3c93feb9 | 40 | elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS |
168c526f JB |
41 | } |
42 | ||
2fc5cae3 JB |
43 | /** @inheritDoc */ |
44 | public constructor ( | |
45 | pool: IPool<Worker, Data, Response>, | |
46 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
47 | ) { | |
48 | super(pool, opts) | |
932fc8be | 49 | this.setTaskStatisticsRequirements(this.opts) |
2fc5cae3 JB |
50 | } |
51 | ||
afc003b2 | 52 | /** @inheritDoc */ |
168c526f JB |
53 | public reset (): boolean { |
54 | return true | |
55 | } | |
56 | ||
138d29a8 JB |
57 | /** @inheritDoc */ |
58 | public update (): boolean { | |
db703c75 JB |
59 | return true |
60 | } | |
61 | ||
62 | /** @inheritDoc */ | |
b1aae695 | 63 | public choose (): number | undefined { |
baca80f7 | 64 | this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) |
fce028d6 | 65 | this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey() |
b1aae695 | 66 | return this.nextWorkerNodeKey |
9b106837 JB |
67 | } |
68 | ||
69 | /** @inheritDoc */ | |
70 | public remove (): boolean { | |
71 | return true | |
72 | } | |
73 | ||
b1aae695 | 74 | private leastBusyNextWorkerNodeKey (): number | undefined { |
f3a91bac JB |
75 | return this.pool.workerNodes.reduce( |
76 | (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { | |
ae3ab61d JB |
77 | return this.isWorkerNodeReady(workerNodeKey) && |
78 | (workerNode.usage.runTime.aggregate ?? 0) + | |
79 | (workerNode.usage.waitTime.aggregate ?? 0) < | |
80 | (workerNodes[minWorkerNodeKey].usage.runTime.aggregate ?? 0) + | |
81 | (workerNodes[minWorkerNodeKey].usage.waitTime.aggregate ?? 0) | |
f3a91bac JB |
82 | ? workerNodeKey |
83 | : minWorkerNodeKey | |
84 | }, | |
85 | 0 | |
86 | ) | |
97a2abc3 | 87 | } |
168c526f | 88 | } |