kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f66247e902a47584991b196af5001c759abf7dbaeb177625f42a51d2ae2463e1 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-05-13 14:30 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-05-13 14:30 +02:00 |
| Parents | 776dc2ff107a, 038cdb5be8aa |
7 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -36,6 +36,7 @@ | ||
| 36 | 36 | executions (JAVA-723) |
| 37 | 37 | - [bug] Fix buffer leaks when compression is enabled (JAVA-734). |
| 38 | 38 | - [improvement] Use Netty's pooled ByteBufAllocator by default (JAVA-756) |
| 39 | +- [improvement] Expose "unsafe" paging state API (JAVA-759) | |
| 39 | 40 | |
| 40 | 41 | |
| 41 | 42 | 2.1.5: |
modified driver-core/src/main/java/com/datastax/driver/core/ExecutionInfo.java +18 −0
| @@ -18,6 +18,8 @@ | ||
| 18 | 18 | import java.nio.ByteBuffer; |
| 19 | 19 | import java.util.List; |
| 20 | 20 | |
| 21 | +import com.datastax.driver.core.utils.Bytes; | |
| 22 | + | |
| 21 | 23 | /** |
| 22 | 24 | * Basic information on the execution of a query. |
| 23 | 25 | */ |
| @@ -145,6 +147,22 @@ | ||
| 145 | 147 | return new PagingState(this.pagingState, this.statement, this.protocolVersion); |
| 146 | 148 | } |
| 147 | 149 | |
| 150 | + /** | |
| 151 | + * Returns the "raw" paging state of the query. | |
| 152 | + * | |
| 153 | + * Contrary to {@link #getPagingState()}, there will be no validation when | |
| 154 | + * this is later reinjected into a statement. | |
| 155 | + * | |
| 156 | + * @return the paging state or null if there is no next page. | |
| 157 | + * | |
| 158 | + * @see Statement#setPagingStateUnsafe(byte[]) | |
| 159 | + */ | |
| 160 | + public byte[] getPagingStateUnsafe() { | |
| 161 | + if (this.pagingState == null) | |
| 162 | + return null; | |
| 163 | + return Bytes.getArray(this.pagingState); | |
| 164 | + } | |
| 165 | + | |
| 148 | 166 | /** |
| 149 | 167 | * Whether the cluster had reached schema agreement after the execution of this query. |
| 150 | 168 | * |
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +36 −3
| @@ -308,8 +308,9 @@ | ||
| 308 | 308 | * This will cause the next execution of this statement to fetch results from a given |
| 309 | 309 | * page, rather than restarting from the beginning. |
| 310 | 310 | * <p> |
| 311 | - * You get the paging state from a previous execution of the statement. This is typically | |
| 312 | - * used to iterate in a "stateless" manner (e.g. across HTTP requests): | |
| 311 | + * You get the paging state from a previous execution of the statement (see | |
| 312 | + * {@link ExecutionInfo#getPagingState()}. | |
| 313 | + * This is typically used to iterate in a "stateless" manner (e.g. across HTTP requests): | |
| 313 | 314 | * <pre> |
| 314 | 315 | * {@code |
| 315 | 316 | * Statement st = new SimpleStatement("your query"); |
| @@ -337,9 +338,17 @@ | ||
| 337 | 338 | * } |
| 338 | 339 | * </pre> |
| 339 | 340 | * <p> |
| 340 | - * Note that the paging state can only be reused between perfectly identical statements | |
| 341 | + * The paging state can only be reused between perfectly identical statements | |
| 341 | 342 | * (same query string, same bound parameters). Altering the contents of the paging state |
| 342 | 343 | * or trying to set it on a different statement will cause this method to fail. |
| 344 | + * <p> | |
| 345 | + * Note that, due to internal implementation details, the paging state is not portable | |
| 346 | + * across native protocol versions (see the | |
| 347 | + * <a href="http://datastax.github.io/java-driver/features/native_protocol">online documentation</a> | |
| 348 | + * for more explanations about the native protocol). | |
| 349 | + * This means that {@code PagingState} instances generated with an old version won't work | |
| 350 | + * with a higher version. If that is a problem for you, consider using the "unsafe" API (see | |
| 351 | + * {@link #setPagingStateUnsafe(byte[])}). | |
| 343 | 352 | * |
| 344 | 353 | * @param pagingState the paging state to set, or {@code null} to remove any state that was |
| 345 | 354 | * previously set on this statement. |
| @@ -364,6 +373,30 @@ | ||
| 364 | 373 | return this; |
| 365 | 374 | } |
| 366 | 375 | |
| 376 | + /** | |
| 377 | + * Sets the paging state. | |
| 378 | + * <p> | |
| 379 | + * Contrary to {@link #setPagingState(PagingState)}, this method takes the "raw" form of the | |
| 380 | + * paging state (previously extracted with {@link ExecutionInfo#getPagingStateUnsafe()}. | |
| 381 | + * It won't validate that this statement matches the one that the paging state was extracted from. | |
| 382 | + * If the paging state was altered in any way, you will get unpredictable behavior from | |
| 383 | + * Cassandra (ranging from wrong results to a query failure). If you decide to use this variant, | |
| 384 | + * it is strongly recommended to add your own validation (for example, signing the raw state with | |
| 385 | + * a private key). | |
| 386 | + * | |
| 387 | + * @param pagingState the paging state to set, or {@code null} to remove any state that was | |
| 388 | + * previously set on this statement. | |
| 389 | + * @return this {@code Statement} object. | |
| 390 | + */ | |
| 391 | + public Statement setPagingStateUnsafe(byte[] pagingState) { | |
| 392 | + if (pagingState == null) { | |
| 393 | + this.pagingState = null; | |
| 394 | + } else { | |
| 395 | + this.pagingState = ByteBuffer.wrap(pagingState); | |
| 396 | + } | |
| 397 | + return this; | |
| 398 | + } | |
| 399 | + | |
| 367 | 400 | ByteBuffer getPagingState() { |
| 368 | 401 | return pagingState; |
| 369 | 402 | } |
modified driver-core/src/test/java/com/datastax/driver/core/PagingStateTest.java +21 −0
| @@ -209,5 +209,26 @@ | ||
| 209 | 209 | batch.setPagingState(emptyStatement); |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | + /** | |
| 213 | + * Validates that the "unsafe" paging state can be reused with the same Statement. | |
| 214 | + * | |
| 215 | + * @test_category paging | |
| 216 | + * @expected_result {@link ResultSet} from the query with the provided raw paging state starts from the | |
| 217 | + * subsequent row from the first query. | |
| 218 | + */ | |
| 219 | + @Test(groups = "short") | |
| 220 | + public void should_complete_when_using_unsafe_paging_state() { | |
| 221 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 222 | + ResultSet result = session.execute(st.setFetchSize(20)); | |
| 223 | + int pageSize = result.getAvailableWithoutFetching(); | |
| 224 | + byte[] savedPagingState = result.getExecutionInfo().getPagingStateUnsafe(); | |
| 225 | + | |
| 226 | + st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 227 | + result = session.execute(st.setFetchSize(20).setPagingStateUnsafe(savedPagingState)); | |
| 228 | + | |
| 229 | + //We have the result starting from the next page we stopped | |
| 230 | + assertThat(result.one().getInt("v")).isEqualTo(pageSize); | |
| 231 | + } | |
| 232 | + | |
| 212 | 233 | } |
| 213 | 234 |
modified driver-core/src/test/java/com/datastax/driver/core/RecommissionedNodeTest.java +2 −0
| @@ -110,6 +110,7 @@ | ||
| 110 | 110 | |
| 111 | 111 | // Node 1 should now be DOWN with no reconnection attempt |
| 112 | 112 | assertThat(mainCluster).host(1) |
| 113 | + .goesDownWithin(10, TimeUnit.SECONDS) | |
| 113 | 114 | .hasState(DOWN) |
| 114 | 115 | .isNotReconnectingFromDown(); |
| 115 | 116 | } |
| @@ -133,6 +134,7 @@ | ||
| 133 | 134 | |
| 134 | 135 | // Node 1 should now be DOWN with no reconnection attempt |
| 135 | 136 | assertThat(mainCluster).host(1) |
| 137 | + .goesDownWithin(10, TimeUnit.SECONDS) | |
| 136 | 138 | .hasState(DOWN) |
| 137 | 139 | .isNotReconnectingFromDown(); |
| 138 | 140 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +1 −0
| @@ -262,6 +262,7 @@ | ||
| 262 | 262 | .withReconnectionPolicy(new ConstantReconnectionPolicy(5000)) |
| 263 | 263 | .withLoadBalancingPolicy(loadBalancingPolicy) |
| 264 | 264 | .withSocketOptions(socketOptions) |
| 265 | + .withProtocolVersion(TestUtils.getDesiredProtocolVersion()) | |
| 265 | 266 | .build(); |
| 266 | 267 | // Create two sessions to have multiple pools |
| 267 | 268 | cluster.connect(); |
modified features/paging/README.md +43 −0
| @@ -173,3 +173,46 @@ | ||
| 173 | 173 | |
| 174 | 174 | [result_set]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ResultSet.html |
| 175 | 175 | [paging_state]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/PagingState.html |
| 176 | + | |
| 177 | + | |
| 178 | +Due to internal implementation details, `PagingState` instances are not | |
| 179 | +portable across [native protocol](../native_protocol/) versions. This | |
| 180 | +could become a problem in the following scenario: | |
| 181 | + | |
| 182 | +* you're using the driver 2.0.x and Cassandra 2.0.x, and therefore | |
| 183 | + native protocol v2; | |
| 184 | +* a user bookmarks a link to your web service that contains a serialized | |
| 185 | + paging state; | |
| 186 | +* you upgrade your server stack to use the driver 2.1.x and Cassandra | |
| 187 | + 2.1.x, so you're now using protocol v3; | |
| 188 | +* the user tries to reload their bookmark, but the paging state was | |
| 189 | + serialized with protocol v2, so trying to reuse it will fail. | |
| 190 | + | |
| 191 | +If this is not acceptable for you, you might want to consider the unsafe | |
| 192 | +API described in the next section. | |
| 193 | + | |
| 194 | +#### Unsafe API | |
| 195 | + | |
| 196 | +As an alternative to the standard API, there are two methods that | |
| 197 | +manipulate a raw `byte[]` instead of a `PagingState` object: | |
| 198 | + | |
| 199 | +* [ExecutionInfo#getPagingStateUnsafe()][gpsu] | |
| 200 | +* [Statement#setPagingStateUnsafe(byte[])][spsu] | |
| 201 | + | |
| 202 | +These low-level methods perform no validation on their arguments; | |
| 203 | +therefore nothing protects you from reusing a paging state that was | |
| 204 | +generated from a different statement, or altered in any way. This could | |
| 205 | +result in sending a corrupt paging state to Cassandra, with | |
| 206 | +unpredictable consequences (ranging from wrong results to a query | |
| 207 | +failure). | |
| 208 | + | |
| 209 | +There are two situations where you might want to use the unsafe API: | |
| 210 | + | |
| 211 | +* you never expose the paging state to end users and you are confident | |
| 212 | + that it won't get altered; | |
| 213 | +* you want portability across protocol versions and/or you prefer | |
| 214 | + implementing your own validation logic (for example, signing the raw | |
| 215 | + state with a private key). | |
| 216 | + | |
| 217 | +[gpsu]: http://www.datastax.com/drivers/java/2.1/com/datastax/driver/core/ExecutionInfo.html#getPagingStateUnsafe() | |
| 218 | +[spsu]: http://www.datastax.com/drivers/java/2.1/com/datastax/driver/core/Statement.html#setPagingStateUnsafe(byte[]) |