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