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 the master is already stopped or has initiated a shutdown, no point in registering the 086 // metrics again. 087 if (masterWrapper != null && masterWrapper.isRunning()) { 088 metricsRecordBuilder 089 .addGauge(Interns.info(MERGE_PLAN_COUNT_NAME, MERGE_PLAN_COUNT_DESC), 090 masterWrapper.getMergePlanCount()) 091 .addGauge(Interns.info(SPLIT_PLAN_COUNT_NAME, SPLIT_PLAN_COUNT_DESC), 092 masterWrapper.getSplitPlanCount()) 093 .addGauge(Interns.info(MASTER_ACTIVE_TIME_NAME, 094 MASTER_ACTIVE_TIME_DESC), masterWrapper.getActiveTime()) 095 .addGauge(Interns.info(MASTER_START_TIME_NAME, 096 MASTER_START_TIME_DESC), masterWrapper.getStartTime()) 097 .addGauge(Interns.info(MASTER_FINISHED_INITIALIZATION_TIME_NAME, 098 MASTER_FINISHED_INITIALIZATION_TIME_DESC), 099 masterWrapper.getMasterInitializationTime()) 100 .addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC), 101 masterWrapper.getAverageLoad()) 102 .tag(Interns.info(LIVE_REGION_SERVERS_NAME, LIVE_REGION_SERVERS_DESC), 103 masterWrapper.getRegionServers()) 104 .addGauge(Interns.info(NUM_REGION_SERVERS_NAME, 105 NUMBER_OF_REGION_SERVERS_DESC), masterWrapper.getNumRegionServers()) 106 .tag(Interns.info(DEAD_REGION_SERVERS_NAME, DEAD_REGION_SERVERS_DESC), 107 masterWrapper.getDeadRegionServers()) 108 .addGauge(Interns.info(NUM_DEAD_REGION_SERVERS_NAME, 109 NUMBER_OF_DEAD_REGION_SERVERS_DESC), 110 masterWrapper.getNumDeadRegionServers()) 111 .tag(Interns.info(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC), 112 masterWrapper.getZookeeperQuorum()) 113 .tag(Interns.info(SERVER_NAME_NAME, SERVER_NAME_DESC), masterWrapper.getServerName()) 114 .tag(Interns.info(CLUSTER_ID_NAME, CLUSTER_ID_DESC), masterWrapper.getClusterId()) 115 .tag(Interns.info(IS_ACTIVE_MASTER_NAME, 116 IS_ACTIVE_MASTER_DESC), 117 String.valueOf(masterWrapper.getIsActiveMaster())); 118 } 119 120 metricsRegistry.snapshot(metricsRecordBuilder, all); 121 if(metricsAdapter != null) { 122 metricsAdapter.snapshotAllMetrics(registry, metricsRecordBuilder); 123 } 124 } 125 126 @Override 127 public OperationMetrics getServerCrashMetrics() { 128 return serverCrashMetrics; 129 } 130}