Flume的Avro Sink和Avro Source研究之二 : Avro Sink
啊,AvroSink要复杂好多:《
好吧,先确定主要问题:
- AvroSink为啥这么多代码?有必要吗?它都有哪些逻辑需要实现?
你看,avro-rpc-quickstart里是这么建client,然后进行RPC的
NettyTransceiver client = new NettyTransceiver(new InetSocketAddress(65111));
// client code - attach to the server and send a message
Mail proxy = (Mail) SpecificRequestor.getClient(Mail.class, client);
proxy.send(message);
那么,AvroSink为啥不是这么简单?它会启动多个线程,并发的RPC? 它会使用连接池?它自己实现了一个?
AvroSink继承自 AbstractRpcSink. AbstractRpcSink对Sink接口的process方法的实现为,由自己持有的RpcClient对象来对消息进行实际处理,即 client.appendBatch(batch);。而AvroSink实现AbstractRpcSink中的虚方法 "protected abstract RpcClient initializeRpcClient(Properties props)" 来提供一个可用的RpcClient。它的实现为:
protected RpcClient initializeRpcClient(Properties props) {
logger.info("Attempting to create Avro Rpc client.");
return RpcClientFactory.getInstance(props);
}
而RpcClientFactory的getInstance方法当“client.type"参数为空时,返回默认的RpcClient,即 NettyAvroRpcClient。
NettyAvroRpcClient
在它的"private void connect(long timeout, TimeUnit tu) throws FlumeException"方法中,实始化进行RPC所需要的代理,即此类中avroClient域。
transceiver = new NettyTransceiver(this.address, socketChannelFactory, tu.toMillis(timeout));
avroClient = SpecificRequestor.getClient(AvroSourceProtocol.Callback.class, transceiver);
avroClient可以代理AvroSourceProtocol.Callback.class,这个AvroSourceProtocol.Callback.class 定义了跟AvroSourceProtocol相似的接口,不过增加了一个参数用来进行回调。
@org.apache.avro.specific.AvroGenerated
public interface AvroSourceProtocol {
public static final org.apache.avro.Protocol PROTOCOL = org.apache.avro.Protocol.parse("{\"protocol\":\"AvroSourceProtocol\",\"namespace\":\"org.apache.flume.source.avro\",\"doc\":\"* Licensed to the Apache Software Foundation (ASF) under one\\n * or more contributor license agreements. See the NOTICE file\\n * distributed with this work for additional information\\n * regarding copyright ownership. The ASF licenses this file\\n * to you under the Apache License, Version 2.0 (the\\n * \\\"License\\\"); you may not use this file except in compliance\\n * with the License. You may obtain a copy of the License at\\n *\\n * http://www.apache.org/licenses/LICENSE-2.0\\n *\\n * Unless required by applicable law or agreed to in writing,\\n * software distributed under the License is distributed on an\\n * \\\"AS IS\\\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\\n * KIND, either express or implied. See the License for the\\n * specific language governing permissions and limitations\\n * under the License.\",\"types\":[{\"type\":\"enum\",\"name\":\"Status\",\"symbols\":[\"OK\",\"FAILED\",\"UNKNOWN\"]},{\"type\":\"record\",\"name\":\"AvroFlumeEvent\",\"fields\":[{\"name\":\"headers\",\"type\":{\"type\":\"map\",\"values\":\"string\"}},{\"name\":\"body\",\"type\":\"bytes\"}]}],\"messages\":{\"append\":{\"request\":[{\"name\":\"event\",\"type\":\"AvroFlumeEvent\"}],\"response\":\"Status\"},\"appendBatch\":{\"request\":[{\"name\":\"events\",\"type\":{\"type\":\"array\",\"items\":\"AvroFlumeEvent\"}}],\"response\":\"Status\"}}}");
org.apache.flume.source.avro.Status append(org.apache.flume.source.avro.AvroFlumeEvent event) throws org.apache.avro.AvroRemoteException;
org.apache.flume.source.avro.Status appendBatch(java.util.List<org.apache.flume.source.avro.AvroFlumeEvent> events) throws org.apache.avro.AvroRemoteException;@SuppressWarnings("all")
public interface Callback extends AvroSourceProtocol {
public static final org.apache.avro.Protocol PROTOCOL = org.apache.flume.source.avro.AvroSourceProtocol.PROTOCOL;
void append(org.apache.flume.source.avro.AvroFlumeEvent event, org.apache.avro.ipc.Callback<org.apache.flume.source.avro.Status> callback) throws java.io.IOException;
void appendBatch(java.util.List<org.apache.flume.source.avro.AvroFlumeEvent> events, org.apache.avro.ipc.Callback<org.apache.flume.source.avro.Status> callback) throws java.io.IOException;
}
}
下边看下NettyAvroRpcClient是怎么实现其RpcClient接口的append和appendBatch方法的。
它所override的append(Event event)方法中,把消息的处理交给自己的append(Event event, long timeout, TimeUnit tu)来处理。
append方法首先将Event对象转化为用于RPC的AvroFlumeEvent对象。然后把RPC的动作提交给一个线程池。
try {
// due to AVRO-1122, avroClient.append() may block
handshake = callTimeoutPool.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
avroClient.append(avroEvent, callFuture);
return null;
}
});
} catch (RejectedExecutionException ex) {
throw new EventDeliveryException(this + ": Executor error", ex);
}
avroClient.append中有一个callFuture参数,future代表了一个异步执行的结果,所以它所被期望的行为是append方法会立即返回,然后另一个线程通过callFuture对象来获取执行的结果。但是实际上,由于avro RPC 之前有个handshake的过程用于确认双方持有的shema是否合适,这个handshake的过程会阻塞client端RPC调用的方法,即会阻塞client端的append方法,使得在提交任务后,直接使用callFuture的get(timeout),若append方法执行完返回了结果,那这个timeout实际上可能包括了handshake的时长加上server端实际执行append方法的时长。因此AvroSink把这两个时长都设为可配置的,即用户可以设定handshake的花的时长,以及等待server端处理请求的时长。但是这个handshake只在client和server第一次通信时进行。所以后续的client端的append RPC调用会立即返回,不再需要等待handshake。
看一下AvroSink的配置选项。
connect-timeout | 20000 | Amount of time (ms) to allow for the first (handshake) request. |
request-timeout | 20000 | Amount of time (ms) to allow for requests after the first. |
connect-timeout的设置是通过下面的代码实现的。等待这个Callable执行完,如果超时,就取消这个Callable.去掉异常处理后的代码是这样子:
handshake.get(connectTimeout, TimeUnit.MILLISECONDS);
finally {
if (!handshake.isDone()) {
handshake.cancel(true);
}
} waitForStatusOK(callFuture, timeout, tu);
而waitForStatusOK是这样子:
try {
Status status = callFuture.get(timeout, tu);
if (status != Status.OK) {
throw new EventDeliveryException(this + ": Avro RPC call returned " +
"Status: " + status);
}
即, append方法会根据在flume配置文件里设置的超时参数进行等待。调用append方法的线程还是会阻塞到这个消息处理完毕。
由于handshake会阻塞RPC调用,而handshake花的时间是不确定的,所以才不得不使用一个线程池,即callTimeoutPool来将append这个RPC调用放在单独的Callable里执行,用Future对RPC的执行情况进行监控,如果append进行过长时间的等待,就通过future取消这个任务。真是用心良苦……
Flume的Avro Sink和Avro Source研究之二 : Avro Sink的更多相关文章
- Flume的Avro Sink和Avro Source研究之一: Avro Source
问题 : Avro Source提供了怎么样RPC服务,是怎么提供的? 问题 1.1 Flume Source是如何启动一个Netty Server来提供RPC服务. 由GitHub上avro-rpc ...
- [ETL] Flume 理论与demo(Taildir Source & Hdfs Sink)
一.Flume简介 1. Flume概述 Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据: ...
- hadoop深入研究:(十六)——Avro序列化与反序列化
转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/9773233 所有源码在github上,https://github.com/l ...
- WARN conf.FlumeConfiguration: Could not configure sink sink1 due to: No channel configured for sink: sink1 org.apache.flume.conf.ConfigurationException: No channel configured for sink: sink1
1.错误如下所示,启动flume采集文件到hdfs案例的时候,出现如下所示的错误: 大概是说No channel configured for sink,所以应该是sink哪里配置出现了错误,百度了一 ...
- 二、Sink例程
1. Sink例程 CSR粗略的将audio蓝牙设备分为了两大类:sink和source设备,并分别提供了两类设备的例程,配置工具,说明文档.如对于sink设备,提供了sink app例程,SinkU ...
- 关于ADMM的研究(二)
关于ADMM的研究(二) 4. Consensus and Sharing 本节讲述的两个优化问题,是非常常见的优化问题,也非常重要,我认为是ADMM算法通往并行和分布式计算的一个途径:consens ...
- hadoop深入研究:(十八)——Avro schema兼容
转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/9900129 所有源码在github上,https://github.com/l ...
- Hadoop实战-Flume之Source regex_extractor(十二)
a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = ...
- 【原创】JPEG图像密写研究(二) 哈夫曼树的建立
[原创]记录自己研究的过程,仅供参考,欢迎讨论... 在根据JPEG图像文件结构读取完文件后,提取出其中DHT段,利用其中内容建立哈夫曼树,便于之后译码工作.这里需要注意的是文件中的哈夫曼表数量不固定 ...
随机推荐
- 导出excel页面假死
如果是asp.net页面 public override void VerifyRenderingInServerForm(Control control) {} 如果是Sharepoint w ...
- 《从零开始学Swift》学习笔记(Day 17)——Swift中数组集合
原创文章,欢迎转载.转载请注明:关东升的博客 数组(Array)是一串有序的由相同类型元素构成的集合.数组中的集合元素是有序的,可以重复出现. 声明一个Array类型的时候可以使用下面的语句之一. v ...
- 关于angularjs的select下拉列表存在空白的解决办法
angularjs 的select的option是通过循环造成的,循环的方式可能有ng-option或者</option ng-repeat></option>option中 ...
- 2018.10.24-day3 python总结
昨日回顾:1.while2.运算符3.初始编码4.补充p2和p3的区别 Python2 (1) 今日学习目录1.整型 int() 2.布尔值 bool() 3.字符串详解 4. for循环
- xenserver 模板导出导入
由于业务需求,新增一台xenserver,需要将原先创建好的模板环境导入到新的母机上面,此处记录一下 1.导出模板 # 获取需要导出的模板uuid [root@localhost ~]# xe tem ...
- jq cookie
//$.cookie("xx");//读取xx的值 //$.cookie("xx","123");//设置xx的值为123 //$.cook ...
- Linux中的系统挂载文件/etc/fstab
[root@localhost ~]# cat /etc/fstab ## /etc/fstab# Created by anaconda on Wed Oct 5 15:21:46 2016## A ...
- 详解mysql数据库的左连接、右连接、内连接的区别
一般所说的左连接,外连接是指左外连接,右外连接.做个简单的测试你看吧. 先说左外连接和右外连接: SQL>select * from t1; ID NAME ---------- ------- ...
- 剑指offer 面试59题
面试59题: 题目:队列的最大值. 题目一:滑动窗口的最大值. 给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值.例如:如果输入数组为[2,3,4,2,6,2,5,1]及滑动窗口大小为3,那 ...
- "export" in SHELL
在shell中,若某一变量需要在随后的子程序中运行,则需要以 export 来使变量变成环境变量: export var export的用法总结: 1:一个shell中用export定义的变量,只对当 ...