diff --git src/main/java/org/apache/hadoop/hbase/ServerLoad.java src/main/java/org/apache/hadoop/hbase/ServerLoad.java index 912f519..df4fd28 100644 --- src/main/java/org/apache/hadoop/hbase/ServerLoad.java +++ src/main/java/org/apache/hadoop/hbase/ServerLoad.java @@ -20,14 +20,16 @@ package org.apache.hadoop.hbase; +import java.util.Arrays; import java.util.List; import java.util.TreeSet; -import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos; -import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionLoad; -import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.Coprocessor; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos; +import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.Coprocessor; +import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionLoad; +import org.apache.hadoop.hbase.util.Strings; /** * This class is used for exporting current state of load on a RegionServer. @@ -35,8 +37,38 @@ import org.apache.hadoop.classification.InterfaceStability; @InterfaceAudience.Public @InterfaceStability.Evolving public class ServerLoad { + private int stores = 0; + private int storefiles = 0; + private int storeUncompressedSizeMB = 0; + private int storefileSizeMB = 0; + private int memstoreSizeMB = 0; + private int storefileIndexSizeMB = 0; + private int readRequestsCount = 0; + private int writeRequestsCount = 0; + private int rootIndexSizeKB = 0; + private int totalStaticIndexSizeKB = 0; + private int totalStaticBloomSizeKB = 0; + private long totalCompactingKVs = 0; + private long currentCompactedKVs = 0; + public ServerLoad(HBaseProtos.ServerLoad serverLoad) { this.serverLoad = serverLoad; + for (HBaseProtos.RegionLoad rl: serverLoad.getRegionLoadsList()) { + stores += rl.getStores(); + storefiles += rl.getStorefiles(); + storeUncompressedSizeMB += rl.getStoreUncompressedSizeMB(); + storefileSizeMB += rl.getStorefileSizeMB(); + memstoreSizeMB += rl.getMemstoreSizeMB(); + storefileIndexSizeMB += rl.getStorefileIndexSizeMB(); + readRequestsCount += rl.getReadRequestsCount(); + writeRequestsCount += rl.getWriteRequestsCount(); + rootIndexSizeKB += rl.getRootIndexSizeKB(); + totalStaticIndexSizeKB += rl.getTotalStaticIndexSizeKB(); + totalStaticBloomSizeKB += rl.getTotalStaticBloomSizeKB(); + totalCompactingKVs += rl.getTotalCompactingKVs(); + currentCompactedKVs += rl.getCurrentCompactedKVs(); + } + } /* @return the underlying ServerLoad protobuf object */ @@ -103,6 +135,58 @@ public class ServerLoad { return serverLoad.getCoprocessorsCount(); } + public int getStores() { + return stores; + } + + public int getStorefiles() { + return storefiles; + } + + public int getStoreUncompressedSizeMB() { + return storeUncompressedSizeMB; + } + + public int getStorefileSizeMB() { + return storefileSizeMB; + } + + public int getMemstoreSizeMB() { + return memstoreSizeMB; + } + + public int getStorefileIndexSizeMB() { + return storefileIndexSizeMB; + } + + public int getReadRequestsCount() { + return readRequestsCount; + } + + public int getWriteRequestsCount() { + return writeRequestsCount; + } + + public int getRootIndexSizeKB() { + return rootIndexSizeKB; + } + + public int getTotalStaticIndexSizeKB() { + return totalStaticIndexSizeKB; + } + + public int getTotalStaticBloomSizeKB() { + return totalStaticBloomSizeKB; + } + + public long getTotalCompactingKVs() { + return totalCompactingKVs; + } + + public long getCurrentCompactedKVs() { + return currentCompactedKVs; + } + /** * Return the RegionServer-level coprocessors from a ServerLoad pb. * @param sl - ServerLoad @@ -150,6 +234,53 @@ public class ServerLoad { return coprocessSet.toArray(new String[0]); } + + @Override + public String toString() { + StringBuilder sb = + Strings.appendKeyValue(new StringBuilder(), "numberOfStores", Integer.valueOf(this.stores)); + sb = Strings.appendKeyValue(sb, "numberOfStorefiles", Integer.valueOf(this.storefiles)); + sb = + Strings.appendKeyValue(sb, "storefileUncompressedSizeMB", + Integer.valueOf(this.storeUncompressedSizeMB)); + sb = Strings.appendKeyValue(sb, "storefileSizeMB", Integer.valueOf(this.storefileSizeMB)); + if (this.storeUncompressedSizeMB != 0) { + sb = + Strings.appendKeyValue( + sb, + "compressionRatio", + String.format("%.4f", (float) this.storefileSizeMB + / (float) this.storeUncompressedSizeMB)); + } + sb = Strings.appendKeyValue(sb, "memstoreSizeMB", Integer.valueOf(this.memstoreSizeMB)); + sb = + Strings.appendKeyValue(sb, "storefileIndexSizeMB", + Integer.valueOf(this.storefileIndexSizeMB)); + sb = Strings.appendKeyValue(sb, "readRequestsCount", Long.valueOf(this.readRequestsCount)); + sb = Strings.appendKeyValue(sb, "writeRequestsCount", Long.valueOf(this.writeRequestsCount)); + sb = Strings.appendKeyValue(sb, "rootIndexSizeKB", Integer.valueOf(this.rootIndexSizeKB)); + sb = + Strings.appendKeyValue(sb, "totalStaticIndexSizeKB", + Integer.valueOf(this.totalStaticIndexSizeKB)); + sb = + Strings.appendKeyValue(sb, "totalStaticBloomSizeKB", + Integer.valueOf(this.totalStaticBloomSizeKB)); + sb = Strings.appendKeyValue(sb, "totalCompactingKVs", Long.valueOf(this.totalCompactingKVs)); + sb = Strings.appendKeyValue(sb, "currentCompactedKVs", Long.valueOf(this.currentCompactedKVs)); + float compactionProgressPct = Float.NaN; + if (this.totalCompactingKVs > 0) { + compactionProgressPct = + Float.valueOf((float) this.currentCompactedKVs / this.totalCompactingKVs); + } + sb = Strings.appendKeyValue(sb, "compactionProgressPct", compactionProgressPct); + + String[] coprocessorStrings = getAllCoprocessors(this); + if (coprocessorStrings != null) { + sb = Strings.appendKeyValue(sb, "coprocessors", Arrays.toString(coprocessorStrings)); + } + return sb.toString(); + } + public static final ServerLoad EMPTY_SERVERLOAD = new ServerLoad(HBaseProtos.ServerLoad.newBuilder().build()); } diff --git src/test/java/org/apache/hadoop/hbase/TestServerLoad.java src/test/java/org/apache/hadoop/hbase/TestServerLoad.java new file mode 100644 index 0000000..c2bb903 --- /dev/null +++ src/test/java/org/apache/hadoop/hbase/TestServerLoad.java @@ -0,0 +1,83 @@ +/** + * Copyright The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase; + +import static org.junit.Assert.*; + +import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import com.google.protobuf.ByteString; + +@Category(SmallTests.class) +public class TestServerLoad { + + @Test + public void testRegionLoadAggregation() { + ServerLoad sl = new ServerLoad(createServerLoadProto()); + assertEquals(13, sl.getStores()); + assertEquals(114, sl.getStorefiles()); + assertEquals(129, sl.getStoreUncompressedSizeMB()); + assertEquals(504, sl.getRootIndexSizeKB()); + assertEquals(820, sl.getStorefileSizeMB()); + assertEquals(82, sl.getStorefileIndexSizeMB()); + assertEquals(0, sl.getReadRequestsCount()); + + } + + @Test + public void testToString() { + ServerLoad sl = new ServerLoad(createServerLoadProto()); + String slToString = sl.toString(); + assertTrue(slToString.contains("numberOfStores=13")); + assertTrue(slToString.contains("numberOfStorefiles=114")); + assertTrue(slToString.contains("storefileUncompressedSizeMB=129")); + assertTrue(slToString.contains("storefileSizeMB=820")); + assertTrue(slToString.contains("rootIndexSizeKB=504")); + assertTrue(slToString.contains("coprocessors=[]")); + } + + private HBaseProtos.ServerLoad createServerLoadProto() { + HBaseProtos.RegionSpecifier rSpecOne = + HBaseProtos.RegionSpecifier.newBuilder() + .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.ENCODED_REGION_NAME) + .setValue(ByteString.copyFromUtf8("ASDFGQWERT")).build(); + HBaseProtos.RegionSpecifier rSpecTwo = + HBaseProtos.RegionSpecifier.newBuilder() + .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.ENCODED_REGION_NAME) + .setValue(ByteString.copyFromUtf8("QWERTYUIOP")).build(); + + HBaseProtos.RegionLoad rlOne = + HBaseProtos.RegionLoad.newBuilder().setRegionSpecifier(rSpecOne).setStores(10) + .setStorefiles(101).setStoreUncompressedSizeMB(106).setStorefileSizeMB(520) + .setStorefileIndexSizeMB(42).setRootIndexSizeKB(201).build(); + HBaseProtos.RegionLoad rlTwo = + HBaseProtos.RegionLoad.newBuilder().setRegionSpecifier(rSpecTwo).setStores(3) + .setStorefiles(13).setStoreUncompressedSizeMB(23).setStorefileSizeMB(300) + .setStorefileIndexSizeMB(40).setRootIndexSizeKB(303).build(); + + HBaseProtos.ServerLoad sl = + HBaseProtos.ServerLoad.newBuilder().addRegionLoads(rlOne).addRegionLoads(rlTwo).build(); + return sl; + } + +} \ No newline at end of file