As an async programming pattern, Future has been popular with many of our programmers across a wide range of languages. Loosely speaking, Future is a wrapper around a value which will be available at some point in the future. Strictly speaking, Future is a monad which supports the following 3 operations:

unit :: T -> Future<T>
map :: (T -> R) -> (Future<T> -> Future<R>)
flatMap :: (T -> Future<R>) -> (Future<T> -> Future<R>)

When holding a future, we know the type of the value, we can register callbacks which will be called when the future is done. But callbacks are not the recommended way to deal with futures, the point of Future pattern is to avoid callbacks and in favor of future transformation. By properly using future transformation, we can make our async code look like sequential code, the callbacks are hidden from us by the futures.

Here is an example, say there are 2 async RPCs. One takes a user ID and returns a future of a list of the user's new message header (ID and title), the other takes a message ID and returns its body.

// RPC 1: Gets a list of new message (headers) of a user.
Future<NewMessagesResponse> getNewMessages(UserId userId); // RPC 2: Gets the full message for a message ID.
Future<Message> getMessage(MessageId messageId); // Data structures.
class Message {
class Header {
MessageId id;
String title;
}
class Body {
...
} Header header;
Body body;
} class NewMessagesResponse {
List<MessageHeaders> headers;
}

Your task is that, given a user ID and a keyword, get the user's new messages whose title contains the keyword. With future transformation, the code may look like:

// Gets the future of a list of messages for a user, whose titles contains a given keyword.
Future<List<Message>> getNewMessages(UserId userId, String keyword) {
Future<NewMessagesResponse> newMessagesFuture = getNewMessages(userId);
Future<List<MessageId>> interestingIdsFuture = filter(newMessagesFuture, keyword);
Future<List<Message>> messagesFuture = getMessages(interestingIdsFuture);
Return messages;
}

The structure of the code is similar to what we do with synchronous code:

List<Message> getNewMessages(UserId userId, String keyword) {
NewMessagesResponse newMessages = getNewMessages(userId);
List<MessageId> interestingIds = filter(newMessages, keyword);
List<Message> messages = getMessages(interestingIds);
Return messages;
}

The async and sync functions are isomorphic, there is a correspondence in their code structure. But their runtime behaviors are different, one happens asynchronously, one happens synchronously.

Now here comes the real challenge. What if we change the RPC a bit, say there may be too many new messages that it has to return messages page by page, each response may contain an optional next page token indicating there are more pages.

// RPC 1: Gets one page of the new message (headers) of a user. The page number is denoted by a pageToken.
Future<NewMessagesResponse> getNewMessageHeaders(UserId userId, String pageToken) class NewMessagesResponse {
List<MessageHeaders> messageHeaders;
String nextPageToken; // Non-empty nextPageToken indicates there are more pages.
}

Your task remains the same, write a function which takes a user ID and a keyword, return a list of the user's new messages whose titles contain the keyword.

Future<List<Message>> getNewMessages(UserId userId, String keyword) {
//TODO
}

The difficulty lies with that in regular future transformations we have fixed number of steps, we can simply chain them together sequentially, then we get one future of the final result; but now because of pagination, the number of steps is not nondeterministic, how can we chain them together?

For synchronous code, we may use a loop like:

List<Message> getNewMessages(UserId userId, String keyword) {
List<MessageId> interestingMessages = new ArrayList<>();
String pageToken = "";
do {
NewMessagesResponse newMessages = getNewMessages(userId, pageToken);
List<MessageId> interestingIds = filter(newMessages, keyword);
allNewMessages.addAll(newMessages.headers);
pageToken = newMessages.nextPageToken;
} while (!isEmpty(pageToken));
}

But unfortunately loop is applicable to futures. How can we get one future for all the pages? Recursion comes to rescue. This is what I call *Stateful Future Transformation*.

class State {
UserId userId;
String keyword;
int pageIndex;
String pageToken;
List<MessageId> buffer;
} Future<State> getInterestingMessages(Future<State> stateFuture) {
return Future.transform(
stateFuture, (State state) -> {
if (state.pageIndex == 0 || !isEmpty(state.pageToken)) {
// Final state.
return Future.immediate(state);
} else {
// Intermediate state.
Future<NewMessagesResponse> newMessagesFuture =
getNewMessages(state.userId, state.pageToken);
return Future.transform(newMessagesFuture, newMessages -> {
state.pageIndex++;
state.pageToken = newMessages.nextPageToken;
state.buffer.addAll(filter(newMessages, state.keyword);
});
}
});
} Future<State> getInterestingMessages(UserId userId, String keyword) {
State initialState = new State(userId, keyword, 0, "", new ArrayList());
Future<State> initialStateFuture = Future.immediate(initialState);
return getInterestingMessages(initialStateFuture);
}

The code above can be refactored into a general stateful future transformation function:

// Transforms the future of an initial state future into the future of its final state.
Future<StateT> transform(
Future<StateT> stateFuture,
Function<StateT, Boolean> isFinalState,
Function<StateT, Future<StateT>> getNextState) {
return Future.transform(
stateFuture,
(StateT state) -> {
return isFinalState.apply(state)
? Future.immediate(state)
: transform(getNextState.appy(state));
}
});
}

Stateful Future Transformation的更多相关文章

  1. Isomorphic JavaScript: The Future of Web Apps

    Isomorphic JavaScript: The Future of Web Apps At Airbnb, we’ve learned a lot over the past few years ...

  2. Spark Streaming揭秘 Day24 Transformation和action图解

    Spark Streaming揭秘 Day24 Transformation和action图解 今天我们进入SparkStreaming的数据处理,谈一下两个重要的操作Transfromation和a ...

  3. Future Works on P4

    Future Works on P4 P4 and NV: MPvisor, Hyper4, HyperV, Flex4 P4 and NFV P4 and Network Cache P4 and ...

  4. explain the past and guide the future 好的代码的标准:解释过去,指引未来;

    好的代码的标准:解释过去,指引未来: Design philosophies | Django documentation | Django https://docs.djangoproject.co ...

  5. .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation

    本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...

  6. 使用 Vert.X Future/Promise 编写异步代码

    Future 和 Promise 是 Vert.X 4.0中的重要角色,贯穿了整个 Vert.X 框架.掌握 Future/Promise 的用法,是用好 Vert.X.编写高质量异步代码的基础.本文 ...

  7. 面向未来的友好设计:Future Friendly

    一年前翻译了本文的一部分,最近终于翻译完成.虽然此设计思想的提出已经好几年了,但是还是觉得应该在国内推广一下,让大家知道“内容策略”,“移动优先”,“响应式设计”,“原子设计”等设计思想和技术的根源. ...

  8. 线程笔记:Future模式

    线程技术可以让我们的程序同时做多件事情,线程的工作模式有很多,常见的一种模式就是处理网站的并发,今天我来说说线程另一种很常见的模式,这个模式和前端里的ajax类似:浏览器一个主线程执行javascri ...

  9. 第二篇 Entity Framework Plus 之 Query Future

    从性能的角度出发,能够减少 增,删,改,查,跟数据库打交道次数,肯定是对性能会有所提升的(这里单纯是数据库部分). 今天主要怎样减少Entity Framework查询跟数据库打交道的次数,来提高查询 ...

随机推荐

  1. linux上安装MongoDB副本集(带keyfile安全认证以及用户权限)

    搭建前准备 MongoDB版本:4.0 主要参考搭建MongoDB副本集网站:https://www.jianshu.com/p/f021f1f3c60b 安装之前最好先确定一下几点: 防火墙关闭 M ...

  2. IP报文头详解

    IPv4报头: 报头长度:20-60字节bytes 白色部分为固定头部部分(20 bytes),绿色option选项部分为可选部分. 固定头部大小计算: 4bit + 4bit + 8bit + 16 ...

  3. Linux sleep 语句以及循环 测试负载

    sleep 命令 sleep 1    睡眠1秒sleep 1s    睡眠1秒sleep 1m   睡眠1分sleep 1h   睡眠1小时 总代码 #!/bin/bash for i in {1. ...

  4. android listview里包含组件(checkbox)点击事件和Item的点击事件冲突

    在listview的item中包含有textview和checkBox.我们既想获取listitem的点击事件,又想获取listitem中textview的点击事件和listitem中checkBox ...

  5. xtrabackup命令用法实战(转)

    xtrabackup命令用法实战 转载出自 https://blog.csdn.net/wfs1994/article/details/80399408 完全备份 1.创建备份 [root@linux ...

  6. WebApi 增加身份验证 (OAuth 2.0方式)

    1,在Webapi项目下添加如下引用: Microsoft.AspNet.WebApi.Owin Owin Microsoft.Owin.Host.SystemWeb Microsoft.Owin.S ...

  7. CSS绝对定位的原点:是在border上、padding上还是在content上?

    用了那么久的绝对定位,却一直没在意一个问题,就是绝对定位的原点,究竟是在盒模型的哪一处.今天想到这个问题,直接搜索没有找到标准文档,也没有搜索到相关的问题,于是决定自己动手实现一下看看,并把这个结果发 ...

  8. 详细分析LoadRunner参数化

    在进行网页的性能测试时,对网页的登录界面进行压力测试情况下就会使用到多用户进行登录,就需要对登录名和密码进行参数化,那么loadrunner怎么参数化设置呢?下面我们来详细分析一下. 一.我们这里通过 ...

  9. rocket-mq windows下载安装

    内容来源:https://www.jianshu.com/p/4a275e779afa 1环境    JDK1.8.Maven.Git 2安装部署 1.下载 1.1地址:http://rocketmq ...

  10. Linux---基础指令(一)

    https://www.linuxprobe.com/chapter-02.html  (Linux就要这么学) 一.执行查看帮助命令 date:date命令用于显示及设置系统的时间或日期,格式为“d ...