Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-395] Aurora read randomize correction
  • Loading branch information
rusher committed Dec 12, 2016
1 parent 764285c commit a84d69d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Expand Up @@ -249,6 +249,9 @@ private List<String> getCurrentEndpointIdentifiers(Protocol protocol) throws Que
endpoints.add(resultSet.getString(1) + urlEndStr);
}

//randomize order for distributed load-balancing
Collections.shuffle(endpoints);

} finally {
proxy.lock.unlock();
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/mariadb/jdbc/failover/BaseMultiHostTest.java
Expand Up @@ -374,6 +374,18 @@ public int getServerId(Connection connection) throws Throwable {
return hostAddressList.indexOf(hostAddress) + 1;
}

/**
* Retrieve current HostAddress.
*
* @param connection connection
* @return Current Host address
* @throws Throwable if any exception occur
*/
public HostAddress getServerHostAddress(Connection connection) throws Throwable {
Protocol protocol = getProtocolFromConnection(connection);
return protocol.getHostAddress();
}

public boolean inTransaction(Connection connection) throws Throwable {
Protocol protocol = getProtocolFromConnection(connection);
return protocol.inTransaction();
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/org/mariadb/jdbc/failover/BaseReplication.java
Expand Up @@ -3,6 +3,7 @@
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.mariadb.jdbc.HostAddress;
import org.mariadb.jdbc.MariaDbServerPreparedStatement;

import java.sql.*;
Expand Down Expand Up @@ -262,7 +263,7 @@ public void writeToSlaveAfterFailover() throws Throwable {
@Test
public void randomConnection() throws Throwable {
Connection connection = null;
Map<String, MutableInt> connectionMap = new HashMap<>();
Map<HostAddress, MutableInt> connectionMap = new HashMap<>();
int masterId = -1;
for (int i = 0; i < 20; i++) {
try {
Expand All @@ -273,10 +274,10 @@ public void randomConnection() throws Throwable {
}
masterId = serverId;
connection.setReadOnly(true);
int replicaId = getServerId(connection);
MutableInt count = connectionMap.get(String.valueOf(replicaId));
HostAddress replicaHost = getServerHostAddress(connection);
MutableInt count = connectionMap.get(replicaHost);
if (count == null) {
connectionMap.put(String.valueOf(replicaId), new MutableInt());
connectionMap.put(replicaHost, new MutableInt());
} else {
count.increment();
}
Expand All @@ -288,7 +289,7 @@ public void randomConnection() throws Throwable {
}

Assert.assertTrue(connectionMap.size() >= 2);
for (String key : connectionMap.keySet()) {
for (HostAddress key : connectionMap.keySet()) {
Integer connectionCount = connectionMap.get(key).get();
Assert.assertTrue(connectionCount > 1);
}
Expand Down

0 comments on commit a84d69d

Please sign in to comment.