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.regionserver.http;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.io.PrintStream;
023import java.io.PrintWriter;
024import java.util.Date;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.ipc.CallQueueInfo;
029import org.apache.hadoop.hbase.monitoring.StateDumpServlet;
030import org.apache.hadoop.hbase.monitoring.TaskMonitor;
031import org.apache.hadoop.hbase.regionserver.CompactSplit;
032import org.apache.hadoop.hbase.regionserver.HRegionServer;
033import org.apache.hadoop.hbase.regionserver.MemStoreFlusher;
034import org.apache.hadoop.hbase.util.LogMonitoring;
035import org.apache.hadoop.hbase.util.Threads;
036import org.apache.yetus.audience.InterfaceAudience;
037
038@InterfaceAudience.Private
039public class RSDumpServlet extends StateDumpServlet {
040  private static final long serialVersionUID = 1L;
041  private static final String LINE = "===========================================================";
042
043  @Override
044  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
045    HRegionServer hrs =
046      (HRegionServer) getServletContext().getAttribute(HRegionServer.REGIONSERVER);
047    assert hrs != null : "No RS in context!";
048
049    response.setContentType("text/plain");
050
051    if (!hrs.isOnline()) {
052      response.getWriter().write("The RegionServer is initializing!");
053      response.getWriter().close();
054      return;
055    }
056
057    OutputStream os = response.getOutputStream();
058    try (PrintWriter out = new PrintWriter(os)) {
059
060      out.println("RegionServer status for " + hrs.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\nRowLocks:");
071      out.println(LINE);
072      hrs.dumpRowLocks(out);
073
074      out.println("\n\nExecutors:");
075      out.println(LINE);
076      dumpExecutors(hrs.getExecutorService(), out);
077
078      out.println("\n\nStacks:");
079      out.println(LINE);
080      PrintStream ps = new PrintStream(response.getOutputStream(), false, "UTF-8");
081      Threads.printThreadInfo(ps, "");
082      ps.flush();
083
084      out.println("\n\nRS Configuration:");
085      out.println(LINE);
086      Configuration conf = hrs.getConfiguration();
087      out.flush();
088      conf.writeXml(os);
089      os.flush();
090
091      out.println("\n\nLogs");
092      out.println(LINE);
093      long tailKb = getTailKbParam(request);
094      LogMonitoring.dumpTailOfLogs(out, tailKb);
095
096      out.println("\n\nRS Queue:");
097      out.println(LINE);
098      if (isShowQueueDump(conf)) {
099        dumpQueue(hrs, out);
100      }
101
102      out.println("\n\nCall Queue Summary:");
103      out.println(LINE);
104      dumpCallQueues(hrs, out);
105
106      out.flush();
107    }
108  }
109
110  public static void dumpQueue(HRegionServer hrs, PrintWriter out) {
111    final CompactSplit compactSplit = hrs.getCompactSplitThread();
112    if (compactSplit != null) {
113      // 1. Print out Compaction/Split Queue
114      out.println("Compaction/Split Queue summary: " + compactSplit);
115      out.println(compactSplit.dumpQueue());
116    }
117
118    final MemStoreFlusher memStoreFlusher = hrs.getMemStoreFlusher();
119    if (memStoreFlusher != null) {
120      // 2. Print out flush Queue
121      out.println();
122      out.println("Flush Queue summary: " + memStoreFlusher);
123      out.println(memStoreFlusher.dumpQueue());
124    }
125  }
126
127  public static void dumpCallQueues(HRegionServer hrs, PrintWriter out) {
128    CallQueueInfo callQueueInfo = hrs.getRpcServer().getScheduler().getCallQueueInfo();
129
130    for (String queueName : callQueueInfo.getCallQueueNames()) {
131
132      out.println("\nQueue Name: " + queueName);
133
134      long totalCallCount = 0L, totalCallSize = 0L;
135      for (String methodName : callQueueInfo.getCalledMethodNames(queueName)) {
136        long thisMethodCount, thisMethodSize;
137        thisMethodCount = callQueueInfo.getCallMethodCount(queueName, methodName);
138        thisMethodSize = callQueueInfo.getCallMethodSize(queueName, methodName);
139
140        out.println("Method in call: " + methodName);
141        out.println("Total call count for method: " + thisMethodCount);
142        out.println("Total call size for method (bytes): " + thisMethodSize);
143
144        totalCallCount += thisMethodCount;
145        totalCallSize += thisMethodSize;
146      }
147      out.println("Total call count for queue: " + totalCallCount);
148      out.println("Total call size for queue (bytes): " + totalCallSize);
149    }
150  }
151}