You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by il...@apache.org on 2019/04/01 15:57:44 UTC

[ignite] branch master updated: IGNITE-11599 Thin client to find valid node for connection from the configuration list - Fixes #6368.

This is an automated email from the ASF dual-hosted git repository.

ilyak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new a1151a7  IGNITE-11599 Thin client to find valid node for connection from the configuration list - Fixes #6368.
a1151a7 is described below

commit a1151a7b1603ef17de01bfc7bc58e50361251b98
Author: shroman <rs...@yahoo.com>
AuthorDate: Mon Apr 1 18:56:09 2019 +0300

    IGNITE-11599 Thin client to find valid node for connection from the configuration list - Fixes #6368.
    
    Signed-off-by: Ilya Kasnacheev <il...@gmail.com>
---
 .../internal/client/thin/ReliableChannel.java      | 25 ++++++++--
 .../ignite/internal/util/HostAndPortRange.java     |  3 ++
 .../org/apache/ignite/client/ConnectionTest.java   | 56 ++++++++++++++++++++++
 .../org/apache/ignite/client/ClientTestSuite.java  |  3 +-
 4 files changed, 83 insertions(+), 4 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ReliableChannel.java b/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ReliableChannel.java
index c23bf84..06d93b1 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ReliableChannel.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ReliableChannel.java
@@ -38,6 +38,7 @@ import org.apache.ignite.configuration.ClientConnectorConfiguration;
 import org.apache.ignite.internal.binary.streams.BinaryInputStream;
 import org.apache.ignite.internal.binary.streams.BinaryOutputStream;
 import org.apache.ignite.internal.util.HostAndPortRange;
+import org.apache.ignite.internal.util.typedef.F;
 
 /**
  * Adds failover abd thread-safety to {@link ClientChannel}.
@@ -81,11 +82,26 @@ final class ReliableChannel implements AutoCloseable {
 
         primary = addrs.get(new Random().nextInt(addrs.size())); // we already verified there is at least one address
 
-        ch = chFactory.apply(new ClientChannelConfiguration(clientCfg).setAddress(primary)).get();
-
-        for (InetSocketAddress a : addrs)
+        for (InetSocketAddress a : addrs) {
             if (a != primary)
                 this.backups.add(a);
+        }
+
+        ClientConnectionException lastEx = null;
+
+        for (int i = 0; i < addrs.size(); i++) {
+            try {
+                ch = chFactory.apply(new ClientChannelConfiguration(clientCfg).setAddress(primary)).get();
+
+                return;
+            } catch (ClientConnectionException e) {
+                lastEx = e;
+
+                changeServer();
+            }
+        }
+
+        throw lastEx;
     }
 
     /** {@inheritDoc} */
@@ -173,6 +189,9 @@ final class ReliableChannel implements AutoCloseable {
      * @return host:port_range address lines parsed as {@link InetSocketAddress}.
      */
     private static List<InetSocketAddress> parseAddresses(String[] addrs) throws ClientException {
+        if (F.isEmpty(addrs))
+            throw new ClientException("Empty addresses");
+
         Collection<HostAndPortRange> ranges = new ArrayList<>(addrs.length);
 
         for (String a : addrs) {
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java b/modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
index 6ac652d..9bfca7f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
@@ -56,6 +56,9 @@ public class HostAndPortRange implements Serializable {
         int portFrom;
         int portTo;
 
+        if (F.isEmpty(addrStr))
+            throw createParseError(addrStr, errMsgPrefix, "Address is empty");
+
         final int colIdx = addrStr.indexOf(':');
 
         if (colIdx > 0) {
diff --git a/modules/core/src/test/java/org/apache/ignite/client/ConnectionTest.java b/modules/core/src/test/java/org/apache/ignite/client/ConnectionTest.java
new file mode 100644
index 0000000..4df1413
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/client/ConnectionTest.java
@@ -0,0 +1,56 @@
+package org.apache.ignite.client;
+
+import org.apache.ignite.Ignition;
+import org.apache.ignite.configuration.ClientConfiguration;
+import org.junit.Test;
+
+/**
+ * Checks if it can connect to a valid address from the node address list.
+ */
+public class ConnectionTest {
+    /** */
+    @Test(expected = org.apache.ignite.client.ClientException.class)
+    public void testEmptyNodeAddress() throws Exception {
+        testConnection("");
+    }
+
+    /** */
+    @Test(expected = org.apache.ignite.client.ClientException.class)
+    public void testNullNodeAddress() throws Exception {
+        testConnection(null);
+    }
+
+    /** */
+    @Test(expected = org.apache.ignite.client.ClientException.class)
+    public void testNullNodeAddresses() throws Exception {
+        testConnection(null, null);
+    }
+
+    /** */
+    @Test
+    public void testValidNodeAddresses() throws Exception {
+        testConnection(Config.SERVER);
+    }
+
+    /** */
+    @Test(expected = org.apache.ignite.client.ClientConnectionException.class)
+    public void testInvalidNodeAddresses() throws Exception {
+        testConnection("127.0.0.1:47500", "127.0.0.1:10801");
+    }
+
+    /** */
+    @Test
+    public void testValidInvalidNodeAddressesMix() throws Exception {
+        testConnection("127.0.0.1:47500", "127.0.0.1:10801", Config.SERVER);
+    }
+
+    /**
+     * @param addrs Addresses to connect.
+     */
+    private void testConnection(String... addrs) throws Exception {
+        try (LocalIgniteCluster cluster = LocalIgniteCluster.start(1);
+             IgniteClient client = Ignition.startClient(new ClientConfiguration()
+                     .setAddresses(addrs))) {
+        }
+    }
+}
diff --git a/modules/indexing/src/test/java/org/apache/ignite/client/ClientTestSuite.java b/modules/indexing/src/test/java/org/apache/ignite/client/ClientTestSuite.java
index 623a19e..b83a813 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/client/ClientTestSuite.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/client/ClientTestSuite.java
@@ -34,7 +34,8 @@ import org.junit.runners.Suite;
     SecurityTest.class,
     FunctionalQueryTest.class,
     IgniteBinaryQueryTest.class,
-    SslParametersTest.class
+    SslParametersTest.class,
+    ConnectionTest.class
 })
 public class ClientTestSuite {
     // No-op.