68
68
import org .apache .pulsar .common .naming .NamespaceName ;
69
69
import org .apache .pulsar .common .naming .TopicDomain ;
70
70
import org .apache .pulsar .common .naming .TopicName ;
71
- import org .apache .pulsar .common .util .FutureUtil ;
72
71
import org .apache .pulsar .common .util .collections .ConcurrentOpenHashMap ;
73
72
import org .apache .pulsar .metadata .api .NotificationType ;
74
73
import org .apache .pulsar .metadata .api .coordination .LeaderElectionState ;
@@ -190,7 +189,10 @@ public ServiceUnitStateChannelImpl(PulsarService pulsar) {
190
189
}
191
190
192
191
public synchronized void start () throws PulsarServerException {
193
- validateChannelState (LeaderElectionServiceStarted , false );
192
+ if (!validateChannelState (LeaderElectionServiceStarted , false )) {
193
+ throw new IllegalStateException ("Invalid channel state:" + channelState .name ());
194
+ }
195
+
194
196
try {
195
197
leaderElectionService .start ();
196
198
this .channelState = LeaderElectionServiceStarted ;
@@ -269,15 +271,24 @@ public synchronized void close() throws PulsarServerException {
269
271
}
270
272
}
271
273
272
- private void validateChannelState (ChannelState targetState , boolean checkLowerIds ) {
274
+ private boolean validateChannelState (ChannelState targetState , boolean checkLowerIds ) {
273
275
int order = checkLowerIds ? -1 : 1 ;
274
276
if (Integer .compare (channelState .id , targetState .id ) * order > 0 ) {
275
- throw new IllegalStateException ( "Invalid channel state:" + channelState . name ()) ;
277
+ return false ;
276
278
}
279
+ return true ;
280
+ }
281
+
282
+ private boolean debug () {
283
+ return pulsar .getConfiguration ().isLoadBalancerDebugModeEnabled () || log .isDebugEnabled ();
277
284
}
278
285
279
286
public CompletableFuture <Optional <String >> getChannelOwnerAsync () {
280
- validateChannelState (LeaderElectionServiceStarted , true );
287
+ if (!validateChannelState (LeaderElectionServiceStarted , true )) {
288
+ return CompletableFuture .failedFuture (
289
+ new IllegalStateException ("Invalid channel state:" + channelState .name ()));
290
+ }
291
+
281
292
return leaderElectionService .readCurrentLeader ().thenApply (leader -> {
282
293
//expecting http://broker-xyz:port
283
294
// TODO: discard this protocol prefix removal
@@ -317,7 +328,11 @@ private boolean isChannelOwner() {
317
328
}
318
329
319
330
public CompletableFuture <Optional <String >> getOwnerAsync (String serviceUnit ) {
320
- validateChannelState (Started , true );
331
+ if (!validateChannelState (Started , true )) {
332
+ return CompletableFuture .failedFuture (
333
+ new IllegalStateException ("Invalid channel state:" + channelState .name ()));
334
+ }
335
+
321
336
ServiceUnitStateData data = tableview .get (serviceUnit );
322
337
ServiceUnitState state = data == null ? Free : data .state ();
323
338
ownerLookUpCounters .get (state ).incrementAndGet ();
@@ -329,81 +344,63 @@ public CompletableFuture<Optional<String>> getOwnerAsync(String serviceUnit) {
329
344
return deferGetOwnerRequest (serviceUnit ).thenApply (Optional ::of );
330
345
}
331
346
case Free -> {
332
- return CompletableFuture .completedFuture (null );
347
+ return CompletableFuture .completedFuture (Optional . empty () );
333
348
}
334
349
default -> {
335
350
String errorMsg = String .format ("Failed to process service unit state data: %s when get owner." , data );
336
351
log .error (errorMsg );
337
- return FutureUtil .failedFuture (new IllegalStateException (errorMsg ));
352
+ return CompletableFuture .failedFuture (new IllegalStateException (errorMsg ));
338
353
}
339
354
}
340
355
}
341
356
342
357
public CompletableFuture <String > publishAssignEventAsync (String serviceUnit , String broker ) {
343
358
EventType eventType = Assign ;
344
359
eventCounters .get (eventType ).getTotal ().incrementAndGet ();
345
- try {
346
- CompletableFuture <String > getOwnerRequest = deferGetOwnerRequest (serviceUnit );
347
- pubAsync (serviceUnit , new ServiceUnitStateData (Assigned , broker ))
348
- .whenComplete ((__ , ex ) -> {
349
- if (ex != null ) {
350
- getOwnerRequests .remove (serviceUnit , getOwnerRequest );
351
- if (!getOwnerRequest .isCompletedExceptionally ()) {
352
- getOwnerRequest .completeExceptionally (ex );
353
- }
354
- eventCounters .get (eventType ).getFailure ().incrementAndGet ();
360
+ CompletableFuture <String > getOwnerRequest = deferGetOwnerRequest (serviceUnit );
361
+ pubAsync (serviceUnit , new ServiceUnitStateData (Assigned , broker ))
362
+ .whenComplete ((__ , ex ) -> {
363
+ if (ex != null ) {
364
+ getOwnerRequests .remove (serviceUnit , getOwnerRequest );
365
+ if (!getOwnerRequest .isCompletedExceptionally ()) {
366
+ getOwnerRequest .completeExceptionally (ex );
355
367
}
356
- });
357
- return getOwnerRequest ;
358
- } catch (Throwable e ) {
359
- log .error ("Failed to publish assign event. serviceUnit:{}, broker:{}, assignPublishFailureCount:{}" ,
360
- serviceUnit , broker , eventCounters .get (eventType ).getFailure ().incrementAndGet (), e );
361
- throw e ;
362
- }
368
+ eventCounters .get (eventType ).getFailure ().incrementAndGet ();
369
+ }
370
+ });
371
+ return getOwnerRequest ;
363
372
}
364
373
365
374
public CompletableFuture <Void > publishUnloadEventAsync (Unload unload ) {
366
375
EventType eventType = Unload ;
367
376
eventCounters .get (eventType ).getTotal ().incrementAndGet ();
368
- try {
369
- String serviceUnit = unload .serviceUnit ();
370
- CompletableFuture <MessageId > future ;
371
- if (isTransferCommand (unload )) {
372
- ServiceUnitStateData next = new ServiceUnitStateData (Assigned ,
373
- unload .destBroker ().get (), unload .sourceBroker ());
374
- future = pubAsync (serviceUnit , next );
375
- } else {
376
- future = tombstoneAsync (serviceUnit );
377
- }
378
-
379
- return future .whenComplete ((__ , ex ) -> {
380
- if (ex != null ) {
381
- eventCounters .get (eventType ).getFailure ().incrementAndGet ();
382
- }
383
- }).thenApply (__ -> null );
384
- } catch (Throwable e ) {
385
- log .error ("Failed to publish unload event. unload:{}. unloadPublishFailureCount:{}" ,
386
- unload , eventCounters .get (eventType ).getFailure ().incrementAndGet (), e );
387
- throw e ;
377
+ String serviceUnit = unload .serviceUnit ();
378
+ CompletableFuture <MessageId > future ;
379
+ if (isTransferCommand (unload )) {
380
+ ServiceUnitStateData next = new ServiceUnitStateData (Assigned ,
381
+ unload .destBroker ().get (), unload .sourceBroker ());
382
+ future = pubAsync (serviceUnit , next );
383
+ } else {
384
+ future = tombstoneAsync (serviceUnit );
388
385
}
386
+
387
+ return future .whenComplete ((__ , ex ) -> {
388
+ if (ex != null ) {
389
+ eventCounters .get (eventType ).getFailure ().incrementAndGet ();
390
+ }
391
+ }).thenApply (__ -> null );
389
392
}
390
393
391
394
public CompletableFuture <Void > publishSplitEventAsync (Split split ) {
392
395
EventType eventType = Split ;
393
396
eventCounters .get (eventType ).getTotal ().incrementAndGet ();
394
- try {
395
- String serviceUnit = split .serviceUnit ();
396
- ServiceUnitStateData next = new ServiceUnitStateData (Splitting , split .sourceBroker ());
397
- return pubAsync (serviceUnit , next ).whenComplete ((__ , ex ) -> {
398
- if (ex != null ) {
399
- eventCounters .get (eventType ).getFailure ().incrementAndGet ();
400
- }
401
- }).thenApply (__ -> null );
402
- } catch (Throwable e ) {
403
- log .error ("Failed to publish split event. split:{}, splitPublishFailureCount:{}" ,
404
- split , eventCounters .get (eventType ).getFailure ().incrementAndGet (), e );
405
- throw e ;
406
- }
397
+ String serviceUnit = split .serviceUnit ();
398
+ ServiceUnitStateData next = new ServiceUnitStateData (Splitting , split .sourceBroker ());
399
+ return pubAsync (serviceUnit , next ).whenComplete ((__ , ex ) -> {
400
+ if (ex != null ) {
401
+ eventCounters .get (eventType ).getFailure ().incrementAndGet ();
402
+ }
403
+ }).thenApply (__ -> null );
407
404
}
408
405
409
406
private void handle (String serviceUnit , ServiceUnitStateData data ) {
@@ -424,8 +421,8 @@ private void handle(String serviceUnit, ServiceUnitStateData data) {
424
421
default -> throw new IllegalStateException ("Failed to handle channel data:" + data );
425
422
}
426
423
} catch (Throwable e ){
427
- getHandlerFailureCounter ( data ). incrementAndGet ();
428
- log . error ( "Failed to handle the event. serviceUnit:{} , data:{}" , serviceUnit , data , e );
424
+ log . error ( "Failed to handle the event. serviceUnit:{}, data:{}, handlerFailureCount:{}" ,
425
+ serviceUnit , data , getHandlerFailureCounter ( data ). incrementAndGet () , e );
429
426
throw e ;
430
427
}
431
428
}
@@ -455,7 +452,7 @@ private AtomicLong getHandlerFailureCounter(ServiceUnitStateData data) {
455
452
}
456
453
457
454
private AtomicLong getHandlerCounter (ServiceUnitStateData data , boolean total ) {
458
- var state = data . state () == null ? Free : data .state ();
455
+ var state = data == null ? Free : data .state ();
459
456
var counter = total
460
457
? handlerCounters .get (state ).getTotal () : handlerCounters .get (state ).getFailure ();
461
458
if (counter == null ) {
@@ -541,7 +538,10 @@ private void handleFreeEvent(String serviceUnit) {
541
538
}
542
539
543
540
private CompletableFuture <MessageId > pubAsync (String serviceUnit , ServiceUnitStateData data ) {
544
- validateChannelState (Started , true );
541
+ if (!validateChannelState (Started , true )) {
542
+ return CompletableFuture .failedFuture (
543
+ new IllegalStateException ("Invalid channel state:" + channelState .name ()));
544
+ }
545
545
CompletableFuture <MessageId > future = new CompletableFuture <>();
546
546
producer .newMessage ()
547
547
.key (serviceUnit )
0 commit comments