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  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
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  public class MasterStatusServlet extends HttpServlet {
44    private static final Log LOG = LogFactory.getLog(MasterStatusServlet.class);
45    private static final long serialVersionUID = 1L;
46  
47    @Override
48    public void doGet(HttpServletRequest request, HttpServletResponse response)
49      throws IOException
50    {
51      HMaster master = (HMaster) getServletContext().getAttribute(HMaster.MASTER);
52      assert master != null : "No Master in context!";
53      
54      Configuration conf = master.getConfiguration();
55      HBaseAdmin admin = new HBaseAdmin(conf);    
56      ServerName metaLocation = null;
57      List<ServerName> servers = null;
58      Set<ServerName> deadServers = null;
59  
60      Map<String, Integer> frags = getFragmentationInfo(master, conf);
61      
62      ServerName rootLocation = getRootLocationOrNull(master);
63  
64      if (master.isActiveMaster()) {
65        metaLocation = master.getCatalogTracker().getMetaLocation();
66        servers = master.getServerManager().getOnlineServersList();
67        deadServers = master.getServerManager().getDeadServers();
68      }
69  
70      response.setContentType("text/html");
71      MasterStatusTmpl tmpl = new MasterStatusTmpl()
72        .setFrags(frags)
73        .setShowAppendWarning(shouldShowAppendWarning(conf))
74        .setRootLocation(rootLocation)
75        .setMetaLocation(metaLocation)
76        .setServers(servers)
77        .setDeadServers(deadServers);
78      if (request.getParameter("filter") != null)
79        tmpl.setFilter(request.getParameter("filter"));
80      if (request.getParameter("format") != null)
81        tmpl.setFormat(request.getParameter("format"));
82      tmpl.render(response.getWriter(),
83            master, admin);
84    }
85  
86    private ServerName getRootLocationOrNull(HMaster master) {
87      try {
88        return (master.getCatalogTracker() == null) ? null : master.getCatalogTracker()
89            .getRootLocation();
90      } catch (InterruptedException e) {
91        LOG.warn("Unable to get root location", e);
92        return null;
93      }
94    }
95  
96    private Map<String, Integer> getFragmentationInfo(
97        HMaster master, Configuration conf) throws IOException {
98      boolean showFragmentation = conf.getBoolean(
99          "hbase.master.ui.fragmentation.enabled", false);    
100     if (showFragmentation) {
101       return FSUtils.getTableFragmentation(master);
102     } else {
103       return null;
104     }
105   }
106 
107   static boolean shouldShowAppendWarning(Configuration conf) {
108     try {
109       return !FSUtils.isAppendSupported(conf) && FSUtils.isHDFS(conf);
110     } catch (IOException e) {
111       LOG.warn("Unable to determine if append is supported", e);
112       return false;
113     }
114   }
115 }