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;
025import javax.servlet.http.HttpServlet;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
031import org.apache.hadoop.hbase.util.FSUtils;
032import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * The servlet responsible for rendering the index page of the
037 * master.
038 */
039@InterfaceAudience.Private
040public class MasterStatusServlet extends HttpServlet {
041  private static final long serialVersionUID = 1L;
042
043  @Override
044  public void doGet(HttpServletRequest request, HttpServletResponse response)
045    throws IOException
046  {
047    HMaster master = (HMaster) getServletContext().getAttribute(HMaster.MASTER);
048    assert master != null : "No Master in context!";
049
050    response.setContentType("text/html");
051
052    Configuration conf = master.getConfiguration();
053
054    Map<String, Integer> frags = getFragmentationInfo(master, conf);
055    ServerName metaLocation = null;
056    List<ServerName> servers = null;
057    Set<ServerName> deadServers = null;
058    
059    if(master.isActiveMaster()) {
060      metaLocation = getMetaLocationOrNull(master);
061      ServerManager serverManager = master.getServerManager();
062      if (serverManager != null) {
063        deadServers = serverManager.getDeadServers().copyServerNames();
064        servers = serverManager.getOnlineServersList();
065      }
066    }
067
068    MasterStatusTmpl tmpl = new MasterStatusTmpl()
069      .setFrags(frags)
070      .setMetaLocation(metaLocation)
071      .setServers(servers)
072      .setDeadServers(deadServers)
073      .setCatalogJanitorEnabled(master.isCatalogJanitorEnabled());
074
075    if (request.getParameter("filter") != null)
076      tmpl.setFilter(request.getParameter("filter"));
077    if (request.getParameter("format") != null)
078      tmpl.setFormat(request.getParameter("format"));
079    tmpl.render(response.getWriter(), master);
080  }
081
082  private ServerName getMetaLocationOrNull(HMaster master) {
083    return MetaTableLocator.getMetaRegionLocation(master.getZooKeeper());
084  }
085
086  private Map<String, Integer> getFragmentationInfo(
087      HMaster master, Configuration conf) throws IOException {
088    boolean showFragmentation = conf.getBoolean(
089        "hbase.master.ui.fragmentation.enabled", false);
090    if (showFragmentation) {
091      return FSUtils.getTableFragmentation(master);
092    } else {
093      return null;
094    }
095  }
096}