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.OutputStreamWriter;
022import java.io.PrintStream;
023import java.io.PrintWriter;
024import java.nio.charset.StandardCharsets;
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.http.HttpServer;
033import org.apache.hadoop.hbase.master.HMaster;
034import org.apache.hadoop.hbase.master.ServerManager;
035import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
036import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
037import org.apache.hadoop.hbase.monitoring.StateDumpServlet;
038import org.apache.hadoop.hbase.monitoring.TaskMonitor;
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    OutputStreamWriter os =
058      new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8);
059    try (PrintWriter out = new PrintWriter(os)) {
060
061      out.println("Master status for " + master.getServerName() + " as of " + new Date());
062
063      out.println("\n\nVersion Info:");
064      out.println(LINE);
065      dumpVersionInfo(out);
066
067      out.println("\n\nTasks:");
068      out.println(LINE);
069      TaskMonitor.get().dumpAsText(out);
070
071      out.println("\n\nServers:");
072      out.println(LINE);
073      dumpServers(master, out);
074
075      out.println("\n\nRegions-in-transition:");
076      out.println(LINE);
077      dumpRIT(master, out);
078
079      out.println("\n\nExecutors:");
080      out.println(LINE);
081      dumpExecutors(master.getExecutorService(), out);
082
083      out.println("\n\nStacks:");
084      out.println(LINE);
085      out.flush();
086      PrintStream ps = new PrintStream(response.getOutputStream(), false, StandardCharsets.UTF_8);
087      Threads.printThreadInfo(ps, "");
088      ps.flush();
089
090      out.println("\n\nMaster configuration:");
091      out.println(LINE);
092      Configuration redactedConf = getRedactedConfiguration(master.getConfiguration());
093      out.flush();
094      redactedConf.writeXml(os);
095      os.flush();
096
097      out.println("\n\nRecent regionserver aborts:");
098      out.println(LINE);
099      master.getRegionServerFatalLogBuffer().dumpTo(out);
100
101      out.println("\n\nLogs");
102      out.println(LINE);
103      long tailKb = getTailKbParam(request);
104      LogMonitoring.dumpTailOfLogs(out, tailKb);
105
106      out.flush();
107    }
108  }
109
110  private void dumpRIT(HMaster master, PrintWriter out) {
111    AssignmentManager am = master.getAssignmentManager();
112    if (am == null) {
113      out.println("AssignmentManager is not initialized");
114      return;
115    }
116
117    for (RegionStateNode rs : am.getRegionsInTransition()) {
118      String rid = rs.getRegionInfo().getEncodedName();
119      out.println("Region " + rid + ": " + rs.toDescriptiveString());
120    }
121  }
122
123  private void dumpServers(HMaster master, PrintWriter out) {
124    ServerManager sm = master.getServerManager();
125    if (sm == null) {
126      out.println("ServerManager is not initialized");
127      return;
128    }
129
130    Map<ServerName, ServerMetrics> servers = sm.getOnlineServers();
131    for (Map.Entry<ServerName, ServerMetrics> e : servers.entrySet()) {
132      out.println(e.getKey() + ": " + e.getValue());
133    }
134  }
135}