Skip to content

Commit a30980e

Browse files
author
Ravindra Dingankar
committed
inverse for loop review comment
1 parent 6bca0d1 commit a30980e

File tree

5 files changed

+49
-52
lines changed

5 files changed

+49
-52
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ public synchronized MutableGaugeFloat newGauge(MetricsInfo info, float iVal) {
215215
* @throws MetricsException if interval is not a positive integer
216216
*/
217217
public synchronized MutableQuantiles newQuantiles(String name, String desc,
218-
String sampleName, String valueName, int interval) {
219-
return newQuantiles(name, desc, sampleName, valueName, interval, false);
218+
String sampleName, String valueName, int interval) {
219+
return newQuantiles(name, desc, sampleName, valueName, interval, false);
220220
}
221221

222222
/**
223-
* Create a mutable metric that estimates quantiles of a stream of values
223+
* Create a mutable metric that estimates quantiles of a stream of values.
224224
* @param name of the metric
225225
* @param desc metric description
226226
* @param sampleName of the metric (e.g., "Ops")
@@ -230,7 +230,8 @@ public synchronized MutableQuantiles newQuantiles(String name, String desc,
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,
234+
String sampleName, String valueName,
234235
int interval, boolean inverseQuantiles) {
235236
checkMetricName(name);
236237
if (interval <= 0) {

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,38 @@
1818

1919
package org.apache.hadoop.metrics2.util;
2020

21+
import java.util.ListIterator;
22+
2123
public class InverseQuantiles extends SampleQuantiles{
2224

2325
public InverseQuantiles(Quantile[] quantiles) {
2426
super(quantiles);
2527
}
2628

2729
/**
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.
30+
* Get the estimated value at the inverse of the specified quantile.
31+
* Eg: return the value at (1 - 0.99)*count position for quantile 0.99.
32+
* When count is 100, quantile 0.99 is desired to return the value at the 1st position
33+
* @param quantile Queried quantile, e.g. 0.50 or 0.99.
34+
* @return Estimated value at that quantile.
3435
*/
35-
int getDesiredLocation(final double quantile, final long count) {
36-
return (int) ((1 - quantile) * count);
37-
}
36+
long query(double quantile) {
37+
int rankMin = 0;
38+
int desired = (int) (quantile * getCount());
3839

39-
/**
40-
* Return the best (minimum) value from given sample
41-
* @return minimum value from given sample
42-
*/
43-
long getMaxValue() {
44-
return samples.get(0).value;
40+
ListIterator<SampleItem> it = getSamples().listIterator(getSampleCount());
41+
SampleItem prev;
42+
SampleItem cur = it.previous();
43+
for (int i = 1; i < getSamples().size(); i++) {
44+
prev = cur;
45+
cur = it.previous();
46+
rankMin += prev.g;
47+
if (rankMin + cur.g + cur.delta > desired + (allowableError(i) / 2)) {
48+
return prev.value;
49+
}
50+
}
51+
52+
// edge case of wanting min value
53+
return getSamples().get(0).value;
4554
}
4655
}

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class SampleQuantiles implements QuantileEstimator {
5757
/**
5858
* Current list of sampled items, maintained in sorted order with error bounds
5959
*/
60-
LinkedList<SampleItem> samples;
60+
private LinkedList<SampleItem> samples;
6161

6262
/**
6363
* Buffers incoming items to be inserted in batch. Items are inserted into
@@ -87,7 +87,7 @@ public SampleQuantiles(Quantile[] quantiles) {
8787
* @param rank
8888
* the index in the list of samples
8989
*/
90-
private double allowableError(int rank) {
90+
double allowableError(int rank) {
9191
int size = samples.size();
9292
double minError = size + 1;
9393
for (Quantile q : quantiles) {
@@ -203,11 +203,9 @@ private void compress() {
203203
* @param quantile Queried quantile, e.g. 0.50 or 0.99.
204204
* @return Estimated value at that quantile.
205205
*/
206-
private long query(double quantile) {
207-
Preconditions.checkState(!samples.isEmpty(), "no data in estimator");
208-
206+
long query(double quantile) {
209207
int rankMin = 0;
210-
int desired = getDesiredLocation(quantile, count);
208+
int desired = (int) (quantile * count);
211209

212210
ListIterator<SampleItem> it = samples.listIterator();
213211
SampleItem prev = null;
@@ -223,28 +221,8 @@ private long query(double quantile) {
223221
}
224222
}
225223

226-
// edge case of wanting the best value
227-
return getMaxValue();
228-
}
229-
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;
224+
// edge case of wanting max value
225+
return samples.get(samples.size() - 1).value;
248226
}
249227

250228
/**
@@ -263,6 +241,7 @@ synchronized public Map<Quantile, Long> snapshot() {
263241

264242
Map<Quantile, Long> values = new TreeMap<Quantile, Long>();
265243
for (int i = 0; i < quantiles.length; i++) {
244+
Preconditions.checkState(!samples.isEmpty(), "no data in estimator");
266245
values.put(quantiles[i], query(quantiles[i].quantile));
267246
}
268247

@@ -278,6 +257,14 @@ synchronized public long getCount() {
278257
return count;
279258
}
280259

260+
/**
261+
* Returns the list of samples
262+
*
263+
* @return samples list of samples
264+
*/
265+
synchronized public LinkedList<SampleItem> getSamples() {
266+
return samples;
267+
}
281268
/**
282269
* Returns the number of samples kept by the estimator
283270
*
@@ -311,7 +298,7 @@ synchronized public String toString() {
311298
* Describes a measured value passed to the estimator, tracking additional
312299
* metadata required by the CKMS algorithm.
313300
*/
314-
static class SampleItem {
301+
static class SampleItem {
315302

316303
/**
317304
* 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ public void testQuantileError() throws IOException {
120120
}
121121

122122
/**
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.
123+
* Correctness test that checks that absolute error of the estimate for inverse quantiles
124+
* is within specified error bounds for some randomly permuted streams of items.
125125
*/
126126
@Test
127127
public void testInverseQuantiles() {

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/metrics/DataNodeMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ public DataNodeMetrics(String name, String sessionId, int[] intervals,
260260
"ops", "latency", interval);
261261
readTransferRateQuantiles[i] = registry.newQuantiles(
262262
"readTransferRate" + interval + "s",
263-
"Rate at which bytes are read from datanode calculated in bytes per second with inverse quantiles",
264-
"ops", "rate", interval, true);
263+
"Rate at which bytes are read from datanode calculated in bytes per second" +
264+
" with inverse quantiles", "ops", "rate", interval, true);
265265
}
266266
}
267267

0 commit comments

Comments
 (0)