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; 019 020import edu.umd.cs.findbugs.annotations.Nullable; 021import java.util.List; 022import java.util.Map; 023import org.apache.hadoop.hbase.client.RegionInfo; 024import org.apache.hadoop.hbase.client.RegionStatesCount; 025import org.apache.hadoop.hbase.master.RegionState; 026import org.apache.yetus.audience.InterfaceAudience; 027 028/** 029 * Metrics information on the HBase cluster. 030 * <p> 031 * <tt>ClusterMetrics</tt> provides clients with information such as: 032 * <ul> 033 * <li>The count and names of region servers in the cluster.</li> 034 * <li>The count and names of dead region servers in the cluster.</li> 035 * <li>The name of the active master for the cluster.</li> 036 * <li>The name(s) of the backup master(s) for the cluster, if they exist.</li> 037 * <li>The average cluster load.</li> 038 * <li>The number of regions deployed on the cluster.</li> 039 * <li>The number of requests since last report.</li> 040 * <li>Detailed region server loading and resource usage information, per server and per 041 * region.</li> 042 * <li>Regions in transition at master</li> 043 * <li>The unique cluster ID</li> 044 * </ul> 045 * <tt>{@link Option}</tt> provides a way to get desired ClusterStatus information. The following 046 * codes will get all the cluster information. 047 * 048 * <pre> 049 * { 050 * @code 051 * // Original version still works 052 * Admin admin = connection.getAdmin(); 053 * ClusterMetrics metrics = admin.getClusterStatus(); 054 * // or below, a new version which has the same effects 055 * ClusterMetrics metrics = admin.getClusterStatus(EnumSet.allOf(Option.class)); 056 * } 057 * </pre> 058 * 059 * If information about live servers is the only wanted. then codes in the following way: 060 * 061 * <pre> 062 * { 063 * @code 064 * Admin admin = connection.getAdmin(); 065 * ClusterMetrics metrics = admin.getClusterStatus(EnumSet.of(Option.LIVE_SERVERS)); 066 * } 067 * </pre> 068 */ 069@InterfaceAudience.Public 070public interface ClusterMetrics { 071 072 /** Returns the HBase version string as reported by the HMaster */ 073 @Nullable 074 String getHBaseVersion(); 075 076 /** Returns the names of region servers on the dead list */ 077 List<ServerName> getDeadServerNames(); 078 079 /** Returns the names of region servers on the live list */ 080 Map<ServerName, ServerMetrics> getLiveServerMetrics(); 081 082 /** Returns the number of regions deployed on the cluster */ 083 default int getRegionCount() { 084 return getLiveServerMetrics().entrySet().stream() 085 .mapToInt(v -> v.getValue().getRegionMetrics().size()).sum(); 086 } 087 088 /** Returns the number of requests since last report */ 089 default long getRequestCount() { 090 return getLiveServerMetrics().entrySet().stream() 091 .flatMap(v -> v.getValue().getRegionMetrics().values().stream()) 092 .mapToLong(RegionMetrics::getRequestCount).sum(); 093 } 094 095 /** 096 * Returns detailed information about the current master {@link ServerName}. 097 * @return current master information if it exists 098 */ 099 @Nullable 100 ServerName getMasterName(); 101 102 /** Returns the names of backup masters */ 103 List<ServerName> getBackupMasterNames(); 104 105 @InterfaceAudience.Private 106 List<RegionState> getRegionStatesInTransition(); 107 108 @Nullable 109 String getClusterId(); 110 111 List<String> getMasterCoprocessorNames(); 112 113 default long getLastMajorCompactionTimestamp(TableName table) { 114 return getLiveServerMetrics().values().stream() 115 .flatMap(s -> s.getRegionMetrics().values().stream()) 116 .filter(r -> RegionInfo.getTable(r.getRegionName()).equals(table)) 117 .mapToLong(RegionMetrics::getLastMajorCompactionTimestamp).min().orElse(0); 118 } 119 120 default long getLastMajorCompactionTimestamp(byte[] regionName) { 121 return getLiveServerMetrics().values().stream() 122 .filter(s -> s.getRegionMetrics().containsKey(regionName)).findAny() 123 .map(s -> s.getRegionMetrics().get(regionName).getLastMajorCompactionTimestamp()).orElse(0L); 124 } 125 126 @Nullable 127 Boolean getBalancerOn(); 128 129 int getMasterInfoPort(); 130 131 List<ServerName> getServersName(); 132 133 /** Returns the average cluster load */ 134 default double getAverageLoad() { 135 int serverSize = getLiveServerMetrics().size(); 136 if (serverSize == 0) { 137 return 0; 138 } 139 return (double) getRegionCount() / (double) serverSize; 140 } 141 142 /** 143 * Provide region states count for given table. e.g howmany regions of give table are 144 * opened/closed/rit etc 145 * @return map of table to region states count 146 */ 147 Map<TableName, RegionStatesCount> getTableRegionStatesCount(); 148 149 /** 150 * Kinds of ClusterMetrics 151 */ 152 enum Option { 153 /** 154 * metrics about hbase version 155 */ 156 HBASE_VERSION, 157 /** 158 * metrics about cluster id 159 */ 160 CLUSTER_ID, 161 /** 162 * metrics about balancer is on or not 163 */ 164 BALANCER_ON, 165 /** 166 * metrics about live region servers 167 */ 168 LIVE_SERVERS, 169 /** 170 * metrics about dead region servers 171 */ 172 DEAD_SERVERS, 173 /** 174 * metrics about master name 175 */ 176 MASTER, 177 /** 178 * metrics about backup masters name 179 */ 180 BACKUP_MASTERS, 181 /** 182 * metrics about master coprocessors 183 */ 184 MASTER_COPROCESSORS, 185 /** 186 * metrics about regions in transition 187 */ 188 REGIONS_IN_TRANSITION, 189 /** 190 * metrics info port 191 */ 192 MASTER_INFO_PORT, 193 /** 194 * metrics about live region servers name 195 */ 196 SERVERS_NAME, 197 /** 198 * metrics about table to no of regions status count 199 */ 200 TABLE_TO_REGIONS_COUNT, 201 } 202}