kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 3168d4757d5ac7e1a1ab8b97cad9b129f07f6690eae83c7945cc54b3174eb1de |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-11-05 18:37 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-11-05 18:37 +01:00 |
| Parents | 659a94ae9fce, d49ddcef7ef2 |
11 files changed
modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +3 −1
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import java.util.concurrent.*; |
| 19 | 19 | import java.util.concurrent.atomic.AtomicReference; |
| 20 | 20 | |
| 21 | +import com.google.common.annotations.VisibleForTesting; | |
| 21 | 22 | import com.google.common.util.concurrent.ListenableFuture; |
| 22 | 23 | import com.google.common.util.concurrent.AbstractFuture; |
| 23 | 24 | import org.slf4j.Logger; |
| @@ -183,7 +184,8 @@ | ||
| 183 | 184 | } |
| 184 | 185 | |
| 185 | 186 | // The future that the handler exposes to its clients via currentAttempt |
| 186 | - private static class HandlerFuture extends AbstractFuture<Void> { | |
| 187 | + @VisibleForTesting | |
| 188 | + static class HandlerFuture extends AbstractFuture<Void> { | |
| 187 | 189 | // A future representing completion of the next task submitted to the executor |
| 188 | 190 | volatile ScheduledFuture<?> nextTry; |
| 189 | 191 |
modified driver-core/src/test/java/com/datastax/driver/core/AbstractReconnectionHandlerTest.java +26 −5
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | import com.google.common.util.concurrent.Futures; |
| 26 | 26 | import com.google.common.util.concurrent.ListenableFuture; |
| 27 | 27 | import com.google.common.util.concurrent.SettableFuture; |
| 28 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 28 | 29 | import org.slf4j.Logger; |
| 29 | 30 | import org.slf4j.LoggerFactory; |
| 30 | 31 | import org.testng.annotations.AfterMethod; |
| @@ -32,8 +33,12 @@ | ||
| 32 | 33 | import org.testng.annotations.Test; |
| 33 | 34 | |
| 34 | 35 | import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.mockito.Mockito.spy; | |
| 37 | +import static org.mockito.Mockito.timeout; | |
| 38 | +import static org.mockito.Mockito.verify; | |
| 35 | 39 | import static org.testng.Assert.fail; |
| 36 | 40 | |
| 41 | +import com.datastax.driver.core.AbstractReconnectionHandler.HandlerFuture; | |
| 37 | 42 | import com.datastax.driver.core.AbstractReconnectionHandlerTest.MockReconnectionWork.ReconnectBehavior; |
| 38 | 43 | import com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule; |
| 39 | 44 | |
| @@ -48,7 +53,7 @@ | ||
| 48 | 53 | |
| 49 | 54 | @BeforeMethod(groups = {"unit", "short"}) |
| 50 | 55 | public void setup() { |
| 51 | - executor = Executors.newScheduledThreadPool(2); | |
| 56 | + executor = spy(Executors.newScheduledThreadPool(2)); | |
| 52 | 57 | schedule = new MockReconnectionSchedule(); |
| 53 | 58 | work = new MockReconnectionWork(); |
| 54 | 59 | future.set(null); |
| @@ -133,6 +138,9 @@ | ||
| 133 | 138 | public void should_stop_if_cancelled_between_attempts() { |
| 134 | 139 | handler.start(); |
| 135 | 140 | |
| 141 | + // Wait for the initial schedule of a reconnect. | |
| 142 | + verify(executor, timeout(1000)).schedule(handler, 0, TimeUnit.MILLISECONDS); | |
| 143 | + | |
| 136 | 144 | // Force a failed reconnect. |
| 137 | 145 | schedule.tick(); |
| 138 | 146 | work.nextReconnect = ReconnectBehavior.THROW_EXCEPTION; |
| @@ -143,17 +151,30 @@ | ||
| 143 | 151 | schedule.delay = 3000; |
| 144 | 152 | schedule.tick(); |
| 145 | 153 | |
| 146 | - // At this point the reconnect should be scheduled but not executed yet, cancel the future. | |
| 154 | + // Ensure reconnect is scheduled (slight timing window after handling failed reconnect | |
| 155 | + // and scheduling next reconnect). | |
| 156 | + verify(executor, timeout(1000)).schedule(handler, schedule.delay, TimeUnit.MILLISECONDS); | |
| 157 | + | |
| 158 | + // Add a short delay to account for nextTry being assigned after schedule completes. | |
| 159 | + Uninterruptibles.sleepUninterruptibly(5, TimeUnit.MILLISECONDS); | |
| 147 | 160 | future.get().cancel(false); |
| 148 | 161 | |
| 149 | - // Should block until scheduler runs, detects cancel and exits. | |
| 162 | + // Should immediately return as the future was cancelled while the task was scheduled. | |
| 150 | 163 | waitForCompletion(); |
| 151 | 164 | |
| 152 | 165 | assertThat(work.success).isFalse(); |
| 153 | 166 | // Should have had 1 failed attempt, no second attempt since cancelled. |
| 154 | 167 | assertThat(work.tries).isEqualTo(1); |
| 155 | - // The future will be marked null once it is detected as cancelled. | |
| 156 | - assertThat(future.get()).isNull(); | |
| 168 | + | |
| 169 | + // The future will be marked cancelled and thus not executed. | |
| 170 | + ListenableFuture<?> currentAttempt = future.get(); | |
| 171 | + assertThat(currentAttempt).isInstanceOf(HandlerFuture.class); | |
| 172 | + HandlerFuture handlerFuture = (HandlerFuture)currentAttempt; | |
| 173 | + assertThat(handlerFuture.isCancelled()); | |
| 174 | + | |
| 175 | + // The next try should also be cancelled. | |
| 176 | + assertThat(handlerFuture.nextTry).isNotNull(); | |
| 177 | + assertThat(handlerFuture.nextTry.isCancelled()); | |
| 157 | 178 | } |
| 158 | 179 | |
| 159 | 180 | @Test(groups = "unit") |
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +15 −4
| @@ -506,19 +506,30 @@ | ||
| 506 | 506 | |
| 507 | 507 | private void clearSimpleKeyspace() { |
| 508 | 508 | if(keyspace != null) { |
| 509 | - // Temporarily extend read timeout to 1 minute to accommodate keyspaces with many tables. | |
| 510 | - // This should be more than enough although some tests create many tables, so dropping a keyspace | |
| 511 | - // could take as long as 12 seconds on restricted hardware. | |
| 509 | + logger.debug("Removing keyspace {}.", keyspace); | |
| 510 | + // Temporarily extend read timeout to 1 minute to accommodate dropping keyspaces and | |
| 511 | + // tables being slow, particularly in a CI environment. | |
| 512 | 512 | int currentTimeout = cluster.getConfiguration().getSocketOptions().getReadTimeoutMillis(); |
| 513 | 513 | cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(60000); |
| 514 | 514 | try { |
| 515 | + KeyspaceMetadata ksm = cluster.getMetadata().getKeyspace(keyspace); | |
| 516 | + if (ksm != null) { | |
| 517 | + // drop each table individually as this seems to be more dependable than dropping | |
| 518 | + // the entire keyspace at once if it has many tables. | |
| 519 | + if(ksm.getTables().size() > 10) { | |
| 520 | + for (TableMetadata table : ksm.getTables()) { | |
| 521 | + logger.debug("Dropping table {}.{}.", keyspace, table.getName()); | |
| 522 | + session.execute("DROP TABLE " + keyspace + "." + table.getName()); | |
| 523 | + } | |
| 524 | + } | |
| 525 | + } | |
| 526 | + logger.debug("Dropping keyspace {}.", keyspace); | |
| 515 | 527 | session.execute("DROP KEYSPACE " + keyspace); |
| 516 | 528 | } finally { |
| 517 | 529 | cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(currentTimeout); |
| 518 | 530 | } |
| 519 | 531 | } |
| 520 | 532 | } |
| 521 | - | |
| 522 | 533 | } |
| 523 | 534 | |
| 524 | 535 | public static class CCMCluster { |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterAssert.java +12 −13
| @@ -15,12 +15,13 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.google.common.collect.Iterators; | |
| 19 | -import org.assertj.core.api.AbstractAssert; | |
| 20 | - | |
| 21 | 18 | import java.util.Iterator; |
| 22 | 19 | import java.util.Set; |
| 23 | 20 | import java.util.TreeSet; |
| 21 | +import java.util.concurrent.TimeUnit; | |
| 22 | + | |
| 23 | +import com.google.common.collect.Iterators; | |
| 24 | +import org.assertj.core.api.AbstractAssert; | |
| 24 | 25 | |
| 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; |
| 26 | 27 | |
| @@ -47,19 +48,17 @@ | ||
| 47 | 48 | } |
| 48 | 49 | |
| 49 | 50 | public HostAssert host(int hostNumber) { |
| 50 | - // TODO at some point this won't work anymore if we have assertions that wait for a node to | |
| 51 | - // join the cluster, e.g. assertThat(cluster).node(3).comesUp(). | |
| 52 | - return new HostAssert( | |
| 53 | - TestUtils.findHost(actual, hostNumber), | |
| 54 | - actual); | |
| 51 | + // Wait for the node to be added if it's not already known. | |
| 52 | + // In 2.2+ C* does not send an added event until the node is ready so we wait a long time. | |
| 53 | + Host host = TestUtils.findOrWaitForHost(actual, hostNumber, | |
| 54 | + 60 + Cluster.NEW_NODE_DELAY_SECONDS, TimeUnit.SECONDS); | |
| 55 | + return new HostAssert(host, actual); | |
| 55 | 56 | } |
| 56 | 57 | |
| 57 | 58 | public HostAssert host(String hostAddress) { |
| 58 | - // TODO at some point this won't work anymore if we have assertions that wait for a node to | |
| 59 | - // join the cluster, e.g. assertThat(cluster).node(3).comesUp(). | |
| 60 | - return new HostAssert( | |
| 61 | - TestUtils.findHost(actual, hostAddress), | |
| 62 | - actual); | |
| 59 | + Host host = TestUtils.findOrWaitForHost(actual, hostAddress, | |
| 60 | + 60 + Cluster.NEW_NODE_DELAY_SECONDS, TimeUnit.SECONDS); | |
| 61 | + return new HostAssert(host, actual); | |
| 63 | 62 | } |
| 64 | 63 | |
| 65 | 64 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterInitTest.java +21 −8
| @@ -27,6 +27,7 @@ | ||
| 27 | 27 | import com.google.common.collect.ImmutableMap; |
| 28 | 28 | import com.google.common.collect.ImmutableSet; |
| 29 | 29 | import com.google.common.collect.Lists; |
| 30 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 30 | 31 | import org.scassandra.Scassandra; |
| 31 | 32 | import org.scassandra.http.client.PrimingClient; |
| 32 | 33 | import org.scassandra.http.client.PrimingRequest; |
| @@ -41,14 +42,13 @@ | ||
| 41 | 42 | |
| 42 | 43 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 43 | 44 | import com.datastax.driver.core.policies.ConstantReconnectionPolicy; |
| 44 | -import com.datastax.driver.core.utils.CassandraVersion; | |
| 45 | 45 | |
| 46 | -import static com.datastax.driver.core.Assertions.*; | |
| 46 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 47 | +import static com.datastax.driver.core.Assertions.fail; | |
| 47 | 48 | import static com.datastax.driver.core.FakeHost.Behavior.THROWING_CONNECT_TIMEOUTS; |
| 48 | -import static com.datastax.driver.core.Host.State.DOWN; | |
| 49 | -import static com.datastax.driver.core.Host.State.UP; | |
| 50 | 49 | import static com.datastax.driver.core.HostDistance.LOCAL; |
| 51 | 50 | import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions; |
| 51 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 52 | 52 | |
| 53 | 53 | public class ClusterInitTest { |
| 54 | 54 | private static final Logger logger = LoggerFactory.getLogger(ClusterInitTest.class); |
| @@ -58,7 +58,6 @@ | ||
| 58 | 58 | * causing timeouts, we want to ensure that the driver does not wait multiple times on the same host. |
| 59 | 59 | */ |
| 60 | 60 | @Test(groups = "short") |
| 61 | - @CassandraVersion(major=2.0, description = "Scassandra currently broken with protocol version 1.") | |
| 62 | 61 | public void should_handle_failing_or_missing_contact_points() throws UnknownHostException { |
| 63 | 62 | Cluster cluster = null; |
| 64 | 63 | Scassandra scassandra = null; |
| @@ -128,9 +127,20 @@ | ||
| 128 | 127 | verify(socketOptions, atLeast(6)).getKeepAlive(); |
| 129 | 128 | verify(socketOptions, atMost(7)).getKeepAlive(); |
| 130 | 129 | |
| 131 | - assertThat(cluster).host(realHostAddress).isNotNull().hasState(UP); | |
| 130 | + assertThat(cluster).host(realHostAddress).isNotNull().isUp(); | |
| 131 | + // It is likely but not guaranteed that a host is marked down at this point. | |
| 132 | + // It should eventually be marked down as Cluster.Manager.triggerOnDown should be | |
| 133 | + // called and submit a task that marks the host down. | |
| 132 | 134 | for (FakeHost failingHost : failingHosts) { |
| 133 | - assertThat(cluster).host(failingHost.address).hasState(DOWN); | |
| 135 | + assertThat(cluster).host(failingHost.address).goesDownWithin(10, TimeUnit.SECONDS); | |
| 136 | + Host host = TestUtils.findHost(cluster, failingHost.address); | |
| 137 | + // There is a possible race here in that the host is marked down in a separate Executor in onDown | |
| 138 | + // and then starts a periodic reconnection attempt shortly after. Since setDown is called before | |
| 139 | + // startPeriodicReconnectionAttempt, we add a slight delay here if the future isn't set yet. | |
| 140 | + if(host.getReconnectionAttemptFuture() == null || host.getReconnectionAttemptFuture().isDone()) { | |
| 141 | + logger.warn("Periodic Reconnection Attempt hasn't started yet for {}, waiting 1 second and then checking.", host); | |
| 142 | + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); | |
| 143 | + } | |
| 134 | 144 | assertThat(cluster).host(failingHost.address).isReconnectingFromDown(); |
| 135 | 145 | } |
| 136 | 146 | assertThat(cluster).host(missingHostAddress).isNull(); |
| @@ -169,7 +179,9 @@ | ||
| 169 | 179 | try { |
| 170 | 180 | cluster.init(); |
| 171 | 181 | } finally { |
| 172 | - assertThat(reconnectionPolicy.count.get()).isEqualTo(0); | |
| 182 | + // We expect a nextDelay invocation from the ConvictionPolicy for each host, but that will | |
| 183 | + // not trigger a reconnection. | |
| 184 | + assertThat(reconnectionPolicy.count.get()).isEqualTo(2); | |
| 173 | 185 | for (FakeHost fakeHost : hosts) { |
| 174 | 186 | fakeHost.stop(); |
| 175 | 187 | } |
| @@ -189,6 +201,7 @@ | ||
| 189 | 201 | public void should_be_able_to_close_cluster_that_never_successfully_connected() throws Exception { |
| 190 | 202 | Cluster cluster = Cluster.builder() |
| 191 | 203 | .addContactPointsWithPorts(Collections.singleton(new InetSocketAddress("127.0.0.1", 65534))) |
| 204 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 192 | 205 | .build(); |
| 193 | 206 | try { |
| 194 | 207 | cluster.connect(); |
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +0 −22
| @@ -120,26 +120,4 @@ | ||
| 120 | 120 | fail(actual + " did not go down within " + duration + " " + unit); |
| 121 | 121 | return this; |
| 122 | 122 | } |
| 123 | - | |
| 124 | - public static class StateListenerBase implements StateListener { | |
| 125 | - @Override | |
| 126 | - public void onAdd(Host host) { | |
| 127 | - } | |
| 128 | - | |
| 129 | - @Override | |
| 130 | - public void onUp(Host host) { | |
| 131 | - } | |
| 132 | - | |
| 133 | - @Override | |
| 134 | - public void onSuspected(Host host) { | |
| 135 | - } | |
| 136 | - | |
| 137 | - @Override | |
| 138 | - public void onDown(Host host) { | |
| 139 | - } | |
| 140 | - | |
| 141 | - @Override | |
| 142 | - public void onRemove(Host host) { | |
| 143 | - } | |
| 144 | - } | |
| 145 | 123 | } |
modified driver-core/src/test/java/com/datastax/driver/core/QueryOptionsTest.java +2 −1
| @@ -66,7 +66,8 @@ | ||
| 66 | 66 | public void validatePrepared(boolean expectAll) { |
| 67 | 67 | // Prepare the statement |
| 68 | 68 | String query = "select sansa_stark from the_known_world"; |
| 69 | - session.prepare(query); | |
| 69 | + PreparedStatement statement = session.prepare(query); | |
| 70 | + assertThat(cluster.manager.preparedQueries).containsValue(statement); | |
| 70 | 71 | |
| 71 | 72 | // Ensure prepared properly based on expectation. |
| 72 | 73 | List<PreparedStatementPreparation> preparationOne = scassandra.retrievePreparedStatementPreparations(1); |
modified driver-core/src/test/java/com/datastax/driver/core/RefreshConnectedHostTest.java +2 −0
| @@ -78,6 +78,8 @@ | ||
| 78 | 78 | assertThat(cluster).host(2) |
| 79 | 79 | .hasState(State.UP) |
| 80 | 80 | .isAtDistance(HostDistance.LOCAL); |
| 81 | + | |
| 82 | + // Ensure that the host is added to the Cluster. | |
| 81 | 83 | assertThat(cluster).host(3) |
| 82 | 84 | .comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS+1, SECONDS) |
| 83 | 85 | .isAtDistance(HostDistance.IGNORED); |
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +1 −1
| @@ -290,7 +290,7 @@ | ||
| 290 | 290 | startLatch.countDown(); |
| 291 | 291 | |
| 292 | 292 | executor.shutdown(); |
| 293 | - boolean normalShutdown = executor.awaitTermination(1, TimeUnit.SECONDS); | |
| 293 | + boolean normalShutdown = executor.awaitTermination(5, TimeUnit.SECONDS); | |
| 294 | 294 | assertTrue(normalShutdown); |
| 295 | 295 | |
| 296 | 296 | // The deadlock occurred here before JAVA-418 |
added driver-core/src/test/java/com/datastax/driver/core/StateListenerBase.java +40 −0
| @@ -0,0 +1,40 @@ | ||
| 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 | +public class StateListenerBase implements Host.StateListener { | |
| 19 | + @Override | |
| 20 | + public void onAdd(Host host) { | |
| 21 | + } | |
| 22 | + | |
| 23 | + @Override | |
| 24 | + public void onUp(Host host) { | |
| 25 | + } | |
| 26 | + | |
| 27 | + @Override | |
| 28 | + public void onSuspected(Host host) { | |
| 29 | + | |
| 30 | + } | |
| 31 | + | |
| 32 | + @Override | |
| 33 | + public void onDown(Host host) { | |
| 34 | + } | |
| 35 | + | |
| 36 | + @Override | |
| 37 | + public void onRemove(Host host) { | |
| 38 | + } | |
| 39 | + | |
| 40 | +} | |
| No newline at the end of the file |
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +33 −0
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | import java.net.ServerSocket; |
| 24 | 24 | import java.nio.ByteBuffer; |
| 25 | 25 | import java.util.*; |
| 26 | +import java.util.concurrent.CountDownLatch; | |
| 26 | 27 | import java.util.concurrent.TimeUnit; |
| 27 | 28 | |
| 28 | 29 | import com.google.common.util.concurrent.Futures; |
| @@ -384,6 +385,38 @@ | ||
| 384 | 385 | } |
| 385 | 386 | } |
| 386 | 387 | |
| 388 | + public static Host findOrWaitForHost(Cluster cluster, int node, long duration, TimeUnit unit) { | |
| 389 | + return findOrWaitForHost(cluster, CCMBridge.ipOfNode(node), duration, unit); | |
| 390 | + } | |
| 391 | + | |
| 392 | + public static Host findOrWaitForHost(final Cluster cluster, final String address, long duration, TimeUnit unit) { | |
| 393 | + Host host = findHost(cluster, address); | |
| 394 | + if(host == null) { | |
| 395 | + final CountDownLatch addSignal = new CountDownLatch(1); | |
| 396 | + Host.StateListener addListener = new StateListenerBase() { | |
| 397 | + @Override | |
| 398 | + public void onAdd(Host host) { | |
| 399 | + if(host.getAddress().getHostAddress().equals(address)) { | |
| 400 | + // for a new node, because of this we also listen for add events. | |
| 401 | + addSignal.countDown(); | |
| 402 | + } | |
| 403 | + } | |
| 404 | + }; | |
| 405 | + cluster.register(addListener); | |
| 406 | + try { | |
| 407 | + // Wait until an add event occurs or we timeout. | |
| 408 | + if (addSignal.await(duration, unit)) { | |
| 409 | + host = findHost(cluster, address); | |
| 410 | + } | |
| 411 | + } catch (InterruptedException e) { | |
| 412 | + return null; | |
| 413 | + } finally { | |
| 414 | + cluster.unregister(addListener); | |
| 415 | + } | |
| 416 | + } | |
| 417 | + return host; | |
| 418 | + } | |
| 419 | + | |
| 387 | 420 | public static Host findHost(Cluster cluster, int hostNumber) { |
| 388 | 421 | return findHost(cluster, CCMBridge.ipOfNode(hostNumber)); |
| 389 | 422 | } |