View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  package org.apache.hadoop.hbase.coprocessor;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.metrics.MetricRegistries;
25  import org.apache.hadoop.hbase.metrics.MetricRegistry;
26  import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
27  
28  /**
29   * Utility class for tracking metrics for various types of coprocessors. Each coprocessor instance
30   * creates its own MetricRegistry which is exported as an individual MetricSource. MetricRegistries
31   * are ref counted using the hbase-metric module interfaces.
32   */
33  @InterfaceAudience.Private
34  public class MetricsCoprocessor {
35  
36    // Master coprocessor metrics
37    private static final String MASTER_COPROC_METRICS_NAME = "Coprocessor.Master";
38    private static final String MASTER_COPROC_METRICS_CONTEXT = "master";
39    private static final String MASTER_COPROC_METRICS_DESCRIPTION
40        = "Metrics about HBase MasterObservers";
41    private static final String MASTER_COPROC_METRICS_JMX_CONTEXT
42        = "Master,sub=" + MASTER_COPROC_METRICS_NAME;
43  
44    // RegionServer coprocessor metrics
45    private static final String RS_COPROC_METRICS_NAME = "Coprocessor.RegionServer";
46    private static final String RS_COPROC_METRICS_CONTEXT = "regionserver";
47    private static final String RS_COPROC_METRICS_DESCRIPTION
48        = "Metrics about HBase RegionServerObservers";
49    private static final String RS_COPROC_METRICS_JMX_CONTEXT = "RegionServer,sub="
50        + RS_COPROC_METRICS_NAME;
51  
52    // Region coprocessor metrics
53    private static final String REGION_COPROC_METRICS_NAME = "Coprocessor.Region";
54    private static final String REGION_COPROC_METRICS_CONTEXT = "regionserver";
55    private static final String REGION_COPROC_METRICS_DESCRIPTION
56        = "Metrics about HBase RegionObservers";
57    private static final String REGION_COPROC_METRICS_JMX_CONTEXT
58        = "RegionServer,sub=" + REGION_COPROC_METRICS_NAME;
59  
60    // WAL coprocessor metrics
61    private static final String WAL_COPROC_METRICS_NAME = "Coprocessor.WAL";
62    private static final String WAL_COPROC_METRICS_CONTEXT = "regionserver";
63    private static final String WAL_COPROC_METRICS_DESCRIPTION
64        = "Metrics about HBase WALObservers";
65    private static final String WAL_COPROC_METRICS_JMX_CONTEXT
66        = "RegionServer,sub=" + WAL_COPROC_METRICS_NAME;
67  
68    private static String suffix(String metricName, String cpName) {
69      return new StringBuilder(metricName)
70          .append(".")
71          .append("CP_")
72          .append(cpName)
73          .toString();
74    }
75  
76    static MetricRegistryInfo createRegistryInfoForMasterCoprocessor(String clazz) {
77      return new MetricRegistryInfo(
78          suffix(MASTER_COPROC_METRICS_NAME, clazz),
79          MASTER_COPROC_METRICS_DESCRIPTION,
80          suffix(MASTER_COPROC_METRICS_JMX_CONTEXT, clazz),
81          MASTER_COPROC_METRICS_CONTEXT, false);
82    }
83  
84    public static MetricRegistry createRegistryForMasterCoprocessor(String clazz) {
85      return MetricRegistries.global().create(createRegistryInfoForMasterCoprocessor(clazz));
86    }
87  
88    static MetricRegistryInfo createRegistryInfoForRSCoprocessor(String clazz) {
89      return new MetricRegistryInfo(
90          suffix(RS_COPROC_METRICS_NAME, clazz),
91          RS_COPROC_METRICS_DESCRIPTION,
92          suffix(RS_COPROC_METRICS_JMX_CONTEXT, clazz),
93          RS_COPROC_METRICS_CONTEXT, false);
94    }
95  
96    public static MetricRegistry createRegistryForRSCoprocessor(String clazz) {
97      return MetricRegistries.global().create(createRegistryInfoForRSCoprocessor(clazz));
98    }
99  
100   public static MetricRegistryInfo createRegistryInfoForRegionCoprocessor(String clazz) {
101     return new MetricRegistryInfo(
102         suffix(REGION_COPROC_METRICS_NAME, clazz),
103         REGION_COPROC_METRICS_DESCRIPTION,
104         suffix(REGION_COPROC_METRICS_JMX_CONTEXT, clazz),
105         REGION_COPROC_METRICS_CONTEXT, false);
106   }
107 
108   public static MetricRegistry createRegistryForRegionCoprocessor(String clazz) {
109     return MetricRegistries.global().create(createRegistryInfoForRegionCoprocessor(clazz));
110   }
111 
112   public static MetricRegistryInfo createRegistryInfoForWALCoprocessor(String clazz) {
113     return new MetricRegistryInfo(
114         suffix(WAL_COPROC_METRICS_NAME, clazz),
115         WAL_COPROC_METRICS_DESCRIPTION,
116         suffix(WAL_COPROC_METRICS_JMX_CONTEXT, clazz),
117         WAL_COPROC_METRICS_CONTEXT, false);
118   }
119 
120   public static MetricRegistry createRegistryForWALCoprocessor(String clazz) {
121     return MetricRegistries.global().create(createRegistryInfoForWALCoprocessor(clazz));
122   }
123 
124   public static void removeRegistry(MetricRegistry registry) {
125     if (registry == null) {
126       return;
127     }
128     MetricRegistries.global().remove(registry.getMetricRegistryInfo());
129   }
130 }