hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From omal...@apache.org
Subject svn commit: r1077597 [3/6] - in /hadoop/common/branches/branch-0.20-security-patches: ./ conf/ ivy/ src/core/org/apache/hadoop/ipc/ src/core/org/apache/hadoop/ipc/metrics/ src/core/org/apache/hadoop/log/ src/core/org/apache/hadoop/metrics/ src/core/org...
Date Fri, 04 Mar 2011 04:34:00 GMT
Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/DefaultMetricsSystem.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,99 @@
+/**
+ * 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.metrics2.lib;
+
+import org.apache.hadoop.metrics2.MetricsSink;
+import org.apache.hadoop.metrics2.MetricsSource;
+import org.apache.hadoop.metrics2.MetricsSystem;
+import org.apache.hadoop.metrics2.impl.MetricsSystemImpl;
+
+/**
+ * The default metrics system singleton
+ */
+public enum DefaultMetricsSystem implements MetricsSystem {
+
+  /**
+   * The singleton instance
+   */
+  INSTANCE;
+
+  private static final int VERSION = 2;
+  private final MetricsSystemImpl impl = new MetricsSystemImpl();
+
+  private MetricsSystem init(String prefix) {
+    impl.init(prefix);
+    return impl;
+  }
+
+  /**
+   * Common static convenience method to initialize the metrics system
+   * @param prefix  for configuration
+   * @return the metrics system instance
+   */
+  public static MetricsSystem initialize(String prefix) {
+    return INSTANCE.init(prefix);
+  }
+
+  public <T extends MetricsSource> T
+  register(String name, String desc, T source) {
+    return impl.register(name, desc, source);
+  }
+
+  /**
+   * Common static method to register a source
+   * @param <T>   type of the source
+   * @param name  of the source
+   * @param desc  description
+   * @param source  the source object to register
+   * @return the source object
+   */
+  public static <T extends MetricsSource> T
+  registerSource(String name, String desc, T source) {
+    return INSTANCE.register(name, desc, source);
+  }
+
+  public <T extends MetricsSink> T register(String name, String desc, T sink) {
+    return impl.register(name, desc, sink);
+  }
+
+  public void register(Callback callback) {
+    impl.register(callback);
+  }
+
+  public void start() {
+    impl.start();
+  }
+
+  public void stop() {
+    impl.stop();
+  }
+
+  public void refreshMBeans() {
+    impl.refreshMBeans();
+  }
+
+  public String currentConfig() {
+    return impl.currentConfig();
+  }
+
+  public void shutdown() {
+    impl.shutdown();
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutable.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutable.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutable.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutable.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,75 @@
+/**
+ * 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.metrics2.lib;
+
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+
+/**
+ * The mutable metric interface
+ */
+public abstract class MetricMutable {
+
+  /** name of the metric */
+  public final String name;
+  /** description of the metric */
+  public final String description;
+
+  private volatile boolean changed = true;
+
+  /**
+   * Construct a mutable metric
+   * @param name  of the metric
+   * @param description of the metric
+   */
+  public MetricMutable(String name, String description) {
+    this.name = name;
+    this.description = description;
+  }
+
+  /**
+   * Get a snapshot/snapshot of the metric
+   * @param builder the metrics record builder
+   * @param all if true, snapshot unchanged metrics as well
+   */
+  public abstract void snapshot(MetricsRecordBuilder builder, boolean all);
+
+  /**
+   * Get a sampe/snapshot of metric if changed
+   * @param builder the metrics record builder
+   */
+  public void snapshot(MetricsRecordBuilder builder) {
+    snapshot(builder, false);
+  }
+
+  /**
+   * Set the changed flag in mutable operations
+   */
+  protected void setChanged() { changed = true; }
+
+  /**
+   * Clear the changed flag in the snapshot operations
+   */
+  protected void clearChanged() { changed = false; }
+
+  /**
+   * @return  true if metric is changed since last snapshot/snapshot
+   */
+  public boolean changed() { return changed; }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounter.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounter.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounter.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounter.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,41 @@
+/**
+ * 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.metrics2.lib;
+
+/**
+ * The mutable counter (monotonically increasing) metric interface
+ * @param <T> the type of the metric
+ */
+public abstract class MetricMutableCounter<T extends Number>
+    extends MetricMutable {
+
+  /**
+   * Construct the metric with name and description
+   * @param name
+   * @param description
+   */
+  public MetricMutableCounter(String name, String description) {
+    super(name, description);
+  }
+
+  /**
+   * Increment the metric value by 1.
+   */
+  public abstract void incr();
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounterInt.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounterInt.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounterInt.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounterInt.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,63 @@
+/**
+ * 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.metrics2.lib;
+
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+
+/**
+ * A mutable int counter for implementing metrics sources
+ */
+public class MetricMutableCounterInt extends MetricMutableCounter<Integer> {
+
+  private volatile int value;
+
+  /**
+   * Construct a mutable int counter
+   * @param name  of the counter
+   * @param description of the counter
+   * @param initValue the initial value of the counter
+   */
+  public MetricMutableCounterInt(String name, String description,
+                                 int initValue) {
+    super(name, description);
+    this.value = initValue;
+  }
+
+  public synchronized void incr() {
+    ++value;
+    setChanged();
+  }
+
+  /**
+   * Increment the value by a delta
+   * @param delta of the increment
+   */
+  public synchronized void incr(int delta) {
+    value += delta;
+    setChanged();
+  }
+
+  public void snapshot(MetricsRecordBuilder builder, boolean all) {
+    if (all || changed()) {
+      builder.addCounter(name, description, value);
+      clearChanged();
+    }
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounterLong.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounterLong.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounterLong.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableCounterLong.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,63 @@
+/**
+ * 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.metrics2.lib;
+
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+
+/**
+ * A mutable long counter
+ */
+public class MetricMutableCounterLong extends MetricMutableCounter<Long> {
+
+  private volatile long value;
+
+  /**
+   * Construct a mutable long counter
+   * @param name  of the counter
+   * @param description of the counter
+   * @param initValue the initial value of the counter
+   */
+  public MetricMutableCounterLong(String name, String description,
+                                  long initValue) {
+    super(name, description);
+    this.value = initValue;
+  }
+
+  public synchronized void incr() {
+    ++value;
+    setChanged();
+  }
+
+  /**
+   * Increment the value by a delta
+   * @param delta of the increment
+   */
+  public synchronized void incr(long delta) {
+    value += delta;
+    setChanged();
+  }
+
+  public void snapshot(MetricsRecordBuilder builder, boolean all) {
+    if (all || changed()) {
+      builder.addCounter(name, description, value);
+      clearChanged();
+    }
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableFactory.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableFactory.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableFactory.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableFactory.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,156 @@
+/**
+ * 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.metrics2.lib;
+
+import org.apache.hadoop.metrics2.Metric;
+
+/**
+ * Factory class for mutable metrics
+ */
+public class MetricMutableFactory {
+
+  static final String DEFAULT_SAMPLE_NAME = "ops";
+  static final String DEFAULT_VALUE_NAME  = "time";
+
+  /**
+   * Create a new mutable metric by name
+   * Usually overridden by app specific factory
+   * @param name  of the metric
+   * @return  a new metric object
+   */
+  public MetricMutable newMetric(String name) {
+    return null;
+  }
+
+  /**
+   * Create a mutable integer counter
+   * @param name  of the metric
+   * @param description of the metric
+   * @param initValue of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableCounterInt newCounter(String name, String description,
+                                            int initValue) {
+    return new MetricMutableCounterInt(name, description, initValue);
+  }
+
+  /**
+   * Create a mutable integer counter with name only.
+   * Usually gets overridden.
+   * @param name  of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableCounterInt newCounterInt(String name) {
+    return new MetricMutableCounterInt(name, Metric.NO_DESCRIPTION, 0);
+  }
+
+  /**
+   * Create a mutable long integer counter
+   * @param name  of the metric
+   * @param description of the metric
+   * @param initValue of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableCounterLong newCounter(String name, String description,
+                                             long initValue) {
+    return new MetricMutableCounterLong(name, description, initValue);
+  }
+
+  /**
+   * Create a mutable long integer counter with a name
+   * Usually gets overridden.
+   * @param name  of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableCounterLong newCounterLong(String name) {
+    return new MetricMutableCounterLong(name, Metric.NO_DESCRIPTION, 0L);
+  }
+
+  /**
+   * Create a mutable integer gauge
+   * @param name  of the metric
+   * @param description of the metric
+   * @param initValue of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableGaugeInt newGauge(String name, String description,
+                                        int initValue) {
+    return new MetricMutableGaugeInt(name, description, initValue);
+  }
+
+  /**
+   * Create a mutable integer gauge with name only.
+   * Usually gets overridden.
+   * @param name  of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableGaugeInt newGaugeInt(String name) {
+    return new MetricMutableGaugeInt(name, Metric.NO_DESCRIPTION, 0);
+  }
+
+  /**
+   * Create a mutable long integer gauge
+   * @param name  of the metric
+   * @param description of the metric
+   * @param initValue of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableGaugeLong newGauge(String name, String description,
+                                         long initValue) {
+    return new MetricMutableGaugeLong(name, description, initValue);
+  }
+
+  /**
+   * Create a mutable long integer gauge with name only.
+   * Usually gets overridden.
+   * @param name  of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableGaugeLong newGaugeLong(String name) {
+    return new MetricMutableGaugeLong(name, Metric.NO_DESCRIPTION, 0L);
+  }
+
+  /**
+   * Create a mutable stat metric
+   * @param name  of the metric
+   * @param description of the metric
+   * @param sampleName  of the metric (e.g., ops)
+   * @param valueName   of the metric (e.g., time or latency)
+   * @param extended    if true, produces extended stat (stdev, min/max etc.)
+   * @return  a new metric object
+   */
+  public MetricMutableStat newStat(String name, String description,
+                                   String sampleName, String valueName,
+                                   boolean extended) {
+    return new MetricMutableStat(name, description, sampleName, valueName,
+                                 extended);
+  }
+
+  /**
+   * Create a mutable stat metric with name only.
+   * Usually gets overridden.
+   * @param name  of the metric
+   * @return  a new metric object
+   */
+  public MetricMutableStat newStat(String name) {
+    return new MetricMutableStat(name, name, DEFAULT_SAMPLE_NAME,
+                                 DEFAULT_VALUE_NAME);
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGauge.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGauge.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGauge.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGauge.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,47 @@
+/**
+ * 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.metrics2.lib;
+
+/**
+ * The mutable gauge metric interface
+ * @param <T> the type of the metric
+ */
+public abstract class MetricMutableGauge<T extends Number>
+    extends MetricMutable {
+
+  /**
+   * Construct the metric with name and description
+   * @param name  of the metric
+   * @param description of the metric
+   */
+  public MetricMutableGauge(String name, String description) {
+    super(name, description);
+  }
+
+  /**
+   * Increment the value of the metric by 1
+   */
+  public abstract void incr();
+
+  /**
+   * Decrement the value of the metric by 1
+   */
+  public abstract void decr();
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGaugeInt.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGaugeInt.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGaugeInt.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGaugeInt.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,86 @@
+/**
+ * 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.metrics2.lib;
+
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+
+/**
+ * A mutable int gauge
+ */
+public class MetricMutableGaugeInt extends MetricMutableGauge<Integer> {
+
+  private volatile int value;
+
+  /**
+   * Construct a mutable int gauge metric
+   * @param name  of the gauge
+   * @param description of the gauge
+   * @param initValue the initial value of the gauge
+   */
+  public MetricMutableGaugeInt(String name, String description,
+                               int initValue) {
+    super(name, description);
+    this.value = initValue;
+  }
+
+  public synchronized void incr() {
+    ++value;
+    setChanged();
+  }
+
+  /**
+   * Increment by delta
+   * @param delta of the increment
+   */
+  public synchronized void incr(int delta) {
+    value += delta;
+    setChanged();
+  }
+
+  public synchronized void decr() {
+    --value;
+    setChanged();
+  }
+
+  /**
+   * decrement by delta
+   * @param delta of the decrement
+   */
+  public synchronized void decr(int delta) {
+    value -= delta;
+    setChanged();
+  }
+
+  /**
+   * Set the value of the metric
+   * @param value to set
+   */
+  public void set(int value) {
+    this.value = value;
+    setChanged();
+  }
+
+  public void snapshot(MetricsRecordBuilder builder, boolean all) {
+    if (all || changed()) {
+      builder.addGauge(name, description, value);
+      clearChanged();
+    }
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGaugeLong.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGaugeLong.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGaugeLong.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableGaugeLong.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,86 @@
+/**
+ * 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.metrics2.lib;
+
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+
+/**
+ * A mutable long gauge
+ */
+public class MetricMutableGaugeLong extends MetricMutableGauge<Long> {
+
+  private volatile long value;
+
+  /**
+   * Construct a mutable long gauge metric
+   * @param name  of the gauge
+   * @param description of the gauge
+   * @param initValue the initial value of the gauge
+   */
+  public MetricMutableGaugeLong(String name, String description,
+                                long initValue) {
+    super(name, description);
+    this.value = initValue;
+  }
+
+  public synchronized void incr() {
+    ++value;
+    setChanged();
+  }
+
+  /**
+   * Increment by delta
+   * @param delta of the increment
+   */
+  public synchronized void incr(long delta) {
+    value += delta;
+    setChanged();
+  }
+
+  public synchronized void decr() {
+    --value;
+    setChanged();
+  }
+
+  /**
+   * decrement by delta
+   * @param delta of the decrement
+   */
+  public synchronized void decr(long delta) {
+    value -= delta;
+    setChanged();
+  }
+
+  /**
+   * Set the value of the metric
+   * @param value to set
+   */
+  public void set(long value) {
+    this.value = value;
+    setChanged();
+  }
+
+  public void snapshot(MetricsRecordBuilder builder, boolean all) {
+    if (all || changed()) {
+      builder.addGauge(name, description, value);
+      clearChanged();
+    }
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableStat.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableStat.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableStat.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricMutableStat.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,140 @@
+/**
+ * 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.metrics2.lib;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.util.SampleStat;
+
+/**
+ * A mutable metric with stats
+ *
+ * Useful for keep throughput/latency stats.
+ * e.g., new MetricMutableStat("rpcName", "rpcName stats", "ops", "time");
+ */
+public class MetricMutableStat extends MetricMutable {
+
+  private final String numSamplesName, numSamplesDesc;
+  private final String avgValueName, avgValueDesc;
+  private final String stdevValueName, stdevValueDesc;
+  private final String iMinValueName, iMinValueDesc;
+  private final String iMaxValueName, iMaxValueDesc;
+  private final String minValueName, minValueDesc;
+  private final String maxValueName, maxValueDesc;
+
+  private final SampleStat intervalStat = new SampleStat();
+  private final SampleStat prevStat = new SampleStat();
+  private final SampleStat.MinMax minMax = new SampleStat.MinMax();
+  private long numSamples = 0;
+  private boolean extended = false;
+
+  /**
+   * Construct a sample statistics metric
+   * @param name        of the metric
+   * @param description of the metric
+   * @param sampleName  of the metric (e.g. "ops")
+   * @param valueName   of the metric (e.g. "time", "latency")
+   * @param extended    create extended stats (stdev, min/max etc.) by default.
+   */
+  public MetricMutableStat(String name, String description,
+                           String sampleName, String valueName,
+                           boolean extended) {
+    super(name, description);
+    String desc = StringUtils.uncapitalize(description);
+    numSamplesName = name +"_num_"+ sampleName;
+    numSamplesDesc = "Number of "+ sampleName +" for "+ desc;
+    avgValueName = name +"_avg_"+ valueName;
+    avgValueDesc = "Average "+ valueName +" for "+ desc;
+    stdevValueName = name +"_stdev_"+ valueName;
+    stdevValueDesc = "Standard deviation of "+ valueName +" for "+ desc;
+    iMinValueName = name +"_imin_"+ valueName;
+    iMinValueDesc = "Interval min "+ valueName +" for "+ desc;
+    iMaxValueName = name + "_imax_"+ valueName;
+    iMaxValueDesc = "Interval max "+ valueName +" for "+ desc;
+    minValueName = name +"_min_"+ valueName;
+    minValueDesc = "Min "+ valueName +" for "+ desc;
+    maxValueName = name +"_max_"+ valueName;
+    maxValueDesc = "Max "+ valueName +" for "+ desc;
+    this.extended = extended;
+  }
+
+  /**
+   * Construct a snapshot stat metric with extended stat off by default
+   * @param name        of the metric
+   * @param description of the metric
+   * @param sampleName  of the metric (e.g. "ops")
+   * @param valueName   of the metric (e.g. "time", "latency")
+   */
+  public MetricMutableStat(String name, String description,
+                           String sampleName, String valueName) {
+    this(name, description, sampleName, valueName, false);
+  }
+
+  /**
+   * Add a number of samples and their sum to the running stat
+   * @param numSamples  number of samples
+   * @param sum of the samples
+   */
+  public synchronized void add(long numSamples, long sum) {
+    intervalStat.add(numSamples, sum);
+    setChanged();
+  }
+
+  /**
+   * Add a snapshot to the metric
+   * @param value of the metric
+   */
+  public synchronized void add(long value) {
+    intervalStat.add(value);
+    minMax.add(value);
+    setChanged();
+  }
+
+  public synchronized void snapshot(MetricsRecordBuilder builder, boolean all) {
+    if (all || changed()) {
+      numSamples += intervalStat.numSamples();
+      builder.addCounter(numSamplesName, numSamplesDesc, numSamples);
+      builder.addGauge(avgValueName, avgValueDesc, lastStat().mean());
+      if (extended) {
+        builder.addGauge(stdevValueName, stdevValueDesc, lastStat().stddev());
+        builder.addGauge(iMinValueName, iMinValueDesc, lastStat().min());
+        builder.addGauge(iMaxValueName, iMaxValueDesc, lastStat().max());
+        builder.addGauge(minValueName, minValueDesc, minMax.min());
+        builder.addGauge(maxValueName, maxValueDesc, minMax.max());
+      }
+      if (changed()) {
+        intervalStat.copyTo(prevStat);
+        intervalStat.reset();
+        clearChanged();
+      }
+    }
+  }
+
+  private SampleStat lastStat() {
+    return changed() ? intervalStat : prevStat;
+  }
+
+  /**
+   * Reset the all time min max of the metric
+   */
+  public void resetMinMax() {
+    minMax.reset();
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricsRegistry.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricsRegistry.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricsRegistry.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/lib/MetricsRegistry.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,364 @@
+/**
+ * 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.metrics2.lib;
+
+import java.util.LinkedHashMap;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.hadoop.metrics2.MetricsException;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.MetricsTag;
+
+/**
+ * An optional metrics registry class for creating and maintaining a
+ * collection of MetricsMutables, making writing metrics source easier.
+ */
+public class MetricsRegistry {
+
+  /** key for the context tag */
+  public static final String CONTEXT_KEY = "context";
+  /** description for the context tag */
+  public static final String CONTEXT_DESC = "Metrics context";
+
+  private final LinkedHashMap<String, MetricMutable> metricsMap =
+      new LinkedHashMap<String, MetricMutable>();
+  private final LinkedHashMap<String, MetricsTag> tagsMap =
+      new LinkedHashMap<String, MetricsTag>();
+  private final String name;
+  private final MetricMutableFactory mf;
+
+  /**
+   * Construct the registry with a record name
+   * @param name  of the record of the metrics
+   */
+  public MetricsRegistry(String name) {
+    this.name = name;
+    this.mf = new MetricMutableFactory();
+  }
+
+  /**
+   * Construct the registry with a name and a metric factory
+   * @param name  of the record of the metrics
+   * @param factory for creating new mutable metrics
+   */
+  public MetricsRegistry(String name, MetricMutableFactory factory) {
+    this.name = name;
+    this.mf = factory;
+  }
+
+  /**
+   * @return  the name of the metrics registry
+   */
+  public String name() {
+    return name;
+  }
+
+  /**
+   * Get a metric by name
+   * @param name  of the metric
+   * @return  the metric object
+   */
+  public MetricMutable get(String name) {
+    return metricsMap.get(name);
+  }
+
+  /**
+   * Create a mutable integer counter
+   * @param name  of the metric
+   * @param description of the metric
+   * @param initValue of the metric
+   * @return  a new counter object
+   */
+  public MetricMutableCounterInt
+  newCounter(String name, String description, int initValue) {
+    checkMetricName(name);
+    MetricMutableCounterInt ret = mf.newCounter(name, description, initValue);
+    metricsMap.put(name, ret);
+    return ret;
+  }
+
+  /**
+   * Create a mutable long integer counter
+   * @param name  of the metric
+   * @param description of the metric
+   * @param initValue of the metric
+   * @return  a new counter object
+   */
+  public MetricMutableCounterLong
+  newCounter(String name, String description, long initValue) {
+    checkMetricName(name);
+    MetricMutableCounterLong ret = mf.newCounter(name, description, initValue);
+    metricsMap.put(name, ret);
+    return ret;
+  }
+
+  /**
+   * Create a mutable integer gauge
+   * @param name  of the metric
+   * @param description of the metric
+   * @param initValue of the metric
+   * @return  a new gauge object
+   */
+  public MetricMutableGaugeInt
+  newGauge(String name, String description, int initValue) {
+    checkMetricName(name);
+    MetricMutableGaugeInt ret = mf.newGauge(name, description, initValue);
+    metricsMap.put(name, ret);
+    return ret;
+  }
+
+  /**
+   * Create a mutable long integer gauge
+   * @param name  of the metric
+   * @param description of the metric
+   * @param initValue of the metric
+   * @return  a new gauge object
+   */
+  public MetricMutableGaugeLong
+  newGauge(String name, String description, long initValue) {
+    checkMetricName(name);
+    MetricMutableGaugeLong ret = mf.newGauge(name, description, initValue);
+    metricsMap.put(name, ret);
+    return ret;
+  }
+
+  /**
+   * Create a mutable metric with stats
+   * @param name  of the metric
+   * @param description of the metric
+   * @param sampleName  of the metric (e.g., "ops")
+   * @param valueName   of the metric (e.g., "time" or "latency")
+   * @param extended    produce extended stat (stdev, min/max etc.) if true.
+   * @return  a new metric object
+   */
+  public MetricMutableStat newStat(String name, String description,
+                                   String sampleName, String valueName,
+                                   boolean extended) {
+    checkMetricName(name);
+    MetricMutableStat ret =
+        mf.newStat(name, description, sampleName, valueName, extended);
+    metricsMap.put(name, ret);
+    return ret;
+  }
+
+  /**
+   * Create a mutable metric with stats
+   * @param name  of the metric
+   * @param description of the metric
+   * @param sampleName  of the metric (e.g., "ops")
+   * @param valueName   of the metric (e.g., "time" or "latency")
+   * @return  a new metric object
+   */
+  public MetricMutableStat newStat(String name, String description,
+                                   String sampleName, String valueName) {
+    return newStat(name, description, sampleName, valueName, false);
+  }
+
+  /**
+   * Create a mutable metric with stats using the name only
+   * @param name  of the metric
+   * @return a new metric object
+   */
+  public MetricMutableStat newStat(String name) {
+    return newStat(name, "", "ops", "time", false);
+  }
+
+  /**
+   * Increment a metric by name.
+   * @param name  of the metric
+   */
+  public void incr(String name) {
+    incr(name, mf);
+  }
+
+  /**
+   * Increment a metric by name.
+   * @param name  of the metric
+   * @param factory to lazily create the metric if not null
+   */
+  public void incr(String name, MetricMutableFactory factory) {
+    MetricMutable m = metricsMap.get(name);
+
+    if (m != null) {
+      if (m instanceof MetricMutableGauge<?>) {
+        ((MetricMutableGauge<?>) m).incr();
+      }
+      else if (m instanceof MetricMutableCounter<?>) {
+        ((MetricMutableCounter<?>) m).incr();
+      }
+      else {
+        throw new MetricsException("Unsupported incr() for metric "+ name);
+      }
+    }
+    else if (factory != null) {
+      metricsMap.put(name, factory.newMetric(name));
+      incr(name, null);
+    }
+    else {
+      throw new MetricsException("Metric "+ name +" doesn't exist");
+    }
+  }
+
+  /**
+   * Decrement a metric by name.
+   * @param name  of the metric
+   */
+  public void decr(String name) {
+    decr(name, mf);
+  }
+
+  /**
+   * Decrement a metric by name.
+   * @param name  of the metric
+   * @param factory to lazily create the metric if not null
+   */
+  public void decr(String name, MetricMutableFactory factory) {
+    MetricMutable m = metricsMap.get(name);
+
+    if (m != null) {
+      if (m instanceof MetricMutableGauge<?>) {
+        ((MetricMutableGauge<?>) m).decr();
+      }
+      else {
+        throw new MetricsException("Unsupported decr() for metric "+ name);
+      }
+    }
+    else if (factory != null) {
+      metricsMap.put(name, factory.newMetric(name));
+      decr(name, null);
+    }
+    else {
+      throw new MetricsException("Metric "+ name +" doesn't exist");
+    }
+  }
+
+  /**
+   * Add a value to a metric by name.
+   * @param name  of the metric
+   * @param value of the snapshot to add
+   */
+  public void add(String name, long value) {
+    add(name, value, mf);
+  }
+
+  /**
+   * Decrement a metric by name.
+   * @param name  of the metric
+   * @param value of the snapshot to add
+   * @param factory to lazily create the metric if not null
+   */
+  public void add(String name, long value, MetricMutableFactory factory) {
+    MetricMutable m = metricsMap.get(name);
+
+    if (m != null) {
+      if (m instanceof MetricMutableStat) {
+        ((MetricMutableStat) m).add(value);
+      }
+      else {
+        throw new MetricsException("Unsupported add(value) for metric "+ name);
+      }
+    }
+    else if (factory != null) {
+      metricsMap.put(name, factory.newStat(name));
+      add(name, value, null);
+    }
+    else {
+      throw new MetricsException("Metric "+ name +" doesn't exist");
+    }
+  }
+
+  /**
+   * Set the metrics context tag
+   * @param name of the context
+   * @return
+   */
+  public MetricsRegistry setContext(String name) {
+    return tag(CONTEXT_KEY, CONTEXT_DESC, name);
+  }
+
+  /**
+   * Add a tag to the metrics
+   * @param name  of the tag
+   * @param description of the tag
+   * @param value of the tag
+   * @return  the registry (for keep adding tags)
+   */
+  public MetricsRegistry tag(String name, String description, String value) {
+    return tag(name, description, value, false);
+  }
+
+  /**
+   * Add a tag to the metrics
+   * @param name  of the tag
+   * @param description of the tag
+   * @param value of the tag
+   * @param override  existing tag if true
+   * @return  the registry (for keep adding tags)
+   */
+  public MetricsRegistry tag(String name, String description, String value,
+                             boolean override) {
+    if (!override) checkTagName(name);
+    tagsMap.put(name, new MetricsTag(name, description, value));
+    return this;
+  }
+
+  /**
+   * Get the tags
+   * @return  the tags set
+   */
+  public Set<Entry<String, MetricsTag>> tags() {
+    return tagsMap.entrySet();
+  }
+
+  /**
+   * Get the metrics
+   * @return  the metrics set
+   */
+  public Set<Entry<String, MetricMutable>> metrics() {
+    return metricsMap.entrySet();
+  }
+
+  private void checkMetricName(String name) {
+    if (metricsMap.containsKey(name)) {
+      throw new MetricsException("Metric name "+ name +" already exists!");
+    }
+  }
+
+  private void checkTagName(String name) {
+    if (tagsMap.containsKey(name)) {
+      throw new MetricsException("Tag "+ name +" already exists!");
+    }
+  }
+
+  /**
+   * Sample all the mutable metrics and put the snapshot in the builder
+   * @param builder to contain the metrics snapshot
+   * @param all get all the metrics even if the values are not changed.
+   */
+  public void snapshot(MetricsRecordBuilder builder, boolean all) {
+    for (Entry<String, MetricsTag> entry : tags()) {
+      builder.add(entry.getValue());
+    }
+    for (Entry<String, MetricMutable> entry : metrics()) {
+      entry.getValue().snapshot(builder, all);
+    }
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/package.html
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/package.html?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/package.html (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/package.html Fri Mar  4 04:33:55 2011
@@ -0,0 +1,319 @@
+<html>
+
+  <!--
+     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.
+  -->
+
+  <head>
+    <title>org.apache.hadoop.metrics2</title>
+  </head>
+  <body>
+    <h1>Metrics 2.0</h1>
+    <ul id="toc">
+      <li><a href="#overview">Overview</a></li>
+      <li><a href="#gettingstarted">Getting Started</a></li>
+      <li><a href="#filtering">Metrics Filtering</a></li>
+      <li><a href="#instrumentation">Metrics Instrumentation Strategy</a></li>
+      <li><a href="#migration">Migration from previous system</a></li>
+    </ul>
+    <h2><a name="overview">Overview</a></h2>
+    <p>This package provides a framework for metrics instrumentation
+      and publication.
+    </p>
+
+    <p>The instrumentation of metrics just need to implement the simple
+      <code>MetricsSource</code> interface with a single <code>getMetrics</code>
+      method; The consumers of metrics just need to implement the simple
+      <code>MetricsSink</code> interface with a <code>putMetrics</code>
+      method along with the <code>init</code> and <code>flush</code> methods.
+      Producers register the metrics
+      sources with a metrics system, while consumers register the sinks. A
+      default metrics system is provided to marshal metrics from sources to
+      sinks based on (per source/sink) configuration options. Metrics
+      from <code>getMetrics</code> would also be published and queryable via
+      the standard JMX mechanism. This document targets the framework
+      users. Framework developers could consult the
+      <a href="http://wiki.apache.org/hadoop/HADOOP-6728-MetricsV2">design
+      document</a> for architecture and implementation notes.
+    </p>
+    <h3>Sub-packages</h3>
+    <dl>
+      <dt><code>org.apache.hadoop.metrics2.impl</code></dt>
+      <dd>Implementation classes of the framework for interface and/or
+        abstract classes defined in the top-level package. Sink plugin code
+        usually does not need to reference any class here.
+      </dd>
+      <dt> <code>org.apache.hadoop.metrics2.lib</code></dt>
+      <dd>Convenience classes for implementing metrics sources, including the
+        <code>MetricMutable[Gauge*|Counter*|Stat]</code> and
+        <code>MetricsRegistry</code>.
+      </dd>
+      <dt> <code>org.apache.hadoop.metrics2.filter</code></dt>
+      <dd>Builtin metrics filter implementations include the
+        <code>GlobFilter</code> and <code>RegexFilter</code>.
+      </dd>
+      <dt><code>org.apache.hadoop.metrics2.source</code></dt>
+      <dd>Builtin metrics source implementations including the
+        <code>JvmMetricsSource</code>.
+      </dd>
+      <dt> <code>org.apache.hadoop.metrics2.sink</code></dt>
+      <dd>Builtin metrics sink implementations including the
+        <code>FileSink</code>.
+      </dd>
+    </dl>
+
+    <h2><a name="gettingstarted">Getting started</a></h2>
+    <p>Here is a simple MetricsSource:</p>
+    <pre>
+    class MyMetrics implements MetricsSource {
+      public void getMetrics(MetricsBuilder builder, boolean all) {
+        builder.addRecord("myRecord").setContext("myContext")
+               .addGauge("myMetric", "My metrics description", 42);
+      }
+    }</pre>
+    <p>In this example there are three names:</p>
+    <dl>
+      <dt><em>myContext</em></dt>
+      <dd>The optional context name typically identifies either the
+        application, or a group of modules within an application or
+        library.
+      </dd>
+
+      <dt><em>myRecord</em></dt>
+      <dd>The record name generally identifies some entity for which a set of
+        metrics are to be reported.  For example, you could have a record named
+        "cacheStat" for reporting a number of statistics relating to the usage
+        of some cache in your application.</dd>
+
+      <dt><em>myMetric</em></dt>
+      <dd>This identifies a particular metric. In this case the metric is a
+        "gauge" type, which means it can vary in both directions, compared
+        with a "counter" type, which can only increase or stay the same. The 42
+        here is the value of the metric, which can be substituted a variable or
+        a non-blocking method call returning a number.
+      </dd>
+    </dl>
+    <p>Note, the boolean argument <code>all</code>, if true, means that the
+      source should send all the metrics it defines, even if the metrics
+      are unchanged since last <code>getMetrics</code> call. This enable an
+      optimization for less copying for metrics that don't change much
+      (total capacity of something etc. which only changes when new
+      resources (nodes or disks) are being added.)
+    </p>
+    <p>Here is a simple MetricsSink:</p>
+    <pre>
+    public class MySink implements MetricsSink {
+      public void putMetrics(MetricsRecord record) {
+        System.out.print(record);
+      }
+      public void init(SubsetConfiguration conf) {}
+      public void flush() {}
+    }</pre>
+    <p>In this example there are three additional concepts:</p>
+    <dl>
+      <dt><em>record</em></dt>
+      <dd>This object corresponds to the record created in metrics sources
+        e.g., the "myRecord" in previous example.
+      </dd>
+      <dt><em>conf</em></dt>
+      <dd>The configuration object for the sink instance with prefix removed.
+        So you can get any sink specific configuration using the usual
+        get* method.
+      </dd>
+      <dt><em>flush</em></dt>
+      <dd>This method is called for each update cycle, which may involve
+        more than one record. The sink should try to flush any buffered metrics
+        to its backend upon the call. But it's not required that the
+        implementation is synchronous.
+      </dd>
+    </dl>
+    <p>In order to make use our <code>MyMetrics</code> and <code>MySink</code>,
+      they need to be hooked up to a metrics system. In this case (and most
+      cases), the <code>DefaultMetricsSystem</code> would suffice.
+    </p>
+    <pre>
+    DefaultMetricsSystem.initialize("test"); // called once per application
+    DefaultMetricsSystem.INSTANCE.register("MyMetrics", "my metrics description",
+                                           new MyMetrics());</pre>
+    <p>Sinks are usually specified in a configuration file, say,
+    "hadoop-metrics2-test.properties", as:
+    </p>
+    <pre>
+    test.sink.mysink0.class=com.example.hadoop.metrics.MySink</pre>
+    <p>The configuration syntax is:</p>
+    <pre>
+    [prefix].[source|sink|jmx|].[instance].[option]</pre>
+    <p>In the previous example, <code>test</code> is the prefix and
+      <code>mysink0</code> is an instance name.
+      <code>DefaultMetricsSystem</code> would try to load
+      <code>hadoop-metrics2-[prefix].properties</code> first, and if not found,
+      try the default <code>hadoop-metrics2.properties</code> in the class path.
+      Note, the <code>[instance]</code> is an arbitrary name to uniquely
+      identify a particular sink instance. The asterisk (<code>*</code>) can be
+      used to specify default options.
+    </p>
+    <p>Consult the metrics instrumentation in jvm, rpc, hdfs and mapred, etc.
+      for more examples.
+    </p>
+
+    <h2><a name="filtering">Metrics Filtering</a></h2>
+    <p>One of the features of the default metrics system is metrics filtering
+      configuration by source, context, record/tags and metrics. The least
+      expensive way to filter out metrics would be at the source level, e.g.,
+      filtering out source named "MyMetrics". The most expensive way would be
+      per metric filtering.
+    </p>
+    <p>Here are some examples:</p>
+    <pre>
+    test.sink.file0.class=org.apache.hadoop.metrics2.sink.FileSink
+    test.sink.file0.context=foo</pre>
+    <p>In this example, we configured one sink instance that would
+      accept metrics from context <code>foo</code> only.
+    </p>
+    <pre>
+    *.source.filter.class=org.apache.hadoop.metrics.filter.GlobFilter
+    test.*.source.filter.include=foo
+    test.*.source.filter.exclude=bar</pre>
+    <p>In this example, we specify a source filter that includes source
+      <code>foo</code> and excludes <code>bar</code>. When only include
+      patterns are specified, the filter operates in the white listing mode,
+      where only matched sources are included. Likewise, when only exclude
+      patterns are specified, only matched sources are excluded. Sources that
+      are not matched in either patterns are included as well when both patterns
+      are present. Note, the include patterns have precedence over the exclude
+      patterns.
+    </p>
+    <p>Similarly, you can specify the <code>record.filter</code> and
+      <code>metrics.filter</code> options, which operate at record and metric
+      level, respectively. Filters can be combined to optimize
+      the filtering efficiency.</p>
+
+    <h2><a name="instrumentation">Metrics instrumentation strategy</a></h2>
+
+    In previous examples, we showed a minimal example to use the
+    metrics framework. In a larger system (like Hadoop) that allows
+    custom metrics instrumentation, we recommend the following strategy:
+    <pre>
+    class MyMetrics extends MyInstrumentation implements MetricsSource {
+
+      final MetricsRegistry registry = new MetricsRegistry("myRecord");
+      final MetricMutableGaugeInt gauge0 =
+          registry.newGauge("myGauge", "my gauge description", 0);
+      final MetricMutableCounterLong counter0 =
+          registry.newCounter("myCounter", "my metric description", 0L);
+      final MetricMutaleStat stat0 =
+          registry.newStat("myStat", "my stat description", "ops", "time");
+
+      @Override public void setGauge0(int value) { gauge0.set(value); }
+      @Override public void incrCounter0() { counter0.incr(); }
+      @Override public void addStat0(long elapsed) { stat0.add(elapsed); }
+
+      public void getMetrics(MetricsBuilder builder, boolean all) {
+        registry.snapshot(builder.addRecord(registry.name()), all);
+      }
+
+    }
+    </pre>
+
+    Note, in this example we introduced the following:
+    <dl>
+      <dt><em>MyInstrumentation</em></dt>
+      <dd>This is usually an abstract class (or interface) to define an
+        instrumentation interface (incrCounter0 etc.) that allows different
+        implementations. This is a mechanism to allow different metrics
+        systems to be used at runtime via configuration.
+      </dd>
+      <dt><em>MetricsRegistry</em></dt>
+      <dd>This is a library class for easy creation and manipulation of
+        mutable metrics.
+      </dd>
+      <dt><em>MetricMutable[Gauge*|Counter*|Stat]</em></dt>
+      <dd>These are library classes to manage mutable metrics for
+        implementations of metrics sources. They produce immutable gauge and
+        counters (Metric[Gauge*|Counter*]) for downstream consumption (sinks)
+        upon <code>snapshot</code>. The <code>MetricMutableStat</code>
+        in particular, provides a way to measure latency and throughput of an
+        operation. In this particular case, it produces a long counter
+        "myStat_num_ops" and double gauge "myStat_avg_time" when snapshotted.
+      </dd>
+    </dl>
+
+    <h2><a name="migration">Migration from previous system</a></h2>
+    <p>Users of the previous metrics system would notice the lack of
+      <code>context</code> prefix in the configuration examples. The new
+      metrics system decouples the concept for context (for grouping) with the
+      implementation where a particular context object does the updating and
+      publishing of metrics, which causes problems when you want to have a
+      single context to be consumed by multiple backends. You would also have to
+      configure an implementation instance per context, even if you have a
+      backend that can handle multiple contexts (file, gangalia etc.):
+    </p>
+    <table width="99%" border="1" cellspacing="0" cellpadding="4">
+      <tbody>
+        <tr>
+          <th width="40%">Before</th><th>After</th>
+        </tr>
+        <tr>
+          <td><pre>
+    context1.class=org.hadoop.metrics.file.FileContext
+    context2.class=org.hadoop.metrics.file.FileContext
+    ...
+    contextn.class=org.hadoop.metrics.file.FileContext</pre>
+          </td>
+          <td><pre>
+    myprefix.sink.file.class=org.hadoop.metrics2.sink.FileSink</pre>
+          </td>
+        </tr>
+      </tbody>
+    </table>
+    <p>In the new metrics system, you can simulate the previous behavior by
+      using the context option in the sink options like the following:
+    </p>
+    <table width="99%" border="1" cellspacing="0" cellpadding="4">
+      <tbody>
+        <tr>
+          <th width="40%">Before</th><th>After</th>
+        </tr>
+        <tr>
+          <td><pre>
+    context0.class=org.hadoop.metrics.file.FileContext
+    context0.fileName=context0.out
+    context1.class=org.hadoop.metrics.file.FileContext
+    context1.fileName=context1.out
+    ...
+    contextn.class=org.hadoop.metrics.file.FileContext
+    contextn.fileName=contextn.out</pre>
+          </td>
+          <td><pre>
+    myprefix.sink.*.class=org.apache.hadoop.metrics2.sink.FileSink
+    myprefix.sink.file0.context=context0
+    myprefix.sink.file0.filename=context1.out
+    myprefix.sink.file1.context=context1
+    myprefix.sink.file1.filename=context1.out
+    ...
+    myprefix.sink.filen.context=contextn
+    myprefix.sink.filen.filename=contextn.out</pre>
+          </td>
+        </tr>
+      </tbody>
+    </table>
+    <p>to send metrics of a particular context to a particular backend. Note,
+      <code>myprefix</code> is an arbitrary prefix for configuration groupings,
+      typically they are the name of a particular process
+      (<code>namenode</code>, <code>jobtracker</code>, etc.)
+    </p>
+  </body>
+</html>

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/sink/FileSink.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/sink/FileSink.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/sink/FileSink.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/sink/FileSink.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,83 @@
+/**
+ * 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.metrics2.sink;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.PrintWriter;
+import org.apache.commons.configuration.SubsetConfiguration;
+import org.apache.hadoop.metrics2.Metric;
+import org.apache.hadoop.metrics2.MetricsException;
+import org.apache.hadoop.metrics2.MetricsRecord;
+import org.apache.hadoop.metrics2.MetricsSink;
+import org.apache.hadoop.metrics2.MetricsTag;
+
+/**
+ * 
+ */
+public class FileSink implements MetricsSink {
+
+  private static final String FILENAME_KEY = "filename";
+  private PrintWriter writer;
+
+  @Override
+  public void init(SubsetConfiguration conf) {
+    String filename = conf.getString(FILENAME_KEY);
+    try {
+      writer = filename == null
+          ? new PrintWriter(new BufferedOutputStream(System.out))
+          : new PrintWriter(new FileWriter(new File(filename), true));
+    }
+    catch (Exception e) {
+      throw new MetricsException("Error creating "+ filename, e);
+    }
+  }
+
+  @Override
+  public void putMetrics(MetricsRecord record) {
+    writer.print(record.timestamp());
+    writer.print(" ");
+    writer.print(record.context());
+    writer.print(".");
+    writer.print(record.name());
+    String separator = ": ";
+    for (MetricsTag tag : record.tags()) {
+      writer.print(separator);
+      separator = ", ";
+      writer.print(tag.name());
+      writer.print("=");
+      writer.print(String.valueOf(tag.value()));
+    }
+    for (Metric metric : record.metrics()) {
+      writer.print(separator);
+      separator = ", ";
+      writer.print(metric.name());
+      writer.print("=");
+      writer.print(metric.value());
+    }
+    writer.println();
+  }
+
+  @Override
+  public void flush() {
+    writer.flush();
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/source/JvmMetricsSource.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/source/JvmMetricsSource.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/source/JvmMetricsSource.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/source/JvmMetricsSource.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,191 @@
+/**
+ * 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.metrics2.source;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
+import java.lang.management.MemoryUsage;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+import static java.lang.Thread.State.*;
+import java.lang.management.GarbageCollectorMXBean;
+import java.util.List;
+import org.apache.hadoop.metrics2.MetricsBuilder;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.MetricsSource;
+import org.apache.hadoop.metrics2.MetricsSystem;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+
+import org.apache.hadoop.log.EventCounter;
+
+/**
+ *
+ */
+public class JvmMetricsSource implements MetricsSource {
+
+  private static final float M = 1024*1024;
+
+  static final String SOURCE_NAME = "jvm";
+  static final String CONTEXT = "jvm";
+  static final String RECORD_NAME = "metrics";
+  static final String SOURCE_DESC = "JVM metrics etc.";
+
+  // tags
+  static final String PROCESSNAME_KEY = "processName";
+  static final String PROCESSNAME_DESC = "Process name";
+  static final String SESSIONID_KEY = "sessionId";
+  static final String SESSIONID_DESC = "Session ID";
+  private final String processName, sessionId;
+
+  // metrics
+  static final String NONHEAP_USED_KEY = "memNonHeapUsedM";
+  static final String NONHEAP_USED_DESC = "Non-heap memory used in MB";
+  static final String NONHEAP_COMMITTED_KEY = "memNonHeapCommittedM";
+  static final String NONHEAP_COMMITTED_DESC = "Non-heap committed in MB";
+  static final String HEAP_USED_KEY = "memHeapUsedM";
+  static final String HEAP_USED_DESC = "Heap memory used in MB";
+  static final String HEAP_COMMITTED_KEY = "memHeapCommittedM";
+  static final String HEAP_COMMITTED_DESC = "Heap memory committed in MB";
+  static final String GC_COUNT_KEY = "gcCount";
+  static final String GC_COUNT_DESC = "Total GC count";
+  static final String GC_TIME_KEY = "gcTimeMillis";
+  static final String GC_TIME_DESC = "Total GC time in milliseconds";
+  static final String THREADS_NEW_KEY = "threadsNew";
+  static final String THREADS_NEW_DESC = "Number of new threads";
+  static final String THREADS_RUNNABLE_KEY = "threadsRunnable";
+  static final String THREADS_RUNNABLE_DESC = "Number of runnable threads";
+  static final String THREADS_BLOCKED_KEY = "threadsBlocked";
+  static final String THREADS_BLOCKED_DESC = "Number of blocked threads";
+  static final String THREADS_WAITING_KEY = "threadsWaiting";
+  static final String THREADS_WAITING_DESC = "Number of waiting threads";
+  static final String THREADS_TIMEDWAITING_KEY = "threadsTimedWaiting";
+  static final String THREADS_TIMEDWAITING_DESC =
+      "Number of timed waiting threads";
+  static final String THREADS_TERMINATED_KEY = "threadsTerminated";
+  static final String THREADS_TERMINATED_DESC = "Number of terminated threads";
+  static final String LOG_FATAL_KEY = "logFatal";
+  static final String LOG_FATAL_DESC = "Total number of fatal log events";
+  static final String LOG_ERROR_KEY = "logError";
+  static final String LOG_ERROR_DESC = "Total number of error log events";
+  static final String LOG_WARN_KEY = "logWarn";
+  static final String LOG_WARN_DESC = "Total number of warning log events";
+  static final String LOG_INFO_KEY = "logInfo";
+  static final String LOG_INFO_DESC = "Total number of info log events";
+
+  private final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
+  private final List<GarbageCollectorMXBean> gcBeans =
+      ManagementFactory.getGarbageCollectorMXBeans();
+  private final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
+
+  JvmMetricsSource(String processName, String sessionId) {
+    this.processName = processName;
+    this.sessionId = sessionId;
+  }
+
+  public static JvmMetricsSource create(String processName, String sessionId,
+                                        MetricsSystem ms) {
+    return ms.register(SOURCE_NAME, SOURCE_DESC,
+                       new JvmMetricsSource(processName, sessionId));
+  }
+
+  public static JvmMetricsSource create(String processName, String sessionId) {
+    return create(processName, sessionId, DefaultMetricsSystem.INSTANCE);
+  }
+
+  @Override
+  public void getMetrics(MetricsBuilder builder, boolean all) {
+    MetricsRecordBuilder rb = builder.addRecord(RECORD_NAME)
+        .setContext(CONTEXT)
+        .tag(PROCESSNAME_KEY, PROCESSNAME_DESC, processName)
+        .tag(SESSIONID_KEY, SESSIONID_DESC, sessionId);
+    getMemoryUsage(rb);
+    getGcUsage(rb);
+    getThreadUsage(rb);
+    getEventCounters(rb);
+  }
+
+  private void getMemoryUsage(MetricsRecordBuilder rb) {
+    MemoryUsage memNonHeap = memoryMXBean.getNonHeapMemoryUsage();
+    MemoryUsage memHeap = memoryMXBean.getHeapMemoryUsage();
+    rb.addGauge(NONHEAP_USED_KEY, NONHEAP_USED_DESC, memNonHeap.getUsed() / M)
+      .addGauge(NONHEAP_COMMITTED_KEY, NONHEAP_COMMITTED_DESC,
+                memNonHeap.getCommitted() / M)
+      .addGauge(HEAP_USED_KEY, HEAP_USED_DESC, memHeap.getUsed() / M)
+      .addGauge(HEAP_COMMITTED_KEY, HEAP_COMMITTED_DESC,
+                memHeap.getCommitted() / M);
+  }
+
+  private void getGcUsage(MetricsRecordBuilder rb) {
+    long count = 0;
+    long timeMillis = 0;
+    for (GarbageCollectorMXBean gcBean : gcBeans) {
+      count += gcBean.getCollectionCount();
+      timeMillis += gcBean.getCollectionTime();
+    }
+    rb.addCounter(GC_COUNT_KEY, GC_COUNT_DESC, count)
+      .addCounter(GC_TIME_KEY, GC_TIME_DESC, timeMillis);
+  }
+
+  private void getThreadUsage(MetricsRecordBuilder rb) {
+    long threadIds[] = threadMXBean.getAllThreadIds();
+    ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds, 0);
+    int threadsNew = 0;
+    int threadsRunnable = 0;
+    int threadsBlocked = 0;
+    int threadsWaiting = 0;
+    int threadsTimedWaiting = 0;
+    int threadsTerminated = 0;
+
+    for (ThreadInfo threadInfo : threadInfos) {
+      // threadInfo is null if the thread is not alive or doesn't exist
+      if (threadInfo == null) {
+        continue;
+      }
+      Thread.State state = threadInfo.getThreadState();
+      if (state == NEW) {
+        threadsNew++;
+      } else if (state == RUNNABLE) {
+        threadsRunnable++;
+      } else if (state == BLOCKED) {
+        threadsBlocked++;
+      } else if (state == WAITING) {
+        threadsWaiting++;
+      } else if (state == TIMED_WAITING) {
+        threadsTimedWaiting++;
+      } else if (state == TERMINATED) {
+        threadsTerminated++;
+      }
+    }
+    rb.addGauge(THREADS_NEW_KEY, THREADS_NEW_DESC, threadsNew)
+      .addGauge(THREADS_RUNNABLE_KEY, THREADS_RUNNABLE_DESC, threadsRunnable)
+      .addGauge(THREADS_BLOCKED_KEY, THREADS_BLOCKED_DESC, threadsBlocked)
+      .addGauge(THREADS_WAITING_KEY, THREADS_WAITING_DESC, threadsWaiting)
+      .addGauge(THREADS_TIMEDWAITING_KEY, THREADS_TIMEDWAITING_DESC,
+                threadsTimedWaiting)
+      .addGauge(THREADS_TERMINATED_KEY, THREADS_TERMINATED_DESC,
+                threadsTerminated);
+  }
+
+  private void getEventCounters(MetricsRecordBuilder rb) {
+    rb.addCounter(LOG_FATAL_KEY, LOG_FATAL_DESC, EventCounter.getFatal())
+      .addCounter(LOG_ERROR_KEY, LOG_ERROR_DESC, EventCounter.getError())
+      .addCounter(LOG_WARN_KEY, LOG_WARN_DESC, EventCounter.getWarn())
+      .addCounter(LOG_INFO_KEY, LOG_INFO_DESC, EventCounter.getInfo());
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/Contracts.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/Contracts.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/Contracts.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/Contracts.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,127 @@
+/**
+ * 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.metrics2.util;
+
+/**
+ * Utilities for programming by contract (preconditions, postconditions etc.)
+ */
+public class Contracts {
+
+  private Contracts() {}
+
+  /**
+   * Check that a reference is not null.
+   * @param <T> type of the reference
+   * @param ref the reference to check
+   * @param msg the error message
+   * @throws NullPointerException if {@code ref} is null
+   * @return the checked reference for convenience
+   */
+  public static <T> T checkNotNull(T ref, Object msg) {
+    if (ref == null) {
+      throw new NullPointerException(String.valueOf(msg) +": "+
+                                     ref.getClass().getName());
+    }
+    return ref;
+  }
+
+  /**
+   * Check the state expression for false conditions
+   * @param expression  the boolean expression to check
+   * @param msg the error message if {@code expression} is false
+   * @throws IllegalStateException if {@code expression} is false
+   */
+  public static void checkState(boolean expression, Object msg) {
+    if (!expression) {
+      throw new IllegalStateException(String.valueOf(msg));
+    }
+  }
+
+  /**
+   * Check an argument for false conditions
+   * @param <T> type of the argument
+   * @param arg the argument to check
+   * @param expression  the boolean expression for the condition
+   * @param msg the error message if {@code expression} is false
+   * @return the argument for convenience
+   */
+  public static <T> T checkArg(T arg, boolean expression, Object msg) {
+    if (!expression) {
+      throw new IllegalArgumentException(String.valueOf(msg) +": "+ arg);
+    }
+    return arg;
+  }
+
+  /**
+   * Check an argument for false conditions
+   * @param arg the argument to check
+   * @param expression  the boolean expression for the condition
+   * @param msg the error message if {@code expression} is false
+   * @return the argument for convenience
+   */
+  public static int checkArg(int arg, boolean expression, Object msg) {
+    if (!expression) {
+      throw new IllegalArgumentException(String.valueOf(msg)+ ": "+arg);
+    }
+    return arg;
+  }
+
+  /**
+   * Check an argument for false conditions
+   * @param arg the argument to check
+   * @param expression  the boolean expression for the condition
+   * @param msg the error message if {@code expression} is false
+   * @return the argument for convenience
+   */
+  public static long checkArg(long arg, boolean expression, Object msg) {
+    if (!expression) {
+      throw new IllegalArgumentException(String.valueOf(msg));
+    }
+    return arg;
+  }
+
+  /**
+   * Check an argument for false conditions
+   * @param arg the argument to check
+   * @param expression  the boolean expression for the condition
+   * @param msg the error message if {@code expression} is false
+   * @return the argument for convenience
+   */
+  public static float checkArg(float arg, boolean expression, Object msg) {
+    if (!expression) {
+      throw new IllegalArgumentException(String.valueOf(msg) +": "+ arg);
+    }
+    return arg;
+  }
+
+  /**
+   * Check an argument for false conditions
+   * @param arg the argument to check
+   * @param expression  the boolean expression for the condition
+   * @param msg the error message if {@code expression} is false
+   * @return the argument for convenience
+   */
+  public static double checkArg(double arg, boolean expression, Object msg) {
+    if (!expression) {
+      throw new IllegalArgumentException(String.valueOf(msg) +": "+ arg);
+    }
+    return arg;
+  }
+
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/MBeans.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/MBeans.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/MBeans.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/MBeans.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,89 @@
+/**
+ * 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.metrics2.util;
+
+import java.lang.management.ManagementFactory;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.InstanceAlreadyExistsException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * This util class provides a method to register an MBean using
+ * our standard naming convention as described in the doc
+ *  for {link {@link #register(String, String, Object)}
+ *
+ */
+public class MBeans {
+
+  private static final Log LOG = LogFactory.getLog(MBeans.class);
+
+  /**
+   * Register the MBean using our standard MBeanName format
+   * "hadoop:service=<serviceName>,name=<nameName>"
+   * Where the <serviceName> and <nameName> are the supplied parameters
+   *
+   * @param serviceName
+   * @param nameName
+   * @param theMbean - the MBean to register
+   * @return the named used to register the MBean
+   */
+  static public ObjectName register(String serviceName, String nameName,
+                                    Object theMbean) {
+    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+    ObjectName name = getMBeanName(serviceName, nameName);
+    try {
+      mbs.registerMBean(theMbean, name);
+      return name;
+    } catch (InstanceAlreadyExistsException ie) {
+      LOG.warn(name, ie);
+    } catch (Exception e) {
+      LOG.warn("Error registering "+ name, e);
+    }
+    return null;
+  }
+
+  static public void unregister(ObjectName mbeanName) {
+    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+    if (mbeanName == null)
+        return;
+    try {
+      mbs.unregisterMBean(mbeanName);
+    } catch (InstanceNotFoundException e ) {
+      LOG.warn(mbeanName, e);
+    } catch (Exception e) {
+      LOG.warn("Error unregistering "+ mbeanName, e);
+    }
+  }
+
+  static private ObjectName getMBeanName(String serviceName, String nameName) {
+    ObjectName name = null;
+    String nameStr = "Hadoop:service="+ serviceName +",name="+ nameName;
+    try {
+      name = new ObjectName(nameStr);
+    } catch (MalformedObjectNameException e) {
+      LOG.warn("Error creating MBean object name: "+ nameStr, e);
+    }
+    return name;
+  }
+}

Added: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/SampleStat.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/SampleStat.java?rev=1077597&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/SampleStat.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/metrics2/util/SampleStat.java Fri Mar  4 04:33:55 2011
@@ -0,0 +1,167 @@
+/**
+ * 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.metrics2.util;
+
+/**
+ * Helper to compute running sample stats
+ */
+public class SampleStat {
+
+  private final MinMax minmax = new MinMax();
+  private long numSamples = 0;
+  private double a0, a1, s0, s1;
+
+  /**
+   * Construct a new running sample stat
+   */
+  public SampleStat() {
+    a0 = s0 = 0.0;
+  }
+
+  public void reset() {
+    numSamples = 0;
+    a0 = s0 = 0.0;
+    minmax.reset();
+  }
+
+  // We want to reuse the object, sometimes.
+  void reset(long numSamples, double a0, double a1, double s0, double s1,
+             MinMax minmax) {
+    this.numSamples = numSamples;
+    this.a0 = a0;
+    this.a1 = a1;
+    this.s0 = s0;
+    this.s1 = s1;
+    this.minmax.reset(minmax);
+  }
+
+  /**
+   * Copy the values to other (saves object creation and gc.)
+   * @param other the destination to hold our values
+   */
+  public void copyTo(SampleStat other) {
+    other.reset(numSamples, a0, a1, s0, s1, minmax);
+  }
+
+  /**
+   * Add a sample the running stat.
+   * @param x the sample number
+   * @return  self
+   */
+  public SampleStat add(double x) {
+    minmax.add(x);
+    return add(1, x);
+  }
+
+  /**
+   * Add some sample and a partial sum to the running stat.
+   * Note, min/max is not evaluated using this method.
+   * @param nSamples  number of samples
+   * @param x the partial sum
+   * @return  self
+   */
+  public SampleStat add(long nSamples, double x) {
+    numSamples += nSamples;
+
+    if (numSamples == 1) {
+      a0 = a1 = x;
+      s0 = 0.0;
+    }
+    else {
+      // The Welford method for numerical stability
+      a1 = a0 + (x - a0) / numSamples;
+      s1 = s0 + (x - a0) * (x - a1);
+      a0 = a1;
+      s0 = s1;
+    }
+    return this;
+  }
+
+  /**
+   * @return  the total number of samples
+   */
+  public long numSamples() {
+    return numSamples;
+  }
+
+  /**
+   * @return  the arithmetic mean of the samples
+   */
+  public double mean() {
+    return numSamples > 0 ? a1 : 0.0;
+  }
+
+  /**
+   * @return  the variance of the samples
+   */
+  public double variance() {
+    return numSamples > 1 ? s1 / (numSamples - 1) : 0.0;
+  }
+
+  /**
+   * @return  the standard deviation of the samples
+   */
+  public double stddev() {
+    return Math.sqrt(variance());
+  }
+
+  /**
+   * @return  the minimum value of the samples
+   */
+  public double min() {
+    return minmax.min();
+  }
+
+  /**
+   * @return  the maximum value of the samples
+   */
+  public double max() {
+    return minmax.max();
+  }
+
+  /**
+   * Helper to keep running min/max
+   */
+  @SuppressWarnings("PublicInnerClass")
+  public static class MinMax {
+
+    private double min = Double.MAX_VALUE;
+    private double max = Double.MIN_VALUE;
+
+    public void add(double value) {
+      if (value > max) max = value;
+      if (value < min) min = value;
+    }
+
+    public double min() { return min; }
+    public double max() { return max; }
+
+    public void reset() {
+      min = Double.MAX_VALUE;
+      max = Double.MIN_VALUE;
+    }
+
+    public void reset(MinMax other) {
+      min = other.min();
+      max = other.max();
+    }
+
+  }
+
+}



Mime
View raw message