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