001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase;
021
022import edu.umd.cs.findbugs.annotations.Nullable;
023import java.util.List;
024import java.util.Map;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.client.RegionStatesCount;
027import org.apache.hadoop.hbase.master.RegionState;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Metrics information on the HBase cluster.
032 * <p>
033 * <tt>ClusterMetrics</tt> provides clients with information such as:
034 * <ul>
035 * <li>The count and names of region servers in the cluster.</li>
036 * <li>The count and names of dead region servers in the cluster.</li>
037 * <li>The name of the active master for the cluster.</li>
038 * <li>The name(s) of the backup master(s) for the cluster, if they exist.</li>
039 * <li>The average cluster load.</li>
040 * <li>The number of regions deployed on the cluster.</li>
041 * <li>The number of requests since last report.</li>
042 * <li>Detailed region server loading and resource usage information,
043 *  per server and per region.</li>
044 * <li>Regions in transition at master</li>
045 * <li>The unique cluster ID</li>
046 * </ul>
047 * <tt>{@link Option}</tt> provides a way to get desired ClusterStatus information.
048 * The following codes will get all the cluster information.
049 * <pre>
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 * If information about live servers is the only wanted.
059 * then codes in the following way:
060 * <pre>
061 * {@code
062 * Admin admin = connection.getAdmin();
063 * ClusterMetrics metrics = admin.getClusterStatus(EnumSet.of(Option.LIVE_SERVERS));
064 * }
065 * </pre>
066 */
067@InterfaceAudience.Public
068public interface ClusterMetrics {
069
070  /**
071   * @return the HBase version string as reported by the HMaster
072   */
073  @Nullable
074  String getHBaseVersion();
075
076  /**
077   * @return the names of region servers on the dead list
078   */
079  List<ServerName> getDeadServerNames();
080
081  /**
082   * @return the names of region servers on the live list
083   */
084  Map<ServerName, ServerMetrics> getLiveServerMetrics();
085
086  /**
087   * @return the number of regions deployed on the cluster
088   */
089  default int getRegionCount() {
090    return getLiveServerMetrics().entrySet().stream()
091        .mapToInt(v -> v.getValue().getRegionMetrics().size()).sum();
092  }
093
094  /**
095   * @return the number of requests since last report
096   */
097  default long getRequestCount() {
098    return getLiveServerMetrics().entrySet().stream()
099        .flatMap(v -> v.getValue().getRegionMetrics().values().stream())
100        .mapToLong(RegionMetrics::getRequestCount).sum();
101  }
102
103  /**
104   * Returns detailed information about the current master {@link ServerName}.
105   * @return current master information if it exists
106   */
107  @Nullable
108  ServerName getMasterName();
109
110  /**
111   * @return the names of backup masters
112   */
113  List<ServerName> getBackupMasterNames();
114
115  @InterfaceAudience.Private
116  List<RegionState> getRegionStatesInTransition();
117
118  @Nullable
119  String getClusterId();
120
121  List<String> getMasterCoprocessorNames();
122
123  default long getLastMajorCompactionTimestamp(TableName table) {
124    return getLiveServerMetrics().values().stream()
125        .flatMap(s -> s.getRegionMetrics().values().stream())
126        .filter(r -> RegionInfo.getTable(r.getRegionName()).equals(table))
127        .mapToLong(RegionMetrics::getLastMajorCompactionTimestamp).min().orElse(0);
128  }
129
130  default long getLastMajorCompactionTimestamp(byte[] regionName) {
131    return getLiveServerMetrics().values().stream()
132        .filter(s -> s.getRegionMetrics().containsKey(regionName))
133        .findAny()
134        .map(s -> s.getRegionMetrics().get(regionName).getLastMajorCompactionTimestamp())
135        .orElse(0L);
136  }
137
138  @Nullable
139  Boolean getBalancerOn();
140
141  int getMasterInfoPort();
142
143  /**
144   * @return the average cluster load
145   */
146  default double getAverageLoad() {
147    int serverSize = getLiveServerMetrics().size();
148    if (serverSize == 0) {
149      return 0;
150    }
151    return (double)getRegionCount() / (double)serverSize;
152  }
153
154  /**
155   * Provide region states count for given table.
156   * e.g howmany regions of give table are opened/closed/rit etc
157   *
158   * @return map of table to region states count
159   */
160  Map<TableName, RegionStatesCount> getTableRegionStatesCount();
161
162  /**
163   * Kinds of ClusterMetrics
164   */
165  enum Option {
166    /**
167     * metrics about hbase version
168     */
169    HBASE_VERSION,
170    /**
171     * metrics about cluster id
172     */
173    CLUSTER_ID,
174    /**
175     * metrics about balancer is on or not
176     */
177    BALANCER_ON,
178    /**
179     * metrics about live region servers
180     */
181    LIVE_SERVERS,
182    /**
183     * metrics about dead region servers
184     */
185    DEAD_SERVERS,
186    /**
187     * metrics about master name
188     */
189    MASTER,
190    /**
191     * metrics about backup masters name
192     */
193    BACKUP_MASTERS,
194    /**
195     * metrics about master coprocessors
196     */
197    MASTER_COPROCESSORS,
198    /**
199     * metrics about regions in transition
200     */
201    REGIONS_IN_TRANSITION,
202    /**
203     * metrics info port
204     */
205    MASTER_INFO_PORT,
206    /**
207     * metrics about table to no of regions status count
208     */
209    TABLE_TO_REGIONS_COUNT
210  }
211}