Hadoop状态页面的Browse the filesystem链接无效的问题

From: Hadoop状态页面的Browse the filesystem链接无效的问题 – 姚明技术博文—创新才是硬道理 – ITeye技术网站.

Note:简而言之,在本地host文件中添加所有hadoop cluster中节点的IP/域名,即可解决该问题。

NameNode ‘192.168.1.164:9000’

Started: Tue Jul 06 14:37:10 CST 2010
Version: 0.20.2, r911707
Compiled: Fri Feb 19 08:07:34 UTC 2010 by chrisdo
Upgrades: There are no upgrades in progress.

Browse the filesystem 
Namenode Logs


Cluster Summary

4 files and directories, 1 blocks = 5 total. Heap Size is 16.12 MB / 888.94 MB (1%)

Configured Capacity : 1.59 TB
DFS Used : 72 KB
Non DFS Used : 120.87 GB
DFS Remaining : 1.47 TB
DFS Used% : 0 %
DFS Remaining% : 92.57 %
Live Nodes : 2
Dead Nodes : 0

NameNode Storage:

Storage Directory Type State
/opt/hadoop/data/dfs.name.dir IMAGE_AND_EDITS Active

Hadoop , 2010.

  以上是我们安装完成,并且正常运行后的HDFS状态页面(访问地址:http://210.66.44.88:50070/dfshealth.jsp),其中的Browse the filesystem 是查看文件系统的入口,但是有可能会出现无法访问的问题,我就遇到过,上网查了很久的解决办法都无果,后来我通过firebug拦截请求发现,Browse the filesystem这个链接的页面会跳转到另外一个页面,而这个页面的地址是http://192.168.1.164:50075/browseDirectory.jsp?namenodeInfoPort=50070&dir=%2F(192.168.1.164是服务器的内网地址,在masters和slaves文件中配置的是局域网IP),但是需要通过外网才能访问(类似于http://210.66.44.88:50075/browseDirectory.jsp?namenodeInfoPort=50070&dir=%2F才行),这就是问题所在。

  我们来看看nn_browsedfscontent.jsp的源代码:

  1. package org.apache.hadoop.hdfs.server.namenode;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.InetAddress;  
  5. import java.net.InetSocketAddress;  
  6. import java.net.URLEncoder;  
  7. import java.util.Vector;  
  8. import javax.servlet.ServletConfig;  
  9. import javax.servlet.ServletContext;  
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13. import javax.servlet.http.HttpSession;  
  14. import javax.servlet.jsp.JspFactory;  
  15. import javax.servlet.jsp.JspWriter;  
  16. import javax.servlet.jsp.PageContext;  
  17. import javax.servlet.jsp.SkipPageException;  
  18. import org.apache.hadoop.util.ServletUtil;  
  19. import org.apache.jasper.runtime.HttpJspBase;  
  20. import org.apache.jasper.runtime.JspSourceDependent;  
  21. import org.apache.jasper.runtime.ResourceInjector;  
  22.   
  23. public final class nn_005fbrowsedfscontent_jsp extends HttpJspBase  
  24.   implements JspSourceDependent  
  25. {  
  26.   private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();  
  27.   private static Vector _jspx_dependants;  
  28.   private ResourceInjector _jspx_resourceInjector;  
  29.   
  30.   //此处是将请求随机转发到一个DataNode节点上  
  31.   public void redirectToRandomDataNode(NameNode nn, HttpServletResponse resp)  
  32.     throws IOException  
  33.   {  
  34.     String nodeToRedirect;  
  35.     int redirectPort;  
  36.     FSNamesystem fsn = nn.getNamesystem();  
  37.     String datanode = fsn.randomDataNode();  
  38.   
  39.     if (datanode != null) {  
  40.       redirectPort = Integer.parseInt(datanode.substring(datanode.indexOf(58) + 1));  
  41.       nodeToRedirect = datanode.substring(0, datanode.indexOf(58));  
  42.     }  
  43.     else {  
  44.       nodeToRedirect = nn.getHttpAddress().getHostName();  
  45.       redirectPort = nn.getHttpAddress().getPort();  
  46.     }  
  47.     // 此处是得到服务器的名称(hostname)  
  48.     String fqdn = InetAddress.getByName(nodeToRedirect).getCanonicalHostName();  
  49.     String redirectLocation = http://” + fqdn + “:” + redirectPort + “/browseDirectory.jsp?namenodeInfoPort=” + nn.getHttpAddress().getPort() + “&dir=” + URLEncoder.encode(“/”“UTF-8”);  
  50.   
  51.     resp.sendRedirect(redirectLocation);  
  52.   }  
  53.   
  54.   public Object getDependants()  
  55.   {  
  56.     return _jspx_dependants;  
  57.   }  
  58.   
  59.   public void _jspService(HttpServletRequest request, HttpServletResponse response)  
  60.     throws IOException, ServletException  
  61.   {  
  62.     PageContext pageContext = null;  
  63.     HttpSession session = null;  
  64.     ServletContext application = null;  
  65.     ServletConfig config = null;  
  66.     JspWriter out = null;  
  67.     Object page = this;  
  68.     JspWriter _jspx_out = null;  
  69.     PageContext _jspx_page_context = null;  
  70.     try  
  71.     {  
  72.       response.setContentType(“text/html; charset=UTF-8”);  
  73.       pageContext = _jspxFactory.getPageContext(this, request, response, nulltrue8192true);  
  74.   
  75.       _jspx_page_context = pageContext;  
  76.       application = pageContext.getServletContext();  
  77.       config = pageContext.getServletConfig();  
  78.       session = pageContext.getSession();  
  79.       out = pageContext.getOut();  
  80.       _jspx_out = out;  
  81.       this._jspx_resourceInjector = ((ResourceInjector)application.getAttribute(“com.sun.appserv.jsp.resource.injector”));  
  82.   
  83.       out.write(10);  
  84.       out.write(“\n\n<html>\n\n<title></title>\n\n<body>\n”);  
  85.   
  86.       NameNode nn = (NameNode)application.getAttribute(“name.node”);  
  87.       redirectToRandomDataNode(nn, response);  
  88.   
  89.       out.write(“\n<hr>\n\n<h2>Local logs</h2>\n<a href=\”/logs/\”>Log</a> directory\n\n”);  
  90.   
  91.       out.println(ServletUtil.htmlFooter());  
  92.   
  93.       out.write(10);  
  94.     } catch (Throwable t) {  
  95.       if (!(t instanceof SkipPageException)) {  
  96.         out = _jspx_out;  
  97.         if ((out != null) && (out.getBufferSize() != 0))  
  98.           out.clearBuffer();  
  99.         if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);  
  100.       }  
  101.     } finally {  
  102.       _jspxFactory.releasePageContext(_jspx_page_context);  
  103.     }  
  104.   }  
  105. }  

  从代码可以看出,当我们点击Browse the filesystem 时后台会将请求随机转发到一台DataNode节点,使用的是slaves文件中配置的服务器列表所反解的域名或主机名,而通过局域网IP未能反解出域名和主机名,所以用的是IP,这样就出问题了,解决办法有两个:视redirectToRandomDataNode方法内生成的URL而定,如果反转域名是主机名的话,你只需要修改本地HOSTS映射就可以了(推荐使用Windows Hosts Editor,软件地址:http://yymmiinngg.iteye.com/blog/360779);如果反转出的域名是主机局域网IP的话,那就需要配置slaves和masters使用域名或外网IP