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      this.numRegions = 0;
40      this.numServers = clusterState.size();
41      this.clusterState = clusterState;
42      serversByLoad = new TreeMap<ServerAndLoad, List<HRegionInfo>>();
43      // Iterate so we can count regions as we build the map
44      for (Map.Entry<ServerName, List<HRegionInfo>> server : clusterState.entrySet()) {
45        List<HRegionInfo> regions = server.getValue();
46        int sz = regions.size();
47        if (sz == 0) emptyRegionServerPresent = true;
48        numRegions += sz;
49        serversByLoad.put(new ServerAndLoad(server.getKey(), sz), regions);
50      }
51    }
52  
53    Map<ServerName, List<HRegionInfo>> getClusterState() {
54      return clusterState;
55    }
56  
57    NavigableMap<ServerAndLoad, List<HRegionInfo>> getServersByLoad() {
58      return serversByLoad;
59    }
60  
61    boolean isEmptyRegionServerPresent() {
62      return emptyRegionServerPresent;
63    }
64  
65    int getNumRegions() {
66      return numRegions;
67    }
68  
69    int getNumServers() {
70      return numServers;
71    }
72  
73    float getLoadAverage() {
74      return (float) numRegions / numServers;
75    }
76  
77    int getMaxLoad() {
78      return getServersByLoad().lastKey().getLoad();
79    }
80  
81    int getMinLoad() {
82      return getServersByLoad().firstKey().getLoad();
83    }
84  
85  }