From 74d445124b9ef80f9a9e07741082c9bba7255c78 Mon Sep 17 00:00:00 2001 From: Nicole00 Date: Fri, 17 Dec 2021 14:18:57 +0800 Subject: [PATCH] support demain address --- .../client/meta/AbstractMetaClient.java | 12 +++++----- .../vesoft/nebula/client/meta/MetaClient.java | 15 ++++++++----- .../nebula/client/meta/MetaManager.java | 5 +++-- .../nebula/client/meta/TestMetaClient.java | 22 ++++++++++++++----- .../nebula/client/meta/TestMetaManager.java | 9 ++++++-- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/client/src/main/java/com/vesoft/nebula/client/meta/AbstractMetaClient.java b/client/src/main/java/com/vesoft/nebula/client/meta/AbstractMetaClient.java index c74e30ac3..f37daee44 100644 --- a/client/src/main/java/com/vesoft/nebula/client/meta/AbstractMetaClient.java +++ b/client/src/main/java/com/vesoft/nebula/client/meta/AbstractMetaClient.java @@ -10,6 +10,8 @@ import com.google.common.base.Preconditions; import com.google.common.net.InetAddresses; import com.vesoft.nebula.client.graph.data.HostAddress; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.List; public class AbstractMetaClient { @@ -22,16 +24,16 @@ public class AbstractMetaClient { protected TTransport transport; public AbstractMetaClient(List addresses, int timeout, - int connectionRetry, int executionRetry) { + int connectionRetry, int executionRetry) throws UnknownHostException { Preconditions.checkArgument(timeout > 0); - Preconditions.checkArgument(connectionRetry > 0); - Preconditions.checkArgument(executionRetry > 0); + Preconditions.checkArgument(connectionRetry >= 0); + Preconditions.checkArgument(executionRetry >= 0); for (HostAddress address : addresses) { - String host = address.getHost(); + String host = InetAddress.getByName(address.getHost()).getHostAddress(); int port = address.getPort(); // check if the address is a valid ip address or uri address and port is valid if ((!InetAddresses.isInetAddress(host) || !InetAddresses.isUriInetAddress(host)) - || (port <= 0 || port >= 65535)) { + || (port <= 0 || port >= 65535)) { throw new IllegalArgumentException(String.format("%s:%d is not a valid address", host, port)); } diff --git a/client/src/main/java/com/vesoft/nebula/client/meta/MetaClient.java b/client/src/main/java/com/vesoft/nebula/client/meta/MetaClient.java index a5cfa60b8..118d99416 100644 --- a/client/src/main/java/com/vesoft/nebula/client/meta/MetaClient.java +++ b/client/src/main/java/com/vesoft/nebula/client/meta/MetaClient.java @@ -48,6 +48,7 @@ import com.vesoft.nebula.meta.VerifyClientVersionResp; import com.vesoft.nebula.util.SslUtil; import java.io.IOException; +import java.net.UnknownHostException; import java.util.Arrays; import java.util.HashSet; import java.util.List; @@ -75,30 +76,32 @@ public class MetaClient extends AbstractMetaClient { private MetaService.Client client; private final List addresses; - public MetaClient(String host, int port) { + public MetaClient(String host, int port) throws UnknownHostException { this(new HostAddress(host, port)); } - public MetaClient(HostAddress address) { + public MetaClient(HostAddress address) throws UnknownHostException { this(Arrays.asList(address), DEFAULT_CONNECTION_RETRY_SIZE, DEFAULT_EXECUTION_RETRY_SIZE); } - public MetaClient(List addresses) { + public MetaClient(List addresses) throws UnknownHostException { this(addresses, DEFAULT_CONNECTION_RETRY_SIZE, DEFAULT_EXECUTION_RETRY_SIZE); } - public MetaClient(List addresses, int connectionRetry, int executionRetry) { + public MetaClient(List addresses, int connectionRetry, int executionRetry) + throws UnknownHostException { this(addresses, DEFAULT_TIMEOUT_MS, connectionRetry, executionRetry); } public MetaClient(List addresses, int timeout, int connectionRetry, - int executionRetry) { + int executionRetry) throws UnknownHostException { super(addresses, timeout, connectionRetry, executionRetry); this.addresses = addresses; } public MetaClient(List addresses, int timeout, int connectionRetry, - int executionRetry, boolean enableSSL, SSLParam sslParam) { + int executionRetry, boolean enableSSL, SSLParam sslParam) + throws UnknownHostException { super(addresses, timeout, connectionRetry, executionRetry); this.addresses = addresses; this.enableSSL = enableSSL; diff --git a/client/src/main/java/com/vesoft/nebula/client/meta/MetaManager.java b/client/src/main/java/com/vesoft/nebula/client/meta/MetaManager.java index 8416b0fd0..1c23ebb7b 100644 --- a/client/src/main/java/com/vesoft/nebula/client/meta/MetaManager.java +++ b/client/src/main/java/com/vesoft/nebula/client/meta/MetaManager.java @@ -16,6 +16,7 @@ import com.vesoft.nebula.meta.IdName; import com.vesoft.nebula.meta.SpaceItem; import com.vesoft.nebula.meta.TagItem; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -55,7 +56,7 @@ private class SpaceInfo { * init the meta info cache */ public MetaManager(List address) - throws TException, ClientServerIncompatibleException { + throws TException, ClientServerIncompatibleException, UnknownHostException { metaClient = new MetaClient(address); metaClient.connect(); fillMetaInfo(); @@ -66,7 +67,7 @@ public MetaManager(List address) */ public MetaManager(List address, int timeout, int connectionRetry, int executionRetry, boolean enableSSL, SSLParam sslParam) - throws TException, ClientServerIncompatibleException { + throws TException, ClientServerIncompatibleException, UnknownHostException { metaClient = new MetaClient(address, timeout, connectionRetry, executionRetry, enableSSL, sslParam); metaClient.connect(); diff --git a/client/src/test/java/com/vesoft/nebula/client/meta/TestMetaClient.java b/client/src/test/java/com/vesoft/nebula/client/meta/TestMetaClient.java index a805fcb96..69e3c5426 100644 --- a/client/src/test/java/com/vesoft/nebula/client/meta/TestMetaClient.java +++ b/client/src/test/java/com/vesoft/nebula/client/meta/TestMetaClient.java @@ -17,6 +17,7 @@ import com.vesoft.nebula.meta.IdName; import com.vesoft.nebula.meta.TagItem; import java.io.IOException; +import java.net.UnknownHostException; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; @@ -43,10 +44,10 @@ public void tearDown() throws Exception { } private void connect() { - metaClient = new MetaClient(address, port); try { + metaClient = new MetaClient(address, port); metaClient.connect(); - } catch (TException | ClientServerIncompatibleException e) { + } catch (TException | UnknownHostException | ClientServerIncompatibleException e) { e.printStackTrace(); assert (false); } @@ -54,10 +55,11 @@ private void connect() { public void testFailConnect() { int port = 1111; - MetaClient client = new MetaClient(address, port); + try { + MetaClient client = new MetaClient(address, port); client.connect(); - } catch (TException | ClientServerIncompatibleException e) { + } catch (TException | UnknownHostException | ClientServerIncompatibleException e) { assert (true); } } @@ -107,7 +109,11 @@ public void testGetPartsAlloc() { public void testListHosts() { if (metaClient == null) { - metaClient = new MetaClient(address, port); + try { + metaClient = new MetaClient(address, port); + } catch (UnknownHostException e) { + assert (false); + } } assert (metaClient.listHosts().size() == 3); } @@ -126,7 +132,11 @@ public void testListOnlineHosts() { assert (false); } if (metaClient == null) { - metaClient = new MetaClient(address, port); + try { + metaClient = new MetaClient(address, port); + } catch (UnknownHostException e) { + assert (false); + } } assert (metaClient.listHosts().size() == 2); diff --git a/client/src/test/java/com/vesoft/nebula/client/meta/TestMetaManager.java b/client/src/test/java/com/vesoft/nebula/client/meta/TestMetaManager.java index 0d6ceeb74..9124a65dd 100644 --- a/client/src/test/java/com/vesoft/nebula/client/meta/TestMetaManager.java +++ b/client/src/test/java/com/vesoft/nebula/client/meta/TestMetaManager.java @@ -16,6 +16,7 @@ import com.vesoft.nebula.meta.SpaceItem; import com.vesoft.nebula.meta.TagItem; import java.io.IOException; +import java.net.UnknownHostException; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -107,8 +108,12 @@ public void testGetSpaceParts() { public void testMultiVersionSchema() throws ClientServerIncompatibleException { MockNebulaGraph.createMultiVersionTagAndEdge(); metaManager.close(); - metaManager = new MetaManager( - Collections.singletonList(new HostAddress("127.0.0.1", 9559))); + try { + metaManager = new MetaManager( + Collections.singletonList(new HostAddress("127.0.0.1", 9559))); + } catch (UnknownHostException e) { + assert (false); + } TagItem tagItem = metaManager.getTag("testMeta", "player"); assert (tagItem.getVersion() == 1); assert (tagItem.schema.getColumns().size() == 1);