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