Elffuss BraidHub

kikocis/java-driver public

Merge pull request #723 from datastax/java1261

JAVA-1261: Throw error when attempting to page in I/O thread
Changeset09c916afb89d4991d0fb9ebc9a4207cb982707ae8e4f8c97e824b8eb8815ae01
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-08-30 13:55 -07:00
CommitterGitHub <noreply@github.com> · 2016-08-30 13:55 -07:00
Parents8775f5e858c0, 14bec54723cf

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 +46 −17
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818 import com.datastax.driver.core.exceptions.InvalidQueryException;
19+import com.datastax.driver.core.utils.CassandraVersion;
1920 import com.google.common.base.Function;
2021 import com.google.common.base.Throwables;
2122 import com.google.common.collect.Lists;
@@ -48,9 +49,9 @@
4849 String keyspace = (String) objects[0];
4950 execute(
5051 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- );
52+ String.format("create table %s.foo(k int, v int, primary key (k, v))", keyspace));
53+ for (int v = 1; v <= 100; v++)
54+ execute(String.format("insert into %s.foo (k, v) values (1, %d)", keyspace, v));
5455 }
5556 }
5657
@@ -127,7 +128,6 @@
127128
128129 @Test(groups = "short")
129130 public void should_fail_when_synchronous_call_on_io_thread() throws Exception {
130- final Thread sameThread = Thread.currentThread();
131131 for (int i = 0; i < 1000; i++) {
132132 ResultSetFuture f = session().executeAsync("select release_version from system.local");
133133 ListenableFuture<Thread> f2 = Futures.transform(f, new Function<ResultSet, Thread>() {
@@ -137,26 +137,55 @@
137137 return Thread.currentThread();
138138 }
139139 });
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.");
140+ if (isFailed(f2, IllegalStateException.class, "Detected a synchronous call on an I/O thread")) {
141+ return;
142+ }
143+ }
144+ fail("callback was not executed on io thread in 1000 attempts, something may be wrong.");
145+ }
146+
147+ @Test(groups = "short")
148+ @CassandraVersion(major = 2.0, description = "Paging is not supported until 2.0")
149+ public void should_fail_when_auto_paging_on_io_thread() throws Exception {
150+ for (int i = 0; i < 1000; i++) {
151+ Statement statement = new SimpleStatement("select v from asyncquerytest.foo where k = 1");
152+ // Ensure results will be paged (there are 100 rows in the test table)
153+ statement.setFetchSize(10);
154+ ResultSetFuture f = session().executeAsync(statement);
155+ ListenableFuture<Thread> f2 = Futures.transform(f, new Function<ResultSet, Thread>() {
156+ @Override
157+ public Thread apply(ResultSet rs) {
158+ rs.all(); // Consume the whole result set
159+ return Thread.currentThread();
149160 }
150- } catch (Exception e) {
151- assertThat(Throwables.getRootCause(e))
152- .isInstanceOf(IllegalStateException.class)
153- .hasMessageContaining("Detected a synchronous Session call");
161+ });
162+ if (isFailed(f2, IllegalStateException.class, "Detected a synchronous call on an I/O thread")) {
154163 return;
155164 }
156165 }
157166 fail("callback was not executed on io thread in 1000 attempts, something may be wrong.");
158167 }
159168
169+ private boolean isFailed(ListenableFuture<Thread> future, Class<?> expectedException, String expectedMessage) {
170+ try {
171+ Thread executedThread = future.get();
172+ if (executedThread != Thread.currentThread()) {
173+ fail("Expected a failed future, callback was executed on " + executedThread);
174+ } else {
175+ // Callback was invoked on the same thread, which indicates that the future completed
176+ // before the transform callback was registered. Try again to produce case where callback
177+ // is called on io thread.
178+ logger.warn("Future completed before transform callback registered, will try again.");
179+ }
180+ } catch (Exception e) {
181+ assertThat(Throwables.getRootCause(e))
182+ .isInstanceOf(expectedException)
183+ .hasMessageContaining(expectedMessage);
184+ return true;
185+ }
186+ return false;
187+ }
188+
160189 private ListenableFuture<Integer> connectAndQuery(String keyspace, Executor executor) {
161190 ListenableFuture<Session> sessionFuture = cluster().connectAsync(keyspace);
162191 ListenableFuture<ResultSet> queryFuture = Futures.transform(sessionFuture, new AsyncFunction<Session, ResultSet>() {