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