Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.0' into 2.1
Changesetf66247e902a47584991b196af5001c759abf7dbaeb177625f42a51d2ae2463e1
Authorolim7t <omichallat+github@gmail.com> · 2015-05-13 14:30 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-05-13 14:30 +02:00
Parents776dc2ff107a, 038cdb5be8aa

Browse the files at this changeset

7 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -36,6 +36,7 @@
3636 executions (JAVA-723)
3737 - [bug] Fix buffer leaks when compression is enabled (JAVA-734).
3838 - [improvement] Use Netty's pooled ByteBufAllocator by default (JAVA-756)
39+- [improvement] Expose "unsafe" paging state API (JAVA-759)
3940
4041
4142 2.1.5:
modified driver-core/src/main/java/com/datastax/driver/core/ExecutionInfo.java +18 −0
@@ -18,6 +18,8 @@
1818 import java.nio.ByteBuffer;
1919 import java.util.List;
2020
21+import com.datastax.driver.core.utils.Bytes;
22+
2123 /**
2224 * Basic information on the execution of a query.
2325 */
@@ -145,6 +147,22 @@
145147 return new PagingState(this.pagingState, this.statement, this.protocolVersion);
146148 }
147149
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+
148166 /**
149167 * Whether the cluster had reached schema agreement after the execution of this query.
150168 *
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +36 −3
@@ -308,8 +308,9 @@
308308 * This will cause the next execution of this statement to fetch results from a given
309309 * page, rather than restarting from the beginning.
310310 * <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):
313314 * <pre>
314315 * {@code
315316 * Statement st = new SimpleStatement("your query");
@@ -337,9 +338,17 @@
337338 * }
338339 * </pre>
339340 * <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
341342 * (same query string, same bound parameters). Altering the contents of the paging state
342343 * 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[])}).
343352 *
344353 * @param pagingState the paging state to set, or {@code null} to remove any state that was
345354 * previously set on this statement.
@@ -364,6 +373,30 @@
364373 return this;
365374 }
366375
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+
367400 ByteBuffer getPagingState() {
368401 return pagingState;
369402 }
modified driver-core/src/test/java/com/datastax/driver/core/PagingStateTest.java +21 −0
@@ -209,5 +209,26 @@
209209 batch.setPagingState(emptyStatement);
210210 }
211211
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+
212233 }
213234
modified driver-core/src/test/java/com/datastax/driver/core/RecommissionedNodeTest.java +2 −0
@@ -110,6 +110,7 @@
110110
111111 // Node 1 should now be DOWN with no reconnection attempt
112112 assertThat(mainCluster).host(1)
113+ .goesDownWithin(10, TimeUnit.SECONDS)
113114 .hasState(DOWN)
114115 .isNotReconnectingFromDown();
115116 }
@@ -133,6 +134,7 @@
133134
134135 // Node 1 should now be DOWN with no reconnection attempt
135136 assertThat(mainCluster).host(1)
137+ .goesDownWithin(10, TimeUnit.SECONDS)
136138 .hasState(DOWN)
137139 .isNotReconnectingFromDown();
138140 }
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +1 −0
@@ -262,6 +262,7 @@
262262 .withReconnectionPolicy(new ConstantReconnectionPolicy(5000))
263263 .withLoadBalancingPolicy(loadBalancingPolicy)
264264 .withSocketOptions(socketOptions)
265+ .withProtocolVersion(TestUtils.getDesiredProtocolVersion())
265266 .build();
266267 // Create two sessions to have multiple pools
267268 cluster.connect();
modified features/paging/README.md +43 −0
@@ -173,3 +173,46 @@
173173
174174 [result_set]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ResultSet.html
175175 [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[])