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