ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From voze...@apache.org
Subject [1/2] ignite git commit: IGNITE-9704: JDBC: round-rodbin distribution of connections between nodes for thin driver.
Date Wed, 26 Sep 2018 15:26:16 GMT
Repository: ignite
Updated Branches:
  refs/heads/master 9f546f878 -> c87dcaef0


IGNITE-9704: JDBC: round-rodbin distribution of connections between nodes for thin driver.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/99b1e2ae
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/99b1e2ae
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/99b1e2ae

Branch: refs/heads/master
Commit: 99b1e2aeb188cdd457b31a24f791a31d4a0abd3b
Parents: fe35a64
Author: devozerov <vozerov@gridgain.com>
Authored: Wed Sep 26 18:25:33 2018 +0300
Committer: devozerov <vozerov@gridgain.com>
Committed: Wed Sep 26 18:25:33 2018 +0300

----------------------------------------------------------------------
 ...JdbcThinConnectionMultipleAddressesTest.java |  2 +-
 .../internal/jdbc/thin/JdbcThinTcpIo.java       | 41 +++++++++++++++-----
 2 files changed, 33 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/99b1e2ae/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionMultipleAddressesTest.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionMultipleAddressesTest.java
b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionMultipleAddressesTest.java
index 12faed7..4f6651c 100644
--- a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionMultipleAddressesTest.java
+++ b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinConnectionMultipleAddressesTest.java
@@ -534,7 +534,7 @@ public class JdbcThinConnectionMultipleAddressesTest extends JdbcThinAbstractSel
         else {
             JdbcThinTcpIo io = GridTestUtils.getFieldValue(conn, "cliIo");
 
-            int idx = GridTestUtils.getFieldValue(io, "srvIdx");
+            int idx = io.serverIndex();
 
             stopGrid(idx);
         }

http://git-wip-us.apache.org/repos/asf/ignite/blob/99b1e2ae/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinTcpIo.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinTcpIo.java
b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinTcpIo.java
index 6155423..b065b7a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinTcpIo.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinTcpIo.java
@@ -27,7 +27,8 @@ import java.net.UnknownHostException;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Random;
+import java.util.concurrent.atomic.AtomicLong;
+
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.internal.binary.BinaryReaderExImpl;
 import org.apache.ignite.internal.binary.BinaryWriterExImpl;
@@ -95,7 +96,7 @@ public class JdbcThinTcpIo {
     private static final int QUERY_CLOSE_MSG_SIZE = 9;
 
     /** Random. */
-    private static final Random RND = new Random(U.currentTimeMillis());
+    private static final AtomicLong IDX_GEN = new AtomicLong();
 
     /** Connection properties. */
     private final ConnectionProperties connProps;
@@ -115,9 +116,6 @@ public class JdbcThinTcpIo {
     /** Ignite server version. */
     private IgniteProductVersion igniteVer;
 
-    /** Address index. */
-    private int srvIdx;
-
     /** Ignite server version. */
     private Thread ownThread;
 
@@ -127,6 +125,9 @@ public class JdbcThinTcpIo {
     /** Current protocol version used to connection to Ignite. */
     private ClientListenerProtocolVersion srvProtocolVer;
 
+    /** Server index. */
+    private volatile int srvIdx;
+
     /**
      * Constructor.
      *
@@ -134,9 +135,6 @@ public class JdbcThinTcpIo {
      */
     public JdbcThinTcpIo(ConnectionProperties connProps) {
         this.connProps = connProps;
-
-        // Try to connect to random address then round robin.
-        srvIdx = RND.nextInt(connProps.getAddresses().length);
     }
 
     /**
@@ -172,7 +170,9 @@ public class JdbcThinTcpIo {
 
             HostAndPortRange[] srvs = connProps.getAddresses();
 
-            for (int i = 0; i < srvs.length; i++, srvIdx = (srvIdx + 1) % srvs.length)
{
+            for (int i = 0; i < srvs.length; i++) {
+                srvIdx = nextServerIndex(srvs.length);
+
                 HostAndPortRange srv = srvs[srvIdx];
 
                 InetAddress[] addrs = getAllAddressesByHost(srv.host());
@@ -652,4 +652,27 @@ public class JdbcThinTcpIo {
 
         return srvProtocolVer.compareTo(VER_2_5_0) >= 0;
     }
+
+    /**
+     * @return Current server index.
+     */
+    public int serverIndex() {
+        return srvIdx;
+    }
+
+    /**
+     * Get next server index.
+     *
+     * @param len Number of servers.
+     * @return Index of the next server to connect to.
+     */
+    private static int nextServerIndex(int len) {
+        if (len == 1)
+            return 0;
+        else {
+            long nextIdx = IDX_GEN.getAndIncrement();
+
+            return (int)(nextIdx % len);
+        }
+    }
 }


Mime
View raw message