This project has retired. For details please refer to its Attic page.
SuperstepMetricsRegistry xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.metrics;
20  
21  import java.io.PrintStream;
22  
23  import org.apache.giraph.bsp.BspService;
24  import org.apache.giraph.conf.GiraphConfiguration;
25  
26  import com.yammer.metrics.core.Histogram;
27  import com.yammer.metrics.core.Metric;
28  import com.yammer.metrics.core.MetricName;
29  import com.yammer.metrics.core.MetricPredicate;
30  import com.yammer.metrics.core.MetricsRegistry;
31  import com.yammer.metrics.reporting.ConsoleReporter;
32  import com.yammer.metrics.reporting.JmxReporter;
33  
34  /**
35   * Wrapper around MetricsRegistry for per-superstep metrics.
36   */
37  public class SuperstepMetricsRegistry extends GiraphMetricsRegistry {
38    /** Number of superstep to use for group of metrics created */
39    private long superstep = BspService.INPUT_SUPERSTEP;
40  
41    /**
42     * Constructor
43     * @param registry {@link com.yammer.metrics.core.MetricsRegistry} to use
44     * @param reporter {@link com.yammer.metrics.reporting.JmxReporter} to use
45     * @param groupName String grouping for metrics
46     * @param type String type name for metrics
47     */
48    protected SuperstepMetricsRegistry(MetricsRegistry registry,
49        JmxReporter reporter, String groupName, String type) {
50      super(registry, reporter, groupName, type);
51    }
52  
53    /**
54     * Create with Hadoop Configuration and superstep number.
55     *
56     * @param conf Hadoop Configuration to use.
57     * @param superstep number of superstep to use as group for metrics.
58     * @return new metrics registry
59     */
60    public static SuperstepMetricsRegistry create(GiraphConfiguration conf,
61        long superstep) {
62      if (conf.metricsEnabled()) {
63        MetricsRegistry registry = new MetricsRegistry();
64        SuperstepMetricsRegistry superstepMetrics = new SuperstepMetricsRegistry(
65            registry, new JmxReporter(registry),
66            "giraph.superstep", String.valueOf(superstep));
67        superstepMetrics.superstep = superstep;
68        return superstepMetrics;
69      } else {
70        return createFake();
71      }
72    }
73  
74    /**
75     * Create an empty registry
76     * @return fake metrics registry that returns no op metrics
77     */
78    public static SuperstepMetricsRegistry createFake() {
79      return new SuperstepMetricsRegistry(new NoOpMetricsRegistry(), null,
80          "", "");
81    }
82  
83    /**
84     * Get superstep stored here
85     * @return long superstep
86     */
87    public long getSuperstep() {
88      return superstep;
89    }
90  
91    /**
92     * Set superstep number used. Internally sets the group for metrics created.
93     *
94     * @param superstep long number of superstep to use.
95     */
96    public void setSuperstep(long superstep) {
97      super.setType(String.valueOf(superstep));
98      this.superstep = superstep;
99    }
100 
101   /**
102    * Print human readable summary of superstep metrics.
103    *
104    * @param out PrintStream to write to.
105    */
106   public void printSummary(PrintStream out) {
107     new WorkerSuperstepMetrics().readFromRegistry().print(superstep, out);
108     out.println("");
109     MetricPredicate superstepFilter = new MetricPredicate() {
110       @Override
111       public boolean matches(MetricName name, Metric metric) {
112         return name.getType().equals(getType());
113       }
114     };
115     new ConsoleReporter(getInternalRegistry(), out, superstepFilter) {
116       @Override
117       public void processHistogram(MetricName name, Histogram histogram,
118           PrintStream stream) {
119         stream.printf("               sum = %,2.2f%n", histogram.sum());
120         super.processHistogram(name, histogram, stream);
121         stream.printf("             count = %d%n", histogram.count());
122       }
123     } .run();
124   }
125 }