Commit | Line | Data |
---|---|---|
2fc5cae3 JB |
1 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
2 | import type { IPool } from '../pool' | |
f06e48d8 | 3 | import type { IWorker } from '../worker' |
168c526f | 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
bf90656c JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
87de9ff5 | 7 | TaskStatisticsRequirements, |
2fc5cae3 | 8 | WorkerChoiceStrategyOptions |
bf90656c | 9 | } from './selection-strategies-types' |
168c526f JB |
10 | |
11 | /** | |
e4543b14 | 12 | * Selects the least busy worker. |
168c526f JB |
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. | |
02706357 | 16 | * @typeParam Response - Type of execution response. This can only be serializable 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 | }, | |
62c15a68 | 37 | elu: false |
168c526f JB |
38 | } |
39 | ||
2fc5cae3 JB |
40 | /** @inheritDoc */ |
41 | public constructor ( | |
42 | pool: IPool<Worker, Data, Response>, | |
43 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
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 { | |
56 | return true | |
57 | } | |
58 | ||
afc003b2 | 59 | /** @inheritDoc */ |
c923ce56 | 60 | public choose (): number { |
1c6fe997 | 61 | let minTime = Infinity |
e4543b14 | 62 | let leastBusyWorkerNodeKey!: number |
08f3f44c | 63 | for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { |
1c6fe997 | 64 | const workerTime = |
932fc8be JB |
65 | workerNode.workerUsage.runTime.aggregate + |
66 | workerNode.workerUsage.waitTime.aggregate | |
1c6fe997 | 67 | if (workerTime === 0) { |
08f3f44c | 68 | return workerNodeKey |
1c6fe997 JB |
69 | } else if (workerTime < minTime) { |
70 | minTime = workerTime | |
e4543b14 | 71 | leastBusyWorkerNodeKey = workerNodeKey |
168c526f JB |
72 | } |
73 | } | |
e4543b14 | 74 | return leastBusyWorkerNodeKey |
168c526f | 75 | } |
97a2abc3 | 76 | |
afc003b2 | 77 | /** @inheritDoc */ |
a4958de2 | 78 | public remove (): boolean { |
97a2abc3 JB |
79 | return true |
80 | } | |
168c526f | 81 | } |