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