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 java.text.DateFormat; |
---|
20 | |
---|
21 | public final class browseBlock_jsp extends org.apache.jasper.runtime.HttpJspBase |
---|
22 | implements org.apache.jasper.runtime.JspSourceDependent { |
---|
23 | |
---|
24 | |
---|
25 | static JspHelper jspHelper = new JspHelper(); |
---|
26 | |
---|
27 | public void generateFileDetails(JspWriter out, HttpServletRequest req) |
---|
28 | throws IOException { |
---|
29 | |
---|
30 | int chunkSizeToView = 0; |
---|
31 | long startOffset = 0; |
---|
32 | int datanodePort; |
---|
33 | |
---|
34 | String blockIdStr = null; |
---|
35 | long currBlockId = 0; |
---|
36 | blockIdStr = req.getParameter("blockId"); |
---|
37 | if (blockIdStr == null) { |
---|
38 | out.print("Invalid input (blockId absent)"); |
---|
39 | return; |
---|
40 | } |
---|
41 | currBlockId = Long.parseLong(blockIdStr); |
---|
42 | |
---|
43 | String datanodePortStr = req.getParameter("datanodePort"); |
---|
44 | if (datanodePortStr == null) { |
---|
45 | out.print("Invalid input (datanodePort absent)"); |
---|
46 | return; |
---|
47 | } |
---|
48 | datanodePort = Integer.parseInt(datanodePortStr); |
---|
49 | |
---|
50 | String namenodeInfoPortStr = req.getParameter("namenodeInfoPort"); |
---|
51 | int namenodeInfoPort = -1; |
---|
52 | if (namenodeInfoPortStr != null) |
---|
53 | namenodeInfoPort = Integer.parseInt(namenodeInfoPortStr); |
---|
54 | |
---|
55 | String chunkSizeToViewStr = req.getParameter("chunkSizeToView"); |
---|
56 | if (chunkSizeToViewStr != null && Integer.parseInt(chunkSizeToViewStr) > 0) |
---|
57 | chunkSizeToView = Integer.parseInt(chunkSizeToViewStr); |
---|
58 | else chunkSizeToView = jspHelper.defaultChunkSizeToView; |
---|
59 | |
---|
60 | String startOffsetStr = req.getParameter("startOffset"); |
---|
61 | if (startOffsetStr == null || Long.parseLong(startOffsetStr) < 0) |
---|
62 | startOffset = 0; |
---|
63 | else startOffset = Long.parseLong(startOffsetStr); |
---|
64 | |
---|
65 | String filename = req.getParameter("filename"); |
---|
66 | if (filename == null || filename.length() == 0) { |
---|
67 | out.print("Invalid input"); |
---|
68 | return; |
---|
69 | } |
---|
70 | |
---|
71 | String blockSizeStr = req.getParameter("blockSize"); |
---|
72 | long blockSize = 0; |
---|
73 | if (blockSizeStr == null || blockSizeStr.length() == 0) { |
---|
74 | out.print("Invalid input"); |
---|
75 | return; |
---|
76 | } |
---|
77 | blockSize = Long.parseLong(blockSizeStr); |
---|
78 | |
---|
79 | DFSClient dfs = new DFSClient(jspHelper.nameNodeAddr, jspHelper.conf); |
---|
80 | List<LocatedBlock> blocks = |
---|
81 | dfs.namenode.getBlockLocations(filename, 0, Long.MAX_VALUE).getLocatedBlocks(); |
---|
82 | //Add the various links for looking at the file contents |
---|
83 | //URL for downloading the full file |
---|
84 | String downloadUrl = "http://" + req.getServerName() + ":" + |
---|
85 | + req.getServerPort() + "/streamFile?" + "filename=" + |
---|
86 | URLEncoder.encode(filename, "UTF-8"); |
---|
87 | out.print("<a name=\"viewOptions\"></a>"); |
---|
88 | out.print("<a href=\"" + downloadUrl + "\">Download this file</a><br>"); |
---|
89 | |
---|
90 | DatanodeInfo chosenNode; |
---|
91 | //URL for TAIL |
---|
92 | LocatedBlock lastBlk = blocks.get(blocks.size() - 1); |
---|
93 | long blockId = lastBlk.getBlock().getBlockId(); |
---|
94 | try { |
---|
95 | chosenNode = jspHelper.bestNode(lastBlk); |
---|
96 | } catch (IOException e) { |
---|
97 | out.print(e.toString()); |
---|
98 | dfs.close(); |
---|
99 | return; |
---|
100 | } |
---|
101 | String fqdn = |
---|
102 | InetAddress.getByName(chosenNode.getHost()).getCanonicalHostName(); |
---|
103 | String tailUrl = "http://" + fqdn + ":" + |
---|
104 | chosenNode.getInfoPort() + |
---|
105 | "/tail.jsp?filename=" + URLEncoder.encode(filename, "UTF-8") + |
---|
106 | "&namenodeInfoPort=" + namenodeInfoPort + |
---|
107 | "&chunkSizeToView=" + chunkSizeToView + |
---|
108 | "&referrer=" + |
---|
109 | URLEncoder.encode(req.getRequestURL() + "?" + req.getQueryString(), |
---|
110 | "UTF-8"); |
---|
111 | out.print("<a href=\"" + tailUrl + "\">Tail this file</a><br>"); |
---|
112 | |
---|
113 | out.print("<form action=\"/browseBlock.jsp\" method=GET>"); |
---|
114 | out.print("<b>Chunk size to view (in bytes, up to file's DFS block size): </b>"); |
---|
115 | out.print("<input type=\"hidden\" name=\"blockId\" value=\"" + currBlockId + |
---|
116 | "\">"); |
---|
117 | out.print("<input type=\"hidden\" name=\"blockSize\" value=\"" + |
---|
118 | blockSize + "\">"); |
---|
119 | out.print("<input type=\"hidden\" name=\"startOffset\" value=\"" + |
---|
120 | startOffset + "\">"); |
---|
121 | out.print("<input type=\"hidden\" name=\"filename\" value=\"" + filename + |
---|
122 | "\">"); |
---|
123 | out.print("<input type=\"hidden\" name=\"datanodePort\" value=\"" + |
---|
124 | datanodePort+ "\">"); |
---|
125 | out.print("<input type=\"hidden\" name=\"namenodeInfoPort\" value=\"" + |
---|
126 | namenodeInfoPort + "\">"); |
---|
127 | out.print("<input type=\"text\" name=\"chunkSizeToView\" value=" + |
---|
128 | chunkSizeToView + " size=10 maxlength=10>"); |
---|
129 | out.print(" <input type=\"submit\" name=\"submit\" value=\"Refresh\">"); |
---|
130 | out.print("</form>"); |
---|
131 | out.print("<hr>"); |
---|
132 | out.print("<a name=\"blockDetails\"></a>"); |
---|
133 | out.print("<B>Total number of blocks: "+blocks.size()+"</B><br>"); |
---|
134 | //generate a table and dump the info |
---|
135 | out.println("\n<table>"); |
---|
136 | for (LocatedBlock cur : blocks) { |
---|
137 | out.print("<tr>"); |
---|
138 | blockId = cur.getBlock().getBlockId(); |
---|
139 | blockSize = cur.getBlock().getNumBytes(); |
---|
140 | String blk = "blk_" + Long.toString(blockId); |
---|
141 | out.print("<td>"+Long.toString(blockId)+":</td>"); |
---|
142 | DatanodeInfo[] locs = cur.getLocations(); |
---|
143 | for(int j=0; j<locs.length; j++) { |
---|
144 | String datanodeAddr = locs[j].getName(); |
---|
145 | datanodePort = Integer.parseInt(datanodeAddr.substring( |
---|
146 | datanodeAddr.indexOf(':') + 1, |
---|
147 | datanodeAddr.length())); |
---|
148 | fqdn = InetAddress.getByName(locs[j].getHost()).getCanonicalHostName(); |
---|
149 | String blockUrl = "http://"+ fqdn + ":" + |
---|
150 | locs[j].getInfoPort() + |
---|
151 | "/browseBlock.jsp?blockId=" + Long.toString(blockId) + |
---|
152 | "&blockSize=" + blockSize + |
---|
153 | "&filename=" + URLEncoder.encode(filename, "UTF-8")+ |
---|
154 | "&datanodePort=" + datanodePort + |
---|
155 | "&genstamp=" + cur.getBlock().getGenerationStamp() + |
---|
156 | "&namenodeInfoPort=" + namenodeInfoPort + |
---|
157 | "&chunkSizeToView=" + chunkSizeToView; |
---|
158 | out.print("<td> </td>" |
---|
159 | + "<td><a href=\"" + blockUrl + "\">" + datanodeAddr + "</a></td>"); |
---|
160 | } |
---|
161 | out.println("</tr>"); |
---|
162 | } |
---|
163 | out.println("</table>"); |
---|
164 | out.print("<hr>"); |
---|
165 | String namenodeHost = jspHelper.nameNodeAddr.getHostName(); |
---|
166 | out.print("<br><a href=\"http://" + |
---|
167 | InetAddress.getByName(namenodeHost).getCanonicalHostName() + ":" + |
---|
168 | namenodeInfoPort + "/dfshealth.jsp\">Go back to DFS home</a>"); |
---|
169 | dfs.close(); |
---|
170 | } |
---|
171 | |
---|
172 | public void generateFileChunks(JspWriter out, HttpServletRequest req) |
---|
173 | throws IOException { |
---|
174 | long startOffset = 0; |
---|
175 | int datanodePort = 0; |
---|
176 | int chunkSizeToView = 0; |
---|
177 | |
---|
178 | String namenodeInfoPortStr = req.getParameter("namenodeInfoPort"); |
---|
179 | int namenodeInfoPort = -1; |
---|
180 | if (namenodeInfoPortStr != null) |
---|
181 | namenodeInfoPort = Integer.parseInt(namenodeInfoPortStr); |
---|
182 | |
---|
183 | String filename = req.getParameter("filename"); |
---|
184 | if (filename == null) { |
---|
185 | out.print("Invalid input (filename absent)"); |
---|
186 | return; |
---|
187 | } |
---|
188 | |
---|
189 | String blockIdStr = null; |
---|
190 | long blockId = 0; |
---|
191 | blockIdStr = req.getParameter("blockId"); |
---|
192 | if (blockIdStr == null) { |
---|
193 | out.print("Invalid input (blockId absent)"); |
---|
194 | return; |
---|
195 | } |
---|
196 | blockId = Long.parseLong(blockIdStr); |
---|
197 | |
---|
198 | String blockGenStamp = null; |
---|
199 | long genStamp = 0; |
---|
200 | blockGenStamp = req.getParameter("genstamp"); |
---|
201 | if (blockGenStamp == null) { |
---|
202 | out.print("Invalid input (genstamp absent)"); |
---|
203 | return; |
---|
204 | } |
---|
205 | genStamp = Long.parseLong(blockGenStamp); |
---|
206 | |
---|
207 | String blockSizeStr; |
---|
208 | long blockSize = 0; |
---|
209 | blockSizeStr = req.getParameter("blockSize"); |
---|
210 | if (blockSizeStr == null) { |
---|
211 | out.print("Invalid input (blockSize absent)"); |
---|
212 | return; |
---|
213 | } |
---|
214 | blockSize = Long.parseLong(blockSizeStr); |
---|
215 | |
---|
216 | String chunkSizeToViewStr = req.getParameter("chunkSizeToView"); |
---|
217 | if (chunkSizeToViewStr != null && Integer.parseInt(chunkSizeToViewStr) > 0) |
---|
218 | chunkSizeToView = Integer.parseInt(chunkSizeToViewStr); |
---|
219 | else chunkSizeToView = jspHelper.defaultChunkSizeToView; |
---|
220 | |
---|
221 | String startOffsetStr = req.getParameter("startOffset"); |
---|
222 | if (startOffsetStr == null || Long.parseLong(startOffsetStr) < 0) |
---|
223 | startOffset = 0; |
---|
224 | else startOffset = Long.parseLong(startOffsetStr); |
---|
225 | |
---|
226 | String datanodePortStr = req.getParameter("datanodePort"); |
---|
227 | if (datanodePortStr == null) { |
---|
228 | out.print("Invalid input (datanodePort absent)"); |
---|
229 | return; |
---|
230 | } |
---|
231 | datanodePort = Integer.parseInt(datanodePortStr); |
---|
232 | out.print("<h3>File: "); |
---|
233 | JspHelper.printPathWithLinks(filename, out, namenodeInfoPort); |
---|
234 | out.print("</h3><hr>"); |
---|
235 | String parent = new File(filename).getParent(); |
---|
236 | JspHelper.printGotoForm(out, namenodeInfoPort, parent); |
---|
237 | out.print("<hr>"); |
---|
238 | out.print("<a href=\"http://" + req.getServerName() + ":" + |
---|
239 | req.getServerPort() + |
---|
240 | "/browseDirectory.jsp?dir=" + |
---|
241 | URLEncoder.encode(parent, "UTF-8") + |
---|
242 | "&namenodeInfoPort=" + namenodeInfoPort + |
---|
243 | "\"><i>Go back to dir listing</i></a><br>"); |
---|
244 | out.print("<a href=\"#viewOptions\">Advanced view/download options</a><br>"); |
---|
245 | out.print("<hr>"); |
---|
246 | |
---|
247 | //Determine the prev & next blocks |
---|
248 | DFSClient dfs = new DFSClient(jspHelper.nameNodeAddr, jspHelper.conf); |
---|
249 | long nextStartOffset = 0; |
---|
250 | long nextBlockSize = 0; |
---|
251 | String nextBlockIdStr = null; |
---|
252 | String nextGenStamp = null; |
---|
253 | String nextHost = req.getServerName(); |
---|
254 | int nextPort = req.getServerPort(); |
---|
255 | int nextDatanodePort = datanodePort; |
---|
256 | //determine data for the next link |
---|
257 | if (startOffset + chunkSizeToView >= blockSize) { |
---|
258 | //we have to go to the next block from this point onwards |
---|
259 | List<LocatedBlock> blocks = |
---|
260 | dfs.namenode.getBlockLocations(filename, 0, Long.MAX_VALUE).getLocatedBlocks(); |
---|
261 | for (int i = 0; i < blocks.size(); i++) { |
---|
262 | if (blocks.get(i).getBlock().getBlockId() == blockId) { |
---|
263 | if (i != blocks.size() - 1) { |
---|
264 | LocatedBlock nextBlock = blocks.get(i+1); |
---|
265 | nextBlockIdStr = Long.toString(nextBlock.getBlock().getBlockId()); |
---|
266 | nextGenStamp = Long.toString(nextBlock.getBlock().getGenerationStamp()); |
---|
267 | nextStartOffset = 0; |
---|
268 | nextBlockSize = nextBlock.getBlock().getNumBytes(); |
---|
269 | DatanodeInfo d = jspHelper.bestNode(nextBlock); |
---|
270 | String datanodeAddr = d.getName(); |
---|
271 | nextDatanodePort = Integer.parseInt( |
---|
272 | datanodeAddr.substring( |
---|
273 | datanodeAddr.indexOf(':') + 1, |
---|
274 | datanodeAddr.length())); |
---|
275 | nextHost = InetAddress.getByName(d.getHost()).getCanonicalHostName(); |
---|
276 | nextPort = d.getInfoPort(); |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | else { |
---|
282 | //we are in the same block |
---|
283 | nextBlockIdStr = blockIdStr; |
---|
284 | nextStartOffset = startOffset + chunkSizeToView; |
---|
285 | nextBlockSize = blockSize; |
---|
286 | nextGenStamp = blockGenStamp; |
---|
287 | } |
---|
288 | String nextUrl = null; |
---|
289 | if (nextBlockIdStr != null) { |
---|
290 | nextUrl = "http://" + nextHost + ":" + |
---|
291 | nextPort + |
---|
292 | "/browseBlock.jsp?blockId=" + nextBlockIdStr + |
---|
293 | "&blockSize=" + nextBlockSize + "&startOffset=" + |
---|
294 | nextStartOffset + |
---|
295 | "&genstamp=" + nextGenStamp + |
---|
296 | "&filename=" + URLEncoder.encode(filename, "UTF-8") + |
---|
297 | "&chunkSizeToView=" + chunkSizeToView + |
---|
298 | "&datanodePort=" + nextDatanodePort + |
---|
299 | "&namenodeInfoPort=" + namenodeInfoPort; |
---|
300 | out.print("<a href=\"" + nextUrl + "\">View Next chunk</a> "); |
---|
301 | } |
---|
302 | //determine data for the prev link |
---|
303 | String prevBlockIdStr = null; |
---|
304 | String prevGenStamp = null; |
---|
305 | long prevStartOffset = 0; |
---|
306 | long prevBlockSize = 0; |
---|
307 | String prevHost = req.getServerName(); |
---|
308 | int prevPort = req.getServerPort(); |
---|
309 | int prevDatanodePort = datanodePort; |
---|
310 | if (startOffset == 0) { |
---|
311 | List<LocatedBlock> blocks = |
---|
312 | dfs.namenode.getBlockLocations(filename, 0, Long.MAX_VALUE).getLocatedBlocks(); |
---|
313 | for (int i = 0; i < blocks.size(); i++) { |
---|
314 | if (blocks.get(i).getBlock().getBlockId() == blockId) { |
---|
315 | if (i != 0) { |
---|
316 | LocatedBlock prevBlock = blocks.get(i-1); |
---|
317 | prevBlockIdStr = Long.toString(prevBlock.getBlock().getBlockId()); |
---|
318 | prevGenStamp = Long.toString(prevBlock.getBlock().getGenerationStamp()); |
---|
319 | prevStartOffset = prevBlock.getBlock().getNumBytes() - chunkSizeToView; |
---|
320 | if (prevStartOffset < 0) |
---|
321 | prevStartOffset = 0; |
---|
322 | prevBlockSize = prevBlock.getBlock().getNumBytes(); |
---|
323 | DatanodeInfo d = jspHelper.bestNode(prevBlock); |
---|
324 | String datanodeAddr = d.getName(); |
---|
325 | prevDatanodePort = Integer.parseInt( |
---|
326 | datanodeAddr.substring( |
---|
327 | datanodeAddr.indexOf(':') + 1, |
---|
328 | datanodeAddr.length())); |
---|
329 | prevHost = InetAddress.getByName(d.getHost()).getCanonicalHostName(); |
---|
330 | prevPort = d.getInfoPort(); |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | else { |
---|
336 | //we are in the same block |
---|
337 | prevBlockIdStr = blockIdStr; |
---|
338 | prevStartOffset = startOffset - chunkSizeToView; |
---|
339 | if (prevStartOffset < 0) prevStartOffset = 0; |
---|
340 | prevBlockSize = blockSize; |
---|
341 | prevGenStamp = blockGenStamp; |
---|
342 | } |
---|
343 | |
---|
344 | String prevUrl = null; |
---|
345 | if (prevBlockIdStr != null) { |
---|
346 | prevUrl = "http://" + prevHost + ":" + |
---|
347 | prevPort + |
---|
348 | "/browseBlock.jsp?blockId=" + prevBlockIdStr + |
---|
349 | "&blockSize=" + prevBlockSize + "&startOffset=" + |
---|
350 | prevStartOffset + |
---|
351 | "&filename=" + URLEncoder.encode(filename, "UTF-8") + |
---|
352 | "&chunkSizeToView=" + chunkSizeToView + |
---|
353 | "&genstamp=" + prevGenStamp + |
---|
354 | "&datanodePort=" + prevDatanodePort + |
---|
355 | "&namenodeInfoPort=" + namenodeInfoPort; |
---|
356 | out.print("<a href=\"" + prevUrl + "\">View Prev chunk</a> "); |
---|
357 | } |
---|
358 | out.print("<hr>"); |
---|
359 | out.print("<textarea cols=\"100\" rows=\"25\" wrap=\"virtual\" style=\"width:100%\" READONLY>"); |
---|
360 | try { |
---|
361 | jspHelper.streamBlockInAscii( |
---|
362 | new InetSocketAddress(req.getServerName(), datanodePort), blockId, |
---|
363 | genStamp, blockSize, startOffset, chunkSizeToView, out); |
---|
364 | } catch (Exception e){ |
---|
365 | out.print(e); |
---|
366 | } |
---|
367 | out.print("</textarea>"); |
---|
368 | dfs.close(); |
---|
369 | } |
---|
370 | |
---|
371 | |
---|
372 | private static java.util.List _jspx_dependants; |
---|
373 | |
---|
374 | public Object getDependants() { |
---|
375 | return _jspx_dependants; |
---|
376 | } |
---|
377 | |
---|
378 | public void _jspService(HttpServletRequest request, HttpServletResponse response) |
---|
379 | throws java.io.IOException, ServletException { |
---|
380 | |
---|
381 | JspFactory _jspxFactory = null; |
---|
382 | PageContext pageContext = null; |
---|
383 | HttpSession session = null; |
---|
384 | ServletContext application = null; |
---|
385 | ServletConfig config = null; |
---|
386 | JspWriter out = null; |
---|
387 | Object page = this; |
---|
388 | JspWriter _jspx_out = null; |
---|
389 | PageContext _jspx_page_context = null; |
---|
390 | |
---|
391 | |
---|
392 | try { |
---|
393 | _jspxFactory = JspFactory.getDefaultFactory(); |
---|
394 | response.setContentType("text/html; charset=UTF-8"); |
---|
395 | pageContext = _jspxFactory.getPageContext(this, request, response, |
---|
396 | null, true, 8192, true); |
---|
397 | _jspx_page_context = pageContext; |
---|
398 | application = pageContext.getServletContext(); |
---|
399 | config = pageContext.getServletConfig(); |
---|
400 | session = pageContext.getSession(); |
---|
401 | out = pageContext.getOut(); |
---|
402 | _jspx_out = out; |
---|
403 | |
---|
404 | out.write('\n'); |
---|
405 | out.write('\n'); |
---|
406 | out.write("\n<html>\n<head>\n"); |
---|
407 | JspHelper.createTitle(out, request, request.getParameter("filename")); |
---|
408 | out.write("\n</head>\n<body onload=\"document.goto.dir.focus()\">\n"); |
---|
409 | |
---|
410 | generateFileChunks(out,request); |
---|
411 | |
---|
412 | out.write("\n<hr>\n"); |
---|
413 | |
---|
414 | generateFileDetails(out,request); |
---|
415 | |
---|
416 | out.write("\n\n<h2>Local logs</h2>\n<a href=\"/logs/\">Log</a> directory\n\n"); |
---|
417 | |
---|
418 | out.println(ServletUtil.htmlFooter()); |
---|
419 | |
---|
420 | out.write('\n'); |
---|
421 | } catch (Throwable t) { |
---|
422 | if (!(t instanceof SkipPageException)){ |
---|
423 | out = _jspx_out; |
---|
424 | if (out != null && out.getBufferSize() != 0) |
---|
425 | out.clearBuffer(); |
---|
426 | if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); |
---|
427 | } |
---|
428 | } finally { |
---|
429 | if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); |
---|
430 | } |
---|
431 | } |
---|
432 | } |
---|