This is an automated email from the ASF dual-hosted git repository.

zike pushed a commit to branch branch-3.3
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit ed03095579a1d11a9d866aa73ba0a5538f79c159
Author: Zike Yang <[email protected]>
AuthorDate: Thu Feb 6 17:56:57 2025 +0800

    [improve][broker] Do not print error logs for NotFound or Conflict errors 
when using the Admin API (#23928)
    
    ### Motivation
    
    Currently, when there is a 404 or 409 error in the Admin API call, the 
broker prints the error logs.
    
    ```
    ERROR org.apache.pulsar.broker.admin.v2.PersistentTopics - [xxx] Failed to 
get partitioned metadata topic persistent://xxx: Namespace not found
    ```
    
    ```
    ERROR org.apache.pulsar.broker.admin.v2.Namespaces - Failed to get policies 
for namespace xxx: Namespace does not exist
    ```
    
    ```
    ERROR org.apache.pulsar.broker.admin.v2.PersistentTopics - [xxx] Failed to 
create non-partitioned topic persistent:/xxx: This topic already exists
    ```
    
    ```
     [pulsar-web-44-1] ERROR org.apache.pulsar.broker.admin.AdminResource - 
[admin] Failed to create partitioned topic persistent://xxx
    
    java.util.concurrent.CompletionException: 
org.apache.pulsar.broker.web.RestException: This topic already exists
    ```
    
    These errors are related to the client side. The client can handle the 
error, so we don't need to print it in the broker log.
    
    ### Modifications
    
    - Print a warning log for NotFound or Conflict errors in the Admin API.
    
    (cherry picked from commit 99dc74b0e268e549384ef2099f17c773101a79bb)
---
 .../org/apache/pulsar/broker/admin/AdminResource.java  | 18 +++++++++++++++++-
 .../org/apache/pulsar/broker/admin/v2/Namespaces.java  |  7 ++++++-
 .../pulsar/broker/admin/v2/PersistentTopics.java       |  4 ++--
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java
index 8754a005080..d2dc5eb787b 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java
@@ -617,7 +617,12 @@ public abstract class AdminResource extends 
PulsarWebResource {
                     asyncResponse.resume(Response.noContent().build());
                 })
                 .exceptionally(ex -> {
-                    log.error("[{}] Failed to create partitioned topic {}", 
clientAppId(), topicName, ex);
+                    if (AdminResource.isConflictException(ex)) {
+                        log.info("[{}] Failed to create partitioned topic {}: 
{}", clientAppId(), topicName,
+                                ex.getMessage());
+                    } else {
+                        log.error("[{}] Failed to create partitioned topic 
{}", clientAppId(), topicName, ex);
+                    }
                     resumeAsyncResponseExceptionally(asyncResponse, ex);
                     return null;
                 });
@@ -881,6 +886,10 @@ public abstract class AdminResource extends 
PulsarWebResource {
                 == Status.TEMPORARY_REDIRECT.getStatusCode();
     }
 
+    protected static boolean isNotFoundOrConflictException(Throwable ex) {
+        return isNotFoundException(ex) || isConflictException(ex);
+    }
+
     protected static boolean isNotFoundException(Throwable ex) {
         Throwable realCause = FutureUtil.unwrapCompletionException(ex);
         return realCause instanceof WebApplicationException
@@ -888,6 +897,13 @@ public abstract class AdminResource extends 
PulsarWebResource {
                 == Status.NOT_FOUND.getStatusCode();
     }
 
+    protected static boolean isConflictException(Throwable ex) {
+        Throwable realCause = FutureUtil.unwrapCompletionException(ex);
+        return realCause instanceof WebApplicationException
+                && ((WebApplicationException) 
realCause).getResponse().getStatus()
+                == Status.CONFLICT.getStatusCode();
+    }
+
     protected static boolean isNot307And404Exception(Throwable ex) {
         return !isRedirectException(ex) && !isNotFoundException(ex);
     }
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/Namespaces.java 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/Namespaces.java
index 36150ee21b3..08c99085f80 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/Namespaces.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/Namespaces.java
@@ -47,6 +47,7 @@ import javax.ws.rs.container.Suspended;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.StreamingOutput;
+import org.apache.pulsar.broker.admin.AdminResource;
 import org.apache.pulsar.broker.admin.impl.NamespacesBase;
 import org.apache.pulsar.broker.admin.impl.OffloaderObjectsScannerUtils;
 import org.apache.pulsar.broker.web.RestException;
@@ -154,7 +155,11 @@ public class Namespaces extends NamespacesBase {
                 .thenCompose(__ -> getNamespacePoliciesAsync(namespaceName))
                 .thenAccept(response::resume)
                 .exceptionally(ex -> {
-                    log.error("Failed to get policies for namespace {}", 
namespaceName, ex);
+                    if (AdminResource.isNotFoundOrConflictException(ex)) {
+                        log.info("Failed to get policies for namespace {}: 
{}", namespaceName, ex.getMessage());
+                    } else {
+                        log.error("Failed to get policies for namespace {}", 
namespaceName, ex);
+                    }
                     resumeAsyncResponseExceptionally(response, ex);
                     return null;
                 });
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
index d0fe3c7c745..ec19c4928b3 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/PersistentTopics.java
@@ -346,7 +346,7 @@ public class PersistentTopics extends PersistentTopicsBase {
         internalCreateNonPartitionedTopicAsync(authoritative, properties)
                 .thenAccept(__ -> 
asyncResponse.resume(Response.noContent().build()))
                 .exceptionally(ex -> {
-                    if (isNot307And404Exception(ex)) {
+                    if (isNot307And404Exception(ex) && 
!isConflictException(ex)) {
                         log.error("[{}] Failed to create non-partitioned topic 
{}", clientAppId(), topicName, ex);
                     }
                     resumeAsyncResponseExceptionally(asyncResponse, ex);
@@ -946,7 +946,7 @@ public class PersistentTopics extends PersistentTopicsBase {
                     Throwable t = FutureUtil.unwrapCompletionException(ex);
                     if (!isRedirectException(t)) {
                         if (AdminResource.isNotFoundException(t)) {
-                            log.error("[{}] Failed to get partitioned metadata 
topic {}: {}",
+                            log.info("[{}] Failed to get partitioned metadata 
topic {}: {}",
                                     clientAppId(), topicName, ex.getMessage());
                         } else {
                             log.error("[{}] Failed to get partitioned metadata 
topic {}",

Reply via email to