63
63
import java .util .stream .Collectors ;
64
64
import javax .naming .AuthenticationException ;
65
65
import javax .net .ssl .SSLSession ;
66
+ import javax .ws .rs .WebApplicationException ;
67
+ import javax .ws .rs .core .Response ;
66
68
import org .apache .bookkeeper .mledger .AsyncCallbacks ;
67
69
import org .apache .bookkeeper .mledger .Entry ;
68
70
import org .apache .bookkeeper .mledger .ManagedLedger ;
@@ -672,8 +674,6 @@ protected void handlePartitionMetadataRequest(CommandPartitionedTopicMetadata pa
672
674
commandSender .sendPartitionMetadataResponse (ServerError .AuthorizationError ,
673
675
ex .getMessage (), requestId );
674
676
} else {
675
- log .warn ("Failed to get Partitioned Metadata [{}] {}: {}" , remoteAddress ,
676
- topicName , ex .getMessage (), ex );
677
677
ServerError error = ServerError .ServiceNotReady ;
678
678
if (ex instanceof MetadataStoreException ) {
679
679
error = ServerError .MetadataError ;
@@ -685,6 +685,14 @@ protected void handlePartitionMetadataRequest(CommandPartitionedTopicMetadata pa
685
685
error = ServerError .MetadataError ;
686
686
}
687
687
}
688
+ if (error == ServerError .TopicNotFound ) {
689
+ log .info ("Trying to get Partitioned Metadata for a resource not exist"
690
+ + "[{}] {}: {}" , remoteAddress ,
691
+ topicName , ex .getMessage ());
692
+ } else {
693
+ log .warn ("Failed to get Partitioned Metadata [{}] {}: {}" ,
694
+ remoteAddress , topicName , ex .getMessage (), ex );
695
+ }
688
696
commandSender .sendPartitionMetadataResponse (error , ex .getMessage (),
689
697
requestId );
690
698
}
@@ -702,6 +710,16 @@ protected void handlePartitionMetadataRequest(CommandPartitionedTopicMetadata pa
702
710
return null ;
703
711
}).exceptionally (ex -> {
704
712
logAuthException (remoteAddress , "partition-metadata" , getPrincipal (), Optional .of (topicName ), ex );
713
+ Throwable actEx = FutureUtil .unwrapCompletionException (ex );
714
+ if (actEx instanceof WebApplicationException restException ) {
715
+ if (restException .getResponse ().getStatus () == Response .Status .NOT_FOUND .getStatusCode ()) {
716
+ writeAndFlush (Commands .newPartitionMetadataResponse (ServerError .MetadataError ,
717
+ "Tenant or namespace or topic does not exist: " + topicName .getNamespace () ,
718
+ requestId ));
719
+ lookupSemaphore .release ();
720
+ return null ;
721
+ }
722
+ }
705
723
final String msg = "Exception occurred while trying to authorize get Partition Metadata" ;
706
724
writeAndFlush (Commands .newPartitionMetadataResponse (ServerError .AuthorizationError , msg ,
707
725
requestId ));
@@ -3663,13 +3681,22 @@ protected void messageReceived() {
3663
3681
private static void logAuthException (SocketAddress remoteAddress , String operation ,
3664
3682
String principal , Optional <TopicName > topic , Throwable ex ) {
3665
3683
String topicString = topic .map (t -> ", topic=" + t .toString ()).orElse ("" );
3666
- if (ex instanceof AuthenticationException ) {
3684
+ Throwable actEx = FutureUtil .unwrapCompletionException (ex );
3685
+ if (actEx instanceof AuthenticationException ) {
3667
3686
log .info ("[{}] Failed to authenticate: operation={}, principal={}{}, reason={}" ,
3668
- remoteAddress , operation , principal , topicString , ex .getMessage ());
3669
- } else {
3670
- log .error ("[{}] Error trying to authenticate: operation={}, principal={}{}" ,
3671
- remoteAddress , operation , principal , topicString , ex );
3687
+ remoteAddress , operation , principal , topicString , actEx .getMessage ());
3688
+ return ;
3689
+ } else if (actEx instanceof WebApplicationException restException ){
3690
+ // Do not print error log if users tries to access a not found resource.
3691
+ if (restException .getResponse ().getStatus () == Response .Status .NOT_FOUND .getStatusCode ()) {
3692
+ log .info ("[{}] Trying to authenticate for a topic which under a namespace not exists: operation={},"
3693
+ + " principal={}{}, reason: {}" ,
3694
+ remoteAddress , operation , principal , topicString , actEx .getMessage ());
3695
+ return ;
3696
+ }
3672
3697
}
3698
+ log .error ("[{}] Error trying to authenticate: operation={}, principal={}{}" ,
3699
+ remoteAddress , operation , principal , topicString , ex );
3673
3700
}
3674
3701
3675
3702
private static void logNamespaceNameAuthException (SocketAddress remoteAddress , String operation ,
0 commit comments