Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -2733,16 +2734,18 @@ public void testRowBinaryWithDefaults() throws ClickHouseException, IOException,
}

@Test(dataProvider = "loadBalancingPolicies", groups = {"unit"})
public void testLoadBalancingPolicyFailover(ClickHouseLoadBalancingPolicy loadBalancingPolicy) {
String firstEndpoint = "111.1.1.1";
String secondEndpoint = "222.2.2.2";
public void testLoadBalancingPolicyFailover(ClickHouseLoadBalancingPolicy loadBalancingPolicy) throws IOException {
String endpoint = "127.0.0.1";
int[] ports = findUnusedPorts(endpoint, 2);
int firstPort = ports[0];
int secondPort = ports[1];

Properties props = new Properties();
props.setProperty("failover", "1");

// nodes where the first node is failed
ClickHouseNodes nodes = ClickHouseNodes.of(
getProtocol() + "://" + firstEndpoint + "," + secondEndpoint,
getProtocol() + "://" + endpoint + ":" + firstPort + "," + endpoint + ":" + secondPort,
props
);

Expand All @@ -2753,21 +2756,47 @@ public void testLoadBalancingPolicyFailover(ClickHouseLoadBalancingPolicy loadBa
Assert.fail("Exception expected for query on failed node");
} catch (Exception failoverException) {
ClickHouseNode failoverNode = loadBalancingPolicy.suggest(nodes, nodes.nodes.getFirst(), failoverException);
Assert.assertEquals(failoverNode.getHost(), secondEndpoint, "The next node is expected to be suggested by the load balancing policy");
Assert.assertEquals(failoverNode.getHost(), endpoint, "The next node is expected to be suggested by the load balancing policy");
Assert.assertEquals(failoverNode.getPort(), secondPort, "The next node is expected to be suggested by the load balancing policy");
}
}

/**
* Returns distinct TCP ports on the given local address that have no listener, so a connection to them is
* refused at once. All sockets stay open until every port is known, so the OS cannot return the same port twice.
*/
private static int[] findUnusedPorts(String localAddress, int count) throws IOException {
InetAddress address = InetAddress.getByName(localAddress);
ServerSocket[] sockets = new ServerSocket[count];
try {
int[] ports = new int[count];
for (int i = 0; i < count; i++) {
sockets[i] = new ServerSocket(0, 1, address);
ports[i] = sockets[i].getLocalPort();
}
return ports;
} finally {
for (ServerSocket socket : sockets) {
if (socket != null) {
socket.close();
}
}
}
}

@Test(groups = {"integration"})
public void testFailover() throws ClickHouseException {
public void testFailover() throws ClickHouseException, IOException {
if (isCloud()) return; //TODO: testFailover - Revisit, see: https://github.com/ClickHouse/clickhouse-java/issues/1747
ClickHouseNode availableNode = getServer();
Properties props = new Properties();
props.setProperty("failover", "1");
props.setProperty(ClickHouseDefaults.PASSWORD.getKey(), getPassword());

// nodes with the first node is unavailable
String unavailableEndpoint = "127.0.0.1";
int unavailablePort = findUnusedPorts(unavailableEndpoint, 1)[0];
ClickHouseNodes nodes = ClickHouseNodes.of(
getProtocol() + "://111.1.1.1," + availableNode.getBaseUri(),
getProtocol() + "://" + unavailableEndpoint + ":" + unavailablePort + "," + availableNode.getBaseUri(),
props
);

Expand Down
Loading