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