Commit | Line | Data |
---|---|---|
f06e48d8 | 1 | import type { IWorker } from '../worker' |
65d7a1c9 | 2 | import type { IPool } from '../pool' |
26ce26ca | 3 | import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils' |
b3432a63 | 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
bf90656c JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
26ce26ca JB |
7 | InternalWorkerChoiceStrategyOptions, |
8 | TaskStatisticsRequirements | |
bf90656c | 9 | } from './selection-strategies-types' |
b3432a63 | 10 | |
b3432a63 JB |
11 | /** |
12 | * Selects the next worker with a weighted round robin scheduling algorithm. | |
13 | * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin. | |
14 | * | |
38e795c1 | 15 | * @typeParam Worker - Type of worker which manages the strategy. |
e102732c JB |
16 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
17 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
b3432a63 JB |
18 | */ |
19 | export class WeightedRoundRobinWorkerChoiceStrategy< | |
f06e48d8 | 20 | Worker extends IWorker, |
b2b1d84e JB |
21 | Data = unknown, |
22 | Response = unknown | |
bf90656c JB |
23 | > |
24 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
17393ac8 | 25 | implements IWorkerChoiceStrategy { |
afc003b2 | 26 | /** @inheritDoc */ |
87de9ff5 | 27 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
932fc8be JB |
28 | runTime: { |
29 | aggregate: true, | |
30 | average: true, | |
31 | median: false | |
32 | }, | |
3c93feb9 JB |
33 | waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
34 | elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS | |
10fcfaf4 JB |
35 | } |
36 | ||
b3432a63 JB |
37 | /** |
38 | * Default worker weight. | |
39 | */ | |
777af0ac | 40 | private readonly defaultWorkerWeight: number |
b3432a63 | 41 | /** |
f3a91bac | 42 | * Worker node virtual task runtime. |
b3432a63 | 43 | */ |
f3a91bac | 44 | private workerNodeVirtualTaskRunTime: number = 0 |
b3432a63 | 45 | |
2fc5cae3 | 46 | /** @inheritDoc */ |
da309861 | 47 | public constructor ( |
c4855468 | 48 | pool: IPool<Worker, Data, Response>, |
26ce26ca | 49 | opts: InternalWorkerChoiceStrategyOptions |
da309861 JB |
50 | ) { |
51 | super(pool, opts) | |
932fc8be | 52 | this.setTaskStatisticsRequirements(this.opts) |
08f3f44c | 53 | this.defaultWorkerWeight = this.computeDefaultWorkerWeight() |
b3432a63 JB |
54 | } |
55 | ||
afc003b2 | 56 | /** @inheritDoc */ |
a6f7f1b4 | 57 | public reset (): boolean { |
39a43af7 | 58 | this.resetWorkerNodeKeyProperties() |
f3a91bac | 59 | this.workerNodeVirtualTaskRunTime = 0 |
ea7a90d3 JB |
60 | return true |
61 | } | |
62 | ||
138d29a8 JB |
63 | /** @inheritDoc */ |
64 | public update (): boolean { | |
65 | return true | |
66 | } | |
67 | ||
afc003b2 | 68 | /** @inheritDoc */ |
b1aae695 | 69 | public choose (): number | undefined { |
baca80f7 | 70 | this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) |
a38b62f1 JB |
71 | this.weightedRoundRobinNextWorkerNodeKey() |
72 | this.checkNextWorkerNodeReadiness() | |
73 | return this.nextWorkerNodeKey | |
b3432a63 JB |
74 | } |
75 | ||
afc003b2 | 76 | /** @inheritDoc */ |
f06e48d8 | 77 | public remove (workerNodeKey: number): boolean { |
226b02a3 JB |
78 | if (this.pool.workerNodes.length === 0) { |
79 | this.reset() | |
80 | } | |
9b106837 | 81 | if (this.nextWorkerNodeKey === workerNodeKey) { |
f3a91bac | 82 | this.workerNodeVirtualTaskRunTime = 0 |
0dd90fe1 JB |
83 | if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { |
84 | this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 | |
85 | } | |
97a2abc3 | 86 | } |
226b02a3 JB |
87 | if ( |
88 | this.previousWorkerNodeKey === workerNodeKey && | |
89 | this.previousWorkerNodeKey > this.pool.workerNodes.length - 1 | |
90 | ) { | |
91 | this.previousWorkerNodeKey = this.pool.workerNodes.length - 1 | |
92 | } | |
08f3f44c | 93 | return true |
2377984d | 94 | } |
9b106837 | 95 | |
b1aae695 | 96 | private weightedRoundRobinNextWorkerNodeKey (): number | undefined { |
a38b62f1 JB |
97 | const workerWeight = |
98 | this.opts.weights?.[ | |
99 | this.nextWorkerNodeKey ?? this.previousWorkerNodeKey | |
100 | ] ?? this.defaultWorkerWeight | |
101 | if (this.workerNodeVirtualTaskRunTime < workerWeight) { | |
102 | this.workerNodeVirtualTaskRunTime = | |
103 | this.workerNodeVirtualTaskRunTime + | |
104 | this.getWorkerNodeTaskRunTime( | |
7c7bb289 | 105 | this.nextWorkerNodeKey ?? this.previousWorkerNodeKey |
a38b62f1 JB |
106 | ) |
107 | } else { | |
108 | this.nextWorkerNodeKey = | |
109 | this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 | |
110 | ? 0 | |
111 | : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1 | |
112 | this.workerNodeVirtualTaskRunTime = 0 | |
113 | } | |
20016c79 | 114 | return this.nextWorkerNodeKey |
9b106837 | 115 | } |
b3432a63 | 116 | } |