001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.master;
020
021import java.io.IOException;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025
026import javax.servlet.http.HttpServlet;
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029
030import org.apache.yetus.audience.InterfaceAudience;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.hbase.ServerName;
033import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
034import org.apache.hadoop.hbase.util.FSUtils;
035import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
036
037/**
038 * The servlet responsible for rendering the index page of the
039 * master.
040 */
041@InterfaceAudience.Private
042public class MasterStatusServlet extends HttpServlet {
043  private static final long serialVersionUID = 1L;
044
045  @Override
046  public void doGet(HttpServletRequest request, HttpServletResponse response)
047    throws IOException
048  {
049    HMaster master = (HMaster) getServletContext().getAttribute(HMaster.MASTER);
050    assert master != null : "No Master in context!";
051
052    response.setContentType("text/html");
053
054    Configuration conf = master.getConfiguration();
055
056    Map<String, Integer> frags = getFragmentationInfo(master, conf);
057    ServerName metaLocation = null;
058    List<ServerName> servers = null;
059    Set<ServerName> deadServers = null;
060    
061    if(master.isActiveMaster()) {
062      metaLocation = getMetaLocationOrNull(master);
063      ServerManager serverManager = master.getServerManager();
064      if (serverManager != null) {
065        deadServers = serverManager.getDeadServers().copyServerNames();
066        servers = serverManager.getOnlineServersList();
067      }
068    }
069
070    MasterStatusTmpl tmpl = new MasterStatusTmpl()
071      .setFrags(frags)
072      .setMetaLocation(metaLocation)
073      .setServers(servers)
074      .setDeadServers(deadServers)
075      .setCatalogJanitorEnabled(master.isCatalogJanitorEnabled());
076
077    if (request.getParameter("filter") != null)
078      tmpl.setFilter(request.getParameter("filter"));
079    if (request.getParameter("format") != null)
080      tmpl.setFormat(request.getParameter("format"));
081    tmpl.render(response.getWriter(), master);
082  }
083
084  private ServerName getMetaLocationOrNull(HMaster master) {
085    MetaTableLocator metaTableLocator = master.getMetaTableLocator();
086    return metaTableLocator == null ? null :
087      metaTableLocator.getMetaRegionLocation(master.getZooKeeper());
088  }
089
090  private Map<String, Integer> getFragmentationInfo(
091      HMaster master, Configuration conf) throws IOException {
092    boolean showFragmentation = conf.getBoolean(
093        "hbase.master.ui.fragmentation.enabled", false);
094    if (showFragmentation) {
095      return FSUtils.getTableFragmentation(master);
096    } else {
097      return null;
098    }
099  }
100}