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 java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.client.HBaseAdmin;
34  import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
35  import org.apache.hadoop.hbase.util.FSUtils;
36  import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
37  
38  /**
39   * The servlet responsible for rendering the index page of the
40   * master.
41   */
42  @InterfaceAudience.Private
43  public class MasterStatusServlet extends HttpServlet {
44    private static final long serialVersionUID = 1L;
45  
46    @Override
47    public void doGet(HttpServletRequest request, HttpServletResponse response)
48      throws IOException
49    {
50      HMaster master = (HMaster) getServletContext().getAttribute(HMaster.MASTER);
51      assert master != null : "No Master in context!";
52  
53      response.setContentType("text/html");
54  
55      Configuration conf = master.getConfiguration();
56      HBaseAdmin admin = new HBaseAdmin(conf);
57  
58      Map<String, Integer> frags = getFragmentationInfo(master, conf);
59      ServerName metaLocation = null;
60      List<ServerName> servers = null;
61      Set<ServerName> deadServers = null;
62      
63      if(master.isActiveMaster()) {
64        metaLocation = getMetaLocationOrNull(master);
65        ServerManager serverManager = master.getServerManager();
66        if (serverManager != null) {
67          deadServers = serverManager.getDeadServers().copyServerNames();
68          servers = serverManager.getOnlineServersList();
69        }
70      }
71  
72      MasterStatusTmpl tmpl = new MasterStatusTmpl()
73        .setFrags(frags)
74        .setMetaLocation(metaLocation)
75        .setServers(servers)
76        .setDeadServers(deadServers)
77        .setCatalogJanitorEnabled(master.isCatalogJanitorEnabled());
78  
79      if (request.getParameter("filter") != null)
80        tmpl.setFilter(request.getParameter("filter"));
81      if (request.getParameter("format") != null)
82        tmpl.setFormat(request.getParameter("format"));
83      tmpl.render(response.getWriter(),
84            master, admin);
85    }
86  
87    private ServerName getMetaLocationOrNull(HMaster master) {
88      MetaTableLocator metaTableLocator = master.getMetaTableLocator();
89      return metaTableLocator == null ? null :
90        metaTableLocator.getMetaRegionLocation(master.getZooKeeper());
91    }
92  
93    private Map<String, Integer> getFragmentationInfo(
94        HMaster master, Configuration conf) throws IOException {
95      boolean showFragmentation = conf.getBoolean(
96          "hbase.master.ui.fragmentation.enabled", false);
97      if (showFragmentation) {
98        return FSUtils.getTableFragmentation(master);
99      } else {
100       return null;
101     }
102   }
103 }