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 public String getRecordName() { 061 return recordName; 062 } 063 064 /** 065 * Sets the named tag to the specified value. 066 * 067 * @param tagName name of the tag 068 * @param tagValue new value of the tag 069 * @throws MetricsException if the tagName conflicts with the configuration 070 */ 071 public void setTag(String tagName, String tagValue) { 072 if (tagValue == null) { 073 tagValue = ""; 074 } 075 tagTable.put(tagName, tagValue); 076 } 077 078 /** 079 * Sets the named tag to the specified value. 080 * 081 * @param tagName name of the tag 082 * @param tagValue new value of the tag 083 * @throws MetricsException if the tagName conflicts with the configuration 084 */ 085 public void setTag(String tagName, int tagValue) { 086 tagTable.put(tagName, Integer.valueOf(tagValue)); 087 } 088 089 /** 090 * Sets the named tag to the specified value. 091 * 092 * @param tagName name of the tag 093 * @param tagValue new value of the tag 094 * @throws MetricsException if the tagName conflicts with the configuration 095 */ 096 public void setTag(String tagName, long tagValue) { 097 tagTable.put(tagName, Long.valueOf(tagValue)); 098 } 099 100 /** 101 * Sets the named tag to the specified value. 102 * 103 * @param tagName name of the tag 104 * @param tagValue new value of the tag 105 * @throws MetricsException if the tagName conflicts with the configuration 106 */ 107 public void setTag(String tagName, short tagValue) { 108 tagTable.put(tagName, Short.valueOf(tagValue)); 109 } 110 111 /** 112 * Sets the named tag to the specified value. 113 * 114 * @param tagName name of the tag 115 * @param tagValue new value of the tag 116 * @throws MetricsException if the tagName conflicts with the configuration 117 */ 118 public void setTag(String tagName, byte tagValue) { 119 tagTable.put(tagName, Byte.valueOf(tagValue)); 120 } 121 122 /** 123 * Removes any tag of the specified name. 124 */ 125 public void removeTag(String tagName) { 126 tagTable.remove(tagName); 127 } 128 129 /** 130 * Sets the named metric to the specified value. 131 * 132 * @param metricName name of the metric 133 * @param metricValue new value of the metric 134 * @throws MetricsException if the metricName or the type of the metricValue 135 * conflicts with the configuration 136 */ 137 public void setMetric(String metricName, int metricValue) { 138 setAbsolute(metricName, Integer.valueOf(metricValue)); 139 } 140 141 /** 142 * Sets the named metric to the specified value. 143 * 144 * @param metricName name of the metric 145 * @param metricValue new value of the metric 146 * @throws MetricsException if the metricName or the type of the metricValue 147 * conflicts with the configuration 148 */ 149 public void setMetric(String metricName, long metricValue) { 150 setAbsolute(metricName, Long.valueOf(metricValue)); 151 } 152 153 /** 154 * Sets the named metric to the specified value. 155 * 156 * @param metricName name of the metric 157 * @param metricValue new value of the metric 158 * @throws MetricsException if the metricName or the type of the metricValue 159 * conflicts with the configuration 160 */ 161 public void setMetric(String metricName, short metricValue) { 162 setAbsolute(metricName, Short.valueOf(metricValue)); 163 } 164 165 /** 166 * Sets the named metric to the specified value. 167 * 168 * @param metricName name of the metric 169 * @param metricValue new value of the metric 170 * @throws MetricsException if the metricName or the type of the metricValue 171 * conflicts with the configuration 172 */ 173 public void setMetric(String metricName, byte metricValue) { 174 setAbsolute(metricName, Byte.valueOf(metricValue)); 175 } 176 177 /** 178 * Sets the named metric to the specified value. 179 * 180 * @param metricName name of the metric 181 * @param metricValue new value of the metric 182 * @throws MetricsException if the metricName or the type of the metricValue 183 * conflicts with the configuration 184 */ 185 public void setMetric(String metricName, float metricValue) { 186 setAbsolute(metricName, new Float(metricValue)); 187 } 188 189 /** 190 * Increments the named metric by the specified value. 191 * 192 * @param metricName name of the metric 193 * @param metricValue incremental value 194 * @throws MetricsException if the metricName or the type of the metricValue 195 * conflicts with the configuration 196 */ 197 public void incrMetric(String metricName, int metricValue) { 198 setIncrement(metricName, Integer.valueOf(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 public void incrMetric(String metricName, long metricValue) { 210 setIncrement(metricName, Long.valueOf(metricValue)); 211 } 212 213 /** 214 * Increments the named metric by the specified value. 215 * 216 * @param metricName name of the metric 217 * @param metricValue incremental value 218 * @throws MetricsException if the metricName or the type of the metricValue 219 * conflicts with the configuration 220 */ 221 public void incrMetric(String metricName, short metricValue) { 222 setIncrement(metricName, Short.valueOf(metricValue)); 223 } 224 225 /** 226 * Increments the named metric by the specified value. 227 * 228 * @param metricName name of the metric 229 * @param metricValue incremental value 230 * @throws MetricsException if the metricName or the type of the metricValue 231 * conflicts with the configuration 232 */ 233 public void incrMetric(String metricName, byte metricValue) { 234 setIncrement(metricName, Byte.valueOf(metricValue)); 235 } 236 237 /** 238 * Increments the named metric by the specified value. 239 * 240 * @param metricName name of the metric 241 * @param metricValue incremental value 242 * @throws MetricsException if the metricName or the type of the metricValue 243 * conflicts with the configuration 244 */ 245 public void incrMetric(String metricName, float metricValue) { 246 setIncrement(metricName, new Float(metricValue)); 247 } 248 249 private void setAbsolute(String metricName, Number metricValue) { 250 metricTable.put(metricName, new MetricValue(metricValue, MetricValue.ABSOLUTE)); 251 } 252 253 private void setIncrement(String metricName, Number metricValue) { 254 metricTable.put(metricName, new MetricValue(metricValue, MetricValue.INCREMENT)); 255 } 256 257 /** 258 * Updates the table of buffered data which is to be sent periodically. 259 * If the tag values match an existing row, that row is updated; 260 * otherwise, a new row is added. 261 */ 262 public void update() { 263 context.update(this); 264 } 265 266 /** 267 * Removes the row, if it exists, in the buffered data table having tags 268 * that equal the tags that have been set on this record. 269 */ 270 public void remove() { 271 context.remove(this); 272 } 273 274 TagMap getTagTable() { 275 return tagTable; 276 } 277 278 Map<String, MetricValue> getMetricTable() { 279 return metricTable; 280 } 281}