單純看書翻源碼很是枯燥,爲了單步debug追Hadoop源碼,最好先在部署環境編譯一份源碼,以免各類環境問題。html
本文記錄了猴子在本身的Mac上編譯Hadoop源碼的過程,結合以前的一次編譯經驗,基本覆蓋了編譯Hadoop源碼時可能遇到的主要問題。java
隨着debug的進一步深刻,後期可能涉及到對源碼的修改,須要屢次從新編譯。所以,這一關是繞不過的。node
Apache Hadoop 2.6.0
macOS 10.12.4
oracle jdk 1.7.0_79
Apache Maven 3.5.0
libprotoc 2.5.0
mvn install -DskipTests -Pdist,native -Dtar
複製代碼
Hadoop源碼量巨大、依賴衆多,編譯時間比較長。linux
下載jar包和編譯protoc是兩個大頭。編譯protoc用了1小時左右,下載jar包+編譯Hadoop用了2個多小時。除去這些時間,也須要1小時左右才能編譯成功。c++
還好上半年爲了看Yarn的狀態機編譯過一回,雖然是不徹底編譯,但也下載了大部分依賴的jar包,並編譯安裝了protoc(強烈建議編譯安裝,忘記當時有什麼坑來着)。此次只須要繼續踩上次剩下的坑了。git
不過,鑑於第一次編譯時,大部分人都會重複屢次才能編譯成功,單次編譯的時間也沒什麼意義了。喝杯茶,慢慢來吧。github
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project hadoop-annotations: Compilation failure: Compilation failure:
[ERROR] /Volumes/Extended/Users/msh/IdealProjects/Git-Study/hadoop/hadoop-common-project/hadoop-annotations/src/main/java/org/apache/hadoop/classification/tools/ExcludePrivateAnnotationsJDiffDoclet.java:[20,22] 錯誤: 程序包com.sun.javadoc不存在
複製代碼
不明白爲啥這個包會不存在,多是JDK版本問題。google一番,參考解決Mac OS 下編譯Hadoop Annotations 程序包com.sun.javadoc找不到問題解決。apache
在全部的pom.xml裏面找設置1.7 jdk的地方:bash
find . -name pom.xml > tmp/tmp.txt
while read file
do
cnt=0
grep '1.7' $file -C2 | while read line; do
if [ -n "$line" ]; then
if [ $cnt -eq 0 ]; then
echo "+++file: $file"
fi
cnt=$((cnt+1))
echo $line
fi
done
cnt=0
done < tmp/tmp.txt
複製代碼
輸出:oracle
+++file: ./hadoop-common-project/hadoop-annotations/pom.xml
</profile>
<profile>
<id>jdk1.7</id>
<activation>
--
<activation>
<jdk>1.7</jdk>
</activation>
<dependencies>
--
--
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
+++file: ./hadoop-project/pom.xml
...(略)
複製代碼
確實./hadoop-common-project/hadoop-annotations/pom.xml
中限制了jdk版本。
猴子Mac上的默認JDK是oracle jdk1.8.0_102
的,翻了下jdk源碼也有這個包。說明不是由於該包實際不存在。
能夠嘗試修改pom裏限制的jdk版本;不過,爲了防止使用了deprecated方法等麻煩,這裏直接切jdk 1.7,不改pom。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (make) on project hadoop-pipes: An Ant BuildException has occured: exec returned: 1
[ERROR] around Ant part ...<exec dir="/Volumes/Extended/Users/msh/IdealProjects/Git-Study/hadoop/hadoop-tools/hadoop-pipes/target/native" executable="cmake" failonerror="true">... @ 5:153 in /Volumes/Extended/Users/msh/IdealProjects/Git-Study/hadoop/hadoop-tools/hadoop-pipes/target/antrun/build-main.xml
複製代碼
猜想是ant版本問題,重裝了jdk1.7適配的ant。
Ant BuildException
也是夠迷惑的。並且以前猴子電腦配置的jdk1.8,切到1.7以後ant就不能用了(brew安裝的ant用1.8jdk編譯的,1.7沒法解析class文件),重裝了適配1.7的ant版本後,ant能夠正常使用了,卻仍是報這個錯。。。
結果仍是報這個錯,打開build-main.xml看,發現是一個cmake命令的配置,copy到終端執行:
cmake /Volumes/Extended/Users/msh/IdealProjects/Git-Study/hadoop/hadoop-tools/hadoop-pipes/src/ -DJVM_ARCH_DATA_MODEL=64
複製代碼
輸出:
...(略)
CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at /usr/local/Cellar/cmake/3.6.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
system variable OPENSSL_ROOT_DIR (missing: OPENSSL_INCLUDE_DIR)
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.6.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
/usr/local/Cellar/cmake/3.6.2/share/cmake/Modules/FindOpenSSL.cmake:380 (find_package_handle_standard_args)
CMakeLists.txt:20 (find_package)
...(略)
複製代碼
OPENSSL_ROOT_DIR
、OPENSSL_INCLUDE_DIR
沒有設置。echo一下確實沒有設置。
Mac自帶OpenSSL,然而猴子並不知道哪裏算是root,哪裏算是include;另外,聽說mac計劃移除默認的openssl。乾脆本身從新安裝:
brew install openssl
複製代碼
而後配置環境變量:
export OPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2n
export OPENSSL_INCLUDE_DIR=$OPENSSL_ROOT_DIR/include
複製代碼
[ERROR] Failed to execute goal on project hadoop-aws: Could not resolve dependencies for project org.apache.hadoop:hadoop-aws:jar:2.6.0: Could not transfer artifact com.amazonaws:aws-java-sdk:jar:1.7.4 from/to central (https://repo.maven.apache.org/maven2): GET request of: com/amazonaws/aws-java-sdk/1.7.4/aws-java-sdk-1.7.4.jar from central failed: SSL peer shut down incorrectly -> [Help 1]
複製代碼
出現相似「Could not resolve dependencies」、「SSL peer shut down incorrectly」等語句,通常是maven不穩定,換個穩定的maven源,或者從新編譯多試幾回。
上次編譯有個小坑,是Hadoop源碼裏的歷史遺留問題。
編譯過程當中會在$JAVA_HOME/Classes
下找一個並不存在的jar包classes.jar
,實際上須要的是$JAVA_HOME/lib/tools.jar
,加個軟鏈就好(注意mac加軟鏈時與linux的區別)。
所以上次編譯猴子已經修復了這個問題,這裏就不復現了。具體能夠看這篇mac下編譯Hadoop
沒有skipTests的話,至少會在測試過程當中如下錯誤:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.16:test (default-test) on project hadoop-auth: There are test failures.
[ERROR]
[ERROR] Please refer to /Volumes/Extended/Users/msh/IdealProjects/Git-Study/hadoop/hadoop-common-project/hadoop-auth/target/surefire-reports for the individual test results.
Results :
Tests in error:
TestKerberosAuthenticator.testAuthenticationHttpClientPost:157 » ClientProtocol
TestKerberosAuthenticator.testAuthenticationHttpClientPost:157 » ClientProtocol
Tests run: 92, Failures: 0, Errors: 2, Skipped: 0
複製代碼
能夠暫時忽略這些相關錯誤,skipTests跳過測試,能追蹤源碼瞭解主要過程便可。
編譯成功後,在hadoop-dist
模塊的target
目錄下,生成了各類發行版。選擇hadoop-2.6.0.tar.gz
,找個地方解壓。
參考官網配置etc/hadoop
目錄下的core-site.xml
、hdfs-site.xml
、yarn-site.xml
、mapred-site.xml
等。
bin/hdfs namenode -format
複製代碼
# 啓動 Namenode、SecondaryNamenode(僞分佈式沒法開啓HA)、Datanode
sbin/start-dfs.sh
# 啓動 ResourceManager、NodeManager
sbin/start-yarn.sh
# 啓動 TimelineServer(ApplicationHistoryServer)
sbin/yarn-daemon.sh start timelineserver
複製代碼
啓動後,訪問NameNode、ResourceManager的Web UI,Timeline的RESTful接口,建立目錄、上傳文件,跑示例MapReduce,以驗證是否成功部署。
本文連接:Mac編譯Hadoop源碼
做者:猴子007
出處:monkeysayhi.github.io
本文基於 知識共享署名-相同方式共享 4.0 國際許可協議發佈,歡迎轉載,演繹或用於商業目的,可是必須保留本文的署名及連接。