1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase;
22
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.classification.InterfaceStability;
25 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26 import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos;
27 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.Coprocessor;
28 import org.apache.hadoop.hbase.replication.ReplicationLoadSink;
29 import org.apache.hadoop.hbase.replication.ReplicationLoadSource;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.apache.hadoop.hbase.util.Strings;
32
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.TreeMap;
37 import java.util.TreeSet;
38
39
40
41
42 @InterfaceAudience.Public
43 @InterfaceStability.Evolving
44 public class ServerLoad {
45 private int stores = 0;
46 private int storefiles = 0;
47 private int storeUncompressedSizeMB = 0;
48 private int storefileSizeMB = 0;
49 private int memstoreSizeMB = 0;
50 private int storefileIndexSizeMB = 0;
51 private long readRequestsCount = 0;
52 private long writeRequestsCount = 0;
53 private int rootIndexSizeKB = 0;
54 private int totalStaticIndexSizeKB = 0;
55 private int totalStaticBloomSizeKB = 0;
56 private long totalCompactingKVs = 0;
57 private long currentCompactedKVs = 0;
58
59 public ServerLoad(ClusterStatusProtos.ServerLoad serverLoad) {
60 this.serverLoad = serverLoad;
61 for (ClusterStatusProtos.RegionLoad rl: serverLoad.getRegionLoadsList()) {
62 stores += rl.getStores();
63 storefiles += rl.getStorefiles();
64 storeUncompressedSizeMB += rl.getStoreUncompressedSizeMB();
65 storefileSizeMB += rl.getStorefileSizeMB();
66 memstoreSizeMB += rl.getMemstoreSizeMB();
67 storefileIndexSizeMB += rl.getStorefileIndexSizeMB();
68 readRequestsCount += rl.getReadRequestsCount();
69 writeRequestsCount += rl.getWriteRequestsCount();
70 rootIndexSizeKB += rl.getRootIndexSizeKB();
71 totalStaticIndexSizeKB += rl.getTotalStaticIndexSizeKB();
72 totalStaticBloomSizeKB += rl.getTotalStaticBloomSizeKB();
73 totalCompactingKVs += rl.getTotalCompactingKVs();
74 currentCompactedKVs += rl.getCurrentCompactedKVs();
75 }
76
77 }
78
79
80
81
82 public ClusterStatusProtos.ServerLoad obtainServerLoadPB() {
83 return serverLoad;
84 }
85
86 protected ClusterStatusProtos.ServerLoad serverLoad;
87
88
89 public long getNumberOfRequests() {
90 return serverLoad.getNumberOfRequests();
91 }
92 public boolean hasNumberOfRequests() {
93 return serverLoad.hasNumberOfRequests();
94 }
95
96
97 public long getTotalNumberOfRequests() {
98 return serverLoad.getTotalNumberOfRequests();
99 }
100 public boolean hasTotalNumberOfRequests() {
101 return serverLoad.hasTotalNumberOfRequests();
102 }
103
104
105 public int getUsedHeapMB() {
106 return serverLoad.getUsedHeapMB();
107 }
108 public boolean hasUsedHeapMB() {
109 return serverLoad.hasUsedHeapMB();
110 }
111
112
113 public int getMaxHeapMB() {
114 return serverLoad.getMaxHeapMB();
115 }
116 public boolean hasMaxHeapMB() {
117 return serverLoad.hasMaxHeapMB();
118 }
119
120 public int getStores() {
121 return stores;
122 }
123
124 public int getStorefiles() {
125 return storefiles;
126 }
127
128 public int getStoreUncompressedSizeMB() {
129 return storeUncompressedSizeMB;
130 }
131
132 public int getStorefileSizeInMB() {
133 return storefileSizeMB;
134 }
135
136 public int getMemstoreSizeInMB() {
137 return memstoreSizeMB;
138 }
139
140 public int getStorefileIndexSizeInMB() {
141 return storefileIndexSizeMB;
142 }
143
144 public long getReadRequestsCount() {
145 return readRequestsCount;
146 }
147
148 public long getWriteRequestsCount() {
149 return writeRequestsCount;
150 }
151
152 public int getRootIndexSizeKB() {
153 return rootIndexSizeKB;
154 }
155
156 public int getTotalStaticIndexSizeKB() {
157 return totalStaticIndexSizeKB;
158 }
159
160 public int getTotalStaticBloomSizeKB() {
161 return totalStaticBloomSizeKB;
162 }
163
164 public long getTotalCompactingKVs() {
165 return totalCompactingKVs;
166 }
167
168 public long getCurrentCompactedKVs() {
169 return currentCompactedKVs;
170 }
171
172
173
174
175 public int getNumberOfRegions() {
176 return serverLoad.getRegionLoadsCount();
177 }
178
179 public int getInfoServerPort() {
180 return serverLoad.getInfoServerPort();
181 }
182
183
184
185
186
187 public List<ReplicationLoadSource> getReplicationLoadSourceList() {
188 return ProtobufUtil.toReplicationLoadSourceList(serverLoad.getReplLoadSourceList());
189 }
190
191
192
193
194
195 public ReplicationLoadSink getReplicationLoadSink() {
196 if (serverLoad.hasReplLoadSink()) {
197 return ProtobufUtil.toReplicationLoadSink(serverLoad.getReplLoadSink());
198 } else {
199 return null;
200 }
201 }
202
203
204
205
206
207
208
209
210
211
212 public int getLoad() {
213
214
215
216
217 return getNumberOfRegions();
218 }
219
220
221
222
223 public Map<byte[], RegionLoad> getRegionsLoad() {
224 Map<byte[], RegionLoad> regionLoads =
225 new TreeMap<byte[], RegionLoad>(Bytes.BYTES_COMPARATOR);
226 for (ClusterStatusProtos.RegionLoad rl : serverLoad.getRegionLoadsList()) {
227 RegionLoad regionLoad = new RegionLoad(rl);
228 regionLoads.put(regionLoad.getName(), regionLoad);
229 }
230 return regionLoads;
231 }
232
233
234
235
236
237 public String[] getRegionServerCoprocessors() {
238 List<Coprocessor> list = obtainServerLoadPB().getCoprocessorsList();
239 String [] ret = new String[list.size()];
240 int i = 0;
241 for (Coprocessor elem : list) {
242 ret[i++] = elem.getName();
243 }
244
245 return ret;
246 }
247
248
249
250
251
252
253 public String[] getRsCoprocessors() {
254
255
256 TreeSet<String> coprocessSet = new TreeSet<String>();
257 for (Coprocessor coprocessor : obtainServerLoadPB().getCoprocessorsList()) {
258 coprocessSet.add(coprocessor.getName());
259 }
260 return coprocessSet.toArray(new String[coprocessSet.size()]);
261 }
262
263
264
265
266 public double getRequestsPerSecond() {
267 return getNumberOfRequests();
268 }
269
270
271
272
273 @Override
274 public String toString() {
275 StringBuilder sb =
276 Strings.appendKeyValue(new StringBuilder(), "requestsPerSecond",
277 Double.valueOf(getRequestsPerSecond()));
278 Strings.appendKeyValue(sb, "numberOfOnlineRegions", Integer.valueOf(getNumberOfRegions()));
279 sb = Strings.appendKeyValue(sb, "usedHeapMB", Integer.valueOf(this.getUsedHeapMB()));
280 sb = Strings.appendKeyValue(sb, "maxHeapMB", Integer.valueOf(getMaxHeapMB()));
281 sb = Strings.appendKeyValue(sb, "numberOfStores", Integer.valueOf(this.stores));
282 sb = Strings.appendKeyValue(sb, "numberOfStorefiles", Integer.valueOf(this.storefiles));
283 sb =
284 Strings.appendKeyValue(sb, "storefileUncompressedSizeMB",
285 Integer.valueOf(this.storeUncompressedSizeMB));
286 sb = Strings.appendKeyValue(sb, "storefileSizeMB", Integer.valueOf(this.storefileSizeMB));
287 if (this.storeUncompressedSizeMB != 0) {
288 sb =
289 Strings.appendKeyValue(
290 sb,
291 "compressionRatio",
292 String.format("%.4f", (float) this.storefileSizeMB
293 / (float) this.storeUncompressedSizeMB));
294 }
295 sb = Strings.appendKeyValue(sb, "memstoreSizeMB", Integer.valueOf(this.memstoreSizeMB));
296 sb =
297 Strings.appendKeyValue(sb, "storefileIndexSizeMB",
298 Integer.valueOf(this.storefileIndexSizeMB));
299 sb = Strings.appendKeyValue(sb, "readRequestsCount", Long.valueOf(this.readRequestsCount));
300 sb = Strings.appendKeyValue(sb, "writeRequestsCount", Long.valueOf(this.writeRequestsCount));
301 sb = Strings.appendKeyValue(sb, "rootIndexSizeKB", Integer.valueOf(this.rootIndexSizeKB));
302 sb =
303 Strings.appendKeyValue(sb, "totalStaticIndexSizeKB",
304 Integer.valueOf(this.totalStaticIndexSizeKB));
305 sb =
306 Strings.appendKeyValue(sb, "totalStaticBloomSizeKB",
307 Integer.valueOf(this.totalStaticBloomSizeKB));
308 sb = Strings.appendKeyValue(sb, "totalCompactingKVs", Long.valueOf(this.totalCompactingKVs));
309 sb = Strings.appendKeyValue(sb, "currentCompactedKVs", Long.valueOf(this.currentCompactedKVs));
310 float compactionProgressPct = Float.NaN;
311 if (this.totalCompactingKVs > 0) {
312 compactionProgressPct =
313 Float.valueOf((float) this.currentCompactedKVs / this.totalCompactingKVs);
314 }
315 sb = Strings.appendKeyValue(sb, "compactionProgressPct", compactionProgressPct);
316
317 String[] coprocessorStrings = getRsCoprocessors();
318 if (coprocessorStrings != null) {
319 sb = Strings.appendKeyValue(sb, "coprocessors", Arrays.toString(coprocessorStrings));
320 }
321 return sb.toString();
322 }
323
324 public static final ServerLoad EMPTY_SERVERLOAD =
325 new ServerLoad(ClusterStatusProtos.ServerLoad.newBuilder().build());
326 }