You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by su...@apache.org on 2012/10/12 06:35:44 UTC
svn commit: r1397432 [2/3] - in
/hadoop/common/trunk/hadoop-common-project/hadoop-common: ./ src/main/docs/
src/main/java/org/apache/hadoop/metrics/
src/main/java/org/apache/hadoop/metrics/file/
src/main/java/org/apache/hadoop/metrics/spi/ src/main/jav...
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/MetricsException.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/MetricsException.java?rev=1397432&r1=1397431&r2=1397432&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/MetricsException.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/MetricsException.java Fri Oct 12 04:35:42 2012
@@ -1,47 +1,47 @@
-/*
- * MetricsException.java
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hadoop.metrics;
-
-import org.apache.hadoop.classification.InterfaceAudience;
-import org.apache.hadoop.classification.InterfaceStability;
-
-/**
- * General-purpose, unchecked metrics exception.
- */
-@InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
-@InterfaceStability.Evolving
-public class MetricsException extends RuntimeException {
-
- private static final long serialVersionUID = -1643257498540498497L;
-
- /** Creates a new instance of MetricsException */
- public MetricsException() {
- }
-
- /** Creates a new instance of MetricsException
- *
- * @param message an error message
- */
- public MetricsException(String message) {
- super(message);
- }
-
-}
+/*
+ * MetricsException.java
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+
+/**
+ * General-purpose, unchecked metrics exception.
+ */
+@InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
+@InterfaceStability.Evolving
+public class MetricsException extends RuntimeException {
+
+ private static final long serialVersionUID = -1643257498540498497L;
+
+ /** Creates a new instance of MetricsException */
+ public MetricsException() {
+ }
+
+ /** Creates a new instance of MetricsException
+ *
+ * @param message an error message
+ */
+ public MetricsException(String message) {
+ super(message);
+ }
+
+}
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/MetricsRecord.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/MetricsRecord.java?rev=1397432&r1=1397431&r2=1397432&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/MetricsRecord.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/MetricsRecord.java Fri Oct 12 04:35:42 2012
@@ -1,251 +1,251 @@
-/*
- * MetricsRecord.java
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hadoop.metrics;
-
-import org.apache.hadoop.classification.InterfaceAudience;
-import org.apache.hadoop.classification.InterfaceStability;
-
-/**
- * A named and optionally tagged set of records to be sent to the metrics
- * system. <p/>
- *
- * A record name identifies the kind of data to be reported. For example, a
- * program reporting statistics relating to the disks on a computer might use
- * a record name "diskStats".<p/>
- *
- * A record has zero or more <i>tags</i>. A tag has a name and a value. To
- * continue the example, the "diskStats" record might use a tag named
- * "diskName" to identify a particular disk. Sometimes it is useful to have
- * more than one tag, so there might also be a "diskType" with value "ide" or
- * "scsi" or whatever.<p/>
- *
- * A record also has zero or more <i>metrics</i>. These are the named
- * values that are to be reported to the metrics system. In the "diskStats"
- * example, possible metric names would be "diskPercentFull", "diskPercentBusy",
- * "kbReadPerSecond", etc.<p/>
- *
- * The general procedure for using a MetricsRecord is to fill in its tag and
- * metric values, and then call <code>update()</code> to pass the record to the
- * client library.
- * Metric data is not immediately sent to the metrics system
- * each time that <code>update()</code> is called.
- * An internal table is maintained, identified by the record name. This
- * table has columns
- * corresponding to the tag and the metric names, and rows
- * corresponding to each unique set of tag values. An update
- * either modifies an existing row in the table, or adds a new row with a set of
- * tag values that are different from all the other rows. Note that if there
- * are no tags, then there can be at most one row in the table. <p/>
- *
- * Once a row is added to the table, its data will be sent to the metrics system
- * on every timer period, whether or not it has been updated since the previous
- * timer period. If this is inappropriate, for example if metrics were being
- * reported by some transient object in an application, the <code>remove()</code>
- * method can be used to remove the row and thus stop the data from being
- * sent.<p/>
- *
- * Note that the <code>update()</code> method is atomic. This means that it is
- * safe for different threads to be updating the same metric. More precisely,
- * it is OK for different threads to call <code>update()</code> on MetricsRecord instances
- * with the same set of tag names and tag values. Different threads should
- * <b>not</b> use the same MetricsRecord instance at the same time.
- */
-@InterfaceAudience.Private
-@InterfaceStability.Evolving
-public interface MetricsRecord {
-
- /**
- * Returns the record name.
- *
- * @return the record name
- */
- public abstract String getRecordName();
-
- /**
- * Sets the named tag to the specified value. The tagValue may be null,
- * which is treated the same as an empty String.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- public abstract void setTag(String tagName, String tagValue);
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- public abstract void setTag(String tagName, int tagValue);
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- public abstract void setTag(String tagName, long tagValue);
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- public abstract void setTag(String tagName, short tagValue);
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- public abstract void setTag(String tagName, byte tagValue);
-
- /**
- * Removes any tag of the specified name.
- *
- * @param tagName name of a tag
- */
- public abstract void removeTag(String tagName);
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void setMetric(String metricName, int metricValue);
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void setMetric(String metricName, long metricValue);
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void setMetric(String metricName, short metricValue);
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void setMetric(String metricName, byte metricValue);
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void setMetric(String metricName, float metricValue);
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void incrMetric(String metricName, int metricValue);
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void incrMetric(String metricName, long metricValue);
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void incrMetric(String metricName, short metricValue);
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void incrMetric(String metricName, byte metricValue);
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- public abstract void incrMetric(String metricName, float metricValue);
-
- /**
- * Updates the table of buffered data which is to be sent periodically.
- * If the tag values match an existing row, that row is updated;
- * otherwise, a new row is added.
- */
- public abstract void update();
-
- /**
- * Removes, from the buffered data table, all rows having tags
- * that equal the tags that have been set on this record. For example,
- * if there are no tags on this record, all rows for this record name
- * would be removed. Or, if there is a single tag on this record, then
- * just rows containing a tag with the same name and value would be removed.
- */
- public abstract void remove();
-
-}
+/*
+ * MetricsRecord.java
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+
+/**
+ * A named and optionally tagged set of records to be sent to the metrics
+ * system. <p/>
+ *
+ * A record name identifies the kind of data to be reported. For example, a
+ * program reporting statistics relating to the disks on a computer might use
+ * a record name "diskStats".<p/>
+ *
+ * A record has zero or more <i>tags</i>. A tag has a name and a value. To
+ * continue the example, the "diskStats" record might use a tag named
+ * "diskName" to identify a particular disk. Sometimes it is useful to have
+ * more than one tag, so there might also be a "diskType" with value "ide" or
+ * "scsi" or whatever.<p/>
+ *
+ * A record also has zero or more <i>metrics</i>. These are the named
+ * values that are to be reported to the metrics system. In the "diskStats"
+ * example, possible metric names would be "diskPercentFull", "diskPercentBusy",
+ * "kbReadPerSecond", etc.<p/>
+ *
+ * The general procedure for using a MetricsRecord is to fill in its tag and
+ * metric values, and then call <code>update()</code> to pass the record to the
+ * client library.
+ * Metric data is not immediately sent to the metrics system
+ * each time that <code>update()</code> is called.
+ * An internal table is maintained, identified by the record name. This
+ * table has columns
+ * corresponding to the tag and the metric names, and rows
+ * corresponding to each unique set of tag values. An update
+ * either modifies an existing row in the table, or adds a new row with a set of
+ * tag values that are different from all the other rows. Note that if there
+ * are no tags, then there can be at most one row in the table. <p/>
+ *
+ * Once a row is added to the table, its data will be sent to the metrics system
+ * on every timer period, whether or not it has been updated since the previous
+ * timer period. If this is inappropriate, for example if metrics were being
+ * reported by some transient object in an application, the <code>remove()</code>
+ * method can be used to remove the row and thus stop the data from being
+ * sent.<p/>
+ *
+ * Note that the <code>update()</code> method is atomic. This means that it is
+ * safe for different threads to be updating the same metric. More precisely,
+ * it is OK for different threads to call <code>update()</code> on MetricsRecord instances
+ * with the same set of tag names and tag values. Different threads should
+ * <b>not</b> use the same MetricsRecord instance at the same time.
+ */
+@InterfaceAudience.Private
+@InterfaceStability.Evolving
+public interface MetricsRecord {
+
+ /**
+ * Returns the record name.
+ *
+ * @return the record name
+ */
+ public abstract String getRecordName();
+
+ /**
+ * Sets the named tag to the specified value. The tagValue may be null,
+ * which is treated the same as an empty String.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ public abstract void setTag(String tagName, String tagValue);
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ public abstract void setTag(String tagName, int tagValue);
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ public abstract void setTag(String tagName, long tagValue);
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ public abstract void setTag(String tagName, short tagValue);
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ public abstract void setTag(String tagName, byte tagValue);
+
+ /**
+ * Removes any tag of the specified name.
+ *
+ * @param tagName name of a tag
+ */
+ public abstract void removeTag(String tagName);
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void setMetric(String metricName, int metricValue);
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void setMetric(String metricName, long metricValue);
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void setMetric(String metricName, short metricValue);
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void setMetric(String metricName, byte metricValue);
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void setMetric(String metricName, float metricValue);
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void incrMetric(String metricName, int metricValue);
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void incrMetric(String metricName, long metricValue);
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void incrMetric(String metricName, short metricValue);
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void incrMetric(String metricName, byte metricValue);
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ public abstract void incrMetric(String metricName, float metricValue);
+
+ /**
+ * Updates the table of buffered data which is to be sent periodically.
+ * If the tag values match an existing row, that row is updated;
+ * otherwise, a new row is added.
+ */
+ public abstract void update();
+
+ /**
+ * Removes, from the buffered data table, all rows having tags
+ * that equal the tags that have been set on this record. For example,
+ * if there are no tags on this record, all rows for this record name
+ * would be removed. Or, if there is a single tag on this record, then
+ * just rows containing a tag with the same name and value would be removed.
+ */
+ public abstract void remove();
+
+}
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/file/FileContext.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/file/FileContext.java?rev=1397432&r1=1397431&r2=1397432&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/file/FileContext.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/file/FileContext.java Fri Oct 12 04:35:42 2012
@@ -1,159 +1,159 @@
-/*
- * FileContext.java
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hadoop.metrics.file;
-
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.PrintWriter;
-
-import org.apache.hadoop.classification.InterfaceAudience;
-import org.apache.hadoop.classification.InterfaceStability;
-import org.apache.hadoop.metrics.ContextFactory;
-import org.apache.hadoop.metrics.spi.AbstractMetricsContext;
-import org.apache.hadoop.metrics.spi.OutputRecord;
-
-/**
- * Metrics context for writing metrics to a file.<p/>
- *
- * This class is configured by setting ContextFactory attributes which in turn
- * are usually configured through a properties file. All the attributes are
- * prefixed by the contextName. For example, the properties file might contain:
- * <pre>
- * myContextName.fileName=/tmp/metrics.log
- * myContextName.period=5
- * </pre>
- * @see org.apache.hadoop.metrics2.sink.FileSink for metrics 2.0.
- */
-@InterfaceAudience.Public
-@InterfaceStability.Evolving
-@Deprecated
-public class FileContext extends AbstractMetricsContext {
-
- /* Configuration attribute names */
- @InterfaceAudience.Private
- protected static final String FILE_NAME_PROPERTY = "fileName";
- @InterfaceAudience.Private
- protected static final String PERIOD_PROPERTY = "period";
-
- private File file = null; // file for metrics to be written to
- private PrintWriter writer = null;
-
- /** Creates a new instance of FileContext */
- @InterfaceAudience.Private
- public FileContext() {}
-
- @Override
- @InterfaceAudience.Private
- public void init(String contextName, ContextFactory factory) {
- super.init(contextName, factory);
-
- String fileName = getAttribute(FILE_NAME_PROPERTY);
- if (fileName != null) {
- file = new File(fileName);
- }
-
- parseAndSetPeriod(PERIOD_PROPERTY);
- }
-
- /**
- * Returns the configured file name, or null.
- */
- @InterfaceAudience.Private
- public String getFileName() {
- if (file == null) {
- return null;
- } else {
- return file.getName();
- }
- }
-
- /**
- * Starts or restarts monitoring, by opening in append-mode, the
- * file specified by the <code>fileName</code> attribute,
- * if specified. Otherwise the data will be written to standard
- * output.
- */
- @Override
- @InterfaceAudience.Private
- public void startMonitoring()
- throws IOException
- {
- if (file == null) {
- writer = new PrintWriter(new BufferedOutputStream(System.out));
- } else {
- writer = new PrintWriter(new FileWriter(file, true));
- }
- super.startMonitoring();
- }
-
- /**
- * Stops monitoring, closing the file.
- * @see #close()
- */
- @Override
- @InterfaceAudience.Private
- public void stopMonitoring() {
- super.stopMonitoring();
-
- if (writer != null) {
- writer.close();
- writer = null;
- }
- }
-
- /**
- * Emits a metrics record to a file.
- */
- @Override
- @InterfaceAudience.Private
- public void emitRecord(String contextName, String recordName, OutputRecord outRec) {
- writer.print(contextName);
- writer.print(".");
- writer.print(recordName);
- String separator = ": ";
- for (String tagName : outRec.getTagNames()) {
- writer.print(separator);
- separator = ", ";
- writer.print(tagName);
- writer.print("=");
- writer.print(outRec.getTag(tagName));
- }
- for (String metricName : outRec.getMetricNames()) {
- writer.print(separator);
- separator = ", ";
- writer.print(metricName);
- writer.print("=");
- writer.print(outRec.getMetric(metricName));
- }
- writer.println();
- }
-
- /**
- * Flushes the output writer, forcing updates to disk.
- */
- @Override
- @InterfaceAudience.Private
- public void flush() {
- writer.flush();
- }
-}
+/*
+ * FileContext.java
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics.file;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.metrics.ContextFactory;
+import org.apache.hadoop.metrics.spi.AbstractMetricsContext;
+import org.apache.hadoop.metrics.spi.OutputRecord;
+
+/**
+ * Metrics context for writing metrics to a file.<p/>
+ *
+ * This class is configured by setting ContextFactory attributes which in turn
+ * are usually configured through a properties file. All the attributes are
+ * prefixed by the contextName. For example, the properties file might contain:
+ * <pre>
+ * myContextName.fileName=/tmp/metrics.log
+ * myContextName.period=5
+ * </pre>
+ * @see org.apache.hadoop.metrics2.sink.FileSink for metrics 2.0.
+ */
+@InterfaceAudience.Public
+@InterfaceStability.Evolving
+@Deprecated
+public class FileContext extends AbstractMetricsContext {
+
+ /* Configuration attribute names */
+ @InterfaceAudience.Private
+ protected static final String FILE_NAME_PROPERTY = "fileName";
+ @InterfaceAudience.Private
+ protected static final String PERIOD_PROPERTY = "period";
+
+ private File file = null; // file for metrics to be written to
+ private PrintWriter writer = null;
+
+ /** Creates a new instance of FileContext */
+ @InterfaceAudience.Private
+ public FileContext() {}
+
+ @Override
+ @InterfaceAudience.Private
+ public void init(String contextName, ContextFactory factory) {
+ super.init(contextName, factory);
+
+ String fileName = getAttribute(FILE_NAME_PROPERTY);
+ if (fileName != null) {
+ file = new File(fileName);
+ }
+
+ parseAndSetPeriod(PERIOD_PROPERTY);
+ }
+
+ /**
+ * Returns the configured file name, or null.
+ */
+ @InterfaceAudience.Private
+ public String getFileName() {
+ if (file == null) {
+ return null;
+ } else {
+ return file.getName();
+ }
+ }
+
+ /**
+ * Starts or restarts monitoring, by opening in append-mode, the
+ * file specified by the <code>fileName</code> attribute,
+ * if specified. Otherwise the data will be written to standard
+ * output.
+ */
+ @Override
+ @InterfaceAudience.Private
+ public void startMonitoring()
+ throws IOException
+ {
+ if (file == null) {
+ writer = new PrintWriter(new BufferedOutputStream(System.out));
+ } else {
+ writer = new PrintWriter(new FileWriter(file, true));
+ }
+ super.startMonitoring();
+ }
+
+ /**
+ * Stops monitoring, closing the file.
+ * @see #close()
+ */
+ @Override
+ @InterfaceAudience.Private
+ public void stopMonitoring() {
+ super.stopMonitoring();
+
+ if (writer != null) {
+ writer.close();
+ writer = null;
+ }
+ }
+
+ /**
+ * Emits a metrics record to a file.
+ */
+ @Override
+ @InterfaceAudience.Private
+ public void emitRecord(String contextName, String recordName, OutputRecord outRec) {
+ writer.print(contextName);
+ writer.print(".");
+ writer.print(recordName);
+ String separator = ": ";
+ for (String tagName : outRec.getTagNames()) {
+ writer.print(separator);
+ separator = ", ";
+ writer.print(tagName);
+ writer.print("=");
+ writer.print(outRec.getTag(tagName));
+ }
+ for (String metricName : outRec.getMetricNames()) {
+ writer.print(separator);
+ separator = ", ";
+ writer.print(metricName);
+ writer.print("=");
+ writer.print(outRec.getMetric(metricName));
+ }
+ writer.println();
+ }
+
+ /**
+ * Flushes the output writer, forcing updates to disk.
+ */
+ @Override
+ @InterfaceAudience.Private
+ public void flush() {
+ writer.flush();
+ }
+}
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/AbstractMetricsContext.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/AbstractMetricsContext.java?rev=1397432&r1=1397431&r2=1397432&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/AbstractMetricsContext.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/AbstractMetricsContext.java Fri Oct 12 04:35:42 2012
@@ -1,491 +1,491 @@
-/*
- * AbstractMetricsContext.java
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hadoop.metrics.spi;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.Timer;
-import java.util.TimerTask;
-import java.util.TreeMap;
-import java.util.Map.Entry;
-
-import org.apache.hadoop.classification.InterfaceAudience;
-import org.apache.hadoop.classification.InterfaceStability;
-import org.apache.hadoop.metrics.ContextFactory;
-import org.apache.hadoop.metrics.MetricsContext;
-import org.apache.hadoop.metrics.MetricsException;
-import org.apache.hadoop.metrics.MetricsRecord;
-import org.apache.hadoop.metrics.Updater;
-
-/**
- * The main class of the Service Provider Interface. This class should be
- * extended in order to integrate the Metrics API with a specific metrics
- * client library. <p/>
- *
- * This class implements the internal table of metric data, and the timer
- * on which data is to be sent to the metrics system. Subclasses must
- * override the abstract <code>emitRecord</code> method in order to transmit
- * the data. <p/>
- */
-@InterfaceAudience.Public
-@InterfaceStability.Evolving
-public abstract class AbstractMetricsContext implements MetricsContext {
-
- private int period = MetricsContext.DEFAULT_PERIOD;
- private Timer timer = null;
-
- private Set<Updater> updaters = new HashSet<Updater>(1);
- private volatile boolean isMonitoring = false;
-
- private ContextFactory factory = null;
- private String contextName = null;
-
- @InterfaceAudience.Private
- public static class TagMap extends TreeMap<String,Object> {
- private static final long serialVersionUID = 3546309335061952993L;
- TagMap() {
- super();
- }
- TagMap(TagMap orig) {
- super(orig);
- }
- /**
- * Returns true if this tagmap contains every tag in other.
- */
- public boolean containsAll(TagMap other) {
- for (Map.Entry<String,Object> entry : other.entrySet()) {
- Object value = get(entry.getKey());
- if (value == null || !value.equals(entry.getValue())) {
- // either key does not exist here, or the value is different
- return false;
- }
- }
- return true;
- }
- }
-
- @InterfaceAudience.Private
- public static class MetricMap extends TreeMap<String,Number> {
- private static final long serialVersionUID = -7495051861141631609L;
- MetricMap() {
- super();
- }
- MetricMap(MetricMap orig) {
- super(orig);
- }
- }
-
- static class RecordMap extends HashMap<TagMap,MetricMap> {
- private static final long serialVersionUID = 259835619700264611L;
- }
-
- private Map<String,RecordMap> bufferedData = new HashMap<String,RecordMap>();
-
-
- /**
- * Creates a new instance of AbstractMetricsContext
- */
- protected AbstractMetricsContext() {
- }
-
- /**
- * Initializes the context.
- */
- @Override
- public void init(String contextName, ContextFactory factory)
- {
- this.contextName = contextName;
- this.factory = factory;
- }
-
- /**
- * Convenience method for subclasses to access factory attributes.
- */
- protected String getAttribute(String attributeName) {
- String factoryAttribute = contextName + "." + attributeName;
- return (String) factory.getAttribute(factoryAttribute);
- }
-
- /**
- * Returns an attribute-value map derived from the factory attributes
- * by finding all factory attributes that begin with
- * <i>contextName</i>.<i>tableName</i>. The returned map consists of
- * those attributes with the contextName and tableName stripped off.
- */
- protected Map<String,String> getAttributeTable(String tableName) {
- String prefix = contextName + "." + tableName + ".";
- Map<String,String> result = new HashMap<String,String>();
- for (String attributeName : factory.getAttributeNames()) {
- if (attributeName.startsWith(prefix)) {
- String name = attributeName.substring(prefix.length());
- String value = (String) factory.getAttribute(attributeName);
- result.put(name, value);
- }
- }
- return result;
- }
-
- /**
- * Returns the context name.
- */
- @Override
- public String getContextName() {
- return contextName;
- }
-
- /**
- * Returns the factory by which this context was created.
- */
- public ContextFactory getContextFactory() {
- return factory;
- }
-
- /**
- * Starts or restarts monitoring, the emitting of metrics records.
- */
- @Override
- public synchronized void startMonitoring()
- throws IOException {
- if (!isMonitoring) {
- startTimer();
- isMonitoring = true;
- }
- }
-
- /**
- * Stops monitoring. This does not free buffered data.
- * @see #close()
- */
- @Override
- public synchronized void stopMonitoring() {
- if (isMonitoring) {
- stopTimer();
- isMonitoring = false;
- }
- }
-
- /**
- * Returns true if monitoring is currently in progress.
- */
- @Override
- public boolean isMonitoring() {
- return isMonitoring;
- }
-
- /**
- * Stops monitoring and frees buffered data, returning this
- * object to its initial state.
- */
- @Override
- public synchronized void close() {
- stopMonitoring();
- clearUpdaters();
- }
-
- /**
- * Creates a new AbstractMetricsRecord instance with the given <code>recordName</code>.
- * Throws an exception if the metrics implementation is configured with a fixed
- * set of record names and <code>recordName</code> is not in that set.
- *
- * @param recordName the name of the record
- * @throws MetricsException if recordName conflicts with configuration data
- */
- @Override
- public final synchronized MetricsRecord createRecord(String recordName) {
- if (bufferedData.get(recordName) == null) {
- bufferedData.put(recordName, new RecordMap());
- }
- return newRecord(recordName);
- }
-
- /**
- * Subclasses should override this if they subclass MetricsRecordImpl.
- * @param recordName the name of the record
- * @return newly created instance of MetricsRecordImpl or subclass
- */
- protected MetricsRecord newRecord(String recordName) {
- return new MetricsRecordImpl(recordName, this);
- }
-
- /**
- * Registers a callback to be called at time intervals determined by
- * the configuration.
- *
- * @param updater object to be run periodically; it should update
- * some metrics records
- */
- @Override
- public synchronized void registerUpdater(final Updater updater) {
- if (!updaters.contains(updater)) {
- updaters.add(updater);
- }
- }
-
- /**
- * Removes a callback, if it exists.
- *
- * @param updater object to be removed from the callback list
- */
- @Override
- public synchronized void unregisterUpdater(Updater updater) {
- updaters.remove(updater);
- }
-
- private synchronized void clearUpdaters() {
- updaters.clear();
- }
-
- /**
- * Starts timer if it is not already started
- */
- private synchronized void startTimer() {
- if (timer == null) {
- timer = new Timer("Timer thread for monitoring " + getContextName(),
- true);
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- try {
- timerEvent();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- };
- long millis = period * 1000;
- timer.scheduleAtFixedRate(task, millis, millis);
- }
- }
-
- /**
- * Stops timer if it is running
- */
- private synchronized void stopTimer() {
- if (timer != null) {
- timer.cancel();
- timer = null;
- }
- }
-
- /**
- * Timer callback.
- */
- private void timerEvent() throws IOException {
- if (isMonitoring) {
- Collection<Updater> myUpdaters;
- synchronized (this) {
- myUpdaters = new ArrayList<Updater>(updaters);
- }
- // Run all the registered updates without holding a lock
- // on this context
- for (Updater updater : myUpdaters) {
- try {
- updater.doUpdates(this);
- } catch (Throwable throwable) {
- throwable.printStackTrace();
- }
- }
- emitRecords();
- }
- }
-
- /**
- * Emits the records.
- */
- private synchronized void emitRecords() throws IOException {
- for (String recordName : bufferedData.keySet()) {
- RecordMap recordMap = bufferedData.get(recordName);
- synchronized (recordMap) {
- Set<Entry<TagMap, MetricMap>> entrySet = recordMap.entrySet ();
- for (Entry<TagMap, MetricMap> entry : entrySet) {
- OutputRecord outRec = new OutputRecord(entry.getKey(), entry.getValue());
- emitRecord(contextName, recordName, outRec);
- }
- }
- }
- flush();
- }
-
- /**
- * Retrieves all the records managed by this MetricsContext.
- * Useful for monitoring systems that are polling-based.
- * @return A non-null collection of all monitoring records.
- */
- @Override
- public synchronized Map<String, Collection<OutputRecord>> getAllRecords() {
- Map<String, Collection<OutputRecord>> out = new TreeMap<String, Collection<OutputRecord>>();
- for (String recordName : bufferedData.keySet()) {
- RecordMap recordMap = bufferedData.get(recordName);
- synchronized (recordMap) {
- List<OutputRecord> records = new ArrayList<OutputRecord>();
- Set<Entry<TagMap, MetricMap>> entrySet = recordMap.entrySet();
- for (Entry<TagMap, MetricMap> entry : entrySet) {
- OutputRecord outRec = new OutputRecord(entry.getKey(), entry.getValue());
- records.add(outRec);
- }
- out.put(recordName, records);
- }
- }
- return out;
- }
-
- /**
- * Sends a record to the metrics system.
- */
- protected abstract void emitRecord(String contextName, String recordName,
- OutputRecord outRec) throws IOException;
-
- /**
- * Called each period after all records have been emitted, this method does nothing.
- * Subclasses may override it in order to perform some kind of flush.
- */
- protected void flush() throws IOException {
- }
-
- /**
- * Called by MetricsRecordImpl.update(). Creates or updates a row in
- * the internal table of metric data.
- */
- protected void update(MetricsRecordImpl record) {
- String recordName = record.getRecordName();
- TagMap tagTable = record.getTagTable();
- Map<String,MetricValue> metricUpdates = record.getMetricTable();
-
- RecordMap recordMap = getRecordMap(recordName);
- synchronized (recordMap) {
- MetricMap metricMap = recordMap.get(tagTable);
- if (metricMap == null) {
- metricMap = new MetricMap();
- TagMap tagMap = new TagMap(tagTable); // clone tags
- recordMap.put(tagMap, metricMap);
- }
-
- Set<Entry<String, MetricValue>> entrySet = metricUpdates.entrySet();
- for (Entry<String, MetricValue> entry : entrySet) {
- String metricName = entry.getKey ();
- MetricValue updateValue = entry.getValue ();
- Number updateNumber = updateValue.getNumber();
- Number currentNumber = metricMap.get(metricName);
- if (currentNumber == null || updateValue.isAbsolute()) {
- metricMap.put(metricName, updateNumber);
- }
- else {
- Number newNumber = sum(updateNumber, currentNumber);
- metricMap.put(metricName, newNumber);
- }
- }
- }
- }
-
- private synchronized RecordMap getRecordMap(String recordName) {
- return bufferedData.get(recordName);
- }
-
- /**
- * Adds two numbers, coercing the second to the type of the first.
- *
- */
- private Number sum(Number a, Number b) {
- if (a instanceof Integer) {
- return Integer.valueOf(a.intValue() + b.intValue());
- }
- else if (a instanceof Float) {
- return new Float(a.floatValue() + b.floatValue());
- }
- else if (a instanceof Short) {
- return Short.valueOf((short)(a.shortValue() + b.shortValue()));
- }
- else if (a instanceof Byte) {
- return Byte.valueOf((byte)(a.byteValue() + b.byteValue()));
- }
- else if (a instanceof Long) {
- return Long.valueOf((a.longValue() + b.longValue()));
- }
- else {
- // should never happen
- throw new MetricsException("Invalid number type");
- }
-
- }
-
- /**
- * Called by MetricsRecordImpl.remove(). Removes all matching rows in
- * the internal table of metric data. A row matches if it has the same
- * tag names and values as record, but it may also have additional
- * tags.
- */
- protected void remove(MetricsRecordImpl record) {
- String recordName = record.getRecordName();
- TagMap tagTable = record.getTagTable();
-
- RecordMap recordMap = getRecordMap(recordName);
- synchronized (recordMap) {
- Iterator<TagMap> it = recordMap.keySet().iterator();
- while (it.hasNext()) {
- TagMap rowTags = it.next();
- if (rowTags.containsAll(tagTable)) {
- it.remove();
- }
- }
- }
- }
-
- /**
- * Returns the timer period.
- */
- @Override
- public int getPeriod() {
- return period;
- }
-
- /**
- * Sets the timer period
- */
- protected void setPeriod(int period) {
- this.period = period;
- }
-
- /**
- * If a period is set in the attribute passed in, override
- * the default with it.
- */
- protected void parseAndSetPeriod(String attributeName) {
- String periodStr = getAttribute(attributeName);
- if (periodStr != null) {
- int period = 0;
- try {
- period = Integer.parseInt(periodStr);
- } catch (NumberFormatException nfe) {
- }
- if (period <= 0) {
- throw new MetricsException("Invalid period: " + periodStr);
- }
- setPeriod(period);
- }
- }
-}
+/*
+ * AbstractMetricsContext.java
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics.spi;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.TreeMap;
+import java.util.Map.Entry;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.metrics.ContextFactory;
+import org.apache.hadoop.metrics.MetricsContext;
+import org.apache.hadoop.metrics.MetricsException;
+import org.apache.hadoop.metrics.MetricsRecord;
+import org.apache.hadoop.metrics.Updater;
+
+/**
+ * The main class of the Service Provider Interface. This class should be
+ * extended in order to integrate the Metrics API with a specific metrics
+ * client library. <p/>
+ *
+ * This class implements the internal table of metric data, and the timer
+ * on which data is to be sent to the metrics system. Subclasses must
+ * override the abstract <code>emitRecord</code> method in order to transmit
+ * the data. <p/>
+ */
+@InterfaceAudience.Public
+@InterfaceStability.Evolving
+public abstract class AbstractMetricsContext implements MetricsContext {
+
+ private int period = MetricsContext.DEFAULT_PERIOD;
+ private Timer timer = null;
+
+ private Set<Updater> updaters = new HashSet<Updater>(1);
+ private volatile boolean isMonitoring = false;
+
+ private ContextFactory factory = null;
+ private String contextName = null;
+
+ @InterfaceAudience.Private
+ public static class TagMap extends TreeMap<String,Object> {
+ private static final long serialVersionUID = 3546309335061952993L;
+ TagMap() {
+ super();
+ }
+ TagMap(TagMap orig) {
+ super(orig);
+ }
+ /**
+ * Returns true if this tagmap contains every tag in other.
+ */
+ public boolean containsAll(TagMap other) {
+ for (Map.Entry<String,Object> entry : other.entrySet()) {
+ Object value = get(entry.getKey());
+ if (value == null || !value.equals(entry.getValue())) {
+ // either key does not exist here, or the value is different
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+
+ @InterfaceAudience.Private
+ public static class MetricMap extends TreeMap<String,Number> {
+ private static final long serialVersionUID = -7495051861141631609L;
+ MetricMap() {
+ super();
+ }
+ MetricMap(MetricMap orig) {
+ super(orig);
+ }
+ }
+
+ static class RecordMap extends HashMap<TagMap,MetricMap> {
+ private static final long serialVersionUID = 259835619700264611L;
+ }
+
+ private Map<String,RecordMap> bufferedData = new HashMap<String,RecordMap>();
+
+
+ /**
+ * Creates a new instance of AbstractMetricsContext
+ */
+ protected AbstractMetricsContext() {
+ }
+
+ /**
+ * Initializes the context.
+ */
+ @Override
+ public void init(String contextName, ContextFactory factory)
+ {
+ this.contextName = contextName;
+ this.factory = factory;
+ }
+
+ /**
+ * Convenience method for subclasses to access factory attributes.
+ */
+ protected String getAttribute(String attributeName) {
+ String factoryAttribute = contextName + "." + attributeName;
+ return (String) factory.getAttribute(factoryAttribute);
+ }
+
+ /**
+ * Returns an attribute-value map derived from the factory attributes
+ * by finding all factory attributes that begin with
+ * <i>contextName</i>.<i>tableName</i>. The returned map consists of
+ * those attributes with the contextName and tableName stripped off.
+ */
+ protected Map<String,String> getAttributeTable(String tableName) {
+ String prefix = contextName + "." + tableName + ".";
+ Map<String,String> result = new HashMap<String,String>();
+ for (String attributeName : factory.getAttributeNames()) {
+ if (attributeName.startsWith(prefix)) {
+ String name = attributeName.substring(prefix.length());
+ String value = (String) factory.getAttribute(attributeName);
+ result.put(name, value);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Returns the context name.
+ */
+ @Override
+ public String getContextName() {
+ return contextName;
+ }
+
+ /**
+ * Returns the factory by which this context was created.
+ */
+ public ContextFactory getContextFactory() {
+ return factory;
+ }
+
+ /**
+ * Starts or restarts monitoring, the emitting of metrics records.
+ */
+ @Override
+ public synchronized void startMonitoring()
+ throws IOException {
+ if (!isMonitoring) {
+ startTimer();
+ isMonitoring = true;
+ }
+ }
+
+ /**
+ * Stops monitoring. This does not free buffered data.
+ * @see #close()
+ */
+ @Override
+ public synchronized void stopMonitoring() {
+ if (isMonitoring) {
+ stopTimer();
+ isMonitoring = false;
+ }
+ }
+
+ /**
+ * Returns true if monitoring is currently in progress.
+ */
+ @Override
+ public boolean isMonitoring() {
+ return isMonitoring;
+ }
+
+ /**
+ * Stops monitoring and frees buffered data, returning this
+ * object to its initial state.
+ */
+ @Override
+ public synchronized void close() {
+ stopMonitoring();
+ clearUpdaters();
+ }
+
+ /**
+ * Creates a new AbstractMetricsRecord instance with the given <code>recordName</code>.
+ * Throws an exception if the metrics implementation is configured with a fixed
+ * set of record names and <code>recordName</code> is not in that set.
+ *
+ * @param recordName the name of the record
+ * @throws MetricsException if recordName conflicts with configuration data
+ */
+ @Override
+ public final synchronized MetricsRecord createRecord(String recordName) {
+ if (bufferedData.get(recordName) == null) {
+ bufferedData.put(recordName, new RecordMap());
+ }
+ return newRecord(recordName);
+ }
+
+ /**
+ * Subclasses should override this if they subclass MetricsRecordImpl.
+ * @param recordName the name of the record
+ * @return newly created instance of MetricsRecordImpl or subclass
+ */
+ protected MetricsRecord newRecord(String recordName) {
+ return new MetricsRecordImpl(recordName, this);
+ }
+
+ /**
+ * Registers a callback to be called at time intervals determined by
+ * the configuration.
+ *
+ * @param updater object to be run periodically; it should update
+ * some metrics records
+ */
+ @Override
+ public synchronized void registerUpdater(final Updater updater) {
+ if (!updaters.contains(updater)) {
+ updaters.add(updater);
+ }
+ }
+
+ /**
+ * Removes a callback, if it exists.
+ *
+ * @param updater object to be removed from the callback list
+ */
+ @Override
+ public synchronized void unregisterUpdater(Updater updater) {
+ updaters.remove(updater);
+ }
+
+ private synchronized void clearUpdaters() {
+ updaters.clear();
+ }
+
+ /**
+ * Starts timer if it is not already started
+ */
+ private synchronized void startTimer() {
+ if (timer == null) {
+ timer = new Timer("Timer thread for monitoring " + getContextName(),
+ true);
+ TimerTask task = new TimerTask() {
+ @Override
+ public void run() {
+ try {
+ timerEvent();
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ }
+ }
+ };
+ long millis = period * 1000;
+ timer.scheduleAtFixedRate(task, millis, millis);
+ }
+ }
+
+ /**
+ * Stops timer if it is running
+ */
+ private synchronized void stopTimer() {
+ if (timer != null) {
+ timer.cancel();
+ timer = null;
+ }
+ }
+
+ /**
+ * Timer callback.
+ */
+ private void timerEvent() throws IOException {
+ if (isMonitoring) {
+ Collection<Updater> myUpdaters;
+ synchronized (this) {
+ myUpdaters = new ArrayList<Updater>(updaters);
+ }
+ // Run all the registered updates without holding a lock
+ // on this context
+ for (Updater updater : myUpdaters) {
+ try {
+ updater.doUpdates(this);
+ } catch (Throwable throwable) {
+ throwable.printStackTrace();
+ }
+ }
+ emitRecords();
+ }
+ }
+
+ /**
+ * Emits the records.
+ */
+ private synchronized void emitRecords() throws IOException {
+ for (String recordName : bufferedData.keySet()) {
+ RecordMap recordMap = bufferedData.get(recordName);
+ synchronized (recordMap) {
+ Set<Entry<TagMap, MetricMap>> entrySet = recordMap.entrySet ();
+ for (Entry<TagMap, MetricMap> entry : entrySet) {
+ OutputRecord outRec = new OutputRecord(entry.getKey(), entry.getValue());
+ emitRecord(contextName, recordName, outRec);
+ }
+ }
+ }
+ flush();
+ }
+
+ /**
+ * Retrieves all the records managed by this MetricsContext.
+ * Useful for monitoring systems that are polling-based.
+ * @return A non-null collection of all monitoring records.
+ */
+ @Override
+ public synchronized Map<String, Collection<OutputRecord>> getAllRecords() {
+ Map<String, Collection<OutputRecord>> out = new TreeMap<String, Collection<OutputRecord>>();
+ for (String recordName : bufferedData.keySet()) {
+ RecordMap recordMap = bufferedData.get(recordName);
+ synchronized (recordMap) {
+ List<OutputRecord> records = new ArrayList<OutputRecord>();
+ Set<Entry<TagMap, MetricMap>> entrySet = recordMap.entrySet();
+ for (Entry<TagMap, MetricMap> entry : entrySet) {
+ OutputRecord outRec = new OutputRecord(entry.getKey(), entry.getValue());
+ records.add(outRec);
+ }
+ out.put(recordName, records);
+ }
+ }
+ return out;
+ }
+
+ /**
+ * Sends a record to the metrics system.
+ */
+ protected abstract void emitRecord(String contextName, String recordName,
+ OutputRecord outRec) throws IOException;
+
+ /**
+ * Called each period after all records have been emitted, this method does nothing.
+ * Subclasses may override it in order to perform some kind of flush.
+ */
+ protected void flush() throws IOException {
+ }
+
+ /**
+ * Called by MetricsRecordImpl.update(). Creates or updates a row in
+ * the internal table of metric data.
+ */
+ protected void update(MetricsRecordImpl record) {
+ String recordName = record.getRecordName();
+ TagMap tagTable = record.getTagTable();
+ Map<String,MetricValue> metricUpdates = record.getMetricTable();
+
+ RecordMap recordMap = getRecordMap(recordName);
+ synchronized (recordMap) {
+ MetricMap metricMap = recordMap.get(tagTable);
+ if (metricMap == null) {
+ metricMap = new MetricMap();
+ TagMap tagMap = new TagMap(tagTable); // clone tags
+ recordMap.put(tagMap, metricMap);
+ }
+
+ Set<Entry<String, MetricValue>> entrySet = metricUpdates.entrySet();
+ for (Entry<String, MetricValue> entry : entrySet) {
+ String metricName = entry.getKey ();
+ MetricValue updateValue = entry.getValue ();
+ Number updateNumber = updateValue.getNumber();
+ Number currentNumber = metricMap.get(metricName);
+ if (currentNumber == null || updateValue.isAbsolute()) {
+ metricMap.put(metricName, updateNumber);
+ }
+ else {
+ Number newNumber = sum(updateNumber, currentNumber);
+ metricMap.put(metricName, newNumber);
+ }
+ }
+ }
+ }
+
+ private synchronized RecordMap getRecordMap(String recordName) {
+ return bufferedData.get(recordName);
+ }
+
+ /**
+ * Adds two numbers, coercing the second to the type of the first.
+ *
+ */
+ private Number sum(Number a, Number b) {
+ if (a instanceof Integer) {
+ return Integer.valueOf(a.intValue() + b.intValue());
+ }
+ else if (a instanceof Float) {
+ return new Float(a.floatValue() + b.floatValue());
+ }
+ else if (a instanceof Short) {
+ return Short.valueOf((short)(a.shortValue() + b.shortValue()));
+ }
+ else if (a instanceof Byte) {
+ return Byte.valueOf((byte)(a.byteValue() + b.byteValue()));
+ }
+ else if (a instanceof Long) {
+ return Long.valueOf((a.longValue() + b.longValue()));
+ }
+ else {
+ // should never happen
+ throw new MetricsException("Invalid number type");
+ }
+
+ }
+
+ /**
+ * Called by MetricsRecordImpl.remove(). Removes all matching rows in
+ * the internal table of metric data. A row matches if it has the same
+ * tag names and values as record, but it may also have additional
+ * tags.
+ */
+ protected void remove(MetricsRecordImpl record) {
+ String recordName = record.getRecordName();
+ TagMap tagTable = record.getTagTable();
+
+ RecordMap recordMap = getRecordMap(recordName);
+ synchronized (recordMap) {
+ Iterator<TagMap> it = recordMap.keySet().iterator();
+ while (it.hasNext()) {
+ TagMap rowTags = it.next();
+ if (rowTags.containsAll(tagTable)) {
+ it.remove();
+ }
+ }
+ }
+ }
+
+ /**
+ * Returns the timer period.
+ */
+ @Override
+ public int getPeriod() {
+ return period;
+ }
+
+ /**
+ * Sets the timer period
+ */
+ protected void setPeriod(int period) {
+ this.period = period;
+ }
+
+ /**
+ * If a period is set in the attribute passed in, override
+ * the default with it.
+ */
+ protected void parseAndSetPeriod(String attributeName) {
+ String periodStr = getAttribute(attributeName);
+ if (periodStr != null) {
+ int period = 0;
+ try {
+ period = Integer.parseInt(periodStr);
+ } catch (NumberFormatException nfe) {
+ }
+ if (period <= 0) {
+ throw new MetricsException("Invalid period: " + periodStr);
+ }
+ setPeriod(period);
+ }
+ }
+}
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/MetricsRecordImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/MetricsRecordImpl.java?rev=1397432&r1=1397431&r2=1397432&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/MetricsRecordImpl.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/MetricsRecordImpl.java Fri Oct 12 04:35:42 2012
@@ -1,300 +1,300 @@
-/*
- * MetricsRecordImpl.java
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.hadoop.metrics.spi;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import org.apache.hadoop.classification.InterfaceAudience;
-import org.apache.hadoop.classification.InterfaceStability;
-import org.apache.hadoop.metrics.MetricsException;
-import org.apache.hadoop.metrics.MetricsRecord;
-import org.apache.hadoop.metrics.spi.AbstractMetricsContext.TagMap;
-
-/**
- * An implementation of MetricsRecord. Keeps a back-pointer to the context
- * from which it was created, and delegates back to it on <code>update</code>
- * and <code>remove()</code>.
- */
-@InterfaceAudience.Public
-@InterfaceStability.Evolving
-public class MetricsRecordImpl implements MetricsRecord {
-
- private TagMap tagTable = new TagMap();
- private Map<String,MetricValue> metricTable = new LinkedHashMap<String,MetricValue>();
-
- private String recordName;
- private AbstractMetricsContext context;
-
-
- /** Creates a new instance of FileRecord */
- protected MetricsRecordImpl(String recordName, AbstractMetricsContext context)
- {
- this.recordName = recordName;
- this.context = context;
- }
-
- /**
- * Returns the record name.
- *
- * @return the record name
- */
- @Override
- public String getRecordName() {
- return recordName;
- }
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- @Override
- public void setTag(String tagName, String tagValue) {
- if (tagValue == null) {
- tagValue = "";
- }
- tagTable.put(tagName, tagValue);
- }
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- @Override
- public void setTag(String tagName, int tagValue) {
- tagTable.put(tagName, Integer.valueOf(tagValue));
- }
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- @Override
- public void setTag(String tagName, long tagValue) {
- tagTable.put(tagName, Long.valueOf(tagValue));
- }
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- @Override
- public void setTag(String tagName, short tagValue) {
- tagTable.put(tagName, Short.valueOf(tagValue));
- }
-
- /**
- * Sets the named tag to the specified value.
- *
- * @param tagName name of the tag
- * @param tagValue new value of the tag
- * @throws MetricsException if the tagName conflicts with the configuration
- */
- @Override
- public void setTag(String tagName, byte tagValue) {
- tagTable.put(tagName, Byte.valueOf(tagValue));
- }
-
- /**
- * Removes any tag of the specified name.
- */
- @Override
- public void removeTag(String tagName) {
- tagTable.remove(tagName);
- }
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void setMetric(String metricName, int metricValue) {
- setAbsolute(metricName, Integer.valueOf(metricValue));
- }
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void setMetric(String metricName, long metricValue) {
- setAbsolute(metricName, Long.valueOf(metricValue));
- }
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void setMetric(String metricName, short metricValue) {
- setAbsolute(metricName, Short.valueOf(metricValue));
- }
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void setMetric(String metricName, byte metricValue) {
- setAbsolute(metricName, Byte.valueOf(metricValue));
- }
-
- /**
- * Sets the named metric to the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue new value of the metric
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void setMetric(String metricName, float metricValue) {
- setAbsolute(metricName, new Float(metricValue));
- }
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void incrMetric(String metricName, int metricValue) {
- setIncrement(metricName, Integer.valueOf(metricValue));
- }
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void incrMetric(String metricName, long metricValue) {
- setIncrement(metricName, Long.valueOf(metricValue));
- }
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void incrMetric(String metricName, short metricValue) {
- setIncrement(metricName, Short.valueOf(metricValue));
- }
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void incrMetric(String metricName, byte metricValue) {
- setIncrement(metricName, Byte.valueOf(metricValue));
- }
-
- /**
- * Increments the named metric by the specified value.
- *
- * @param metricName name of the metric
- * @param metricValue incremental value
- * @throws MetricsException if the metricName or the type of the metricValue
- * conflicts with the configuration
- */
- @Override
- public void incrMetric(String metricName, float metricValue) {
- setIncrement(metricName, new Float(metricValue));
- }
-
- private void setAbsolute(String metricName, Number metricValue) {
- metricTable.put(metricName, new MetricValue(metricValue, MetricValue.ABSOLUTE));
- }
-
- private void setIncrement(String metricName, Number metricValue) {
- metricTable.put(metricName, new MetricValue(metricValue, MetricValue.INCREMENT));
- }
-
- /**
- * Updates the table of buffered data which is to be sent periodically.
- * If the tag values match an existing row, that row is updated;
- * otherwise, a new row is added.
- */
- @Override
- public void update() {
- context.update(this);
- }
-
- /**
- * Removes the row, if it exists, in the buffered data table having tags
- * that equal the tags that have been set on this record.
- */
- @Override
- public void remove() {
- context.remove(this);
- }
-
- TagMap getTagTable() {
- return tagTable;
- }
-
- Map<String, MetricValue> getMetricTable() {
- return metricTable;
- }
-}
+/*
+ * MetricsRecordImpl.java
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.metrics.spi;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.metrics.MetricsException;
+import org.apache.hadoop.metrics.MetricsRecord;
+import org.apache.hadoop.metrics.spi.AbstractMetricsContext.TagMap;
+
+/**
+ * An implementation of MetricsRecord. Keeps a back-pointer to the context
+ * from which it was created, and delegates back to it on <code>update</code>
+ * and <code>remove()</code>.
+ */
+@InterfaceAudience.Public
+@InterfaceStability.Evolving
+public class MetricsRecordImpl implements MetricsRecord {
+
+ private TagMap tagTable = new TagMap();
+ private Map<String,MetricValue> metricTable = new LinkedHashMap<String,MetricValue>();
+
+ private String recordName;
+ private AbstractMetricsContext context;
+
+
+ /** Creates a new instance of FileRecord */
+ protected MetricsRecordImpl(String recordName, AbstractMetricsContext context)
+ {
+ this.recordName = recordName;
+ this.context = context;
+ }
+
+ /**
+ * Returns the record name.
+ *
+ * @return the record name
+ */
+ @Override
+ public String getRecordName() {
+ return recordName;
+ }
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ @Override
+ public void setTag(String tagName, String tagValue) {
+ if (tagValue == null) {
+ tagValue = "";
+ }
+ tagTable.put(tagName, tagValue);
+ }
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ @Override
+ public void setTag(String tagName, int tagValue) {
+ tagTable.put(tagName, Integer.valueOf(tagValue));
+ }
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ @Override
+ public void setTag(String tagName, long tagValue) {
+ tagTable.put(tagName, Long.valueOf(tagValue));
+ }
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ @Override
+ public void setTag(String tagName, short tagValue) {
+ tagTable.put(tagName, Short.valueOf(tagValue));
+ }
+
+ /**
+ * Sets the named tag to the specified value.
+ *
+ * @param tagName name of the tag
+ * @param tagValue new value of the tag
+ * @throws MetricsException if the tagName conflicts with the configuration
+ */
+ @Override
+ public void setTag(String tagName, byte tagValue) {
+ tagTable.put(tagName, Byte.valueOf(tagValue));
+ }
+
+ /**
+ * Removes any tag of the specified name.
+ */
+ @Override
+ public void removeTag(String tagName) {
+ tagTable.remove(tagName);
+ }
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void setMetric(String metricName, int metricValue) {
+ setAbsolute(metricName, Integer.valueOf(metricValue));
+ }
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void setMetric(String metricName, long metricValue) {
+ setAbsolute(metricName, Long.valueOf(metricValue));
+ }
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void setMetric(String metricName, short metricValue) {
+ setAbsolute(metricName, Short.valueOf(metricValue));
+ }
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void setMetric(String metricName, byte metricValue) {
+ setAbsolute(metricName, Byte.valueOf(metricValue));
+ }
+
+ /**
+ * Sets the named metric to the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue new value of the metric
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void setMetric(String metricName, float metricValue) {
+ setAbsolute(metricName, new Float(metricValue));
+ }
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void incrMetric(String metricName, int metricValue) {
+ setIncrement(metricName, Integer.valueOf(metricValue));
+ }
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void incrMetric(String metricName, long metricValue) {
+ setIncrement(metricName, Long.valueOf(metricValue));
+ }
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void incrMetric(String metricName, short metricValue) {
+ setIncrement(metricName, Short.valueOf(metricValue));
+ }
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void incrMetric(String metricName, byte metricValue) {
+ setIncrement(metricName, Byte.valueOf(metricValue));
+ }
+
+ /**
+ * Increments the named metric by the specified value.
+ *
+ * @param metricName name of the metric
+ * @param metricValue incremental value
+ * @throws MetricsException if the metricName or the type of the metricValue
+ * conflicts with the configuration
+ */
+ @Override
+ public void incrMetric(String metricName, float metricValue) {
+ setIncrement(metricName, new Float(metricValue));
+ }
+
+ private void setAbsolute(String metricName, Number metricValue) {
+ metricTable.put(metricName, new MetricValue(metricValue, MetricValue.ABSOLUTE));
+ }
+
+ private void setIncrement(String metricName, Number metricValue) {
+ metricTable.put(metricName, new MetricValue(metricValue, MetricValue.INCREMENT));
+ }
+
+ /**
+ * Updates the table of buffered data which is to be sent periodically.
+ * If the tag values match an existing row, that row is updated;
+ * otherwise, a new row is added.
+ */
+ @Override
+ public void update() {
+ context.update(this);
+ }
+
+ /**
+ * Removes the row, if it exists, in the buffered data table having tags
+ * that equal the tags that have been set on this record.
+ */
+ @Override
+ public void remove() {
+ context.remove(this);
+ }
+
+ TagMap getTagTable() {
+ return tagTable;
+ }
+
+ Map<String, MetricValue> getMetricTable() {
+ return metricTable;
+ }
+}