001/* 002 * MetricsRecordImpl.java 003 * 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021package org.apache.hadoop.metrics.spi; 022 023import java.util.LinkedHashMap; 024import java.util.Map; 025 026import org.apache.hadoop.classification.InterfaceAudience; 027import org.apache.hadoop.classification.InterfaceStability; 028import org.apache.hadoop.metrics.MetricsException; 029import org.apache.hadoop.metrics.MetricsRecord; 030import org.apache.hadoop.metrics.spi.AbstractMetricsContext.TagMap; 031 032/** 033 * An implementation of MetricsRecord. Keeps a back-pointer to the context 034 * from which it was created, and delegates back to it on <code>update</code> 035 * and <code>remove()</code>. 036 */ 037@InterfaceAudience.Public 038@InterfaceStability.Evolving 039public class MetricsRecordImpl implements MetricsRecord { 040 041 private TagMap tagTable = new TagMap(); 042 private Map<String,MetricValue> metricTable = new LinkedHashMap<String,MetricValue>(); 043 044 private String recordName; 045 private AbstractMetricsContext context; 046 047 048 /** Creates a new instance of FileRecord */ 049 protected MetricsRecordImpl(String recordName, AbstractMetricsContext context) 050 { 051 this.recordName = recordName; 052 this.context = context; 053 } 054 055 /** 056 * Returns the record name. 057 * 058 * @return the record name 059 */ 060 @Override 061 public String getRecordName() { 062 return recordName; 063 } 064 065 /** 066 * Sets the named tag to the specified value. 067 * 068 * @param tagName name of the tag 069 * @param tagValue new value of the tag 070 * @throws MetricsException if the tagName conflicts with the configuration 071 */ 072 @Override 073 public void setTag(String tagName, String tagValue) { 074 if (tagValue == null) { 075 tagValue = ""; 076 } 077 tagTable.put(tagName, tagValue); 078 } 079 080 /** 081 * Sets the named tag to the specified value. 082 * 083 * @param tagName name of the tag 084 * @param tagValue new value of the tag 085 * @throws MetricsException if the tagName conflicts with the configuration 086 */ 087 @Override 088 public void setTag(String tagName, int tagValue) { 089 tagTable.put(tagName, Integer.valueOf(tagValue)); 090 } 091 092 /** 093 * Sets the named tag to the specified value. 094 * 095 * @param tagName name of the tag 096 * @param tagValue new value of the tag 097 * @throws MetricsException if the tagName conflicts with the configuration 098 */ 099 @Override 100 public void setTag(String tagName, long tagValue) { 101 tagTable.put(tagName, Long.valueOf(tagValue)); 102 } 103 104 /** 105 * Sets the named tag to the specified value. 106 * 107 * @param tagName name of the tag 108 * @param tagValue new value of the tag 109 * @throws MetricsException if the tagName conflicts with the configuration 110 */ 111 @Override 112 public void setTag(String tagName, short tagValue) { 113 tagTable.put(tagName, Short.valueOf(tagValue)); 114 } 115 116 /** 117 * Sets the named tag to the specified value. 118 * 119 * @param tagName name of the tag 120 * @param tagValue new value of the tag 121 * @throws MetricsException if the tagName conflicts with the configuration 122 */ 123 @Override 124 public void setTag(String tagName, byte tagValue) { 125 tagTable.put(tagName, Byte.valueOf(tagValue)); 126 } 127 128 /** 129 * Removes any tag of the specified name. 130 */ 131 @Override 132 public void removeTag(String tagName) { 133 tagTable.remove(tagName); 134 } 135 136 /** 137 * Sets the named metric to the specified value. 138 * 139 * @param metricName name of the metric 140 * @param metricValue new value of the metric 141 * @throws MetricsException if the metricName or the type of the metricValue 142 * conflicts with the configuration 143 */ 144 @Override 145 public void setMetric(String metricName, int metricValue) { 146 setAbsolute(metricName, Integer.valueOf(metricValue)); 147 } 148 149 /** 150 * Sets the named metric to the specified value. 151 * 152 * @param metricName name of the metric 153 * @param metricValue new value of the metric 154 * @throws MetricsException if the metricName or the type of the metricValue 155 * conflicts with the configuration 156 */ 157 @Override 158 public void setMetric(String metricName, long metricValue) { 159 setAbsolute(metricName, Long.valueOf(metricValue)); 160 } 161 162 /** 163 * Sets the named metric to the specified value. 164 * 165 * @param metricName name of the metric 166 * @param metricValue new value of the metric 167 * @throws MetricsException if the metricName or the type of the metricValue 168 * conflicts with the configuration 169 */ 170 @Override 171 public void setMetric(String metricName, short metricValue) { 172 setAbsolute(metricName, Short.valueOf(metricValue)); 173 } 174 175 /** 176 * Sets the named metric to the specified value. 177 * 178 * @param metricName name of the metric 179 * @param metricValue new value of the metric 180 * @throws MetricsException if the metricName or the type of the metricValue 181 * conflicts with the configuration 182 */ 183 @Override 184 public void setMetric(String metricName, byte metricValue) { 185 setAbsolute(metricName, Byte.valueOf(metricValue)); 186 } 187 188 /** 189 * Sets the named metric to the specified value. 190 * 191 * @param metricName name of the metric 192 * @param metricValue new value of the metric 193 * @throws MetricsException if the metricName or the type of the metricValue 194 * conflicts with the configuration 195 */ 196 @Override 197 public void setMetric(String metricName, float metricValue) { 198 setAbsolute(metricName, new Float(metricValue)); 199 } 200 201 /** 202 * Increments the named metric by the specified value. 203 * 204 * @param metricName name of the metric 205 * @param metricValue incremental value 206 * @throws MetricsException if the metricName or the type of the metricValue 207 * conflicts with the configuration 208 */ 209 @Override 210 public void incrMetric(String metricName, int metricValue) { 211 setIncrement(metricName, Integer.valueOf(metricValue)); 212 } 213 214 /** 215 * Increments the named metric by the specified value. 216 * 217 * @param metricName name of the metric 218 * @param metricValue incremental value 219 * @throws MetricsException if the metricName or the type of the metricValue 220 * conflicts with the configuration 221 */ 222 @Override 223 public void incrMetric(String metricName, long metricValue) { 224 setIncrement(metricName, Long.valueOf(metricValue)); 225 } 226 227 /** 228 * Increments the named metric by the specified value. 229 * 230 * @param metricName name of the metric 231 * @param metricValue incremental value 232 * @throws MetricsException if the metricName or the type of the metricValue 233 * conflicts with the configuration 234 */ 235 @Override 236 public void incrMetric(String metricName, short metricValue) { 237 setIncrement(metricName, Short.valueOf(metricValue)); 238 } 239 240 /** 241 * Increments the named metric by the specified value. 242 * 243 * @param metricName name of the metric 244 * @param metricValue incremental value 245 * @throws MetricsException if the metricName or the type of the metricValue 246 * conflicts with the configuration 247 */ 248 @Override 249 public void incrMetric(String metricName, byte metricValue) { 250 setIncrement(metricName, Byte.valueOf(metricValue)); 251 } 252 253 /** 254 * Increments the named metric by the specified value. 255 * 256 * @param metricName name of the metric 257 * @param metricValue incremental value 258 * @throws MetricsException if the metricName or the type of the metricValue 259 * conflicts with the configuration 260 */ 261 @Override 262 public void incrMetric(String metricName, float metricValue) { 263 setIncrement(metricName, new Float(metricValue)); 264 } 265 266 private void setAbsolute(String metricName, Number metricValue) { 267 metricTable.put(metricName, new MetricValue(metricValue, MetricValue.ABSOLUTE)); 268 } 269 270 private void setIncrement(String metricName, Number metricValue) { 271 metricTable.put(metricName, new MetricValue(metricValue, MetricValue.INCREMENT)); 272 } 273 274 /** 275 * Updates the table of buffered data which is to be sent periodically. 276 * If the tag values match an existing row, that row is updated; 277 * otherwise, a new row is added. 278 */ 279 @Override 280 public void update() { 281 context.update(this); 282 } 283 284 /** 285 * Removes the row, if it exists, in the buffered data table having tags 286 * that equal the tags that have been set on this record. 287 */ 288 @Override 289 public void remove() { 290 context.remove(this); 291 } 292 293 TagMap getTagTable() { 294 return tagTable; 295 } 296 297 Map<String, MetricValue> getMetricTable() { 298 return metricTable; 299 } 300}