Really fix the clang warning in Format-inl.h
[folly.git] / folly / wangle / README.md
1 # Wangle
2 Wangle is a framework for expressing asynchronous code in C++ using the Future pattern.
3
4 **wan•gle** |ˈwaNGgəl| informal  
5 *verb*  
6 Obtain (something that is desired) by persuading others to comply or by manipulating events.
7
8 *noun*  
9 A framework for expressing asynchronous control flow in C++, that is composable and easily translated to/from synchronous code.
10
11 *synonyms*  
12 [Finagle](http://twitter.github.io/finagle/)
13
14 Wangle is a futures-based async framework inspired by [Twitter's Finagle](http://twitter.github.io/finagle/) (which is in scala), and (loosely) building upon the existing (but anemic) Futures code found in the C++11 standard ([`std::future`](http://en.cppreference.com/w/cpp/thread/future)) and [`boost::future`](http://www.boost.org/doc/libs/1_53_0/boost/thread/future.hpp) (especially >= 1.53.0). Although inspired by the std::future interface, it is not syntactically drop-in compatible because some ideas didn't translate well enough and we decided to break from the API. But semantically, it should be straightforward to translate from existing std::future code to Wangle.
15
16 The primary semantic differences are that Wangle Futures and Promises are not threadsafe; and as does `boost::future`, Wangle supports continuing callbacks (`then()`) and there are helper methods `whenAll()` and `whenAny()` which are important compositional building blocks.
17
18 ## Brief Synopsis
19
20 ```C++
21 #include <folly/wangle/Future.h>
22 using namespace folly::wangle;
23 using namespace std;
24
25 void foo(int x) {
26   // do something with x
27   cout << "foo(" << x << ")" << endl;
28 }
29
30 // ...
31
32   cout << "making Promise" << endl;
33   Promise<int> p;
34   Future<int> f = p.getFuture();
35   f.then(
36     [](Try<int>&& t) {
37       foo(t.value());
38     });
39   cout << "Future chain made" << endl;
40
41 // ... now perhaps in another event callback
42
43   cout << "fulfilling Promise" << endl;
44   p.setValue(42);
45   cout << "Promise fulfilled" << endl;
46 ```
47
48 This would print:
49   
50 ```
51 making Promise
52 Future chain made
53 fulfilling Promise
54 foo(42)
55 Promise fulfilled
56 ```
57
58 ## User Guide
59
60 Let's begin with an example. Consider a simplified Memcache client class with this interface:
61
62 ```C++
63 class MemcacheClient {
64  public:
65   struct GetReply {
66     enum class Result {
67       FOUND,
68       NOT_FOUND,
69       SERVER_ERROR,
70     };
71
72     Result result;
73     // The value when result is FOUND,
74     // The error message when result is SERVER_ERROR or CLIENT_ERROR
75     // undefined otherwise
76     std::string value;
77   };
78
79   GetReply get(std::string key);
80 };
81 ```
82
83 This API is synchronous, i.e. when you call `get()` you have to wait for the result. This is very simple, but unfortunately it is also very easy to write very slow code using synchronous APIs.
84
85 Now, consider this traditional asynchronous signature for `get()`:
86
87 ```C++
88 int get(std::string key, std::function<void(GetReply)> callback);
89 ```
90
91 When you call `get()`, your asynchronous operation begins and when it finishes your callback will be called with the result. (Unless something goes drastically wrong and you get an error code from `get()`.) Very performant code can be written with an API like this, but for nontrivial applications the code descends into a special kind of spaghetti code affectionately referred to as "callback hell".
92
93 The Future-based API looks like this:
94
95 ```C++
96 Future<GetReply> get(std::string key);
97 ```
98
99 A `Future<GetReply>` is a placeholder for the `GetReply` that we will eventually get. A Future usually starts life out "unfulfilled", or incomplete, i.e.:
100
101 ```C++
102 fut.isReady() == false
103 fut.value()  // will throw an exception because the Future is not ready
104 ```
105
106 At some point in the future, the Future will have been fulfilled, and we can access its value.
107
108 ```C++
109 fut.isReady() == true
110 GetReply& reply = fut.value();
111 ```
112
113 Futures support exceptions. If something exceptional happened, your Future may represent an exception instead of a value. In that case:
114
115 ```C++
116 fut.isReady() == true
117 fut.value() // will rethrow the exception
118 ```
119
120 Just what is exceptional depends on the API. In our example we have chosen not to raise exceptions for `SERVER_ERROR`, but represent this explicitly in the `GetReply` object. On the other hand, an astute Memcache veteran would notice that we left `CLIENT_ERROR` out of `GetReply::Result`, and perhaps a `CLIENT_ERROR` would have been raised as an exception, because `CLIENT_ERROR` means there's a bug in the library and this would be truly exceptional. These decisions are judgement calls by the API designer. The important thing is that exceptional conditions (including and especially spurious exceptions that nobody expects) get captured and can be handled higher up the "stack".
121
122 So far we have described a way to initiate an asynchronous operation via an API that returns a Future, and then sometime later after it is fulfilled, we get its value. This is slightly more useful than a synchronous API, but it's not yet ideal. There are two more very important pieces to the puzzle.
123
124 First, we can aggregate Futures, to define a new Future that completes after some or all of the aggregated Futures complete.  Consider two examples: fetching a batch of requests and waiting for all of them, and fetching a group of requests and waiting for only one of them.
125
126 ```C++
127 vector<Future<GetReply>> futs;
128 for (auto& key : keys) {
129   futs.push_back(mc.get(key));
130 }
131 auto all = whenAll(futs.begin(), futs.end());
132
133 vector<Future<GetReply>> futs;
134 for (auto& key : keys) {
135   futs.push_back(mc.get(key));
136 }
137 auto any = whenAny(futs.begin(), futs.end());
138 ```
139
140 `all` and `any` are Futures (for the exact type and usage see the header files).  They will be complete when all/one of `futs` are complete, respectively. (There is also `whenN()` for when you need *some*.)
141
142 Second, we can attach callbacks to a Future, and chain them together monadically. An example will clarify:
143
144 ```C++
145 Future<GetReply> fut1 = mc.get("foo");
146
147 Future<string> fut2 = fut1.then(
148   [](Try<GetReply>&& t) {
149     if (t.value().result == MemcacheClient::GetReply::Result::FOUND)
150       return t.value().value;
151     throw SomeException("No value");
152   });
153
154 Future<void> fut3 = fut2.then(
155   [](Try<string>&& t) {
156     try {
157       cout << t.value() << endl;
158     } catch (std::exception const& e) {
159       cerr << e.what() << endl;
160     }
161   });
162 ```
163
164 That example is a little contrived but the idea is that you can transform a result from one type to another, potentially in a chain, and unhandled errors propagate. Of course, the intermediate variables are optional. `Try<T>` is the object wrapper that supports both value and exception.
165
166 Using `then` to add callbacks is idiomatic. It brings all the code into one place, which avoids callback hell.
167
168 Up to this point we have skirted around the matter of waiting for Futures. You may never need to wait for a Future, because your code is event-driven and all follow-up action happens in a then-block. But if want to have a batch workflow, where you initiate a batch of asynchronous operations and then wait for them all to finish at a synchronization point, then you will want to wait for a Future.
169
170 Other future frameworks like Finagle and std::future/boost::future, give you the ability to wait directly on a Future, by calling `fut.wait()` (naturally enough). Wangle has diverged from this pattern because we don't want to be in the business of dictating how your thread waits. We may work out something that we feel is sufficiently general, in the meantime adapt this spin loop to however your thread should wait:
171
172   while (!f.isReady()) {}
173
174 (Hint: you might want to use an event loop or a semaphore or something. You probably don't want to just spin like this.)
175
176 Wangle is partially threadsafe. A Promise or Future can migrate between threads as long as there's a full memory barrier of some sort. `Future::then` and `Promise::setValue` (and all variants that boil down to those two calls) can be called from different threads. BUT, be warned that you might be surprised about which thread your callback executes on. Let's consider an example.
177
178 ```C++
179 // Thread A
180 Promise<void> p;
181 auto f = p.getFuture();
182
183 // Thread B
184 f.then(x).then(y).then(z);
185
186 // Thread A
187 p.setValue();
188 ```
189
190 This is legal and technically threadsafe. However, it is important to realize that you do not know in which thread `x`, `y`, and/or `z` will execute. Maybe they will execute in Thread A when `p.setValue()` is called. Or, maybe they will execute in Thread B when `f.then` is called. Or, maybe `x` will execute in Thread B, but `y` and/or `z` will execute in Thread A. There's a race between `setValue` and `then`—whichever runs last will execute the callback. The only guarantee is that one of them will run the callback.
191
192 Naturally, you will want some control over which thread executes callbacks. We have a few mechanisms to help.
193
194 The first and most useful is `via`, which passes execution through an `Executor`, which usually has the effect of running the callback in a new thread.
195 ```C++
196 aFuture
197   .then(x)
198   .via(e1).then(y1).then(y2)
199   .via(e2).then(z);
200 ```
201 `x` will execute in the current thread. `y1` and `y2` will execute in the thread on the other side of `e1`, and `z` will execute in the thread on the other side of `e2`. `y1` and `y2` will execute on the same thread, whichever thread that is. If `e1` and `e2` execute in different threads than the current thread, then the final callback does not happen in the current thread. If you want to get back to the current thread, you need to get there via an executor.
202
203 This works because `via` returns a deactivated ("cold") Future, which blocks the propagation of callbacks until it is activated. Activation happens either explicitly (`activate`) or implicitly when the Future returned by `via` is destructed. In this example, there is no ambiguity about in which context any of the callbacks happen (including `y2`), because propagation is blocked at the `via` callsites until after everything is wired up (temporaries are destructed after the calls to `then` have completed).
204
205 You can still have a race after `via` if you break it into multiple statements, e.g. in this counterexample:
206 ```C++
207 f = f.via(e1).then(y1).then(y2); // nothing racy here
208 f2.then(y3); // racy
209 ```
210 If you want more control over the delayed execution, check out `Later`.
211 ```C++
212 Later<void> later;
213 later = later.via(e1).then(y1).then(y2); // nothing racy here
214 later = later.then(y3); // nor here
215 later.launch(); // explicit launch
216 ```
217
218 ## You make me Promises, Promises
219
220 If you are wrapping an asynchronous operation, or providing an asynchronous API to users, then you will want to make Promises. Every Future has a corresponding Promise (except Futures that spring into existence already completed, with `makeFuture()`). Promises are simple, you make one, you extract the Future, and you fulfil it with a value or an exception. Example:
221
222 ```C++
223 Promise<int> p;
224 Future<int> f = p.getFuture();
225
226 f.isReady() == false
227
228 p.setValue(42);
229
230 f.isReady() == true
231 f.value() == 42
232 ```
233
234 and an exception example:
235
236 ```C++
237 Promise<int> p;
238 Future<int> f = p.getFuture();
239
240 f.isReady() == false
241
242 p.setException(std::runtime_error("Fail"));
243
244 f.isReady() == true
245 f.value() // throws the exception
246 ```
247
248 It's good practice to use fulfil which takes a function and automatically captures exceptions, e.g.
249
250 ```C++
251 Promise<int> p;
252 p.fulfil([]{
253   try {
254     // do stuff that may throw
255     return 42;
256   } catch (MySpecialException const& e) {
257     // handle it
258     return 7;
259   }
260   // Any exceptions that we didn't catch, will be caught for us
261 });
262 ```
263
264 ## FAQ
265
266 ### Why not use std::future?
267 No callback support.
268 See also http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3428.pdf
269
270 ### Why not use boost::future?
271 - 1.53 is brand new, and not in fbcode
272 - It's still a bit buggy/bleeding-edge
273 - They haven't fleshed out the threading model very well yet, e.g. every single `then` currently spawns a new thread unless you explicitly ask it to work on this thread only, and there is no support for executors yet.
274
275 ### Why use heap-allocated shared state? Why is Promise not a subclass of Future?
276 C++. It boils down to wanting to return a Future by value for performance (move semantics and compiler optimizations), and programmer sanity, and needing a reference to the shared state by both the user (which holds the Future) and the asynchronous operation (which holds the Promise), and allowing either to go out of scope.
277
278 ### What about proper continuations? Futures suck.
279 People mean two things here, they either mean using continuations (as in CSP) or they mean using generators which require continuations. It's important to know those are two distinct questions, but in our context the answer is the same because continuations are a prerequisite for generators.
280
281 C++ doesn't directly support continuations very well. But there are some ways to do them in C/C++ that rely on some rather low-level facilities like `setjmp` and `longjmp` (among others). So yes, they are possible (cf. [Mordor](https://github.com/ccutrer/mordor)).
282
283 The tradeoff is memory. Each continuation has a stack, and that stack is usually fixed-size and has to be big enough to support whatever ordinary computation you might want to do on it. So each living continuation requires a relatively large amount of memory. If you know the number of continuations will be small, this might be a good fit. In particular, it might be faster and the code might read cleaner.
284
285 Wangle takes the middle road between callback hell and continuations, one which has been trodden and proved useful in other languages. It doesn't claim to be the best model for all situations. Use your tools wisely.