Elffuss BraidHub

kikocis/java-driver public

JAVA-1261: Throw error when attempting to page in I/O thread

Iterating a result set past the current page triggers a blocking call
for the next page. This should never be done in a Netty I/O thread (for
example by iterating in a future callback), because it can deadlock.

Similar to JAVA-1182, we throw an error so that client code can detect
this mistake early.
Changesetc2c55074dc9c533e8bec933026af605b2224d8184206de19fe505ff5af3a8c88
Authorolim7t <omichallat+github@gmail.com> · 2016-08-10 14:08 -07:00
Committerolim7t <omichallat+github@gmail.com> · 2016-08-30 13:54 -07:00
Parentsc064b2b20c54

Browse the files at this changeset

4 files changed

modified changelog/README.md +2 −0
@@ -3,6 +3,8 @@
33 ### 3.0.4 (in progress)
44
55 - [improvement] JAVA-1246: Driver swallows the real exception in a few cases
6+- [improvement] JAVA-1261: Throw error when attempting to page in I/O thread.
7+
68
79 ### 3.0.3
810
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +1 −0
@@ -307,6 +307,7 @@
307307 // We need to know if there is more result, so fetch the next page and
308308 // wait on it.
309309 try {
310+ session.checkNotInEventLoop();
310311 Uninterruptibles.getUninterruptibly(fetchMoreResults());
311312 } catch (ExecutionException e) {
312313 throw DriverThrowables.propagateCause(e);
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +5 −4
@@ -673,10 +673,11 @@
673673 for (EventExecutor executor : connectionFactory.eventLoopGroup) {
674674 if (executor.inEventLoop()) {
675675 throw new IllegalStateException(
676- "Detected a synchronous Session call (execute() or prepare()) on an I/O thread, " +
677- "this can cause deadlocks or unpredictable behavior. " +
678- "Make sure your Future callbacks only use async calls, or schedule them on a " +
679- "different executor.");
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.");
680681 }
681682 }
682683 }
modified driver-core/src/test/java/com/datastax/driver/core/AsyncQueryTest.java +44 −17
@@ -48,9 +48,9 @@
4848 String keyspace = (String) objects[0];
4949 execute(
5050 String.format("create keyspace %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}", keyspace),
51- String.format("create table %s.foo(k int primary key, v int)", keyspace),
52- String.format("insert into %s.foo (k, v) values (1, 1)", keyspace)
53- );
51+ String.format("create table %s.foo(k int, v int, primary key (k, v))", keyspace));
52+ for (int v = 0; v < 100; v++)
53+ execute(String.format("insert into %s.foo (k, v) values (1, %d)", keyspace, v));
5454 }
5555 }
5656
@@ -127,7 +127,6 @@
127127
128128 @Test(groups = "short")
129129 public void should_fail_when_synchronous_call_on_io_thread() throws Exception {
130- final Thread sameThread = Thread.currentThread();
131130 for (int i = 0; i < 1000; i++) {
132131 ResultSetFuture f = session().executeAsync("select release_version from system.local");
133132 ListenableFuture<Thread> f2 = Futures.transform(f, new Function<ResultSet, Thread>() {
@@ -137,26 +136,54 @@
137136 return Thread.currentThread();
138137 }
139138 });
140- try {
141- Thread executedThread = f2.get();
142- if(executedThread != sameThread) {
143- fail("Expected a failed future, callback was executed on " + executedThread);
144- } else {
145- // Callback was invoked on the same thread, which indicates that the future completed
146- // before the transform callback was registered. Try again to produce case where callback
147- // is called on io thread.
148- logger.warn("Future completed before transform callback registered, will try again.");
139+ if (isFailed(f2, IllegalStateException.class, "Detected a synchronous call on an I/O thread")) {
140+ return;
141+ }
142+ }
143+ fail("callback was not executed on io thread in 1000 attempts, something may be wrong.");
144+ }
145+
146+ @Test(groups = "short")
147+ public void should_fail_when_auto_paging_on_io_thread() throws Exception {
148+ for (int i = 0; i < 1000; i++) {
149+ Statement statement = new SimpleStatement("select v from asyncquerytest.foo where k = 1");
150+ // Ensure results will be paged (there are 100 rows in the test table)
151+ statement.setFetchSize(10);
152+ ResultSetFuture f = session().executeAsync(statement);
153+ ListenableFuture<Thread> f2 = Futures.transform(f, new Function<ResultSet, Thread>() {
154+ @Override
155+ public Thread apply(ResultSet rs) {
156+ rs.all(); // Consume the whole result set
157+ return Thread.currentThread();
149158 }
150- } catch (Exception e) {
151- assertThat(Throwables.getRootCause(e))
152- .isInstanceOf(IllegalStateException.class)
153- .hasMessageContaining("Detected a synchronous Session call");
159+ });
160+ if (isFailed(f2, IllegalStateException.class, "Detected a synchronous call on an I/O thread")) {
154161 return;
155162 }
156163 }
157164 fail("callback was not executed on io thread in 1000 attempts, something may be wrong.");
158165 }
159166
167+ private boolean isFailed(ListenableFuture<Thread> future, Class<?> expectedException, String expectedMessage) {
168+ try {
169+ Thread executedThread = future.get();
170+ if (executedThread != Thread.currentThread()) {
171+ fail("Expected a failed future, callback was executed on " + executedThread);
172+ } else {
173+ // Callback was invoked on the same thread, which indicates that the future completed
174+ // before the transform callback was registered. Try again to produce case where callback
175+ // is called on io thread.
176+ logger.warn("Future completed before transform callback registered, will try again.");
177+ }
178+ } catch (Exception e) {
179+ assertThat(Throwables.getRootCause(e))
180+ .isInstanceOf(expectedException)
181+ .hasMessageContaining(expectedMessage);
182+ return true;
183+ }
184+ return false;
185+ }
186+
160187 private ListenableFuture<Integer> connectAndQuery(String keyspace, Executor executor) {
161188 ListenableFuture<Session> sessionFuture = cluster().connectAsync(keyspace);
162189 ListenableFuture<ResultSet> queryFuture = Futures.transform(sessionFuture, new AsyncFunction<Session, ResultSet>() {