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/22 22:43:25 UTC
svn commit: r1401071 [3/4] - in
/hadoop/common/branches/branch-trunk-win/hadoop-common-project:
hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/client/
hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/client/
hado...
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/AbstractMetricsContext.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/AbstractMetricsContext.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/AbstractMetricsContext.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/AbstractMetricsContext.java Mon Oct 22 20:43:16 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/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/MetricsRecordImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/MetricsRecordImpl.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/MetricsRecordImpl.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics/spi/MetricsRecordImpl.java Mon Oct 22 20:43:16 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;
+ }
+}
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsCollector.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsCollector.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsCollector.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsCollector.java Mon Oct 22 20:43:16 2012
@@ -30,14 +30,14 @@ public interface MetricsCollector {
/**
* Add a metrics record
* @param name of the record
- * @return a metrics record builder for the record
+ * @return a {@link MetricsRecordBuilder} for the record {@code name}
*/
public MetricsRecordBuilder addRecord(String name);
/**
* Add a metrics record
* @param info of the record
- * @return a metrics record builder for the record
+ * @return a {@link MetricsRecordBuilder} for metrics {@code info}
*/
public MetricsRecordBuilder addRecord(MetricsInfo info);
}
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsFilter.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsFilter.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsFilter.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsFilter.java Mon Oct 22 20:43:16 2012
@@ -22,7 +22,9 @@ import org.apache.hadoop.classification.
import org.apache.hadoop.classification.InterfaceStability;
/**
- * The metrics filter interface
+ * The metrics filter interface. The MetricsFilter objects can be used either to
+ * filter the metrics from {@link MetricsSource}s or to filter metrics per
+ * {@link MetricsSink}.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsInfo.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsInfo.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsInfo.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsInfo.java Mon Oct 22 20:43:16 2012
@@ -20,19 +20,24 @@ package org.apache.hadoop.metrics2;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.metrics2.annotation.Metric;
/**
- * Interface to provide immutable meta info for metrics
+ * Interface to provide immutable metainfo for metrics.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface MetricsInfo {
/**
+ * Typically name corresponds to annotation {@link Metric#value()} or
+ * the name of the class.
* @return the name of the metric/tag
*/
String name();
/**
+ * Typically the description corresponds to annotation {@link Metric#about()}
+ * or the name of the class.
* @return the description of the metric/tag
*/
String description();
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsRecord.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsRecord.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsRecord.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsRecord.java Mon Oct 22 20:43:16 2012
@@ -36,17 +36,17 @@ public interface MetricsRecord {
long timestamp();
/**
- * @return the record name
+ * @return the metrics record name
*/
String name();
/**
- * @return the description of the record
+ * @return the description of the metrics record
*/
String description();
/**
- * @return the context name of the record
+ * @return the context name of the metrics record
*/
String context();
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsRecordBuilder.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsRecordBuilder.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsRecordBuilder.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsRecordBuilder.java Mon Oct 22 20:43:16 2012
@@ -28,7 +28,7 @@ import org.apache.hadoop.classification.
@InterfaceStability.Evolving
public abstract class MetricsRecordBuilder {
/**
- * Add a metrics tag
+ * Add a metrics value with metrics information
* @param info metadata of the tag
* @param value of the tag
* @return self
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSink.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSink.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSink.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSink.java Mon Oct 22 20:43:16 2012
@@ -22,7 +22,11 @@ import org.apache.hadoop.classification.
import org.apache.hadoop.classification.InterfaceStability;
/**
- * The metrics sink interface
+ * The metrics sink interface. <p>
+ * Implementations of this interface consume the {@link MetricsRecord} generated
+ * from {@link MetricsSource}. It registers with {@link MetricsSystem} which
+ * periodically pushes the {@link MetricsRecord} to the sink using
+ * {@link #putMetrics(MetricsRecord)} method.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSource.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSource.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSource.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSource.java Mon Oct 22 20:43:16 2012
@@ -22,13 +22,15 @@ import org.apache.hadoop.classification.
import org.apache.hadoop.classification.InterfaceStability;
/**
- * The metrics source interface
+ * The source of metrics information. It generates and updates metrics. It
+ * registers with {@link MetricsSystem}, which periodically polls it to collect
+ * {@link MetricsRecord} and passes it to {@link MetricsSink}.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface MetricsSource {
/**
- * Get metrics from the source
+ * Get metrics from the metrics source
* @param collector to contain the resulting metrics snapshot
* @param all if true, return all metrics even if unchanged.
*/
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSystem.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSystem.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSystem.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/MetricsSystem.java Mon Oct 22 20:43:16 2012
@@ -22,7 +22,18 @@ import org.apache.hadoop.classification.
import org.apache.hadoop.classification.InterfaceStability;
/**
- * The metrics system interface
+ * The metrics system interface.
+ *
+ * The following components are used for metrics.
+ * <ul>
+ * <li>{@link MetricsSource} generate and update metrics information.</li>
+ * <li>{@link MetricsSink} consume the metrics information</li>
+ * </ul>
+ *
+ * {@link MetricsSource} and {@link MetricsSink} register with the metrics
+ * system. Implementations of {@link MetricsSystem} polls the
+ * {@link MetricsSource}s periodically and pass the {@link MetricsRecord}s to
+ * {@link MetricsSink}.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/annotation/Metric.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/annotation/Metric.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/annotation/Metric.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/annotation/Metric.java Mon Oct 22 20:43:16 2012
@@ -24,7 +24,8 @@ import org.apache.hadoop.classification.
import org.apache.hadoop.classification.InterfaceStability;
/**
- * Annotation interface for a single metric
+ * Annotation interface for a single metric used to annotate a field or a method
+ * in the class.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsRecordBuilderImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsRecordBuilderImpl.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsRecordBuilderImpl.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsRecordBuilderImpl.java Mon Oct 22 20:43:16 2012
@@ -32,6 +32,14 @@ import org.apache.hadoop.metrics2.Metric
import org.apache.hadoop.metrics2.lib.Interns;
import org.apache.hadoop.util.Time;
+/**
+ * {@link MetricsRecordBuilder} implementation used for building metrics records
+ * by the {@link MetricsCollector}. It provides the following functionality:
+ * <ul>
+ * <li>Allows configuring filters for metrics.
+ * </ul>
+ *
+ */
class MetricsRecordBuilderImpl extends MetricsRecordBuilder {
private final MetricsCollector parent;
private final long timestamp;
@@ -41,9 +49,15 @@ class MetricsRecordBuilderImpl extends M
private final MetricsFilter recordFilter, metricFilter;
private final boolean acceptable;
+ /**
+ * @param parent {@link MetricsCollector} using this record builder
+ * @param info metrics information
+ * @param rf
+ * @param mf
+ * @param acceptable
+ */
MetricsRecordBuilderImpl(MetricsCollector parent, MetricsInfo info,
- MetricsFilter rf, MetricsFilter mf,
- boolean acceptable) {
+ MetricsFilter rf, MetricsFilter mf, boolean acceptable) {
this.parent = parent;
timestamp = Time.now();
recInfo = info;
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsRecordImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsRecordImpl.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsRecordImpl.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsRecordImpl.java Mon Oct 22 20:43:16 2012
@@ -37,7 +37,7 @@ class MetricsRecordImpl extends Abstract
/**
* Construct a metrics record
- * @param info {@link MetricInfo} of the record
+ * @param info {@link MetricsInfo} of the record
* @param timestamp of the record
* @param tags of the record
* @param metrics of the record
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsSystemImpl.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsSystemImpl.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsSystemImpl.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsSystemImpl.java Mon Oct 22 20:43:16 2012
@@ -235,11 +235,9 @@ public class MetricsSystemImpl extends M
void registerSource(String name, String desc, MetricsSource source) {
checkNotNull(config, "config");
MetricsConfig conf = sourceConfigs.get(name);
- MetricsSourceAdapter sa = conf != null
- ? new MetricsSourceAdapter(prefix, name, desc, source,
- injectedTags, period, conf)
- : new MetricsSourceAdapter(prefix, name, desc, source,
- injectedTags, period, config.subset(SOURCE_KEY));
+ MetricsSourceAdapter sa = new MetricsSourceAdapter(prefix, name, desc,
+ source, injectedTags, period, conf != null ? conf
+ : config.subset(SOURCE_KEY));
sources.put(name, sa);
sa.start();
LOG.debug("Registered source "+ name);
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java Mon Oct 22 20:43:16 2012
@@ -27,8 +27,13 @@ import org.apache.hadoop.metrics2.Metric
import org.apache.hadoop.metrics2.MetricsSystem;
import org.apache.hadoop.metrics2.impl.MetricsSystemImpl;
+import com.google.common.annotations.VisibleForTesting;
+
/**
- * The default metrics system singleton
+ * The default metrics system singleton. This class is used by all the daemon
+ * processes(such as NameNode, DataNode, JobTracker etc.). During daemon process
+ * initialization the processes call {@link DefaultMetricsSystem#init(String)}
+ * to initialize the {@link MetricsSystem}.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
@@ -37,7 +42,10 @@ public enum DefaultMetricsSystem {
private AtomicReference<MetricsSystem> impl =
new AtomicReference<MetricsSystem>(new MetricsSystemImpl());
+
+ @VisibleForTesting
volatile boolean miniClusterMode = false;
+
final UniqueNames mBeanNames = new UniqueNames();
final UniqueNames sourceNames = new UniqueNames();
@@ -87,12 +95,12 @@ public enum DefaultMetricsSystem {
MetricsSystem getImpl() { return impl.get(); }
- @InterfaceAudience.Private
+ @VisibleForTesting
public static void setMiniClusterMode(boolean choice) {
INSTANCE.miniClusterMode = choice;
}
- @InterfaceAudience.Private
+ @VisibleForTesting
public static boolean inMiniClusterMode() {
return INSTANCE.miniClusterMode;
}
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java Mon Oct 22 20:43:16 2012
@@ -35,7 +35,16 @@ import org.apache.hadoop.metrics2.annota
import org.apache.hadoop.metrics2.annotation.Metrics;
/**
- * Helper class to build metrics source object from annotations
+ * Helper class to build {@link MetricsSource} object from annotations.
+ * <p>
+ * For a given source object:
+ * <ul>
+ * <li>Sets the {@link Field}s annotated with {@link Metric} to
+ * {@link MutableMetric} and adds it to the {@link MetricsRegistry}.</li>
+ * <li>
+ * For {@link Method}s annotated with {@link Metric} creates
+ * {@link MutableMetric} and adds it to the {@link MetricsRegistry}.</li>
+ * </ul>
*/
@InterfaceAudience.Private
public class MetricsSourceBuilder {
@@ -115,9 +124,15 @@ public class MetricsSourceBuilder {
return r;
}
+ /**
+ * Change the declared field {@code field} in {@code source} Object to
+ * {@link MutableMetric}
+ */
private void add(Object source, Field field) {
for (Annotation annotation : field.getAnnotations()) {
- if (!(annotation instanceof Metric)) continue;
+ if (!(annotation instanceof Metric)) {
+ continue;
+ }
try {
// skip fields already set
field.setAccessible(true);
@@ -131,7 +146,7 @@ public class MetricsSourceBuilder {
registry);
if (mutable != null) {
try {
- field.set(source, mutable);
+ field.set(source, mutable); // Set the source field to MutableMetric
hasAtMetric = true;
} catch (Exception e) {
throw new MetricsException("Error setting field "+ field +
@@ -141,9 +156,12 @@ public class MetricsSourceBuilder {
}
}
+ /** Add {@link MutableMetric} for a method annotated with {@link Metric} */
private void add(Object source, Method method) {
for (Annotation annotation : method.getAnnotations()) {
- if (!(annotation instanceof Metric)) continue;
+ if (!(annotation instanceof Metric)) {
+ continue;
+ }
factory.newForMethod(source, method, (Metric) annotation, registry);
hasAtMetric = true;
}
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableMetricsFactory.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableMetricsFactory.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableMetricsFactory.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableMetricsFactory.java Mon Oct 22 20:43:16 2012
@@ -130,6 +130,10 @@ public class MutableMetricsFactory {
return Interns.info(name2, about.isEmpty() ? name2 : about);
}
+ /**
+ * Remove the prefix "get", if any, from the method name. Return the
+ * capacitalized method name."
+ */
protected String getName(Method method) {
String methodName = method.getName();
if (methodName.startsWith("get")) {
@@ -140,12 +144,15 @@ public class MutableMetricsFactory {
protected MetricsInfo getInfo(Metric annotation, String defaultName) {
String[] value = annotation.value();
- if (value.length == 2) {
+ if (value.length == 2) {
+ // Use name and description from the annotation
return Interns.info(value[0], value[1]);
}
if (value.length == 1) {
+ // Use description from the annotation and method name as metric name
return Interns.info(defaultName, value[0]);
}
+ // Use method name as metric name and description
return Interns.info(defaultName, defaultName);
}
}
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableQuantiles.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableQuantiles.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableQuantiles.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MutableQuantiles.java Mon Oct 22 20:43:16 2012
@@ -20,7 +20,6 @@ package org.apache.hadoop.metrics2.lib;
import static org.apache.hadoop.metrics2.lib.Interns.info;
-import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -150,14 +149,8 @@ public class MutableQuantiles extends Mu
@Override
public void run() {
synchronized (parent) {
- try {
- parent.previousCount = parent.estimator.getCount();
- parent.previousSnapshot = parent.estimator.snapshot();
- } catch (IOException e) {
- // Couldn't get a new snapshot because the window was empty
- parent.previousCount = 0;
- parent.previousSnapshot = null;
- }
+ parent.previousCount = parent.estimator.getCount();
+ parent.previousSnapshot = parent.estimator.snapshot();
parent.estimator.clear();
}
parent.setChanged();
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/Quantile.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/Quantile.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/Quantile.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/Quantile.java Mon Oct 22 20:43:16 2012
@@ -20,12 +20,14 @@ package org.apache.hadoop.metrics2.util;
import org.apache.hadoop.classification.InterfaceAudience;
+import com.google.common.collect.ComparisonChain;
+
/**
* Specifies a quantile (with error bounds) to be watched by a
* {@link SampleQuantiles} object.
*/
@InterfaceAudience.Private
-public class Quantile {
+public class Quantile implements Comparable<Quantile> {
public final double quantile;
public final double error;
@@ -57,4 +59,19 @@ public class Quantile {
return (int) (Double.doubleToLongBits(quantile) ^ Double
.doubleToLongBits(error));
}
+
+ @Override
+ public int compareTo(Quantile other) {
+ return ComparisonChain.start()
+ .compare(quantile, other.quantile)
+ .compare(error, other.error)
+ .result();
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%.2f %%ile +/- %.2f%%",
+ quantile * 100, error * 100);
+ }
+
}
\ No newline at end of file
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/SampleQuantiles.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/SampleQuantiles.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/SampleQuantiles.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/util/SampleQuantiles.java Mon Oct 22 20:43:16 2012
@@ -18,16 +18,17 @@
package org.apache.hadoop.metrics2.util;
-import java.io.IOException;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map;
+import java.util.TreeMap;
import org.apache.hadoop.classification.InterfaceAudience;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
/**
* Implementation of the Cormode, Korn, Muthukrishnan, and Srivastava algorithm
@@ -202,10 +203,8 @@ public class SampleQuantiles {
* @param quantile Queried quantile, e.g. 0.50 or 0.99.
* @return Estimated value at that quantile.
*/
- private long query(double quantile) throws IOException {
- if (samples.size() == 0) {
- throw new IOException("No samples present");
- }
+ private long query(double quantile) {
+ Preconditions.checkState(!samples.isEmpty(), "no data in estimator");
int rankMin = 0;
int desired = (int) (quantile * count);
@@ -231,14 +230,18 @@ public class SampleQuantiles {
/**
* Get a snapshot of the current values of all the tracked quantiles.
*
- * @return snapshot of the tracked quantiles
- * @throws IOException
- * if no items have been added to the estimator
+ * @return snapshot of the tracked quantiles. If no items are added
+ * to the estimator, returns null.
*/
- synchronized public Map<Quantile, Long> snapshot() throws IOException {
+ synchronized public Map<Quantile, Long> snapshot() {
// flush the buffer first for best results
insertBatch();
- Map<Quantile, Long> values = new HashMap<Quantile, Long>(quantiles.length);
+
+ if (samples.isEmpty()) {
+ return null;
+ }
+
+ Map<Quantile, Long> values = new TreeMap<Quantile, Long>();
for (int i = 0; i < quantiles.length; i++) {
values.put(quantiles[i], query(quantiles[i].quantile));
}
@@ -273,6 +276,16 @@ public class SampleQuantiles {
bufferCount = 0;
samples.clear();
}
+
+ @Override
+ synchronized public String toString() {
+ Map<Quantile, Long> data = snapshot();
+ if (data == null) {
+ return "[no samples]";
+ } else {
+ return Joiner.on("\n").withKeyValueSeparator(": ").join(data);
+ }
+ }
/**
* Describes a measured value passed to the estimator, tracking additional
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/JniBasedUnixGroupsMapping.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/JniBasedUnixGroupsMapping.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/JniBasedUnixGroupsMapping.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/JniBasedUnixGroupsMapping.java Mon Oct 22 20:43:16 2012
@@ -48,7 +48,7 @@ public class JniBasedUnixGroupsMapping i
throw new RuntimeException("Bailing out since native library couldn't " +
"be loaded");
}
- LOG.info("Using JniBasedUnixGroupsMapping for Group resolution");
+ LOG.debug("Using JniBasedUnixGroupsMapping for Group resolution");
}
@Override
@@ -57,7 +57,11 @@ public class JniBasedUnixGroupsMapping i
try {
groups = getGroupForUser(user);
} catch (Exception e) {
- LOG.warn("Error getting groups for " + user, e);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Error getting groups for " + user, e);
+ } else {
+ LOG.info("Error getting groups for " + user + ": " + e.getMessage());
+ }
}
return Arrays.asList(groups);
}
Modified: hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/JniBasedUnixGroupsNetgroupMapping.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/JniBasedUnixGroupsNetgroupMapping.java?rev=1401071&r1=1401070&r2=1401071&view=diff
==============================================================================
--- hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/JniBasedUnixGroupsNetgroupMapping.java (original)
+++ hadoop/common/branches/branch-trunk-win/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/JniBasedUnixGroupsNetgroupMapping.java Mon Oct 22 20:43:16 2012
@@ -52,7 +52,7 @@ public class JniBasedUnixGroupsNetgroupM
throw new RuntimeException("Bailing out since native library couldn't " +
"be loaded");
}
- LOG.info("Using JniBasedUnixGroupsNetgroupMapping for Netgroup resolution");
+ LOG.debug("Using JniBasedUnixGroupsNetgroupMapping for Netgroup resolution");
}
/**
@@ -115,7 +115,12 @@ public class JniBasedUnixGroupsNetgroupM
// JNI code does not expect '@' at the begining of the group name
users = getUsersForNetgroupJNI(netgroup.substring(1));
} catch (Exception e) {
- LOG.warn("error getting users for netgroup " + netgroup, e);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Error getting users for netgroup " + netgroup, e);
+ } else {
+ LOG.info("Error getting users for netgroup " + netgroup +
+ ": " + e.getMessage());
+ }
}
if (users != null && users.length != 0) {
return Arrays.asList(users);