This project has retired. For details please refer to its Attic page.
GiraphMetrics 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  package org.apache.giraph.metrics;
19  
20  import org.apache.giraph.conf.GiraphConfiguration;
21  import org.apache.giraph.utils.GcTracker;
22  
23  import com.google.common.collect.Lists;
24  
25  import java.io.PrintStream;
26  import java.util.List;
27  
28  import static org.apache.giraph.bsp.BspService.INPUT_SUPERSTEP;
29  
30  /**
31   * Top level metrics class for using Yammer's metrics in Giraph.
32   */
33  public class GiraphMetrics {
34    /** Singleton instance for everyone to use */
35    private static GiraphMetrics INSTANCE = new GiraphMetrics();
36  
37    /** registry for per-superstep metrics */
38    private final SuperstepMetricsRegistry perSuperstep;
39  
40    /** registry for optional per-job metrics */
41    private final GiraphMetricsRegistry perJobOptional;
42  
43    /** registry for required per-job metrics */
44    private final GiraphMetricsRegistry perJobRequired;
45  
46    /** Garbage collection tracker */
47    private final GcTracker gcTracker;
48  
49    /** observer for per-superstep metrics re-initialization */
50    private final List<ResetSuperstepMetricsObserver> observers =
51        Lists.newArrayList();
52  
53    /**
54     * Initialize no-op registry that creates no-op metrics.
55     */
56    private GiraphMetrics() {
57      perJobOptional = GiraphMetricsRegistry.createFake();
58      perSuperstep = SuperstepMetricsRegistry.createFake();
59      perJobRequired = GiraphMetricsRegistry.createWithOptional("giraph", "job");
60      gcTracker = new GcTracker();
61    }
62  
63    /**
64     * Initialize GiraphMetrics with Hadoop Context
65     *
66     * @param conf GiraphConfiguration to use.
67     */
68    private GiraphMetrics(GiraphConfiguration conf) {
69      perJobOptional = GiraphMetricsRegistry.create(conf, "giraph", "job");
70      perSuperstep = SuperstepMetricsRegistry.create(conf, INPUT_SUPERSTEP);
71      perJobRequired = GiraphMetricsRegistry.createWithOptional("giraph", "job");
72      gcTracker = new GcTracker(conf);
73    }
74  
75    /**
76     * Get singleton instance of GiraphMetrics.
77     *
78     * @return GiraphMetrics singleton instance
79     */
80    public static GiraphMetrics get() {
81      return INSTANCE;
82    }
83  
84    /**
85     * Initialize singleton instance of GiraphMetrics.
86     *
87     * @param conf GiraphConfiguration to use.
88     */
89    public static void init(GiraphConfiguration conf) {
90      INSTANCE = new GiraphMetrics(conf);
91    }
92  
93    /**
94     * Get per-job optional metrics.
95     *
96     * @return per-job optional {@link GiraphMetricsRegistry}
97     */
98    public GiraphMetricsRegistry perJobOptional() {
99      return perJobOptional;
100   }
101 
102   /**
103    * Get per-job required metrics.
104    *
105    * @return per-job require {@link GiraphMetricsRegistry}
106    */
107   public GiraphMetricsRegistry perJobRequired() {
108     return perJobRequired;
109   }
110 
111   /**
112    * Get per-superstep metrics.
113    *
114    * @return per-superstep GiraphMetricsRegistry
115    */
116   public SuperstepMetricsRegistry perSuperstep() {
117     return perSuperstep;
118   }
119 
120   /**
121    * Get GC tracker
122    *
123    * @return Gc tracker
124    */
125   public GcTracker getGcTracker() {
126     return gcTracker;
127   }
128 
129   /**
130    * Anyone using per-superstep counters needs to re-initialize their Metrics
131    * object on each new superstep. Otherwise they will always be updating just
132    * one counter. This method allows people to easily register a callback for
133    * when they should do the re-initializing.
134    *
135    * @param observer SuperstepObserver to watch
136    */
137   public synchronized void addSuperstepResetObserver(
138       ResetSuperstepMetricsObserver observer) {
139     observers.add(observer);
140   }
141 
142   /**
143    * Reset the per-superstep MetricsRegistry
144    *
145    * @param superstep long number of superstep
146    */
147   public synchronized void resetSuperstepMetrics(long superstep) {
148     perSuperstep.setSuperstep(superstep);
149     for (ResetSuperstepMetricsObserver observer : observers) {
150       observer.newSuperstep(perSuperstep);
151     }
152   }
153 
154   /**
155    * Dump all metrics to output stream provided.
156    *
157    * @param out PrintStream to dump to.
158    */
159   public void dumpToStream(PrintStream out) {
160     perJobOptional.printToStream(out);
161     perJobRequired.printToStream(out);
162   }
163 
164   /**
165    * Stop using metrics (for cleanup)
166    */
167   public void shutdown() {
168     perJobOptional.shutdown();
169     perJobRequired.shutdown();
170     perSuperstep.shutdown();
171   }
172 }