HDFS-5274 添加了对通过 HDFS 跟踪请求的支持,使用开源跟踪库 Apache HTrace。设置跟踪非常简单,但需要对客户端代码进行一些非常小的更改。
跟踪系统通过在称为“跨度”的结构中收集信息来工作。由您选择如何使用 HTrace 捆绑的 SpanReceiver 接口实现或自行实现来接收此信息。
HTrace 提供了以下选项
有关 HTrace 配置键的说明,请参阅 core-default.xml。在某些情况下,您还需要将包含您正在使用的 SpanReceiver 的 jar 添加到每个节点上 Hadoop 的类路径中。(在上面的示例中,LocalFileSpanReceiver 包含在与 Hadoop 捆绑在一起的 htrace-core4 jar 中。)
$ cp htrace-htraced/target/htrace-htraced-4.1.0-incubating.jar $HADOOP_HOME/share/hadoop/common/lib/
您可以使用 hadoop trace 命令查看和更新每个服务器的跟踪配置。您必须通过 -host 选项指定 namenode 或 datanode 的 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
如果集群已进行 Kerberos 化,则必须使用 -principal 选项指定服务主体名称。例如,要显示 namenode 的 span 接收器列表
$ hadoop trace -list -host NN1:8020 -principal namenode/NN1@EXAMPLE.COM
或者,对于 datanode
$ 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>