kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 2df3e5bf8cdc48c3217eddef099de00c7806d405460ee64cb9fcc5c11fd92ec1 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-10-19 10:49 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-10-30 16:24 +01:00 |
| Parents | 4f35a1d89427 |
7 files changed
modified changelog/README.md +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-950: Fix Cluster.connect with a case-sensitive keyspace. |
| 6 | 6 | - [improvement] JAVA-920: Downgrade "error creating pool" message to WARN. |
| 7 | +- [bug] JAVA-954: Don't trigger reconnection before initialization complete. | |
| 7 | 8 | |
| 8 | 9 | |
| 9 | 10 | ### 2.0.11 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +1 −0
| @@ -1415,6 +1415,7 @@ | ||
| 1415 | 1415 | loadBalancingPolicy().onDown(host); |
| 1416 | 1416 | for (Host.StateListener listener : listeners) |
| 1417 | 1417 | listener.onDown(host); |
| 1418 | + startPeriodicReconnectionAttempt(host, true); | |
| 1418 | 1419 | } |
| 1419 | 1420 | |
| 1420 | 1421 | for (Host host : allHosts) { |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +0 −1
| @@ -193,7 +193,6 @@ | ||
| 193 | 193 | // Mark the host down right away so that we don't try it again during the initialization process. |
| 194 | 194 | // We don't call cluster.triggerOnDown because it does a bunch of other things we don't want to do here (notify LBP, etc.) |
| 195 | 195 | host.setDown(); |
| 196 | - cluster.startPeriodicReconnectionAttempt(host, true); | |
| 197 | 196 | } |
| 198 | 197 | } catch (ExecutionException e) { |
| 199 | 198 | errors = logError(host, e.getCause(), errors, iter); |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterInitTest.java +36 −1
| @@ -129,8 +129,10 @@ | ||
| 129 | 129 | verify(socketOptions, atMost(7)).getKeepAlive(); |
| 130 | 130 | |
| 131 | 131 | assertThat(cluster).host(realHostAddress).isNotNull().hasState(UP); |
| 132 | - for (FakeHost failingHost : failingHosts) | |
| 132 | + for (FakeHost failingHost : failingHosts) { | |
| 133 | 133 | assertThat(cluster).host(failingHost.address).hasState(DOWN); |
| 134 | + assertThat(cluster).host(failingHost.address).isReconnectingFromDown(); | |
| 135 | + } | |
| 134 | 136 | assertThat(cluster).host(missingHostAddress).isNull(); |
| 135 | 137 | |
| 136 | 138 | } finally { |
| @@ -143,6 +145,39 @@ | ||
| 143 | 145 | } |
| 144 | 146 | } |
| 145 | 147 | |
| 148 | + /** | |
| 149 | + * Validates that if hosts are unreachable during Cluster initialization, no background reconnection to them | |
| 150 | + * is scheduled before the initialization is complete. | |
| 151 | + * | |
| 152 | + * @test_category connection | |
| 153 | + * @jira_ticket JAVA-954 | |
| 154 | + * @expected_result No reconnection scheduled. | |
| 155 | + */ | |
| 156 | + @Test(groups="short", expectedExceptions = NoHostAvailableException.class) | |
| 157 | + public void should_not_schedule_reconnections_before_init_complete() { | |
| 158 | + // Both contact points time out so we're sure we'll try both of them and init will never complete. | |
| 159 | + List<FakeHost> hosts = Lists.newArrayList( | |
| 160 | + new FakeHost(CCMBridge.ipOfNode(0), 9042, THROWING_CONNECT_TIMEOUTS), | |
| 161 | + new FakeHost(CCMBridge.ipOfNode(0), 9042, THROWING_CONNECT_TIMEOUTS)); | |
| 162 | + // Use a low reconnection interval and keep the default connect timeout (5 seconds). So if a reconnection was scheduled, | |
| 163 | + // we would see a call to the reconnection policy. | |
| 164 | + CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(100)); | |
| 165 | + Cluster cluster = Cluster.builder() | |
| 166 | + .addContactPoints(hosts.get(0).address, hosts.get(1).address) | |
| 167 | + .withReconnectionPolicy(reconnectionPolicy) | |
| 168 | + .build(); | |
| 169 | + try { | |
| 170 | + cluster.init(); | |
| 171 | + } finally { | |
| 172 | + assertThat(reconnectionPolicy.count.get()).isEqualTo(0); | |
| 173 | + for (FakeHost fakeHost : hosts) { | |
| 174 | + fakeHost.stop(); | |
| 175 | + } | |
| 176 | + } | |
| 177 | + // We don't test that reconnections are scheduled if init succeeds, but that's covered in | |
| 178 | + // should_handle_failing_or_missing_contact_points | |
| 179 | + } | |
| 180 | + | |
| 146 | 181 | /** |
| 147 | 182 | * Validates that a Cluster that was never able to successfully establish connection a session can be closed |
| 148 | 183 | * properly. |
added driver-core/src/test/java/com/datastax/driver/core/CountingReconnectionPolicy.java +51 −0
| @@ -0,0 +1,51 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.policies.ReconnectionPolicy; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * A reconnection policy that tracks how many times its schedule has been invoked. | |
| 24 | + */ | |
| 25 | +public class CountingReconnectionPolicy implements ReconnectionPolicy { | |
| 26 | + public final AtomicInteger count = new AtomicInteger(); | |
| 27 | + private final ReconnectionPolicy childPolicy; | |
| 28 | + | |
| 29 | + public CountingReconnectionPolicy(ReconnectionPolicy childPolicy) { | |
| 30 | + this.childPolicy = childPolicy; | |
| 31 | + } | |
| 32 | + | |
| 33 | + @Override | |
| 34 | + public ReconnectionSchedule newSchedule() { | |
| 35 | + return new CountingSchedule(childPolicy.newSchedule()); | |
| 36 | + } | |
| 37 | + | |
| 38 | + class CountingSchedule implements ReconnectionSchedule { | |
| 39 | + private final ReconnectionSchedule childSchedule; | |
| 40 | + | |
| 41 | + public CountingSchedule(ReconnectionSchedule childSchedule) { | |
| 42 | + this.childSchedule = childSchedule; | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + public long nextDelayMs() { | |
| 47 | + count.incrementAndGet(); | |
| 48 | + return childSchedule.nextDelayMs(); | |
| 49 | + } | |
| 50 | + } | |
| 51 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +6 −0
| @@ -57,6 +57,12 @@ | ||
| 57 | 57 | return this; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | + public HostAssert isReconnectingFromDown() { | |
| 61 | + assertThat(actual.getReconnectionAttemptFuture() != null && !actual.getReconnectionAttemptFuture().isDone()) | |
| 62 | + .isTrue(); | |
| 63 | + return this; | |
| 64 | + } | |
| 65 | + | |
| 60 | 66 | public HostAssert isNotReconnectingFromDown() { |
| 61 | 67 | assertThat(actual.getReconnectionAttemptFuture() != null && !actual.getReconnectionAttemptFuture().isDone()) |
| 62 | 68 | .isFalse(); |
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +0 −31
| @@ -312,37 +312,6 @@ | ||
| 312 | 312 | } |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | - /** | |
| 316 | - * A reconnection policy that tracks how many times its schedule has been invoked. | |
| 317 | - */ | |
| 318 | - public static class CountingReconnectionPolicy implements ReconnectionPolicy { | |
| 319 | - public final AtomicInteger count = new AtomicInteger(); | |
| 320 | - private final ReconnectionPolicy childPolicy; | |
| 321 | - | |
| 322 | - public CountingReconnectionPolicy(ReconnectionPolicy childPolicy) { | |
| 323 | - this.childPolicy = childPolicy; | |
| 324 | - } | |
| 325 | - | |
| 326 | - @Override | |
| 327 | - public ReconnectionSchedule newSchedule() { | |
| 328 | - return new CountingSchedule(childPolicy.newSchedule()); | |
| 329 | - } | |
| 330 | - | |
| 331 | - class CountingSchedule implements ReconnectionSchedule { | |
| 332 | - private final ReconnectionSchedule childSchedule; | |
| 333 | - | |
| 334 | - public CountingSchedule(ReconnectionSchedule childSchedule) { | |
| 335 | - this.childSchedule = childSchedule; | |
| 336 | - } | |
| 337 | - | |
| 338 | - @Override | |
| 339 | - public long nextDelayMs() { | |
| 340 | - count.incrementAndGet(); | |
| 341 | - return childSchedule.nextDelayMs(); | |
| 342 | - } | |
| 343 | - } | |
| 344 | - } | |
| 345 | - | |
| 346 | 315 | /** |
| 347 | 316 | * A load balancing policy that: |
| 348 | 317 | * - can be "disabled" by having its query plan return no hosts. |