kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 9eb596ab550a57009493ea1b32b393f767e65a8dac31fd254eb8c4555eb75237 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-23 16:35 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-23 16:37 +02:00 |
| Parents | f9a61083aeb0 |
5 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | - [bug] LatencyAwarePolicy does not shutdown executor on invocation of close (JAVA-516) |
| 25 | 25 | - [improvement] Throw an exception when DCAwareRoundRobinPolicy is built with |
| 26 | 26 | an explicit but null or empty local datacenter (JAVA-451). |
| 27 | +- [bug] Fix check for local contact points in DCAware policy's init (JAVA-511) | |
| 27 | 28 | |
| 28 | 29 | |
| 29 | 30 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +10 −12
| @@ -1172,27 +1172,25 @@ | ||
| 1172 | 1172 | try { |
| 1173 | 1173 | while (true) { |
| 1174 | 1174 | try { |
| 1175 | - // Make a copy of the hosts - which only contain the contact points at this stage - before we initialize the | |
| 1176 | - // control connection. We use a set with guaranteed insertion order, so that the contact points will remain | |
| 1177 | - // first, before any other host discovered by the control connection. | |
| 1178 | - // This is important if the load balancing policy is the DCAware one, because it needs to see the contact points | |
| 1179 | - // first for the local DC detection to work properly. | |
| 1180 | - Set<Host> hosts = Sets.newLinkedHashSet(metadata.allHosts()); | |
| 1175 | + // At this stage, metadata.allHosts() only contains the contact points, that's what we want to pass to LBP.init(). | |
| 1176 | + // But the control connection will initialize first and discover more hosts, so make a copy. | |
| 1177 | + Set<Host> contactPointHosts = Sets.newHashSet(metadata.allHosts()); | |
| 1181 | 1178 | |
| 1182 | 1179 | controlConnection.connect(); |
| 1183 | 1180 | if (connectionFactory.protocolVersion < 0) |
| 1184 | 1181 | connectionFactory.protocolVersion = 2; |
| 1185 | 1182 | |
| 1186 | - // metadata now also contains other hosts discovered by the connection, update our copy | |
| 1187 | - hosts.addAll(metadata.allHosts()); | |
| 1188 | - | |
| 1189 | 1183 | // Now that the control connection is ready, we have all the information we need about the nodes (datacenter, |
| 1190 | 1184 | // rack...) to initialize the load balancing policy |
| 1191 | - loadBalancingPolicy().init(Cluster.this, hosts); | |
| 1192 | - | |
| 1185 | + loadBalancingPolicy().init(Cluster.this, contactPointHosts); | |
| 1186 | + // Add the remaining hosts that were discovered by the control connection: | |
| 1187 | + for (Host host : metadata.allHosts()) { | |
| 1188 | + if (!contactPointHosts.contains(host)) | |
| 1189 | + loadBalancingPolicy().onAdd(host); | |
| 1190 | + } | |
| 1193 | 1191 | isFullyInit = true; |
| 1194 | 1192 | |
| 1195 | - for (Host host : hosts) | |
| 1193 | + for (Host host : metadata.allHosts()) | |
| 1196 | 1194 | triggerOnAdd(host); |
| 1197 | 1195 | |
| 1198 | 1196 | return; |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +3 −3
| @@ -193,14 +193,14 @@ | ||
| 193 | 193 | for (Host host : hosts) { |
| 194 | 194 | String dc = dc(host); |
| 195 | 195 | |
| 196 | - if (!dc.equals(localDc)) notInLocalDC.add(String.format("%s (%s)", host.toString(), host.getDatacenter())); | |
| 197 | - | |
| 198 | 196 | // If the localDC was in "auto-discover" mode and it's the first host for which we have a DC, use it. |
| 199 | 197 | if (localDc == UNSET && dc != UNSET) { |
| 200 | 198 | logger.info("Using data-center name '{}' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)", dc); |
| 201 | 199 | localDc = dc; |
| 202 | 200 | } |
| 203 | 201 | |
| 202 | + if (!dc.equals(localDc)) notInLocalDC.add(String.format("%s (%s)", host.toString(), host.getDatacenter())); | |
| 203 | + | |
| 204 | 204 | CopyOnWriteArrayList<Host> prev = perDcLiveHosts.get(dc); |
| 205 | 205 | if (prev == null) |
| 206 | 206 | perDcLiveHosts.put(dc, new CopyOnWriteArrayList<Host>(Collections.singletonList(host))); |
| @@ -210,7 +210,7 @@ | ||
| 210 | 210 | |
| 211 | 211 | if (notInLocalDC.size() > 0) { |
| 212 | 212 | String nonLocalHosts = Joiner.on(",").join(notInLocalDC); |
| 213 | - logger.warn("Some contact points don't match specified local data center. Local DC = {}. Non-conforming contact points: {}", localDc, nonLocalHosts); | |
| 213 | + logger.warn("Some contact points don't match local data center. Local DC = {}. Non-conforming contact points: {}", localDc, nonLocalHosts); | |
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | 216 |
added driver-core/src/test/java/com/datastax/driver/core/MemoryAppender.java +25 −0
| @@ -0,0 +1,25 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import java.io.StringWriter; | |
| 4 | + | |
| 5 | +import org.apache.log4j.PatternLayout; | |
| 6 | +import org.apache.log4j.WriterAppender; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * Simple Log4J appender that captures logs to memory in order to inspect them in unit tests. | |
| 10 | + * <p> | |
| 11 | + * There is no purging mechanism, so make sure it doesn't stay enabled for too long (this is best | |
| 12 | + * done with an {@code @After} method that removes it). | |
| 13 | + */ | |
| 14 | +public class MemoryAppender extends WriterAppender { | |
| 15 | + public final StringWriter writer = new StringWriter(); | |
| 16 | + | |
| 17 | + public MemoryAppender() { | |
| 18 | + setWriter(writer); | |
| 19 | + setLayout(new PatternLayout("%m%n")); | |
| 20 | + } | |
| 21 | + | |
| 22 | + public String get() { | |
| 23 | + return writer.toString(); | |
| 24 | + } | |
| 25 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicyTest.java +162 −18
| @@ -1,42 +1,186 @@ | ||
| 1 | 1 | package com.datastax.driver.core.policies; |
| 2 | 2 | |
| 3 | -import org.testng.annotations.Test; | |
| 3 | +import java.util.Collection; | |
| 4 | +import java.util.Set; | |
| 5 | +import java.util.concurrent.CopyOnWriteArraySet; | |
| 6 | + | |
| 7 | +import org.apache.log4j.Level; | |
| 8 | +import org.apache.log4j.Logger; | |
| 9 | +import org.testng.annotations.*; | |
| 10 | + | |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 4 | 12 | import static org.testng.Assert.assertEquals; |
| 5 | 13 | |
| 6 | -import com.datastax.driver.core.CCMBridge; | |
| 7 | -import com.datastax.driver.core.Cluster; | |
| 8 | -import com.datastax.driver.core.Host; | |
| 9 | -import com.datastax.driver.core.TestUtils; | |
| 14 | +import com.datastax.driver.core.*; | |
| 10 | 15 | |
| 11 | 16 | public class DCAwareRoundRobinPolicyTest { |
| 17 | + MemoryAppender logs; | |
| 18 | + CCMBridge ccm; | |
| 19 | + | |
| 20 | + @BeforeClass(groups = "short") | |
| 21 | + public void createCcm() { | |
| 22 | + // Two-DC cluster with one host in each DC | |
| 23 | + ccm = CCMBridge.create("test", 1, 1); | |
| 24 | + } | |
| 25 | + | |
| 26 | + @AfterClass(groups = "short") | |
| 27 | + public void deleteCcm() { | |
| 28 | + if (ccm != null) | |
| 29 | + ccm.remove(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + @BeforeMethod(groups = "short") | |
| 33 | + public void startRecordingLogs() { | |
| 34 | + Logger policyLogger = Logger.getLogger(DCAwareRoundRobinPolicy.class); | |
| 35 | + assertThat(Level.WARN.isGreaterOrEqual(policyLogger.getEffectiveLevel())) | |
| 36 | + .isTrue() | |
| 37 | + .as("Log level must be at least WARN for this test to work"); | |
| 38 | + | |
| 39 | + logs = new MemoryAppender(); | |
| 40 | + policyLogger.addAppender(logs); | |
| 41 | + } | |
| 42 | + | |
| 43 | + @AfterMethod(groups = "short") | |
| 44 | + public void stopRecordingLogs() { | |
| 45 | + Logger.getLogger(DCAwareRoundRobinPolicy.class).removeAppender(logs); | |
| 46 | + } | |
| 12 | 47 | |
| 13 | 48 | @Test(groups = "short") |
| 14 | - public void should_pick_local_dc_from_contact_points() { | |
| 15 | - CCMBridge ccm = null; | |
| 49 | + public void should_use_local_dc_from_contact_points_when_not_explicitly_specified() { | |
| 16 | 50 | Cluster cluster = null; |
| 17 | - DCAwareRoundRobinPolicy policy; | |
| 51 | + CountingDCAwarePolicy policy = new CountingDCAwarePolicy(); | |
| 18 | 52 | |
| 19 | 53 | try { |
| 20 | - // Create two-DC cluster with one host in each DC | |
| 21 | - ccm = CCMBridge.create("test", 1, 1); | |
| 22 | - policy = new DCAwareRoundRobinPolicy(); | |
| 23 | - | |
| 24 | 54 | // Pass host1 as contact point |
| 25 | 55 | cluster = Cluster.builder() |
| 26 | - .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 27 | - .withLoadBalancingPolicy(policy) | |
| 28 | - .build(); | |
| 56 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 57 | + .withLoadBalancingPolicy(policy) | |
| 58 | + .build(); | |
| 29 | 59 | cluster.init(); |
| 30 | 60 | |
| 61 | + Host host1 = TestUtils.findHost(cluster, 1); | |
| 62 | + | |
| 31 | 63 | // Policy's localDC should be the one from the contact point |
| 64 | + assertThat(policy.getLocalDc()).isEqualTo(host1.getDatacenter()); | |
| 65 | + | |
| 66 | + assertThat(policy.initHosts).containsExactly(host1); | |
| 67 | + | |
| 68 | + assertThat(logs.get()) | |
| 69 | + .doesNotContain("Some contact points don't match local data center"); | |
| 70 | + | |
| 71 | + } finally { | |
| 72 | + if (cluster != null) | |
| 73 | + cluster.close(); | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + @Test(groups = "short") | |
| 78 | + public void should_warn_if_contact_points_have_different_dcs_when_not_explicitly_specified() { | |
| 79 | + Cluster cluster = null; | |
| 80 | + CountingDCAwarePolicy policy = new CountingDCAwarePolicy(); | |
| 81 | + | |
| 82 | + try { | |
| 83 | + // Pass both hosts as contact points, they have different DCs | |
| 84 | + cluster = Cluster.builder() | |
| 85 | + .addContactPoints(CCMBridge.ipOfNode(1), CCMBridge.ipOfNode(2)) | |
| 86 | + .withLoadBalancingPolicy(policy) | |
| 87 | + .build(); | |
| 88 | + cluster.init(); | |
| 89 | + | |
| 90 | + Host host1 = TestUtils.findHost(cluster, 1); | |
| 91 | + Host host2 = TestUtils.findHost(cluster, 2); | |
| 92 | + | |
| 93 | + assertThat(policy.initHosts).containsExactly(host1, host2); | |
| 94 | + | |
| 95 | + assertThat(logs.get()) | |
| 96 | + .contains("Some contact points don't match local data center"); | |
| 97 | + | |
| 98 | + } finally { | |
| 99 | + if (cluster != null) | |
| 100 | + cluster.close(); | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + @Test(groups = "short") | |
| 105 | + public void should_use_provided_local_dc_and_not_warn_if_contact_points_match() { | |
| 106 | + Cluster cluster = null; | |
| 107 | + String providedLocalDc = "dc1"; | |
| 108 | + CountingDCAwarePolicy policy = new CountingDCAwarePolicy(providedLocalDc); | |
| 109 | + | |
| 110 | + try { | |
| 111 | + cluster = Cluster.builder() | |
| 112 | + .addContactPoints(CCMBridge.ipOfNode(1)) | |
| 113 | + .withLoadBalancingPolicy(policy) | |
| 114 | + .build(); | |
| 115 | + cluster.init(); | |
| 116 | + | |
| 117 | + Host host1 = TestUtils.findHost(cluster, 1); | |
| 118 | + | |
| 119 | + assertEquals(policy.getLocalDc(), providedLocalDc); | |
| 120 | + | |
| 121 | + assertThat(policy.initHosts).containsExactly(host1); | |
| 122 | + | |
| 123 | + assertThat(logs.get()) | |
| 124 | + .doesNotContain("Some contact points don't match local data center"); | |
| 125 | + | |
| 126 | + } finally { | |
| 127 | + if (cluster != null) | |
| 128 | + cluster.close(); | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + @Test(groups = "short") | |
| 133 | + public void should_use_provided_local_dc_and_warn_if_contact_points_dont_match() { | |
| 134 | + Cluster cluster = null; | |
| 135 | + // Provide non-existent DC to make sure none of the contact points matches | |
| 136 | + String providedLocalDc = "dc3"; | |
| 137 | + CountingDCAwarePolicy policy = new CountingDCAwarePolicy(providedLocalDc); | |
| 138 | + | |
| 139 | + try { | |
| 140 | + cluster = Cluster.builder() | |
| 141 | + .addContactPoints(CCMBridge.ipOfNode(1), CCMBridge.ipOfNode(2)) | |
| 142 | + .withLoadBalancingPolicy(policy) | |
| 143 | + .build(); | |
| 144 | + cluster.init(); | |
| 145 | + | |
| 32 | 146 | Host host1 = TestUtils.findHost(cluster, 1); |
| 33 | - assertEquals(policy.localDc, host1.getDatacenter()); | |
| 147 | + Host host2 = TestUtils.findHost(cluster, 2); | |
| 148 | + | |
| 149 | + assertEquals(policy.getLocalDc(), providedLocalDc); | |
| 150 | + | |
| 151 | + assertThat(policy.initHosts).containsExactly(host1, host2); | |
| 152 | + | |
| 153 | + assertThat(logs.get()) | |
| 154 | + .contains("Some contact points don't match local data center"); | |
| 34 | 155 | |
| 35 | 156 | } finally { |
| 36 | 157 | if (cluster != null) |
| 37 | 158 | cluster.close(); |
| 38 | - if (ccm != null) | |
| 39 | - ccm.remove(); | |
| 159 | + } | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Wraps the policy under test to spy the calls to init. | |
| 164 | + */ | |
| 165 | + static class CountingDCAwarePolicy extends DelegatingLoadBalancingPolicy { | |
| 166 | + Set<Host> initHosts = new CopyOnWriteArraySet<Host>(); | |
| 167 | + | |
| 168 | + CountingDCAwarePolicy() { | |
| 169 | + super(new DCAwareRoundRobinPolicy()); | |
| 170 | + } | |
| 171 | + | |
| 172 | + CountingDCAwarePolicy(String localDc) { | |
| 173 | + super(new DCAwareRoundRobinPolicy(localDc)); | |
| 174 | + } | |
| 175 | + | |
| 176 | + String getLocalDc() { | |
| 177 | + return ((DCAwareRoundRobinPolicy) delegate).localDc; | |
| 178 | + } | |
| 179 | + | |
| 180 | + @Override public void init(Cluster cluster, Collection<Host> hosts) { | |
| 181 | + System.out.println("init " + hosts); | |
| 182 | + initHosts.addAll(hosts); | |
| 183 | + super.init(cluster, hosts); | |
| 40 | 184 | } |
| 41 | 185 | } |
| 42 | 186 | } |