View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.classification.InterfaceStability;
31  import org.apache.hadoop.hbase.master.RegionState;
32  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
33  import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos;
34  import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos.LiveServerInfo;
35  import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos.RegionInTransition;
36  import org.apache.hadoop.hbase.protobuf.generated.FSProtos.HBaseVersionFileContent;
37  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
38  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier;
39  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType;
40  import org.apache.hadoop.hbase.util.ByteStringer;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.apache.hadoop.io.VersionedWritable;
43  
44  
45  /**
46   * Status information on the HBase cluster.
47   * <p>
48   * <tt>ClusterStatus</tt> provides clients with information such as:
49   * <ul>
50   * <li>The count and names of region servers in the cluster.</li>
51   * <li>The count and names of dead region servers in the cluster.</li>
52   * <li>The name of the active master for the cluster.</li>
53   * <li>The name(s) of the backup master(s) for the cluster, if they exist.</li>
54   * <li>The average cluster load.</li>
55   * <li>The number of regions deployed on the cluster.</li>
56   * <li>The number of requests since last report.</li>
57   * <li>Detailed region server loading and resource usage information,
58   *  per server and per region.</li>
59   * <li>Regions in transition at master</li>
60   * <li>The unique cluster ID</li>
61   * </ul>
62   */
63  @InterfaceAudience.Public
64  @InterfaceStability.Evolving
65  public class ClusterStatus extends VersionedWritable {
66    /**
67     * Version for object serialization.  Incremented for changes in serialized
68     * representation.
69     * <dl>
70     *   <dt>0</dt> <dd>Initial version</dd>
71     *   <dt>1</dt> <dd>Added cluster ID</dd>
72     *   <dt>2</dt> <dd>Added Map of ServerName to ServerLoad</dd>
73     *   <dt>3</dt> <dd>Added master and backupMasters</dd>
74     * </dl>
75     */
76    private static final byte VERSION = 2;
77  
78    private String hbaseVersion;
79    private Map<ServerName, ServerLoad> liveServers;
80    private Collection<ServerName> deadServers;
81    private ServerName master;
82    private Collection<ServerName> backupMasters;
83    private Map<String, RegionState> intransition;
84    private String clusterId;
85    private String[] masterCoprocessors;
86    private Boolean balancerOn;
87  
88    /**
89     * Constructor, for Writable
90     * @deprecated Used by Writables and Writables are going away.
91     */
92    @Deprecated
93    public ClusterStatus() {
94      super();
95    }
96  
97    public ClusterStatus(final String hbaseVersion, final String clusterid,
98        final Map<ServerName, ServerLoad> servers,
99        final Collection<ServerName> deadServers,
100       final ServerName master,
101       final Collection<ServerName> backupMasters,
102       final Map<String, RegionState> rit,
103       final String[] masterCoprocessors,
104       final Boolean balancerOn) {
105     this.hbaseVersion = hbaseVersion;
106 
107     this.liveServers = servers;
108     this.deadServers = deadServers;
109     this.master = master;
110     this.backupMasters = backupMasters;
111     this.intransition = rit;
112     this.clusterId = clusterid;
113     this.masterCoprocessors = masterCoprocessors;
114     this.balancerOn = balancerOn;
115   }
116 
117   /**
118    * @return the names of region servers on the dead list
119    */
120   public Collection<ServerName> getDeadServerNames() {
121     return Collections.unmodifiableCollection(deadServers);
122   }
123 
124   /**
125    * @return the number of region servers in the cluster
126    */
127   public int getServersSize() {
128     return liveServers.size();
129   }
130 
131   /**
132    * @return the number of dead region servers in the cluster
133    */
134   public int getDeadServers() {
135     return deadServers.size();
136   }
137 
138   /**
139    * @return the average cluster load
140    */
141   public double getAverageLoad() {
142     int load = getRegionsCount();
143     return (double)load / (double)getServersSize();
144   }
145 
146   /**
147    * @return the number of regions deployed on the cluster
148    */
149   public int getRegionsCount() {
150     int count = 0;
151     for (Map.Entry<ServerName, ServerLoad> e: this.liveServers.entrySet()) {
152       count += e.getValue().getNumberOfRegions();
153     }
154     return count;
155   }
156 
157   /**
158    * @return the number of requests since last report
159    */
160   public int getRequestsCount() {
161     int count = 0;
162     for (Map.Entry<ServerName, ServerLoad> e: this.liveServers.entrySet()) {
163       count += e.getValue().getNumberOfRequests();
164     }
165     return count;
166   }
167 
168   /**
169    * @return the HBase version string as reported by the HMaster
170    */
171   public String getHBaseVersion() {
172     return hbaseVersion;
173   }
174 
175   /**
176    * @see java.lang.Object#equals(java.lang.Object)
177    */
178   public boolean equals(Object o) {
179     if (this == o) {
180       return true;
181     }
182     if (!(o instanceof ClusterStatus)) {
183       return false;
184     }
185     return (getVersion() == ((ClusterStatus)o).getVersion()) &&
186       getHBaseVersion().equals(((ClusterStatus)o).getHBaseVersion()) &&
187       this.liveServers.equals(((ClusterStatus)o).liveServers) &&
188       this.deadServers.containsAll(((ClusterStatus)o).deadServers) &&
189       Arrays.equals(this.masterCoprocessors,
190                     ((ClusterStatus)o).masterCoprocessors) &&
191       this.master.equals(((ClusterStatus)o).master) &&
192       this.backupMasters.containsAll(((ClusterStatus)o).backupMasters);
193   }
194 
195   /**
196    * @see java.lang.Object#hashCode()
197    */
198   public int hashCode() {
199     return VERSION + hbaseVersion.hashCode() + this.liveServers.hashCode() +
200       this.deadServers.hashCode() + this.master.hashCode() +
201       this.backupMasters.hashCode();
202   }
203 
204   /** @return the object version number */
205   public byte getVersion() {
206     return VERSION;
207   }
208 
209   //
210   // Getters
211   //
212 
213   /**
214    * Returns detailed region server information: A list of
215    * {@link ServerName}.
216    * @return region server information
217    * @deprecated Use {@link #getServers()}
218    */
219   @Deprecated
220   public Collection<ServerName> getServerInfo() {
221     return getServers();
222   }
223 
224   public Collection<ServerName> getServers() {
225     return Collections.unmodifiableCollection(this.liveServers.keySet());
226   }
227 
228   /**
229    * Returns detailed information about the current master {@link ServerName}.
230    * @return current master information if it exists
231    */
232   public ServerName getMaster() {
233     return this.master;
234   }
235 
236   /**
237    * @return the number of backup masters in the cluster
238    */
239   public int getBackupMastersSize() {
240     return this.backupMasters.size();
241   }
242 
243   /**
244    * @return the names of backup masters
245    */
246   public Collection<ServerName> getBackupMasters() {
247     return Collections.unmodifiableCollection(this.backupMasters);
248   }
249 
250   /**
251    * @param sn
252    * @return Server's load or null if not found.
253    */
254   public ServerLoad getLoad(final ServerName sn) {
255     return this.liveServers.get(sn);
256   }
257 
258   @InterfaceAudience.Private
259   public Map<String, RegionState> getRegionsInTransition() {
260     return this.intransition;
261   }
262 
263   public String getClusterId() {
264     return clusterId;
265   }
266 
267   public String[] getMasterCoprocessors() {
268     return masterCoprocessors;
269   }
270 
271   public long getLastMajorCompactionTsForTable(TableName table) {
272     long result = Long.MAX_VALUE;
273     for (ServerName server : getServers()) {
274       ServerLoad load = getLoad(server);
275       for (RegionLoad rl : load.getRegionsLoad().values()) {
276         if (table.equals(HRegionInfo.getTable(rl.getName()))) {
277           result = Math.min(result, rl.getLastMajorCompactionTs());
278         }
279       }
280     }
281     return result == Long.MAX_VALUE ? 0 : result;
282   }
283 
284   public long getLastMajorCompactionTsForRegion(final byte[] region) {
285     for (ServerName server : getServers()) {
286       ServerLoad load = getLoad(server);
287       RegionLoad rl = load.getRegionsLoad().get(region);
288       if (rl != null) {
289         return rl.getLastMajorCompactionTs();
290       }
291     }
292     return 0;
293   }
294 
295   public boolean isBalancerOn() {
296     return balancerOn != null && balancerOn;
297   }
298 
299   public Boolean getBalancerOn() {
300     return balancerOn;
301   }
302 
303   public String toString() {
304     StringBuilder sb = new StringBuilder(1024);
305     sb.append("Master: " + master);
306     sb.append("\nNumber of backup masters: " + backupMasters.size());
307     for (ServerName serverName: backupMasters) {
308       sb.append("\n  " + serverName);
309     }
310 
311     sb.append("\nNumber of live region servers: " + liveServers.size());
312     for (ServerName serverName: liveServers.keySet()) {
313       sb.append("\n  " + serverName.getServerName());
314     }
315 
316     sb.append("\nNumber of dead region servers: " + deadServers.size());
317     for (ServerName serverName: deadServers) {
318       sb.append("\n  " + serverName);
319     }
320 
321     sb.append("\nAverage load: " + getAverageLoad());
322     sb.append("\nNumber of requests: " + getRequestsCount());
323     sb.append("\nNumber of regions: " + getRegionsCount());
324     sb.append("\nNumber of regions in transition: " + intransition.size());
325     for (RegionState state: intransition.values()) {
326       sb.append("\n  " + state.toDescriptiveString());
327     }
328     return sb.toString();
329   }
330 
331   /**
332     * Convert a ClusterStatus to a protobuf ClusterStatus
333     *
334     * @return the protobuf ClusterStatus
335     */
336   public ClusterStatusProtos.ClusterStatus convert() {
337     ClusterStatusProtos.ClusterStatus.Builder builder =
338         ClusterStatusProtos.ClusterStatus.newBuilder();
339     builder.setHbaseVersion(HBaseVersionFileContent.newBuilder().setVersion(getHBaseVersion()));
340 
341     if (liveServers != null){
342       for (Map.Entry<ServerName, ServerLoad> entry : liveServers.entrySet()) {
343         LiveServerInfo.Builder lsi =
344           LiveServerInfo.newBuilder().setServer(ProtobufUtil.toServerName(entry.getKey()));
345         lsi.setServerLoad(entry.getValue().obtainServerLoadPB());
346         builder.addLiveServers(lsi.build());
347       }
348     }
349 
350     if (deadServers != null){
351       for (ServerName deadServer : deadServers) {
352         builder.addDeadServers(ProtobufUtil.toServerName(deadServer));
353       }
354     }
355 
356     if (intransition != null) {
357       for (Map.Entry<String, RegionState> rit : getRegionsInTransition().entrySet()) {
358         ClusterStatusProtos.RegionState rs = rit.getValue().convert();
359         RegionSpecifier.Builder spec =
360             RegionSpecifier.newBuilder().setType(RegionSpecifierType.REGION_NAME);
361         spec.setValue(ByteStringer.wrap(Bytes.toBytes(rit.getKey())));
362 
363         RegionInTransition pbRIT =
364             RegionInTransition.newBuilder().setSpec(spec.build()).setRegionState(rs).build();
365         builder.addRegionsInTransition(pbRIT);
366       }
367     }
368 
369     if (clusterId != null) {
370       builder.setClusterId(new ClusterId(clusterId).convert());
371     }
372 
373     if (masterCoprocessors != null) {
374       for (String coprocessor : masterCoprocessors) {
375         builder.addMasterCoprocessors(HBaseProtos.Coprocessor.newBuilder().setName(coprocessor));
376       }
377     }
378 
379     if (master != null){
380       builder.setMaster(ProtobufUtil.toServerName(getMaster()));
381     }
382 
383     if (backupMasters != null) {
384       for (ServerName backup : backupMasters) {
385         builder.addBackupMasters(ProtobufUtil.toServerName(backup));
386       }
387     }
388 
389     if (balancerOn != null){
390       builder.setBalancerOn(balancerOn);
391     }
392 
393     return builder.build();
394   }
395 
396   /**
397    * Convert a protobuf ClusterStatus to a ClusterStatus
398    *
399    * @param proto the protobuf ClusterStatus
400    * @return the converted ClusterStatus
401    */
402   public static ClusterStatus convert(ClusterStatusProtos.ClusterStatus proto) {
403 
404     Map<ServerName, ServerLoad> servers = null;
405     servers = new HashMap<ServerName, ServerLoad>(proto.getLiveServersList().size());
406     for (LiveServerInfo lsi : proto.getLiveServersList()) {
407       servers.put(ProtobufUtil.toServerName(
408           lsi.getServer()), new ServerLoad(lsi.getServerLoad()));
409     }
410 
411     Collection<ServerName> deadServers = null;
412     deadServers = new ArrayList<ServerName>(proto.getDeadServersList().size());
413     for (HBaseProtos.ServerName sn : proto.getDeadServersList()) {
414       deadServers.add(ProtobufUtil.toServerName(sn));
415     }
416 
417     Collection<ServerName> backupMasters = null;
418     backupMasters = new ArrayList<ServerName>(proto.getBackupMastersList().size());
419     for (HBaseProtos.ServerName sn : proto.getBackupMastersList()) {
420       backupMasters.add(ProtobufUtil.toServerName(sn));
421     }
422 
423     Map<String, RegionState> rit = null;
424     rit = new HashMap<String, RegionState>(proto.getRegionsInTransitionList().size());
425     for (RegionInTransition region : proto.getRegionsInTransitionList()) {
426       String key = new String(region.getSpec().getValue().toByteArray());
427       RegionState value = RegionState.convert(region.getRegionState());
428       rit.put(key, value);
429     }
430 
431     String[] masterCoprocessors = null;
432     final int numMasterCoprocessors = proto.getMasterCoprocessorsCount();
433     masterCoprocessors = new String[numMasterCoprocessors];
434     for (int i = 0; i < numMasterCoprocessors; i++) {
435       masterCoprocessors[i] = proto.getMasterCoprocessors(i).getName();
436     }
437 
438     return new ClusterStatus(proto.getHbaseVersion().getVersion(),
439       ClusterId.convert(proto.getClusterId()).toString(),servers,deadServers,
440       ProtobufUtil.toServerName(proto.getMaster()),backupMasters,rit,masterCoprocessors,
441       proto.getBalancerOn());
442   }
443 }