001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.master;
020
021import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
022import org.apache.hadoop.hbase.metrics.Interns;
023import org.apache.hadoop.metrics2.MetricsCollector;
024import org.apache.hadoop.metrics2.MetricsRecordBuilder;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Hadoop2 implementation of MetricsMasterSource.
029 *
030 * Implements BaseSource through BaseSourceImpl, following the pattern
031 */
032@InterfaceAudience.Private
033public class MetricsMasterProcSourceImpl
034    extends BaseSourceImpl implements MetricsMasterProcSource {
035
036  private final MetricsMasterWrapper masterWrapper;
037
038  public MetricsMasterProcSourceImpl(MetricsMasterWrapper masterWrapper) {
039    this(METRICS_NAME,
040        METRICS_DESCRIPTION,
041        METRICS_CONTEXT,
042        METRICS_JMX_CONTEXT,
043        masterWrapper);
044  }
045
046  public MetricsMasterProcSourceImpl(String metricsName,
047                                     String metricsDescription,
048                                     String metricsContext,
049                                     String metricsJmxContext,
050                                     MetricsMasterWrapper masterWrapper) {
051    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
052    this.masterWrapper = masterWrapper;
053
054  }
055
056  @Override
057  public void init() {
058    super.init();
059  }
060
061  @Override
062  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
063    MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName);
064
065    // masterWrapper can be null because this function is called inside of init.
066    if (masterWrapper != null) {
067      metricsRecordBuilder
068          .addGauge(Interns.info(NUM_MASTER_WALS_NAME, NUM_MASTER_WALS_DESC),
069              masterWrapper.getNumWALFiles());
070    }
071
072    metricsRegistry.snapshot(metricsRecordBuilder, all);
073  }
074
075}