001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest.model;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.Iterator;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.testclassification.RestTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.ClassRule;
031import org.junit.experimental.categories.Category;
032
033@Category({ RestTests.class, SmallTests.class })
034public class TestStorageClusterStatusModel extends TestModelBase<StorageClusterStatusModel> {
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037    HBaseClassTestRule.forClass(TestStorageClusterStatusModel.class);
038
039  public TestStorageClusterStatusModel() throws Exception {
040    super(StorageClusterStatusModel.class);
041
042    AS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
043      + "<ClusterStatus averageLoad=\"1.0\" regions=\"2\" requests=\"0\">"
044      + "<DeadNodes/><LiveNodes>"
045      + "<Node heapSizeMB=\"128\" maxHeapSizeMB=\"1024\" name=\"test1\" requests=\"0\" "
046      + "startCode=\"1245219839331\"><Region currentCompactedKVs=\"1\" memstoreSizeMB=\"0\" "
047      + "name=\"aGJhc2U6cm9vdCwsMA==\" readRequestsCount=\"1\" rootIndexSizeKB=\"1\" "
048      + "storefileIndexSizeKB=\"0\" storefileSizeMB=\"0\" storefiles=\"1\" stores=\"1\" "
049      + "totalCompactingKVs=\"1\" totalStaticBloomSizeKB=\"1\" totalStaticIndexSizeKB=\"1\" "
050      + "writeRequestsCount=\"2\"/></Node>"
051      + "<Node heapSizeMB=\"512\" maxHeapSizeMB=\"1024\" name=\"test2\" requests=\"0\" "
052      + "startCode=\"1245239331198\">" + "<Region currentCompactedKVs=\"1\" memstoreSizeMB=\"0\" "
053      + "name=\"aGJhc2U6bWV0YSwsMTI0NjAwMDA0MzcyNA==\" readRequestsCount=\"1\" "
054      + "rootIndexSizeKB=\"1\" storefileIndexSizeKB=\"0\" storefileSizeMB=\"0\" "
055      + "storefiles=\"1\" stores=\"1\" totalCompactingKVs=\"1\" totalStaticBloomSizeKB=\"1\" "
056      + "totalStaticIndexSizeKB=\"1\" writeRequestsCount=\"2\"/></Node></LiveNodes>"
057      + "</ClusterStatus>";
058
059    AS_PB = "Cj8KBXRlc3QxEOO6i+eeJBgAIIABKIAIMicKDWhiYXNlOnJvb3QsLDAQARgBIAAoADAAOAFAAkgB"
060      + "UAFYAWABaAEKSwoFdGVzdDIQ/pKx8J4kGAAggAQogAgyMwoZaGJhc2U6bWV0YSwsMTI0NjAwMDA0"
061      + "MzcyNBABGAEgACgAMAA4AUACSAFQAVgBYAFoARgCIAApAAAAAAAA8D8=";
062
063    // Using jackson will break json backward compatibilty for this representation
064    // but the original one was broken as it would only print one Node element
065    // so the format itself was broken
066    AS_JSON =
067      "{\"regions\":2,\"requests\":0,\"averageLoad\":1.0,\"LiveNodes\":[{\"name\":\"test1\","
068        + "\"Region\":[{\"name\":\"aGJhc2U6cm9vdCwsMA==\",\"stores\":1,\"storefiles\":1,"
069        + "\"storefileSizeMB\":0,\"memStoreSizeMB\":0,\"storefileIndexSizeKB\":0,"
070        + "\"readRequestsCount\":1,\"writeRequestsCount\":2,\"rootIndexSizeKB\":1,"
071        + "\"totalStaticIndexSizeKB\":1,\"totalStaticBloomSizeKB\":1,\"totalCompactingKVs\":1,"
072        + "\"currentCompactedKVs\":1}],\"requests\":0,\"startCode\":1245219839331,"
073        + "\"heapSizeMB\":128,\"maxHeapSizeMB\":1024},{\"name\":\"test2\","
074        + "\"Region\":[{\"name\":\"aGJhc2U6bWV0YSwsMTI0NjAwMDA0MzcyNA==\",\"stores\":1,"
075        + "\"storefiles\":1,\"storefileSizeMB\":0,\"memStoreSizeMB\":0,\"storefileIndexSizeKB\":0,"
076        + "\"readRequestsCount\":1,\"writeRequestsCount\":2,\"rootIndexSizeKB\":1,"
077        + "\"totalStaticIndexSizeKB\":1,\"totalStaticBloomSizeKB\":1,\"totalCompactingKVs\":1,"
078        + "\"currentCompactedKVs\":1}],\"requests\":0,\"startCode\":1245239331198,"
079        + "\"heapSizeMB\":512,\"maxHeapSizeMB\":1024}],\"DeadNodes\":[]}";
080  }
081
082  @Override
083  protected StorageClusterStatusModel buildTestModel() {
084    StorageClusterStatusModel model = new StorageClusterStatusModel();
085    model.setRegions(2);
086    model.setRequests(0);
087    model.setAverageLoad(1.0);
088    model.addLiveNode("test1", 1245219839331L, 128, 1024).addRegion(Bytes.toBytes("hbase:root,,0"),
089      1, 1, 0, 0, 0, 1, 2, 1, 1, 1, 1, 1);
090    model.addLiveNode("test2", 1245239331198L, 512, 1024).addRegion(
091      Bytes.toBytes(TableName.META_TABLE_NAME + ",,1246000043724"), 1, 1, 0, 0, 0, 1, 2, 1, 1, 1, 1,
092      1);
093    return model;
094  }
095
096  @Override
097  protected void checkModel(StorageClusterStatusModel model) {
098    assertEquals(2, model.getRegions());
099    assertEquals(0, model.getRequests());
100    assertEquals(1.0, model.getAverageLoad(), 0.0);
101    Iterator<StorageClusterStatusModel.Node> nodes = model.getLiveNodes().iterator();
102    StorageClusterStatusModel.Node node = nodes.next();
103    assertEquals("test1", node.getName());
104    assertEquals(1245219839331L, node.getStartCode());
105    assertEquals(128, node.getHeapSizeMB());
106    assertEquals(1024, node.getMaxHeapSizeMB());
107    Iterator<StorageClusterStatusModel.Node.Region> regions = node.getRegions().iterator();
108    StorageClusterStatusModel.Node.Region region = regions.next();
109    assertTrue(Bytes.toString(region.getName()).equals("hbase:root,,0"));
110    assertEquals(1, region.getStores());
111    assertEquals(1, region.getStorefiles());
112    assertEquals(0, region.getStorefileSizeMB());
113    assertEquals(0, region.getMemStoreSizeMB());
114    assertEquals(0, region.getStorefileIndexSizeKB());
115    assertEquals(1, region.getReadRequestsCount());
116    assertEquals(2, region.getWriteRequestsCount());
117    assertEquals(1, region.getRootIndexSizeKB());
118    assertEquals(1, region.getTotalStaticIndexSizeKB());
119    assertEquals(1, region.getTotalStaticBloomSizeKB());
120    assertEquals(1, region.getTotalCompactingKVs());
121    assertEquals(1, region.getCurrentCompactedKVs());
122    assertFalse(regions.hasNext());
123    node = nodes.next();
124    assertEquals("test2", node.getName());
125    assertEquals(1245239331198L, node.getStartCode());
126    assertEquals(512, node.getHeapSizeMB());
127    assertEquals(1024, node.getMaxHeapSizeMB());
128    regions = node.getRegions().iterator();
129    region = regions.next();
130    assertEquals(Bytes.toString(region.getName()), TableName.META_TABLE_NAME + ",,1246000043724");
131    assertEquals(1, region.getStores());
132    assertEquals(1, region.getStorefiles());
133    assertEquals(0, region.getStorefileSizeMB());
134    assertEquals(0, region.getMemStoreSizeMB());
135    assertEquals(0, region.getStorefileIndexSizeKB());
136    assertEquals(1, region.getReadRequestsCount());
137    assertEquals(2, region.getWriteRequestsCount());
138    assertEquals(1, region.getRootIndexSizeKB());
139    assertEquals(1, region.getTotalStaticIndexSizeKB());
140    assertEquals(1, region.getTotalStaticBloomSizeKB());
141    assertEquals(1, region.getTotalCompactingKVs());
142    assertEquals(1, region.getCurrentCompactedKVs());
143
144    assertFalse(regions.hasNext());
145    assertFalse(nodes.hasNext());
146  }
147}