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