View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.ipc;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.metrics.MetricsContext;
26  import org.apache.hadoop.metrics.MetricsRecord;
27  import org.apache.hadoop.metrics.MetricsUtil;
28  import org.apache.hadoop.metrics.Updater;
29  import org.apache.hadoop.metrics.util.MetricsRegistry;
30  import org.apache.hadoop.metrics.util.MetricsTimeVaryingRate;
31  
32  /**
33   *
34   * This class is for maintaining  the various RPC statistics
35   * and publishing them through the metrics interfaces.
36   * This also registers the JMX MBean for RPC.
37   * <p>
38   * This class has a number of metrics variables that are publicly accessible;
39   * these variables (objects) have methods to update their values;
40   * for example:
41   *  <p> {@link #rpcQueueTime}.inc(time)
42   *
43   */
44  public class HBaseRpcMetrics implements Updater {
45    private MetricsRecord metricsRecord;
46    private static Log LOG = LogFactory.getLog(HBaseRpcMetrics.class);
47    private final HBaseRPCStatistics rpcStatistics;
48  
49    public HBaseRpcMetrics(String hostName, String port) {
50      MetricsContext context = MetricsUtil.getContext("rpc");
51      metricsRecord = MetricsUtil.createRecord(context, "metrics");
52  
53      metricsRecord.setTag("port", port);
54  
55      LOG.info("Initializing RPC Metrics with hostName="
56          + hostName + ", port=" + port);
57  
58      context.registerUpdater(this);
59  
60      rpcStatistics = new HBaseRPCStatistics(this.registry, hostName, port);
61    }
62  
63  
64    /**
65     * The metrics variables are public:
66     *  - they can be set directly by calling their set/inc methods
67     *  -they can also be read directly - e.g. JMX does this.
68     */
69    public final MetricsRegistry registry = new MetricsRegistry();
70  
71    public MetricsTimeVaryingRate rpcQueueTime = new MetricsTimeVaryingRate("RpcQueueTime", registry);
72    public MetricsTimeVaryingRate rpcProcessingTime = new MetricsTimeVaryingRate("RpcProcessingTime", registry);
73  
74    //public Map <String, MetricsTimeVaryingRate> metricsList = Collections.synchronizedMap(new HashMap<String, MetricsTimeVaryingRate>());
75  
76  
77    private MetricsTimeVaryingRate get(String key) {
78      return (MetricsTimeVaryingRate) registry.get(key);
79    }
80    private MetricsTimeVaryingRate create(String key) {
81      return new MetricsTimeVaryingRate(key, this.registry);
82    }
83  
84    public synchronized void inc(String name, int amt) {
85      MetricsTimeVaryingRate m = get(name);
86      if (m == null) {
87        m = create(name);
88      }
89      m.inc(amt);
90    }
91  
92    /**
93     * Push the metrics to the monitoring subsystem on doUpdate() call.
94     * @param context ctx
95     */
96    public void doUpdates(MetricsContext context) {
97      rpcQueueTime.pushMetric(metricsRecord);
98      rpcProcessingTime.pushMetric(metricsRecord);
99  
100     synchronized (registry) {
101       // Iterate through the registry to propagate the different rpc metrics.
102 
103       for (String metricName : registry.getKeyList() ) {
104         MetricsTimeVaryingRate value = (MetricsTimeVaryingRate) registry.get(metricName);
105 
106         value.pushMetric(metricsRecord);
107       }
108     }
109     metricsRecord.update();
110   }
111 
112   public void shutdown() {
113     if (rpcStatistics != null)
114       rpcStatistics.shutdown();
115   }
116 }