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.hadoop.hbase.master.metrics;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.hbase.metrics.MetricsRate;
23  import org.apache.hadoop.metrics.MetricsContext;
24  import org.apache.hadoop.metrics.MetricsRecord;
25  import org.apache.hadoop.metrics.MetricsUtil;
26  import org.apache.hadoop.metrics.Updater;
27  import org.apache.hadoop.metrics.jvm.JvmMetrics;
28  import org.apache.hadoop.metrics.util.MetricsRegistry;
29  
30  
31  /**
32   * This class is for maintaining the various master statistics
33   * and publishing them through the metrics interfaces.
34   * <p>
35   * This class has a number of metrics variables that are publicly accessible;
36   * these variables (objects) have methods to update their values.
37   */
38  public class MasterMetrics implements Updater {
39    private final Log LOG = LogFactory.getLog(this.getClass());
40    private final MetricsRecord metricsRecord;
41    private final MetricsRegistry registry = new MetricsRegistry();
42    private final MasterStatistics masterStatistics;
43    /*
44     * Count of requests to the cluster since last call to metrics update
45     */
46    private final MetricsRate cluster_requests =
47      new MetricsRate("cluster_requests", registry);
48  
49    public MasterMetrics(final String name) {
50      MetricsContext context = MetricsUtil.getContext("hbase");
51      metricsRecord = MetricsUtil.createRecord(context, "master");
52      metricsRecord.setTag("Master", name);
53      context.registerUpdater(this);
54      JvmMetrics.init("Master", name);
55  
56      // expose the MBean for metrics
57      masterStatistics = new MasterStatistics(this.registry);
58  
59      LOG.info("Initialized");
60    }
61  
62    public void shutdown() {
63      if (masterStatistics != null)
64        masterStatistics.shutdown();
65    }
66  
67    /**
68     * Since this object is a registered updater, this method will be called
69     * periodically, e.g. every 5 seconds.
70     * @param unused
71     */
72    public void doUpdates(MetricsContext unused) {
73      synchronized (this) {
74        this.cluster_requests.pushMetric(metricsRecord);
75      }
76      this.metricsRecord.update();
77    }
78  
79    public void resetAllMinMax() {
80      // Nothing to do
81    }
82  
83    /**
84     * @return Count of requests.
85     */
86    public float getRequests() {
87      return this.cluster_requests.getPreviousIntervalValue();
88    }
89  
90    /**
91     * @param inc How much to add to requests.
92     */
93    public void incrementRequests(final int inc) {
94      this.cluster_requests.inc(inc);
95    }
96  }