Commit | Line | Data |
---|---|---|
feec6e8c JB |
1 | import type { IWorker } from '../worker' |
2 | import type { IPool } from '../pool' | |
26ce26ca | 3 | import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils' |
feec6e8c JB |
4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
5 | import type { | |
6 | IWorkerChoiceStrategy, | |
26ce26ca JB |
7 | InternalWorkerChoiceStrategyOptions, |
8 | TaskStatisticsRequirements | |
feec6e8c JB |
9 | } from './selection-strategies-types' |
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 | */ |
d33be430 | 39 | private roundId: number = 0 |
feec6e8c JB |
40 | /** |
41 | * Round weights. | |
42 | */ | |
00e1bdeb | 43 | private roundWeights!: number[] |
feec6e8c | 44 | /** |
619f403b | 45 | * Worker node id. |
feec6e8c | 46 | */ |
619f403b JB |
47 | private workerNodeId: number = 0 |
48 | /** | |
f3a91bac | 49 | * Worker node virtual task runtime. |
619f403b | 50 | */ |
f3a91bac | 51 | private workerNodeVirtualTaskRunTime: number = 0 |
feec6e8c JB |
52 | |
53 | /** @inheritDoc */ | |
54 | public constructor ( | |
55 | pool: IPool<Worker, Data, Response>, | |
26ce26ca | 56 | opts: InternalWorkerChoiceStrategyOptions |
feec6e8c JB |
57 | ) { |
58 | super(pool, opts) | |
00e1bdeb | 59 | this.setOptions(this.opts) |
feec6e8c JB |
60 | } |
61 | ||
62 | /** @inheritDoc */ | |
63 | public reset (): boolean { | |
39a43af7 | 64 | this.resetWorkerNodeKeyProperties() |
d33be430 | 65 | this.roundId = 0 |
619f403b | 66 | this.workerNodeId = 0 |
f3a91bac | 67 | this.workerNodeVirtualTaskRunTime = 0 |
feec6e8c JB |
68 | return true |
69 | } | |
70 | ||
71 | /** @inheritDoc */ | |
72 | public update (): boolean { | |
73 | return true | |
74 | } | |
75 | ||
76 | /** @inheritDoc */ | |
b1aae695 | 77 | public choose (): number | undefined { |
297f3bbe | 78 | for ( |
d33be430 | 79 | let roundIndex = this.roundId; |
d3127e84 JB |
80 | roundIndex < this.roundWeights.length; |
81 | roundIndex++ | |
297f3bbe | 82 | ) { |
619f403b | 83 | this.roundId = roundIndex |
feec6e8c | 84 | for ( |
619f403b | 85 | let workerNodeKey = this.workerNodeId; |
297f3bbe JB |
86 | workerNodeKey < this.pool.workerNodes.length; |
87 | workerNodeKey++ | |
feec6e8c | 88 | ) { |
619f403b | 89 | this.workerNodeId = workerNodeKey |
619f403b JB |
90 | if ( |
91 | this.workerNodeId !== this.nextWorkerNodeKey && | |
f3a91bac | 92 | this.workerNodeVirtualTaskRunTime !== 0 |
619f403b | 93 | ) { |
f3a91bac | 94 | this.workerNodeVirtualTaskRunTime = 0 |
619f403b | 95 | } |
00e1bdeb | 96 | const workerWeight = this.opts.weights?.[workerNodeKey] as number |
619f403b | 97 | if ( |
97256a85 | 98 | this.isWorkerNodeReady(workerNodeKey) && |
619f403b | 99 | workerWeight >= this.roundWeights[roundIndex] && |
f3a91bac | 100 | this.workerNodeVirtualTaskRunTime < workerWeight |
619f403b | 101 | ) { |
f3a91bac JB |
102 | this.workerNodeVirtualTaskRunTime = |
103 | this.workerNodeVirtualTaskRunTime + | |
104 | this.getWorkerNodeTaskRunTime(workerNodeKey) | |
baca80f7 | 105 | this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) |
619f403b JB |
106 | this.nextWorkerNodeKey = workerNodeKey |
107 | return this.nextWorkerNodeKey | |
feec6e8c JB |
108 | } |
109 | } | |
297f3bbe | 110 | } |
619f403b JB |
111 | this.interleavedWeightedRoundRobinNextWorkerNodeId() |
112 | } | |
113 | ||
114 | private interleavedWeightedRoundRobinNextWorkerNodeId (): void { | |
a38b62f1 JB |
115 | if ( |
116 | this.roundId === this.roundWeights.length - 1 && | |
117 | this.workerNodeId === this.pool.workerNodes.length - 1 | |
118 | ) { | |
119 | this.roundId = 0 | |
120 | this.workerNodeId = 0 | |
121 | } else if (this.workerNodeId === this.pool.workerNodes.length - 1) { | |
122 | this.roundId = this.roundId + 1 | |
123 | this.workerNodeId = 0 | |
124 | } else { | |
125 | this.workerNodeId = this.workerNodeId + 1 | |
126 | } | |
feec6e8c JB |
127 | } |
128 | ||
129 | /** @inheritDoc */ | |
130 | public remove (workerNodeKey: number): boolean { | |
226b02a3 JB |
131 | if (this.pool.workerNodes.length === 0) { |
132 | this.reset() | |
133 | } | |
134 | if ( | |
135 | this.workerNodeId === workerNodeKey && | |
136 | this.workerNodeId > this.pool.workerNodes.length - 1 | |
137 | ) { | |
138 | this.workerNodeId = this.pool.workerNodes.length - 1 | |
139 | } | |
140 | if ( | |
141 | this.previousWorkerNodeKey === workerNodeKey && | |
142 | this.previousWorkerNodeKey > this.pool.workerNodes.length - 1 | |
143 | ) { | |
144 | this.previousWorkerNodeKey = this.pool.workerNodes.length - 1 | |
feec6e8c JB |
145 | } |
146 | return true | |
147 | } | |
148 | ||
e4854a4e | 149 | /** @inheritDoc */ |
26ce26ca | 150 | public setOptions (opts: InternalWorkerChoiceStrategyOptions): void { |
e4854a4e JB |
151 | super.setOptions(opts) |
152 | this.roundWeights = this.getRoundWeights() | |
153 | } | |
154 | ||
feec6e8c | 155 | private getRoundWeights (): number[] { |
feec6e8c JB |
156 | return [ |
157 | ...new Set( | |
00e1bdeb | 158 | Object.values(this.opts.weights as Record<number, number>) |
feec6e8c | 159 | .slice() |
b808b625 | 160 | .sort((a, b) => a - b) |
feec6e8c JB |
161 | ) |
162 | ] | |
163 | } | |
164 | } |