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  
19  package org.apache.hadoop.hbase.master;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
23  import org.apache.hadoop.metrics2.MetricsCollector;
24  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
25  import org.apache.hadoop.metrics2.lib.Interns;
26  
27  /**
28   * Hadoop2 implementation of MetricsMasterSource.
29   *
30   * Implements BaseSource through BaseSourceImpl, following the pattern
31   */
32  @InterfaceAudience.Private
33  public class MetricsMasterProcSourceImpl
34      extends BaseSourceImpl implements MetricsMasterProcSource {
35  
36    private final MetricsMasterWrapper masterWrapper;
37  
38    public MetricsMasterProcSourceImpl(MetricsMasterWrapper masterWrapper) {
39      this(METRICS_NAME,
40          METRICS_DESCRIPTION,
41          METRICS_CONTEXT,
42          METRICS_JMX_CONTEXT,
43          masterWrapper);
44    }
45  
46    public MetricsMasterProcSourceImpl(String metricsName,
47                                       String metricsDescription,
48                                       String metricsContext,
49                                       String metricsJmxContext,
50                                       MetricsMasterWrapper masterWrapper) {
51      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
52      this.masterWrapper = masterWrapper;
53  
54    }
55  
56    @Override
57    public void init() {
58      super.init();
59    }
60  
61    @Override
62    public void getMetrics(MetricsCollector metricsCollector, boolean all) {
63      MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName);
64  
65      // masterWrapper can be null because this function is called inside of init.
66      if (masterWrapper != null) {
67        metricsRecordBuilder
68            .addGauge(Interns.info(NUM_MASTER_WALS_NAME, NUM_MASTER_WALS_DESC),
69                masterWrapper.getNumWALFiles());
70      }
71  
72      metricsRegistry.snapshot(metricsRecordBuilder, all);
73    }
74  
75  }