kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | dc11f14cdcc4e4f231d260d474520296b65758359a47fcab5d6fe4bf3c1c957f |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2016-09-16 00:44 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-09-15 15:44 -07:00 |
| Parents | 167c5c83cf3b |
4 files changed
modified changelog/README.md +1 −0
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | - [improvement] JAVA-1275: Use Netty's default thread factory |
| 11 | 11 | - [bug] JAVA-1285: QueryBuilder routing key auto-discovery should handle case-sensitive column names. |
| 12 | 12 | - [bug] JAVA-1283: Don't cache failed query preparations in the mapper. |
| 13 | +- [improvement] JAVA-1277: Expose AbstractSession.checkNotInEventLoop. | |
| 13 | 14 | |
| 14 | 15 | |
| 15 | 16 | ### 3.0.3 |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractSession.java +28 −3
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import com.google.common.util.concurrent.Futures; |
| 20 | 20 | import com.google.common.util.concurrent.ListenableFuture; |
| 21 | 21 | import com.google.common.util.concurrent.Uninterruptibles; |
| 22 | +import io.netty.util.concurrent.EventExecutor; | |
| 22 | 23 | |
| 23 | 24 | import java.nio.ByteBuffer; |
| 24 | 25 | import java.util.Map; |
| @@ -31,6 +32,9 @@ | ||
| 31 | 32 | */ |
| 32 | 33 | public abstract class AbstractSession implements Session { |
| 33 | 34 | |
| 35 | + private static final boolean CHECK_IO_DEADLOCKS = SystemProperties.getBoolean( | |
| 36 | + "com.datastax.driver.CHECK_IO_DEADLOCKS", true); | |
| 37 | + | |
| 34 | 38 | /** |
| 35 | 39 | * {@inheritDoc} |
| 36 | 40 | */ |
| @@ -179,9 +183,30 @@ | ||
| 179 | 183 | /** |
| 180 | 184 | * Checks that the current thread is not one of the Netty I/O threads used by the driver. |
| 181 | 185 | * <p/> |
| 182 | - * This is called from the synchronous methods of this class to prevent deadlock issues. | |
| 186 | + * This method is called from all the synchronous methods of this class to prevent deadlock issues. | |
| 187 | + * <p/> | |
| 188 | + * User code extending this class can also call this method at any time to check if any code | |
| 189 | + * making blocking calls is being wrongly executed on a Netty I/O thread. | |
| 190 | + * <p/> | |
| 191 | + * Note that the check performed by this method has a small overhead; if | |
| 192 | + * that is an issue, checks can be disabled by setting the System property | |
| 193 | + * {@code com.datastax.driver.CHECK_IO_DEADLOCKS} to {@code false}. | |
| 194 | + * | |
| 195 | + * @throws IllegalStateException if the current thread is one of the Netty I/O thread used by the driver. | |
| 183 | 196 | */ |
| 184 | - protected void checkNotInEventLoop() { | |
| 185 | - // This method is concrete only to avoid a breaking change. See subclass for the actual implementation. | |
| 197 | + public void checkNotInEventLoop() { | |
| 198 | + Connection.Factory connectionFactory = getCluster().manager.connectionFactory; | |
| 199 | + if (!CHECK_IO_DEADLOCKS || connectionFactory == null) | |
| 200 | + return; | |
| 201 | + for (EventExecutor executor : connectionFactory.eventLoopGroup) { | |
| 202 | + if (executor.inEventLoop()) { | |
| 203 | + throw new IllegalStateException( | |
| 204 | + "Detected a synchronous call on an I/O thread, this can cause deadlocks or unpredictable " + | |
| 205 | + "behavior. This generally happens when a Future callback calls a synchronous Session " + | |
| 206 | + "method (execute() or prepare()), or iterates a result set past the fetch size " + | |
| 207 | + "(causing an internal synchronous fetch of the next page of results). " + | |
| 208 | + "Avoid this in your callbacks, or schedule them on a different executor."); | |
| 209 | + } | |
| 210 | + } | |
| 186 | 211 | } |
| 187 | 212 | } |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +0 −21
| @@ -28,7 +28,6 @@ | ||
| 28 | 28 | import com.google.common.collect.ImmutableList; |
| 29 | 29 | import com.google.common.collect.Lists; |
| 30 | 30 | import com.google.common.util.concurrent.*; |
| 31 | -import io.netty.util.concurrent.EventExecutor; | |
| 32 | 31 | import org.slf4j.Logger; |
| 33 | 32 | import org.slf4j.LoggerFactory; |
| 34 | 33 | |
| @@ -48,9 +47,6 @@ | ||
| 48 | 47 | |
| 49 | 48 | private static final Logger logger = LoggerFactory.getLogger(Session.class); |
| 50 | 49 | |
| 51 | - private static final boolean CHECK_IO_DEADLOCKS = SystemProperties.getBoolean( | |
| 52 | - "com.datastax.driver.CHECK_IO_DEADLOCKS", true); | |
| 53 | - | |
| 54 | 50 | final Cluster cluster; |
| 55 | 51 | final ConcurrentMap<Host, HostConnectionPool> pools; |
| 56 | 52 | final HostConnectionPool.PoolState poolsState; |
| @@ -665,23 +661,6 @@ | ||
| 665 | 661 | } |
| 666 | 662 | } |
| 667 | 663 | |
| 668 | - @Override | |
| 669 | - protected void checkNotInEventLoop() { | |
| 670 | - Connection.Factory connectionFactory = cluster.manager.connectionFactory; | |
| 671 | - if (!CHECK_IO_DEADLOCKS || connectionFactory == null) | |
| 672 | - return; | |
| 673 | - for (EventExecutor executor : connectionFactory.eventLoopGroup) { | |
| 674 | - if (executor.inEventLoop()) { | |
| 675 | - throw new IllegalStateException( | |
| 676 | - "Detected a synchronous call on an I/O thread, this can cause deadlocks or unpredictable " + | |
| 677 | - "behavior. This generally happens when a Future callback calls a synchronous Session " + | |
| 678 | - "method (execute() or prepare()), or iterates a result set past the fetch size " + | |
| 679 | - "(causing an internal synchronous fetch of the next page of results). " + | |
| 680 | - "Avoid this in your callbacks, or schedule them on a different executor."); | |
| 681 | - } | |
| 682 | - } | |
| 683 | - } | |
| 684 | - | |
| 685 | 664 | private static class State implements Session.State { |
| 686 | 665 | |
| 687 | 666 | private final SessionManager session; |
modified driver-core/src/test/java/com/datastax/driver/core/AsyncQueryTest.java +82 −0
| @@ -26,7 +26,9 @@ | ||
| 26 | 26 | import org.testng.annotations.DataProvider; |
| 27 | 27 | import org.testng.annotations.Test; |
| 28 | 28 | |
| 29 | +import java.nio.ByteBuffer; | |
| 29 | 30 | import java.util.Collection; |
| 31 | +import java.util.Map; | |
| 30 | 32 | import java.util.concurrent.*; |
| 31 | 33 | |
| 32 | 34 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -144,6 +146,25 @@ | ||
| 144 | 146 | fail("callback was not executed on io thread in 1000 attempts, something may be wrong."); |
| 145 | 147 | } |
| 146 | 148 | |
| 149 | + @Test(groups = "short") | |
| 150 | + public void should_fail_when_synchronous_call_on_io_thread_with_session_wrapper() throws Exception { | |
| 151 | + final Session session = new SessionWrapper(session()); | |
| 152 | + for (int i = 0; i < 1000; i++) { | |
| 153 | + ResultSetFuture f = session.executeAsync("select release_version from system.local"); | |
| 154 | + ListenableFuture<Thread> f2 = Futures.transform(f, new Function<ResultSet, Thread>() { | |
| 155 | + @Override | |
| 156 | + public Thread apply(ResultSet input) { | |
| 157 | + session.execute("select release_version from system.local"); | |
| 158 | + return Thread.currentThread(); | |
| 159 | + } | |
| 160 | + }); | |
| 161 | + if (isFailed(f2, IllegalStateException.class, "Detected a synchronous call on an I/O thread")) { | |
| 162 | + return; | |
| 163 | + } | |
| 164 | + } | |
| 165 | + fail("callback was not executed on io thread in 1000 attempts, something may be wrong."); | |
| 166 | + } | |
| 167 | + | |
| 147 | 168 | @Test(groups = "short") |
| 148 | 169 | @CassandraVersion(major = 2.0, description = "Paging is not supported until 2.0") |
| 149 | 170 | public void should_fail_when_auto_paging_on_io_thread() throws Exception { |
| @@ -207,4 +228,65 @@ | ||
| 207 | 228 | assertEquals(pools.size(), 1); |
| 208 | 229 | return pools.iterator().next(); |
| 209 | 230 | } |
| 231 | + | |
| 232 | + private static class SessionWrapper extends AbstractSession { | |
| 233 | + | |
| 234 | + private final Session session; | |
| 235 | + | |
| 236 | + public SessionWrapper(Session session) { | |
| 237 | + this.session = session; | |
| 238 | + } | |
| 239 | + | |
| 240 | + @Override | |
| 241 | + public ResultSet execute(Statement statement) { | |
| 242 | + // test a custom call to checkNotInEventLoop() | |
| 243 | + checkNotInEventLoop(); | |
| 244 | + return executeAsync(statement).getUninterruptibly(); | |
| 245 | + } | |
| 246 | + | |
| 247 | + @Override | |
| 248 | + public String getLoggedKeyspace() { | |
| 249 | + return session.getLoggedKeyspace(); | |
| 250 | + } | |
| 251 | + | |
| 252 | + @Override | |
| 253 | + public Session init() { | |
| 254 | + return session.init(); | |
| 255 | + } | |
| 256 | + | |
| 257 | + @Override | |
| 258 | + public ListenableFuture<Session> initAsync() { | |
| 259 | + return session.initAsync(); | |
| 260 | + } | |
| 261 | + | |
| 262 | + @Override | |
| 263 | + public ResultSetFuture executeAsync(Statement statement) { | |
| 264 | + return session.executeAsync(statement); | |
| 265 | + } | |
| 266 | + | |
| 267 | + @Override | |
| 268 | + public CloseFuture closeAsync() { | |
| 269 | + return session.closeAsync(); | |
| 270 | + } | |
| 271 | + | |
| 272 | + @Override | |
| 273 | + public boolean isClosed() { | |
| 274 | + return session.isClosed(); | |
| 275 | + } | |
| 276 | + | |
| 277 | + @Override | |
| 278 | + public Cluster getCluster() { | |
| 279 | + return session.getCluster(); | |
| 280 | + } | |
| 281 | + | |
| 282 | + @Override | |
| 283 | + public State getState() { | |
| 284 | + return session.getState(); | |
| 285 | + } | |
| 286 | + | |
| 287 | + @Override | |
| 288 | + protected ListenableFuture<PreparedStatement> prepareAsync(String query, Map<String, ByteBuffer> customPayload) { | |
| 289 | + return ((SessionManager) session).prepareAsync(query, customPayload); | |
| 290 | + } | |
| 291 | + } | |
| 210 | 292 | } |