From e9b4eaa56c6ac3a7b7fddbe2869dda794dfa5733 Mon Sep 17 00:00:00 2001 From: Yong Zhang Date: Mon, 13 Jan 2020 18:16:02 +0800 Subject: [PATCH 1/4] Fixes vertx can not receive the request body --- *Motivation* Vertx can not receive the request body so that the REST API can not work well if you have a request with body. *Modifications* - Add body handler for the vertx http service *Verify this change* - Pass the test `testHttpMethodsWithBody` --- .../http/service/NullHttpService.java | 3 ++ .../http/vertx/VertxHttpServer.java | 2 ++ .../http/vertx/TestVertxHttpServer.java | 29 ++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/service/NullHttpService.java b/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/service/NullHttpService.java index 2f93f34efd2..6d38a17af15 100644 --- a/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/service/NullHttpService.java +++ b/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/service/NullHttpService.java @@ -30,6 +30,9 @@ public class NullHttpService implements HttpEndpointService { @Override public HttpServiceResponse handle(HttpServiceRequest request) { + if (request.getBody() != null) { + return new HttpServiceResponse(request.getBody(), HttpServer.StatusCode.OK); + } return new HttpServiceResponse(CONTENT, HttpServer.StatusCode.OK); } } diff --git a/bookkeeper-http/vertx-http-server/src/main/java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java b/bookkeeper-http/vertx-http-server/src/main/java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java index 6d0bd63d7ac..68e2c3aa7be 100644 --- a/bookkeeper-http/vertx-http-server/src/main/java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java +++ b/bookkeeper-http/vertx-http-server/src/main/java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java @@ -30,6 +30,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; +import io.vertx.ext.web.handler.BodyHandler; import org.apache.bookkeeper.http.HttpRouter; import org.apache.bookkeeper.http.HttpServer; import org.apache.bookkeeper.http.HttpServiceProvider; @@ -66,6 +67,7 @@ public boolean startServer(int port) { CompletableFuture> future = new CompletableFuture<>(); VertxHttpHandlerFactory handlerFactory = new VertxHttpHandlerFactory(httpServiceProvider); Router router = Router.router(vertx); + router.route().handler(BodyHandler.create()); HttpRouter requestRouter = new HttpRouter(handlerFactory) { @Override public void bindHandler(String endpoint, VertxAbstractHandler handler) { diff --git a/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java b/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java index c6cc13301db..7148dd0adce 100644 --- a/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java +++ b/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java @@ -26,8 +26,10 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; import org.apache.bookkeeper.http.HttpRouter; import org.apache.bookkeeper.http.HttpServer; @@ -81,12 +83,37 @@ public void testHttpMethods() throws Exception { httpServer.stopServer(); } - // HTTP request + @Test + public void testHttpMethodsWithBody() throws IOException { + VertxHttpServer httpServer = new VertxHttpServer(); + HttpServiceProvider httpServiceProvider = NullHttpServiceProvider.getInstance(); + httpServer.initialize(httpServiceProvider); + assertTrue(httpServer.startServer(0)); + int port = httpServer.getListeningPort(); + String body = "{\"bookie_src\": \"localhost:3181\"}"; + HttpResponse httpResponse = send(getUrl(port, HttpRouter.DECOMMISSION), HttpServer.Method.PUT, body); + assertEquals(HttpServer.StatusCode.OK.getValue(), httpResponse.responseCode); + assertEquals(body, httpResponse.responseBody); + httpServer.stopServer(); + } + private HttpResponse send(String url, HttpServer.Method method) throws IOException { + return send(url, method, ""); + } + + // HTTP request + private HttpResponse send(String url, HttpServer.Method method, String body) throws IOException { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional, default is GET con.setRequestMethod(method.toString()); + if (body != "") { + con.setDoOutput(true); + con.setFixedLengthStreamingMode(body.length()); + OutputStream outputStream = con.getOutputStream();; + outputStream.write(body.getBytes(StandardCharsets.UTF_8)); + outputStream.flush(); + } int responseCode = con.getResponseCode(); StringBuilder response = new StringBuilder(); BufferedReader in = null; From 6d68332928a52e5e305a3f762187967946a5838b Mon Sep 17 00:00:00 2001 From: Yong Zhang Date: Tue, 14 Jan 2020 10:51:51 +0800 Subject: [PATCH 2/4] * Fix pr validation --- .../java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java | 2 +- .../org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bookkeeper-http/vertx-http-server/src/main/java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java b/bookkeeper-http/vertx-http-server/src/main/java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java index 68e2c3aa7be..31e11173e5d 100644 --- a/bookkeeper-http/vertx-http-server/src/main/java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java +++ b/bookkeeper-http/vertx-http-server/src/main/java/org/apache/bookkeeper/http/vertx/VertxHttpServer.java @@ -24,13 +24,13 @@ import io.vertx.core.AsyncResult; import io.vertx.core.Vertx; import io.vertx.ext.web.Router; +import io.vertx.ext.web.handler.BodyHandler; import java.io.IOException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; -import io.vertx.ext.web.handler.BodyHandler; import org.apache.bookkeeper.http.HttpRouter; import org.apache.bookkeeper.http.HttpServer; import org.apache.bookkeeper.http.HttpServiceProvider; diff --git a/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java b/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java index 7148dd0adce..79667f73915 100644 --- a/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java +++ b/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java @@ -110,7 +110,7 @@ private HttpResponse send(String url, HttpServer.Method method, String body) thr if (body != "") { con.setDoOutput(true); con.setFixedLengthStreamingMode(body.length()); - OutputStream outputStream = con.getOutputStream();; + OutputStream outputStream = con.getOutputStream(); outputStream.write(body.getBytes(StandardCharsets.UTF_8)); outputStream.flush(); } From b6649d5d763a1c1fa9ee334dd8e741f15af59e80 Mon Sep 17 00:00:00 2001 From: Yong Zhang Date: Tue, 28 Jan 2020 19:16:46 +0800 Subject: [PATCH 3/4] * Fix code style --- .../bookkeeper/discover/TestZkRegistrationManager.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/TestZkRegistrationManager.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/TestZkRegistrationManager.java index 8949e12bb08..52d882a68de 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/TestZkRegistrationManager.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/discover/TestZkRegistrationManager.java @@ -18,6 +18,8 @@ */ package org.apache.bookkeeper.discover; +import static org.junit.Assert.assertTrue; + import org.apache.bookkeeper.conf.ServerConfiguration; import org.apache.bookkeeper.conf.TestBKConfiguration; import org.apache.bookkeeper.test.ZooKeeperCluster; @@ -27,7 +29,6 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.assertTrue; /** @@ -55,9 +56,9 @@ public void testPrepareFormat () throws Exception{ ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setMetadataServiceUri("zk+hierarchical://localhost:2181/test/ledgers"); zkc = localZkServer.getZooKeeperClient(); - ZKRegistrationManager zkRegistrationManager = new ZKRegistrationManager(conf, zkc,() -> {} ); + ZKRegistrationManager zkRegistrationManager = new ZKRegistrationManager(conf, zkc, () -> {}); zkRegistrationManager.prepareFormat(); - assertTrue(zkc.exists("/test/ledgers",false) != null); + assertTrue(zkc.exists("/test/ledgers", false) != null); } finally { if (zkc != null) { zkc.close(); From 08cc93a31a2fdef57e23de0ac4c43fac24eb2e2e Mon Sep 17 00:00:00 2001 From: Yong Zhang Date: Tue, 28 Jan 2020 21:00:44 +0800 Subject: [PATCH 4/4] * Fix code style --- .../stream/storage/impl/cluster/ZkClusterInitializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stream/storage/impl/src/main/java/org/apache/bookkeeper/stream/storage/impl/cluster/ZkClusterInitializer.java b/stream/storage/impl/src/main/java/org/apache/bookkeeper/stream/storage/impl/cluster/ZkClusterInitializer.java index 081bd92c141..06ca7ab48c4 100644 --- a/stream/storage/impl/src/main/java/org/apache/bookkeeper/stream/storage/impl/cluster/ZkClusterInitializer.java +++ b/stream/storage/impl/src/main/java/org/apache/bookkeeper/stream/storage/impl/cluster/ZkClusterInitializer.java @@ -19,6 +19,7 @@ package org.apache.bookkeeper.stream.storage.impl.cluster; import static org.apache.bookkeeper.stream.storage.StorageConstants.ZK_METADATA_ROOT_PATH; +import static org.apache.bookkeeper.stream.storage.StorageConstants.getSegmentsRootPath; import com.google.common.base.Strings; import java.net.URI; @@ -32,7 +33,6 @@ import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.KeeperException; -import static org.apache.bookkeeper.stream.storage.StorageConstants.getSegmentsRootPath; /** * ZooKeeper Based Cluster Initializer.