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