View Javadoc

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 java.io.IOException;
23  
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.conf.Configuration;
35  import org.apache.hadoop.hbase.ServerName;
36  import org.apache.hadoop.hbase.client.HBaseAdmin;
37  import org.apache.hadoop.hbase.util.FSUtils;
38  import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
39  
40  /**
41   * The servlet responsible for rendering the index page of the
42   * master.
43   */
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      Configuration conf = master.getConfiguration();
56      HBaseAdmin admin = new HBaseAdmin(conf);    
57          
58      Map<String, Integer> frags = getFragmentationInfo(master, conf);
59      
60      ServerName rootLocation = getRootLocationOrNull(master);
61      ServerName metaLocation = master.getCatalogTracker().getMetaLocation();
62      List<ServerName> servers = master.getServerManager().getOnlineServersList();
63      Set<ServerName> deadServers = master.getServerManager().getDeadServers();
64  
65      response.setContentType("text/html");
66      MasterStatusTmpl tmpl = new MasterStatusTmpl()
67        .setFrags(frags)
68        .setShowAppendWarning(shouldShowAppendWarning(conf))
69        .setRootLocation(rootLocation)
70        .setMetaLocation(metaLocation)
71        .setServers(servers)
72        .setDeadServers(deadServers);
73      if (request.getParameter("filter") != null)
74        tmpl.setFilter(request.getParameter("filter"));
75      if (request.getParameter("format") != null)
76        tmpl.setFormat(request.getParameter("format"));
77      tmpl.render(response.getWriter(),
78            master, admin);
79    }
80  
81    private ServerName getRootLocationOrNull(HMaster master) {
82      try {
83        return master.getCatalogTracker().getRootLocation();
84      } catch (InterruptedException e) {
85        LOG.warn("Unable to get root location", e);
86        return null;
87      }
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 
101   static boolean shouldShowAppendWarning(Configuration conf) {
102     try {
103       return !FSUtils.isAppendSupported(conf) && FSUtils.isHDFS(conf);
104     } catch (IOException e) {
105       LOG.warn("Unable to determine if append is supported", e);
106       return false;
107     }
108   }
109 }