Elffuss BraidHub

kikocis/java-driver public

JAVA-784: LoadBalancingPolicy.distance() called before init().
Changesete8d1c653ef99c5bcbcee363ef6a630812f0c0dc4e8b842e899b9a162bccef940
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-08-04 23:12 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-08-14 18:58 +02:00
Parents75b0d9b26d32

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -36,6 +36,7 @@
3636 - [improvement] Customizable creation of netty timer (JAVA-853)
3737 - [bug] Avoid quadratic ring processing with invalid replication factors (JAVA-859)
3838 - [improvement] Debounce control connection queries (JAVA-657)
39+- [bug] LoadBalancingPolicy.distance() called before init() (JAVA-784)
3940
4041 Merged from 2.0.10_fixes branch:
4142
added driver-core/src/test/java/com/datastax/driver/core/EventDebouncerIntegrationTest.java +135 −0
@@ -0,0 +1,135 @@
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.Collection;
19+import java.util.concurrent.CountDownLatch;
20+import java.util.concurrent.CyclicBarrier;
21+import java.util.concurrent.TimeUnit;
22+
23+import org.testng.annotations.Test;
24+
25+import static org.assertj.core.api.Assertions.assertThat;
26+import static org.assertj.core.api.Fail.fail;
27+
28+import static com.datastax.driver.core.CCMBridge.ipOfNode;
29+
30+public class EventDebouncerIntegrationTest {
31+
32+ /**
33+ * Tests that DOWN, UP, REMOVE or ADD events will not be delivered to
34+ * load balancing policy nor host state listeners
35+ * before the cluster is fully initialized.
36+ *
37+ * @throws InterruptedException
38+ * @jira_ticket JAVA-784
39+ * @since 2.0.11
40+ */
41+ @Test(groups = "long")
42+ public void should_wait_until_load_balancing_policy_is_fully_initialized() throws InterruptedException {
43+ TestLoadBalancingPolicy policy = new TestLoadBalancingPolicy();
44+ CCMBridge ccm = CCMBridge.builder("main").withNodes(3).build();
45+ final Cluster cluster = new Cluster.Builder()
46+ .addContactPoints(ipOfNode(1))
47+ .withLoadBalancingPolicy(policy)
48+ .withQueryOptions(new QueryOptions()
49+ .setRefreshNodeIntervalMillis(0)
50+ .setRefreshNodeListIntervalMillis(0)
51+ .setRefreshSchemaIntervalMillis(0)
52+ ).build();
53+ try {
54+ new Thread() {
55+ @Override
56+ public void run() {
57+ cluster.init();
58+ }
59+ }.start();
60+ // stop cluster initialization in the middle of LBP initialization
61+ policy.stop();
62+ // generate a DOWN event - will not be delivered immediately
63+ // because the debouncers are not started
64+ // note: a graceful stop notify other nodes which send a topology change event to the driver right away
65+ // while forceStop kills the node so other nodes take much more time to detect node failure
66+ ccm.stop(3);
67+ ccm.waitForDown(3);
68+ // finish cluster initialization and deliver the DOWN event
69+ policy.proceed();
70+ assertThat(policy.onDownCalledBeforeInit).isFalse();
71+ assertThat(policy.onDownCalled()).isTrue();
72+ assertThat(policy.hosts).doesNotContain(TestUtils.findHost(cluster, 3));
73+ } finally {
74+ if (cluster != null)
75+ cluster.close();
76+ ccm.remove("main");
77+ }
78+ }
79+
80+ private class TestLoadBalancingPolicy extends SortingLoadBalancingPolicy {
81+
82+ CyclicBarrier stop = new CyclicBarrier(2);
83+
84+ CyclicBarrier proceed = new CyclicBarrier(2);
85+
86+ CountDownLatch onDownCalled = new CountDownLatch(1);
87+
88+ volatile boolean init = false;
89+
90+ volatile boolean onDownCalledBeforeInit = false;
91+
92+ @Override
93+ public void init(Cluster cluster, Collection<Host> hosts) {
94+ try {
95+ stop.await(1, TimeUnit.MINUTES);
96+ proceed.await(1, TimeUnit.MINUTES);
97+ } catch (Exception e) {
98+ fail(e.getMessage());
99+ }
100+ super.init(cluster, hosts);
101+ init = true;
102+ }
103+
104+ @Override
105+ public void onDown(Host host) {
106+ if (!init)
107+ onDownCalledBeforeInit = true;
108+ super.onDown(host);
109+ if (host.getAddress().toString().contains(ipOfNode(3)))
110+ onDownCalled.countDown();
111+ }
112+
113+ void stop() throws InterruptedException {
114+ try {
115+ stop.await(1, TimeUnit.MINUTES);
116+ } catch (Exception e) {
117+ fail(e.getMessage());
118+ }
119+ }
120+
121+ void proceed() throws InterruptedException {
122+ try {
123+ proceed.await(1, TimeUnit.MINUTES);
124+ } catch (Exception e) {
125+ fail(e.getMessage());
126+ }
127+ }
128+
129+ boolean onDownCalled() throws InterruptedException {
130+ return onDownCalled.await(1, TimeUnit.MINUTES);
131+ }
132+
133+ }
134+
135+}
modified driver-core/src/test/java/com/datastax/driver/core/SortingLoadBalancingPolicy.java +1 −1
@@ -31,7 +31,7 @@
3131 */
3232 public class SortingLoadBalancingPolicy implements LoadBalancingPolicy {
3333
34- private final SortedSet<Host> hosts = new ConcurrentSkipListSet<Host>(new Comparator<Host>() {
34+ final SortedSet<Host> hosts = new ConcurrentSkipListSet<Host>(new Comparator<Host>() {
3535 @Override
3636 public int compare(Host host1, Host host2) {
3737 byte[] address1 = host1.getAddress().getAddress();