1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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 }