This page keeps the design notes for HADOOP-6728 only. Current dev/user documentation for metrics system should be kept elsewhere (say, package.html and/or package-info.java in respective packages).

Scope

While we realize that there are many optimization opportunities, the main goal is to fix existing issues and lay down the foundation for future improvements.

Design Overview

In the v2 framework, metrics sources are where the metrics are generated/updated, and metrics sinks consume the records generated by the metrics sources. A metrics system would poll the metrics sources periodically and pass the metrics records to metrics sinks (Figure 1). The source getMetrics interface allows lockless implementations of metrics instrumentation (with volatile metrics values). The sink interface is simple, where the putMetrics method would be called with an immutable metrics record (Figure 2), so that plugin implementers don't have to worry about thread safety.

Metrics system overview

Figure 1: Metrics system overview

Figure 1 is a UML sketch class diagram illustrating the involved passive objects (different colors indicating different kinds of driving threads in discussion): MetricsSource (in cyan) is driven by a timer thread (for getMetrics()) and MetricSink (in green) is driven by a thread for each Sink. The MetricsFilter objects (in orange) can be used either to filter the metrics from sources in the timer thread or to filter metrics per sink in its respective thread. The metrics system expects that the getMetrics call would return fairly quickly (i.e., latency smaller than the polling period). The MetricsSinkQueue is a nonblocking queue with preconfigured size (tolerance of sink latency: n * period). New metrics records would be lost if the queue is full. The JMX MBean interface would be implemented to allow existing JMX clients (JConsole, jManage etc.) to stop and start the metrics system at run time.

Immutable metrics objects

Figure 2: Immutable metrics objects

As an example, a JobTracker metrics instrumentation would implement the MetricsSource interface and override the getMetrics method to return a snapshot of metrics when the timer thread polls it with a MetricsBuilder. The timer thread would then enqueue the resulting metrics records from the MetricsBuilder to each MetricsSinkQueue. The thread for each sink would blocked/wait on each MetricsSinkQueue until it's notified by the timer thread after new metrics records are enqueued and then proceeds to dequeue and call the putMetrics method of the corresponding sink object. Figure 3 and 4 illustrate the new data flow vs the old data flow.

Before

After

Figure 3: V1 data flow diagram

Figure 4: V2 data flow diagram

The following new packages are proposed for a smooth transition of the metrics framework:

package

Contents

Notes

org.apache.hadoop.metrics2

Public interface/abstract classes of the metrics system

org.apache.hadoop.metrics2.annotation

Public annotations for implementing simple metrics sources

org.apache.hadoop.metrics2.lib

Public reusable components for using the metrics system

org.apache.hadoop.metrics2.filter

Public (class names) builtin metrics filter (Glob/Regex) classes

org.apache.hadoop.metrics2.source

Public (class names) builtin metrics source (JVM etc.) classes

org.apache.hadoop.metrics2.sink

Public (class names) builtin metrics sink (file, ganglia etc.) classes

org.apache.hadoop.metrics2.util

Public utility classes for developing metrics system (including plugins)

org.apache.hadoop.metrics2.impl

Metrics system internal implementation classes

Metrics Filtering

The framework supports 3 levels of filters: source, record and metrics names, thus 6 ways to filter metrics with increasing cost (in terms of memory/CPU):

  1. Global source name filtering: any sources with matching names are skipped for getMetrics calls.
  2. Per sink source name filtering: any sources with matching names are skipped for putMetrics calls.
  3. Per source record filtering: any records with matching names or tag values are skipped in the MetricsBuilder.add* calls in the getMetrics calls.

  4. Per sink record filtering: any records with matching names or tag values are skipped for the putMetrics calls.
  5. Per source metrics filtering: any metrics with matching names are skipped in the Metric.sample* calls in the getMetrics calls.
  6. Per sink metrics filtering: any metrics with matching names are skipped in the iteration of the MetricsRecord in putMetrics calls.

These can be mixed and matched to optimize for lower total filtering cost if necessary. See below for configuration examples.

Configuration

The new framework uses the PropertiesConfiguration from the apache commons configuration library for backward compatibility (java properties) and more features (include, variable substitution, subset etc.)

Proposed configuration examples:

# Basic syntax: <prefix>.(source|sink).<instance>.<option>
*.sink.file.class=org.apache.hadoop.metrics2.sink.FileSink
*.source.filter.class=org.apache.hadoop.metrics2.filter.GlobFilter
*.record.filter.class=${*.source.filter.class}
*.metric.filter.class=${*.source.filter.class}
*.period=10

# Filter out any sources with names end with Details
jobtracker.*.source.filter.exclude=*Details

# Filter out records with names that matches foo* in the source named "rpc"
jobtracker.source.rpc.record.filter.exclude=foo*

# Filter out metrics with names that matches foo* for sink instance "file" only
jobtracker.sink.file.metric.filter.exclude=foo*
jobtracker.sink.file.filename=jt-metrics.out

# Custom sink plugin
jobtracker.sink.my.class=com.example.hadoop.metrics.my.MyPlugin
# MyPlugin only handles metrics in "foo" context
jobtracker.sink.my.context=foo

Metrics Source (Instrumentation) Development

A minimal metrics source:

   1 // default record name is the class name
   2 // default context name is "default"
   3 @Metrics(context="bar")
   4 public class MyPojo {
   5   // Default name of metric is method name sans get
   6   // Default type of metric is gauge
   7   @Metric("An integer gauge named MyMetric")
   8   public int getMyMetric() { return 42; }
   9 
  10   // Recommended helper method
  11   public MyPojo registerWith(MetricsSystem ms) {
  12     return ms.register("MyPojo", "MyPojo metrics", this);
  13   }
  14 }

By using annotations, one can add simple metrics to any methods returning supported types (int, long, float and double and their object counter parts) in any java classes.

An example using the mutable metric library objects:

   1 @Metrics{context="bar")
   2 public class MyMetrics {
   3   // Default metric name is the variable name
   4   @Metric("An integer gauge") MutableGaugeInt g1;
   5   // Default type is inferred from the mutable metric type
   6   @Metric("An long integer counter") MutableCounterLong c1;
   7 
   8   // Recommended helper method
   9   public MyMetrics registerWith(MetricsSystem ms) {
  10     return ms.register("MyMetrics", "MyMetrics description", this);
  11   }
  12 }

Mutable metrics for instrumentation

Figure 5: Mutable metrics helper objects for instrumentation

Initialize the metrics system:

   1 // Somewhere in your app's startup code, initialize the metric system.
   2 DefaultMetricsSystem.initialize("JobTracker");
   3 
   4 // Create the metrics source object
   5 MyMetrics myMetrics = new MyMetrics().registerWith(DefaultMetricsSystem.instance());
   6 
   7 // Update the metrics
   8 myMetrics.foo.set(someValue);
   9 myMetrics.bar.incr();

Note, for simple metrics sources, using annotations make the code declarative and concise. For more advanced metrics source implementations, one might need to explicitly implement the MetricsSource interface and override the getMetrics method and use the metrics builder API (Figure 6.):

   1 class MyMetricsSource implements MetricsSource {
   2 
   3   @Override
   4   public void getMetrics(MetricsBuilder builder) {
   5     builder.addRecord("foo")
   6       .addGauge("g0", "an integer gauge", 42)
   7       .addCounter("c0", "a long counter", 42L);
   8 
   9     // Typical metrics sources generate one record per snapshot.
  10     // We can add more records, which is not supported by annotations.
  11     builder.addRecord("bar")
  12       .addGauge("g1", "a float gauge", 42.0)
  13       .addCounter("c1", "a integer counter", 42);
  14   }
  15 
  16   public MyMetricSource registerWith(MetricsSystem ms) {
  17     return ms.register("MyMetrics", "MyMetrics description", this);
  18   }
  19 }

Metrics builders

Figure 6: Metrics builders

Metrics Sink (Plugin) Developement

Implementing a sink plugin with schema conversion (without a forest of if/switches):

   1 public class EchoPlugin implements MetricsSink, MetricsVisitor {
   2 
   3   @Override // MetricsPlugin
   4   public void init(SubsetConfiguration conf) {
   5     // do plugin specific initialization here
   6   }
   7 
   8   @Override // MetricsSink
   9   public void putMetrics(MetricsRecord rec) {
  10     echoHeader(rec.name(), rec.context());
  11 
  12     for (MetricTag tag : rec.tags())
  13       echoTag(tag.getName(), tag.getValue());
  14   
  15     for (AbstractMetric metric : rec.metrics())
  16       metric.visit(this);
  17   }
  18 
  19   @Override // MetricsSink
  20   public void flush() {
  21     // do sink specific buffer management here
  22   }
  23 
  24   @Override // MetricsVisitor
  25   public void counter(MetricInfo info, int value) {
  26     echoCounterInt32(info.name(), value);
  27   }
  28 
  29   @Override // MetricsVisitor
  30   public void counter(MetricInfo info, long value) {
  31     echoCounterInt64(info.name(), value);
  32   }
  33 
  34   @Override // MetricsVisitor
  35   public void gauge(MetricInfo info, int value) {
  36     echoGaugeInt32(info.name(), value);
  37   }
  38 
  39   @Override // MetricsVisitor
  40   public void gauge(MetricInfo info, long value) {
  41     echoGaugeInt64(info.name(), value);
  42   }
  43 
  44   @Override // MetricsVisitor
  45   public void gauge(MetricInfo info, float value) {
  46     echoGaugeFp32(info.name(), value);
  47   }
  48 
  49   @Override // MetricsVisitor
  50   public void gauge(MetricInfo info, double value) {
  51     echoGaugeFp64(info.name(), value);
  52   }
  53 }

HADOOP-6728-MetricsV2 (last edited 2011-05-12 18:14:09 by LukeLu)