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