[120] | 1 | package org.apache.hadoop.hdfs.server.datanode; |
---|
| 2 | |
---|
| 3 | import javax.servlet.*; |
---|
| 4 | import javax.servlet.http.*; |
---|
| 5 | import javax.servlet.jsp.*; |
---|
| 6 | import javax.servlet.*; |
---|
| 7 | import javax.servlet.http.*; |
---|
| 8 | import java.io.*; |
---|
| 9 | import java.util.*; |
---|
| 10 | import java.net.*; |
---|
| 11 | import org.apache.hadoop.hdfs.*; |
---|
| 12 | import org.apache.hadoop.hdfs.server.namenode.*; |
---|
| 13 | import org.apache.hadoop.hdfs.server.datanode.*; |
---|
| 14 | import org.apache.hadoop.hdfs.protocol.*; |
---|
| 15 | import org.apache.hadoop.io.*; |
---|
| 16 | import org.apache.hadoop.conf.*; |
---|
| 17 | import org.apache.hadoop.net.DNS; |
---|
| 18 | import org.apache.hadoop.util.*; |
---|
| 19 | import org.apache.hadoop.net.NetUtils; |
---|
| 20 | import java.text.DateFormat; |
---|
| 21 | |
---|
| 22 | public final class tail_jsp extends org.apache.jasper.runtime.HttpJspBase |
---|
| 23 | implements org.apache.jasper.runtime.JspSourceDependent { |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | static JspHelper jspHelper = new JspHelper(); |
---|
| 27 | |
---|
| 28 | public void generateFileChunks(JspWriter out, HttpServletRequest req) |
---|
| 29 | throws IOException { |
---|
| 30 | long startOffset = 0; |
---|
| 31 | |
---|
| 32 | int chunkSizeToView = 0; |
---|
| 33 | |
---|
| 34 | String referrer = req.getParameter("referrer"); |
---|
| 35 | boolean noLink = false; |
---|
| 36 | if (referrer == null) { |
---|
| 37 | noLink = true; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | String filename = req.getParameter("filename"); |
---|
| 41 | if (filename == null) { |
---|
| 42 | out.print("Invalid input (file name absent)"); |
---|
| 43 | return; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | String namenodeInfoPortStr = req.getParameter("namenodeInfoPort"); |
---|
| 47 | int namenodeInfoPort = -1; |
---|
| 48 | if (namenodeInfoPortStr != null) |
---|
| 49 | namenodeInfoPort = Integer.parseInt(namenodeInfoPortStr); |
---|
| 50 | |
---|
| 51 | String chunkSizeToViewStr = req.getParameter("chunkSizeToView"); |
---|
| 52 | if (chunkSizeToViewStr != null && Integer.parseInt(chunkSizeToViewStr) > 0) |
---|
| 53 | chunkSizeToView = Integer.parseInt(chunkSizeToViewStr); |
---|
| 54 | else chunkSizeToView = jspHelper.defaultChunkSizeToView; |
---|
| 55 | |
---|
| 56 | if (!noLink) { |
---|
| 57 | out.print("<h3>Tail of File: "); |
---|
| 58 | JspHelper.printPathWithLinks(filename, out, namenodeInfoPort); |
---|
| 59 | out.print("</h3><hr>"); |
---|
| 60 | out.print("<a href=\"" + referrer + "\">Go Back to File View</a><hr>"); |
---|
| 61 | } |
---|
| 62 | else { |
---|
| 63 | out.print("<h3>" + filename + "</h3>"); |
---|
| 64 | } |
---|
| 65 | out.print("<b>Chunk size to view (in bytes, up to file's DFS block size): </b>"); |
---|
| 66 | out.print("<input type=\"text\" name=\"chunkSizeToView\" value=" + |
---|
| 67 | chunkSizeToView + " size=10 maxlength=10>"); |
---|
| 68 | out.print(" <input type=\"submit\" name=\"submit\" value=\"Refresh\"><hr>"); |
---|
| 69 | out.print("<input type=\"hidden\" name=\"filename\" value=\"" + filename + |
---|
| 70 | "\">"); |
---|
| 71 | out.print("<input type=\"hidden\" name=\"namenodeInfoPort\" value=\"" + namenodeInfoPort + |
---|
| 72 | "\">"); |
---|
| 73 | if (!noLink) |
---|
| 74 | out.print("<input type=\"hidden\" name=\"referrer\" value=\"" + |
---|
| 75 | referrer+ "\">"); |
---|
| 76 | |
---|
| 77 | //fetch the block from the datanode that has the last block for this file |
---|
| 78 | DFSClient dfs = new DFSClient(jspHelper.nameNodeAddr, |
---|
| 79 | jspHelper.conf); |
---|
| 80 | List<LocatedBlock> blocks = |
---|
| 81 | dfs.namenode.getBlockLocations(filename, 0, Long.MAX_VALUE).getLocatedBlocks(); |
---|
| 82 | if (blocks == null || blocks.size() == 0) { |
---|
| 83 | out.print("No datanodes contain blocks of file "+filename); |
---|
| 84 | dfs.close(); |
---|
| 85 | return; |
---|
| 86 | } |
---|
| 87 | LocatedBlock lastBlk = blocks.get(blocks.size() - 1); |
---|
| 88 | long blockSize = lastBlk.getBlock().getNumBytes(); |
---|
| 89 | long blockId = lastBlk.getBlock().getBlockId(); |
---|
| 90 | long genStamp = lastBlk.getBlock().getGenerationStamp(); |
---|
| 91 | DatanodeInfo chosenNode; |
---|
| 92 | try { |
---|
| 93 | chosenNode = jspHelper.bestNode(lastBlk); |
---|
| 94 | } catch (IOException e) { |
---|
| 95 | out.print(e.toString()); |
---|
| 96 | dfs.close(); |
---|
| 97 | return; |
---|
| 98 | } |
---|
| 99 | InetSocketAddress addr = NetUtils.createSocketAddr(chosenNode.getName()); |
---|
| 100 | //view the last chunkSizeToView bytes while Tailing |
---|
| 101 | if (blockSize >= chunkSizeToView) |
---|
| 102 | startOffset = blockSize - chunkSizeToView; |
---|
| 103 | else startOffset = 0; |
---|
| 104 | |
---|
| 105 | out.print("<textarea cols=\"100\" rows=\"25\" wrap=\"virtual\" style=\"width:100%\" READONLY>"); |
---|
| 106 | jspHelper.streamBlockInAscii(addr, blockId, genStamp, blockSize, startOffset, chunkSizeToView, out); |
---|
| 107 | out.print("</textarea>"); |
---|
| 108 | dfs.close(); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | private static java.util.List _jspx_dependants; |
---|
| 113 | |
---|
| 114 | public Object getDependants() { |
---|
| 115 | return _jspx_dependants; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | public void _jspService(HttpServletRequest request, HttpServletResponse response) |
---|
| 119 | throws java.io.IOException, ServletException { |
---|
| 120 | |
---|
| 121 | JspFactory _jspxFactory = null; |
---|
| 122 | PageContext pageContext = null; |
---|
| 123 | HttpSession session = null; |
---|
| 124 | ServletContext application = null; |
---|
| 125 | ServletConfig config = null; |
---|
| 126 | JspWriter out = null; |
---|
| 127 | Object page = this; |
---|
| 128 | JspWriter _jspx_out = null; |
---|
| 129 | PageContext _jspx_page_context = null; |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | try { |
---|
| 133 | _jspxFactory = JspFactory.getDefaultFactory(); |
---|
| 134 | response.setContentType("text/html; charset=UTF-8"); |
---|
| 135 | pageContext = _jspxFactory.getPageContext(this, request, response, |
---|
| 136 | null, true, 8192, true); |
---|
| 137 | _jspx_page_context = pageContext; |
---|
| 138 | application = pageContext.getServletContext(); |
---|
| 139 | config = pageContext.getServletConfig(); |
---|
| 140 | session = pageContext.getSession(); |
---|
| 141 | out = pageContext.getOut(); |
---|
| 142 | _jspx_out = out; |
---|
| 143 | |
---|
| 144 | out.write('\n'); |
---|
| 145 | out.write('\n'); |
---|
| 146 | out.write("\n\n\n\n<html>\n<head>\n"); |
---|
| 147 | JspHelper.createTitle(out, request, request.getParameter("filename")); |
---|
| 148 | out.write("\n</head>\n<body>\n<form action=\"/tail.jsp\" method=\"GET\">\n"); |
---|
| 149 | |
---|
| 150 | generateFileChunks(out,request); |
---|
| 151 | |
---|
| 152 | out.write("\n</form>\n<hr>\n\n<h2>Local logs</h2>\n<a href=\"/logs/\">Log</a> directory\n\n"); |
---|
| 153 | |
---|
| 154 | out.println(ServletUtil.htmlFooter()); |
---|
| 155 | |
---|
| 156 | out.write('\n'); |
---|
| 157 | } catch (Throwable t) { |
---|
| 158 | if (!(t instanceof SkipPageException)){ |
---|
| 159 | out = _jspx_out; |
---|
| 160 | if (out != null && out.getBufferSize() != 0) |
---|
| 161 | out.clearBuffer(); |
---|
| 162 | if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); |
---|
| 163 | } |
---|
| 164 | } finally { |
---|
| 165 | if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | } |
---|