Elffuss BraidHub

kikocis/java-driver public

Merge pull request #500 from datastax/java954

Don't trigger reconnection before initialization complete (JAVA-954).
Changesetd95f5db298a6631aae99c48c7310d46631a2481c713065e43940184435fbed1d
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-10-30 16:26 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-10-30 16:26 +01:00
Parents4f35a1d89427, 2df3e5bf8cdc

Browse the files at this changeset

7 files changed

modified changelog/README.md +1 −0
@@ -4,6 +4,7 @@
44
55 - [bug] JAVA-950: Fix Cluster.connect with a case-sensitive keyspace.
66 - [improvement] JAVA-920: Downgrade "error creating pool" message to WARN.
7+- [bug] JAVA-954: Don't trigger reconnection before initialization complete.
78
89
910 ### 2.0.11
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +1 −0
@@ -1415,6 +1415,7 @@
14151415 loadBalancingPolicy().onDown(host);
14161416 for (Host.StateListener listener : listeners)
14171417 listener.onDown(host);
1418+ startPeriodicReconnectionAttempt(host, true);
14181419 }
14191420
14201421 for (Host host : allHosts) {
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +0 −1
@@ -193,7 +193,6 @@
193193 // Mark the host down right away so that we don't try it again during the initialization process.
194194 // We don't call cluster.triggerOnDown because it does a bunch of other things we don't want to do here (notify LBP, etc.)
195195 host.setDown();
196- cluster.startPeriodicReconnectionAttempt(host, true);
197196 }
198197 } catch (ExecutionException e) {
199198 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 @@
129129 verify(socketOptions, atMost(7)).getKeepAlive();
130130
131131 assertThat(cluster).host(realHostAddress).isNotNull().hasState(UP);
132- for (FakeHost failingHost : failingHosts)
132+ for (FakeHost failingHost : failingHosts) {
133133 assertThat(cluster).host(failingHost.address).hasState(DOWN);
134+ assertThat(cluster).host(failingHost.address).isReconnectingFromDown();
135+ }
134136 assertThat(cluster).host(missingHostAddress).isNull();
135137
136138 } finally {
@@ -143,6 +145,39 @@
143145 }
144146 }
145147
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+
146181 /**
147182 * Validates that a Cluster that was never able to successfully establish connection a session can be closed
148183 * 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 @@
5757 return this;
5858 }
5959
60+ public HostAssert isReconnectingFromDown() {
61+ assertThat(actual.getReconnectionAttemptFuture() != null && !actual.getReconnectionAttemptFuture().isDone())
62+ .isTrue();
63+ return this;
64+ }
65+
6066 public HostAssert isNotReconnectingFromDown() {
6167 assertThat(actual.getReconnectionAttemptFuture() != null && !actual.getReconnectionAttemptFuture().isDone())
6268 .isFalse();
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +0 −31
@@ -312,37 +312,6 @@
312312 }
313313 }
314314
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-
346315 /**
347316 * A load balancing policy that:
348317 * - can be "disabled" by having its query plan return no hosts.