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