Elffuss BraidHub

kikocis/java-driver public

Add test for cluster init with failing contact points (JAVA-522).
Changeset324c75d775bf4fe9a4d177549369d7ab2f049ecd8b04af517511c4d2128d880e
Authorolim7t <omichallat+github@gmail.com> · 2014-10-31 15:34 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2014-11-03 09:43 +01:00
Parents183f3bc05ab2

Browse the files at this changeset

2 files changed

added driver-core/src/test/java/com/datastax/driver/core/ClusterInitTest.java +102 −0
@@ -0,0 +1,102 @@
1+package com.datastax.driver.core;
2+
3+import java.net.InetAddress;
4+import java.util.List;
5+import java.util.concurrent.TimeUnit;
6+
7+import com.google.common.collect.Lists;
8+import org.slf4j.Logger;
9+import org.slf4j.LoggerFactory;
10+import org.testng.annotations.Test;
11+
12+import static org.assertj.core.api.Assertions.assertThat;
13+import static org.assertj.core.api.Assertions.fail;
14+
15+import com.datastax.driver.core.utils.UUIDs;
16+
17+import static com.datastax.driver.core.FakeHost.Behavior.THROWING_CONNECT_TIMEOUTS;
18+
19+public class ClusterInitTest {
20+ private static final Logger logger = LoggerFactory.getLogger(ClusterInitTest.class);
21+
22+ /**
23+ * Test for JAVA-522: when the cluster and session initialize, if some contact points are behaving badly and
24+ * causing timeouts, we want to ensure that the driver does not wait multiple times on the same host.
25+ */
26+
27+ @Test(groups = "short")
28+ public void should_wait_for_each_contact_point_at_most_once() {
29+ CCMBridge ccm = null;
30+ Cluster cluster = null;
31+ List<FakeHost> fakeHosts = Lists.newArrayList();
32+ try {
33+ // Obtaining connect timeouts is not trivial: we create a 6-host cluster but only start one of them,
34+ // then simulate the other 5.
35+ ccm = CCMBridge.create("test");
36+ ccm.populate(6);
37+ ccm.start(1);
38+
39+ for (int i = 0; i < 5; i++) {
40+ FakeHost fakeHost = new FakeHost(CCMBridge.ipOfNode(i + 2), 9042, THROWING_CONNECT_TIMEOUTS);
41+ fakeHosts.add(fakeHost);
42+ fakeHost.start();
43+ }
44+
45+ // Our real instance has no rows in its system.peers table. That would cause the driver to ignore our fake
46+ // hosts when the control connection refreshes the host list.
47+ // So we also insert fake rows in system.peers:
48+ fakePeerRowsInNode1();
49+
50+ logger.info("Environment is set up, starting test");
51+ long start = System.nanoTime();
52+
53+ cluster = Cluster.builder().addContactPoints(
54+ CCMBridge.ipOfNode(1), CCMBridge.ipOfNode(2), CCMBridge.ipOfNode(3),
55+ CCMBridge.ipOfNode(4), CCMBridge.ipOfNode(5), CCMBridge.ipOfNode(6)
56+ ).build();
57+ cluster.connect();
58+
59+ long initTimeMs = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
60+
61+ // It's hard to estimate how much time init should take. But at the very least, we should wait at most
62+ // once for each of the 5 failing hosts, so 6 times the connect timeout is a reasonable upper bound.
63+ int expected = cluster.getConfiguration().getSocketOptions().getConnectTimeoutMillis() * 6;
64+
65+ logger.info("Cluster and session initialized in {} ms", initTimeMs);
66+ assertThat(initTimeMs).isLessThan(expected);
67+ } finally {
68+ if (cluster != null)
69+ cluster.close();
70+ for (FakeHost fakeHost : fakeHosts)
71+ fakeHost.stop();
72+ if (ccm != null)
73+ ccm.remove();
74+ }
75+ }
76+
77+ private void fakePeerRowsInNode1() {
78+ Cluster cluster = null;
79+ try {
80+ cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
81+ Session session = cluster.connect("system");
82+
83+ String releaseVersion = session.execute("SELECT release_version FROM local")
84+ .one().getString("release_version");
85+
86+ for (int i = 2; i <= 6; i++)
87+ session.execute("INSERT INTO peers (peer, data_center, host_id, rack, release_version, rpc_address, schema_version) " +
88+ "VALUES (?, 'datacenter1', ?, 'rack1', ?, ?, ?)",
89+ InetAddress.getByName(CCMBridge.ipOfNode(i)),
90+ UUIDs.random(),
91+ releaseVersion,
92+ InetAddress.getByName(CCMBridge.ipOfNode(i)),
93+ UUIDs.random()
94+ );
95+ } catch (Exception e) {
96+ fail("Error while inserting fake peer rows", e);
97+ } finally {
98+ if (cluster != null)
99+ cluster.close();
100+ }
101+ }
102+}
added driver-core/src/test/java/com/datastax/driver/core/FakeHost.java +73 −0
@@ -0,0 +1,73 @@
1+package com.datastax.driver.core;
2+
3+import java.io.IOException;
4+import java.net.InetAddress;
5+import java.net.ServerSocket;
6+import java.net.Socket;
7+import java.util.concurrent.ExecutorService;
8+import java.util.concurrent.Executors;
9+import java.util.concurrent.TimeUnit;
10+
11+import static org.assertj.core.api.Assertions.fail;
12+
13+/**
14+ * Fake Cassandra host that will cause a given error when the driver tries to connect to it.
15+ */
16+public class FakeHost {
17+ public enum Behavior {THROWING_CONNECT_TIMEOUTS, THROWING_OPERATION_TIMEOUTS}
18+
19+ private final String address;
20+ private final int port;
21+ private final Behavior behavior;
22+ private final ExecutorService executor;
23+
24+ FakeHost(String address, int port, Behavior behavior) {
25+ this.address = address;
26+ this.port = port;
27+ this.behavior = behavior;
28+ this.executor = Executors.newSingleThreadExecutor();
29+ }
30+
31+ public void start() {
32+ executor.execute(new AcceptClientAndWait(address, port, behavior));
33+ }
34+
35+ public void stop() {
36+ executor.shutdownNow();
37+ }
38+
39+ private static class AcceptClientAndWait implements Runnable {
40+
41+ private final String address;
42+ private final int port;
43+ private final Behavior behavior;
44+
45+ public AcceptClientAndWait(String address, int port, Behavior behavior) {
46+ this.address = address;
47+ this.port = port;
48+ this.behavior = behavior;
49+ }
50+
51+ @Override public void run() {
52+ try {
53+ InetAddress bindAddress = InetAddress.getByName(address);
54+ int backlog = (behavior == Behavior.THROWING_CONNECT_TIMEOUTS)
55+ ? 1
56+ : -1; // default
57+ ServerSocket server = new ServerSocket(port, backlog, bindAddress);
58+
59+ if (behavior == Behavior.THROWING_CONNECT_TIMEOUTS) {
60+ // fill backlog queue
61+ new Socket().connect(server.getLocalSocketAddress());
62+ }
63+
64+ TimeUnit.MINUTES.sleep(10);
65+ fail("Mock host wasn't expected to live more than 10 minutes");
66+ } catch (IOException e) {
67+ fail("Unexpected I/O exception", e);
68+ } catch (InterruptedException e) {
69+ // interruption is the expected way to stop this runnable, exit
70+ }
71+ }
72+ }
73+}