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.hbase.metrics.OperationMetrics;
023import org.apache.hadoop.metrics2.MetricsCollector;
024import org.apache.hadoop.metrics2.MetricsRecordBuilder;
025import org.apache.hadoop.metrics2.lib.MutableFastCounter;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * Hadoop2 implementation of MetricsMasterSource. Implements BaseSource through BaseSourceImpl,
030 * following the pattern
031 */
032@InterfaceAudience.Private
033public class MetricsMasterSourceImpl extends BaseSourceImpl implements MetricsMasterSource {
034
035  private final MetricsMasterWrapper masterWrapper;
036  private MutableFastCounter clusterRequestsCounter;
037
038  private OperationMetrics serverCrashMetrics;
039
040  public MetricsMasterSourceImpl(MetricsMasterWrapper masterWrapper) {
041    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, masterWrapper);
042  }
043
044  public MetricsMasterSourceImpl(String metricsName, String metricsDescription,
045    String metricsContext, String metricsJmxContext, MetricsMasterWrapper masterWrapper) {
046    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
047    this.masterWrapper = masterWrapper;
048
049  }
050
051  @Override
052  public void init() {
053    super.init();
054    clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0L);
055
056    /*
057     * NOTE: Please refer to HBASE-9774 and HBASE-14282. Based on these two issues, HBase is moving
058     * away from using Hadoop's metric2 to having independent HBase specific Metrics. Use {@link
059     * BaseSourceImpl#registry} to register the new metrics.
060     */
061    serverCrashMetrics = new OperationMetrics(registry, SERVER_CRASH_METRIC_PREFIX);
062  }
063
064  @Override
065  public void incRequests(final long inc) {
066    this.clusterRequestsCounter.incr(inc);
067  }
068
069  @Override
070  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
071
072    MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName);
073
074    // masterWrapper can be null because this function is called inside of init.
075    // If the master is already stopped or has initiated a shutdown, no point in registering the
076    // metrics again.
077    if (masterWrapper != null && masterWrapper.isRunning()) {
078      metricsRecordBuilder
079        .addGauge(Interns.info(MERGE_PLAN_COUNT_NAME, MERGE_PLAN_COUNT_DESC),
080          masterWrapper.getMergePlanCount())
081        .addGauge(Interns.info(SPLIT_PLAN_COUNT_NAME, SPLIT_PLAN_COUNT_DESC),
082          masterWrapper.getSplitPlanCount())
083        .addGauge(Interns.info(MASTER_ACTIVE_TIME_NAME, MASTER_ACTIVE_TIME_DESC),
084          masterWrapper.getActiveTime())
085        .addGauge(Interns.info(MASTER_START_TIME_NAME, MASTER_START_TIME_DESC),
086          masterWrapper.getStartTime())
087        .addGauge(Interns.info(MASTER_FINISHED_INITIALIZATION_TIME_NAME,
088          MASTER_FINISHED_INITIALIZATION_TIME_DESC), masterWrapper.getMasterInitializationTime())
089        .addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC),
090          masterWrapper.getAverageLoad())
091        .tag(Interns.info(LIVE_REGION_SERVERS_NAME, LIVE_REGION_SERVERS_DESC),
092          masterWrapper.getRegionServers())
093        .addGauge(Interns.info(NUM_REGION_SERVERS_NAME, NUMBER_OF_REGION_SERVERS_DESC),
094          masterWrapper.getNumRegionServers())
095        .tag(Interns.info(DEAD_REGION_SERVERS_NAME, DEAD_REGION_SERVERS_DESC),
096          masterWrapper.getDeadRegionServers())
097        .addGauge(Interns.info(NUM_DEAD_REGION_SERVERS_NAME, NUMBER_OF_DEAD_REGION_SERVERS_DESC),
098          masterWrapper.getNumDeadRegionServers())
099        .tag(Interns.info(DRAINING_REGION_SERVER_NAME, DRAINING_REGION_SERVER_DESC),
100          masterWrapper.getDrainingRegionServers())
101        .addGauge(
102          Interns.info(NUM_DRAINING_REGION_SERVERS_NAME, NUMBER_OF_DRAINING_REGION_SERVERS_DESC),
103          masterWrapper.getNumDrainingRegionServers())
104        .tag(Interns.info(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC),
105          masterWrapper.getZookeeperQuorum())
106        .tag(Interns.info(SERVER_NAME_NAME, SERVER_NAME_DESC), masterWrapper.getServerName())
107        .tag(Interns.info(CLUSTER_ID_NAME, CLUSTER_ID_DESC), masterWrapper.getClusterId())
108        .tag(Interns.info(IS_ACTIVE_MASTER_NAME, IS_ACTIVE_MASTER_DESC),
109          String.valueOf(masterWrapper.getIsActiveMaster()));
110    }
111
112    metricsRegistry.snapshot(metricsRecordBuilder, all);
113    if (metricsAdapter != null) {
114      metricsAdapter.snapshotAllMetrics(registry, metricsRecordBuilder);
115    }
116  }
117
118  @Override
119  public OperationMetrics getServerCrashMetrics() {
120    return serverCrashMetrics;
121  }
122}