View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master;
20  
21  import static org.junit.Assert.*;
22  
23  import java.io.IOException;
24  import java.io.StringWriter;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.NavigableMap;
28  import java.util.Set;
29  import java.util.regex.Matcher;
30  import java.util.regex.Pattern;
31  
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.hbase.*;
34  import org.apache.hadoop.hbase.client.HBaseAdmin;
35  import org.apache.hadoop.hbase.testclassification.MediumTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
38  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
39  import org.apache.hadoop.hbase.tmpl.master.AssignmentManagerStatusTmpl;
40  import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
41  import org.junit.Before;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  import org.mockito.Mockito;
45  
46  import com.google.common.collect.Lists;
47  import com.google.common.collect.Maps;
48  
49  /**
50   * Tests for the master status page and its template.
51   */
52  @Category(MediumTests.class)
53  public class TestMasterStatusServlet {
54    
55    private HMaster master;
56    private Configuration conf;
57    private HBaseAdmin admin;
58  
59    static final ServerName FAKE_HOST =
60        ServerName.valueOf("fakehost", 12345, 1234567890);
61    static final HTableDescriptor FAKE_TABLE =
62      new HTableDescriptor(TableName.valueOf("mytable"));
63    static final HRegionInfo FAKE_HRI =
64        new HRegionInfo(FAKE_TABLE.getTableName(),
65            Bytes.toBytes("a"), Bytes.toBytes("b"));
66  
67    @Before
68    public void setupBasicMocks() {
69      conf = HBaseConfiguration.create();
70  
71      master = Mockito.mock(HMaster.class);
72      Mockito.doReturn(FAKE_HOST).when(master).getServerName();
73      Mockito.doReturn(conf).when(master).getConfiguration();
74  
75      //Fake DeadServer
76      DeadServer deadServer = Mockito.mock(DeadServer.class);
77      // Fake serverManager
78      ServerManager serverManager = Mockito.mock(ServerManager.class);
79      Mockito.doReturn(1.0).when(serverManager).getAverageLoad();
80      Mockito.doReturn(serverManager).when(master).getServerManager();
81      Mockito.doReturn(deadServer).when(serverManager).getDeadServers();
82  
83      // Fake AssignmentManager and RIT
84      AssignmentManager am = Mockito.mock(AssignmentManager.class);
85      RegionStates rs = Mockito.mock(RegionStates.class);
86      NavigableMap<String, RegionState> regionsInTransition =
87        Maps.newTreeMap();
88      regionsInTransition.put("r1",
89        new RegionState(FAKE_HRI, RegionState.State.CLOSING, 12345L, FAKE_HOST));
90      Mockito.doReturn(rs).when(am).getRegionStates();
91      Mockito.doReturn(regionsInTransition).when(rs).getRegionsInTransition();
92      Mockito.doReturn(am).when(master).getAssignmentManager();
93      Mockito.doReturn(serverManager).when(master).getServerManager();
94  
95      // Fake ZKW
96      ZooKeeperWatcher zkw = Mockito.mock(ZooKeeperWatcher.class);
97      Mockito.doReturn("fakequorum").when(zkw).getQuorum();
98      Mockito.doReturn(zkw).when(master).getZooKeeperWatcher();
99  
100     // Fake MasterAddressTracker
101     MasterAddressTracker tracker = Mockito.mock(MasterAddressTracker.class);
102     Mockito.doReturn(tracker).when(master).getMasterAddressTracker();
103     Mockito.doReturn(FAKE_HOST).when(tracker).getMasterAddress();
104 
105     // Mock admin
106     admin = Mockito.mock(HBaseAdmin.class); 
107   }
108 
109   private void setupMockTables() throws IOException {
110     HTableDescriptor tables[] = new HTableDescriptor[] {
111         new HTableDescriptor(TableName.valueOf("foo")),
112         new HTableDescriptor(TableName.valueOf("bar"))
113     };
114     Mockito.doReturn(tables).when(admin).listTables();
115   }
116   
117   @Test
118   public void testStatusTemplateNoTables() throws IOException {
119     new MasterStatusTmpl().render(new StringWriter(),
120         master, admin);
121   }
122 
123   @Test
124   public void testStatusTemplateMetaAvailable() throws IOException {
125     setupMockTables();
126     
127     new MasterStatusTmpl()
128       .setMetaLocation(ServerName.valueOf("metaserver:123,12345"))
129       .render(new StringWriter(),
130         master, admin);
131   }
132 
133   @Test
134   public void testStatusTemplateWithServers() throws IOException {
135     setupMockTables();
136     
137     List<ServerName> servers = Lists.newArrayList(
138         ServerName.valueOf("rootserver:123,12345"),
139         ServerName.valueOf("metaserver:123,12345"));
140     Set<ServerName> deadServers = new HashSet<ServerName>(
141         Lists.newArrayList(
142             ServerName.valueOf("badserver:123,12345"),
143             ServerName.valueOf("uglyserver:123,12345"))
144     );
145 
146     new MasterStatusTmpl()
147       .setMetaLocation(ServerName.valueOf("metaserver:123,12345"))
148       .setServers(servers)
149       .setDeadServers(deadServers)
150       .render(new StringWriter(),
151         master, admin);
152   }
153   
154   @Test
155   public void testAssignmentManagerTruncatedList() throws IOException {
156     AssignmentManager am = Mockito.mock(AssignmentManager.class);
157     RegionStates rs = Mockito.mock(RegionStates.class);
158 
159     // Add 100 regions as in-transition
160     NavigableMap<String, RegionState> regionsInTransition =
161       Maps.newTreeMap();
162     for (byte i = 0; i < 100; i++) {
163       HRegionInfo hri = new HRegionInfo(FAKE_TABLE.getTableName(),
164           new byte[]{i}, new byte[]{(byte) (i+1)});
165       regionsInTransition.put(hri.getEncodedName(),
166         new RegionState(hri, RegionState.State.CLOSING, 12345L, FAKE_HOST));
167     }
168     // Add hbase:meta in transition as well
169     regionsInTransition.put(
170         HRegionInfo.FIRST_META_REGIONINFO.getEncodedName(),
171         new RegionState(HRegionInfo.FIRST_META_REGIONINFO,
172                         RegionState.State.CLOSING, 12345L, FAKE_HOST));
173     Mockito.doReturn(rs).when(am).getRegionStates();
174     Mockito.doReturn(regionsInTransition).when(rs).getRegionsInTransition();
175 
176     // Render to a string
177     StringWriter sw = new StringWriter();
178     new AssignmentManagerStatusTmpl()
179       .setLimit(50)
180       .render(sw, am);
181     String result = sw.toString();
182 
183     // Should always include META
184     assertTrue(result.contains(HRegionInfo.FIRST_META_REGIONINFO.getEncodedName()));
185     
186     // Make sure we only see 50 of them
187     Matcher matcher = Pattern.compile("CLOSING").matcher(result);
188     int count = 0;
189     while (matcher.find()) {
190       count++;
191     }
192     assertEquals(50, count);
193   }
194 
195 }
196