Elffuss BraidHub

kikocis/java-driver public

Optimize internal copies of Request objects (JAVA-726).

This also fixes tracing across retries (JAVA-815).
Changesetc7bbffe1167fcca172971ef746fb7ea0e811d6b1a057635d78961bddf27ac64b
Authorolim7t <omichallat+github@gmail.com> · 2015-06-26 22:12 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-26 22:19 +02:00
Parentsd192a852345e

Browse the files at this changeset

6 files changed

modified changelog/README.md +2 −0
@@ -18,6 +18,8 @@
1818 - [improvement] Add PoolingOptions method to set both core and max
1919 connections (JAVA-662).
2020 - [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)
2123
2224 Merged from 2.0.10_fixes branch:
2325
modified driver-core/src/main/java/com/datastax/driver/core/Message.java +13 −4
@@ -90,14 +90,15 @@
9090 }
9191
9292 public final Type type;
93- protected boolean tracingRequested;
93+ private final boolean tracingRequested;
9494
9595 protected Request(Type type) {
96- this.type = type;
96+ this(type, false);
9797 }
9898
99- public void setTracingRequested() {
100- this.tracingRequested = true;
99+ protected Request(Type type, boolean tracingRequested) {
100+ this.type = type;
101+ this.tracingRequested = tracingRequested;
101102 }
102103
103104 public boolean isTracingRequested() {
@@ -128,6 +129,14 @@
128129 default: return null;
129130 }
130131 }
132+
133+ Request copy() {
134+ throw new UnsupportedOperationException();
135+ }
136+
137+ Request copy(ConsistencyLevel newConsistencyLevel) {
138+ throw new UnsupportedOperationException();
139+ }
131140 }
132141
133142 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 @@
111111 // Clone the request after the first execution, since we set the streamId on it later and we
112112 // don't want to share that across executions.
113113 if (position > 1)
114- request = manager.makeRequestMessage(statement, request.pagingState()) ;
114+ request = request.copy();
115115
116116 SpeculativeExecution execution = new SpeculativeExecution(request, position);
117117 runningExecutions.add(execution);
@@ -405,7 +405,7 @@
405405 @Override
406406 public Message.Request request() {
407407 if (retryConsistencyLevel != null && retryConsistencyLevel != request.consistency())
408- return manager.makeRequestMessage(statement, retryConsistencyLevel, request.consistency(), request.pagingState());
408+ return request.copy(retryConsistencyLevel);
409409 else
410410 return request;
411411 }
modified driver-core/src/main/java/com/datastax/driver/core/Requests.java +41 −7
@@ -135,15 +135,25 @@
135135 public final QueryProtocolOptions options;
136136
137137 public Query(String query) {
138- this(query, QueryProtocolOptions.DEFAULT);
138+ this(query, QueryProtocolOptions.DEFAULT, false);
139139 }
140140
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);
143143 this.query = query;
144144 this.options = options;
145145 }
146146
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+
147157 @Override
148158 public String toString() {
149159 return "QUERY " + query + '(' + options + ')';
@@ -181,12 +191,22 @@
181191 public final MD5Digest statementId;
182192 public final QueryProtocolOptions options;
183193
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);
186196 this.statementId = statementId;
187197 this.options = options;
188198 }
189199
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+
190210 @Override
191211 public String toString() {
192212 return "EXECUTE " + statementId + " (" + options + ')';
@@ -264,6 +284,10 @@
264284 flags.add(Flag.SERIAL_CONSISTENCY);
265285 }
266286
287+ public QueryProtocolOptions copy(ConsistencyLevel newConsistencyLevel) {
288+ return new QueryProtocolOptions(newConsistencyLevel, values, skipMetadata, pageSize, pagingState, serialConsistency);
289+ }
290+
267291 public void encode(ByteBuf dest) {
268292 CBUtil.writeConsistencyLevel(consistency, dest);
269293
@@ -352,14 +376,24 @@
352376 public final List<List<ByteBuffer>> values;
353377 public final ConsistencyLevel consistency;
354378
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);
357381 this.type = type;
358382 this.queryOrIdList = queryOrIdList;
359383 this.values = values;
360384 this.consistency = consistency;
361385 }
362386
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+
363397 @Override
364398 public String toString() {
365399 StringBuilder sb = new StringBuilder();
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +5 −13
@@ -454,14 +454,6 @@
454454 if (serialConsistency == null)
455455 serialConsistency = configuration().getQueryOptions().getSerialConsistencyLevel();
456456
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) {
465457 int protoVersion = cluster.manager.protocolVersion();
466458 int fetchSize = statement.getFetchSize();
467459 ByteBuffer usedPagingState = pagingState;
@@ -504,8 +496,8 @@
504496
505497 List<ByteBuffer> values = rawValues == null ? Collections.<ByteBuffer>emptyList() : Arrays.asList(rawValues);
506498 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());
509501 } else if (statement instanceof BoundStatement) {
510502 BoundStatement bs = (BoundStatement)statement;
511503 if (!cluster.manager.preparedQueries.containsKey(bs.statement.getPreparedId().id)) {
@@ -513,8 +505,8 @@
513505 + "You may have used a PreparedStatement that was created with another Cluster instance.", bs.statement.getPreparedId().id));
514506 }
515507 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());
518510 } else {
519511 assert statement instanceof BatchStatement : statement;
520512 assert pagingState == null;
@@ -524,7 +516,7 @@
524516
525517 BatchStatement bs = (BatchStatement)statement;
526518 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());
528520 }
529521 }
530522
modified driver-core/src/test/java/com/datastax/driver/core/TracingTest.java +25 −0
@@ -20,6 +20,7 @@
2020 import java.util.List;
2121 import java.util.UUID;
2222
23+import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy;
2324 import com.datastax.driver.core.utils.CassandraVersion;
2425 import org.testng.annotations.BeforeClass;
2526 import org.testng.annotations.Test;
@@ -29,6 +30,11 @@
2930 @CassandraVersion(major=2.0)
3031 public class TracingTest extends CCMBridge.PerClassSingleNodeCluster {
3132
33+ @Override
34+ protected Cluster.Builder configure(Cluster.Builder builder) {
35+ return builder.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
36+ }
37+
3238 public static final String KEY = "tracing_test";
3339
3440 @Override
@@ -69,5 +75,24 @@
6975 assertThat(result.getExecutionInfo().getQueryTrace().getEvents()).isNotNull()
7076 .isNotEmpty();
7177 }
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+ }
7297 }
7398