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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.io.ByteArrayInputStream;
026import java.io.IOException;
027import javax.xml.bind.JAXBContext;
028import javax.xml.bind.JAXBException;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.Waiter;
033import org.apache.hadoop.hbase.rest.client.Client;
034import org.apache.hadoop.hbase.rest.client.Cluster;
035import org.apache.hadoop.hbase.rest.client.Response;
036import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.testclassification.RestTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.junit.jupiter.api.AfterAll;
041import org.junit.jupiter.api.BeforeAll;
042import org.junit.jupiter.api.Tag;
043import org.junit.jupiter.api.Test;
044
045@Tag(RestTests.TAG)
046@Tag(MediumTests.TAG)
047public class TestStatusResource {
048
049  private static final byte[] META_REGION_NAME = Bytes.toBytes(TableName.META_TABLE_NAME + ",,1");
050
051  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
052  private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
053  private static Client client;
054  private static JAXBContext context;
055  private static Configuration conf;
056
057  private static void validate(StorageClusterStatusModel model) {
058    assertNotNull(model);
059    assertTrue(model.getRegions() >= 1, model.getRegions() + ">= 1");
060    assertTrue(model.getRequests() >= 0);
061    assertTrue(model.getAverageLoad() >= 0.0);
062    assertNotNull(model.getLiveNodes());
063    assertNotNull(model.getDeadNodes());
064    assertFalse(model.getLiveNodes().isEmpty());
065    boolean foundMeta = false;
066    for (StorageClusterStatusModel.Node node : model.getLiveNodes()) {
067      assertNotNull(node.getName());
068      assertTrue(node.getStartCode() > 0L);
069      assertTrue(node.getRequests() >= 0);
070      for (StorageClusterStatusModel.Node.Region region : node.getRegions()) {
071        if (Bytes.equals(region.getName(), META_REGION_NAME)) {
072          foundMeta = true;
073        }
074      }
075    }
076    assertTrue(foundMeta);
077  }
078
079  @BeforeAll
080  public static void setUpBeforeClass() throws Exception {
081    conf = TEST_UTIL.getConfiguration();
082    TEST_UTIL.startMiniCluster();
083    TEST_UTIL.createTable(TableName.valueOf("TestStatusResource"), Bytes.toBytes("D"));
084    TEST_UTIL.createTable(TableName.valueOf("TestStatusResource2"), Bytes.toBytes("D"));
085    REST_TEST_UTIL.startServletContainer(conf);
086    Cluster cluster = new Cluster();
087    cluster.add("localhost", REST_TEST_UTIL.getServletPort());
088    client = new Client(cluster);
089    context = JAXBContext.newInstance(StorageClusterStatusModel.class);
090    TEST_UTIL.waitFor(6000, new Waiter.Predicate<IOException>() {
091      @Override
092      public boolean evaluate() throws IOException {
093        return TEST_UTIL.getMiniHBaseCluster().getClusterMetrics().getAverageLoad() > 0;
094      }
095    });
096  }
097
098  @AfterAll
099  public static void tearDownAfterClass() throws Exception {
100    REST_TEST_UTIL.shutdownServletContainer();
101    TEST_UTIL.shutdownMiniCluster();
102  }
103
104  @Test
105  public void testGetClusterStatusXML() throws IOException, JAXBException {
106    Response response = client.get("/status/cluster", Constants.MIMETYPE_XML);
107    assertEquals(200, response.getCode());
108    assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
109    StorageClusterStatusModel model = (StorageClusterStatusModel) context.createUnmarshaller()
110      .unmarshal(new ByteArrayInputStream(response.getBody()));
111    validate(model);
112  }
113
114  @Test
115  public void testGetClusterStatusPB() throws IOException {
116    Response response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF);
117    assertEquals(200, response.getCode());
118    assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
119    StorageClusterStatusModel model = new StorageClusterStatusModel();
120    model.getObjectFromMessage(response.getBody());
121    validate(model);
122    response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF_IETF);
123    assertEquals(200, response.getCode());
124    assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
125    model = new StorageClusterStatusModel();
126    model.getObjectFromMessage(response.getBody());
127    validate(model);
128  }
129}