You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2018/09/26 15:28:36 UTC
[2/2] ignite git commit: IGNITE-9704: JDBC: round-rodbin distribution
of connections between nodes for thin driver.
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/99723897
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/99723897
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/99723897
Branch: refs/heads/ignite-2.7
Commit: 99723897bc7445eeaadae206a0006c55bdaa1e14
Parents: 22696ed
Author: devozerov <vo...@gridgain.com>
Authored: Wed Sep 26 18:25:33 2018 +0300
Committer: devozerov <vo...@gridgain.com>
Committed: Wed Sep 26 18:28:12 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/99723897/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/99723897/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);
+ }
+ }
}