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