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  package org.apache.hadoop.hbase.metrics;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.metrics.MetricsRecord;
24  import org.apache.hadoop.metrics.util.MetricsBase;
25  import org.apache.hadoop.metrics.util.MetricsRegistry;
26  import org.apache.hadoop.util.StringUtils;
27  
28  /**
29   * Publishes a rate based on a counter - you increment the counter each
30   * time an event occurs (eg: an RPC call) and this publishes a rate.
31   */
32  @Deprecated
33  @InterfaceAudience.Private
34  public class MetricsRate extends MetricsBase {
35    private static final Log LOG = LogFactory.getLog("org.apache.hadoop.hbase.metrics");
36  
37    private int value;
38    private float prevRate;
39    private long ts;
40  
41    public MetricsRate(final String name, final MetricsRegistry registry,
42        final String description) {
43      super(name, description);
44      this.value = 0;
45      this.prevRate = 0;
46      this.ts = System.currentTimeMillis();
47      registry.add(name, this);
48    }
49  
50    public MetricsRate(final String name, final MetricsRegistry registry) {
51      this(name, registry, NO_DESCRIPTION);
52    }
53  
54    public synchronized void inc(final int incr) {
55      value += incr;
56    }
57  
58    public synchronized void inc() {
59      value++;
60    }
61  
62    public synchronized void intervalHeartBeat() {
63      long now = System.currentTimeMillis();
64      long diff = (now-ts) / 1000;
65      if (diff < 1){
66          // To make sure our averages aren't skewed by fast repeated calls,
67          // we simply ignore fast repeated calls.
68      	return;
69      }
70      this.prevRate = (float)value / diff;
71      this.value = 0;
72      this.ts = now;
73    }
74  
75    @Override
76    public synchronized void pushMetric(final MetricsRecord mr) {
77      intervalHeartBeat();
78      try {
79        mr.setMetric(getName(), getPreviousIntervalValue());
80      } catch (Exception e) {
81        LOG.info("pushMetric failed for " + getName() + "\n" +
82            StringUtils.stringifyException(e));
83      }
84    }
85  
86    public synchronized float getPreviousIntervalValue() {
87      return this.prevRate;
88    }
89  }