[120] | 1 | package org.apache.hadoop.metrics.util; |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 5 | * or more contributor license agreements. See the NOTICE file |
---|
| 6 | * distributed with this work for additional information |
---|
| 7 | * regarding copyright ownership. The ASF licenses this file |
---|
| 8 | * to you under the Apache License, Version 2.0 (the |
---|
| 9 | * "License"); you may not use this file except in compliance |
---|
| 10 | * with the License. You may obtain a copy of the License at |
---|
| 11 | * |
---|
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 13 | * |
---|
| 14 | * Unless required by applicable law or agreed to in writing, software |
---|
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 17 | * See the License for the specific language governing permissions and |
---|
| 18 | * limitations under the License. |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | import org.apache.hadoop.metrics.MetricsRecord; |
---|
| 22 | import org.apache.hadoop.util.StringUtils; |
---|
| 23 | |
---|
| 24 | import org.apache.commons.logging.Log; |
---|
| 25 | import org.apache.commons.logging.LogFactory; |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * The MetricsTimeVaryingLong class is for a metric that naturally |
---|
| 29 | * varies over time (e.g. number of files created). The metrics is accumulated |
---|
| 30 | * over an interval (set in the metrics config file); the metrics is |
---|
| 31 | * published at the end of each interval and then |
---|
| 32 | * reset to zero. Hence the counter has the value in the current interval. |
---|
| 33 | * |
---|
| 34 | * Note if one wants a time associated with the metric then use |
---|
| 35 | * @see org.apache.hadoop.metrics.util.MetricsTimeVaryingRate |
---|
| 36 | * |
---|
| 37 | */ |
---|
| 38 | public class MetricsTimeVaryingLong extends MetricsBase{ |
---|
| 39 | |
---|
| 40 | private static final Log LOG = |
---|
| 41 | LogFactory.getLog("org.apache.hadoop.metrics.util"); |
---|
| 42 | |
---|
| 43 | private long currentValue; |
---|
| 44 | private long previousIntervalValue; |
---|
| 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 | */ |
---|
| 51 | public MetricsTimeVaryingLong(final String nam, MetricsRegistry registry, final String description) { |
---|
| 52 | super(nam, description); |
---|
| 53 | currentValue = 0; |
---|
| 54 | previousIntervalValue = 0; |
---|
| 55 | registry.add(nam, this); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Constructor - create a new metric |
---|
| 61 | * @param nam the name of the metrics to be used to publish the metric |
---|
| 62 | * @param registry - where the metrics object will be registered |
---|
| 63 | * A description of {@link #NO_DESCRIPTION} is used |
---|
| 64 | */ |
---|
| 65 | public MetricsTimeVaryingLong(final String nam, MetricsRegistry registry) { |
---|
| 66 | this(nam, registry, NO_DESCRIPTION); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * Inc metrics for incr vlaue |
---|
| 71 | * @param incr - number of operations |
---|
| 72 | */ |
---|
| 73 | public synchronized void inc(final long incr) { |
---|
| 74 | currentValue += incr; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Inc metrics by one |
---|
| 79 | */ |
---|
| 80 | public synchronized void inc() { |
---|
| 81 | currentValue++; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | private synchronized void intervalHeartBeat() { |
---|
| 85 | previousIntervalValue = currentValue; |
---|
| 86 | currentValue = 0; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Push the delta metrics to the mr. |
---|
| 91 | * The delta is since the last push/interval. |
---|
| 92 | * |
---|
| 93 | * Note this does NOT push to JMX |
---|
| 94 | * (JMX gets the info via {@link #previousIntervalValue} |
---|
| 95 | * |
---|
| 96 | * @param mr |
---|
| 97 | */ |
---|
| 98 | public synchronized void pushMetric(final MetricsRecord mr) { |
---|
| 99 | intervalHeartBeat(); |
---|
| 100 | try { |
---|
| 101 | mr.incrMetric(getName(), getPreviousIntervalValue()); |
---|
| 102 | } catch (Exception e) { |
---|
| 103 | LOG.info("pushMetric failed for " + getName() + "\n" + |
---|
| 104 | StringUtils.stringifyException(e)); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * The Value at the Previous interval |
---|
| 111 | * @return prev interval value |
---|
| 112 | */ |
---|
| 113 | public synchronized long getPreviousIntervalValue() { |
---|
| 114 | return previousIntervalValue; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * The Value at the current interval |
---|
| 119 | * @return prev interval value |
---|
| 120 | */ |
---|
| 121 | public synchronized long getCurrentIntervalValue() { |
---|
| 122 | return currentValue; |
---|
| 123 | } |
---|
| 124 | } |
---|