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  import com.google.common.annotations.VisibleForTesting;
29  
30  /**
31   * Utility class for tracking metrics for various types of coprocessors. Each coprocessor instance
32   * creates its own MetricRegistry which is exported as an individual MetricSource. MetricRegistries
33   * are ref counted using the hbase-metric module interfaces.
34   */
35  @InterfaceAudience.Private
36  public class MetricsCoprocessor {
37  
38    // Master coprocessor metrics
39    private static final String MASTER_COPROC_METRICS_NAME = "Coprocessor.Master";
40    private static final String MASTER_COPROC_METRICS_CONTEXT = "master";
41    private static final String MASTER_COPROC_METRICS_DESCRIPTION
42        = "Metrics about HBase MasterObservers";
43    private static final String MASTER_COPROC_METRICS_JMX_CONTEXT
44        = "Master,sub=" + MASTER_COPROC_METRICS_NAME;
45  
46    // RegionServer coprocessor metrics
47    private static final String RS_COPROC_METRICS_NAME = "Coprocessor.RegionServer";
48    private static final String RS_COPROC_METRICS_CONTEXT = "regionserver";
49    private static final String RS_COPROC_METRICS_DESCRIPTION
50        = "Metrics about HBase RegionServerObservers";
51    private static final String RS_COPROC_METRICS_JMX_CONTEXT = "RegionServer,sub="
52        + RS_COPROC_METRICS_NAME;
53  
54    // Region coprocessor metrics
55    private static final String REGION_COPROC_METRICS_NAME = "Coprocessor.Region";
56    private static final String REGION_COPROC_METRICS_CONTEXT = "regionserver";
57    private static final String REGION_COPROC_METRICS_DESCRIPTION
58        = "Metrics about HBase RegionObservers";
59    private static final String REGION_COPROC_METRICS_JMX_CONTEXT
60        = "RegionServer,sub=" + REGION_COPROC_METRICS_NAME;
61  
62    // WAL coprocessor metrics
63    private static final String WAL_COPROC_METRICS_NAME = "Coprocessor.WAL";
64    private static final String WAL_COPROC_METRICS_CONTEXT = "regionserver";
65    private static final String WAL_COPROC_METRICS_DESCRIPTION
66        = "Metrics about HBase WALObservers";
67    private static final String WAL_COPROC_METRICS_JMX_CONTEXT
68        = "RegionServer,sub=" + WAL_COPROC_METRICS_NAME;
69  
70    private static String suffix(String metricName, String cpName) {
71      return new StringBuilder(metricName)
72          .append(".")
73          .append("CP_")
74          .append(cpName)
75          .toString();
76    }
77  
78    @VisibleForTesting
79    static MetricRegistryInfo createRegistryInfoForMasterCoprocessor(String clazz) {
80      return new MetricRegistryInfo(
81          suffix(MASTER_COPROC_METRICS_NAME, clazz),
82          MASTER_COPROC_METRICS_DESCRIPTION,
83          suffix(MASTER_COPROC_METRICS_JMX_CONTEXT, clazz),
84          MASTER_COPROC_METRICS_CONTEXT, false);
85    }
86  
87    public static MetricRegistry createRegistryForMasterCoprocessor(String clazz) {
88      return MetricRegistries.global().create(createRegistryInfoForMasterCoprocessor(clazz));
89    }
90  
91    @VisibleForTesting
92    static MetricRegistryInfo createRegistryInfoForRSCoprocessor(String clazz) {
93      return new MetricRegistryInfo(
94          suffix(RS_COPROC_METRICS_NAME, clazz),
95          RS_COPROC_METRICS_DESCRIPTION,
96          suffix(RS_COPROC_METRICS_JMX_CONTEXT, clazz),
97          RS_COPROC_METRICS_CONTEXT, false);
98    }
99  
100   public static MetricRegistry createRegistryForRSCoprocessor(String clazz) {
101     return MetricRegistries.global().create(createRegistryInfoForRSCoprocessor(clazz));
102   }
103 
104   @VisibleForTesting
105   public static MetricRegistryInfo createRegistryInfoForRegionCoprocessor(String clazz) {
106     return new MetricRegistryInfo(
107         suffix(REGION_COPROC_METRICS_NAME, clazz),
108         REGION_COPROC_METRICS_DESCRIPTION,
109         suffix(REGION_COPROC_METRICS_JMX_CONTEXT, clazz),
110         REGION_COPROC_METRICS_CONTEXT, false);
111   }
112 
113   public static MetricRegistry createRegistryForRegionCoprocessor(String clazz) {
114     return MetricRegistries.global().create(createRegistryInfoForRegionCoprocessor(clazz));
115   }
116 
117   @VisibleForTesting
118   public static MetricRegistryInfo createRegistryInfoForWALCoprocessor(String clazz) {
119     return new MetricRegistryInfo(
120         suffix(WAL_COPROC_METRICS_NAME, clazz),
121         WAL_COPROC_METRICS_DESCRIPTION,
122         suffix(WAL_COPROC_METRICS_JMX_CONTEXT, clazz),
123         WAL_COPROC_METRICS_CONTEXT, false);
124   }
125 
126   public static MetricRegistry createRegistryForWALCoprocessor(String clazz) {
127     return MetricRegistries.global().create(createRegistryInfoForWALCoprocessor(clazz));
128   }
129 
130   public static void removeRegistry(MetricRegistry registry) {
131     if (registry == null) {
132       return;
133     }
134     MetricRegistries.global().remove(registry.getMetricRegistryInfo());
135   }
136 }