Skip to content

Commit daea7f7

Browse files
author
Alexandre Dutra
committed
JAVA-1257: Implement a customizable StatementFormatter.
This commit also delegates Statement.toString() implementation to StatementFormatter.format(). It also introduces EnhancedQueryLogger, an equivalent to QueryLogger that delegates statement formatting tasks to StatementFormatter.
1 parent 26ca703 commit daea7f7

25 files changed

Lines changed: 3890 additions & 311 deletions

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [improvement] JAVA-1328: Provide compatibility with Guava 20.
1616
- [improvement] JAVA-1247: Disable idempotence warnings.
1717
- [improvement] JAVA-1286: Support setting and retrieving udt fields in QueryBuilder.
18+
- [new feature] JAVA-1257: Implement a customizable StatementFormatter.
1819

1920
Merged from 3.1.x branch:
2021

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public enum Type {
6161
COUNTER
6262
}
6363

64-
;
65-
6664
final Type batchType;
6765
private final List<Statement> statements = new ArrayList<Statement>();
6866

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,11 @@ boolean removeSession(Session session) {
16551655

16561656
void reportQuery(Host host, Statement statement, Exception exception, long latencyNanos) {
16571657
for (LatencyTracker tracker : latencyTrackers) {
1658-
tracker.update(host, statement, exception, latencyNanos);
1658+
try {
1659+
tracker.update(host, statement, exception, latencyNanos);
1660+
} catch (Exception e) {
1661+
logger.error(String.format("Tracker %s threw exception: %s", tracker, e.getMessage()), e);
1662+
}
16591663
}
16601664
}
16611665

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

Lines changed: 471 additions & 0 deletions
Large diffs are not rendered by default.

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
@@ -27,6 +27,7 @@
2727

2828
import java.util.Set;
2929
import java.util.concurrent.*;
30+
import java.util.concurrent.TimeUnit;
3031

3132
import static com.google.common.base.Preconditions.checkArgument;
3233
import static java.util.concurrent.TimeUnit.*;
@@ -39,7 +40,7 @@
3940
* determined by {@link #computeKey(Host, Statement, Exception)}.
4041
* <p/>
4142
* This class is used by percentile-aware components such as
42-
* {@link QueryLogger.Builder#withDynamicThreshold(PercentileTracker, double)} QueryLogger} and
43+
* {@link EnhancedQueryLogger.Builder#withDynamicThreshold(PercentileTracker, double)} QueryLogger} and
4344
* {@link com.datastax.driver.core.policies.PercentileSpeculativeExecutionPolicy}.
4445
* <p/>
4546
* It uses <a href="http://hdrhistogram.github.io/HdrHistogram/">HdrHistogram</a> to record latencies:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@
8787
* This class is thread-safe.
8888
*
8989
* @since 2.0.10
90+
* @deprecated since 3.2.0, use the more easily configurable {@link EnhancedQueryLogger} instead.
91+
* This class might be removed in a future major version.
9092
*/
93+
@SuppressWarnings("DeprecatedIsStillUsed")
94+
@Deprecated
9195
public abstract class QueryLogger implements LatencyTracker {
9296

9397
/**
@@ -230,6 +234,7 @@ public void onUnregister(Cluster cluster) {
230234
* This implementation is the default and should be preferred to {@link DynamicThresholdQueryLogger}
231235
* which is still in beta state.
232236
*/
237+
@Deprecated
233238
public static class ConstantThresholdQueryLogger extends QueryLogger {
234239

235240
private volatile long slowQueryLatencyThresholdMillis;
@@ -289,6 +294,7 @@ protected void maybeLogSlowQuery(Host host, Statement statement, long latencyMs)
289294
* Dynamic thresholds are based on per-host latency percentiles, as computed
290295
* by {@link PercentileTracker}.
291296
*/
297+
@Deprecated
292298
public static class DynamicThresholdQueryLogger extends QueryLogger {
293299

294300
private volatile double slowQueryLatencyThresholdPercentile;

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,4 @@ public boolean hasValues() {
189189
return hasValues(CodecRegistry.DEFAULT_INSTANCE);
190190
}
191191

192-
/**
193-
* Returns this statement as a CQL query string.
194-
* <p/>
195-
* It is important to note that the query string is merely
196-
* a CQL representation of this statement, but it does
197-
* <em>not</em> convey all the information stored in {@link Statement}
198-
* objects.
199-
* <p/>
200-
* See the javadocs of {@link #getQueryString()} for more information.
201-
*
202-
* @return this statement as a CQL query string.
203-
* @see #getQueryString()
204-
*/
205-
@Override
206-
public String toString() {
207-
return getQueryString();
208-
}
209192
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,17 @@ protected static Boolean isBatchIdempotent(Collection<? extends Statement> state
596596
}
597597
return (hasNullIdempotentStatements) ? null : true;
598598
}
599+
600+
@Override
601+
public String toString() {
602+
try {
603+
return StatementFormatter.DEFAULT_INSTANCE.format(
604+
this,
605+
StatementFormatter.StatementFormatVerbosity.EXTENDED,
606+
ProtocolVersion.NEWEST_SUPPORTED,
607+
CodecRegistry.DEFAULT_INSTANCE);
608+
} catch (StatementFormatter.StatementFormatException e) {
609+
return super.toString();
610+
}
611+
}
599612
}

0 commit comments

Comments
 (0)