View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master.balancer;
19  
20  import java.util.List;
21  import java.util.Map;
22  import java.util.NavigableMap;
23  import java.util.TreeMap;
24  
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.ServerName;
27  
28  /**
29   * Class used to hold the current state of the cluster and how balanced it is.
30   */
31  public class ClusterLoadState {
32    private final Map<ServerName, List<HRegionInfo>> clusterState;
33    private final NavigableMap<ServerAndLoad, List<HRegionInfo>> serversByLoad;
34    private boolean emptyRegionServerPresent = false;
35    private int numRegions = 0;
36    private int numServers = 0;
37  
38    public ClusterLoadState(Map<ServerName, List<HRegionInfo>> clusterState) {
39      super();
40      this.numRegions = 0;
41      this.numServers = clusterState.size();
42      this.clusterState = clusterState;
43      serversByLoad = new TreeMap<ServerAndLoad, List<HRegionInfo>>();
44      // Iterate so we can count regions as we build the map
45      for (Map.Entry<ServerName, List<HRegionInfo>> server : clusterState.entrySet()) {
46        List<HRegionInfo> regions = server.getValue();
47        int sz = regions.size();
48        if (sz == 0) emptyRegionServerPresent = true;
49        numRegions += sz;
50        serversByLoad.put(new ServerAndLoad(server.getKey(), sz), regions);
51      }
52    }
53  
54    Map<ServerName, List<HRegionInfo>> getClusterState() {
55      return clusterState;
56    }
57  
58    NavigableMap<ServerAndLoad, List<HRegionInfo>> getServersByLoad() {
59      return serversByLoad;
60    }
61  
62    boolean isEmptyRegionServerPresent() {
63      return emptyRegionServerPresent;
64    }
65  
66    int getNumRegions() {
67      return numRegions;
68    }
69  
70    int getNumServers() {
71      return numServers;
72    }
73  
74    float getLoadAverage() {
75      return (float) numRegions / numServers;
76    }
77  
78    int getMaxLoad() {
79      return getServersByLoad().lastKey().getLoad();
80    }
81  
82    int getMinLoad() {
83      return getServersByLoad().firstKey().getLoad();
84    }
85  
86  }