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 */
018package org.apache.hadoop.hbase.master;
019
020import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
021import org.apache.hadoop.hbase.metrics.Interns;
022import org.apache.hadoop.metrics2.MetricsCollector;
023import org.apache.hadoop.metrics2.MetricsRecordBuilder;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Hadoop2 implementation of MetricsMasterSource. Implements BaseSource through BaseSourceImpl,
028 * following the pattern
029 */
030@InterfaceAudience.Private
031public class MetricsMasterProcSourceImpl extends BaseSourceImpl implements MetricsMasterProcSource {
032
033  private final MetricsMasterWrapper masterWrapper;
034
035  public MetricsMasterProcSourceImpl(MetricsMasterWrapper masterWrapper) {
036    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, masterWrapper);
037  }
038
039  public MetricsMasterProcSourceImpl(String metricsName, String metricsDescription,
040    String metricsContext, String metricsJmxContext, MetricsMasterWrapper masterWrapper) {
041    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
042    this.masterWrapper = masterWrapper;
043
044  }
045
046  @Override
047  public void init() {
048    super.init();
049  }
050
051  @Override
052  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
053    MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName);
054
055    // masterWrapper can be null because this function is called inside of init.
056    if (masterWrapper != null) {
057      metricsRecordBuilder.addGauge(Interns.info(NUM_MASTER_WALS_NAME, NUM_MASTER_WALS_DESC),
058        masterWrapper.getNumWALFiles());
059    }
060
061    metricsRegistry.snapshot(metricsRecordBuilder, all);
062    if (metricsAdapter != null) {
063      metricsAdapter.snapshotAllMetrics(registry, metricsRecordBuilder);
064    }
065  }
066
067}