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 *   &#64;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 *   &#64;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 unknown list */
080  List<ServerName> getUnknownServerNames();
081
082  /** Returns the names of region servers on the decommissioned list */
083  List<ServerName> getDecommissionedServerNames();
084
085  /** Returns the names of region servers on the live list */
086  Map<ServerName, ServerMetrics> getLiveServerMetrics();
087
088  /** Returns the number of regions deployed on the cluster */
089  default int getRegionCount() {
090    return getLiveServerMetrics().entrySet().stream()
091      .mapToInt(v -> v.getValue().getRegionMetrics().size()).sum();
092  }
093
094  /** Returns the number of requests since last report */
095  default long getRequestCount() {
096    return getLiveServerMetrics().entrySet().stream()
097      .flatMap(v -> v.getValue().getRegionMetrics().values().stream())
098      .mapToLong(RegionMetrics::getRequestCount).sum();
099  }
100
101  /**
102   * Returns detailed information about the current master {@link ServerName}.
103   * @return current master information if it exists
104   */
105  @Nullable
106  ServerName getMasterName();
107
108  /** Returns the names of backup masters */
109  List<ServerName> getBackupMasterNames();
110
111  @InterfaceAudience.Private
112  List<RegionState> getRegionStatesInTransition();
113
114  @Nullable
115  String getClusterId();
116
117  List<String> getMasterCoprocessorNames();
118
119  default long getLastMajorCompactionTimestamp(TableName table) {
120    return getLiveServerMetrics().values().stream()
121      .flatMap(s -> s.getRegionMetrics().values().stream())
122      .filter(r -> RegionInfo.getTable(r.getRegionName()).equals(table))
123      .mapToLong(RegionMetrics::getLastMajorCompactionTimestamp).min().orElse(0);
124  }
125
126  default long getLastMajorCompactionTimestamp(byte[] regionName) {
127    return getLiveServerMetrics().values().stream()
128      .filter(s -> s.getRegionMetrics().containsKey(regionName)).findAny()
129      .map(s -> s.getRegionMetrics().get(regionName).getLastMajorCompactionTimestamp()).orElse(0L);
130  }
131
132  @Nullable
133  Boolean getBalancerOn();
134
135  int getMasterInfoPort();
136
137  List<ServerName> getServersName();
138
139  /** Returns the average cluster load */
140  default double getAverageLoad() {
141    int serverSize = getLiveServerMetrics().size();
142    if (serverSize == 0) {
143      return 0;
144    }
145    return (double) getRegionCount() / (double) serverSize;
146  }
147
148  /**
149   * Provide region states count for given table. e.g howmany regions of give table are
150   * opened/closed/rit etc
151   * @return map of table to region states count
152   */
153  Map<TableName, RegionStatesCount> getTableRegionStatesCount();
154
155  /**
156   * Provide the list of master tasks
157   */
158  @Nullable
159  List<ServerTask> getMasterTasks();
160
161  /**
162   * Kinds of ClusterMetrics
163   */
164  enum Option {
165    /**
166     * metrics about hbase version
167     */
168    HBASE_VERSION,
169    /**
170     * metrics about cluster id
171     */
172    CLUSTER_ID,
173    /**
174     * metrics about balancer is on or not
175     */
176    BALANCER_ON,
177    /**
178     * metrics about live region servers
179     */
180    LIVE_SERVERS,
181    /**
182     * metrics about dead region servers
183     */
184    DEAD_SERVERS,
185    /**
186     * metrics about unknown region servers
187     */
188    UNKNOWN_SERVERS,
189    /**
190     * metrics about master name
191     */
192    MASTER,
193    /**
194     * metrics about backup masters name
195     */
196    BACKUP_MASTERS,
197    /**
198     * metrics about master coprocessors
199     */
200    MASTER_COPROCESSORS,
201    /**
202     * metrics about regions in transition
203     */
204    REGIONS_IN_TRANSITION,
205    /**
206     * metrics info port
207     */
208    MASTER_INFO_PORT,
209    /**
210     * metrics about live region servers name
211     */
212    SERVERS_NAME,
213    /**
214     * metrics about table to no of regions status count
215     */
216    TABLE_TO_REGIONS_COUNT,
217    /**
218     * metrics about monitored tasks
219     */
220    TASKS,
221    /**
222     * metrics about decommissioned region servers
223     */
224    DECOMMISSIONED_SERVERS,
225  }
226}