This is an automated email from the ASF dual-hosted git repository. xiangying pushed a commit to branch branch-2.10 in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit b3f31acf2a3c4c0ccebdacb8db083753bb70bdb2 Author: Enrico Olivelli <[email protected]> AuthorDate: Tue Mar 29 11:39:17 2022 +0200 Pulsar Admin: grab contextual stacktrace for sync methods (#14620) (cherry picked from commit 1730415bece2d779cf2d978f043209b5888ed2f8) --- .../pulsar/client/admin/PulsarAdminException.java | 77 ++++++++++++++++++++++ .../pulsar/client/admin/internal/BaseResource.java | 9 ++- .../client/admin/internal/TopicPoliciesImpl.java | 15 +---- 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/pulsar-client-admin-api/src/main/java/org/apache/pulsar/client/admin/PulsarAdminException.java b/pulsar-client-admin-api/src/main/java/org/apache/pulsar/client/admin/PulsarAdminException.java index 041f8a659f4..6e0285d7d1e 100644 --- a/pulsar-client-admin-api/src/main/java/org/apache/pulsar/client/admin/PulsarAdminException.java +++ b/pulsar-client-admin-api/src/main/java/org/apache/pulsar/client/admin/PulsarAdminException.java @@ -69,6 +69,15 @@ public class PulsarAdminException extends Exception { return statusCode; } + /** + * This method is meant to be overriden by all subclasses. + * We cannot make it 'abstract' because it would be a breaking change in the public API. + * @return a new PulsarAdminException + */ + protected PulsarAdminException clone() { + return new PulsarAdminException(getMessage(), getCause(), httpError, statusCode); + } + /** * Not Authorized Exception. */ @@ -76,6 +85,11 @@ public class PulsarAdminException extends Exception { public NotAuthorizedException(Throwable t, String httpError, int statusCode) { super(httpError, t, httpError, statusCode); } + + @Override + protected PulsarAdminException clone() { + return new NotAuthorizedException(getCause(), getHttpError(), getStatusCode()); + } } /** @@ -85,6 +99,11 @@ public class PulsarAdminException extends Exception { public NotFoundException(Throwable t, String httpError, int statusCode) { super(httpError, t, httpError, statusCode); } + + @Override + protected PulsarAdminException clone() { + return new NotFoundException(getCause(), getHttpError(), getStatusCode()); + } } /** @@ -94,6 +113,11 @@ public class PulsarAdminException extends Exception { public NotAllowedException(Throwable t, String httpError, int statusCode) { super(httpError, t, httpError, statusCode); } + + @Override + protected PulsarAdminException clone() { + return new NotAllowedException(getCause(), getHttpError(), getStatusCode()); + } } /** @@ -103,6 +127,11 @@ public class PulsarAdminException extends Exception { public ConflictException(Throwable t, String httpError, int statusCode) { super(httpError, t, httpError, statusCode); } + + @Override + protected PulsarAdminException clone() { + return new ConflictException(getCause(), getHttpError(), getStatusCode()); + } } /** @@ -112,6 +141,11 @@ public class PulsarAdminException extends Exception { public PreconditionFailedException(Throwable t, String httpError, int statusCode) { super(httpError, t, httpError, statusCode); } + + @Override + protected PulsarAdminException clone() { + return new PreconditionFailedException(getCause(), getHttpError(), getStatusCode()); + } } /** @@ -121,6 +155,11 @@ public class PulsarAdminException extends Exception { public TimeoutException(Throwable t) { super(t); } + + @Override + protected PulsarAdminException clone() { + return new TimeoutException(getCause()); + } } /** @@ -131,9 +170,15 @@ public class PulsarAdminException extends Exception { super(message, t, httpError, statusCode); } + @Deprecated public ServerSideErrorException(Throwable t) { super("Some error occourred on the server", t); } + + @Override + protected PulsarAdminException clone() { + return new ServerSideErrorException(getCause(), getMessage(), getHttpError(), getStatusCode()); + } } /** @@ -147,6 +192,11 @@ public class PulsarAdminException extends Exception { public HttpErrorException(Throwable t) { super(t); } + + @Override + protected PulsarAdminException clone() { + return new HttpErrorException(getCause()); + } } /** @@ -160,6 +210,11 @@ public class PulsarAdminException extends Exception { public ConnectException(String message, Throwable t) { super(message, t); } + + @Override + protected PulsarAdminException clone() { + return new ConnectException(getMessage(), getCause()); + } } /** @@ -170,8 +225,30 @@ public class PulsarAdminException extends Exception { super(t); } + @Deprecated public GettingAuthenticationDataException(String msg) { super(msg); } + + @Override + protected PulsarAdminException clone() { + return new GettingAuthenticationDataException(getCause()); + } + } + + /** + * Clone the exception and grab the current stacktrace. + * @param e a PulsarAdminException + * @return a new PulsarAdminException, of the same class. + */ + public static PulsarAdminException wrap(PulsarAdminException e) { + PulsarAdminException cloned = e.clone(); + if (e.getClass() != cloned.getClass()) { + throw new IllegalStateException("Cloning a " + e.getClass() + " generated a " + + cloned.getClass() + ", this is a bug, original error is " + e, e); + } + // adding a reference to the original exception. + cloned.addSuppressed(e); + return (PulsarAdminException) cloned.fillInStackTrace(); } } diff --git a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java index 6df43c29b8f..4625c01fcef 100644 --- a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java +++ b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java @@ -290,15 +290,18 @@ public abstract class BaseResource { protected <T> T sync(Supplier<CompletableFuture<T>> executor) throws PulsarAdminException { try { return executor.get().get(this.readTimeoutMs, TimeUnit.MILLISECONDS); - } catch (ExecutionException e) { - throw (PulsarAdminException) e.getCause(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new PulsarAdminException(e); } catch (TimeoutException e) { throw new PulsarAdminException.TimeoutException(e); + } catch (ExecutionException e) { + // we want to have a stacktrace that points to this point, in order to return a meaninful + // stacktrace to the user, otherwise we will have a stacktrace + // related to another thread, because all Admin API calls are async + throw PulsarAdminException.wrap(getApiException(e.getCause())); } catch (Exception e) { - throw getApiException(e); + throw PulsarAdminException.wrap(getApiException(e)); } } } diff --git a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/TopicPoliciesImpl.java b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/TopicPoliciesImpl.java index 2caf1876f51..bd300377807 100644 --- a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/TopicPoliciesImpl.java +++ b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/TopicPoliciesImpl.java @@ -21,9 +21,6 @@ package org.apache.pulsar.client.admin.internal; import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; import javax.ws.rs.client.WebTarget; @@ -1237,17 +1234,7 @@ public class TopicPoliciesImpl extends BaseResource implements TopicPolicies { @Override public void removeSubscriptionTypesEnabled(String topic) throws PulsarAdminException { - try { - removeSubscriptionTypesEnabledAsync(topic) - .get(this.readTimeoutMs, TimeUnit.MILLISECONDS); - } catch (ExecutionException e) { - throw (PulsarAdminException) e.getCause(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new PulsarAdminException(e); - } catch (TimeoutException e) { - throw new PulsarAdminException.TimeoutException(e); - } + sync(() -> removeSubscriptionTypesEnabledAsync(topic)); } @Override
