Elffuss BraidHub

kikocis/java-driver public

JAVA-963: Automatically register PercentileTracker from components that use it.
Changeset65fa7c30289cc04c5e9e89eacdd6435242b327bf3bea20dbf61ff64b46b5fe95
Authorolim7t <omichallat+github@gmail.com> · 2016-02-08 17:55 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-03 16:54 +01:00
Parents9d8a5f7c4111

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -27,6 +27,7 @@
2727 - [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger.
2828 - [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key.
2929 - [improvement] JAVA-888: Add cluster-wide percentile tracker.
30+- [improvement] JAVA-963: Automatically register PercentileTracker from components that use it.
3031
3132 Merged from 2.0 branch:
3233
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +14 −12
@@ -435,9 +435,9 @@
435435 */
436436 public Cluster register(Host.StateListener listener) {
437437 checkNotClosed(manager);
438- if (listener instanceof Host.LifecycleAwareStateListener)
438+ boolean added = manager.listeners.add(listener);
439+ if (added && listener instanceof Host.LifecycleAwareStateListener)
439440 ((Host.LifecycleAwareStateListener) listener).onRegister(this);
440- manager.listeners.add(listener);
441441 return this;
442442 }
443443
@@ -452,9 +452,9 @@
452452 */
453453 public Cluster unregister(Host.StateListener listener) {
454454 checkNotClosed(manager);
455- if (listener instanceof Host.LifecycleAwareStateListener)
455+ boolean removed = manager.listeners.remove(listener);
456+ if (removed && listener instanceof Host.LifecycleAwareStateListener)
456457 ((Host.LifecycleAwareStateListener) listener).onUnregister(this);
457- manager.listeners.remove(listener);
458458 return this;
459459 }
460460
@@ -479,9 +479,9 @@
479479 */
480480 public Cluster register(LatencyTracker tracker) {
481481 checkNotClosed(manager);
482- if (tracker instanceof LifecycleAwareLatencyTracker)
482+ boolean added = manager.trackers.add(tracker);
483+ if (added && tracker instanceof LifecycleAwareLatencyTracker)
483484 ((LifecycleAwareLatencyTracker) tracker).onRegister(this);
484- manager.trackers.add(tracker);
485485 return this;
486486 }
487487
@@ -497,9 +497,9 @@
497497 */
498498 public Cluster unregister(LatencyTracker tracker) {
499499 checkNotClosed(manager);
500- if (tracker instanceof LifecycleAwareLatencyTracker)
500+ boolean removed = manager.trackers.remove(tracker);
501+ if (removed && tracker instanceof LifecycleAwareLatencyTracker)
501502 ((LifecycleAwareLatencyTracker) tracker).onUnregister(this);
502- manager.trackers.remove(tracker);
503503 return this;
504504 }
505505
@@ -513,8 +513,9 @@
513513 */
514514 public Cluster register(SchemaChangeListener listener) {
515515 checkNotClosed(manager);
516- listener.onRegister(this);
517- manager.schemaChangeListeners.add(listener);
516+ boolean added = manager.schemaChangeListeners.add(listener);
517+ if (added)
518+ listener.onRegister(this);
518519 return this;
519520 }
520521
@@ -530,8 +531,9 @@
530531 */
531532 public Cluster unregister(SchemaChangeListener listener) {
532533 checkNotClosed(manager);
533- listener.onUnregister(this);
534- manager.schemaChangeListeners.remove(listener);
534+ boolean removed = manager.schemaChangeListeners.remove(listener);
535+ if (removed)
536+ listener.onUnregister(this);
535537 return this;
536538 }
537539
modified driver-core/src/main/java/com/datastax/driver/core/PercentileTracker.java +3 −4
@@ -39,11 +39,10 @@
3939 * determined by {@link #computeKey(Host, Statement, Exception)}.
4040 * <p/>
4141 * This class is used by percentile-aware components such as
42- * {@link QueryLogger.Builder#withDynamicThreshold(PercentileTracker, double) QueryLogger} and
43- * {@link com.datastax.driver.core.policies.PercentileSpeculativeExecutionPolicy}. If you have multiple percentile-aware
44- * components, you should use a single, shared percentile tracker for efficiency; make sure it is registered only once.
42+ * {@link QueryLogger.Builder#withDynamicThreshold(PercentileTracker, double)} QueryLogger} and
43+ * {@link com.datastax.driver.core.policies.PercentileSpeculativeExecutionPolicy}.
4544 * <p/>
46- * This class uses <a href="http://hdrhistogram.github.io/HdrHistogram/">HdrHistogram</a> to record latencies:
45+ * It uses <a href="http://hdrhistogram.github.io/HdrHistogram/">HdrHistogram</a> to record latencies:
4746 * for each category, there is a "live" histogram where current latencies are recorded, and a "cached", read-only
4847 * histogram that is used when clients call {@link #getLatencyAtPercentile(Host, Statement, Exception, double)}. Each
4948 * time the cached histogram becomes older than the interval, the two histograms are switched. Statistics will not be
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +41 −42
@@ -15,7 +15,6 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import com.google.common.annotations.Beta;
1918 import com.google.common.annotations.VisibleForTesting;
2019 import org.slf4j.Logger;
2120 import org.slf4j.LoggerFactory;
@@ -80,33 +79,15 @@
8079 * <strong>Constant thresholds vs. Dynamic thresholds</strong>
8180 * <p/>
8281 * Currently the QueryLogger can track slow queries in two different ways:
83- * using a constant threshold in milliseconds (which is the default behavior),
84- * or using a dynamic threshold based on per-host percentiles computed by
85- * {@link PercentileTracker}.
86- * <p/>
87- * <b>Note that the dynamic threshold version is currently provided as a beta preview: it hasn't been extensively
88- * tested yet, and the API is still subject to change.</b> To use it, you must first obtain and register
89- * an instance of {@link PercentileTracker}, then create your QueryLogger as follows:
90- * <p/>
91- * <pre>
92- * Cluster cluster = ...
93- * // create an instance of PercentileTracker and register it
94- * PercentileTracker tracker = ...;
95- * cluster.register(tracker);
96- * // create an instance of QueryLogger and register it
97- * QueryLogger queryLogger = QueryLogger.builder(cluster)
98- * .withDynamicThreshold(tracker, ...)
99- * .withMaxQueryStringLength(...)
100- * .build();
101- * cluster.register(queryLogger);
102- * </pre>
103- * <p/>
82+ * using a {@link Builder#withConstantThreshold(long)} constant threshold} in milliseconds (which is the default
83+ * behavior), or using a {@link Builder#withDynamicThreshold(PercentileTracker, double) dynamic threshold}
84+ * based on latency percentiles.
10485 * <p/>
10586 * This class is thread-safe.
10687 *
10788 * @since 2.0.10
10889 */
109-public abstract class QueryLogger implements LatencyTracker {
90+public abstract class QueryLogger implements LifecycleAwareLatencyTracker {
11091
11192 /**
11293 * The default latency threshold in milliseconds beyond which queries are considered 'slow'
@@ -288,6 +269,16 @@
288269 logQuery(statement, null, SLOW_LOGGER, message);
289270 }
290271 }
272+
273+ @Override
274+ public void onRegister(Cluster cluster) {
275+ // nothing to do
276+ }
277+
278+ @Override
279+ public void onUnregister(Cluster cluster) {
280+ // nothing to do
281+ }
291282 }
292283
293284 /**
@@ -296,18 +287,16 @@
296287 * <p/>
297288 * Dynamic thresholds are based on per-host latency percentiles, as computed
298289 * by {@link PercentileTracker}.
299- * <p/>
300- * <b>This class is currently provided as a beta preview: it hasn't been extensively tested yet, and the API is still subject
301- * to change.</b>
302290 */
303- @Beta
304291 public static class DynamicThresholdQueryLogger extends QueryLogger {
305292
306293 private volatile double slowQueryLatencyThresholdPercentile;
307294
308295 private volatile PercentileTracker percentileLatencyTracker;
309296
310- private DynamicThresholdQueryLogger(Cluster cluster, int maxQueryStringLength, int maxParameterValueLength, int maxLoggedParameters, double slowQueryLatencyThresholdPercentile, PercentileTracker percentileLatencyTracker) {
297+ private DynamicThresholdQueryLogger(Cluster cluster, int maxQueryStringLength, int maxParameterValueLength,
298+ int maxLoggedParameters, double slowQueryLatencyThresholdPercentile,
299+ PercentileTracker percentileLatencyTracker) {
311300 super(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters);
312301 this.setSlowQueryLatencyThresholdPercentile(slowQueryLatencyThresholdPercentile);
313302 this.setPercentileLatencyTracker(percentileLatencyTracker);
@@ -378,6 +367,16 @@
378367 logQuery(statement, null, SLOW_LOGGER, message);
379368 }
380369 }
370+
371+ @Override
372+ public void onRegister(Cluster cluster) {
373+ cluster.register(percentileLatencyTracker);
374+ }
375+
376+ @Override
377+ public void onUnregister(Cluster cluster) {
378+ // Don't unregister the latency tracker, we can't guess if it's being used by another component or not.
379+ }
381380 }
382381
383382 /**
@@ -426,25 +425,22 @@
426425 /**
427426 * Enables slow query latency tracking based on dynamic thresholds.
428427 * <p/>
429- * Dynamic thresholds are based on per-host latency percentiles, as computed
430- * by {@link PercentileTracker}.
431- * <p/>
432- * Note: You should either use {@link #withConstantThreshold(long) constant thresholds}
433- * or {@link #withDynamicThreshold(PercentileTracker, double) dynamic thresholds},
434- * not both.
428+ * Dynamic thresholds are based on latency percentiles, as computed by {@link PercentileTracker}.
435429 * <p/>
436- * <b>This feature is currently provided as a beta preview: it hasn't been extensively tested yet, and the API is still subject
437- * to change.</b>
430+ * Note: You should either use {@link #withConstantThreshold(long) constant thresholds} or
431+ * {@link #withDynamicThreshold(PercentileTracker, double) dynamic thresholds}, not both.
438432 *
439- * @param percentileLatencyTracker the {@link PercentileTracker} instance to use for recording per-host latency histograms.
440- * Cannot be {@code null}.
433+ * @param percentileLatencyTracker the {@link PercentileTracker} instance to use for recording
434+ * latency histograms. Cannot be {@code null}.
435+ * It will get {@link Cluster#register(LatencyTracker) registered}
436+ * with the cluster at the same time as this logger.
441437 * @param slowQueryLatencyThresholdPercentile Slow queries threshold percentile.
442438 * It must be comprised between 0 inclusive and 100 exclusive.
443439 * The default value is {@link #DEFAULT_SLOW_QUERY_THRESHOLD_PERCENTILE}
444440 * @return this {@link Builder} instance (for method chaining).
445441 */
446- @Beta
447- public Builder withDynamicThreshold(PercentileTracker percentileLatencyTracker, double slowQueryLatencyThresholdPercentile) {
442+ public Builder withDynamicThreshold(PercentileTracker percentileLatencyTracker,
443+ double slowQueryLatencyThresholdPercentile) {
448444 this.percentileLatencyTracker = percentileLatencyTracker;
449445 this.slowQueryLatencyThresholdPercentile = slowQueryLatencyThresholdPercentile;
450446 constantThreshold = false;
@@ -512,9 +508,12 @@
512508 */
513509 public QueryLogger build() {
514510 if (constantThreshold) {
515- return new ConstantThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters, slowQueryLatencyThresholdMillis);
511+ return new ConstantThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength,
512+ maxLoggedParameters, slowQueryLatencyThresholdMillis);
516513 } else {
517- return new DynamicThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters, slowQueryLatencyThresholdPercentile, percentileLatencyTracker);
514+ return new DynamicThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength,
515+ maxLoggedParameters, slowQueryLatencyThresholdPercentile,
516+ percentileLatencyTracker);
518517 }
519518 }
520519
modified driver-core/src/main/java/com/datastax/driver/core/policies/PercentileSpeculativeExecutionPolicy.java +11 −27
@@ -16,7 +16,6 @@
1616 package com.datastax.driver.core.policies;
1717
1818 import com.datastax.driver.core.*;
19-import com.google.common.annotations.Beta;
2019
2120 import java.util.concurrent.atomic.AtomicInteger;
2221
@@ -24,26 +23,7 @@
2423
2524 /**
2625 * A policy that triggers speculative executions when the request to the current host is above a given percentile.
27- * <p/>
28- * This class uses a {@link PerHostPercentileTracker} that must be registered with the cluster instance:
29- * <pre>
30- * PerHostPercentileTracker tracker = PerHostPercentileTracker
31- * .builderWithHighestTrackableLatencyMillis(15000)
32- * .build();
33- * PercentileSpeculativeExecutionPolicy policy = new PercentileSpeculativeExecutionPolicy(tracker, 99.0, 2);
34- * cluster = Cluster.builder()
35- * .addContactPoint("127.0.0.1")
36- * .withSpeculativeExecutionPolicy(policy)
37- * .build();
38- * cluster.register(tracker);
39- * </pre>
40- * You <b>must</b> register the tracker with the cluster yourself (as shown on the last line above), this class will not
41- * do it itself.
42- * <p/>
43- * <b>This class is currently provided as a beta preview: it hasn't been extensively tested yet, and the API is still subject
44- * to change.</b>
4526 */
46-@Beta
4727 public class PercentileSpeculativeExecutionPolicy implements SpeculativeExecutionPolicy {
4828 private final PercentileTracker percentileTracker;
4929 private final double percentile;
@@ -52,13 +32,17 @@
5232 /**
5333 * Builds a new instance.
5434 *
55- * @param percentileTracker the component that will record latencies. Note that this policy doesn't register it with the {@code Cluster},
56- * you <b>must</b> do it yourself (see the code example in this class's Javadoc).
57- * @param percentile the percentile that a request's latency must fall into to be considered slow (ex: {@code 99.0}).
58- * @param maxSpeculativeExecutions the maximum number of speculative executions that will be triggered for a given request (this does not
59- * include the initial, normal request). Must be strictly positive.
35+ * @param percentileTracker the component that will record latencies. It will get
36+ * {@link Cluster#register(LatencyTracker) registered} with the cluster when this
37+ * policy initializes.
38+ * @param percentile the percentile that a request's latency must fall into to be considered slow (ex:
39+ * {@code 99.0}).
40+ * @param maxSpeculativeExecutions the maximum number of speculative executions that will be triggered for a given
41+ * request (this does not include the initial, normal request). Must be strictly
42+ * positive.
6043 */
61- public PercentileSpeculativeExecutionPolicy(PercentileTracker percentileTracker, double percentile, int maxSpeculativeExecutions) {
44+ public PercentileSpeculativeExecutionPolicy(PercentileTracker percentileTracker,
45+ double percentile, int maxSpeculativeExecutions) {
6246 checkArgument(maxSpeculativeExecutions > 0,
6347 "number of speculative executions must be strictly positive (was %d)", maxSpeculativeExecutions);
6448 checkArgument(percentile >= 0.0 && percentile < 100,
@@ -86,7 +70,7 @@
8670
8771 @Override
8872 public void init(Cluster cluster) {
89- // nothing
73+ cluster.register(percentileTracker);
9074 }
9175
9276 @Override