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.metrics.util; |
---|
19 | |
---|
20 | import org.apache.hadoop.metrics.MetricsRecord; |
---|
21 | import org.apache.hadoop.util.StringUtils; |
---|
22 | |
---|
23 | import org.apache.commons.logging.Log; |
---|
24 | import org.apache.commons.logging.LogFactory; |
---|
25 | |
---|
26 | /** |
---|
27 | * The MetricsTimeVaryingRate class is for a rate based metric that |
---|
28 | * naturally varies over time (e.g. time taken to create a file). |
---|
29 | * The rate is averaged at each interval heart beat (the interval |
---|
30 | * is set in the metrics config file). |
---|
31 | * This class also keeps track of the min and max rates along with |
---|
32 | * a method to reset the min-max. |
---|
33 | * |
---|
34 | */ |
---|
35 | public class MetricsTimeVaryingRate extends MetricsBase { |
---|
36 | |
---|
37 | private static final Log LOG = |
---|
38 | LogFactory.getLog("org.apache.hadoop.metrics.util"); |
---|
39 | |
---|
40 | static class Metrics { |
---|
41 | int numOperations = 0; |
---|
42 | long time = 0; // total time or average time |
---|
43 | |
---|
44 | void set(final Metrics resetTo) { |
---|
45 | numOperations = resetTo.numOperations; |
---|
46 | time = resetTo.time; |
---|
47 | } |
---|
48 | |
---|
49 | void reset() { |
---|
50 | numOperations = 0; |
---|
51 | time = 0; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | static class MinMax { |
---|
56 | long minTime = -1; |
---|
57 | long maxTime = 0; |
---|
58 | |
---|
59 | void set(final MinMax newVal) { |
---|
60 | minTime = newVal.minTime; |
---|
61 | maxTime = newVal.maxTime; |
---|
62 | } |
---|
63 | |
---|
64 | void reset() { |
---|
65 | minTime = -1; |
---|
66 | maxTime = 0; |
---|
67 | } |
---|
68 | void update(final long time) { // update min max |
---|
69 | minTime = (minTime == -1) ? time : Math.min(minTime, time); |
---|
70 | minTime = Math.min(minTime, time); |
---|
71 | maxTime = Math.max(maxTime, time); |
---|
72 | } |
---|
73 | } |
---|
74 | private Metrics currentData; |
---|
75 | private Metrics previousIntervalData; |
---|
76 | private MinMax minMax; |
---|
77 | |
---|
78 | |
---|
79 | /** |
---|
80 | * Constructor - create a new metric |
---|
81 | * @param nam the name of the metrics to be used to publish the metric |
---|
82 | * @param registry - where the metrics object will be registered |
---|
83 | */ |
---|
84 | public MetricsTimeVaryingRate(final String nam, final MetricsRegistry registry, final String description) { |
---|
85 | super(nam, description); |
---|
86 | currentData = new Metrics(); |
---|
87 | previousIntervalData = new Metrics(); |
---|
88 | minMax = new MinMax(); |
---|
89 | registry.add(nam, this); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Constructor - create a new metric |
---|
94 | * @param nam the name of the metrics to be used to publish the metric |
---|
95 | * @param registry - where the metrics object will be registered |
---|
96 | * A description of {@link #NO_DESCRIPTION} is used |
---|
97 | */ |
---|
98 | public MetricsTimeVaryingRate(final String nam, MetricsRegistry registry) { |
---|
99 | this(nam, registry, NO_DESCRIPTION); |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /** |
---|
105 | * Increment the metrics for numOps operations |
---|
106 | * @param numOps - number of operations |
---|
107 | * @param time - time for numOps operations |
---|
108 | */ |
---|
109 | public synchronized void inc(final int numOps, final long time) { |
---|
110 | currentData.numOperations += numOps; |
---|
111 | currentData.time += time; |
---|
112 | long timePerOps = time/numOps; |
---|
113 | minMax.update(timePerOps); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Increment the metrics for one operation |
---|
118 | * @param time for one operation |
---|
119 | */ |
---|
120 | public synchronized void inc(final long time) { |
---|
121 | currentData.numOperations++; |
---|
122 | currentData.time += time; |
---|
123 | minMax.update(time); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | private synchronized void intervalHeartBeat() { |
---|
129 | previousIntervalData.numOperations = currentData.numOperations; |
---|
130 | previousIntervalData.time = (currentData.numOperations == 0) ? |
---|
131 | 0 : currentData.time / currentData.numOperations; |
---|
132 | currentData.reset(); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Push the delta metrics to the mr. |
---|
137 | * The delta is since the last push/interval. |
---|
138 | * |
---|
139 | * Note this does NOT push to JMX |
---|
140 | * (JMX gets the info via {@link #getPreviousIntervalAverageTime()} and |
---|
141 | * {@link #getPreviousIntervalNumOps()} |
---|
142 | * |
---|
143 | * @param mr |
---|
144 | */ |
---|
145 | public synchronized void pushMetric(final MetricsRecord mr) { |
---|
146 | intervalHeartBeat(); |
---|
147 | try { |
---|
148 | mr.incrMetric(getName() + "_num_ops", getPreviousIntervalNumOps()); |
---|
149 | mr.setMetric(getName() + "_avg_time", getPreviousIntervalAverageTime()); |
---|
150 | } catch (Exception e) { |
---|
151 | LOG.info("pushMetric failed for " + getName() + "\n" + |
---|
152 | StringUtils.stringifyException(e)); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * The number of operations in the previous interval |
---|
158 | * @return - ops in prev interval |
---|
159 | */ |
---|
160 | public synchronized int getPreviousIntervalNumOps() { |
---|
161 | return previousIntervalData.numOperations; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * The average rate of an operation in the previous interval |
---|
166 | * @return - the average rate. |
---|
167 | */ |
---|
168 | public synchronized long getPreviousIntervalAverageTime() { |
---|
169 | return previousIntervalData.time; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * The min time for a single operation since the last reset |
---|
174 | * {@link #resetMinMax()} |
---|
175 | * @return min time for an operation |
---|
176 | */ |
---|
177 | public synchronized long getMinTime() { |
---|
178 | return minMax.minTime; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * The max time for a single operation since the last reset |
---|
183 | * {@link #resetMinMax()} |
---|
184 | * @return max time for an operation |
---|
185 | */ |
---|
186 | public synchronized long getMaxTime() { |
---|
187 | return minMax.maxTime; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * Reset the min max values |
---|
192 | */ |
---|
193 | public synchronized void resetMinMax() { |
---|
194 | minMax.reset(); |
---|
195 | } |
---|
196 | } |
---|