kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 9d8a5f7c4111bac45245d37babdd2eb929a393763135a41d938e14a371cf8f16 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-02-08 16:18 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-03 16:54 +01:00 |
| Parents | 5e235af049f6 |
8 files changed
modified changelog/README.md +1 −0
| @@ -26,6 +26,7 @@ | ||
| 26 | 26 | - [bug] JAVA-819: Expose more errors in RetryPolicy + provide idempotent-aware wrapper. |
| 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 | +- [improvement] JAVA-888: Add cluster-wide percentile tracker. | |
| 29 | 30 | |
| 30 | 31 | Merged from 2.0 branch: |
| 31 | 32 |
modified clirr-ignores.xml +118 −42
| @@ -7,50 +7,126 @@ | ||
| 7 | 7 | * run `mvn clirr:clirr` at the root directory |
| 8 | 8 | * check the reports in `<module>/target/site/clirr-report.html` |
| 9 | 9 | * add new differences if needed. Difference types are explained at http://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html |
| 10 | + | |
| 11 | + The formatter introduces line breaks that make Clirr unhappy: | |
| 12 | + @formatter:off | |
| 10 | 13 | --> |
| 11 | 14 | <differences> |
| 12 | 15 | |
| 13 | - <difference> | |
| 14 | - <differenceType>6006</differenceType> <!-- field now final --> | |
| 15 | - <className>com/datastax/driver/core/ProtocolVersion</className> | |
| 16 | - <field>NEWEST_SUPPORTED</field> | |
| 17 | - <justification>This was an oversight, this field was never intended to be non final</justification> | |
| 18 | - </difference> | |
| 19 | - | |
| 20 | - <difference> | |
| 21 | - <differenceType>8001</differenceType> <!-- class removed --> | |
| 22 | - <className>com/datastax/driver/core/schemabuilder/ColumnType$NativeColumnType</className> | |
| 23 | - <justification>This class was accidentally exposed, it was meant to be package-private</justification> | |
| 24 | - </difference> | |
| 25 | - | |
| 26 | - <difference> | |
| 27 | - <differenceType>1001</differenceType> <!-- decreased visibility --> | |
| 28 | - <className>com/datastax/driver/core/HostConnectionPool$Phase</className> | |
| 29 | - <justification>False positive: HostConnectionPool is not exposed to clients</justification> | |
| 30 | - </difference> | |
| 31 | - | |
| 32 | - <difference> | |
| 33 | - <differenceType>1001</differenceType> <!-- decreased visibility --> | |
| 34 | - <className>com/datastax/driver/core/ConvictionPolicy$Factory</className> | |
| 35 | - <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 36 | - </difference> | |
| 37 | - | |
| 38 | - <difference> | |
| 39 | - <differenceType>8001</differenceType> <!-- class removed --> | |
| 40 | - <className>com/datastax/driver/core/ConvictionPolicy$Simple</className> | |
| 41 | - <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 42 | - </difference> | |
| 43 | - | |
| 44 | - <difference> | |
| 45 | - <differenceType>8001</differenceType> <!-- class removed --> | |
| 46 | - <className>com/datastax/driver/core/ConvictionPolicy$Simple$Factory</className> | |
| 47 | - <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 48 | - </difference> | |
| 49 | - | |
| 50 | - <difference> | |
| 51 | - <differenceType>1001</differenceType> <!-- decreased visibility --> | |
| 52 | - <className>com/datastax/driver/mapping/ColumnMapper$Kind</className> | |
| 53 | - <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 54 | - </difference> | |
| 16 | + <difference> | |
| 17 | + <differenceType>6006</differenceType> <!-- field now final --> | |
| 18 | + <className>com/datastax/driver/core/ProtocolVersion</className> | |
| 19 | + <field>NEWEST_SUPPORTED</field> | |
| 20 | + <justification>This was an oversight, this field was never intended to be non final</justification> | |
| 21 | + </difference> | |
| 22 | + | |
| 23 | + <difference> | |
| 24 | + <differenceType>8001</differenceType> <!-- class removed --> | |
| 25 | + <className>com/datastax/driver/core/schemabuilder/ColumnType$NativeColumnType</className> | |
| 26 | + <justification>This class was accidentally exposed, it was meant to be package-private</justification> | |
| 27 | + </difference> | |
| 28 | + | |
| 29 | + <difference> | |
| 30 | + <differenceType>1001</differenceType> <!-- decreased visibility --> | |
| 31 | + <className>com/datastax/driver/core/HostConnectionPool$Phase</className> | |
| 32 | + <justification>False positive: HostConnectionPool is not exposed to clients</justification> | |
| 33 | + </difference> | |
| 34 | + | |
| 35 | + <difference> | |
| 36 | + <differenceType>1001</differenceType> <!-- decreased visibility --> | |
| 37 | + <className>com/datastax/driver/core/ConvictionPolicy$Factory</className> | |
| 38 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 39 | + </difference> | |
| 40 | + | |
| 41 | + <difference> | |
| 42 | + <differenceType>8001</differenceType> <!-- class removed --> | |
| 43 | + <className>com/datastax/driver/core/ConvictionPolicy$Simple</className> | |
| 44 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 45 | + </difference> | |
| 46 | + | |
| 47 | + <difference> | |
| 48 | + <differenceType>8001</differenceType> <!-- class removed --> | |
| 49 | + <className>com/datastax/driver/core/ConvictionPolicy$Simple$Factory</className> | |
| 50 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 51 | + </difference> | |
| 52 | + | |
| 53 | + <difference> | |
| 54 | + <differenceType>1001</differenceType> <!-- decreased visibility --> | |
| 55 | + <className>com/datastax/driver/mapping/ColumnMapper$Kind</className> | |
| 56 | + <justification>False positive, the enclosing class is package-private so this was never exposed</justification> | |
| 57 | + </difference> | |
| 58 | + | |
| 59 | + <difference> | |
| 60 | + <differenceType>7002</differenceType> <!-- method removed --> | |
| 61 | + <className>com/datastax/driver/core/PerHostPercentileTracker</className> | |
| 62 | + <method>com.datastax.driver.core.PerHostPercentileTracker$Builder builderWithHighestTrackableLatencyMillis(long) | |
| 63 | + </method> | |
| 64 | + <justification>Renamed (API was marked as beta and still subject to change)</justification> | |
| 65 | + </difference> | |
| 66 | + | |
| 67 | + <difference> | |
| 68 | + <differenceType>7002</differenceType> <!-- method removed --> | |
| 69 | + <className>com/datastax/driver/core/PerHostPercentileTracker</className> | |
| 70 | + <method>long getLatencyAtPercentile(com.datastax.driver.core.Host, double)</method> | |
| 71 | + <justification>Moved to new parent class with more parameters (API was marked as beta and still subject to change)</justification> | |
| 72 | + </difference> | |
| 73 | + | |
| 74 | + <difference> | |
| 75 | + <differenceType>7002</differenceType> <!-- method removed --> | |
| 76 | + <className>com/datastax/driver/core/PerHostPercentileTracker$Builder</className> | |
| 77 | + <method>com.datastax.driver.core.PerHostPercentileTracker$Builder withInterval(long, java.util.concurrent.TimeUnit)</method> | |
| 78 | + <justification>False positive, method now inherited from generic parent class</justification> | |
| 79 | + </difference> | |
| 80 | + | |
| 81 | + <difference> | |
| 82 | + <differenceType>7002</differenceType> <!-- method removed --> | |
| 83 | + <className>com/datastax/driver/core/PerHostPercentileTracker$Builder</className> | |
| 84 | + <method>com.datastax.driver.core.PerHostPercentileTracker$Builder withMinRecordedValues(int)</method> | |
| 85 | + <justification>False positive, method now inherited from generic parent class</justification> | |
| 86 | + </difference> | |
| 87 | + | |
| 88 | + <difference> | |
| 89 | + <differenceType>7002</differenceType> <!-- method removed --> | |
| 90 | + <className>com/datastax/driver/core/PerHostPercentileTracker$Builder</className> | |
| 91 | + <method>com.datastax.driver.core.PerHostPercentileTracker$Builder withNumberOfSignificantValueDigits(int)</method> | |
| 92 | + <justification>False positive, method now inherited from generic parent class</justification> | |
| 93 | + </difference> | |
| 94 | + | |
| 95 | + <difference> | |
| 96 | + <differenceType>7002</differenceType> <!-- method removed --> | |
| 97 | + <className>com/datastax/driver/core/PerHostPercentileTracker$Builder</className> | |
| 98 | + <method>com.datastax.driver.core.PerHostPercentileTracker$Builder withNumberOfHosts(int)</method> | |
| 99 | + <justification>Removed (API was marked as beta and still subject to change)</justification> | |
| 100 | + </difference> | |
| 101 | + | |
| 102 | + <difference> | |
| 103 | + <differenceType>7005</differenceType> <!-- method argument type changed --> | |
| 104 | + <className>com/datastax/driver/core/QueryLogger$Builder</className> | |
| 105 | + <method>com.datastax.driver.core.QueryLogger$Builder withDynamicThreshold(com.datastax.driver.core.PerHostPercentileTracker, double)</method> | |
| 106 | + <to>com.datastax.driver.core.QueryLogger$Builder withDynamicThreshold(com.datastax.driver.core.PercentileTracker, double)</to> | |
| 107 | + <justification>Introduced more generic parent type PercentileTracker (API was marked as beta and still subject to change)</justification> | |
| 108 | + </difference> | |
| 109 | + | |
| 110 | + <difference> | |
| 111 | + <differenceType>7002</differenceType> <!-- method removed --> | |
| 112 | + <className>com/datastax/driver/core/QueryLogger$DynamicThresholdQueryLogger</className> | |
| 113 | + <method>com.datastax.driver.core.PerHostPercentileTracker getPerHostPercentileLatencyTracker()</method> | |
| 114 | + <justification>Introduced more generic parent type PercentileTracker (API was marked as beta and still subject to change)</justification> | |
| 115 | + </difference> | |
| 116 | + | |
| 117 | + <difference> | |
| 118 | + <differenceType>7002</differenceType> <!-- method removed --> | |
| 119 | + <className>com/datastax/driver/core/QueryLogger$DynamicThresholdQueryLogger</className> | |
| 120 | + <method>void setPerHostPercentileLatencyTracker(com.datastax.driver.core.PerHostPercentileTracker)</method> | |
| 121 | + <justification>Introduced more generic parent type PercentileTracker (API was marked as beta and still subject to change)</justification> | |
| 122 | + </difference> | |
| 123 | + | |
| 124 | + <difference> | |
| 125 | + <differenceType>7005</differenceType> <!-- method argument type changed --> | |
| 126 | + <className>com/datastax/driver/core/policies/PercentileSpeculativeExecutionPolicy</className> | |
| 127 | + <method>PercentileSpeculativeExecutionPolicy(com.datastax.driver.core.PerHostPercentileTracker, double, int)</method> | |
| 128 | + <to>*</to> <!-- didn't find a way to write the new signature --> | |
| 129 | + <justification>Introduced more generic parent type PercentileTracker (API was marked as beta and still subject to change)</justification> | |
| 130 | + </difference> | |
| 55 | 131 | |
| 56 | 132 | </differences> |
added driver-core/src/main/java/com/datastax/driver/core/ClusterWidePercentileTracker.java +76 −0
| @@ -0,0 +1,76 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 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 | + * A {@code PercentileTracker} that aggregates all measurements into a single histogram. | |
| 20 | + * <p/> | |
| 21 | + * This gives you global latency percentiles for the whole cluster, meaning that latencies of slower hosts will tend to | |
| 22 | + * appear in higher percentiles. | |
| 23 | + */ | |
| 24 | +public class ClusterWidePercentileTracker extends PercentileTracker { | |
| 25 | + private volatile Cluster cluster; | |
| 26 | + | |
| 27 | + private ClusterWidePercentileTracker(long highestTrackableLatencyMillis, | |
| 28 | + int numberOfSignificantValueDigits, | |
| 29 | + int minRecordedValues, | |
| 30 | + long intervalMs) { | |
| 31 | + super(highestTrackableLatencyMillis, numberOfSignificantValueDigits, minRecordedValues, intervalMs); | |
| 32 | + } | |
| 33 | + | |
| 34 | + @Override | |
| 35 | + public void onRegister(Cluster cluster) { | |
| 36 | + this.cluster = cluster; | |
| 37 | + } | |
| 38 | + | |
| 39 | + @Override | |
| 40 | + protected Cluster computeKey(Host host, Statement statement, Exception exception) { | |
| 41 | + return cluster; | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Returns a builder to create a new instance. | |
| 46 | + * | |
| 47 | + * @param highestTrackableLatencyMillis the highest expected latency. If a higher value is reported, it will be | |
| 48 | + * ignored and a warning will be logged. A good rule of thumb is to set it | |
| 49 | + * slightly higher than {@link SocketOptions#getReadTimeoutMillis()}. | |
| 50 | + * @return the builder. | |
| 51 | + */ | |
| 52 | + public static Builder builder(long highestTrackableLatencyMillis) { | |
| 53 | + return new Builder(highestTrackableLatencyMillis); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Helper class to build {@code PerHostPercentileTracker} instances with a fluent interface. | |
| 58 | + */ | |
| 59 | + public static class Builder extends PercentileTracker.Builder<Builder, ClusterWidePercentileTracker> { | |
| 60 | + | |
| 61 | + Builder(long highestTrackableLatencyMillis) { | |
| 62 | + super(highestTrackableLatencyMillis); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + protected Builder self() { | |
| 67 | + return this; | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public ClusterWidePercentileTracker build() { | |
| 72 | + return new ClusterWidePercentileTracker(highestTrackableLatencyMillis, numberOfSignificantValueDigits, | |
| 73 | + minRecordedValues, intervalMs); | |
| 74 | + } | |
| 75 | + } | |
| 76 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/PerHostPercentileTracker.java +23 −270
| @@ -15,301 +15,54 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.exceptions.*; | |
| 19 | -import com.google.common.annotations.Beta; | |
| 20 | -import com.google.common.collect.ImmutableSet; | |
| 21 | -import com.google.common.collect.MapMaker; | |
| 22 | -import com.google.common.util.concurrent.Futures; | |
| 23 | -import com.google.common.util.concurrent.ListenableFuture; | |
| 24 | -import com.google.common.util.concurrent.SettableFuture; | |
| 25 | -import org.HdrHistogram.Histogram; | |
| 26 | -import org.HdrHistogram.Recorder; | |
| 27 | -import org.slf4j.Logger; | |
| 28 | -import org.slf4j.LoggerFactory; | |
| 29 | - | |
| 30 | -import java.util.Set; | |
| 31 | -import java.util.concurrent.ConcurrentMap; | |
| 32 | -import java.util.concurrent.ExecutionException; | |
| 33 | -import java.util.concurrent.TimeUnit; | |
| 34 | -import java.util.concurrent.TimeoutException; | |
| 35 | - | |
| 36 | -import static com.google.common.base.Preconditions.checkArgument; | |
| 37 | -import static java.util.concurrent.TimeUnit.*; | |
| 38 | - | |
| 39 | 18 | /** |
| 40 | - * A {@link LatencyTracker} that records latencies for each host over a sliding time interval, and exposes an | |
| 41 | - * API to retrieve the latency at a given percentile. | |
| 19 | + * A {@code PercentileTracker} that maintains a separate histogram for each host. | |
| 42 | 20 | * <p/> |
| 43 | - * To use this class, build an instance with {@link #builderWithHighestTrackableLatencyMillis(long)} and register | |
| 44 | - * it with your {@link com.datastax.driver.core.Cluster} instance: | |
| 45 | - * <pre> | |
| 46 | - * PerHostPercentileTracker tracker = PerHostPercentileTracker | |
| 47 | - * .builderWithHighestTrackableLatencyMillis(15000) | |
| 48 | - * .build(); | |
| 49 | - * | |
| 50 | - * cluster.register(tracker); | |
| 51 | - * ... | |
| 52 | - * tracker.getLatencyAtPercentile(host1, 99.0); | |
| 53 | - * </pre> | |
| 54 | - * <p/> | |
| 55 | - * This class uses <a href="http://hdrhistogram.github.io/HdrHistogram/">HdrHistogram</a> to record latencies: | |
| 56 | - * for each host, there is a "live" histogram where current latencies are recorded, and a "cached", read-only histogram | |
| 57 | - * that is used when clients call {@link #getLatencyAtPercentile(Host, double)}. Each time the cached histogram becomes | |
| 58 | - * older than the interval, the two histograms are switched. Note that statistics will not be available during the first | |
| 59 | - * interval at cluster startup, since we don't have a cached histogram yet. | |
| 60 | - * <p/> | |
| 61 | - * Note that this class is currently marked "beta": it hasn't been extensively tested yet, and the API is still subject | |
| 62 | - * to change. | |
| 21 | + * This gives you per-host latency percentiles, meaning that each host will only be compared to itself. | |
| 63 | 22 | */ |
| 64 | -@Beta | |
| 65 | -public class PerHostPercentileTracker implements LatencyTracker { | |
| 66 | - private static final Logger logger = LoggerFactory.getLogger(PerHostPercentileTracker.class); | |
| 67 | - | |
| 68 | - private final ConcurrentMap<Host, Recorder> recorders; | |
| 69 | - private final ConcurrentMap<Host, CachedHistogram> cachedHistograms; | |
| 70 | - private final long highestTrackableLatencyMillis; | |
| 71 | - private final int numberOfSignificantValueDigits; | |
| 72 | - private final int minRecordedValues; | |
| 73 | - private final long intervalMs; | |
| 74 | - | |
| 75 | - private PerHostPercentileTracker(long highestTrackableLatencyMillis, int numberOfSignificantValueDigits, | |
| 76 | - int numberOfHosts, | |
| 23 | +public class PerHostPercentileTracker extends PercentileTracker { | |
| 24 | + private PerHostPercentileTracker(long highestTrackableLatencyMillis, | |
| 25 | + int numberOfSignificantValueDigits, | |
| 77 | 26 | int minRecordedValues, |
| 78 | 27 | long intervalMs) { |
| 79 | - this.highestTrackableLatencyMillis = highestTrackableLatencyMillis; | |
| 80 | - this.numberOfSignificantValueDigits = numberOfSignificantValueDigits; | |
| 81 | - this.minRecordedValues = minRecordedValues; | |
| 82 | - this.intervalMs = intervalMs; | |
| 83 | - this.recorders = new MapMaker().initialCapacity(numberOfHosts).makeMap(); | |
| 84 | - this.cachedHistograms = new MapMaker().initialCapacity(numberOfHosts).makeMap(); | |
| 28 | + super(highestTrackableLatencyMillis, numberOfSignificantValueDigits, minRecordedValues, intervalMs); | |
| 29 | + } | |
| 30 | + | |
| 31 | + @Override | |
| 32 | + protected Host computeKey(Host host, Statement statement, Exception exception) { | |
| 33 | + return host; | |
| 85 | 34 | } |
| 86 | 35 | |
| 87 | 36 | /** |
| 88 | 37 | * Returns a builder to create a new instance. |
| 89 | 38 | * |
| 90 | - * @param highestTrackableLatencyMillis the highest expected latency. If a higher value is reported, it will be ignored and a | |
| 91 | - * warning will be logged. A good rule of thumb is to set it slightly higher than | |
| 92 | - * {@link SocketOptions#getReadTimeoutMillis()}. | |
| 39 | + * @param highestTrackableLatencyMillis the highest expected latency. If a higher value is reported, it will be | |
| 40 | + * ignored and a warning will be logged. A good rule of thumb is to set it | |
| 41 | + * slightly higher than {@link SocketOptions#getReadTimeoutMillis()}. | |
| 93 | 42 | * @return the builder. |
| 94 | 43 | */ |
| 95 | - public static Builder builderWithHighestTrackableLatencyMillis(long highestTrackableLatencyMillis) { | |
| 44 | + public static Builder builder(long highestTrackableLatencyMillis) { | |
| 96 | 45 | return new Builder(highestTrackableLatencyMillis); |
| 97 | 46 | } |
| 98 | 47 | |
| 99 | 48 | /** |
| 100 | - * Helper class to builder {@code PerHostPercentileTracker} instances with a fluent interface. | |
| 49 | + * Helper class to build {@code PerHostPercentileTracker} instances with a fluent interface. | |
| 101 | 50 | */ |
| 102 | - public static class Builder { | |
| 103 | - private final long highestTrackableLatencyMillis; | |
| 104 | - private int numberOfSignificantValueDigits = 3; | |
| 105 | - private int minRecordedValues = 1000; | |
| 106 | - private int numberOfHosts = 16; | |
| 107 | - private long intervalMs = MINUTES.toMillis(5); | |
| 51 | + public static class Builder extends PercentileTracker.Builder<Builder, PerHostPercentileTracker> { | |
| 108 | 52 | |
| 109 | 53 | Builder(long highestTrackableLatencyMillis) { |
| 110 | - this.highestTrackableLatencyMillis = highestTrackableLatencyMillis; | |
| 111 | - } | |
| 112 | - | |
| 113 | - /** | |
| 114 | - * Sets the number of significant decimal digits to which histograms will maintain value | |
| 115 | - * resolution and separation. This must be an integer between 0 and 5. | |
| 116 | - * <p/> | |
| 117 | - * If not set explicitly, this value defaults to 3. | |
| 118 | - * <p/> | |
| 119 | - * See <a href="http://hdrhistogram.github.io/HdrHistogram/JavaDoc/org/HdrHistogram/Histogram.html">the HdrHistogram Javadocs</a> | |
| 120 | - * for a more detailed explanation on how this parameter affects the resolution of recorded samples. | |
| 121 | - * | |
| 122 | - * @param numberOfSignificantValueDigits the new value. | |
| 123 | - * @return this builder. | |
| 124 | - */ | |
| 125 | - public Builder withNumberOfSignificantValueDigits(int numberOfSignificantValueDigits) { | |
| 126 | - this.numberOfSignificantValueDigits = numberOfSignificantValueDigits; | |
| 127 | - return this; | |
| 128 | - } | |
| 129 | - | |
| 130 | - /** | |
| 131 | - * Sets the minimum number of values that must be recorded for a host before we consider | |
| 132 | - * the sample size significant. | |
| 133 | - * <p/> | |
| 134 | - * If this count is not reached during a given interval, {@link #getLatencyAtPercentile(Host, double)} | |
| 135 | - * will return a negative value, indicating that statistics are not available. In particular, this is true | |
| 136 | - * during the first interval. | |
| 137 | - * <p/> | |
| 138 | - * If not set explicitly, this value default to 1000. | |
| 139 | - * | |
| 140 | - * @param minRecordedValues the new value. | |
| 141 | - * @return this builder. | |
| 142 | - */ | |
| 143 | - public Builder withMinRecordedValues(int minRecordedValues) { | |
| 144 | - this.minRecordedValues = minRecordedValues; | |
| 145 | - return this; | |
| 146 | - } | |
| 147 | - | |
| 148 | - /** | |
| 149 | - * Sets the number of distinct hosts that the driver will ever connect to. | |
| 150 | - * <p/> | |
| 151 | - * This parameter is only used to pre-size internal maps in order to avoid unnecessary rehashing. | |
| 152 | - * <p/> | |
| 153 | - * If not set explicitly, this value defaults to 16. | |
| 154 | - * | |
| 155 | - * @param numberOfHosts the new value. | |
| 156 | - * @return this builder. | |
| 157 | - */ | |
| 158 | - public Builder withNumberOfHosts(int numberOfHosts) { | |
| 159 | - this.numberOfHosts = numberOfHosts; | |
| 160 | - return this; | |
| 54 | + super(highestTrackableLatencyMillis); | |
| 161 | 55 | } |
| 162 | 56 | |
| 163 | - /** | |
| 164 | - * Sets the time interval over which samples are recorded. | |
| 165 | - * <p/> | |
| 166 | - * For each host, there is a "live" histogram where current latencies are recorded, and a "cached", read-only histogram | |
| 167 | - * that is used when clients call {@link #getLatencyAtPercentile(Host, double)}. Each time the cached histogram becomes | |
| 168 | - * older than the interval, the two histograms are switched. Note that statistics will not be available during the first | |
| 169 | - * interval at cluster startup, since we don't have a cached histogram yet. | |
| 170 | - * <p/> | |
| 171 | - * If not set explicitly, this value defaults to 5 minutes. | |
| 172 | - * | |
| 173 | - * @param interval the new interval. | |
| 174 | - * @param unit the unit that the interval is expressed in. | |
| 175 | - * @return this builder. | |
| 176 | - */ | |
| 177 | - public Builder withInterval(long interval, TimeUnit unit) { | |
| 178 | - this.intervalMs = MILLISECONDS.convert(interval, unit); | |
| 57 | + @Override | |
| 58 | + protected Builder self() { | |
| 179 | 59 | return this; |
| 180 | 60 | } |
| 181 | 61 | |
| 182 | - /** | |
| 183 | - * Builds the {@code PerHostPercentileTracker} instance configured with this builder. | |
| 184 | - * | |
| 185 | - * @return the instance. | |
| 186 | - */ | |
| 62 | + @Override | |
| 187 | 63 | public PerHostPercentileTracker build() { |
| 188 | - return new PerHostPercentileTracker(highestTrackableLatencyMillis, numberOfSignificantValueDigits, numberOfHosts, minRecordedValues, intervalMs); | |
| 189 | - } | |
| 190 | - } | |
| 191 | - | |
| 192 | - @Override | |
| 193 | - public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) { | |
| 194 | - if (!shouldConsiderNewLatency(statement, exception)) | |
| 195 | - return; | |
| 196 | - | |
| 197 | - long latencyMs = NANOSECONDS.toMillis(newLatencyNanos); | |
| 198 | - try { | |
| 199 | - getRecorder(host).recordValue(latencyMs); | |
| 200 | - } catch (ArrayIndexOutOfBoundsException e) { | |
| 201 | - logger.warn("Got request with latency of {} ms, which exceeds the configured maximum trackable value {}", | |
| 202 | - latencyMs, highestTrackableLatencyMillis); | |
| 203 | - } | |
| 204 | - } | |
| 205 | - | |
| 206 | - /** | |
| 207 | - * Returns the request latency for a host at a given percentile. | |
| 208 | - * | |
| 209 | - * @param host the host. | |
| 210 | - * @param percentile the percentile (for example, {@code 99.0} for the 99th percentile). | |
| 211 | - * @return the latency (in milliseconds) at the given percentile, or a negative value if it's not available yet. | |
| 212 | - */ | |
| 213 | - public long getLatencyAtPercentile(Host host, double percentile) { | |
| 214 | - checkArgument(percentile >= 0.0 && percentile < 100, | |
| 215 | - "percentile must be between 0.0 and 100 (was %f)"); | |
| 216 | - Histogram histogram = getLastIntervalHistogram(host); | |
| 217 | - if (histogram == null || histogram.getTotalCount() < minRecordedValues) | |
| 218 | - return -1; | |
| 219 | - | |
| 220 | - return histogram.getValueAtPercentile(percentile); | |
| 221 | - } | |
| 222 | - | |
| 223 | - private Recorder getRecorder(Host host) { | |
| 224 | - Recorder recorder = recorders.get(host); | |
| 225 | - if (recorder == null) { | |
| 226 | - recorder = new Recorder(highestTrackableLatencyMillis, numberOfSignificantValueDigits); | |
| 227 | - Recorder old = recorders.putIfAbsent(host, recorder); | |
| 228 | - if (old != null) { | |
| 229 | - // We got beaten at creating the recorder, use the actual instance and discard ours | |
| 230 | - recorder = old; | |
| 231 | - } else { | |
| 232 | - // Also set an empty cache entry to remember the time we started recording: | |
| 233 | - cachedHistograms.putIfAbsent(host, CachedHistogram.empty()); | |
| 234 | - } | |
| 235 | - } | |
| 236 | - return recorder; | |
| 237 | - } | |
| 238 | - | |
| 239 | - /** | |
| 240 | - * @return null if no histogram is available yet (no entries recorded, or not for long enough) | |
| 241 | - */ | |
| 242 | - private Histogram getLastIntervalHistogram(Host host) { | |
| 243 | - try { | |
| 244 | - while (true) { | |
| 245 | - CachedHistogram entry = cachedHistograms.get(host); | |
| 246 | - if (entry == null) | |
| 247 | - return null; | |
| 248 | - | |
| 249 | - long age = System.currentTimeMillis() - entry.timestamp; | |
| 250 | - if (age < intervalMs) { // current histogram is recent enough | |
| 251 | - return entry.histogram.get(); | |
| 252 | - } else { // need to refresh | |
| 253 | - Recorder recorder = recorders.get(host); | |
| 254 | - // intervalMs should be much larger than the time it takes to replace a histogram, so this future should never block | |
| 255 | - Histogram staleHistogram = entry.histogram.get(0, MILLISECONDS); | |
| 256 | - SettableFuture<Histogram> future = SettableFuture.create(); | |
| 257 | - CachedHistogram newEntry = new CachedHistogram(future); | |
| 258 | - if (cachedHistograms.replace(host, entry, newEntry)) { | |
| 259 | - // Only get the new histogram if we successfully replaced the cache entry. | |
| 260 | - // This ensures that only one thread will do it. | |
| 261 | - Histogram newHistogram = recorder.getIntervalHistogram(staleHistogram); | |
| 262 | - future.set(newHistogram); | |
| 263 | - return newHistogram; | |
| 264 | - } | |
| 265 | - // If we couldn't replace the entry it means we raced, so loop to try again | |
| 266 | - } | |
| 267 | - } | |
| 268 | - } catch (InterruptedException e) { | |
| 269 | - Thread.currentThread().interrupt(); | |
| 270 | - return null; | |
| 271 | - } catch (ExecutionException e) { | |
| 272 | - throw new DriverInternalError("Unexpected error", e.getCause()); | |
| 273 | - } catch (TimeoutException e) { | |
| 274 | - throw new DriverInternalError("Unexpected timeout while getting histogram", e); | |
| 275 | - } | |
| 276 | - } | |
| 277 | - | |
| 278 | - static class CachedHistogram { | |
| 279 | - final ListenableFuture<Histogram> histogram; | |
| 280 | - final long timestamp; | |
| 281 | - | |
| 282 | - CachedHistogram(ListenableFuture<Histogram> histogram) { | |
| 283 | - this.histogram = histogram; | |
| 284 | - this.timestamp = System.currentTimeMillis(); | |
| 64 | + return new PerHostPercentileTracker(highestTrackableLatencyMillis, numberOfSignificantValueDigits, | |
| 65 | + minRecordedValues, intervalMs); | |
| 285 | 66 | } |
| 286 | - | |
| 287 | - static CachedHistogram empty() { | |
| 288 | - return new CachedHistogram(Futures.<Histogram>immediateFuture(null)); | |
| 289 | - } | |
| 290 | - } | |
| 291 | - | |
| 292 | - // TODO this was copy/pasted from LatencyAwarePolicy, maybe it could be refactored as a shared method | |
| 293 | - private boolean shouldConsiderNewLatency(Statement statement, Exception exception) { | |
| 294 | - // query was successful: always consider | |
| 295 | - if (exception == null) | |
| 296 | - return true; | |
| 297 | - // filter out "fast" errors | |
| 298 | - return !EXCLUDED_EXCEPTIONS.contains(exception.getClass()); | |
| 299 | 67 | } |
| 300 | - | |
| 301 | - /** | |
| 302 | - * A set of DriverException subclasses that we should prevent from updating the host's score. | |
| 303 | - * The intent behind it is to filter out "fast" errors: when a host replies with such errors, | |
| 304 | - * it usually does so very quickly, because it did not involve any actual | |
| 305 | - * coordination work. Such errors are not good indicators of the host's responsiveness, | |
| 306 | - * and tend to make the host's score look better than it actually is. | |
| 307 | - */ | |
| 308 | - private static final Set<Class<? extends Exception>> EXCLUDED_EXCEPTIONS = ImmutableSet.<Class<? extends Exception>>of( | |
| 309 | - UnavailableException.class, // this is done via the snitch and is usually very fast | |
| 310 | - OverloadedException.class, | |
| 311 | - BootstrappingException.class, | |
| 312 | - UnpreparedException.class, | |
| 313 | - QueryValidationException.class // query validation also happens at early stages in the coordinator | |
| 314 | - ); | |
| 315 | 68 | } |
added driver-core/src/main/java/com/datastax/driver/core/PercentileTracker.java +343 −0
| @@ -0,0 +1,343 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 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 | +import com.datastax.driver.core.exceptions.*; | |
| 19 | +import com.google.common.collect.ImmutableSet; | |
| 20 | +import com.google.common.util.concurrent.Futures; | |
| 21 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 22 | +import com.google.common.util.concurrent.SettableFuture; | |
| 23 | +import org.HdrHistogram.Histogram; | |
| 24 | +import org.HdrHistogram.Recorder; | |
| 25 | +import org.slf4j.Logger; | |
| 26 | +import org.slf4j.LoggerFactory; | |
| 27 | + | |
| 28 | +import java.util.Set; | |
| 29 | +import java.util.concurrent.*; | |
| 30 | + | |
| 31 | +import static com.google.common.base.Preconditions.checkArgument; | |
| 32 | +import static java.util.concurrent.TimeUnit.*; | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * A {@link LatencyTracker} that records query latencies over a sliding time interval, and exposes an API to retrieve | |
| 36 | + * the latency at a given percentile. | |
| 37 | + * <p/> | |
| 38 | + * Percentiles may be computed separately for different categories of requests; this is implementation-dependent and | |
| 39 | + * determined by {@link #computeKey(Host, Statement, Exception)}. | |
| 40 | + * <p/> | |
| 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. | |
| 45 | + * <p/> | |
| 46 | + * This class uses <a href="http://hdrhistogram.github.io/HdrHistogram/">HdrHistogram</a> to record latencies: | |
| 47 | + * for each category, there is a "live" histogram where current latencies are recorded, and a "cached", read-only | |
| 48 | + * histogram that is used when clients call {@link #getLatencyAtPercentile(Host, Statement, Exception, double)}. Each | |
| 49 | + * time the cached histogram becomes older than the interval, the two histograms are switched. Statistics will not be | |
| 50 | + * available during the first interval at cluster startup, since we don't have a cached histogram yet. | |
| 51 | + */ | |
| 52 | +public abstract class PercentileTracker implements LifecycleAwareLatencyTracker { | |
| 53 | + private static final Logger logger = LoggerFactory.getLogger(PercentileTracker.class); | |
| 54 | + | |
| 55 | + private final long highestTrackableLatencyMillis; | |
| 56 | + private final int numberOfSignificantValueDigits; | |
| 57 | + private final int minRecordedValues; | |
| 58 | + private final long intervalMs; | |
| 59 | + | |
| 60 | + // The "live" recorders: this is where we store the latencies received from the cluster | |
| 61 | + private final ConcurrentMap<Object, Recorder> recorders; | |
| 62 | + // The cached histograms, corresponding to the previous interval. This is where we get the percentiles from when the | |
| 63 | + // user requests them. Each histogram is valid for a given duration, when it gets stale we request a new one from | |
| 64 | + // the corresponding recorder. | |
| 65 | + private final ConcurrentMap<Object, CachedHistogram> cachedHistograms; | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Builds a new instance. | |
| 69 | + * | |
| 70 | + * @see Builder | |
| 71 | + */ | |
| 72 | + protected PercentileTracker(long highestTrackableLatencyMillis, | |
| 73 | + int numberOfSignificantValueDigits, | |
| 74 | + int minRecordedValues, | |
| 75 | + long intervalMs) { | |
| 76 | + this.highestTrackableLatencyMillis = highestTrackableLatencyMillis; | |
| 77 | + this.numberOfSignificantValueDigits = numberOfSignificantValueDigits; | |
| 78 | + this.minRecordedValues = minRecordedValues; | |
| 79 | + this.intervalMs = intervalMs; | |
| 80 | + this.recorders = new ConcurrentHashMap<Object, Recorder>(); | |
| 81 | + this.cachedHistograms = new ConcurrentHashMap<Object, CachedHistogram>(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Computes a key used to categorize measurements. Measurements with the same key will be recorded in the same | |
| 86 | + * histogram. | |
| 87 | + * <p/> | |
| 88 | + * It's recommended to keep the number of distinct keys low, in order to limit the memory footprint of the | |
| 89 | + * histograms. | |
| 90 | + * | |
| 91 | + * @param host the host that was queried. | |
| 92 | + * @param statement the statement that was executed. | |
| 93 | + * @param exception if the query failed, the corresponding exception. | |
| 94 | + * @return the key. | |
| 95 | + */ | |
| 96 | + protected abstract Object computeKey(Host host, Statement statement, Exception exception); | |
| 97 | + | |
| 98 | + @Override | |
| 99 | + public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) { | |
| 100 | + if (!include(host, statement, exception)) | |
| 101 | + return; | |
| 102 | + | |
| 103 | + long latencyMs = NANOSECONDS.toMillis(newLatencyNanos); | |
| 104 | + try { | |
| 105 | + Recorder recorder = getRecorder(host, statement, exception); | |
| 106 | + if (recorder != null) | |
| 107 | + recorder.recordValue(latencyMs); | |
| 108 | + } catch (ArrayIndexOutOfBoundsException e) { | |
| 109 | + logger.warn("Got request with latency of {} ms, which exceeds the configured maximum trackable value {}", | |
| 110 | + latencyMs, highestTrackableLatencyMillis); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Returns the request latency at a given percentile. | |
| 116 | + * | |
| 117 | + * @param host the host (if this is relevant in the way percentiles are categorized). | |
| 118 | + * @param statement the statement (if this is relevant in the way percentiles are categorized). | |
| 119 | + * @param exception the exception (if this is relevant in the way percentiles are categorized). | |
| 120 | + * @param percentile the percentile (for example, {@code 99.0} for the 99th percentile). | |
| 121 | + * @return the latency (in milliseconds) at the given percentile, or a negative value if it's not available yet. | |
| 122 | + * @see #computeKey(Host, Statement, Exception) | |
| 123 | + */ | |
| 124 | + public long getLatencyAtPercentile(Host host, Statement statement, Exception exception, double percentile) { | |
| 125 | + checkArgument(percentile >= 0.0 && percentile < 100, | |
| 126 | + "percentile must be between 0.0 and 100 (was %s)", percentile); | |
| 127 | + Histogram histogram = getLastIntervalHistogram(host, statement, exception); | |
| 128 | + if (histogram == null || histogram.getTotalCount() < minRecordedValues) | |
| 129 | + return -1; | |
| 130 | + | |
| 131 | + return histogram.getValueAtPercentile(percentile); | |
| 132 | + } | |
| 133 | + | |
| 134 | + private Recorder getRecorder(Host host, Statement statement, Exception exception) { | |
| 135 | + Object key = computeKey(host, statement, exception); | |
| 136 | + if (key == null) | |
| 137 | + return null; | |
| 138 | + | |
| 139 | + Recorder recorder = recorders.get(key); | |
| 140 | + if (recorder == null) { | |
| 141 | + recorder = new Recorder(highestTrackableLatencyMillis, numberOfSignificantValueDigits); | |
| 142 | + Recorder old = recorders.putIfAbsent(key, recorder); | |
| 143 | + if (old != null) { | |
| 144 | + // We got beaten at creating the recorder, use the actual instance and discard ours | |
| 145 | + recorder = old; | |
| 146 | + } else { | |
| 147 | + // Also set an empty cache entry to remember the time we started recording: | |
| 148 | + cachedHistograms.putIfAbsent(key, CachedHistogram.empty()); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + return recorder; | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * @return null if no histogram is available yet (no entries recorded, or not for long enough) | |
| 156 | + */ | |
| 157 | + private Histogram getLastIntervalHistogram(Host host, Statement statement, Exception exception) { | |
| 158 | + Object key = computeKey(host, statement, exception); | |
| 159 | + if (key == null) | |
| 160 | + return null; | |
| 161 | + | |
| 162 | + try { | |
| 163 | + while (true) { | |
| 164 | + CachedHistogram entry = cachedHistograms.get(key); | |
| 165 | + if (entry == null) | |
| 166 | + return null; | |
| 167 | + | |
| 168 | + long age = System.currentTimeMillis() - entry.timestamp; | |
| 169 | + if (age < intervalMs) { // current histogram is recent enough | |
| 170 | + return entry.histogram.get(); | |
| 171 | + } else { // need to refresh | |
| 172 | + Recorder recorder = recorders.get(key); | |
| 173 | + // intervalMs should be much larger than the time it takes to replace a histogram, so this future should never block | |
| 174 | + Histogram staleHistogram = entry.histogram.get(0, MILLISECONDS); | |
| 175 | + SettableFuture<Histogram> future = SettableFuture.create(); | |
| 176 | + CachedHistogram newEntry = new CachedHistogram(future); | |
| 177 | + if (cachedHistograms.replace(key, entry, newEntry)) { | |
| 178 | + // Only get the new histogram if we successfully replaced the cache entry. | |
| 179 | + // This ensures that only one thread will do it. | |
| 180 | + Histogram newHistogram = recorder.getIntervalHistogram(staleHistogram); | |
| 181 | + future.set(newHistogram); | |
| 182 | + return newHistogram; | |
| 183 | + } | |
| 184 | + // If we couldn't replace the entry it means we raced, so loop to try again | |
| 185 | + } | |
| 186 | + } | |
| 187 | + } catch (InterruptedException e) { | |
| 188 | + Thread.currentThread().interrupt(); | |
| 189 | + return null; | |
| 190 | + } catch (ExecutionException e) { | |
| 191 | + throw new DriverInternalError("Unexpected error", e.getCause()); | |
| 192 | + } catch (TimeoutException e) { | |
| 193 | + throw new DriverInternalError("Unexpected timeout while getting histogram", e); | |
| 194 | + } | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * A histogram and the timestamp at which it was retrieved. | |
| 199 | + * The data is only relevant for (timestamp + intervalMs); after that, the histogram is stale and we want to | |
| 200 | + * retrieve a new one. | |
| 201 | + */ | |
| 202 | + static class CachedHistogram { | |
| 203 | + final ListenableFuture<Histogram> histogram; | |
| 204 | + final long timestamp; | |
| 205 | + | |
| 206 | + CachedHistogram(ListenableFuture<Histogram> histogram) { | |
| 207 | + this.histogram = histogram; | |
| 208 | + this.timestamp = System.currentTimeMillis(); | |
| 209 | + } | |
| 210 | + | |
| 211 | + static CachedHistogram empty() { | |
| 212 | + return new CachedHistogram(Futures.<Histogram>immediateFuture(null)); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + @Override | |
| 217 | + public void onRegister(Cluster cluster) { | |
| 218 | + // nothing by default | |
| 219 | + } | |
| 220 | + | |
| 221 | + @Override | |
| 222 | + public void onUnregister(Cluster cluster) { | |
| 223 | + // nothing by default | |
| 224 | + } | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * Determines whether a particular measurement should be included. | |
| 228 | + * <p/> | |
| 229 | + * This is used to ignore measurements that could skew the statistics; for example, we typically want to ignore | |
| 230 | + * invalid query errors because they have a very low latency and would make a given cluster/host appear faster than | |
| 231 | + * it really is. | |
| 232 | + * | |
| 233 | + * @param host the host that was queried. | |
| 234 | + * @param statement the statement that was executed. | |
| 235 | + * @param exception if the query failed, the corresponding exception. | |
| 236 | + * @return whether the measurement should be included. | |
| 237 | + */ | |
| 238 | + protected boolean include(Host host, Statement statement, Exception exception) { | |
| 239 | + // query was successful: always consider | |
| 240 | + if (exception == null) | |
| 241 | + return true; | |
| 242 | + // filter out "fast" errors | |
| 243 | + // TODO this was copy/pasted from LatencyAwarePolicy, maybe it could be refactored as a shared method | |
| 244 | + return !EXCLUDED_EXCEPTIONS.contains(exception.getClass()); | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * A set of DriverException subclasses that we should prevent from updating the host's score. | |
| 249 | + * The intent behind it is to filter out "fast" errors: when a host replies with such errors, | |
| 250 | + * it usually does so very quickly, because it did not involve any actual | |
| 251 | + * coordination work. Such errors are not good indicators of the host's responsiveness, | |
| 252 | + * and tend to make the host's score look better than it actually is. | |
| 253 | + */ | |
| 254 | + private static final Set<Class<? extends Exception>> EXCLUDED_EXCEPTIONS = ImmutableSet.<Class<? extends Exception>>of( | |
| 255 | + UnavailableException.class, // this is done via the snitch and is usually very fast | |
| 256 | + OverloadedException.class, | |
| 257 | + BootstrappingException.class, | |
| 258 | + UnpreparedException.class, | |
| 259 | + QueryValidationException.class // query validation also happens at early stages in the coordinator | |
| 260 | + ); | |
| 261 | + | |
| 262 | + /** | |
| 263 | + * Base class for {@code PercentileTracker} implementation builders. | |
| 264 | + * | |
| 265 | + * @param <B> the type of the concrete builder implementation. | |
| 266 | + * @param <T> the type of the object to build. | |
| 267 | + */ | |
| 268 | + public static abstract class Builder<B, T> { | |
| 269 | + protected final long highestTrackableLatencyMillis; | |
| 270 | + protected int numberOfSignificantValueDigits = 3; | |
| 271 | + protected int minRecordedValues = 1000; | |
| 272 | + protected long intervalMs = MINUTES.toMillis(5); | |
| 273 | + | |
| 274 | + Builder(long highestTrackableLatencyMillis) { | |
| 275 | + this.highestTrackableLatencyMillis = highestTrackableLatencyMillis; | |
| 276 | + } | |
| 277 | + | |
| 278 | + protected abstract B self(); | |
| 279 | + | |
| 280 | + /** | |
| 281 | + * Sets the number of significant decimal digits to which histograms will maintain value | |
| 282 | + * resolution and separation. This must be an integer between 0 and 5. | |
| 283 | + * <p/> | |
| 284 | + * If not set explicitly, this value defaults to 3. | |
| 285 | + * <p/> | |
| 286 | + * See <a href="http://hdrhistogram.github.io/HdrHistogram/JavaDoc/org/HdrHistogram/Histogram.html">the HdrHistogram Javadocs</a> | |
| 287 | + * for a more detailed explanation on how this parameter affects the resolution of recorded samples. | |
| 288 | + * | |
| 289 | + * @param numberOfSignificantValueDigits the new value. | |
| 290 | + * @return this builder. | |
| 291 | + */ | |
| 292 | + public B withNumberOfSignificantValueDigits(int numberOfSignificantValueDigits) { | |
| 293 | + this.numberOfSignificantValueDigits = numberOfSignificantValueDigits; | |
| 294 | + return self(); | |
| 295 | + } | |
| 296 | + | |
| 297 | + /** | |
| 298 | + * Sets the minimum number of values that must be recorded for a host before we consider | |
| 299 | + * the sample size significant. | |
| 300 | + * <p/> | |
| 301 | + * If this count is not reached during a given interval, | |
| 302 | + * {@link #getLatencyAtPercentile(Host, Statement, Exception, double)} will return a negative value, indicating | |
| 303 | + * that statistics are not available. In particular, this is true during the first interval. | |
| 304 | + * <p/> | |
| 305 | + * If not set explicitly, this value default to 1000. | |
| 306 | + * | |
| 307 | + * @param minRecordedValues the new value. | |
| 308 | + * @return this builder. | |
| 309 | + */ | |
| 310 | + public B withMinRecordedValues(int minRecordedValues) { | |
| 311 | + this.minRecordedValues = minRecordedValues; | |
| 312 | + return self(); | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * Sets the time interval over which samples are recorded. | |
| 317 | + * <p/> | |
| 318 | + * For each host, there is a "live" histogram where current latencies are recorded, and a "cached", read-only | |
| 319 | + * histogram that is used when clients call {@link #getLatencyAtPercentile(Host, Statement, Exception, double)}. | |
| 320 | + * Each time the cached histogram becomes older than the interval, the two histograms are switched. Note that | |
| 321 | + * statistics will not be available during the first interval at cluster startup, since we don't have a cached | |
| 322 | + * histogram yet. | |
| 323 | + * <p/> | |
| 324 | + * If not set explicitly, this value defaults to 5 minutes. | |
| 325 | + * | |
| 326 | + * @param interval the new interval. | |
| 327 | + * @param unit the unit that the interval is expressed in. | |
| 328 | + * @return this builder. | |
| 329 | + */ | |
| 330 | + public B withInterval(long interval, TimeUnit unit) { | |
| 331 | + this.intervalMs = MILLISECONDS.convert(interval, unit); | |
| 332 | + return self(); | |
| 333 | + } | |
| 334 | + | |
| 335 | + /** | |
| 336 | + * Builds the {@code PercentileTracker} instance configured with this builder. | |
| 337 | + * | |
| 338 | + * @return the instance. | |
| 339 | + */ | |
| 340 | + public abstract T build(); | |
| 341 | + } | |
| 342 | + | |
| 343 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +27 −27
| @@ -82,16 +82,16 @@ | ||
| 82 | 82 | * Currently the QueryLogger can track slow queries in two different ways: |
| 83 | 83 | * using a constant threshold in milliseconds (which is the default behavior), |
| 84 | 84 | * or using a dynamic threshold based on per-host percentiles computed by |
| 85 | - * {@link PerHostPercentileTracker}. | |
| 85 | + * {@link PercentileTracker}. | |
| 86 | 86 | * <p/> |
| 87 | 87 | * <b>Note that the dynamic threshold version is currently provided as a beta preview: it hasn't been extensively |
| 88 | 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 PerHostPercentileTracker}, then create your QueryLogger as follows: | |
| 89 | + * an instance of {@link PercentileTracker}, then create your QueryLogger as follows: | |
| 90 | 90 | * <p/> |
| 91 | 91 | * <pre> |
| 92 | 92 | * Cluster cluster = ... |
| 93 | - * // create an instance of PerHostPercentileTracker and register it | |
| 94 | - * PerHostPercentileTracker tracker = ...; | |
| 93 | + * // create an instance of PercentileTracker and register it | |
| 94 | + * PercentileTracker tracker = ...; | |
| 95 | 95 | * cluster.register(tracker); |
| 96 | 96 | * // create an instance of QueryLogger and register it |
| 97 | 97 | * QueryLogger queryLogger = QueryLogger.builder(cluster) |
| @@ -295,7 +295,7 @@ | ||
| 295 | 295 | * to track slow queries. |
| 296 | 296 | * <p/> |
| 297 | 297 | * Dynamic thresholds are based on per-host latency percentiles, as computed |
| 298 | - * by {@link PerHostPercentileTracker}. | |
| 298 | + * by {@link PercentileTracker}. | |
| 299 | 299 | * <p/> |
| 300 | 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 | 301 | * to change.</b> |
| @@ -305,35 +305,35 @@ | ||
| 305 | 305 | |
| 306 | 306 | private volatile double slowQueryLatencyThresholdPercentile; |
| 307 | 307 | |
| 308 | - private volatile PerHostPercentileTracker perHostPercentileLatencyTracker; | |
| 308 | + private volatile PercentileTracker percentileLatencyTracker; | |
| 309 | 309 | |
| 310 | - private DynamicThresholdQueryLogger(Cluster cluster, int maxQueryStringLength, int maxParameterValueLength, int maxLoggedParameters, double slowQueryLatencyThresholdPercentile, PerHostPercentileTracker perHostPercentileLatencyTracker) { | |
| 310 | + private DynamicThresholdQueryLogger(Cluster cluster, int maxQueryStringLength, int maxParameterValueLength, int maxLoggedParameters, double slowQueryLatencyThresholdPercentile, PercentileTracker percentileLatencyTracker) { | |
| 311 | 311 | super(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters); |
| 312 | 312 | this.setSlowQueryLatencyThresholdPercentile(slowQueryLatencyThresholdPercentile); |
| 313 | - this.setPerHostPercentileLatencyTracker(perHostPercentileLatencyTracker); | |
| 313 | + this.setPercentileLatencyTracker(percentileLatencyTracker); | |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | /** |
| 317 | - * Return the {@link PerHostPercentileTracker} instance to use for recording per-host latency histograms. | |
| 317 | + * Return the percentile tracker to use for recording per-host latency histograms. | |
| 318 | 318 | * Cannot be {@code null}. |
| 319 | 319 | * |
| 320 | - * @return the {@link PerHostPercentileTracker} instance to use. | |
| 320 | + * @return the percentile tracker to use. | |
| 321 | 321 | */ |
| 322 | - public PerHostPercentileTracker getPerHostPercentileLatencyTracker() { | |
| 323 | - return perHostPercentileLatencyTracker; | |
| 322 | + public PercentileTracker getPercentileLatencyTracker() { | |
| 323 | + return percentileLatencyTracker; | |
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | /** |
| 327 | - * Set the {@link PerHostPercentileTracker} instance to use for recording per-host latency histograms. | |
| 327 | + * Set the percentile tracker to use for recording per-host latency histograms. | |
| 328 | 328 | * Cannot be {@code null}. |
| 329 | 329 | * |
| 330 | - * @param perHostPercentileLatencyTracker the {@link PerHostPercentileTracker} instance to use. | |
| 331 | - * @throws IllegalArgumentException if {@code perHostPercentileLatencyTracker == null}. | |
| 330 | + * @param percentileLatencyTracker the percentile tracker instance to use. | |
| 331 | + * @throws IllegalArgumentException if {@code percentileLatencyTracker == null}. | |
| 332 | 332 | */ |
| 333 | - public void setPerHostPercentileLatencyTracker(PerHostPercentileTracker perHostPercentileLatencyTracker) { | |
| 334 | - if (perHostPercentileLatencyTracker == null) | |
| 333 | + public void setPercentileLatencyTracker(PercentileTracker percentileLatencyTracker) { | |
| 334 | + if (percentileLatencyTracker == null) | |
| 335 | 335 | throw new IllegalArgumentException("perHostPercentileLatencyTracker cannot be null"); |
| 336 | - this.perHostPercentileLatencyTracker = perHostPercentileLatencyTracker; | |
| 336 | + this.percentileLatencyTracker = percentileLatencyTracker; | |
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | /** |
| @@ -364,7 +364,7 @@ | ||
| 364 | 364 | |
| 365 | 365 | @Override |
| 366 | 366 | protected void maybeLogNormalOrSlowQuery(Host host, Statement statement, long latencyMs) { |
| 367 | - long threshold = perHostPercentileLatencyTracker.getLatencyAtPercentile(host, slowQueryLatencyThresholdPercentile); | |
| 367 | + long threshold = percentileLatencyTracker.getLatencyAtPercentile(host, statement, null, slowQueryLatencyThresholdPercentile); | |
| 368 | 368 | if (threshold >= 0 && latencyMs > threshold) { |
| 369 | 369 | maybeLogSlowQuery(host, statement, latencyMs, threshold); |
| 370 | 370 | } else { |
| @@ -397,7 +397,7 @@ | ||
| 397 | 397 | |
| 398 | 398 | private double slowQueryLatencyThresholdPercentile = DEFAULT_SLOW_QUERY_THRESHOLD_PERCENTILE; |
| 399 | 399 | |
| 400 | - private PerHostPercentileTracker perHostPercentileLatencyTracker; | |
| 400 | + private PercentileTracker percentileLatencyTracker; | |
| 401 | 401 | |
| 402 | 402 | private boolean constantThreshold = true; |
| 403 | 403 | |
| @@ -409,7 +409,7 @@ | ||
| 409 | 409 | * Enables slow query latency tracking based on constant thresholds. |
| 410 | 410 | * <p/> |
| 411 | 411 | * Note: You should either use {@link #withConstantThreshold(long) constant thresholds} |
| 412 | - * or {@link #withDynamicThreshold(PerHostPercentileTracker, double) dynamic thresholds}, | |
| 412 | + * or {@link #withDynamicThreshold(PercentileTracker, double) dynamic thresholds}, | |
| 413 | 413 | * not both. |
| 414 | 414 | * |
| 415 | 415 | * @param slowQueryLatencyThresholdMillis The threshold in milliseconds beyond which queries are considered 'slow' |
| @@ -427,16 +427,16 @@ | ||
| 427 | 427 | * Enables slow query latency tracking based on dynamic thresholds. |
| 428 | 428 | * <p/> |
| 429 | 429 | * Dynamic thresholds are based on per-host latency percentiles, as computed |
| 430 | - * by {@link PerHostPercentileTracker}. | |
| 430 | + * by {@link PercentileTracker}. | |
| 431 | 431 | * <p/> |
| 432 | 432 | * Note: You should either use {@link #withConstantThreshold(long) constant thresholds} |
| 433 | - * or {@link #withDynamicThreshold(PerHostPercentileTracker, double) dynamic thresholds}, | |
| 433 | + * or {@link #withDynamicThreshold(PercentileTracker, double) dynamic thresholds}, | |
| 434 | 434 | * not both. |
| 435 | 435 | * <p/> |
| 436 | 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 | 437 | * to change.</b> |
| 438 | 438 | * |
| 439 | - * @param perHostPercentileLatencyTracker the {@link PerHostPercentileTracker} instance to use for recording per-host latency histograms. | |
| 439 | + * @param percentileLatencyTracker the {@link PercentileTracker} instance to use for recording per-host latency histograms. | |
| 440 | 440 | * Cannot be {@code null}. |
| 441 | 441 | * @param slowQueryLatencyThresholdPercentile Slow queries threshold percentile. |
| 442 | 442 | * It must be comprised between 0 inclusive and 100 exclusive. |
| @@ -444,8 +444,8 @@ | ||
| 444 | 444 | * @return this {@link Builder} instance (for method chaining). |
| 445 | 445 | */ |
| 446 | 446 | @Beta |
| 447 | - public Builder withDynamicThreshold(PerHostPercentileTracker perHostPercentileLatencyTracker, double slowQueryLatencyThresholdPercentile) { | |
| 448 | - this.perHostPercentileLatencyTracker = perHostPercentileLatencyTracker; | |
| 447 | + public Builder withDynamicThreshold(PercentileTracker percentileLatencyTracker, double slowQueryLatencyThresholdPercentile) { | |
| 448 | + this.percentileLatencyTracker = percentileLatencyTracker; | |
| 449 | 449 | this.slowQueryLatencyThresholdPercentile = slowQueryLatencyThresholdPercentile; |
| 450 | 450 | constantThreshold = false; |
| 451 | 451 | return this; |
| @@ -514,7 +514,7 @@ | ||
| 514 | 514 | if (constantThreshold) { |
| 515 | 515 | return new ConstantThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters, slowQueryLatencyThresholdMillis); |
| 516 | 516 | } else { |
| 517 | - return new DynamicThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters, slowQueryLatencyThresholdPercentile, perHostPercentileLatencyTracker); | |
| 517 | + return new DynamicThresholdQueryLogger(cluster, maxQueryStringLength, maxParameterValueLength, maxLoggedParameters, slowQueryLatencyThresholdPercentile, percentileLatencyTracker); | |
| 518 | 518 | } |
| 519 | 519 | } |
| 520 | 520 |
modified driver-core/src/main/java/com/datastax/driver/core/policies/PercentileSpeculativeExecutionPolicy.java +4 −7
| @@ -15,10 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.Cluster; | |
| 19 | -import com.datastax.driver.core.Host; | |
| 20 | -import com.datastax.driver.core.PerHostPercentileTracker; | |
| 21 | -import com.datastax.driver.core.Statement; | |
| 18 | +import com.datastax.driver.core.*; | |
| 22 | 19 | import com.google.common.annotations.Beta; |
| 23 | 20 | |
| 24 | 21 | import java.util.concurrent.atomic.AtomicInteger; |
| @@ -48,7 +45,7 @@ | ||
| 48 | 45 | */ |
| 49 | 46 | @Beta |
| 50 | 47 | public class PercentileSpeculativeExecutionPolicy implements SpeculativeExecutionPolicy { |
| 51 | - private final PerHostPercentileTracker percentileTracker; | |
| 48 | + private final PercentileTracker percentileTracker; | |
| 52 | 49 | private final double percentile; |
| 53 | 50 | private final int maxSpeculativeExecutions; |
| 54 | 51 | |
| @@ -61,7 +58,7 @@ | ||
| 61 | 58 | * @param maxSpeculativeExecutions the maximum number of speculative executions that will be triggered for a given request (this does not |
| 62 | 59 | * include the initial, normal request). Must be strictly positive. |
| 63 | 60 | */ |
| 64 | - public PercentileSpeculativeExecutionPolicy(PerHostPercentileTracker percentileTracker, double percentile, int maxSpeculativeExecutions) { | |
| 61 | + public PercentileSpeculativeExecutionPolicy(PercentileTracker percentileTracker, double percentile, int maxSpeculativeExecutions) { | |
| 65 | 62 | checkArgument(maxSpeculativeExecutions > 0, |
| 66 | 63 | "number of speculative executions must be strictly positive (was %d)", maxSpeculativeExecutions); |
| 67 | 64 | checkArgument(percentile >= 0.0 && percentile < 100, |
| @@ -80,7 +77,7 @@ | ||
| 80 | 77 | @Override |
| 81 | 78 | public long nextExecution(Host lastQueried) { |
| 82 | 79 | if (remaining.getAndDecrement() > 0) |
| 83 | - return percentileTracker.getLatencyAtPercentile(lastQueried, percentile); | |
| 80 | + return percentileTracker.getLatencyAtPercentile(lastQueried, null, null, percentile); | |
| 84 | 81 | else |
| 85 | 82 | return -1; |
| 86 | 83 | } |
modified pom.xml +1 −1
| @@ -157,7 +157,7 @@ | ||
| 157 | 157 | </execution> |
| 158 | 158 | </executions> |
| 159 | 159 | <configuration> |
| 160 | - <comparisonVersion>2.1.8</comparisonVersion> | |
| 160 | + <comparisonVersion>2.1.9</comparisonVersion> | |
| 161 | 161 | <ignoredDifferencesFile>../clirr-ignores.xml</ignoredDifferencesFile> |
| 162 | 162 | <excludes> |
| 163 | 163 | <!-- Package-private, internal classes --> |