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      Mockito.doReturn(zkw).when(master).getZooKeeper();
100 
101     // Fake MasterAddressTracker
102     MasterAddressTracker tracker = Mockito.mock(MasterAddressTracker.class);
103     Mockito.doReturn(tracker).when(master).getMasterAddressTracker();
104     Mockito.doReturn(FAKE_HOST).when(tracker).getMasterAddress();
105 
106     // Mock admin
107     admin = Mockito.mock(HBaseAdmin.class); 
108   }
109 
110   private void setupMockTables() throws IOException {
111     HTableDescriptor tables[] = new HTableDescriptor[] {
112         new HTableDescriptor(TableName.valueOf("foo")),
113         new HTableDescriptor(TableName.valueOf("bar"))
114     };
115     Mockito.doReturn(tables).when(admin).listTables();
116   }
117   
118   @Test
119   public void testStatusTemplateNoTables() throws IOException {
120     new MasterStatusTmpl().render(new StringWriter(),
121         master, admin);
122   }
123 
124   @Test
125   public void testStatusTemplateMetaAvailable() throws IOException {
126     setupMockTables();
127     
128     new MasterStatusTmpl()
129       .setMetaLocation(ServerName.valueOf("metaserver:123,12345"))
130       .render(new StringWriter(),
131         master, admin);
132   }
133 
134   @Test
135   public void testStatusTemplateWithServers() throws IOException {
136     setupMockTables();
137     
138     List<ServerName> servers = Lists.newArrayList(
139         ServerName.valueOf("rootserver:123,12345"),
140         ServerName.valueOf("metaserver:123,12345"));
141     Set<ServerName> deadServers = new HashSet<ServerName>(
142         Lists.newArrayList(
143             ServerName.valueOf("badserver:123,12345"),
144             ServerName.valueOf("uglyserver:123,12345"))
145     );
146 
147     new MasterStatusTmpl()
148       .setMetaLocation(ServerName.valueOf("metaserver:123,12345"))
149       .setServers(servers)
150       .setDeadServers(deadServers)
151       .render(new StringWriter(),
152         master, admin);
153   }
154   
155   @Test
156   public void testAssignmentManagerTruncatedList() throws IOException {
157     AssignmentManager am = Mockito.mock(AssignmentManager.class);
158     RegionStates rs = Mockito.mock(RegionStates.class);
159 
160     // Add 100 regions as in-transition
161     NavigableMap<String, RegionState> regionsInTransition =
162       Maps.newTreeMap();
163     for (byte i = 0; i < 100; i++) {
164       HRegionInfo hri = new HRegionInfo(FAKE_TABLE.getTableName(),
165           new byte[]{i}, new byte[]{(byte) (i+1)});
166       regionsInTransition.put(hri.getEncodedName(),
167         new RegionState(hri, RegionState.State.CLOSING, 12345L, FAKE_HOST));
168     }
169     // Add hbase:meta in transition as well
170     regionsInTransition.put(
171         HRegionInfo.FIRST_META_REGIONINFO.getEncodedName(),
172         new RegionState(HRegionInfo.FIRST_META_REGIONINFO,
173                         RegionState.State.CLOSING, 12345L, FAKE_HOST));
174     Mockito.doReturn(rs).when(am).getRegionStates();
175     Mockito.doReturn(regionsInTransition).when(rs).getRegionsInTransition();
176 
177     // Render to a string
178     StringWriter sw = new StringWriter();
179     new AssignmentManagerStatusTmpl()
180       .setLimit(50)
181       .render(sw, am);
182     String result = sw.toString();
183 
184     // Should always include META
185     assertTrue(result.contains(HRegionInfo.FIRST_META_REGIONINFO.getEncodedName()));
186     
187     // Make sure we only see 50 of them
188     Matcher matcher = Pattern.compile("CLOSING").matcher(result);
189     int count = 0;
190     while (matcher.find()) {
191       count++;
192     }
193     assertEquals(50, count);
194   }
195 
196 }
197