让hadoop远程调试飞

来源:互联网 发布:系统数据恢复 编辑:程序博客网 时间:2024/06/03 10:20

读hadoop源代码时,经常需要调试源代码。hadoop几乎所有的进程的启动最终全部依靠$HADOOP_HOME/bin/hadoop 脚本实现,开始就简单的在需要的地方添加JDWP的debug选项,做的多了,感觉不够自动化,简单修改了一下hadoop这个脚本,让自动化远程调试飞!

修改的脚本如下:

 

在$HADOOP_HOME/bin/hadoop中后添加:

 

 

[c-sharp] view plaincopy
  1. "$bin"/hadoop-config.sh  
  2. choose_debug_port()  
  3. {  
  4.         debug_port_base=11000  
  5.         while [ -z "$DEBUG_PORT" ]  
  6.         do  
  7.                 if [ "$(netstat -tln |grep $debug_port_base |wc -l)" -eq 0 ]   
  8.                 then  
  9.                         DEBUG_PORT=$debug_port_base  
  10.                 else  
  11.                         debug_port_base=$(($debug_port_base+1))  
  12.                 fi    
  13.         done  
  14. }  
  15. debug_file="$bin/hadoop.debug"  
  16. choose_debug_port  
  17. is_debug_enabled()  
  18. {  
  19.         if [ -f "$debug_file" ]  
  20.         then  
  21.                 echo $(cat $debug_file | grep $1)  
  22.         fi    
  23. }  
 

 

在$HADOOP_HOME/bin/hadoop中246行后添加( # cygwin path translation前):

 

[c-sharp] view plaincopy
  1. if [ $(is_debug_enabled $COMMAND) ]  
  2. then  
  3.         echo "debug for $COMMAND is enabled, port:$DEBUG_PORT"  
  4.         export HADOOP_OPTS="$HADOOP_OPTS -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=$DEBUG_PORT,suspend=y,server=y "  
  5. fi  
 

 

这样,hadoop脚本会根据COMMAND和$HADOOP_HOME/bin/hadoop.debug 内容决定是否开启调试。

 

比如,想调试datanode,执行echo datanode > $HADOOP_HOME/bin/hadoop.debug

启动hadoop时,仅仅datanode会开启remote debug

console显示:

[c-sharp] view plaincopy
  1. debug for datanode is enabled, port:11000  
  2. Listening for transport dt_socket at address: 11000  
 

 

想开启namenode等调试,仅仅需要echo 你想调试的command > $HADOOP_HOME/bin/hadoop.debug 

hadoop命令支持的所有COMMAND都可以轻松开启调试:

附hadoop所有命令:

[c-sharp] view plaincopy
  1. hadoop@haitaoyao-laptop:~/hadoop/bin$ hadoop   
  2. Usage: hadoop [--config confdir] COMMAND  
  3. where COMMAND is one of:  
  4.   namenode -format     format the DFS filesystem  
  5.   secondarynamenode    run the DFS secondary namenode  
  6.   namenode             run the DFS namenode  
  7.   datanode             run a DFS datanode  
  8.   dfsadmin             run a DFS admin client  
  9.   mradmin              run a Map-Reduce admin client  
  10.   fsck                 run a DFS filesystem checking utility  
  11.   fs                   run a generic filesystem user client  
  12.   balancer             run a cluster balancing utility  
  13.   jobtracker           run the MapReduce job Tracker node  
  14.   pipes                run a Pipes job  
  15.   tasktracker          run a MapReduce task Tracker node  
  16.   job                  manipulate MapReduce jobs  
  17.   queue                get information regarding JobQueues  
  18.   version              print the version  
  19.   jar <jar>            run a jar file  
  20.   distcp <srcurl> <desturl> copy file or directories recursively  
  21.   archive -archiveName NAME <src>* <dest> create a hadoop archive  
  22.   daemonlog            get/set the log level for each daemon  
  23.  or  
  24.   CLASSNAME            run the class named CLASSNAME  
 

 

0 0
原创粉丝点击