Skip to content

Commit 55c76d8

Browse files
author
Ravindra Dingankar
committed
review comments and fix spaces
1 parent a66f26a commit 55c76d8

File tree

5 files changed

+92
-65
lines changed

5 files changed

+92
-65
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ public synchronized MutableQuantiles newQuantiles(String name, String desc,
226226
* @param sampleName of the metric (e.g., "Ops")
227227
* @param valueName of the metric (e.g., "Time" or "Latency")
228228
* @param interval rollover interval of estimator in seconds
229-
* @param inverseQuantiles inverse the quantiles ( e.g. P99 will give the 1st quantile )
229+
* @param inverseQuantiles inverse the quantiles ( e.g. P99 will give the 1st quantile )
230230
* @return a new quantile estimator object
231231
* @throws MetricsException if interval is not a positive integer
232232
*/
233-
public synchronized MutableQuantiles newQuantiles(String name, String desc, String sampleName, String valueName,
233+
public synchronized MutableQuantiles newQuantiles(String name, String desc, String sampleName, String valueName,
234234
int interval, boolean inverseQuantiles) {
235235
checkMetricName(name);
236236
if (interval <= 0) {

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableQuantiles.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public class MutableQuantiles extends MutableMetric {
8282
* type of the values
8383
* @param interval
8484
* rollover interval (in seconds) of the estimator
85+
* @param inverseQuantiles
86+
* flag to denote if inverse quantiles are requested
8587
*/
8688
public MutableQuantiles(String name, String description, String sampleName,
8789
String valueName, int interval, boolean inverseQuantiles) {
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
package org.apache.hadoop.metrics2.util;
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
218

3-
import org.apache.hadoop.util.Preconditions;
4-
import java.util.ListIterator;
19+
package org.apache.hadoop.metrics2.util;
520

621
public class InverseQuantiles extends SampleQuantiles{
722

823
public InverseQuantiles(Quantile[] quantiles) {
924
super(quantiles);
1025
}
11-
1226

1327
/**
14-
* Get the estimated value at the inverse of the specified quantile.
15-
* Eg: return the value at (1 - 0.99)*count position for quantile 0.99.
16-
* When count is 100, quantile 0.99 is desired to return the value at the 1st position
17-
*
18-
* @param quantile Queried quantile, e.g. 0.50 or 0.99.
19-
* @return Estimated value at the inverse position of that quantile.
28+
* Get the desired location from the sample for inverse of the specified quantile.
29+
* Eg: return (1 - 0.99)*count position for quantile 0.99.
30+
* When count is 100, the desired location for quantile 0.99 is the 1st position
31+
* @param quantile queried quantile, e.g. 0.50 or 0.99.
32+
* @param count sample size count
33+
* @return Desired location inverse position of that quantile.
2034
*/
21-
long query(double quantile) {
22-
Preconditions.checkState(!samples.isEmpty(), "no data in estimator");
23-
24-
int rankMin = 0;
25-
int desired = (int) ((1 - quantile) * count);
26-
27-
ListIterator<SampleItem> it = samples.listIterator();
28-
SampleItem prev;
29-
SampleItem cur = it.next();
30-
for (int i = 1; i < samples.size(); i++) {
31-
prev = cur;
32-
cur = it.next();
33-
34-
rankMin += prev.g;
35-
36-
if (rankMin + cur.g + cur.delta > desired + (allowableError(i) / 2)) {
37-
return prev.value;
38-
}
39-
}
35+
int getDesiredLocation(final double quantile, final long count) {
36+
return (int) ((1 - quantile) * count);
37+
}
4038

41-
// edge case of wanting max value
39+
/**
40+
* Return the best (minimum) value from given sample
41+
* @return minimum value from given sample
42+
*/
43+
long getMaxValue() {
4244
return samples.get(0).value;
4345
}
4446
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/SampleQuantiles.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class SampleQuantiles implements QuantileEstimator {
5252
/**
5353
* Total number of items in stream
5454
*/
55-
long count = 0;
55+
private long count = 0;
5656

5757
/**
5858
* Current list of sampled items, maintained in sorted order with error bounds
@@ -87,7 +87,7 @@ public SampleQuantiles(Quantile[] quantiles) {
8787
* @param rank
8888
* the index in the list of samples
8989
*/
90-
double allowableError(int rank) {
90+
private double allowableError(int rank) {
9191
int size = samples.size();
9292
double minError = size + 1;
9393
for (Quantile q : quantiles) {
@@ -203,11 +203,11 @@ private void compress() {
203203
* @param quantile Queried quantile, e.g. 0.50 or 0.99.
204204
* @return Estimated value at that quantile.
205205
*/
206-
long query(double quantile) {
206+
private long query(double quantile) {
207207
Preconditions.checkState(!samples.isEmpty(), "no data in estimator");
208208

209209
int rankMin = 0;
210-
int desired = (int) (quantile * count);
210+
int desired = getDesiredLocation(quantile, count);
211211

212212
ListIterator<SampleItem> it = samples.listIterator();
213213
SampleItem prev = null;
@@ -223,10 +223,30 @@ long query(double quantile) {
223223
}
224224
}
225225

226-
// edge case of wanting max value
226+
// edge case of wanting the best value
227227
return samples.get(samples.size() - 1).value;
228228
}
229229

230+
/**
231+
* Get the desired location from the sample for inverse of the specified quantile.
232+
* Eg: return (1 - 0.99)*count position for quantile 0.99.
233+
* When count is 100, the desired location for quantile 0.99 is the 1st position
234+
* @param quantile queried quantile, e.g. 0.50 or 0.99.
235+
* @param count sample size count
236+
* @return Desired location inverse position of that quantile.
237+
*/
238+
int getDesiredLocation(final double quantile, final long count) {
239+
return (int) (quantile * count);
240+
}
241+
242+
/**
243+
* Return the best (maximum) value from given sample
244+
* @return maximum value from given sample
245+
*/
246+
long getMaxValue() {
247+
return samples.get(samples.size() - 1).value;
248+
}
249+
230250
/**
231251
* Get a snapshot of the current values of all the tracked quantiles.
232252
*
@@ -291,7 +311,7 @@ synchronized public String toString() {
291311
* Describes a measured value passed to the estimator, tracking additional
292312
* metadata required by the CKMS algorithm.
293313
*/
294-
static class SampleItem {
314+
static class SampleItem {
295315

296316
/**
297317
* Value of the sampled item (e.g. a measured latency value)

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/util/TestSampleQuantiles.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,36 +118,39 @@ public void testQuantileError() throws IOException {
118118
}
119119
}
120120
}
121-
121+
122+
/**
123+
* Correctness test that checks that absolute error of the estimate for inverse quantiles is within
124+
* specified error bounds for some randomly permuted streams of items.
125+
*/
122126
@Test
123127
public void testInverseQuantiles() {
124-
SampleQuantiles estimatorWithInverseQuantiles = new InverseQuantiles(quantiles);
125-
final int count = 100000;
126-
Random r = new Random(0xDEADDEAD);
127-
Long[] values = new Long[count];
128-
for (int i = 0; i < count; i++) {
129-
values[i] = (long) (i + 1);
128+
SampleQuantiles estimatorWithInverseQuantiles = new InverseQuantiles(quantiles);
129+
final int count = 100000;
130+
Random r = new Random(0xDEADDEAD);
131+
Long[] values = new Long[count];
132+
for (int i = 0; i < count; i++) {
133+
values[i] = (long) (i + 1);
134+
}
135+
// Do 10 shuffle/insert/check cycles
136+
for (int i = 0; i < 10; i++) {
137+
System.out.println("Starting run " + i);
138+
Collections.shuffle(Arrays.asList(values), r);
139+
estimatorWithInverseQuantiles.clear();
140+
for (int j = 0; j < count; j++) {
141+
estimatorWithInverseQuantiles.insert(values[j]);
130142
}
131-
// Do 10 shuffle/insert/check cycles
132-
for (int i = 0; i < 10; i++) {
133-
System.out.println("Starting run " + i);
134-
Collections.shuffle(Arrays.asList(values), r);
135-
estimatorWithInverseQuantiles.clear();
136-
for (int j = 0; j < count; j++) {
137-
estimatorWithInverseQuantiles.insert(values[j]);
138-
}
139-
Map<Quantile, Long> snapshot;
140-
snapshot = estimatorWithInverseQuantiles.snapshot();
141-
for (Quantile q : quantiles) {
142-
long actual = (long) ((1 - q.quantile) * count);
143-
long error = (long) ((0.1 - q.error) * count);
144-
long estimate = snapshot.get(q);
145-
System.out
146-
.println(String.format("For quantile %f Expected %d with error %d, estimated %d",
147-
q.quantile, actual, error, estimate));
148-
assertThat(estimate <= actual + error).isTrue();
149-
assertThat(estimate >= actual - error).isTrue();
150-
}
143+
Map<Quantile, Long> snapshot;
144+
snapshot = estimatorWithInverseQuantiles.snapshot();
145+
for (Quantile q : quantiles) {
146+
long actual = (long) ((1 - q.quantile) * count);
147+
long error = (long) ((0.1 - q.error) * count);
148+
long estimate = snapshot.get(q);
149+
System.out.println(String.format("For quantile %f Expected %d with error %d, estimated %d",
150+
q.quantile, actual, error, estimate));
151+
assertThat(estimate <= actual + error).isTrue();
152+
assertThat(estimate >= actual - error).isTrue();
151153
}
154+
}
152155
}
153156
}

0 commit comments

Comments
 (0)