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,\"cpRequestsCount\":1,\"writeRequestsCount\":2,"
071        + "\"rootIndexSizeKB\":1,\"totalStaticIndexSizeKB\":1,\"totalStaticBloomSizeKB\":1,"
072        + "\"totalCompactingKVs\":1,\"currentCompactedKVs\":1}],\"requests\":0,"
073        + "\"startCode\":1245219839331,\"heapSizeMB\":128,\"maxHeapSizeMB\":1024},"
074        + "{\"name\":\"test2\",\"Region\":[{\"name\":\"aGJhc2U6bWV0YSwsMTI0NjAwMDA0MzcyNA==\","
075        + "\"stores\":1,\"storefiles\":1,\"storefileSizeMB\":0,\"memStoreSizeMB\":0,"
076        + "\"storefileIndexSizeKB\":0,\"readRequestsCount\":1,\"cpRequestsCount\":1,"
077        + "\"writeRequestsCount\":2,\"rootIndexSizeKB\":1,\"totalStaticIndexSizeKB\":1,"
078        + "\"totalStaticBloomSizeKB\":1,\"totalCompactingKVs\":1,\"currentCompactedKVs\":1}],"
079        + "\"requests\":0,\"startCode\":1245239331198,\"heapSizeMB\":512,"
080        + "\"maxHeapSizeMB\":1024}],\"DeadNodes\":[]}";
081  }
082
083  @Override
084  protected StorageClusterStatusModel buildTestModel() {
085    StorageClusterStatusModel model = new StorageClusterStatusModel();
086    model.setRegions(2);
087    model.setRequests(0);
088    model.setAverageLoad(1.0);
089    model.addLiveNode("test1", 1245219839331L, 128, 1024).addRegion(Bytes.toBytes("hbase:root,,0"),
090      1, 1, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1);
091    model.addLiveNode("test2", 1245239331198L, 512, 1024).addRegion(
092      Bytes.toBytes(TableName.META_TABLE_NAME + ",,1246000043724"), 1, 1, 0, 0, 0, 1, 1, 2, 1, 1, 1,
093      1, 1);
094    return model;
095  }
096
097  @Override
098  protected void checkModel(StorageClusterStatusModel model) {
099    assertEquals(2, model.getRegions());
100    assertEquals(0, model.getRequests());
101    assertEquals(1.0, model.getAverageLoad(), 0.0);
102    Iterator<StorageClusterStatusModel.Node> nodes = model.getLiveNodes().iterator();
103    StorageClusterStatusModel.Node node = nodes.next();
104    assertEquals("test1", node.getName());
105    assertEquals(1245219839331L, node.getStartCode());
106    assertEquals(128, node.getHeapSizeMB());
107    assertEquals(1024, node.getMaxHeapSizeMB());
108    Iterator<StorageClusterStatusModel.Node.Region> regions = node.getRegions().iterator();
109    StorageClusterStatusModel.Node.Region region = regions.next();
110    assertTrue(Bytes.toString(region.getName()).equals("hbase:root,,0"));
111    assertEquals(1, region.getStores());
112    assertEquals(1, region.getStorefiles());
113    assertEquals(0, region.getStorefileSizeMB());
114    assertEquals(0, region.getMemStoreSizeMB());
115    assertEquals(0, region.getStorefileIndexSizeKB());
116    assertEquals(1, region.getReadRequestsCount());
117    assertEquals(2, region.getWriteRequestsCount());
118    assertEquals(1, region.getRootIndexSizeKB());
119    assertEquals(1, region.getTotalStaticIndexSizeKB());
120    assertEquals(1, region.getTotalStaticBloomSizeKB());
121    assertEquals(1, region.getTotalCompactingKVs());
122    assertEquals(1, region.getCurrentCompactedKVs());
123    assertFalse(regions.hasNext());
124    node = nodes.next();
125    assertEquals("test2", node.getName());
126    assertEquals(1245239331198L, node.getStartCode());
127    assertEquals(512, node.getHeapSizeMB());
128    assertEquals(1024, node.getMaxHeapSizeMB());
129    regions = node.getRegions().iterator();
130    region = regions.next();
131    assertEquals(Bytes.toString(region.getName()), TableName.META_TABLE_NAME + ",,1246000043724");
132    assertEquals(1, region.getStores());
133    assertEquals(1, region.getStorefiles());
134    assertEquals(0, region.getStorefileSizeMB());
135    assertEquals(0, region.getMemStoreSizeMB());
136    assertEquals(0, region.getStorefileIndexSizeKB());
137    assertEquals(1, region.getReadRequestsCount());
138    assertEquals(2, region.getWriteRequestsCount());
139    assertEquals(1, region.getRootIndexSizeKB());
140    assertEquals(1, region.getTotalStaticIndexSizeKB());
141    assertEquals(1, region.getTotalStaticBloomSizeKB());
142    assertEquals(1, region.getTotalCompactingKVs());
143    assertEquals(1, region.getCurrentCompactedKVs());
144
145    assertFalse(regions.hasNext());
146    assertFalse(nodes.hasNext());
147  }
148}