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  
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.classification.InterfaceAudience;
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.protobuf.RequestConverter;
39  import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
40  import com.google.protobuf.ServiceException;
41  
42  /**
43   * The servlet responsible for rendering the index page of the
44   * master.
45   */
46  @InterfaceAudience.Private
47  public class MasterStatusServlet extends HttpServlet {
48    private static final Log LOG = LogFactory.getLog(MasterStatusServlet.class);
49    private static final long serialVersionUID = 1L;
50  
51    @Override
52    public void doGet(HttpServletRequest request, HttpServletResponse response)
53      throws IOException
54    {
55      HMaster master = (HMaster) getServletContext().getAttribute(HMaster.MASTER);
56      assert master != null : "No Master in context!";
57      
58      Configuration conf = master.getConfiguration();
59      HBaseAdmin admin = new HBaseAdmin(conf);    
60          
61      Map<String, Integer> frags = getFragmentationInfo(master, conf);
62      
63      ServerName metaLocation = getMetaLocationOrNull(master);
64      //ServerName metaLocation = master.getCatalogTracker().getMetaLocation();
65      List<ServerName> servers = master.getServerManager().getOnlineServersList();
66      Set<ServerName> deadServers = master.getServerManager().getDeadServers().copyServerNames();
67  
68      response.setContentType("text/html");
69      MasterStatusTmpl tmpl;
70      try {
71         tmpl = new MasterStatusTmpl()
72        .setFrags(frags)
73        .setShowAppendWarning(shouldShowAppendWarning(conf))
74        .setMetaLocation(metaLocation)
75        .setServers(servers)
76        .setDeadServers(deadServers)
77        .setCatalogJanitorEnabled(master.isCatalogJanitorEnabled(null,
78            RequestConverter.buildIsCatalogJanitorEnabledRequest()).getValue());
79      } catch (ServiceException s) {
80        throw new IOException(s);
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      try {
92        return master.getCatalogTracker().getMetaLocation();
93      } catch (InterruptedException e) {
94        LOG.warn("Unable to get meta location", e);
95        return null;
96      }
97    }
98  
99    private Map<String, Integer> getFragmentationInfo(
100       HMaster master, Configuration conf) throws IOException {
101     boolean showFragmentation = conf.getBoolean(
102         "hbase.master.ui.fragmentation.enabled", false);    
103     if (showFragmentation) {
104       return FSUtils.getTableFragmentation(master);
105     } else {
106       return null;
107     }
108   }
109 
110   static boolean shouldShowAppendWarning(Configuration conf) {
111     try {
112       return !FSUtils.isAppendSupported(conf) && FSUtils.isHDFS(conf);
113     } catch (IOException e) {
114       LOG.warn("Unable to determine if append is supported", e);
115       return false;
116     }
117   }
118 }