kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 09c916afb89d4991d0fb9ebc9a4207cb982707ae8e4f8c97e824b8eb8815ae01 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-08-30 13:55 -07:00 |
| Committer | GitHub <noreply@github.com> · 2016-08-30 13:55 -07:00 |
| Parents | 8775f5e858c0, 14bec54723cf |
4 files changed
modified changelog/README.md +2 −0
| @@ -3,6 +3,8 @@ | ||
| 3 | 3 | ### 3.0.4 (in progress) |
| 4 | 4 | |
| 5 | 5 | - [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 | + | |
| 6 | 8 | |
| 7 | 9 | ### 3.0.3 |
| 8 | 10 |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +1 −0
| @@ -307,6 +307,7 @@ | ||
| 307 | 307 | // We need to know if there is more result, so fetch the next page and |
| 308 | 308 | // wait on it. |
| 309 | 309 | try { |
| 310 | + session.checkNotInEventLoop(); | |
| 310 | 311 | Uninterruptibles.getUninterruptibly(fetchMoreResults()); |
| 311 | 312 | } catch (ExecutionException e) { |
| 312 | 313 | throw DriverThrowables.propagateCause(e); |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +5 −4
| @@ -673,10 +673,11 @@ | ||
| 673 | 673 | for (EventExecutor executor : connectionFactory.eventLoopGroup) { |
| 674 | 674 | if (executor.inEventLoop()) { |
| 675 | 675 | 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."); | |
| 680 | 681 | } |
| 681 | 682 | } |
| 682 | 683 | } |
modified driver-core/src/test/java/com/datastax/driver/core/AsyncQueryTest.java +46 −17
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 19 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 19 | 20 | import com.google.common.base.Function; |
| 20 | 21 | import com.google.common.base.Throwables; |
| 21 | 22 | import com.google.common.collect.Lists; |
| @@ -48,9 +49,9 @@ | ||
| 48 | 49 | String keyspace = (String) objects[0]; |
| 49 | 50 | execute( |
| 50 | 51 | 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)); | |
| 54 | 55 | } |
| 55 | 56 | } |
| 56 | 57 | |
| @@ -127,7 +128,6 @@ | ||
| 127 | 128 | |
| 128 | 129 | @Test(groups = "short") |
| 129 | 130 | public void should_fail_when_synchronous_call_on_io_thread() throws Exception { |
| 130 | - final Thread sameThread = Thread.currentThread(); | |
| 131 | 131 | for (int i = 0; i < 1000; i++) { |
| 132 | 132 | ResultSetFuture f = session().executeAsync("select release_version from system.local"); |
| 133 | 133 | ListenableFuture<Thread> f2 = Futures.transform(f, new Function<ResultSet, Thread>() { |
| @@ -137,26 +137,55 @@ | ||
| 137 | 137 | return Thread.currentThread(); |
| 138 | 138 | } |
| 139 | 139 | }); |
| 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(); | |
| 149 | 160 | } |
| 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")) { | |
| 154 | 163 | return; |
| 155 | 164 | } |
| 156 | 165 | } |
| 157 | 166 | fail("callback was not executed on io thread in 1000 attempts, something may be wrong."); |
| 158 | 167 | } |
| 159 | 168 | |
| 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 | + | |
| 160 | 189 | private ListenableFuture<Integer> connectAndQuery(String keyspace, Executor executor) { |
| 161 | 190 | ListenableFuture<Session> sessionFuture = cluster().connectAsync(keyspace); |
| 162 | 191 | ListenableFuture<ResultSet> queryFuture = Futures.transform(sessionFuture, new AsyncFunction<Session, ResultSet>() { |