kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5e29e40a6747fae1bf2f7424b233c1e108ca260da84f421d09660a5b78d5e4e3 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-05-05 15:53 -07:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-05-09 11:40 -07:00 |
| Parents | 378c87813a49 |
6 files changed
modified changelog/README.md +2 −0
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | ### 2.0.12.2 (in progress) |
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-1179: Request objects should be copied when executed. |
| 6 | +- [improvement] JAVA-1182: Throw error when synchronous call made on I/O thread. | |
| 6 | 7 | |
| 7 | 8 | |
| 8 | 9 | ### 2.0.12.1 |
| @@ -11,6 +12,7 @@ | ||
| 11 | 12 | - [improvement] JAVA-805: Document that metrics are null until Cluster is initialized. |
| 12 | 13 | - [bug] JAVA-1072: Ensure defunct connections are properly evicted from the pool. |
| 13 | 14 | |
| 15 | + | |
| 14 | 16 | ### 2.0.12 |
| 15 | 17 | |
| 16 | 18 | - [bug] JAVA-950: Fix Cluster.connect with a case-sensitive keyspace. |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractSession.java +12 −0
| @@ -51,6 +51,7 @@ | ||
| 51 | 51 | */ |
| 52 | 52 | @Override |
| 53 | 53 | public ResultSet execute(Statement statement) { |
| 54 | + checkNotInEventLoop(); | |
| 54 | 55 | return executeAsync(statement).getUninterruptibly(); |
| 55 | 56 | } |
| 56 | 57 | |
| @@ -75,6 +76,7 @@ | ||
| 75 | 76 | */ |
| 76 | 77 | @Override |
| 77 | 78 | public PreparedStatement prepare(String query) { |
| 79 | + checkNotInEventLoop(); | |
| 78 | 80 | try { |
| 79 | 81 | return Uninterruptibles.getUninterruptibly(prepareAsync(query)); |
| 80 | 82 | } catch (ExecutionException e) { |
| @@ -87,6 +89,7 @@ | ||
| 87 | 89 | */ |
| 88 | 90 | @Override |
| 89 | 91 | public PreparedStatement prepare(RegularStatement statement) { |
| 92 | + checkNotInEventLoop(); | |
| 90 | 93 | try { |
| 91 | 94 | return Uninterruptibles.getUninterruptibly(prepareAsync(statement)); |
| 92 | 95 | } catch (ExecutionException e) { |
| @@ -132,4 +135,13 @@ | ||
| 132 | 135 | Thread.currentThread().interrupt(); |
| 133 | 136 | } |
| 134 | 137 | } |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Checks that the current thread is not one of the Netty I/O threads used by the driver. | |
| 141 | + * <p/> | |
| 142 | + * This is called from the synchronous methods of this class to prevent deadlock issues. | |
| 143 | + */ | |
| 144 | + protected void checkNotInEventLoop() { | |
| 145 | + // This method is concrete only to avoid a breaking change. See subclass for the actual implementation. | |
| 146 | + } | |
| 135 | 147 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +1 −1
| @@ -679,7 +679,7 @@ | ||
| 679 | 679 | |
| 680 | 680 | public final Timer timer; |
| 681 | 681 | |
| 682 | - private final EventLoopGroup eventLoopGroup; | |
| 682 | + final EventLoopGroup eventLoopGroup; | |
| 683 | 683 | private final Class<? extends Channel> channelClass; |
| 684 | 684 | |
| 685 | 685 | private final ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +19 −0
| @@ -27,6 +27,7 @@ | ||
| 27 | 27 | import com.google.common.collect.ImmutableList; |
| 28 | 28 | import com.google.common.collect.Lists; |
| 29 | 29 | import com.google.common.util.concurrent.*; |
| 30 | +import io.netty.util.concurrent.EventExecutor; | |
| 30 | 31 | import org.slf4j.Logger; |
| 31 | 32 | import org.slf4j.LoggerFactory; |
| 32 | 33 | |
| @@ -46,6 +47,9 @@ | ||
| 46 | 47 | |
| 47 | 48 | private static final Logger logger = LoggerFactory.getLogger(Session.class); |
| 48 | 49 | |
| 50 | + private static final boolean CHECK_IO_DEADLOCKS = SystemProperties.getBoolean( | |
| 51 | + "com.datastax.driver.CHECK_IO_DEADLOCKS", true); | |
| 52 | + | |
| 49 | 53 | final Cluster cluster; |
| 50 | 54 | final ConcurrentMap<Host, HostConnectionPool> pools; |
| 51 | 55 | final HostConnectionPool.PoolState poolsState; |
| @@ -611,6 +615,21 @@ | ||
| 611 | 615 | } |
| 612 | 616 | } |
| 613 | 617 | |
| 618 | + @Override | |
| 619 | + protected void checkNotInEventLoop() { | |
| 620 | + if (!CHECK_IO_DEADLOCKS) | |
| 621 | + return; | |
| 622 | + for (EventExecutor executor : cluster.manager.connectionFactory.eventLoopGroup) { | |
| 623 | + if (executor.inEventLoop()) { | |
| 624 | + throw new IllegalStateException( | |
| 625 | + "Detected a synchronous Session call (execute() or prepare()) on an I/O thread, " + | |
| 626 | + "this can cause deadlocks or unpredictable behavior. " + | |
| 627 | + "Make sure your Future callbacks only use async calls, or schedule them on a " + | |
| 628 | + "different executor."); | |
| 629 | + } | |
| 630 | + } | |
| 631 | + } | |
| 632 | + | |
| 614 | 633 | private static class State implements Session.State { |
| 615 | 634 | |
| 616 | 635 | private final SessionManager session; |
modified driver-core/src/main/java/com/datastax/driver/core/SystemProperties.java +3 −3
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | /** |
| 22 | 22 | * Allows overriding internal settings via system properties. |
| 23 | 23 | * <p/> |
| 24 | - * Warning: this is meant for integration tests only, NOT FOR PRODUCTION USE. | |
| 24 | + * This is generally reserved for tests or "expert" usage. | |
| 25 | 25 | */ |
| 26 | 26 | class SystemProperties { |
| 27 | 27 | private static final Logger logger = LoggerFactory.getLogger(SystemProperties.class); |
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | } |
| 35 | 35 | try { |
| 36 | 36 | int value = Integer.parseInt(stringValue); |
| 37 | - logger.warn("{} is defined, using value {}", key, value); | |
| 37 | + logger.info("{} is defined, using value {}", key, value); | |
| 38 | 38 | return value; |
| 39 | 39 | } catch (NumberFormatException e) { |
| 40 | 40 | logger.warn("{} is defined but could not parse value {}, using default value {}", key, stringValue, defaultValue); |
| @@ -50,7 +50,7 @@ | ||
| 50 | 50 | } |
| 51 | 51 | try { |
| 52 | 52 | boolean value = Boolean.parseBoolean(stringValue); |
| 53 | - logger.warn("{} is defined, using value {}", key, value); | |
| 53 | + logger.info("{} is defined, using value {}", key, value); | |
| 54 | 54 | return value; |
| 55 | 55 | } catch (NumberFormatException e) { |
| 56 | 56 | logger.warn("{} is defined but could not parse value {}, using default value {}", key, stringValue, defaultValue); |
modified driver-core/src/test/java/com/datastax/driver/core/AsyncQueryTest.java +22 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 19 | 19 | import com.google.common.base.Function; |
| 20 | +import com.google.common.base.Throwables; | |
| 20 | 21 | import com.google.common.collect.Lists; |
| 21 | 22 | import com.google.common.util.concurrent.*; |
| 22 | 23 | import org.testng.annotations.DataProvider; |
| @@ -27,6 +28,7 @@ | ||
| 27 | 28 | import java.util.concurrent.*; |
| 28 | 29 | |
| 29 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.fail; | |
| 30 | 32 | import static org.testng.Assert.assertEquals; |
| 31 | 33 | import static org.testng.Assert.assertTrue; |
| 32 | 34 | |
| @@ -129,6 +131,26 @@ | ||
| 129 | 131 | } |
| 130 | 132 | } |
| 131 | 133 | |
| 134 | + @Test(groups = "short") | |
| 135 | + public void should_fail_when_synchronous_call_on_io_thread() throws Exception { | |
| 136 | + ResultSetFuture f = session.executeAsync("select release_version from system.local"); | |
| 137 | + ListenableFuture<Void> f2 = Futures.transform(f, new Function<ResultSet, Void>() { | |
| 138 | + @Override | |
| 139 | + public Void apply(ResultSet input) { | |
| 140 | + session.execute("select release_version from system.local"); | |
| 141 | + return null; | |
| 142 | + } | |
| 143 | + }); | |
| 144 | + try { | |
| 145 | + f2.get(); | |
| 146 | + fail("Expected a failed future"); | |
| 147 | + } catch (Exception e) { | |
| 148 | + assertThat(Throwables.getRootCause(e)) | |
| 149 | + .isInstanceOf(IllegalStateException.class) | |
| 150 | + .hasMessageContaining("Detected a synchronous Session call"); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + | |
| 132 | 154 | private ListenableFuture<Integer> connectAndQuery(String keyspace, Executor executor) { |
| 133 | 155 | ListenableFuture<Session> sessionFuture = cluster.connectAsync(keyspace); |
| 134 | 156 | ListenableFuture<ResultSet> queryFuture = Futures.transform(sessionFuture, new AsyncFunction<Session, ResultSet>() { |