flink 异步io mysql 缓存_Flink 异步IO实战

本文介绍了Flink异步IO操作的基本概念,通过对比同步IO和异步IO的样例,展示了异步IO如何提高处理效率。通过模拟查询延迟,展示了异步查询如何避免阻塞,使得数据处理顺序不再受限于查询速度,从而提升整体性能。
摘要由CSDN通过智能技术生成

基本概念

首先通过官网的一个图片了解一下Asynchronous I/O Operation

985e842855024a1de8e157777723de81.png

Flink source收到一条数据就会进行处理,如果需要通过这条数据关联外部数据源,例如mysql,在发出查询请求后,同步IO的方式是会等待查询结果再处理下一条数据的查询,也就是每一条数据都要等待上一个查询结束。而异步IO是指数据来了以后发出查询请求,先不等查询结果,直接继续发送下一条的查询请求,对于查询结果是异步返回的,返回结果之后再进入下一个算子的计算。这两种方式性能差距请看下的样例。

样例

代码传送门

生成6条数据,从0开始递增的6个数字。模拟异步查询之后,加上时间戳输出

public class AsyncIODemo {

public static void main(String[] args) throws Exception {

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

env.setParallelism(1);

final int maxCount = 6;

final int taskNum = 1;

final long timeout = 40000;

DataStream inputStream = env.addSource(new SimpleSource(maxCount));

AsyncFunction function = new SampleAsyncFunction();

DataStream result = AsyncDataStream.unorderedWait(

inputStream,

function,

timeout,

TimeUnit.MILLISECONDS,

10).setParallelism(taskNum);

result.map(new MapFunction() {

@Override

public String map(String value) throws Exception {

return value + "," + System.currentTimeMillis();

}

}).print();

env.execute("Async IO Demo");

}

private static class SimpleSource implements SourceFunction {

private volatile boolean isRunning = true;

private int counter = 0;

private int start = 0;

public SimpleSource(int maxNum) {

this.counter = maxNum;

}

@Override

public void run(SourceContext ctx) throws Exception {

while ((start < counter || counter == -1) && isRunning) {

synchronized (ctx.getCheckpointLock()) {

System.out.println("send data:" + start);

ctx.collect(start);

++start;

}

Thread.sleep(10L);

}

}

@Override

public void cancel() {

isRunning = false;

}

}

}

异步方法

public class SampleAsyncFunction extends RichAsyncFunction {

private long[] sleep = {100L, 1000L, 5000L, 2000L, 6000L, 100L};

@Override

public void open(Configuration parameters) throws Exception {

super.open(parameters);

}

@Override

public void close() throws Exception {

super.close();

}

@Override

public void asyncInvoke(final Integer input, final ResultFuture resultFuture) {

System.out.println(System.currentTimeMillis() + "-input:" + input + " will sleep " + sleep[input] + " ms");

query(input, resultFuture);

}

private void query(final Integer input, final ResultFuture resultFuture) {

try {

Thread.sleep(sleep[input]);

resultFuture.complete(Collections.singletonList(String.valueOf(input)));

} catch (InterruptedException e) {

resultFuture.complete(new ArrayList<>(0));

}

}

private void asyncQuery(final Integer input, final ResultFuture resultFuture) {

CompletableFuture.supplyAsync(new Supplier() {

@Override

public Integer get() {

try {

Thread.sleep(sleep[input]);

return input;

} catch (Exception e) {

return null;

}

}

}).thenAccept((Integer dbResult) -> {

resultFuture.complete(Collections.singleton(String.valueOf(dbResult)));

});

}

}

上面的代码中有两个方法query()和asyncQuery(),其中Thread.sleep(sleep[input]);用来模拟查询需要等待的时间,每条数据等待的时间分别为100L, 1000L, 5000L, 2000L, 6000L, 100L毫秒。

结果分析

运行query()的结果为

send data:0

send data:1

send data:2

send data:3

send data:4

send data:5

1577801193230-input:0 will sleep 100 ms

1577801193331-input:1 will sleep 1000 ms

0,1577801194336

1,1577801194336

1577801194336-input:2 will sleep 5000 ms

1577801199339-input:3 will sleep 2000 ms

2,1577801201341

1577801201342-input:4 will sleep 6000 ms

3,1577801207345

4,1577801207345

1577801207346-input:5 will sleep 100 ms

5,1577801207451

可以看到第一条数据进入到map算子的时间与最后一条相差了13115毫秒,执行的顺序与source中数据的顺序一致,并且是串行的。

运行asyncQuery()的结果为

send data:0

send data:1

send data:2

send data:3

1577802161755-input:0 will sleep 100 ms

1577802161756-input:1 will sleep 1000 ms

1577802161757-input:2 will sleep 5000 ms

send data:4

send data:5

1577802161783-input:3 will sleep 2000 ms

1577802161784-input:4 will sleep 6000 ms

1577802161785-input:5 will sleep 100 ms

0,1577802161859

1,1577802162759

3,1577802163862

5,1577802163962

2,1577802166760

4,1577802168762

同样第一条数据进入map算子的时间与最后一条仅相差了6903毫秒,而且输出结果的顺序并不是source中的顺序,而是按照查询时间递增的顺序输出,并且查询请求几乎是同一时间发出的。

通过上面的例子可以看出,flink所谓的异步IO,并不是只要实现了asyncInvoke方法就是异步了,这个方法并不是异步的,而是要依靠这个方法里面所写的查询是异步的才可以。否则像是上面query()方法那样,同样会阻塞查询相当于同步IO。在实现flink异步IO的时候一定要注意。官方文档也给出了相关的说明。

For example, the following patterns result in a blocking asyncInvoke(...) functions and thus void the asynchronous behavior:Using a database client whose lookup/query method call blocks until the result has been received back

总结

本文基于flink 1.9.0。通过样例介绍了如何实现flink异步IO,读者可以修改本文样例体验异步IO其他的特性,例如Order of Results或者Event Time。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值