https://docs.google.com/document/d/1Lr9UYXEz6s6R_3PWg3bZQLF3upGaNEkc0rQCFSzaYDI/edit

 

// create the original stream
DataStream<String> stream = ...; // apply the async I/O transformation
DataStream<Tuple2<String, String>> resultStream =
AsyncDataStream.unorderedWait(stream, new AsyncDatabaseRequest(), 1000, TimeUnit.MILLISECONDS, 100);

 

AsyncDataStream

有一组接口,

unorderedWait
orderedWait

 

最终都是调用到,

addOperator(in, func, timeUnit.toMillis(timeout), capacity, OutputMode.ORDERED)

是否是ordered,只是最后一个参数不同

    private static <IN, OUT> SingleOutputStreamOperator<OUT> addOperator(
DataStream<IN> in,
AsyncFunction<IN, OUT> func,
long timeout,
int bufSize,
OutputMode mode) { TypeInformation<OUT> outTypeInfo =
TypeExtractor.getUnaryOperatorReturnType(func, AsyncFunction.class, false,
true, in.getType(), Utils.getCallLocationName(), true); // create transform
AsyncWaitOperator<IN, OUT> operator = new AsyncWaitOperator<>(
in.getExecutionEnvironment().clean(func),
timeout,
bufSize,
mode); return in.transform("async wait operator", outTypeInfo, operator);
}

 

AsyncWaitOperator

setup主要是初始化,任务队列

    @Override
public void setup(StreamTask<?, ?> containingTask, StreamConfig config, Output<StreamRecord<OUT>> output) {
super.setup(containingTask, config, output); // create the operators executor for the complete operations of the queue entries
this.executor = Executors.newSingleThreadExecutor(); //单线程的Executor,用于处理队列 switch (outputMode) {
case ORDERED:
queue = new OrderedStreamElementQueue(
capacity,
executor,
this);
break;
case UNORDERED:
queue = new UnorderedStreamElementQueue(
capacity,
executor,
this);
break;
default:
throw new IllegalStateException("Unknown async mode: " + outputMode + '.');
}
}

 

看下,OrderedStreamElementQueue

public class OrderedStreamElementQueue implements StreamElementQueue {

    /** Queue for the inserted StreamElementQueueEntries. */
private final ArrayDeque<StreamElementQueueEntry<?>> queue; //放所有的element @Override
public AsyncResult peekBlockingly() throws InterruptedException { //取
lock.lockInterruptibly(); try {
while (queue.isEmpty() || !queue.peek().isDone()) { //如果queue的第一个element没有完成
headIsCompleted.await(); //等锁,等他完成
} return queue.peek(); //如果完成就peek出来,注意peek是不会移除这个element的,所以需要poll
} finally {
lock.unlock();
}
} @Override
public AsyncResult poll() throws InterruptedException { //单独做poll
lock.lockInterruptibly(); try {
while (queue.isEmpty() || !queue.peek().isDone()) { //如果第一个没完成,等待
headIsCompleted.await();
} notFull.signalAll(); //poll后,队列一定不满,所以解锁notFull return queue.poll();
} finally {
lock.unlock();
}
} private <T> void addEntry(StreamElementQueueEntry<T> streamElementQueueEntry) { //put,tryput都是调用这个 queue.addLast(streamElementQueueEntry); //加到queue里面 streamElementQueueEntry.onComplete(new AcceptFunction<StreamElementQueueEntry<T>>() { //给element加上complete的callback,调用onCompleteHandler
@Override
public void accept(StreamElementQueueEntry<T> value) {
try {
onCompleteHandler(value);
}
}
}, executor);
} private void onCompleteHandler(StreamElementQueueEntry<?> streamElementQueueEntry) throws InterruptedException {
lock.lockInterruptibly(); try {
if (!queue.isEmpty() && queue.peek().isDone()) {
headIsCompleted.signalAll(); //放开锁,告诉大家我完成了
}
} finally {
lock.unlock();
}
}
}

对于queue主要就是,读取操作

这里取是分两步,先peek,再poll

 

open,主要是处理从snapshot中恢复的数据

并启动emiter

    @Override
public void open() throws Exception {
super.open(); // process stream elements from state, since the Emit thread will start as soon as all
// elements from previous state are in the StreamElementQueue, we have to make sure that the
// order to open all operators in the operator chain proceeds from the tail operator to the
// head operator.
if (recoveredStreamElements != null) {
for (StreamElement element : recoveredStreamElements.get()) { //处理从snapshot中恢复出的element
if (element.isRecord()) {
processElement(element.<IN>asRecord());
}
else if (element.isWatermark()) {
processWatermark(element.asWatermark());
}
else if (element.isLatencyMarker()) {
processLatencyMarker(element.asLatencyMarker());
}
else {
throw new IllegalStateException("Unknown record type " + element.getClass() +
" encountered while opening the operator.");
}
}
recoveredStreamElements = null;
} // create the emitter
this.emitter = new Emitter<>(checkpointingLock, output, queue, this); //创建Emitter // start the emitter thread
this.emitterThread = new Thread(emitter, "AsyncIO-Emitter-Thread (" + getOperatorName() + ')');
emitterThread.setDaemon(true);
emitterThread.start(); }

 

Emitter

    @Override
public void run() {
try {
while (running) {
LOG.debug("Wait for next completed async stream element result.");
AsyncResult streamElementEntry = streamElementQueue.peekBlockingly(); output(streamElementEntry);
}

从queue中peek数据,对于上面OrderedStreamElementQueue,只有完成的数据会被peek到

    private void output(AsyncResult asyncResult) throws InterruptedException {
if (asyncResult.isWatermark()) {
//......
} else {
AsyncCollectionResult<OUT> streamRecordResult = asyncResult.asResultCollection(); synchronized (checkpointLock) { //collect数据需要加checkpoint锁
LOG.debug("Output async stream element collection result."); try {
Collection<OUT> resultCollection = streamRecordResult.get(); if (resultCollection != null) {
for (OUT result : resultCollection) {
timestampedCollector.collect(result); //真正emit数据
}
}
} // remove the peeked element from the async collector buffer so that it is no longer
// checkpointed
streamElementQueue.poll(); //emit完可以将数据从queue中删除 // notify the main thread that there is again space left in the async collector
// buffer
checkpointLock.notifyAll();
}
}
}

可以看到当数据被emit后,才会从queue删除掉

 

processElement

    @Override
public void processElement(StreamRecord<IN> element) throws Exception {
final StreamRecordQueueEntry<OUT> streamRecordBufferEntry = new StreamRecordQueueEntry<>(element); //封装成StreamRecordQueueEntry if (timeout > 0L) {
// register a timeout for this AsyncStreamRecordBufferEntry
long timeoutTimestamp = timeout + getProcessingTimeService().getCurrentProcessingTime(); final ScheduledFuture<?> timerFuture = getProcessingTimeService().registerTimer( //开个定时器,到时间就会colloct一个超时异常
timeoutTimestamp,
new ProcessingTimeCallback() {
@Override
public void onProcessingTime(long timestamp) throws Exception {
streamRecordBufferEntry.collect(
new TimeoutException("Async function call has timed out."));
}
}); // Cancel the timer once we've completed the stream record buffer entry. This will remove
// the register trigger task
streamRecordBufferEntry.onComplete(new AcceptFunction<StreamElementQueueEntry<Collection<OUT>>>() { //在StreamRecordQueueEntry完成是触发删除这个定时器,这样就只有未完成的会触发定时器
@Override
public void accept(StreamElementQueueEntry<Collection<OUT>> value) {
timerFuture.cancel(true);
}
}, executor);
} addAsyncBufferEntry(streamRecordBufferEntry); //把StreamRecordQueueEntry加到queue中去 userFunction.asyncInvoke(element.getValue(), streamRecordBufferEntry); //调用用户定义的asyncInvoke
}

 

StreamRecordQueueEntry

public class StreamRecordQueueEntry<OUT> extends StreamElementQueueEntry<Collection<OUT>>
implements AsyncCollectionResult<OUT>, AsyncCollector<OUT> { /** Future containing the collection result. */
private final CompletableFuture<Collection<OUT>> resultFuture; @Override
public void collect(Collection<OUT> result) {
resultFuture.complete(result);
} @Override
public void collect(Throwable error) {
resultFuture.completeExceptionally(error);
}
}

前面在emitter里面判断,entry是否做完就看,resultFuture是否isDone

可以看到resultFuture只有在collect的时候才会被complete

当resultFuture.complete时,onComplete callback会被触发,

这个callback在OrderedStreamElementQueue.addEntry被注册上来,做的事也就是告诉大家headIsCompleted;这样随后Emitter可以把结果数据emit出去

 

最终调用到用户定义的,

userFunction.asyncInvoke

@Override
public void asyncInvoke(final String str, final AsyncCollector<Tuple2<String, String>> asyncCollector) throws Exception { // issue the asynchronous request, receive a future for result
Future<String> resultFuture = client.query(str); // set the callback to be executed once the request by the client is complete
// the callback simply forwards the result to the collector
resultFuture.thenAccept( (String result) -> { asyncCollector.collect(Collections.singleton(new Tuple2<>(str, result))); });
}
}

 

首先client必须是异步的,如果不是,没法返回Future,那需要自己用连接池实现

主要逻辑就是在resultFuture完成后,调用asyncCollector.collect把结果返回给element

Flink - Asynchronous I/O的更多相关文章

  1. Flink 原理(六)——异步I/O(asynchronous I/O)

    1.前言 本文是基于Flink官网上Asynchronous  I/O的介绍结合自己的理解写成的,若有不正确的欢迎大伙留言交流,谢谢! 2.Asynchronous  I/O简介 将Flink用于流计 ...

  2. Flink - RocksDBStateBackend

    如果要考虑易用性和效率,使用rocksDB来替代普通内存的kv是有必要的 有了rocksdb,可以range查询,可以支持columnfamily,可以各种压缩 但是rocksdb本身是一个库,是跑在 ...

  3. Flink - Checkpoint

    Flink在流上最大的特点,就是引入全局snapshot,   CheckpointCoordinator 做snapshot的核心组件为, CheckpointCoordinator /** * T ...

  4. Flink - FLIP

    https://cwiki.apache.org/confluence/display/FLINK/Flink+Improvement+Proposals FLIP-1 : Fine Grained ...

  5. Flink Internals

    https://cwiki.apache.org/confluence/display/FLINK/Flink+Internals   Memory Management (Batch API) In ...

  6. Flink资料(2)-- 数据流容错机制

    数据流容错机制 该文档翻译自Data Streaming Fault Tolerance,文档描述flink在流式数据流图上的容错机制. ------------------------------- ...

  7. Apache Flink 分布式执行

    Flink 的分布式执行过程包含两个重要的角色,master 和 worker,参与 Flink 程序执行的有多个进程,包括 Job Manager,Task Manager 以及 Job Clien ...

  8. Blink: How Alibaba Uses Apache Flink

    This is a guest post from Xiaowei Jiang, Senior Director of Alibaba’s search infrastructure team. Th ...

  9. 阿里巴巴开源的Asynchronous I/O Design and Implementation

    Motivation I/O access, for the most case, is a time-consuming process, making the TPS for single ope ...

随机推荐

  1. 【Big Data - Hadoop - MapReduce】初学Hadoop之图解MapReduce与WordCount示例分析

    Hadoop的框架最核心的设计就是:HDFS和MapReduce.HDFS为海量的数据提供了存储,MapReduce则为海量的数据提供了计算. HDFS是Google File System(GFS) ...

  2. Goldengate:ERROR 180 encountered commit SCN that is not greater than the highest SCN already processed

    How to recover from Extract ERROR 180 encountered commit SCN that is not greater than the highest SC ...

  3. Vue中的computed属性

    阅读Vue官网的过程中,对于计算属于与监听器章节的内容有点理解的不清晰:https://cn.vuejs.org/v2/guide/computed.html. 后来上网查询了资料,结合官网的说明,总 ...

  4. iOS系统及客户端软件测试的基础介绍

    iOS系统及客户端软件测试的基础介绍 iOS现在的最新版本iOS5是10月12号推出,当前版本是4.3.5 先是硬件部分,采用iOS系统的是iPad,iPhone,iTouch这三种设备,其中iPho ...

  5. Python 的 Magic Methods 指南(转)

    介绍 本指南是数月博客的总结.主题是魔术方法. 什么是魔术方法呢?它们是面向对象Python语言中的一切.它们是你可以自定义并添加“魔法”到类中的特殊方法.它们被双下划线环绕(比如__init__或_ ...

  6. Kubernetes集群部署之一系统环境初始化

    安装版本: centos version: 7.4 docker version: 18.03.1-ce kubectl version: v1.10.1 etcdctl version: 3.2.1 ...

  7. 基于Java实现批量下载网络图片

    昨天朋友做项目遇到一个需求,需要把上千个的微博表情图片下载到本地磁盘,并做好规范命名,塞给我一堆Json数据,让我帮忙处理下,反正闲着也没事干,就帮忙写了.(很简单的一个功能,随手记录下,刚好填补下最 ...

  8. 巧妙解决windows下 copy命令不接受太长路径的问题

    今天遇到了写的bat文件中执行xcopy成功,但是部分文件丢失的问题,查看日志,发现很多提示 : “the system can not find the path specified.“ 但是去指定 ...

  9. Mybatis常见面试题(转)

    Mybatis技术内幕系列博客,从原理和源码角度,介绍了其内部实现细节,无论是写的好与不好,我确实是用心写了,由于并不是介绍如何使用Mybatis的文章,所以,一些参数使用细节略掉了,我们的目标是介绍 ...

  10. Navicat -- Oracle -- 错误锦集

    ORA:connection to server failed,probable Oracle Net admin error 解决的方案是: oci.dll的版本不对  从 http://www.o ...