Skip to content

Commit 13ac176

Browse files
committed
JAVA-1682: Provide a way to record latencies for cancelled speculative executions
Those latencies are emitted with a CancelledSpeculativeExecutionException, which allows LatencyTracker implementations to choose whether to include them or not. For example, a tracker that wants to detect "slowness" of a node might want to record them (otherwise a node that always hits the speculative execution threshold would appear to have no measurement), whereas a tracker that monitors latencies of successful executions should ignore them.
1 parent 58af03b commit 13ac176

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [new feature] JAVA-1651: Add NO_COMPACT startup option.
1212
- [improvement] JAVA-1683: Add metrics to track writes to nodes.
1313
- [new feature] JAVA-1229: Allow specifying the keyspace for individual queries.
14+
- [improvement] JAVA-1682: Provide a way to record latencies for cancelled speculative executions.
1415

1516
Merged from 3.3.x:
1617

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2012-2017 DataStax Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.driver.core;
17+
18+
/**
19+
* Special exception that gets emitted to {@link LatencyTracker}s with the latencies of cancelled speculative
20+
* executions. This allows those trackers to choose whether to ignore those latencies or not.
21+
*/
22+
class CancelledSpeculativeExecutionException extends Exception {
23+
24+
static CancelledSpeculativeExecutionException INSTANCE = new CancelledSpeculativeExecutionException();
25+
26+
private CancelledSpeculativeExecutionException() {
27+
super();
28+
}
29+
}

driver-core/src/main/java/com/datastax/driver/core/PercentileTracker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ protected boolean include(Host host, Statement statement, Exception exception) {
255255
OverloadedException.class,
256256
BootstrappingException.class,
257257
UnpreparedException.class,
258-
QueryValidationException.class // query validation also happens at early stages in the coordinator
258+
QueryValidationException.class, // query validation also happens at early stages in the coordinator
259+
CancelledSpeculativeExecutionException.class
259260
);
260261

261262
/**

driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ class SpeculativeExecution implements Connection.ResponseCallback {
274274
private volatile ConsistencyLevel retryConsistencyLevel;
275275
private final AtomicReference<QueryState> queryStateRef;
276276
private final AtomicBoolean nextExecutionScheduled = new AtomicBoolean();
277+
private final long startTime = System.nanoTime();
277278

278279
// This represents the number of times a retry has been triggered by the RetryPolicy (this is different from
279280
// queryStateRef.get().retryCount, because some retries don't involve the policy, for example after an
@@ -511,10 +512,18 @@ void cancel() {
511512
// If it's still null, this will be handled by re-checking queryStateRef at the end of write().
512513
if (connectionHandler != null && connectionHandler.cancelHandler())
513514
connectionHandler.connection.release();
515+
Host queriedHost = current;
516+
if (queriedHost != null && statement != Statement.DEFAULT) {
517+
manager.cluster.manager.reportQuery(queriedHost, statement, CancelledSpeculativeExecutionException.INSTANCE, System.nanoTime() - startTime);
518+
}
514519
return;
515520
} else if (!previous.inProgress && queryStateRef.compareAndSet(previous, QueryState.CANCELLED_WHILE_COMPLETE)) {
516521
if (logger.isTraceEnabled())
517522
logger.trace("[{}] Cancelled while complete", id);
523+
Host queriedHost = current;
524+
if (queriedHost != null && statement != Statement.DEFAULT) {
525+
manager.cluster.manager.reportQuery(queriedHost, statement, CancelledSpeculativeExecutionException.INSTANCE, System.nanoTime() - startTime);
526+
}
518527
return;
519528
}
520529
}

0 commit comments

Comments
 (0)