View Javadoc

1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase;
22  
23  import java.io.DataInput;
24  import java.io.DataOutput;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.Map;
30  import java.util.TreeMap;
31  
32  import org.apache.hadoop.hbase.master.AssignmentManager.RegionState;
33  import org.apache.hadoop.io.VersionedWritable;
34  
35  /**
36   * Status information on the HBase cluster.
37   * <p>
38   * <tt>ClusterStatus</tt> provides clients with information such as:
39   * <ul>
40   * <li>The count and names of region servers in the cluster.</li>
41   * <li>The count and names of dead region servers in the cluster.</li>
42   * <li>The average cluster load.</li>
43   * <li>The number of regions deployed on the cluster.</li>
44   * <li>The number of requests since last report.</li>
45   * <li>Detailed region server loading and resource usage information,
46   *  per server and per region.</li>
47   *  <li>Regions in transition at master</li>
48   * </ul>
49   */
50  public class ClusterStatus extends VersionedWritable {
51    private static final byte VERSION = 0;
52  
53    private String hbaseVersion;
54    private Collection<HServerInfo> liveServerInfo;
55    private Collection<String> deadServers;
56    private Map<String, RegionState> intransition;
57  
58    /**
59     * Constructor, for Writable
60     */
61    public ClusterStatus() {
62      super();
63    }
64  
65    /**
66     * @return the names of region servers on the dead list
67     */
68    public Collection<String> getDeadServerNames() {
69      return Collections.unmodifiableCollection(deadServers);
70    }
71  
72    /**
73     * @return the number of region servers in the cluster
74     */
75    public int getServers() {
76      return liveServerInfo.size();
77    }
78  
79    /**
80     * @return the number of dead region servers in the cluster
81     */
82    public int getDeadServers() {
83      return deadServers.size();
84    }
85  
86    /**
87     * @return the average cluster load
88     */
89    public double getAverageLoad() {
90      int load = 0;
91      for (HServerInfo server: liveServerInfo) {
92        load += server.getLoad().getLoad();
93      }
94      return (double)load / (double)liveServerInfo.size();
95    }
96  
97    /**
98     * @return the number of regions deployed on the cluster
99     */
100   public int getRegionsCount() {
101     int count = 0;
102     for (HServerInfo server: liveServerInfo) {
103       count += server.getLoad().getNumberOfRegions();
104     }
105     return count;
106   }
107 
108   /**
109    * @return the number of requests since last report
110    */
111   public int getRequestsCount() {
112     int count = 0;
113     for (HServerInfo server: liveServerInfo) {
114       count += server.getLoad().getNumberOfRequests();
115     }
116     return count;
117   }
118 
119   /**
120    * @return the HBase version string as reported by the HMaster
121    */
122   public String getHBaseVersion() {
123     return hbaseVersion;
124   }
125 
126   /**
127    * @param version the HBase version string
128    */
129   public void setHBaseVersion(String version) {
130     hbaseVersion = version;
131   }
132 
133   /**
134    * @see java.lang.Object#equals(java.lang.Object)
135    */
136   public boolean equals(Object o) {
137     if (this == o) {
138       return true;
139     }
140     if (!(o instanceof ClusterStatus)) {
141       return false;
142     }
143     return (getVersion() == ((ClusterStatus)o).getVersion()) &&
144       getHBaseVersion().equals(((ClusterStatus)o).getHBaseVersion()) &&
145       liveServerInfo.equals(((ClusterStatus)o).liveServerInfo) &&
146       deadServers.equals(((ClusterStatus)o).deadServers);
147   }
148 
149   /**
150    * @see java.lang.Object#hashCode()
151    */
152   public int hashCode() {
153     return VERSION + hbaseVersion.hashCode() + liveServerInfo.hashCode() +
154       deadServers.hashCode();
155   }
156 
157   /** @return the object version number */
158   public byte getVersion() {
159     return VERSION;
160   }
161 
162   //
163   // Getters
164   //
165 
166   /**
167    * Returns detailed region server information: A list of
168    * {@link HServerInfo}, containing server load and resource usage
169    * statistics as {@link HServerLoad}, containing per-region
170    * statistics as {@link HServerLoad.RegionLoad}.
171    * @return region server information
172    */
173   public Collection<HServerInfo> getServerInfo() {
174     return Collections.unmodifiableCollection(liveServerInfo);
175   }
176 
177   //
178   // Setters
179   //
180 
181   public void setServerInfo(Collection<HServerInfo> serverInfo) {
182     this.liveServerInfo = serverInfo;
183   }
184 
185   public void setDeadServers(Collection<String> deadServers) {
186     this.deadServers = deadServers;
187   }
188 
189   public Map<String, RegionState> getRegionsInTransition() {
190     return this.intransition;
191   }
192 
193   public void setRegionsInTransition(final Map<String, RegionState> m) {
194     this.intransition = m;
195   }
196 
197   //
198   // Writable
199   //
200 
201   public void write(DataOutput out) throws IOException {
202     super.write(out);
203     out.writeUTF(hbaseVersion);
204     out.writeInt(liveServerInfo.size());
205     for (HServerInfo server: liveServerInfo) {
206       server.write(out);
207     }
208     out.writeInt(deadServers.size());
209     for (String server: deadServers) {
210       out.writeUTF(server);
211     }
212     out.writeInt(this.intransition.size());
213     for (Map.Entry<String, RegionState> e: this.intransition.entrySet()) {
214       out.writeUTF(e.getKey());
215       e.getValue().write(out);
216     }
217   }
218 
219   public void readFields(DataInput in) throws IOException {
220     super.readFields(in);
221     hbaseVersion = in.readUTF();
222     int count = in.readInt();
223     liveServerInfo = new ArrayList<HServerInfo>(count);
224     for (int i = 0; i < count; i++) {
225       HServerInfo info = new HServerInfo();
226       info.readFields(in);
227       liveServerInfo.add(info);
228     }
229     count = in.readInt();
230     deadServers = new ArrayList<String>(count);
231     for (int i = 0; i < count; i++) {
232       deadServers.add(in.readUTF());
233     }
234     count = in.readInt();
235     this.intransition = new TreeMap<String, RegionState>();
236     for (int i = 0; i < count; i++) {
237       String key = in.readUTF();
238       RegionState regionState = new RegionState();
239       regionState.readFields(in);
240       this.intransition.put(key, regionState);
241     }
242   }
243 }