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.master.http;
019
020import java.io.IOException;
021import java.io.StringWriter;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Optional;
027import java.util.Set;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseConfiguration;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.Admin;
034import org.apache.hadoop.hbase.client.RegionInfo;
035import org.apache.hadoop.hbase.client.RegionInfoBuilder;
036import org.apache.hadoop.hbase.client.TableDescriptor;
037import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
038import org.apache.hadoop.hbase.master.DeadServer;
039import org.apache.hadoop.hbase.master.HMaster;
040import org.apache.hadoop.hbase.master.RegionState;
041import org.apache.hadoop.hbase.master.ServerManager;
042import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
043import org.apache.hadoop.hbase.master.assignment.RegionStates;
044import org.apache.hadoop.hbase.testclassification.MasterTests;
045import org.apache.hadoop.hbase.testclassification.MediumTests;
046import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
047import org.apache.hadoop.hbase.util.Bytes;
048import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
049import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
050import org.junit.Before;
051import org.junit.ClassRule;
052import org.junit.Test;
053import org.junit.experimental.categories.Category;
054import org.mockito.Mockito;
055
056import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
057
058/**
059 * Tests for the master status page and its template.
060 */
061@Category({ MasterTests.class, MediumTests.class })
062public class TestMasterStatusServlet {
063
064  @ClassRule
065  public static final HBaseClassTestRule CLASS_RULE =
066    HBaseClassTestRule.forClass(TestMasterStatusServlet.class);
067
068  private HMaster master;
069  private Configuration conf;
070  private Admin admin;
071
072  static final ServerName FAKE_HOST = ServerName.valueOf("fakehost", 12345, 1234567890);
073  static final TableDescriptor FAKE_TABLE =
074    TableDescriptorBuilder.newBuilder(TableName.valueOf("mytable")).build();
075
076  static final RegionInfo FAKE_HRI = RegionInfoBuilder.newBuilder(FAKE_TABLE.getTableName())
077    .setStartKey(Bytes.toBytes("a")).setEndKey(Bytes.toBytes("b")).build();
078
079  @Before
080  public void setupBasicMocks() {
081    conf = HBaseConfiguration.create();
082
083    master = Mockito.mock(HMaster.class);
084    Mockito.doReturn(FAKE_HOST).when(master).getServerName();
085    Mockito.doReturn(conf).when(master).getConfiguration();
086
087    // Fake DeadServer
088    DeadServer deadServer = Mockito.mock(DeadServer.class);
089    // Fake serverManager
090    ServerManager serverManager = Mockito.mock(ServerManager.class);
091    Mockito.doReturn(1.0).when(serverManager).getAverageLoad();
092    Mockito.doReturn(serverManager).when(master).getServerManager();
093    Mockito.doReturn(deadServer).when(serverManager).getDeadServers();
094
095    // Fake AssignmentManager and RIT
096    AssignmentManager am = Mockito.mock(AssignmentManager.class);
097    RegionStates rs = Mockito.mock(RegionStates.class);
098    List<RegionState> regionsInTransition = new ArrayList<>();
099    regionsInTransition
100      .add(new RegionState(FAKE_HRI, RegionState.State.CLOSING, 12345L, FAKE_HOST));
101    Mockito.doReturn(rs).when(am).getRegionStates();
102    Mockito.doReturn(regionsInTransition).when(rs).getRegionsInTransition();
103    Mockito.doReturn(am).when(master).getAssignmentManager();
104    Mockito.doReturn(serverManager).when(master).getServerManager();
105
106    // Fake ZKW
107    ZKWatcher zkw = Mockito.mock(ZKWatcher.class);
108    Mockito.doReturn(new ZNodePaths(conf)).when(zkw).getZNodePaths();
109    Mockito.doReturn("fakequorum").when(zkw).getQuorum();
110    Mockito.doReturn(zkw).when(master).getZooKeeper();
111
112    // Fake ActiveMaster
113    Mockito.doReturn(Optional.of(FAKE_HOST)).when(master).getActiveMaster();
114
115    // Mock admin
116    admin = Mockito.mock(Admin.class);
117  }
118
119  private void setupMockTables() throws IOException {
120    List<TableDescriptor> tables =
121      Arrays.asList(TableDescriptorBuilder.newBuilder(TableName.valueOf("foo")).build(),
122        TableDescriptorBuilder.newBuilder(TableName.valueOf("bar")).build());
123    Mockito.doReturn(tables).when(admin).listTableDescriptors();
124  }
125
126  @Test
127  public void testStatusTemplateNoTables() throws IOException {
128    new MasterStatusTmpl().render(new StringWriter(), master);
129  }
130
131  @Test
132  public void testStatusTemplateMetaAvailable() throws IOException {
133    setupMockTables();
134
135    new MasterStatusTmpl().setMetaLocation(ServerName.valueOf("metaserver,123,12345"))
136      .render(new StringWriter(), master);
137  }
138
139  @Test
140  public void testStatusTemplateWithServers() throws IOException {
141    setupMockTables();
142
143    List<ServerName> servers = Lists.newArrayList(ServerName.valueOf("rootserver,123,12345"),
144      ServerName.valueOf("metaserver,123,12345"));
145    Set<ServerName> deadServers =
146      new HashSet<>(Lists.newArrayList(ServerName.valueOf("badserver,123,12345"),
147        ServerName.valueOf("uglyserver,123,12345")));
148
149    new MasterStatusTmpl().setMetaLocation(ServerName.valueOf("metaserver,123,12345"))
150      .setServers(servers).setDeadServers(deadServers).render(new StringWriter(), master);
151  }
152}