[120] | 1 | <%@ page |
---|
| 2 | contentType="text/html; charset=UTF-8" |
---|
| 3 | import="javax.servlet.*" |
---|
| 4 | import="javax.servlet.http.*" |
---|
| 5 | import="java.io.*" |
---|
| 6 | import="java.util.*" |
---|
| 7 | import="java.net.*" |
---|
| 8 | import="org.apache.hadoop.hdfs.*" |
---|
| 9 | import="org.apache.hadoop.hdfs.server.namenode.*" |
---|
| 10 | import="org.apache.hadoop.hdfs.server.datanode.*" |
---|
| 11 | import="org.apache.hadoop.hdfs.protocol.*" |
---|
| 12 | import="org.apache.hadoop.io.*" |
---|
| 13 | import="org.apache.hadoop.conf.*" |
---|
| 14 | import="org.apache.hadoop.net.DNS" |
---|
| 15 | import="org.apache.hadoop.util.*" |
---|
| 16 | import="org.apache.hadoop.net.NetUtils" |
---|
| 17 | import="java.text.DateFormat" |
---|
| 18 | %> |
---|
| 19 | |
---|
| 20 | <%! |
---|
| 21 | static JspHelper jspHelper = new JspHelper(); |
---|
| 22 | |
---|
| 23 | public void generateFileChunks(JspWriter out, HttpServletRequest req) |
---|
| 24 | throws IOException { |
---|
| 25 | long startOffset = 0; |
---|
| 26 | |
---|
| 27 | int chunkSizeToView = 0; |
---|
| 28 | |
---|
| 29 | String referrer = req.getParameter("referrer"); |
---|
| 30 | boolean noLink = false; |
---|
| 31 | if (referrer == null) { |
---|
| 32 | noLink = true; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | String filename = req.getParameter("filename"); |
---|
| 36 | if (filename == null) { |
---|
| 37 | out.print("Invalid input (file name absent)"); |
---|
| 38 | return; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | String namenodeInfoPortStr = req.getParameter("namenodeInfoPort"); |
---|
| 42 | int namenodeInfoPort = -1; |
---|
| 43 | if (namenodeInfoPortStr != null) |
---|
| 44 | namenodeInfoPort = Integer.parseInt(namenodeInfoPortStr); |
---|
| 45 | |
---|
| 46 | String chunkSizeToViewStr = req.getParameter("chunkSizeToView"); |
---|
| 47 | if (chunkSizeToViewStr != null && Integer.parseInt(chunkSizeToViewStr) > 0) |
---|
| 48 | chunkSizeToView = Integer.parseInt(chunkSizeToViewStr); |
---|
| 49 | else chunkSizeToView = jspHelper.defaultChunkSizeToView; |
---|
| 50 | |
---|
| 51 | if (!noLink) { |
---|
| 52 | out.print("<h3>Tail of File: "); |
---|
| 53 | JspHelper.printPathWithLinks(filename, out, namenodeInfoPort); |
---|
| 54 | out.print("</h3><hr>"); |
---|
| 55 | out.print("<a href=\"" + referrer + "\">Go Back to File View</a><hr>"); |
---|
| 56 | } |
---|
| 57 | else { |
---|
| 58 | out.print("<h3>" + filename + "</h3>"); |
---|
| 59 | } |
---|
| 60 | out.print("<b>Chunk size to view (in bytes, up to file's DFS block size): </b>"); |
---|
| 61 | out.print("<input type=\"text\" name=\"chunkSizeToView\" value=" + |
---|
| 62 | chunkSizeToView + " size=10 maxlength=10>"); |
---|
| 63 | out.print(" <input type=\"submit\" name=\"submit\" value=\"Refresh\"><hr>"); |
---|
| 64 | out.print("<input type=\"hidden\" name=\"filename\" value=\"" + filename + |
---|
| 65 | "\">"); |
---|
| 66 | out.print("<input type=\"hidden\" name=\"namenodeInfoPort\" value=\"" + namenodeInfoPort + |
---|
| 67 | "\">"); |
---|
| 68 | if (!noLink) |
---|
| 69 | out.print("<input type=\"hidden\" name=\"referrer\" value=\"" + |
---|
| 70 | referrer+ "\">"); |
---|
| 71 | |
---|
| 72 | //fetch the block from the datanode that has the last block for this file |
---|
| 73 | DFSClient dfs = new DFSClient(jspHelper.nameNodeAddr, |
---|
| 74 | jspHelper.conf); |
---|
| 75 | List<LocatedBlock> blocks = |
---|
| 76 | dfs.namenode.getBlockLocations(filename, 0, Long.MAX_VALUE).getLocatedBlocks(); |
---|
| 77 | if (blocks == null || blocks.size() == 0) { |
---|
| 78 | out.print("No datanodes contain blocks of file "+filename); |
---|
| 79 | dfs.close(); |
---|
| 80 | return; |
---|
| 81 | } |
---|
| 82 | LocatedBlock lastBlk = blocks.get(blocks.size() - 1); |
---|
| 83 | long blockSize = lastBlk.getBlock().getNumBytes(); |
---|
| 84 | long blockId = lastBlk.getBlock().getBlockId(); |
---|
| 85 | long genStamp = lastBlk.getBlock().getGenerationStamp(); |
---|
| 86 | DatanodeInfo chosenNode; |
---|
| 87 | try { |
---|
| 88 | chosenNode = jspHelper.bestNode(lastBlk); |
---|
| 89 | } catch (IOException e) { |
---|
| 90 | out.print(e.toString()); |
---|
| 91 | dfs.close(); |
---|
| 92 | return; |
---|
| 93 | } |
---|
| 94 | InetSocketAddress addr = NetUtils.createSocketAddr(chosenNode.getName()); |
---|
| 95 | //view the last chunkSizeToView bytes while Tailing |
---|
| 96 | if (blockSize >= chunkSizeToView) |
---|
| 97 | startOffset = blockSize - chunkSizeToView; |
---|
| 98 | else startOffset = 0; |
---|
| 99 | |
---|
| 100 | out.print("<textarea cols=\"100\" rows=\"25\" wrap=\"virtual\" style=\"width:100%\" READONLY>"); |
---|
| 101 | jspHelper.streamBlockInAscii(addr, blockId, genStamp, blockSize, startOffset, chunkSizeToView, out); |
---|
| 102 | out.print("</textarea>"); |
---|
| 103 | dfs.close(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | %> |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | <html> |
---|
| 111 | <head> |
---|
| 112 | <%JspHelper.createTitle(out, request, request.getParameter("filename")); %> |
---|
| 113 | </head> |
---|
| 114 | <body> |
---|
| 115 | <form action="/tail.jsp" method="GET"> |
---|
| 116 | <% |
---|
| 117 | generateFileChunks(out,request); |
---|
| 118 | %> |
---|
| 119 | </form> |
---|
| 120 | <hr> |
---|
| 121 | |
---|
| 122 | <h2>Local logs</h2> |
---|
| 123 | <a href="/logs/">Log</a> directory |
---|
| 124 | |
---|
| 125 | <% |
---|
| 126 | out.println(ServletUtil.htmlFooter()); |
---|
| 127 | %> |
---|