From c73a53e6c882cdd690ae5542fcf071768197176d Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Tue, 30 Apr 2019 00:21:26 +0800 Subject: [PATCH] Prevent misleading warning with HK2 MultiException WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected: MultiException stack 1 of 1 java.lang.IllegalStateException: Please wait for the server to initialize Change-Id: Ic683b982c95390fcb356074b9291fd664fcf4be2 improve: #490 --- .../hugegraph/api/filter/ExceptionFilter.java | 34 ++++++++++++------- .../baidu/hugegraph/core/GraphManager.java | 1 + .../hugegraph/server/ApplicationConfig.java | 8 +++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/ExceptionFilter.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/ExceptionFilter.java index 87eaff37e7..364754119a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/ExceptionFilter.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/ExceptionFilter.java @@ -32,6 +32,8 @@ import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; +import org.glassfish.hk2.api.MultiException; + import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; @@ -39,6 +41,13 @@ public class ExceptionFilter { + private static final int BAD_REQUEST_ERROR = + Response.Status.BAD_REQUEST.getStatusCode(); + private static final int NOT_FOUND_ERROR = + Response.Status.NOT_FOUND.getStatusCode(); + private static final int INTERNAL_SERVER_ERROR = + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); + public static class TracedExceptionMapper { @Context @@ -59,7 +68,7 @@ public static class HugeExceptionMapper @Override public Response toResponse(HugeException exception) { - return Response.status(400) + return Response.status(BAD_REQUEST_ERROR) .type(MediaType.APPLICATION_JSON) .entity(formatException(exception)) .build(); @@ -72,7 +81,7 @@ public static class IllegalArgumentExceptionMapper @Override public Response toResponse(IllegalArgumentException exception) { - return Response.status(400) + return Response.status(BAD_REQUEST_ERROR) .type(MediaType.APPLICATION_JSON) .entity(formatException(exception)) .build(); @@ -85,7 +94,7 @@ public static class NotFoundExceptionMapper @Override public Response toResponse(NotFoundException exception) { - return Response.status(404) + return Response.status(NOT_FOUND_ERROR) .type(MediaType.APPLICATION_JSON) .entity(formatException(exception)) .build(); @@ -98,7 +107,7 @@ public static class NoSuchElementExceptionMapper @Override public Response toResponse(NoSuchElementException exception) { - return Response.status(404) + return Response.status(NOT_FOUND_ERROR) .type(MediaType.APPLICATION_JSON) .entity(formatException(exception)) .build(); @@ -110,9 +119,6 @@ public static class WebApplicationExceptionMapper extends TracedExceptionMapper implements ExceptionMapper { - private static final int INTERNAL_SERVER_ERROR = - Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); - @Override public Response toResponse(WebApplicationException exception) { Response response = exception.getResponse(); @@ -136,22 +142,26 @@ private boolean trace(int status) { @Provider public static class UnknownExceptionMapper extends TracedExceptionMapper - implements ExceptionMapper { + implements ExceptionMapper { @Override - public Response toResponse(Exception exception) { - return Response.status(500) + public Response toResponse(Throwable exception) { + if (exception instanceof MultiException && + ((MultiException) exception).getErrors().size() == 1) { + exception = ((MultiException) exception).getErrors().get(0); + } + return Response.status(INTERNAL_SERVER_ERROR) .type(MediaType.APPLICATION_JSON) .entity(formatException(exception, this.trace())) .build(); } } - public static String formatException(Exception exception) { + public static String formatException(Throwable exception) { return formatException(exception, false); } - public static String formatException(Exception exception, boolean trace) { + public static String formatException(Throwable exception, boolean trace) { String clazz = exception.getClass().toString(); String msg = exception.getMessage() != null ? exception.getMessage() : ""; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 6be12c6663..5d52b8458b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -207,6 +207,7 @@ private void restoreUncompletedTasks() { for (String graph : this.graphs()) { HugeGraph hugegraph = this.graph(graph); assert hugegraph != null; + LOG.info("Restoring incomplete tasks for graph '{}'...", graph); hugegraph.taskScheduler().restoreTasks(); } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java index 77510c413c..4ea8246418 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/ApplicationConfig.java @@ -23,6 +23,7 @@ import org.apache.tinkerpop.gremlin.server.util.MetricManager; import org.glassfish.hk2.api.Factory; +import org.glassfish.hk2.api.MultiException; import org.glassfish.hk2.utilities.binding.AbstractBinder; import org.glassfish.jersey.process.internal.RequestScoped; import org.glassfish.jersey.server.ResourceConfig; @@ -32,6 +33,7 @@ import org.glassfish.jersey.server.monitoring.RequestEvent; import org.glassfish.jersey.server.monitoring.RequestEventListener; +import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.core.WorkLoad; @@ -121,8 +123,10 @@ protected void configure() { @Override public GraphManager provide() { - E.checkState(this.manager != null, - "Please wait for the server to initialize"); + if (this.manager == null) { + String message = "Please wait for the server to initialize"; + throw new MultiException(new HugeException(message), false); + } return this.manager; }