1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase;
21
22 import java.util.ArrayList;
23 import java.util.Comparator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.NavigableSet;
27 import java.util.TreeMap;
28 import java.util.TreeSet;
29
30
31
32
33
34
35
36 public class HDFSBlocksDistribution {
37 private Map<String,HostAndWeight> hostAndWeights = null;
38 private long uniqueBlocksTotalWeight = 0;
39
40
41
42
43
44
45
46
47
48
49
50 public static class HostAndWeight {
51
52 private String host;
53 private long weight;
54
55
56
57
58
59
60 public HostAndWeight(String host, long weight) {
61 this.host = host;
62 this.weight = weight;
63 }
64
65
66
67
68
69 public void addWeight(long weight) {
70 this.weight += weight;
71 }
72
73
74
75
76 public String getHost() {
77 return host;
78 }
79
80
81
82
83 public long getWeight() {
84 return weight;
85 }
86
87
88
89
90 public static class WeightComparator implements Comparator<HostAndWeight> {
91 @Override
92 public int compare(HostAndWeight l, HostAndWeight r) {
93 if(l.getWeight() == r.getWeight()) {
94 return l.getHost().compareTo(r.getHost());
95 }
96 return l.getWeight() < r.getWeight() ? -1 : 1;
97 }
98 }
99 }
100
101
102
103
104 public HDFSBlocksDistribution() {
105 this.hostAndWeights =
106 new TreeMap<String,HostAndWeight>();
107 }
108
109
110
111
112 @Override
113 public synchronized String toString() {
114 return "number of unique hosts in the disribution=" +
115 this.hostAndWeights.size();
116 }
117
118
119
120
121
122
123 public void addHostsAndBlockWeight(String[] hosts, long weight) {
124 if (hosts == null || hosts.length == 0) {
125
126 return;
127 }
128
129 addUniqueWeight(weight);
130 for (String hostname : hosts) {
131 addHostAndBlockWeight(hostname, weight);
132 }
133 }
134
135
136
137
138
139 private void addUniqueWeight(long weight) {
140 uniqueBlocksTotalWeight += weight;
141 }
142
143
144
145
146
147
148
149 private void addHostAndBlockWeight(String host, long weight) {
150 if (host == null) {
151
152 return;
153 }
154
155 HostAndWeight hostAndWeight = this.hostAndWeights.get(host);
156 if(hostAndWeight == null) {
157 hostAndWeight = new HostAndWeight(host, weight);
158 this.hostAndWeights.put(host, hostAndWeight);
159 } else {
160 hostAndWeight.addWeight(weight);
161 }
162 }
163
164
165
166
167 public Map<String,HostAndWeight> getHostAndWeights() {
168 return this.hostAndWeights;
169 }
170
171
172
173
174
175
176
177 public long getWeight(String host) {
178 long weight = 0;
179 if (host != null) {
180 HostAndWeight hostAndWeight = this.hostAndWeights.get(host);
181 if(hostAndWeight != null) {
182 weight = hostAndWeight.getWeight();
183 }
184 }
185 return weight;
186 }
187
188
189
190
191 public long getUniqueBlocksTotalWeight() {
192 return uniqueBlocksTotalWeight;
193 }
194
195
196
197
198
199
200 public float getBlockLocalityIndex(String host) {
201 float localityIndex = 0;
202 HostAndWeight hostAndWeight = this.hostAndWeights.get(host);
203 if (hostAndWeight != null && uniqueBlocksTotalWeight != 0) {
204 localityIndex=(float)hostAndWeight.weight/(float)uniqueBlocksTotalWeight;
205 }
206 return localityIndex;
207 }
208
209
210
211
212
213
214 public void add(HDFSBlocksDistribution otherBlocksDistribution) {
215 Map<String,HostAndWeight> otherHostAndWeights =
216 otherBlocksDistribution.getHostAndWeights();
217 for (Map.Entry<String, HostAndWeight> otherHostAndWeight:
218 otherHostAndWeights.entrySet()) {
219 addHostAndBlockWeight(otherHostAndWeight.getValue().host,
220 otherHostAndWeight.getValue().weight);
221 }
222 addUniqueWeight(otherBlocksDistribution.getUniqueBlocksTotalWeight());
223 }
224
225
226
227
228 public List<String> getTopHosts() {
229 NavigableSet<HostAndWeight> orderedHosts = new TreeSet<HostAndWeight>(
230 new HostAndWeight.WeightComparator());
231 orderedHosts.addAll(this.hostAndWeights.values());
232 List<String> topHosts = new ArrayList<String>(orderedHosts.size());
233 for(HostAndWeight haw : orderedHosts.descendingSet()) {
234 topHosts.add(haw.getHost());
235 }
236 return topHosts;
237 }
238
239 }