HDFS-5274 加入支援透過 HDFS 追蹤要求,使用開源追蹤程式庫 Apache HTrace。設定追蹤相當簡單,但需要對用戶端程式碼做一些非常小的變更。
追蹤系統會透過收集稱為「範圍」的結構中的資訊來運作。您可以透過使用與 HTrace 捆綁或自行實作 SpanReceiver 介面的實作,選擇如何接收這些資訊。
HTrace 提供選項,例如
請參閱 core-default.xml 以取得 HTrace 組態金鑰的說明。在某些情況下,您還需要將包含您正在使用的 SpanReceiver 的 jar 檔新增到每個節點上 Hadoop 的類別路徑。(在上面的範例中,LocalFileSpanReceiver 包含在 htrace-core4 jar 檔中,而該 jar 檔與 Hadoop 捆綁在一起。)
$ cp htrace-htraced/target/htrace-htraced-4.1.0-incubating.jar $HADOOP_HOME/share/hadoop/common/lib/
您可以使用 hadoop trace
指令來查看和更新每個伺服器的追蹤組態。您必須透過 -host
選項指定名稱節點或資料節點的 IPC 伺服器位址。如果您想要更新所有伺服器的組態,您需要針對所有伺服器執行指令。
hadoop trace -list
會顯示與 id 相關聯的已載入 span 接收器的清單。
$ hadoop trace -list -host 192.168.56.2:9000 ID CLASS 1 org.apache.htrace.core.LocalFileSpanReceiver $ hadoop trace -list -host 192.168.56.2:9867 ID CLASS 1 org.apache.htrace.core.LocalFileSpanReceiver
hadoop trace -remove
會從伺服器移除 span 接收器。-remove
選項會將 span 接收器的 id 作為引數。
$ hadoop trace -remove 1 -host 192.168.56.2:9000 Removed trace span receiver 1
hadoop trace -add
會將 span 接收器新增到伺服器。您需要將 span 接收器的類別名稱指定為 -class
選項的引數。您可以透過 -Ckey=value
選項指定與 span 接收器相關聯的組態。
$ hadoop trace -add -class org.apache.htrace.core.LocalFileSpanReceiver -Chadoop.htrace.local.file.span.receiver.path=/tmp/htrace.out -host 192.168.56.2:9000 Added trace span receiver 2 with configuration hadoop.htrace.local.file.span.receiver.path = /tmp/htrace.out $ hadoop trace -list -host 192.168.56.2:9000 ID CLASS 2 org.apache.htrace.core.LocalFileSpanReceiver
如果叢集是 Kerberized,則必須使用 -principal
選項指定服務主體名稱。例如,若要顯示名稱節點的 span 接收器清單
$ hadoop trace -list -host NN1:8020 -principal namenode/NN1@EXAMPLE.COM
或,對於資料節點
$ hadoop trace -list -host DN2:9867 -principal datanode/DN1@EXAMPLE.COM
為了追蹤,您需要使用 追蹤 span 包裝被追蹤的邏輯,如下所示。當有執行的追蹤 span 時,追蹤資訊會與 RPC 要求一起傳播到伺服器。
import org.apache.hadoop.hdfs.HdfsConfiguration; import org.apache.htrace.core.Tracer; import org.apache.htrace.core.TraceScope; ... ... TraceScope ts = tracer.newScope("Gets"); try { ... // traced logic } finally { ts.close(); }
下面顯示的 TracingFsShell.java
是 FsShell 的包裝器,它會在呼叫 HDFS shell 指令之前開始追蹤 span。
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.tracing.TraceUtils; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import org.apache.htrace.core.Tracer; import org.apache.htrace.core.TraceScope; public class Sample extends Configured implements Tool { @Override public int run(String argv[]) throws Exception { FileSystem fs = FileSystem.get(getConf()); Tracer tracer = new Tracer.Builder("Sample"). conf(TraceUtils.wrapHadoopConf("sample.htrace.", getConf())). build(); int res = 0; try (TraceScope scope = tracer.newScope("sample")) { Thread.sleep(1000); fs.listStatus(new Path("/")); } tracer.close(); return res; } public static void main(String argv[]) throws Exception { ToolRunner.run(new Sample(), argv); } }
您可以編譯並執行此程式碼,如下所示。
$ javac -cp `hadoop classpath` Sample.java $ java -cp .:`hadoop classpath` Sample \ -Dsample.htrace.span.receiver.classes=LocalFileSpanReceiver \ -Dsample.htrace.sampler.classes=AlwaysSampler
FileSystem Shell 可以透過組態屬性啟用追蹤。
使用屬性 fs.client.htrace.sampler.classes
和 fs.client.htrace.spanreceiver.classes
在 core-site.xml
或透過屬性命令列中組態 span 接收器和取樣器。
$ hdfs dfs -Dfs.shell.htrace.span.receiver.classes=LocalFileSpanReceiver \ -Dfs.shell.htrace.sampler.classes=AlwaysSampler \ -ls /
DFSClient 可以內部啟用追蹤。這讓您可以與您的伺服器一起使用 HTrace,而不用修改伺服器原始程式碼。
使用屬性 fs.client.htrace.sampler.classes
和 fs.client.htrace.spanreceiver.classes
在 hdfs-site.xml
中組態 span 接收器和取樣器。fs.client.htrace.sampler.classes
的值可以是 NeverSampler、AlwaysSampler 或 ProbabilitySampler。
<property> <name>hadoop.htrace.span.receiver.classes</name> <value>LocalFileSpanReceiver</value> </property> <property> <name>fs.client.htrace.sampler.classes</name> <value>ProbabilitySampler</value> </property> <property> <name>fs.client.htrace.sampler.fraction</name> <value>0.01</value> </property>