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.HashSet;
024import java.util.List;
025import java.util.Optional;
026import java.util.Set;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseConfiguration;
030import org.apache.hadoop.hbase.HRegionInfo;
031import org.apache.hadoop.hbase.HTableDescriptor;
032import org.apache.hadoop.hbase.ServerName;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.Admin;
035import org.apache.hadoop.hbase.master.DeadServer;
036import org.apache.hadoop.hbase.master.HMaster;
037import org.apache.hadoop.hbase.master.RegionState;
038import org.apache.hadoop.hbase.master.ServerManager;
039import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
040import org.apache.hadoop.hbase.master.assignment.RegionStates;
041import org.apache.hadoop.hbase.regionserver.MetricsRegionServer;
042import org.apache.hadoop.hbase.regionserver.MetricsRegionServerWrapperStub;
043import org.apache.hadoop.hbase.testclassification.MasterTests;
044import org.apache.hadoop.hbase.testclassification.MediumTests;
045import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
046import org.apache.hadoop.hbase.util.Bytes;
047import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
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 HTableDescriptor FAKE_TABLE = new HTableDescriptor(TableName.valueOf("mytable"));
074  static final HRegionInfo FAKE_HRI =
075    new HRegionInfo(FAKE_TABLE.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("b"));
076
077  @Before
078  public void setupBasicMocks() {
079    conf = HBaseConfiguration.create();
080
081    master = Mockito.mock(HMaster.class);
082    Mockito.doReturn(FAKE_HOST).when(master).getServerName();
083    Mockito.doReturn(conf).when(master).getConfiguration();
084
085    // Fake DeadServer
086    DeadServer deadServer = Mockito.mock(DeadServer.class);
087    // Fake serverManager
088    ServerManager serverManager = Mockito.mock(ServerManager.class);
089    Mockito.doReturn(1.0).when(serverManager).getAverageLoad();
090    Mockito.doReturn(serverManager).when(master).getServerManager();
091    Mockito.doReturn(deadServer).when(serverManager).getDeadServers();
092
093    // Fake AssignmentManager and RIT
094    AssignmentManager am = Mockito.mock(AssignmentManager.class);
095    RegionStates rs = Mockito.mock(RegionStates.class);
096    List<RegionState> regionsInTransition = new ArrayList<>();
097    regionsInTransition
098      .add(new RegionState(FAKE_HRI, RegionState.State.CLOSING, 12345L, FAKE_HOST));
099    Mockito.doReturn(rs).when(am).getRegionStates();
100    Mockito.doReturn(regionsInTransition).when(rs).getRegionsInTransition();
101    Mockito.doReturn(am).when(master).getAssignmentManager();
102    Mockito.doReturn(serverManager).when(master).getServerManager();
103
104    // Fake ZKW
105    ZKWatcher zkw = Mockito.mock(ZKWatcher.class);
106    Mockito.doReturn(new ZNodePaths(conf)).when(zkw).getZNodePaths();
107    Mockito.doReturn("fakequorum").when(zkw).getQuorum();
108    Mockito.doReturn(zkw).when(master).getZooKeeper();
109
110    // Fake MasterAddressTracker
111    MasterAddressTracker tracker = Mockito.mock(MasterAddressTracker.class);
112    Mockito.doReturn(tracker).when(master).getMasterAddressTracker();
113    Mockito.doReturn(FAKE_HOST).when(tracker).getMasterAddress();
114
115    // Fake ActiveMaster
116    Mockito.doReturn(Optional.of(FAKE_HOST)).when(master).getActiveMaster();
117
118    MetricsRegionServer rms = Mockito.mock(MetricsRegionServer.class);
119    Mockito.doReturn(new MetricsRegionServerWrapperStub()).when(rms).getRegionServerWrapper();
120    Mockito.doReturn(rms).when(master).getMetrics();
121
122    // Mock admin
123    admin = Mockito.mock(Admin.class);
124  }
125
126  private void setupMockTables() throws IOException {
127    HTableDescriptor tables[] =
128      new HTableDescriptor[] { new HTableDescriptor(TableName.valueOf("foo")),
129        new HTableDescriptor(TableName.valueOf("bar")) };
130    Mockito.doReturn(tables).when(admin).listTables();
131  }
132
133  @Test
134  public void testStatusTemplateNoTables() throws IOException {
135    new MasterStatusTmpl().render(new StringWriter(), master);
136  }
137
138  @Test
139  public void testStatusTemplateMetaAvailable() throws IOException {
140    setupMockTables();
141
142    new MasterStatusTmpl().setMetaLocation(ServerName.valueOf("metaserver,123,12345"))
143      .render(new StringWriter(), master);
144  }
145
146  @Test
147  public void testStatusTemplateWithServers() throws IOException {
148    setupMockTables();
149
150    List<ServerName> servers = Lists.newArrayList(ServerName.valueOf("rootserver,123,12345"),
151      ServerName.valueOf("metaserver,123,12345"));
152    Set<ServerName> deadServers =
153      new HashSet<>(Lists.newArrayList(ServerName.valueOf("badserver,123,12345"),
154        ServerName.valueOf("uglyserver,123,12345")));
155
156    new MasterStatusTmpl().setMetaLocation(ServerName.valueOf("metaserver,123,12345"))
157      .setServers(servers).setDeadServers(deadServers).render(new StringWriter(), master);
158  }
159}