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      ServerName metaLocation = null;
63      List<ServerName> servers = null;
64      Set<ServerName> deadServers = null;
65      
66      if(master.isActiveMaster()) {
67        if (master.getServerManager() == null) {
68          response.sendError(503, "Master not ready");
69          return;
70        }
71        metaLocation = getMetaLocationOrNull(master);
72        //ServerName metaLocation = master.getCatalogTracker().getMetaLocation();
73        servers = master.getServerManager().getOnlineServersList();
74        deadServers = master.getServerManager().getDeadServers().copyServerNames();
75      }
76      
77      response.setContentType("text/html");
78      MasterStatusTmpl tmpl;
79      try {
80         tmpl = new MasterStatusTmpl()
81        .setFrags(frags)
82        .setMetaLocation(metaLocation)
83        .setServers(servers)
84        .setDeadServers(deadServers)
85        .setCatalogJanitorEnabled(master.isCatalogJanitorEnabled(null,
86            RequestConverter.buildIsCatalogJanitorEnabledRequest()).getValue());
87      } catch (ServiceException s) {
88        admin.close();
89        throw new IOException(s);
90      }
91      if (request.getParameter("filter") != null)
92        tmpl.setFilter(request.getParameter("filter"));
93      if (request.getParameter("format") != null)
94        tmpl.setFormat(request.getParameter("format"));
95      tmpl.render(response.getWriter(),
96            master, admin);
97    }
98  
99    private ServerName getMetaLocationOrNull(HMaster master) {
100     try {
101       return (master.getCatalogTracker() == null) ? null : master.getCatalogTracker().getMetaLocation();
102     } catch (InterruptedException e) {
103       LOG.warn("Unable to get meta location", e);
104       return null;
105     }
106   }
107 
108   private Map<String, Integer> getFragmentationInfo(
109       HMaster master, Configuration conf) throws IOException {
110     boolean showFragmentation = conf.getBoolean(
111         "hbase.master.ui.fragmentation.enabled", false);
112     if (showFragmentation) {
113       return FSUtils.getTableFragmentation(master);
114     } else {
115       return null;
116     }
117   }
118 }