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  List<ServerName> getServersName();
144
145  /**
146   * @return the average cluster load
147   */
148  default double getAverageLoad() {
149    int serverSize = getLiveServerMetrics().size();
150    if (serverSize == 0) {
151      return 0;
152    }
153    return (double)getRegionCount() / (double)serverSize;
154  }
155
156  /**
157   * Provide region states count for given table.
158   * e.g howmany regions of give table are opened/closed/rit etc
159   *
160   * @return map of table to region states count
161   */
162  Map<TableName, RegionStatesCount> getTableRegionStatesCount();
163
164  /**
165   * Kinds of ClusterMetrics
166   */
167  enum Option {
168    /**
169     * metrics about hbase version
170     */
171    HBASE_VERSION,
172    /**
173     * metrics about cluster id
174     */
175    CLUSTER_ID,
176    /**
177     * metrics about balancer is on or not
178     */
179    BALANCER_ON,
180    /**
181     * metrics about live region servers
182     */
183    LIVE_SERVERS,
184    /**
185     * metrics about dead region servers
186     */
187    DEAD_SERVERS,
188    /**
189     * metrics about master name
190     */
191    MASTER,
192    /**
193     * metrics about backup masters name
194     */
195    BACKUP_MASTERS,
196    /**
197     * metrics about master coprocessors
198     */
199    MASTER_COPROCESSORS,
200    /**
201     * metrics about regions in transition
202     */
203    REGIONS_IN_TRANSITION,
204    /**
205     * metrics info port
206     */
207    MASTER_INFO_PORT,
208    /**
209     * metrics about live region servers name
210     */
211    SERVERS_NAME,
212    /**
213     * metrics about table to no of regions status count
214     */
215    TABLE_TO_REGIONS_COUNT,
216  }
217}