Elffuss BraidHub

kikocis/java-driver public

JAVA-1277: Expose AbstractSession.checkNotInEventLoop. (#741)

Changesetdc11f14cdcc4e4f231d260d474520296b65758359a47fcab5d6fe4bf3c1c957f
AuthorAlexandre Dutra <adutra@users.noreply.github.com> · 2016-09-16 00:44 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-09-15 15:44 -07:00
Parents167c5c83cf3b

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -10,6 +10,7 @@
1010 - [improvement] JAVA-1275: Use Netty's default thread factory
1111 - [bug] JAVA-1285: QueryBuilder routing key auto-discovery should handle case-sensitive column names.
1212 - [bug] JAVA-1283: Don't cache failed query preparations in the mapper.
13+- [improvement] JAVA-1277: Expose AbstractSession.checkNotInEventLoop.
1314
1415
1516 ### 3.0.3
modified driver-core/src/main/java/com/datastax/driver/core/AbstractSession.java +28 −3
@@ -19,6 +19,7 @@
1919 import com.google.common.util.concurrent.Futures;
2020 import com.google.common.util.concurrent.ListenableFuture;
2121 import com.google.common.util.concurrent.Uninterruptibles;
22+import io.netty.util.concurrent.EventExecutor;
2223
2324 import java.nio.ByteBuffer;
2425 import java.util.Map;
@@ -31,6 +32,9 @@
3132 */
3233 public abstract class AbstractSession implements Session {
3334
35+ private static final boolean CHECK_IO_DEADLOCKS = SystemProperties.getBoolean(
36+ "com.datastax.driver.CHECK_IO_DEADLOCKS", true);
37+
3438 /**
3539 * {@inheritDoc}
3640 */
@@ -179,9 +183,30 @@
179183 /**
180184 * Checks that the current thread is not one of the Netty I/O threads used by the driver.
181185 * <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.
183196 */
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+ }
186211 }
187212 }
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +0 −21
@@ -28,7 +28,6 @@
2828 import com.google.common.collect.ImmutableList;
2929 import com.google.common.collect.Lists;
3030 import com.google.common.util.concurrent.*;
31-import io.netty.util.concurrent.EventExecutor;
3231 import org.slf4j.Logger;
3332 import org.slf4j.LoggerFactory;
3433
@@ -48,9 +47,6 @@
4847
4948 private static final Logger logger = LoggerFactory.getLogger(Session.class);
5049
51- private static final boolean CHECK_IO_DEADLOCKS = SystemProperties.getBoolean(
52- "com.datastax.driver.CHECK_IO_DEADLOCKS", true);
53-
5450 final Cluster cluster;
5551 final ConcurrentMap<Host, HostConnectionPool> pools;
5652 final HostConnectionPool.PoolState poolsState;
@@ -665,23 +661,6 @@
665661 }
666662 }
667663
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-
685664 private static class State implements Session.State {
686665
687666 private final SessionManager session;
modified driver-core/src/test/java/com/datastax/driver/core/AsyncQueryTest.java +82 −0
@@ -26,7 +26,9 @@
2626 import org.testng.annotations.DataProvider;
2727 import org.testng.annotations.Test;
2828
29+import java.nio.ByteBuffer;
2930 import java.util.Collection;
31+import java.util.Map;
3032 import java.util.concurrent.*;
3133
3234 import static org.assertj.core.api.Assertions.assertThat;
@@ -144,6 +146,25 @@
144146 fail("callback was not executed on io thread in 1000 attempts, something may be wrong.");
145147 }
146148
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+
147168 @Test(groups = "short")
148169 @CassandraVersion(major = 2.0, description = "Paging is not supported until 2.0")
149170 public void should_fail_when_auto_paging_on_io_thread() throws Exception {
@@ -207,4 +228,65 @@
207228 assertEquals(pools.size(), 1);
208229 return pools.iterator().next();
209230 }
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+ }
210292 }