Commit | Line | Data |
---|---|---|
d35e5717 | 1 | import type { IPool } from '../pool.js' |
e9ed6eee | 2 | import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js' |
ded253e2 | 3 | import type { IWorker } from '../worker.js' |
d35e5717 | 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' |
feec6e8c JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
39618ede JB |
7 | TaskStatisticsRequirements, |
8 | WorkerChoiceStrategyOptions | |
d35e5717 | 9 | } from './selection-strategies-types.js' |
feec6e8c JB |
10 | |
11 | /** | |
12 | * Selects the next worker with an interleaved weighted round robin scheduling algorithm. | |
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. | |
feec6e8c JB |
17 | */ |
18 | export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< | |
19 | Worker extends IWorker, | |
20 | Data = unknown, | |
21 | Response = unknown | |
22 | > | |
23 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
24 | implements IWorkerChoiceStrategy { | |
619f403b JB |
25 | /** @inheritDoc */ |
26 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { | |
27 | runTime: { | |
28 | aggregate: true, | |
29 | average: true, | |
30 | median: false | |
31 | }, | |
32 | waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
33 | elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS | |
34 | } | |
35 | ||
feec6e8c | 36 | /** |
d33be430 | 37 | * Round id. |
feec6e8c | 38 | */ |
c63a35a0 | 39 | private roundId = 0 |
feec6e8c JB |
40 | /** |
41 | * Round weights. | |
42 | */ | |
b2365b8a | 43 | private roundWeights: number[] |
feec6e8c | 44 | /** |
619f403b | 45 | * Worker node id. |
feec6e8c | 46 | */ |
c63a35a0 | 47 | private workerNodeId = 0 |
619f403b | 48 | /** |
f3a91bac | 49 | * Worker node virtual task runtime. |
619f403b | 50 | */ |
c63a35a0 | 51 | private workerNodeVirtualTaskRunTime = 0 |
feec6e8c JB |
52 | |
53 | /** @inheritDoc */ | |
54 | public constructor ( | |
55 | pool: IPool<Worker, Data, Response>, | |
39618ede | 56 | opts?: WorkerChoiceStrategyOptions |
feec6e8c JB |
57 | ) { |
58 | super(pool, opts) | |
3d6f0f73 JB |
59 | this.setTaskStatisticsRequirements(this.opts) |
60 | this.roundWeights = this.getRoundWeights() | |
feec6e8c JB |
61 | } |
62 | ||
63 | /** @inheritDoc */ | |
64 | public reset (): boolean { | |
39a43af7 | 65 | this.resetWorkerNodeKeyProperties() |
d33be430 | 66 | this.roundId = 0 |
619f403b | 67 | this.workerNodeId = 0 |
f3a91bac | 68 | this.workerNodeVirtualTaskRunTime = 0 |
feec6e8c JB |
69 | return true |
70 | } | |
71 | ||
72 | /** @inheritDoc */ | |
73 | public update (): boolean { | |
74 | return true | |
75 | } | |
76 | ||
77 | /** @inheritDoc */ | |
b1aae695 | 78 | public choose (): number | undefined { |
297f3bbe | 79 | for ( |
d33be430 | 80 | let roundIndex = this.roundId; |
d3127e84 JB |
81 | roundIndex < this.roundWeights.length; |
82 | roundIndex++ | |
297f3bbe | 83 | ) { |
619f403b | 84 | this.roundId = roundIndex |
feec6e8c | 85 | for ( |
619f403b | 86 | let workerNodeKey = this.workerNodeId; |
297f3bbe JB |
87 | workerNodeKey < this.pool.workerNodes.length; |
88 | workerNodeKey++ | |
feec6e8c | 89 | ) { |
619f403b | 90 | this.workerNodeId = workerNodeKey |
619f403b JB |
91 | if ( |
92 | this.workerNodeId !== this.nextWorkerNodeKey && | |
f3a91bac | 93 | this.workerNodeVirtualTaskRunTime !== 0 |
619f403b | 94 | ) { |
f3a91bac | 95 | this.workerNodeVirtualTaskRunTime = 0 |
619f403b | 96 | } |
67f3f2d6 | 97 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
7f0e1334 | 98 | const workerWeight = this.opts!.weights![workerNodeKey] |
619f403b | 99 | if ( |
97256a85 | 100 | this.isWorkerNodeReady(workerNodeKey) && |
619f403b | 101 | workerWeight >= this.roundWeights[roundIndex] && |
f3a91bac | 102 | this.workerNodeVirtualTaskRunTime < workerWeight |
619f403b | 103 | ) { |
f3a91bac JB |
104 | this.workerNodeVirtualTaskRunTime = |
105 | this.workerNodeVirtualTaskRunTime + | |
106 | this.getWorkerNodeTaskRunTime(workerNodeKey) | |
baca80f7 | 107 | this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) |
619f403b JB |
108 | this.nextWorkerNodeKey = workerNodeKey |
109 | return this.nextWorkerNodeKey | |
feec6e8c JB |
110 | } |
111 | } | |
297f3bbe | 112 | } |
619f403b JB |
113 | this.interleavedWeightedRoundRobinNextWorkerNodeId() |
114 | } | |
115 | ||
116 | private interleavedWeightedRoundRobinNextWorkerNodeId (): void { | |
8e8d9101 JB |
117 | if (this.pool.workerNodes.length === 0) { |
118 | this.workerNodeId = 0 | |
119 | } else if ( | |
a38b62f1 JB |
120 | this.roundId === this.roundWeights.length - 1 && |
121 | this.workerNodeId === this.pool.workerNodes.length - 1 | |
122 | ) { | |
123 | this.roundId = 0 | |
124 | this.workerNodeId = 0 | |
125 | } else if (this.workerNodeId === this.pool.workerNodes.length - 1) { | |
126 | this.roundId = this.roundId + 1 | |
127 | this.workerNodeId = 0 | |
128 | } else { | |
129 | this.workerNodeId = this.workerNodeId + 1 | |
130 | } | |
feec6e8c JB |
131 | } |
132 | ||
133 | /** @inheritDoc */ | |
134 | public remove (workerNodeKey: number): boolean { | |
226b02a3 | 135 | if (this.pool.workerNodes.length === 0) { |
153179f2 JB |
136 | this.resetWorkerNodeKeyProperties() |
137 | this.workerNodeId = 0 | |
138 | this.workerNodeVirtualTaskRunTime = 0 | |
139 | return true | |
226b02a3 JB |
140 | } |
141 | if ( | |
142 | this.workerNodeId === workerNodeKey && | |
143 | this.workerNodeId > this.pool.workerNodes.length - 1 | |
144 | ) { | |
145 | this.workerNodeId = this.pool.workerNodes.length - 1 | |
146 | } | |
147 | if ( | |
148 | this.previousWorkerNodeKey === workerNodeKey && | |
149 | this.previousWorkerNodeKey > this.pool.workerNodes.length - 1 | |
150 | ) { | |
151 | this.previousWorkerNodeKey = this.pool.workerNodes.length - 1 | |
feec6e8c JB |
152 | } |
153 | return true | |
154 | } | |
155 | ||
e4854a4e | 156 | /** @inheritDoc */ |
39618ede | 157 | public setOptions (opts: WorkerChoiceStrategyOptions | undefined): void { |
e4854a4e JB |
158 | super.setOptions(opts) |
159 | this.roundWeights = this.getRoundWeights() | |
160 | } | |
161 | ||
feec6e8c | 162 | private getRoundWeights (): number[] { |
feec6e8c JB |
163 | return [ |
164 | ...new Set( | |
67f3f2d6 | 165 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
39618ede | 166 | Object.values(this.opts!.weights!) |
feec6e8c | 167 | .slice() |
b808b625 | 168 | .sort((a, b) => a - b) |
feec6e8c JB |
169 | ) |
170 | ] | |
171 | } | |
172 | } |