2021SC@SDUSC
本篇我将对hadoop-common中的Tracing进行分析
Enabling Dapper-like Tracing in Hadoop
Dapper-like Tracing in Hadoop
HTrace
HDFS-5274增加了对通过HDFS跟踪请求的支持,使用开源跟踪库,Apache HTrace设置跟踪非常简单,但是需要对客户端代码进行一些非常小的更改。
SpanReceivers
跟踪系统通过在名为“跨度”的结构中收集信息来工作。您可以通过使用的实现来选择接收此信息的方式SpanReceiver接口与HTrace捆绑在一起或者自己实现。
Htrace提供如下选项
- FlumeSpanReceiver
- HBaseSpanReceiver
- HTracedRESTReceiver
- ZipkinSpanReceiver
在某些情况下,需要将正在使用的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跟踪命令查看和更新每台服务器的跟踪配置。您必须通过以下方式指定名称节点或数据节点的IPC服务器地址主持人选项。如果要更新所有服务器的配置,需要对所有服务器运行该命令。
hadoop跟踪列表显示与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跟踪-删除从服务器中删除span接收器。-移除options以span接收器的id为参数。
$ hadoop trace -remove 1 -host 192.168.56.2:9000
Removed trace span receiver 1
hadoop跟踪-添加向服务器添加span接收器。您需要指定span接收器的类名作为的参数-上课选项。您可以通过以下方式指定与span接收器关联的配置-Ckey =值选项。
$ 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化的,则必须使用指定服务主体名称校长选项。例如,要显示名称节点的跨度接收器列表:
$ hadoop trace -list -host NN1:8020 -principal namenode/NN1@EXAMPLE.COM
或者,对于数据节点:
$ hadoop trace -list -host DN2:9867 -principal datanode/DN1@EXAMPLE.COM
通过HTrace API开始跟踪范围
为了进行跟踪,需要用跟踪跨度如下所示。当存在正在运行的跟踪跨度时,跟踪信息将与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();
}
HTrace API跟踪的示例代码
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
通过文件系统外壳程序开始追踪范围
文件系统外壳可以通过配置属性启用跟踪。
在中配置量程接收器和采样器核心网站. xml或命令行属性fs . client . htrace . sampler . class和fs . client . htrace . span receiver . class。
$ hdfs dfs -Dfs.shell.htrace.span.receiver.classes=LocalFileSpanReceiver \
-Dfs.shell.htrace.sampler.classes=AlwaysSampler \
-ls /
根据HDFS客户端的配置开始追踪范围
DFSClient可以在内部启用跟踪。这允许您在不修改客户端源代码的情况下对客户端使用HTrace。
在中配置量程接收器和采样器hdfs-site.xml按属性fs . client . htrace . sampler . class和fs . client . htrace . span receiver . class。的价值fs . client . htrace . sampler . class可以是NeverSampler、AlwaysSampler或概率Sampler。
- NeverSampler:对于名称节点和数据节点的所有请求,HTrace都是关闭的;
- AlwaysSampler:对于名称节点和数据节点的所有请求,HTrace为ON;
- 概率示例:对于名称节点和数据节点的请求,HTrace打开了一定百分比的百分比
<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>