View Javadoc

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   */
18  
19  package org.apache.hadoop.metrics2.lib;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.classification.InterfaceStability;
23  import org.apache.hadoop.metrics2.MetricHistogram;
24  import org.apache.hadoop.metrics2.MetricsExecutor;
25  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
26  import org.apache.hadoop.metrics2.util.MetricQuantile;
27  import org.apache.hadoop.metrics2.util.MetricSampleQuantiles;
28  
29  import java.io.IOException;
30  import java.util.Map;
31  import java.util.concurrent.TimeUnit;
32  
33  /**
34   * Watches a stream of long values, maintaining online estimates of specific quantiles with provably
35   * low error bounds. This is particularly useful for accurate high-percentile (e.g. 95th, 99th)
36   * latency metrics.
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Evolving
40  public class MetricMutableQuantiles extends MetricMutable implements MetricHistogram {
41  
42    static final MetricQuantile[] quantiles = {new MetricQuantile(0.50, 0.050),
43        new MetricQuantile(0.75, 0.025), new MetricQuantile(0.90, 0.010),
44        new MetricQuantile(0.95, 0.005), new MetricQuantile(0.99, 0.001)};
45  
46    static final String[] quantilesSuffix = {"_Median",
47        "_75th_percentile", "_90th_percentile",
48        "_95th_percentile", "_99th_percentile"};
49  
50    private final int interval;
51  
52    private MetricSampleQuantiles estimator;
53    private long previousCount = 0;
54    private MetricsExecutor executor;
55  
56    protected Map<MetricQuantile, Long> previousSnapshot = null;
57  
58    /**
59     * Instantiates a new {@link MetricMutableQuantiles} for a metric that rolls itself over on the
60     * specified time interval.
61     *
62     * @param name        of the metric
63     * @param description long-form textual description of the metric
64     * @param sampleName  type of items in the stream (e.g., "Ops")
65     * @param valueName   type of the values
66     * @param interval    rollover interval (in seconds) of the estimator
67     */
68    public MetricMutableQuantiles(String name, String description, String sampleName,
69                                  String valueName, int interval) {
70      super(name, description);
71  
72      estimator = new MetricSampleQuantiles(quantiles);
73  
74      executor = new MetricsExecutorImpl();
75  
76      this.interval = interval;
77      executor.getExecutor().scheduleAtFixedRate(new RolloverSample(this),
78          interval,
79          interval,
80          TimeUnit.SECONDS);
81    }
82  
83    public MetricMutableQuantiles(String name, String description) {
84      this(name, description, "Ops", "", 60);
85    }
86  
87    @Override
88    public synchronized void snapshot(MetricsRecordBuilder builder, boolean all) {
89      if (all || changed()) {
90        builder.addCounter(name + "NumOps", description, previousCount);
91        for (int i = 0; i < quantiles.length; i++) {
92          long newValue = 0;
93          // If snapshot is null, we failed to update since the window was empty
94          if (previousSnapshot != null) {
95            newValue = previousSnapshot.get(quantiles[i]);
96          }
97          builder.addGauge(name + quantilesSuffix[i], description, newValue);
98        }
99        if (changed()) {
100         clearChanged();
101       }
102     }
103   }
104 
105   public synchronized void add(long value) {
106     estimator.insert(value);
107   }
108 
109   public int getInterval() {
110     return interval;
111   }
112 
113   /** Runnable used to periodically roll over the internal {@link org.apache.hadoop.metrics2.util.MetricSampleQuantiles} every interval. */
114   private static class RolloverSample implements Runnable {
115 
116     MetricMutableQuantiles parent;
117 
118     public RolloverSample(MetricMutableQuantiles parent) {
119       this.parent = parent;
120     }
121 
122     @Override
123     public void run() {
124       synchronized (parent) {
125         try {
126           parent.previousCount = parent.estimator.getCount();
127           parent.previousSnapshot = parent.estimator.snapshot();
128         } catch (IOException e) {
129           // Couldn't get a new snapshot because the window was empty
130           parent.previousCount = 0;
131           parent.previousSnapshot = null;
132         }
133         parent.estimator.clear();
134       }
135       parent.setChanged();
136     }
137 
138   }
139 }