kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e7b0645714f968f89145af648c48108622365b612886841921779c2ef41bad14 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-01-12 16:31 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-01-30 16:37 +01:00 |
| Parents | 9913b399123d |
3 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | streamId was already released and reused (JAVA-632). |
| 10 | 10 | - [bug] Fix issue when newly opened pool fails before we could mark the node UP |
| 11 | 11 | (JAVA-642) |
| 12 | +- [bug] Fix unwanted LBP notifications when a contact host is down (JAVA-613) | |
| 12 | 13 | |
| 13 | 14 | |
| 14 | 15 | 2.0.9: |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +8 −8
| @@ -447,7 +447,7 @@ | ||
| 447 | 447 | return false; |
| 448 | 448 | } |
| 449 | 449 | |
| 450 | - updateInfo(host, row, cluster); | |
| 450 | + updateInfo(host, row, cluster, false); | |
| 451 | 451 | return true; |
| 452 | 452 | |
| 453 | 453 | } catch (ConnectionException e) { |
| @@ -474,9 +474,9 @@ | ||
| 474 | 474 | } |
| 475 | 475 | |
| 476 | 476 | // 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) { | |
| 478 | 478 | 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); | |
| 480 | 480 | |
| 481 | 481 | String version = row.getString("release_version"); |
| 482 | 482 | // 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 @@ | ||
| 487 | 487 | host.setVersionAndListenAdress(version, listenAddress); |
| 488 | 488 | } |
| 489 | 489 | |
| 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) { | |
| 491 | 491 | if (Objects.equal(host.getDatacenter(), datacenter) && Objects.equal(host.getRack(), rack)) |
| 492 | 492 | return; |
| 493 | 493 | |
| 494 | 494 | // If the dc/rack information changes for an existing node, we need to update the load balancing policy. |
| 495 | 495 | // For that, we remove and re-add the node against the policy. Not the most elegant, and assumes |
| 496 | 496 | // that the policy will update correctly, but in practice this should work. |
| 497 | - if (!host.wasJustAdded()) | |
| 497 | + if (!isInitialConnection) | |
| 498 | 498 | cluster.loadBalancingPolicy().onDown(host); |
| 499 | 499 | host.setLocationInfo(datacenter, rack); |
| 500 | - if (!host.wasJustAdded()) | |
| 500 | + if (!isInitialConnection) | |
| 501 | 501 | cluster.loadBalancingPolicy().onAdd(host); |
| 502 | 502 | } |
| 503 | 503 | |
| @@ -531,7 +531,7 @@ | ||
| 531 | 531 | if (host == null) { |
| 532 | 532 | logger.debug("Host in local system table ({}) unknown to us (ok if said host just got removed)", connection.address); |
| 533 | 533 | } else { |
| 534 | - updateInfo(host, localRow, cluster); | |
| 534 | + updateInfo(host, localRow, cluster, isInitialConnection); | |
| 535 | 535 | Set<String> tokens = localRow.getSet("tokens", String.class); |
| 536 | 536 | if (partitioner != null && !tokens.isEmpty()) |
| 537 | 537 | tokenMap.put(host, tokens); |
| @@ -568,7 +568,7 @@ | ||
| 568 | 568 | isNew = true; |
| 569 | 569 | } |
| 570 | 570 | 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); | |
| 572 | 572 | if (cassandraVersions.get(i) != null) |
| 573 | 573 | host.setVersionAndListenAdress(cassandraVersions.get(i), listenAddresses.get(i)); |
| 574 | 574 |
modified driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyBootstrapTest.java +158 −46
| @@ -15,95 +15,207 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.io.PrintWriter; | |
| 19 | -import java.io.StringWriter; | |
| 20 | 18 | 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; | |
| 24 | 25 | import org.testng.annotations.Test; |
| 25 | 26 | |
| 26 | -import static org.testng.Assert.assertEquals; | |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | 28 | |
| 28 | 29 | import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy; |
| 29 | 30 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| 30 | 31 | import com.datastax.driver.core.policies.RoundRobinPolicy; |
| 31 | 32 | |
| 33 | +import static com.datastax.driver.core.LoadBalancingPolicyBootstrapTest.HistoryPolicy.Action.*; | |
| 34 | +import static com.datastax.driver.core.LoadBalancingPolicyBootstrapTest.HistoryPolicy.entry; | |
| 35 | + | |
| 32 | 36 | 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 | - } | |
| 42 | 37 | |
| 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 | + */ | |
| 43 | 47 | @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 | + } | |
| 51 | 72 | } |
| 52 | 73 | |
| 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 | + } | |
| 56 | 144 | } |
| 57 | 145 | |
| 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 | + } | |
| 59 | 178 | |
| 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 | + } | |
| 66 | 182 | |
| 67 | - final StringWriter history = new StringWriter(); | |
| 68 | - private final PrintWriter out = new PrintWriter(history); | |
| 183 | + List<Entry> history = Lists.newArrayList(); | |
| 69 | 184 | |
| 70 | - public CountingPolicy(LoadBalancingPolicy delegate) { | |
| 185 | + public HistoryPolicy(LoadBalancingPolicy delegate) { | |
| 71 | 186 | super(delegate); |
| 72 | 187 | } |
| 73 | 188 | |
| 74 | 189 | @Override |
| 75 | 190 | public void init(Cluster cluster, Collection<Host> hosts) { |
| 76 | 191 | super.init(cluster, hosts); |
| 77 | - inits += hosts.size(); | |
| 192 | + for (Host host : hosts) { | |
| 193 | + history.add(entry(INIT, host)); | |
| 194 | + } | |
| 78 | 195 | } |
| 79 | 196 | |
| 80 | 197 | public void onAdd(Host host) { |
| 81 | - out.printf("add %s%n", host); | |
| 82 | - adds++; | |
| 198 | + history.add(entry(ADD, host)); | |
| 83 | 199 | super.onAdd(host); |
| 84 | 200 | } |
| 85 | 201 | |
| 86 | 202 | public void onSuspected(Host host) { |
| 87 | - out.printf("suspect %s%n", host); | |
| 88 | - suspecteds++; | |
| 203 | + history.add(entry(SUSPECT, host)); | |
| 89 | 204 | super.onSuspected(host); |
| 90 | 205 | } |
| 91 | 206 | |
| 92 | 207 | public void onUp(Host host) { |
| 93 | - out.printf("up %s%n", host); | |
| 94 | - ups++; | |
| 208 | + history.add(entry(UP, host)); | |
| 95 | 209 | super.onUp(host); |
| 96 | 210 | } |
| 97 | 211 | |
| 98 | 212 | public void onDown(Host host) { |
| 99 | - out.printf("down %s%n", host); | |
| 100 | - downs++; | |
| 213 | + history.add(entry(DOWN, host)); | |
| 101 | 214 | super.onDown(host); |
| 102 | 215 | } |
| 103 | 216 | |
| 104 | 217 | public void onRemove(Host host) { |
| 105 | - out.printf("remove %s%n", host); | |
| 106 | - removes++; | |
| 218 | + history.add(entry(REMOVE, host)); | |
| 107 | 219 | super.onRemove(host); |
| 108 | 220 | } |
| 109 | 221 | } |