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(ServerName master,
39        Map<ServerName, List<HRegionInfo>> clusterState) {
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        if (master != null && numServers > 1 && master.equals(server.getKey())) {
47          // Don't count the master since its load is meant to be low.
48          numServers--;
49          continue;
50        }
51        List<HRegionInfo> regions = server.getValue();
52        int sz = regions.size();
53        if (sz == 0) emptyRegionServerPresent = true;
54        numRegions += sz;
55        serversByLoad.put(new ServerAndLoad(server.getKey(), sz), regions);
56      }
57    }
58  
59    Map<ServerName, List<HRegionInfo>> getClusterState() {
60      return clusterState;
61    }
62  
63    NavigableMap<ServerAndLoad, List<HRegionInfo>> getServersByLoad() {
64      return serversByLoad;
65    }
66  
67    boolean isEmptyRegionServerPresent() {
68      return emptyRegionServerPresent;
69    }
70  
71    int getNumRegions() {
72      return numRegions;
73    }
74  
75    int getNumServers() {
76      return numServers;
77    }
78  
79    float getLoadAverage() {
80      return (float) numRegions / numServers;
81    }
82  
83    int getMaxLoad() {
84      return getServersByLoad().lastKey().getLoad();
85    }
86  
87    int getMinLoad() {
88      return getServersByLoad().firstKey().getLoad();
89    }
90  
91  }