kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 07db25ca83b141f158ba37f2c6adf83c51e4e0de9d8c23d8ca9f85d196baa3a8 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-02-02 11:23 +01:00 |
| Committer | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-02-13 07:25 -06:00 |
| Parents | ac197a0a34d1 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +9 −6
| @@ -205,14 +205,17 @@ | ||
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
| 208 | - * Triggers an asynchronous one-time reconnection attempt to this host. | |
| 208 | + * Triggers an asynchronous reconnection attempt to this host. | |
| 209 | 209 | * <p> |
| 210 | - * If reconnection succeeds, the host will be marked {@code UP} and its connection pool(s) | |
| 211 | - * will be initialized (unless the load balancing policy returns {@link HostDistance#IGNORED} | |
| 212 | - * for it). If reconnection fails, no further attempts will be scheduled. | |
| 210 | + * This method is intended for load balancing policies that mark hosts as {@link HostDistance#IGNORED IGNORED}, | |
| 211 | + * but still need a way to periodically check these hosts' states (UP / DOWN). | |
| 213 | 212 | * <p> |
| 214 | - * This method has no effect if the node is already {@code UP}, or if a reconnection attempt | |
| 215 | - * is already in progress. | |
| 213 | + * For a host that is at distance {@code IGNORED}, this method will try to reconnect exactly once: if | |
| 214 | + * reconnection succeeds, the host is marked {@code UP}; otherwise, no further attempts will be scheduled. | |
| 215 | + * It has no effect if the node is already {@code UP}, or if a reconnection attempt is already in progress. | |
| 216 | + * <p> | |
| 217 | + * Note that if the host is <em>not</em> a distance {@code IGNORED}, this method <em>will</em> trigger a periodic | |
| 218 | + * reconnection attempt if the reconnection fails. | |
| 216 | 219 | */ |
| 217 | 220 | public void tryReconnectOnce() { |
| 218 | 221 | this.manager.startSingleReconnectionAttempt(this); |
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +41 −19
| @@ -5,6 +5,8 @@ | ||
| 5 | 5 | import java.util.Collections; |
| 6 | 6 | import java.util.Iterator; |
| 7 | 7 | import java.util.Map; |
| 8 | +import java.util.concurrent.ConcurrentHashMap; | |
| 9 | +import java.util.concurrent.ConcurrentMap; | |
| 8 | 10 | import java.util.concurrent.TimeUnit; |
| 9 | 11 | import java.util.concurrent.atomic.AtomicInteger; |
| 10 | 12 | |
| @@ -12,7 +14,9 @@ | ||
| 12 | 14 | |
| 13 | 15 | import com.google.common.base.Charsets; |
| 14 | 16 | import com.google.common.collect.ImmutableMap; |
| 17 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 15 | 18 | import org.testng.annotations.Test; |
| 19 | + | |
| 16 | 20 | import static org.assertj.core.api.Assertions.assertThat; |
| 17 | 21 | |
| 18 | 22 | import com.datastax.driver.core.Host.State; |
| @@ -33,9 +37,9 @@ | ||
| 33 | 37 | ccm = CCMBridge.create("test", 2); |
| 34 | 38 | int reconnectionDelay = 1000; |
| 35 | 39 | cluster = Cluster.builder() |
| 36 | - .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 37 | - .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelay)) | |
| 38 | - .build(); | |
| 40 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 41 | + .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelay)) | |
| 42 | + .build(); | |
| 39 | 43 | cluster.connect(); |
| 40 | 44 | |
| 41 | 45 | assertThat(cluster).usesControlHost(1); |
| @@ -78,11 +82,11 @@ | ||
| 78 | 82 | CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMs)); |
| 79 | 83 | |
| 80 | 84 | cluster = Cluster.builder() |
| 81 | - .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 82 | - // Start with the correct auth so that we can initialize the server | |
| 83 | - .withAuthProvider(authProvider) | |
| 84 | - .withReconnectionPolicy(reconnectionPolicy) | |
| 85 | - .build(); | |
| 85 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 86 | + // Start with the correct auth so that we can initialize the server | |
| 87 | + .withAuthProvider(authProvider) | |
| 88 | + .withReconnectionPolicy(reconnectionPolicy) | |
| 89 | + .build(); | |
| 86 | 90 | |
| 87 | 91 | cluster.init(); |
| 88 | 92 | assertThat(cluster).usesControlHost(1); |
| @@ -128,9 +132,9 @@ | ||
| 128 | 132 | try { |
| 129 | 133 | ccm = CCMBridge.create("test", 2); |
| 130 | 134 | cluster = Cluster.builder() |
| 131 | - .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 132 | - .withReconnectionPolicy(reconnectionPolicy) | |
| 133 | - .build(); | |
| 135 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 136 | + .withReconnectionPolicy(reconnectionPolicy) | |
| 137 | + .build(); | |
| 134 | 138 | cluster.connect(); |
| 135 | 139 | |
| 136 | 140 | // Stop a node and cancel the reconnection attempts to it |
| @@ -169,22 +173,25 @@ | ||
| 169 | 173 | try { |
| 170 | 174 | ccm = CCMBridge.create("test", 1); |
| 171 | 175 | cluster = Cluster.builder() |
| 172 | - .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 173 | - .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 174 | - .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis)) | |
| 175 | - .build(); | |
| 176 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 177 | + .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 178 | + .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis)) | |
| 179 | + .build(); | |
| 176 | 180 | cluster.connect(); |
| 177 | 181 | |
| 178 | 182 | // Tweak the LBP so that the control connection never reconnects, otherwise |
| 179 | 183 | // it would interfere with the rest of the test (this is a bit of a hack) |
| 180 | 184 | loadBalancingPolicy.returnEmptyQueryPlan = true; |
| 181 | 185 | |
| 182 | - // Stop the node and cancel the reconnection attempts to it | |
| 186 | + // Stop the node, ignore it and cancel reconnection attempts to it | |
| 183 | 187 | ccm.stop(1); |
| 184 | 188 | ccm.waitForDown(1); |
| 185 | 189 | assertThat(cluster).host(1).goesDownWithin(20, SECONDS); |
| 186 | 190 | Host host1 = TestUtils.findHost(cluster, 1); |
| 187 | - host1.getReconnectionAttemptFuture().cancel(false); | |
| 191 | + loadBalancingPolicy.setDistance(TestUtils.findHost(cluster, 1), HostDistance.IGNORED); | |
| 192 | + ListenableFuture<?> reconnectionAttemptFuture = host1.getReconnectionAttemptFuture(); | |
| 193 | + if (reconnectionAttemptFuture != null) | |
| 194 | + reconnectionAttemptFuture.cancel(false); | |
| 188 | 195 | |
| 189 | 196 | // Trigger a one-time reconnection attempt (this will fail) |
| 190 | 197 | host1.tryReconnectOnce(); |
| @@ -262,7 +269,7 @@ | ||
| 262 | 269 | Map<String, String> getCredentials() { |
| 263 | 270 | count.incrementAndGet(); |
| 264 | 271 | return ImmutableMap.of("username", username, |
| 265 | - "password", password); | |
| 272 | + "password", password); | |
| 266 | 273 | } |
| 267 | 274 | } |
| 268 | 275 | } |
| @@ -299,16 +306,31 @@ | ||
| 299 | 306 | } |
| 300 | 307 | |
| 301 | 308 | /** |
| 302 | - * A load balancing policy that can be "disabled" by having its query plan return no hosts. | |
| 309 | + * A load balancing policy that: | |
| 310 | + * - can be "disabled" by having its query plan return no hosts. | |
| 311 | + * - can be instructed to return a specific distance for some hosts. | |
| 303 | 312 | */ |
| 304 | 313 | public static class TogglabePolicy extends DelegatingLoadBalancingPolicy { |
| 305 | 314 | |
| 306 | 315 | volatile boolean returnEmptyQueryPlan; |
| 316 | + final ConcurrentMap<Host, HostDistance> distances = new ConcurrentHashMap<Host, HostDistance>(); | |
| 307 | 317 | |
| 308 | 318 | public TogglabePolicy(LoadBalancingPolicy delegate) { |
| 309 | 319 | super(delegate); |
| 310 | 320 | } |
| 311 | 321 | |
| 322 | + @Override | |
| 323 | + public HostDistance distance(Host host) { | |
| 324 | + HostDistance distance = distances.get(host); | |
| 325 | + return (distance != null) | |
| 326 | + ? distance | |
| 327 | + : super.distance(host); | |
| 328 | + } | |
| 329 | + | |
| 330 | + public void setDistance(Host host, HostDistance distance) { | |
| 331 | + distances.put(host, distance); | |
| 332 | + } | |
| 333 | + | |
| 312 | 334 | @Override |
| 313 | 335 | public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { |
| 314 | 336 | if (returnEmptyQueryPlan) |