001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master.http;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024import javax.servlet.http.HttpServlet;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.client.RegionInfoBuilder;
030import org.apache.hadoop.hbase.master.HMaster;
031import org.apache.hadoop.hbase.master.RegionState;
032import org.apache.hadoop.hbase.master.ServerManager;
033import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
034import org.apache.hadoop.hbase.tmpl.master.MasterStatusTmpl;
035import org.apache.hadoop.hbase.util.FSUtils;
036import org.apache.yetus.audience.InterfaceAudience;
037
038/**
039 * The servlet responsible for rendering the index page of the 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) throws IOException {
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 =
069      new MasterStatusTmpl().setFrags(frags).setMetaLocation(metaLocation).setServers(servers)
070        .setDeadServers(deadServers).setCatalogJanitorEnabled(master.isCatalogJanitorEnabled());
071
072    if (request.getParameter("filter") != null) tmpl.setFilter(request.getParameter("filter"));
073    if (request.getParameter("format") != null) tmpl.setFormat(request.getParameter("format"));
074    tmpl.render(response.getWriter(), master);
075  }
076
077  private ServerName getMetaLocationOrNull(HMaster master) {
078    RegionStateNode rsn = master.getAssignmentManager().getRegionStates()
079      .getRegionStateNode(RegionInfoBuilder.FIRST_META_REGIONINFO);
080    return rsn.isInState(RegionState.State.OPEN) ? rsn.getRegionLocation() : null;
081  }
082
083  private Map<String, Integer> getFragmentationInfo(HMaster master, Configuration conf)
084    throws IOException {
085    boolean showFragmentation = conf.getBoolean("hbase.master.ui.fragmentation.enabled", false);
086    if (showFragmentation) {
087      return FSUtils.getTableFragmentation(master);
088    } else {
089      return null;
090    }
091  }
092}