source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/metrics/util/MetricsTimeVaryingInt.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.8 KB
Line 
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 */
18package org.apache.hadoop.metrics.util;
19
20import org.apache.hadoop.metrics.MetricsRecord;
21import org.apache.hadoop.util.StringUtils;
22
23import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25
26/**
27 * The MetricsTimeVaryingInt class is for a metric that naturally
28 * varies over time (e.g. number of files created). The metrics is accumulated
29 * over an interval (set in the metrics config file); the metrics is
30 *  published at the end of each interval and then
31 * reset to zero. Hence the counter has the value in the current interval.
32 *
33 * Note if one wants a time associated with the metric then use
34 * @see org.apache.hadoop.metrics.util.MetricsTimeVaryingRate
35 *
36 */
37public class MetricsTimeVaryingInt extends MetricsBase {
38
39  private static final Log LOG =
40    LogFactory.getLog("org.apache.hadoop.metrics.util");
41 
42  private int currentValue;
43  private int previousIntervalValue;
44 
45 
46  /**
47   * Constructor - create a new metric
48   * @param nam the name of the metrics to be used to publish the metric
49   * @param registry - where the metrics object will be registered
50   * @param description - the description
51   */
52  public MetricsTimeVaryingInt(final String nam,
53                               final MetricsRegistry registry,
54                               final String description) {
55    super(nam, description);
56    currentValue = 0;
57    previousIntervalValue = 0;
58    registry.add(nam, this);
59  }
60 
61  /**
62   * Constructor - create a new metric
63   * @param nam the name of the metrics to be used to publish the metric
64   * @param registry - where the metrics object will be registered
65   * A description of {@link #NO_DESCRIPTION} is used
66   */
67  public MetricsTimeVaryingInt(final String nam, final MetricsRegistry registry) {
68    this(nam, registry, NO_DESCRIPTION);
69  }
70 
71
72 
73  /**
74   * Inc metrics for incr vlaue
75   * @param incr - number of operations
76   */
77  public synchronized void inc(final int incr) {
78    currentValue += incr;
79  }
80 
81  /**
82   * Inc metrics by one
83   */
84  public synchronized void inc() {
85    currentValue++;
86  }
87
88  private synchronized void intervalHeartBeat() {
89     previousIntervalValue = currentValue;
90     currentValue = 0;
91  }
92 
93  /**
94   * Push the delta  metrics to the mr.
95   * The delta is since the last push/interval.
96   *
97   * Note this does NOT push to JMX
98   * (JMX gets the info via {@link #previousIntervalValue}
99   *
100   * @param mr
101   */
102  public synchronized void pushMetric(final MetricsRecord mr) {
103    intervalHeartBeat();
104    try {
105      mr.incrMetric(getName(), getPreviousIntervalValue());
106    } catch (Exception e) {
107      LOG.info("pushMetric failed for " + getName() + "\n" +
108          StringUtils.stringifyException(e));
109    }
110  }
111 
112 
113  /**
114   * The Value at the Previous interval
115   * @return prev interval value
116   */
117  public synchronized int getPreviousIntervalValue() { 
118    return previousIntervalValue;
119  }
120 
121  /**
122   * The Value at the current interval
123   * @return prev interval value
124   */
125  public synchronized int getCurrentIntervalValue() { 
126    return currentValue;
127  } 
128}
Note: See TracBrowser for help on using the repository browser.