From 8046ac806df35b959c161ced18c3bb44e9ba11ff Mon Sep 17 00:00:00 2001 From: chengyouling Date: Wed, 6 Dec 2023 11:11:20 +0800 Subject: [PATCH 1/5] [#4074] optimize cors model init failed unclear prompt problem. --- .../servicecomb/foundation/vertx/VertxUtils.java | 11 ++++++----- .../servicecomb/transport/highway/HighwayClient.java | 6 +++++- .../transport/highway/HighwayTransport.java | 8 +++++++- .../transport/rest/vertx/VertxRestTransport.java | 9 ++++++++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java index 5cc68f13d8..951829ec01 100644 --- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java +++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.lang.management.ManagementFactory; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; @@ -85,16 +86,16 @@ public static DeploymentOptions createClientDeployOptions( } // deploy Verticle and wait for its success. do not call this method in event-loop thread - public static boolean blockDeploy(Vertx vertx, + public static Map blockDeploy(Vertx vertx, Class cls, DeploymentOptions options) throws InterruptedException { - Holder result = new Holder<>(); + Map result = new HashMap<>(); CountDownLatch latch = new CountDownLatch(1); vertx.deployVerticle(cls.getName(), options, ar -> { - result.value = ar.succeeded(); - + result.put("code", String.valueOf(ar.succeeded())); if (ar.failed()) { + result.put("message", ar.cause().getMessage()); LOGGER.error("deploy vertx failed, cause ", ar.cause()); } @@ -103,7 +104,7 @@ public static boolean blockDeploy(Vertx vertx, latch.await(); - return result.value; + return result; } public static Vertx getOrCreateVertxByName(String name, VertxOptions vertxOptions) { diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java index 3e0f98bf54..e79b4ab891 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java @@ -17,6 +17,7 @@ package org.apache.servicecomb.transport.highway; +import java.util.Map; import java.util.concurrent.TimeoutException; import javax.ws.rs.core.Response.Status; @@ -63,7 +64,10 @@ public void init(Vertx vertx) throws Exception { DeploymentOptions deployOptions = VertxUtils.createClientDeployOptions(clientMgr, HighwayConfig.getClientThreadCount()); - VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions); + Map result = VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions); + if (!Boolean.parseBoolean(result.get("code"))) { + throw new Exception(result.get("message")); + } } @VisibleForTesting diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java index b283b568c1..8c2607fac7 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java @@ -18,6 +18,7 @@ package org.apache.servicecomb.transport.highway; import java.util.Collections; +import java.util.Map; import org.apache.servicecomb.core.Const; import org.apache.servicecomb.core.Invocation; @@ -47,7 +48,12 @@ public boolean init() throws Exception { json.put(ENDPOINT_KEY, getEndpoint()); deployOptions.setConfig(json); deployOptions.setWorkerPoolName("pool-worker-transport-highway"); - return VertxUtils.blockDeploy(transportVertx, HighwayServerVerticle.class, deployOptions); + Map result = VertxUtils.blockDeploy(transportVertx, HighwayServerVerticle.class, deployOptions); + if (Boolean.parseBoolean(result.get("code"))) { + return true; + } else { + throw new Exception(result.get("message")); + } } @Override diff --git a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java index 3267bf28a5..07b5b8b9c5 100644 --- a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java +++ b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java @@ -19,6 +19,7 @@ import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.servicecomb.core.Const; import org.apache.servicecomb.core.Invocation; @@ -105,7 +106,13 @@ public boolean init() throws Exception { options.setWorkerPoolSize(VertxOptions.DEFAULT_WORKER_POOL_SIZE); prepareBlockResource(); - return VertxUtils.blockDeploy(transportVertx, TransportConfig.getRestServerVerticle(), options); + Map result = VertxUtils.blockDeploy(transportVertx, TransportConfig.getRestServerVerticle(), + options); + if (Boolean.parseBoolean(result.get("code"))) { + return true; + } else { + throw new Exception(result.get("message")); + } } private void prepareBlockResource() { From 3c16f85aa6380d683124f032291a1b74a4b7005d Mon Sep 17 00:00:00 2001 From: chengyouling Date: Wed, 6 Dec 2023 14:38:31 +0800 Subject: [PATCH 2/5] fixed operation review --- .../org/apache/servicecomb/foundation/vertx/VertxUtils.java | 6 +++--- .../apache/servicecomb/transport/highway/HighwayClient.java | 6 +++--- .../servicecomb/transport/highway/HighwayTransport.java | 6 +++--- .../transport/rest/vertx/VertxRestTransport.java | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java index 951829ec01..2a74e9b58d 100644 --- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java +++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java @@ -86,14 +86,14 @@ public static DeploymentOptions createClientDeployOptions( } // deploy Verticle and wait for its success. do not call this method in event-loop thread - public static Map blockDeploy(Vertx vertx, + public static Map blockDeploy(Vertx vertx, Class cls, DeploymentOptions options) throws InterruptedException { - Map result = new HashMap<>(); + Map result = new HashMap<>(); CountDownLatch latch = new CountDownLatch(1); vertx.deployVerticle(cls.getName(), options, ar -> { - result.put("code", String.valueOf(ar.succeeded())); + result.put("code", ar.succeeded()); if (ar.failed()) { result.put("message", ar.cause().getMessage()); LOGGER.error("deploy vertx failed, cause ", ar.cause()); diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java index e79b4ab891..3069d38834 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java @@ -64,9 +64,9 @@ public void init(Vertx vertx) throws Exception { DeploymentOptions deployOptions = VertxUtils.createClientDeployOptions(clientMgr, HighwayConfig.getClientThreadCount()); - Map result = VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions); - if (!Boolean.parseBoolean(result.get("code"))) { - throw new Exception(result.get("message")); + Map result = VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions); + if (!(boolean)result.get("code")) { + throw new IllegalStateException((String) result.get("message")); } } diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java index 8c2607fac7..48ff052813 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java @@ -48,11 +48,11 @@ public boolean init() throws Exception { json.put(ENDPOINT_KEY, getEndpoint()); deployOptions.setConfig(json); deployOptions.setWorkerPoolName("pool-worker-transport-highway"); - Map result = VertxUtils.blockDeploy(transportVertx, HighwayServerVerticle.class, deployOptions); - if (Boolean.parseBoolean(result.get("code"))) { + Map result = VertxUtils.blockDeploy(transportVertx, HighwayServerVerticle.class, deployOptions); + if ((boolean)result.get("code")) { return true; } else { - throw new Exception(result.get("message")); + throw new Exception((String) result.get("message")); } } diff --git a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java index 07b5b8b9c5..ff8f59a13b 100644 --- a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java +++ b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java @@ -106,12 +106,12 @@ public boolean init() throws Exception { options.setWorkerPoolSize(VertxOptions.DEFAULT_WORKER_POOL_SIZE); prepareBlockResource(); - Map result = VertxUtils.blockDeploy(transportVertx, TransportConfig.getRestServerVerticle(), + Map result = VertxUtils.blockDeploy(transportVertx, TransportConfig.getRestServerVerticle(), options); - if (Boolean.parseBoolean(result.get("code"))) { + if ((boolean)result.get("code")) { return true; } else { - throw new Exception(result.get("message")); + throw new Exception((String) result.get("message")); } } From b596c16ca2ade4ebfcf3ab009e9e29dcb798fe84 Mon Sep 17 00:00:00 2001 From: chengyouling Date: Wed, 6 Dec 2023 15:07:38 +0800 Subject: [PATCH 3/5] fixed exception class --- .../apache/servicecomb/transport/highway/HighwayTransport.java | 2 +- .../servicecomb/transport/rest/vertx/VertxRestTransport.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java index 48ff052813..d1487a9aae 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java @@ -52,7 +52,7 @@ public boolean init() throws Exception { if ((boolean)result.get("code")) { return true; } else { - throw new Exception((String) result.get("message")); + throw new IllegalStateException((String) result.get("message")); } } diff --git a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java index ff8f59a13b..86247f50e0 100644 --- a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java +++ b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java @@ -111,7 +111,7 @@ public boolean init() throws Exception { if ((boolean)result.get("code")) { return true; } else { - throw new Exception((String) result.get("message")); + throw new IllegalStateException((String) result.get("message")); } } From 4b92ac769d00e498dee661cf3dbcd0e21dbf40dc Mon Sep 17 00:00:00 2001 From: chengyouling Date: Wed, 6 Dec 2023 15:18:07 +0800 Subject: [PATCH 4/5] fixed checkstyle --- .../java/org/apache/servicecomb/foundation/vertx/VertxUtils.java | 1 - 1 file changed, 1 deletion(-) diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java index 2a74e9b58d..b9db0621af 100644 --- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java +++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java @@ -27,7 +27,6 @@ import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; -import org.apache.servicecomb.foundation.common.Holder; import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx; import org.apache.servicecomb.foundation.vertx.client.ClientPoolManager; import org.apache.servicecomb.foundation.vertx.client.ClientVerticle; From ce066f22de681e5166042a184a84922b996662e4 Mon Sep 17 00:00:00 2001 From: chengyouling Date: Wed, 6 Dec 2023 17:44:00 +0800 Subject: [PATCH 5/5] fixed ci check --- .../transport/highway/HighwayClient.java | 2 +- .../transport/highway/HighwayTransport.java | 2 +- .../transport/highway/TestHighwayClient.java | 15 +++++++++++---- .../transport/rest/vertx/VertxRestTransport.java | 2 +- .../rest/vertx/TestVertxRestTransport.java | 8 ++++++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java index 3069d38834..4afe98a1f2 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayClient.java @@ -65,7 +65,7 @@ public void init(Vertx vertx) throws Exception { DeploymentOptions deployOptions = VertxUtils.createClientDeployOptions(clientMgr, HighwayConfig.getClientThreadCount()); Map result = VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions); - if (!(boolean)result.get("code")) { + if (!(boolean) result.get("code")) { throw new IllegalStateException((String) result.get("message")); } } diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java index d1487a9aae..f40ff86b18 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayTransport.java @@ -49,7 +49,7 @@ public boolean init() throws Exception { deployOptions.setConfig(json); deployOptions.setWorkerPoolName("pool-worker-transport-highway"); Map result = VertxUtils.blockDeploy(transportVertx, HighwayServerVerticle.class, deployOptions); - if ((boolean)result.get("code")) { + if ((boolean) result.get("code")) { return true; } else { throw new IllegalStateException((String) result.get("message")); diff --git a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayClient.java b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayClient.java index ee3c63148c..43db614335 100644 --- a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayClient.java +++ b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayClient.java @@ -17,6 +17,9 @@ package org.apache.servicecomb.transport.highway; +import java.util.HashMap; +import java.util.Map; + import javax.ws.rs.core.Response.Status; import org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf; @@ -97,10 +100,12 @@ public void testLoginTimeout(@Mocked Vertx vertx) { public void testHighwayClientSSL(@Mocked Vertx vertx) throws Exception { new MockUp() { @Mock - boolean blockDeploy(Vertx vertx, + Map blockDeploy(Vertx vertx, Class cls, DeploymentOptions options) { - return true; + Map result = new HashMap<>(); + result.put("code", true); + return result; } }; @@ -114,10 +119,12 @@ private Object doTestSend(Vertx vertx, HighwayClientConnectionPool pool, Highway Object decodedResponse) throws Exception { new MockUp() { @Mock - boolean blockDeploy(Vertx vertx, + Map blockDeploy(Vertx vertx, Class cls, DeploymentOptions options) { - return true; + Map result = new HashMap<>(); + result.put("code", true); + return result; } }; diff --git a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java index 86247f50e0..267fd708f7 100644 --- a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java +++ b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/VertxRestTransport.java @@ -108,7 +108,7 @@ public boolean init() throws Exception { prepareBlockResource(); Map result = VertxUtils.blockDeploy(transportVertx, TransportConfig.getRestServerVerticle(), options); - if ((boolean)result.get("code")) { + if ((boolean) result.get("code")) { return true; } else { throw new IllegalStateException((String) result.get("message")); diff --git a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestTransport.java b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestTransport.java index dbe6cae4e2..9a5f43f6fe 100644 --- a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestTransport.java +++ b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestTransport.java @@ -19,6 +19,8 @@ import java.io.IOException; import java.net.ServerSocket; +import java.util.HashMap; +import java.util.Map; import org.apache.servicecomb.core.Endpoint; import org.apache.servicecomb.core.Invocation; @@ -62,9 +64,11 @@ public Vertx init(VertxOptions vertxOptions) { } @Mock - public boolean blockDeploy(Vertx vertx, Class cls, + public Map blockDeploy(Vertx vertx, Class cls, DeploymentOptions options) throws InterruptedException { - return true; + Map result = new HashMap<>(); + result.put("code", true); + return result; } }; instance.init();