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.io.OutputStream;
023import java.io.PrintStream;
024import java.io.PrintWriter;
025import java.util.Date;
026import java.util.Map;
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.ServerMetrics;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
033import org.apache.hadoop.hbase.master.assignment.RegionStates.RegionStateNode;
034import org.apache.hadoop.hbase.monitoring.LogMonitoring;
035import org.apache.hadoop.hbase.monitoring.StateDumpServlet;
036import org.apache.hadoop.hbase.monitoring.TaskMonitor;
037import org.apache.hadoop.hbase.regionserver.RSDumpServlet;
038import org.apache.hadoop.hbase.util.Threads;
039import org.apache.yetus.audience.InterfaceAudience;
040
041@InterfaceAudience.Private
042public class MasterDumpServlet extends StateDumpServlet {
043  private static final long serialVersionUID = 1L;
044  private static final String LINE =
045    "===========================================================";
046
047  @Override
048  public void doGet(HttpServletRequest request, HttpServletResponse response)
049      throws IOException {
050    HMaster master = (HMaster) getServletContext().getAttribute(HMaster.MASTER);
051    assert master != null : "No Master in context!";
052
053    response.setContentType("text/plain");
054    OutputStream os = response.getOutputStream();
055    try (PrintWriter out = new PrintWriter(os)) {
056
057      out.println("Master status for " + master.getServerName()
058        + " as of " + new Date());
059
060      out.println("\n\nVersion Info:");
061      out.println(LINE);
062      dumpVersionInfo(out);
063
064      out.println("\n\nTasks:");
065      out.println(LINE);
066      TaskMonitor.get().dumpAsText(out);
067
068      out.println("\n\nServers:");
069      out.println(LINE);
070      dumpServers(master, out);
071
072      out.println("\n\nRegions-in-transition:");
073      out.println(LINE);
074      dumpRIT(master, out);
075
076      out.println("\n\nExecutors:");
077      out.println(LINE);
078      dumpExecutors(master.getExecutorService(), out);
079
080      out.println("\n\nStacks:");
081      out.println(LINE);
082      out.flush();
083      PrintStream ps = new PrintStream(response.getOutputStream(), false, "UTF-8");
084      Threads.printThreadInfo(ps, "");
085      ps.flush();
086
087      out.println("\n\nMaster configuration:");
088      out.println(LINE);
089      Configuration conf = master.getConfiguration();
090      out.flush();
091      conf.writeXml(os);
092      os.flush();
093
094      out.println("\n\nRecent regionserver aborts:");
095      out.println(LINE);
096      master.getRegionServerFatalLogBuffer().dumpTo(out);
097
098      out.println("\n\nLogs");
099      out.println(LINE);
100      long tailKb = getTailKbParam(request);
101      LogMonitoring.dumpTailOfLogs(out, tailKb);
102
103      out.println("\n\nRS Queue:");
104      out.println(LINE);
105      if (isShowQueueDump(conf)) {
106        RSDumpServlet.dumpQueue(master, out);
107      }
108      out.flush();
109    }
110  }
111
112
113  private void dumpRIT(HMaster master, PrintWriter out) {
114    AssignmentManager am = master.getAssignmentManager();
115    if (am == null) {
116      out.println("AssignmentManager is not initialized");
117      return;
118    }
119
120    for (RegionStateNode rs : am.getRegionsInTransition()) {
121      String rid = rs.getRegionInfo().getEncodedName();
122      out.println("Region " + rid + ": " + rs.toDescriptiveString());
123    }
124  }
125
126  private void dumpServers(HMaster master, PrintWriter out) {
127    ServerManager sm = master.getServerManager();
128    if (sm == null) {
129      out.println("ServerManager is not initialized");
130      return;
131    }
132
133    Map<ServerName, ServerMetrics> servers = sm.getOnlineServers();
134    for (Map.Entry<ServerName, ServerMetrics> e : servers.entrySet()) {
135      out.println(e.getKey() + ": " + e.getValue());
136    }
137  }
138}