23
23
import static org .mockito .ArgumentMatchers .eq ;
24
24
import static org .mockito .Mockito .doReturn ;
25
25
import static org .mockito .Mockito .mock ;
26
+ import static org .mockito .Mockito .spy ;
26
27
import static org .mockito .Mockito .times ;
27
28
import static org .mockito .Mockito .verify ;
28
29
import static org .testng .Assert .assertEquals ;
29
30
import java .util .concurrent .CompletableFuture ;
30
31
import java .util .concurrent .Executors ;
32
+ import java .util .concurrent .ScheduledExecutorService ;
31
33
import java .util .concurrent .TimeUnit ;
32
34
import org .apache .commons .lang .reflect .FieldUtils ;
33
35
import org .apache .pulsar .broker .PulsarService ;
40
42
import org .apache .pulsar .broker .service .BrokerService ;
41
43
import org .apache .pulsar .broker .service .PulsarStats ;
42
44
import org .apache .pulsar .broker .stats .BrokerStats ;
45
+ import org .apache .pulsar .client .util .ExecutorProvider ;
43
46
import org .apache .pulsar .policies .data .loadbalancer .ResourceUsage ;
44
47
import org .apache .pulsar .policies .data .loadbalancer .SystemResourceUsage ;
45
48
import org .mockito .MockedStatic ;
46
49
import org .mockito .Mockito ;
47
50
import org .testcontainers .shaded .org .awaitility .Awaitility ;
51
+ import org .testng .annotations .AfterMethod ;
48
52
import org .testng .annotations .BeforeMethod ;
49
53
import org .testng .annotations .Test ;
50
54
@@ -59,17 +63,22 @@ public class BrokerLoadDataReporterTest {
59
63
SystemResourceUsage usage ;
60
64
String broker = "broker1" ;
61
65
String bundle = "bundle1" ;
66
+ ScheduledExecutorService executor ;
62
67
63
68
@ BeforeMethod
64
69
void setup () {
65
70
config = new ServiceConfiguration ();
71
+ config .setLoadBalancerDebugModeEnabled (true );
66
72
pulsar = mock (PulsarService .class );
67
73
store = mock (LoadDataStore .class );
68
74
brokerService = mock (BrokerService .class );
69
75
pulsarStats = mock (PulsarStats .class );
70
76
doReturn (brokerService ).when (pulsar ).getBrokerService ();
71
77
doReturn (config ).when (pulsar ).getConfiguration ();
72
- doReturn (Executors .newSingleThreadScheduledExecutor ()).when (pulsar ).getLoadManagerExecutor ();
78
+ executor = Executors
79
+ .newSingleThreadScheduledExecutor (new
80
+ ExecutorProvider .ExtendedThreadFactory ("pulsar-load-manager" ));
81
+ doReturn (executor ).when (pulsar ).getLoadManagerExecutor ();
73
82
doReturn (pulsarStats ).when (brokerService ).getPulsarStats ();
74
83
brokerStats = new BrokerStats (0 );
75
84
brokerStats .topics = 6 ;
@@ -81,6 +90,7 @@ void setup() {
81
90
doReturn (pulsarStats ).when (brokerService ).getPulsarStats ();
82
91
doReturn (brokerStats ).when (pulsarStats ).getBrokerStats ();
83
92
doReturn (CompletableFuture .completedFuture (null )).when (store ).pushAsync (any (), any ());
93
+ doReturn (CompletableFuture .completedFuture (null )).when (store ).removeAsync (any ());
84
94
85
95
usage = new SystemResourceUsage ();
86
96
usage .setCpu (new ResourceUsage (1.0 , 100.0 ));
@@ -90,6 +100,11 @@ void setup() {
90
100
usage .setBandwidthOut (new ResourceUsage (4.0 , 100.0 ));
91
101
}
92
102
103
+ @ AfterMethod
104
+ void shutdown (){
105
+ executor .shutdown ();
106
+ }
107
+
93
108
public void testGenerate () throws IllegalAccessException {
94
109
try (MockedStatic <LoadManagerShared > mockLoadManagerShared = Mockito .mockStatic (LoadManagerShared .class )) {
95
110
mockLoadManagerShared .when (() -> LoadManagerShared .getSystemResourceUsage (any ())).thenReturn (usage );
@@ -132,47 +147,54 @@ public void testReport() throws IllegalAccessException {
132
147
}
133
148
134
149
@ Test
135
- public void testTombstone () throws IllegalAccessException {
150
+ public void testTombstone () throws IllegalAccessException , InterruptedException {
136
151
137
- var target = new BrokerLoadDataReporter (pulsar , broker , store );
152
+ var target = spy ( new BrokerLoadDataReporter (pulsar , broker , store ) );
138
153
139
154
target .handleEvent (bundle ,
140
155
new ServiceUnitStateData (ServiceUnitState .Assigning , broker , VERSION_ID_INIT ), null );
141
156
verify (store , times (0 )).removeAsync (eq (broker ));
157
+ verify (target , times (0 )).tombstone ();
142
158
143
159
target .handleEvent (bundle ,
144
160
new ServiceUnitStateData (ServiceUnitState .Deleted , broker , VERSION_ID_INIT ), null );
145
161
verify (store , times (0 )).removeAsync (eq (broker ));
162
+ verify (target , times (0 )).tombstone ();
146
163
147
164
148
165
target .handleEvent (bundle ,
149
166
new ServiceUnitStateData (ServiceUnitState .Init , broker , VERSION_ID_INIT ), null );
150
167
verify (store , times (0 )).removeAsync (eq (broker ));
168
+ verify (target , times (0 )).tombstone ();
151
169
152
170
target .handleEvent (bundle ,
153
171
new ServiceUnitStateData (ServiceUnitState .Free , broker , VERSION_ID_INIT ), null );
154
172
verify (store , times (0 )).removeAsync (eq (broker ));
173
+ verify (target , times (0 )).tombstone ();
155
174
156
175
target .handleEvent (bundle ,
157
176
new ServiceUnitStateData (ServiceUnitState .Releasing , "broker-2" , broker , VERSION_ID_INIT ), null );
158
177
Awaitility .waitAtMost (3 , TimeUnit .SECONDS ).untilAsserted (() -> {
178
+ verify (target , times (1 )).tombstone ();
159
179
verify (store , times (1 )).removeAsync (eq (broker ));
160
180
var localData = (BrokerLoadData ) FieldUtils .readDeclaredField (target , "localData" , true );
161
181
assertEquals (localData , new BrokerLoadData ());
162
182
});
163
183
164
- {
165
- target .handleEvent (bundle ,
166
- new ServiceUnitStateData (ServiceUnitState .Releasing , "broker-2" , broker , VERSION_ID_INIT ), null );
184
+ target .handleEvent (bundle ,
185
+ new ServiceUnitStateData (ServiceUnitState .Releasing , "broker-2" , broker , VERSION_ID_INIT ), null );
186
+ Awaitility .waitAtMost (3 , TimeUnit .SECONDS ).untilAsserted (() -> {
187
+ verify (target , times (2 )).tombstone ();
167
188
verify (store , times (1 )).removeAsync (eq (broker ));
168
189
var localData = (BrokerLoadData ) FieldUtils .readDeclaredField (target , "localData" , true );
169
190
assertEquals (localData , new BrokerLoadData ());
170
- }
191
+ });
171
192
172
193
FieldUtils .writeDeclaredField (target , "tombstoneDelayInMillis" , 0 , true );
173
194
target .handleEvent (bundle ,
174
195
new ServiceUnitStateData (ServiceUnitState .Splitting , "broker-2" , broker , VERSION_ID_INIT ), null );
175
196
Awaitility .waitAtMost (3 , TimeUnit .SECONDS ).untilAsserted (() -> {
197
+ verify (target , times (3 )).tombstone ();
176
198
verify (store , times (2 )).removeAsync (eq (broker ));
177
199
var localData = (BrokerLoadData ) FieldUtils .readDeclaredField (target , "localData" , true );
178
200
assertEquals (localData , new BrokerLoadData ());
@@ -181,10 +203,10 @@ public void testTombstone() throws IllegalAccessException {
181
203
target .handleEvent (bundle ,
182
204
new ServiceUnitStateData (ServiceUnitState .Owned , broker , VERSION_ID_INIT ), null );
183
205
Awaitility .waitAtMost (3 , TimeUnit .SECONDS ).untilAsserted (() -> {
206
+ verify (target , times (4 )).tombstone ();
184
207
verify (store , times (3 )).removeAsync (eq (broker ));
185
208
var localData = (BrokerLoadData ) FieldUtils .readDeclaredField (target , "localData" , true );
186
209
assertEquals (localData , new BrokerLoadData ());
187
210
});
188
-
189
211
}
190
212
}
0 commit comments