kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 65fa7c30289cc04c5e9e89eacdd6435242b327bf3bea20dbf61ff64b46b5fe95 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-02-08 17:55 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-03 16:54 +01:00 |
| Parents | 9d8a5f7c4111 |
5 files changed
modified changelog/README.md +1 −0
| @@ -27,6 +27,7 @@ | ||
| 27 | 27 | - [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger. |
| 28 | 28 | - [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key. |
| 29 | 29 | - [improvement] JAVA-888: Add cluster-wide percentile tracker. |
| 30 | +- [improvement] JAVA-963: Automatically register PercentileTracker from components that use it. | |
| 30 | 31 | |
| 31 | 32 | Merged from 2.0 branch: |
| 32 | 33 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +14 −12
| @@ -435,9 +435,9 @@ | ||
| 435 | 435 | */ |
| 436 | 436 | public Cluster register(Host.StateListener listener) { |
| 437 | 437 | checkNotClosed(manager); |
| 438 | - if (listener instanceof Host.LifecycleAwareStateListener) | |
| 438 | + boolean added = manager.listeners.add(listener); | |
| 439 | + if (added && listener instanceof Host.LifecycleAwareStateListener) | |
| 439 | 440 | ((Host.LifecycleAwareStateListener) listener).onRegister(this); |
| 440 | - manager.listeners.add(listener); | |
| 441 | 441 | return this; |
| 442 | 442 | } |
| 443 | 443 | |
| @@ -452,9 +452,9 @@ | ||
| 452 | 452 | */ |
| 453 | 453 | public Cluster unregister(Host.StateListener listener) { |
| 454 | 454 | checkNotClosed(manager); |
| 455 | - if (listener instanceof Host.LifecycleAwareStateListener) | |
| 455 | + boolean removed = manager.listeners.remove(listener); | |
| 456 | + if (removed && listener instanceof Host.LifecycleAwareStateListener) | |
| 456 | 457 | ((Host.LifecycleAwareStateListener) listener).onUnregister(this); |
| 457 | - manager.listeners.remove(listener); | |
| 458 | 458 | return this; |
| 459 | 459 | } |
| 460 | 460 | |
| @@ -479,9 +479,9 @@ | ||
| 479 | 479 | */ |
| 480 | 480 | public Cluster register(LatencyTracker tracker) { |
| 481 | 481 | checkNotClosed(manager); |
| 482 | - if (tracker instanceof LifecycleAwareLatencyTracker) | |
| 482 | + boolean added = manager.trackers.add(tracker); | |
| 483 | + if (added && tracker instanceof LifecycleAwareLatencyTracker) | |
| 483 | 484 | ((LifecycleAwareLatencyTracker) tracker).onRegister(this); |
| 484 | - manager.trackers.add(tracker); | |
| 485 | 485 | return this; |
| 486 | 486 | } |
| 487 | 487 | |
| @@ -497,9 +497,9 @@ | ||
| 497 | 497 | */ |
| 498 | 498 | public Cluster unregister(LatencyTracker tracker) { |
| 499 | 499 | checkNotClosed(manager); |
| 500 | - if (tracker instanceof LifecycleAwareLatencyTracker) | |
| 500 | + boolean removed = manager.trackers.remove(tracker); | |
| 501 | + if (removed && tracker instanceof LifecycleAwareLatencyTracker) | |
| 501 | 502 | ((LifecycleAwareLatencyTracker) tracker).onUnregister(this); |
| 502 | - manager.trackers.remove(tracker); | |
| 503 | 503 | return this; |
| 504 | 504 | } |
| 505 | 505 | |
| @@ -513,8 +513,9 @@ | ||
| 513 | 513 | */ |
| 514 | 514 | public Cluster register(SchemaChangeListener listener) { |
| 515 | 515 | 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); | |
| 518 | 519 | return this; |
| 519 | 520 | } |
| 520 | 521 | |
| @@ -530,8 +531,9 @@ | ||
| 530 | 531 | */ |
| 531 | 532 | public Cluster unregister(SchemaChangeListener listener) { |
| 532 | 533 | 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); | |
| 535 | 537 | return this; |
| 536 | 538 | } |
| 537 | 539 |
modified driver-core/src/main/java/com/datastax/driver/core/PercentileTracker.java +3 −4
| @@ -39,11 +39,10 @@ | ||
| 39 | 39 | * determined by {@link #computeKey(Host, Statement, Exception)}. |
| 40 | 40 | * <p/> |
| 41 | 41 | * 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}. | |
| 45 | 44 | * <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: | |
| 47 | 46 | * for each category, there is a "live" histogram where current latencies are recorded, and a "cached", read-only |
| 48 | 47 | * histogram that is used when clients call {@link #getLatencyAtPercentile(Host, Statement, Exception, double)}. Each |
| 49 | 48 | * 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 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.google.common.annotations.Beta; | |
| 19 | 18 | import com.google.common.annotations.VisibleForTesting; |
| 20 | 19 | import org.slf4j.Logger; |
| 21 | 20 | import org.slf4j.LoggerFactory; |
| @@ -80,33 +79,15 @@ | ||
| 80 | 79 | * <strong>Constant thresholds vs. Dynamic thresholds</strong> |
| 81 | 80 | * <p/> |
| 82 | 81 | * 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. | |
| 104 | 85 | * <p/> |
| 105 | 86 | * This class is thread-safe. |
| 106 | 87 | * |
| 107 | 88 | * @since 2.0.10 |
| 108 | 89 | */ |
| 109 | -public abstract class QueryLogger implements LatencyTracker { | |
| 90 | +public abstract class QueryLogger implements LifecycleAwareLatencyTracker { | |
| 110 | 91 | |
| 111 | 92 | /** |
| 112 | 93 | * The default latency threshold in milliseconds beyond which queries are considered 'slow' |
| @@ -288,6 +269,16 @@ | ||
| 288 | 269 | logQuery(statement, null, SLOW_LOGGER, message); |
| 289 | 270 | } |
| 290 | 271 | } |
| 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 | + } | |
| 291 | 282 | } |
| 292 | 283 | |
| 293 | 284 | /** |
| @@ -296,18 +287,16 @@ | ||
| 296 | 287 | * <p/> |
| 297 | 288 | * Dynamic thresholds are based on per-host latency percentiles, as computed |
| 298 | 289 | * 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> | |
| 302 | 290 | */ |
| 303 | - @Beta | |
| 304 | 291 | public static class DynamicThresholdQueryLogger extends QueryLogger { |
| 305 | 292 | |
| 306 | 293 | private volatile double slowQueryLatencyThresholdPercentile; |
| 307 | 294 | |
| 308 | 295 | private volatile PercentileTracker percentileLatencyTracker; |
| 309 | 296 | |
| 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) { | |
| 311 | 300 | super(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters); |
| 312 | 301 | this.setSlowQueryLatencyThresholdPercentile(slowQueryLatencyThresholdPercentile); |
| 313 | 302 | this.setPercentileLatencyTracker(percentileLatencyTracker); |
| @@ -378,6 +367,16 @@ | ||
| 378 | 367 | logQuery(statement, null, SLOW_LOGGER, message); |
| 379 | 368 | } |
| 380 | 369 | } |
| 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 | + } | |
| 381 | 380 | } |
| 382 | 381 | |
| 383 | 382 | /** |
| @@ -426,25 +425,22 @@ | ||
| 426 | 425 | /** |
| 427 | 426 | * Enables slow query latency tracking based on dynamic thresholds. |
| 428 | 427 | * <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}. | |
| 435 | 429 | * <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. | |
| 438 | 432 | * |
| 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. | |
| 441 | 437 | * @param slowQueryLatencyThresholdPercentile Slow queries threshold percentile. |
| 442 | 438 | * It must be comprised between 0 inclusive and 100 exclusive. |
| 443 | 439 | * The default value is {@link #DEFAULT_SLOW_QUERY_THRESHOLD_PERCENTILE} |
| 444 | 440 | * @return this {@link Builder} instance (for method chaining). |
| 445 | 441 | */ |
| 446 | - @Beta | |
| 447 | - public Builder withDynamicThreshold(PercentileTracker percentileLatencyTracker, double slowQueryLatencyThresholdPercentile) { | |
| 442 | + public Builder withDynamicThreshold(PercentileTracker percentileLatencyTracker, | |
| 443 | + double slowQueryLatencyThresholdPercentile) { | |
| 448 | 444 | this.percentileLatencyTracker = percentileLatencyTracker; |
| 449 | 445 | this.slowQueryLatencyThresholdPercentile = slowQueryLatencyThresholdPercentile; |
| 450 | 446 | constantThreshold = false; |
| @@ -512,9 +508,12 @@ | ||
| 512 | 508 | */ |
| 513 | 509 | public QueryLogger build() { |
| 514 | 510 | if (constantThreshold) { |
| 515 | - return new ConstantThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters, slowQueryLatencyThresholdMillis); | |
| 511 | + return new ConstantThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, | |
| 512 | + maxLoggedParameters, slowQueryLatencyThresholdMillis); | |
| 516 | 513 | } else { |
| 517 | - return new DynamicThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters, slowQueryLatencyThresholdPercentile, percentileLatencyTracker); | |
| 514 | + return new DynamicThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, | |
| 515 | + maxLoggedParameters, slowQueryLatencyThresholdPercentile, | |
| 516 | + percentileLatencyTracker); | |
| 518 | 517 | } |
| 519 | 518 | } |
| 520 | 519 |
modified driver-core/src/main/java/com/datastax/driver/core/policies/PercentileSpeculativeExecutionPolicy.java +11 −27
| @@ -16,7 +16,6 @@ | ||
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.*; |
| 19 | -import com.google.common.annotations.Beta; | |
| 20 | 19 | |
| 21 | 20 | import java.util.concurrent.atomic.AtomicInteger; |
| 22 | 21 | |
| @@ -24,26 +23,7 @@ | ||
| 24 | 23 | |
| 25 | 24 | /** |
| 26 | 25 | * 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> | |
| 45 | 26 | */ |
| 46 | -@Beta | |
| 47 | 27 | public class PercentileSpeculativeExecutionPolicy implements SpeculativeExecutionPolicy { |
| 48 | 28 | private final PercentileTracker percentileTracker; |
| 49 | 29 | private final double percentile; |
| @@ -52,13 +32,17 @@ | ||
| 52 | 32 | /** |
| 53 | 33 | * Builds a new instance. |
| 54 | 34 | * |
| 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. | |
| 60 | 43 | */ |
| 61 | - public PercentileSpeculativeExecutionPolicy(PercentileTracker percentileTracker, double percentile, int maxSpeculativeExecutions) { | |
| 44 | + public PercentileSpeculativeExecutionPolicy(PercentileTracker percentileTracker, | |
| 45 | + double percentile, int maxSpeculativeExecutions) { | |
| 62 | 46 | checkArgument(maxSpeculativeExecutions > 0, |
| 63 | 47 | "number of speculative executions must be strictly positive (was %d)", maxSpeculativeExecutions); |
| 64 | 48 | checkArgument(percentile >= 0.0 && percentile < 100, |
| @@ -86,7 +70,7 @@ | ||
| 86 | 70 | |
| 87 | 71 | @Override |
| 88 | 72 | public void init(Cluster cluster) { |
| 89 | - // nothing | |
| 73 | + cluster.register(percentileTracker); | |
| 90 | 74 | } |
| 91 | 75 | |
| 92 | 76 | @Override |