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