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