Elffuss BraidHub

kikocis/java-driver public

Fix unwanted LBP notifications when a contact host is down (JAVA-613).
Changesete7b0645714f968f89145af648c48108622365b612886841921779c2ef41bad14
Authorolim7t <omichallat+github@gmail.com> · 2015-01-12 16:31 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-01-30 16:37 +01:00
Parents9913b399123d

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -9,6 +9,7 @@
99 streamId was already released and reused (JAVA-632).
1010 - [bug] Fix issue when newly opened pool fails before we could mark the node UP
1111 (JAVA-642)
12+- [bug] Fix unwanted LBP notifications when a contact host is down (JAVA-613)
1213
1314
1415 2.0.9:
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +8 −8
@@ -447,7 +447,7 @@
447447 return false;
448448 }
449449
450- updateInfo(host, row, cluster);
450+ updateInfo(host, row, cluster, false);
451451 return true;
452452
453453 } catch (ConnectionException e) {
@@ -474,9 +474,9 @@
474474 }
475475
476476 // row can come either from the 'local' table or the 'peers' one
477- private static void updateInfo(Host host, Row row, Cluster.Manager cluster) {
477+ private static void updateInfo(Host host, Row row, Cluster.Manager cluster, boolean isInitialConnection) {
478478 if (!row.isNull("data_center") || !row.isNull("rack"))
479- updateLocationInfo(host, row.getString("data_center"), row.getString("rack"), cluster);
479+ updateLocationInfo(host, row.getString("data_center"), row.getString("rack"), isInitialConnection, cluster);
480480
481481 String version = row.getString("release_version");
482482 // We don't know if it's a 'local' or a 'peers' row, and only 'peers' rows have the 'peer' field.
@@ -487,17 +487,17 @@
487487 host.setVersionAndListenAdress(version, listenAddress);
488488 }
489489
490- private static void updateLocationInfo(Host host, String datacenter, String rack, Cluster.Manager cluster) {
490+ private static void updateLocationInfo(Host host, String datacenter, String rack, boolean isInitialConnection, Cluster.Manager cluster) {
491491 if (Objects.equal(host.getDatacenter(), datacenter) && Objects.equal(host.getRack(), rack))
492492 return;
493493
494494 // If the dc/rack information changes for an existing node, we need to update the load balancing policy.
495495 // For that, we remove and re-add the node against the policy. Not the most elegant, and assumes
496496 // that the policy will update correctly, but in practice this should work.
497- if (!host.wasJustAdded())
497+ if (!isInitialConnection)
498498 cluster.loadBalancingPolicy().onDown(host);
499499 host.setLocationInfo(datacenter, rack);
500- if (!host.wasJustAdded())
500+ if (!isInitialConnection)
501501 cluster.loadBalancingPolicy().onAdd(host);
502502 }
503503
@@ -531,7 +531,7 @@
531531 if (host == null) {
532532 logger.debug("Host in local system table ({}) unknown to us (ok if said host just got removed)", connection.address);
533533 } else {
534- updateInfo(host, localRow, cluster);
534+ updateInfo(host, localRow, cluster, isInitialConnection);
535535 Set<String> tokens = localRow.getSet("tokens", String.class);
536536 if (partitioner != null && !tokens.isEmpty())
537537 tokenMap.put(host, tokens);
@@ -568,7 +568,7 @@
568568 isNew = true;
569569 }
570570 if (dcs.get(i) != null || racks.get(i) != null)
571- updateLocationInfo(host, dcs.get(i), racks.get(i), cluster);
571+ updateLocationInfo(host, dcs.get(i), racks.get(i), isInitialConnection, cluster);
572572 if (cassandraVersions.get(i) != null)
573573 host.setVersionAndListenAdress(cassandraVersions.get(i), listenAddresses.get(i));
574574
modified driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyBootstrapTest.java +158 −46
@@ -15,95 +15,207 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import java.io.PrintWriter;
19-import java.io.StringWriter;
2018 import java.util.Collection;
21-
22-import org.testng.annotations.AfterClass;
23-import org.testng.annotations.BeforeClass;
19+import java.util.List;
20+
21+import com.google.common.base.Objects;
22+import com.google.common.collect.Lists;
23+import org.slf4j.Logger;
24+import org.slf4j.LoggerFactory;
2425 import org.testng.annotations.Test;
2526
26-import static org.testng.Assert.assertEquals;
27+import static org.assertj.core.api.Assertions.assertThat;
2728
2829 import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy;
2930 import com.datastax.driver.core.policies.LoadBalancingPolicy;
3031 import com.datastax.driver.core.policies.RoundRobinPolicy;
3132
33+import static com.datastax.driver.core.LoadBalancingPolicyBootstrapTest.HistoryPolicy.Action.*;
34+import static com.datastax.driver.core.LoadBalancingPolicyBootstrapTest.HistoryPolicy.entry;
35+
3236 public class LoadBalancingPolicyBootstrapTest {
33- CountingPolicy policy;
34- CCMBridge.CCMCluster c;
35-
36- @BeforeClass(groups = "short")
37- private void setup() {
38- policy = new CountingPolicy(new RoundRobinPolicy());
39- Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(policy);
40- c = CCMBridge.buildCluster(2, builder);
41- }
4237
38+ private static final Logger logger = LoggerFactory.getLogger(LoadBalancingPolicyBootstrapTest.class);
39+
40+ /**
41+ * Ensures that when a cluster is initialized that {@link LoadBalancingPolicy#init(Cluster, Collection)} is called
42+ * with each reachable contact point.
43+ *
44+ * @test_category load_balancing:notification
45+ * @expected_result init() is called for each of two contact points.
46+ */
4347 @Test(groups = "short")
44- public void notificationsTest() throws Exception {
45- assertEquals(policy.inits, 2, "inits\n" + policy.history);
46- assertEquals(policy.adds, 0, "adds\n" + policy.history);
47- assertEquals(policy.suspecteds, 0, "suspecteds\n" + policy.history);
48- assertEquals(policy.removes, 0, "removes\n" + policy.history);
49- assertEquals(policy.ups, 0, "ups\n" + policy.history);
50- assertEquals(policy.downs, 0, "downs\n" + policy.history);
48+ public void should_init_policy_with_up_contact_points() throws Exception {
49+ HistoryPolicy policy = new HistoryPolicy(new RoundRobinPolicy());
50+
51+ CCMBridge ccm = null;
52+ Cluster cluster = null;
53+ try {
54+ ccm = CCMBridge.create("test", 2);
55+ cluster = Cluster.builder()
56+ .addContactPoints(CCMBridge.ipOfNode(1), CCMBridge.ipOfNode(2))
57+ .withLoadBalancingPolicy(policy)
58+ .build();
59+
60+ cluster.init();
61+
62+ assertThat(policy.history).containsOnly(
63+ entry(INIT, TestUtils.findHost(cluster, 1)),
64+ entry(INIT, TestUtils.findHost(cluster, 2))
65+ );
66+ } finally {
67+ if (cluster != null)
68+ cluster.close();
69+ if (ccm != null)
70+ ccm.remove();
71+ }
5172 }
5273
53- @AfterClass(groups = "short")
54- private void tearDown() {
55- c.discard();
74+ /**
75+ * Ensures that {@link LoadBalancingPolicy#onDown(Host)} is called for a contact point that couldn't
76+ * be reached while initializing the control connection, but only after
77+ * {@link LoadBalancingPolicy#init(Cluster, Collection)} has been called.
78+ *
79+ * @test_category load_balancing:notification
80+ * @expected_result init() is called with the up host, followed by onDown() for the downed host.
81+ * @jira_ticket JAVA-613
82+ * @since 2.0.10, 2.1.5
83+ */
84+ @Test(groups = "short")
85+ public void should_send_down_notifications_after_init_when_contact_points_are_down() throws Exception {
86+ CCMBridge ccm = null;
87+ Cluster cluster = null;
88+ try {
89+ ccm = CCMBridge.create("test", 2);
90+
91+ // In order to validate this behavior, we need to stop the first node that would be attempted to be
92+ // established as the control connection.
93+ // This depends on the internal behavior and will even be made totally random by JAVA-618, therefore
94+ // we retry the scenario until we get the desired preconditions.
95+
96+ int nodeToStop = 1;
97+ int tries = 1, maxTries = 10;
98+ for (; tries <= maxTries; tries++) {
99+ nodeToStop = (nodeToStop == 1) ? 2 : 1; // switch nodes at each try
100+ int activeNode = nodeToStop == 2 ? 1 : 2;
101+
102+ ccm.stop(nodeToStop);
103+ ccm.waitForDown(nodeToStop);
104+
105+ HistoryPolicy policy = new HistoryPolicy(new RoundRobinPolicy());
106+ cluster = Cluster.builder()
107+ .addContactPoints(CCMBridge.ipOfNode(1), CCMBridge.ipOfNode(2))
108+ .withLoadBalancingPolicy(policy)
109+ .build();
110+
111+ cluster.init();
112+
113+ if (policy.history.contains(entry(DOWN, TestUtils.findHost(cluster, nodeToStop)))) {
114+ // This is the situation we're testing, the control connection tried the stopped node first.
115+ assertThat(policy.history).containsExactly(
116+ entry(INIT, TestUtils.findHost(cluster, activeNode)),
117+ entry(DOWN, TestUtils.findHost(cluster, nodeToStop))
118+ );
119+ break;
120+ } else {
121+ assertThat(policy.history).containsOnly(
122+ entry(INIT, TestUtils.findHost(cluster, 1)),
123+ entry(INIT, TestUtils.findHost(cluster, 2))
124+ );
125+
126+ logger.info("Could not get first contact point to fail, retrying");
127+
128+ cluster.close();
129+
130+ ccm.start(nodeToStop);
131+ ccm.waitForUp(nodeToStop);
132+ }
133+ }
134+
135+ if (tries == maxTries + 1)
136+ logger.warn("Could not get first contact point to fail after {} tries", maxTries);
137+
138+ } finally {
139+ if (cluster != null)
140+ cluster.close();
141+ if (ccm != null)
142+ ccm.remove();
143+ }
56144 }
57145
58- static class CountingPolicy extends DelegatingLoadBalancingPolicy {
146+ static class HistoryPolicy extends DelegatingLoadBalancingPolicy {
147+ enum Action {INIT, UP, DOWN, ADD, REMOVE, SUSPECT}
148+
149+ static class Entry {
150+ final Action action;
151+ final Host host;
152+
153+ public Entry(Action action, Host host) {
154+ this.action = action;
155+ this.host = host;
156+ }
157+
158+ @Override
159+ public boolean equals(Object other) {
160+ if (other == this)
161+ return true;
162+ if (other instanceof Entry) {
163+ Entry that = (Entry)other;
164+ return this.action == that.action && this.host.equals(that.host);
165+ }
166+ return false;
167+ }
168+
169+ @Override
170+ public int hashCode() {
171+ return Objects.hashCode(action, host);
172+ }
173+
174+ @Override public String toString() {
175+ return Objects.toStringHelper(this).add("action", action).add("host", host).toString();
176+ }
177+ }
59178
60- int inits;
61- int adds;
62- int suspecteds;
63- int removes;
64- int ups;
65- int downs;
179+ static Entry entry(Action action, Host host) {
180+ return new Entry(action, host);
181+ }
66182
67- final StringWriter history = new StringWriter();
68- private final PrintWriter out = new PrintWriter(history);
183+ List<Entry> history = Lists.newArrayList();
69184
70- public CountingPolicy(LoadBalancingPolicy delegate) {
185+ public HistoryPolicy(LoadBalancingPolicy delegate) {
71186 super(delegate);
72187 }
73188
74189 @Override
75190 public void init(Cluster cluster, Collection<Host> hosts) {
76191 super.init(cluster, hosts);
77- inits += hosts.size();
192+ for (Host host : hosts) {
193+ history.add(entry(INIT, host));
194+ }
78195 }
79196
80197 public void onAdd(Host host) {
81- out.printf("add %s%n", host);
82- adds++;
198+ history.add(entry(ADD, host));
83199 super.onAdd(host);
84200 }
85201
86202 public void onSuspected(Host host) {
87- out.printf("suspect %s%n", host);
88- suspecteds++;
203+ history.add(entry(SUSPECT, host));
89204 super.onSuspected(host);
90205 }
91206
92207 public void onUp(Host host) {
93- out.printf("up %s%n", host);
94- ups++;
208+ history.add(entry(UP, host));
95209 super.onUp(host);
96210 }
97211
98212 public void onDown(Host host) {
99- out.printf("down %s%n", host);
100- downs++;
213+ history.add(entry(DOWN, host));
101214 super.onDown(host);
102215 }
103216
104217 public void onRemove(Host host) {
105- out.printf("remove %s%n", host);
106- removes++;
218+ history.add(entry(REMOVE, host));
107219 super.onRemove(host);
108220 }
109221 }