kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | c7bbffe1167fcca172971ef746fb7ea0e811d6b1a057635d78961bddf27ac64b |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-06-26 22:12 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-06-26 22:19 +02:00 |
| Parents | d192a852345e |
6 files changed
modified changelog/README.md +2 −0
| @@ -18,6 +18,8 @@ | ||
| 18 | 18 | - [improvement] Add PoolingOptions method to set both core and max |
| 19 | 19 | connections (JAVA-662). |
| 20 | 20 | - [improvement] Do not include epoll JAR in binary distribution (JAVA-766) |
| 21 | +- [improvement] Optimize internal copies of Request objects (JAVA-726) | |
| 22 | +- [bug] Preserve tracing across retries (JAVA-815) | |
| 21 | 23 | |
| 22 | 24 | Merged from 2.0.10_fixes branch: |
| 23 | 25 |
modified driver-core/src/main/java/com/datastax/driver/core/Message.java +13 −4
| @@ -90,14 +90,15 @@ | ||
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | public final Type type; |
| 93 | - protected boolean tracingRequested; | |
| 93 | + private final boolean tracingRequested; | |
| 94 | 94 | |
| 95 | 95 | protected Request(Type type) { |
| 96 | - this.type = type; | |
| 96 | + this(type, false); | |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - public void setTracingRequested() { | |
| 100 | - this.tracingRequested = true; | |
| 99 | + protected Request(Type type, boolean tracingRequested) { | |
| 100 | + this.type = type; | |
| 101 | + this.tracingRequested = tracingRequested; | |
| 101 | 102 | } |
| 102 | 103 | |
| 103 | 104 | public boolean isTracingRequested() { |
| @@ -128,6 +129,14 @@ | ||
| 128 | 129 | default: return null; |
| 129 | 130 | } |
| 130 | 131 | } |
| 132 | + | |
| 133 | + Request copy() { | |
| 134 | + throw new UnsupportedOperationException(); | |
| 135 | + } | |
| 136 | + | |
| 137 | + Request copy(ConsistencyLevel newConsistencyLevel) { | |
| 138 | + throw new UnsupportedOperationException(); | |
| 139 | + } | |
| 131 | 140 | } |
| 132 | 141 | |
| 133 | 142 | public static abstract class Response extends Message { |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +2 −2
| @@ -111,7 +111,7 @@ | ||
| 111 | 111 | // Clone the request after the first execution, since we set the streamId on it later and we |
| 112 | 112 | // don't want to share that across executions. |
| 113 | 113 | if (position > 1) |
| 114 | - request = manager.makeRequestMessage(statement, request.pagingState()) ; | |
| 114 | + request = request.copy(); | |
| 115 | 115 | |
| 116 | 116 | SpeculativeExecution execution = new SpeculativeExecution(request, position); |
| 117 | 117 | runningExecutions.add(execution); |
| @@ -405,7 +405,7 @@ | ||
| 405 | 405 | @Override |
| 406 | 406 | public Message.Request request() { |
| 407 | 407 | if (retryConsistencyLevel != null && retryConsistencyLevel != request.consistency()) |
| 408 | - return manager.makeRequestMessage(statement, retryConsistencyLevel, request.consistency(), request.pagingState()); | |
| 408 | + return request.copy(retryConsistencyLevel); | |
| 409 | 409 | else |
| 410 | 410 | return request; |
| 411 | 411 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Requests.java +41 −7
| @@ -135,15 +135,25 @@ | ||
| 135 | 135 | public final QueryProtocolOptions options; |
| 136 | 136 | |
| 137 | 137 | public Query(String query) { |
| 138 | - this(query, QueryProtocolOptions.DEFAULT); | |
| 138 | + this(query, QueryProtocolOptions.DEFAULT, false); | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - public Query(String query, QueryProtocolOptions options) { | |
| 142 | - super(Type.QUERY); | |
| 141 | + public Query(String query, QueryProtocolOptions options, boolean tracingRequested) { | |
| 142 | + super(Type.QUERY, tracingRequested); | |
| 143 | 143 | this.query = query; |
| 144 | 144 | this.options = options; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | + @Override | |
| 148 | + Request copy() { | |
| 149 | + return new Query(this.query, options, isTracingRequested()); | |
| 150 | + } | |
| 151 | + | |
| 152 | + @Override | |
| 153 | + Request copy(ConsistencyLevel newConsistencyLevel) { | |
| 154 | + return new Query(this.query, options.copy(newConsistencyLevel), isTracingRequested()); | |
| 155 | + } | |
| 156 | + | |
| 147 | 157 | @Override |
| 148 | 158 | public String toString() { |
| 149 | 159 | return "QUERY " + query + '(' + options + ')'; |
| @@ -181,12 +191,22 @@ | ||
| 181 | 191 | public final MD5Digest statementId; |
| 182 | 192 | public final QueryProtocolOptions options; |
| 183 | 193 | |
| 184 | - public Execute(MD5Digest statementId, QueryProtocolOptions options) { | |
| 185 | - super(Message.Request.Type.EXECUTE); | |
| 194 | + public Execute(MD5Digest statementId, QueryProtocolOptions options, boolean tracingRequested) { | |
| 195 | + super(Message.Request.Type.EXECUTE, tracingRequested); | |
| 186 | 196 | this.statementId = statementId; |
| 187 | 197 | this.options = options; |
| 188 | 198 | } |
| 189 | 199 | |
| 200 | + @Override | |
| 201 | + Request copy() { | |
| 202 | + return new Execute(statementId, options, isTracingRequested()); | |
| 203 | + } | |
| 204 | + | |
| 205 | + @Override | |
| 206 | + Request copy(ConsistencyLevel newConsistencyLevel) { | |
| 207 | + return new Execute(statementId, options.copy(newConsistencyLevel), isTracingRequested()); | |
| 208 | + } | |
| 209 | + | |
| 190 | 210 | @Override |
| 191 | 211 | public String toString() { |
| 192 | 212 | return "EXECUTE " + statementId + " (" + options + ')'; |
| @@ -264,6 +284,10 @@ | ||
| 264 | 284 | flags.add(Flag.SERIAL_CONSISTENCY); |
| 265 | 285 | } |
| 266 | 286 | |
| 287 | + public QueryProtocolOptions copy(ConsistencyLevel newConsistencyLevel) { | |
| 288 | + return new QueryProtocolOptions(newConsistencyLevel, values, skipMetadata, pageSize, pagingState, serialConsistency); | |
| 289 | + } | |
| 290 | + | |
| 267 | 291 | public void encode(ByteBuf dest) { |
| 268 | 292 | CBUtil.writeConsistencyLevel(consistency, dest); |
| 269 | 293 | |
| @@ -352,14 +376,24 @@ | ||
| 352 | 376 | public final List<List<ByteBuffer>> values; |
| 353 | 377 | public final ConsistencyLevel consistency; |
| 354 | 378 | |
| 355 | - public Batch(BatchStatement.Type type, List<Object> queryOrIdList, List<List<ByteBuffer>> values, ConsistencyLevel consistency) { | |
| 356 | - super(Message.Request.Type.BATCH); | |
| 379 | + public Batch(BatchStatement.Type type, List<Object> queryOrIdList, List<List<ByteBuffer>> values, ConsistencyLevel consistency, boolean tracingRequested) { | |
| 380 | + super(Message.Request.Type.BATCH, tracingRequested); | |
| 357 | 381 | this.type = type; |
| 358 | 382 | this.queryOrIdList = queryOrIdList; |
| 359 | 383 | this.values = values; |
| 360 | 384 | this.consistency = consistency; |
| 361 | 385 | } |
| 362 | 386 | |
| 387 | + @Override | |
| 388 | + Request copy() { | |
| 389 | + return new Batch(type, queryOrIdList, values, consistency, isTracingRequested()); | |
| 390 | + } | |
| 391 | + | |
| 392 | + @Override | |
| 393 | + Request copy(ConsistencyLevel newConsistencyLevel) { | |
| 394 | + return new Batch(type, queryOrIdList, values, newConsistencyLevel, isTracingRequested()); | |
| 395 | + } | |
| 396 | + | |
| 363 | 397 | @Override |
| 364 | 398 | public String toString() { |
| 365 | 399 | StringBuilder sb = new StringBuilder(); |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +5 −13
| @@ -454,14 +454,6 @@ | ||
| 454 | 454 | if (serialConsistency == null) |
| 455 | 455 | serialConsistency = configuration().getQueryOptions().getSerialConsistencyLevel(); |
| 456 | 456 | |
| 457 | - Message.Request request = makeRequestMessage(statement, consistency, serialConsistency, pagingState); | |
| 458 | - if (statement.isTracing()) | |
| 459 | - request.setTracingRequested(); | |
| 460 | - | |
| 461 | - return request; | |
| 462 | - } | |
| 463 | - | |
| 464 | - Message.Request makeRequestMessage(Statement statement, ConsistencyLevel cl, ConsistencyLevel scl, ByteBuffer pagingState) { | |
| 465 | 457 | int protoVersion = cluster.manager.protocolVersion(); |
| 466 | 458 | int fetchSize = statement.getFetchSize(); |
| 467 | 459 | ByteBuffer usedPagingState = pagingState; |
| @@ -504,8 +496,8 @@ | ||
| 504 | 496 | |
| 505 | 497 | List<ByteBuffer> values = rawValues == null ? Collections.<ByteBuffer>emptyList() : Arrays.asList(rawValues); |
| 506 | 498 | String qString = rs.getQueryString(); |
| 507 | - Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(cl, values, false, fetchSize, usedPagingState, scl); | |
| 508 | - return new Requests.Query(qString, options); | |
| 499 | + Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(consistency, values, false, fetchSize, usedPagingState, serialConsistency); | |
| 500 | + return new Requests.Query(qString, options, statement.isTracing()); | |
| 509 | 501 | } else if (statement instanceof BoundStatement) { |
| 510 | 502 | BoundStatement bs = (BoundStatement)statement; |
| 511 | 503 | if (!cluster.manager.preparedQueries.containsKey(bs.statement.getPreparedId().id)) { |
| @@ -513,8 +505,8 @@ | ||
| 513 | 505 | + "You may have used a PreparedStatement that was created with another Cluster instance.", bs.statement.getPreparedId().id)); |
| 514 | 506 | } |
| 515 | 507 | boolean skipMetadata = protoVersion != 1 && bs.statement.getPreparedId().resultSetMetadata != null; |
| 516 | - Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(cl, Arrays.asList(bs.values), skipMetadata, fetchSize, usedPagingState, scl); | |
| 517 | - return new Requests.Execute(bs.statement.getPreparedId().id, options); | |
| 508 | + Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(consistency, Arrays.asList(bs.values), skipMetadata, fetchSize, usedPagingState, serialConsistency); | |
| 509 | + return new Requests.Execute(bs.statement.getPreparedId().id, options, statement.isTracing()); | |
| 518 | 510 | } else { |
| 519 | 511 | assert statement instanceof BatchStatement : statement; |
| 520 | 512 | assert pagingState == null; |
| @@ -524,7 +516,7 @@ | ||
| 524 | 516 | |
| 525 | 517 | BatchStatement bs = (BatchStatement)statement; |
| 526 | 518 | BatchStatement.IdAndValues idAndVals = bs.getIdAndValues(); |
| 527 | - return new Requests.Batch(bs.batchType, idAndVals.ids, idAndVals.values, cl); | |
| 519 | + return new Requests.Batch(bs.batchType, idAndVals.ids, idAndVals.values, consistency, statement.isTracing()); | |
| 528 | 520 | } |
| 529 | 521 | } |
| 530 | 522 |
modified driver-core/src/test/java/com/datastax/driver/core/TracingTest.java +25 −0
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import java.util.List; |
| 21 | 21 | import java.util.UUID; |
| 22 | 22 | |
| 23 | +import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy; | |
| 23 | 24 | import com.datastax.driver.core.utils.CassandraVersion; |
| 24 | 25 | import org.testng.annotations.BeforeClass; |
| 25 | 26 | import org.testng.annotations.Test; |
| @@ -29,6 +30,11 @@ | ||
| 29 | 30 | @CassandraVersion(major=2.0) |
| 30 | 31 | public class TracingTest extends CCMBridge.PerClassSingleNodeCluster { |
| 31 | 32 | |
| 33 | + @Override | |
| 34 | + protected Cluster.Builder configure(Cluster.Builder builder) { | |
| 35 | + return builder.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE); | |
| 36 | + } | |
| 37 | + | |
| 32 | 38 | public static final String KEY = "tracing_test"; |
| 33 | 39 | |
| 34 | 40 | @Override |
| @@ -69,5 +75,24 @@ | ||
| 69 | 75 | assertThat(result.getExecutionInfo().getQueryTrace().getEvents()).isNotNull() |
| 70 | 76 | .isNotEmpty(); |
| 71 | 77 | } |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Validates that if a query gets retried, the second internal query will still have tracing enabled. | |
| 81 | + * | |
| 82 | + * To force a retry, we use the downgrading policy with an impossible CL. | |
| 83 | + * | |
| 84 | + * @test_category tracing | |
| 85 | + * @jira_ticket JAVA-815 | |
| 86 | + * @expected_result {@link ResultSet} where {@link ExecutionInfo} contains trace information after a retry. | |
| 87 | + */ | |
| 88 | + @Test(groups = "short") | |
| 89 | + public void should_preserve_tracing_status_across_retries() { | |
| 90 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 91 | + st.setConsistencyLevel(ConsistencyLevel.THREE).enableTracing(); | |
| 92 | + | |
| 93 | + ResultSet result = session.execute(st); | |
| 94 | + | |
| 95 | + assertThat(result.getExecutionInfo().getQueryTrace()).isNotNull(); | |
| 96 | + } | |
| 72 | 97 | } |
| 73 | 98 |