Infinit's reactor C++ framework allows developers to program in a natural way without having to deal with complex thread-based flows that decrease maintainability and efficiency.
Powering Real-Time Decisions with Continuous Data Streams
Highly concurrent yet natural programming
1. Highly concurrent yet natural
programming
mefyl
quentin.hocquet@infinit.io
Version 1.2
2. Infinit & me
Me
• Quentin "mefyl" Hocquet
• Epita CSI (LRDE) 2008.
• Ex Gostai
• Into language theory
• Joined Infinit early two years ago.
3. Infinit & me
Me
• Quentin "mefyl" Hocquet
• Epita CSI (LRDE) 2008.
• Ex Gostai
• Into language theory
• Joined Infinit early two years ago.
Infinit
• Founded my Julien "mycure" Quintard, Epita SRS 2007
• Based on his thesis at Cambridge
• Decentralized filesystem in byzantine environment
• Frontend: file transfer application based on the technology.
• Strong technical culture
5. Know the difference
Parallel programming
Aims at running two tasks simultaneously. It is a matter of performances.
Concurrent programming
Aims at running two tasks without inter-blocking. It is a matter of behavior.
11. Sequential Concurrent
Know the difference
Parallel
Sequential Concurrent Parallel
CPU usage N N N
Execution time Long Short Shorter
12. Sequential Concurrent
Know the difference
Parallel
Sequential Concurrent Parallel
CPU usage N N N
Execution time Long Short Shorter
Need to run in parallel No No Yes
17. Some programming examples
Video encoding: encode a raw 2GB raw file to mp4.
• CPU bound.
• File chunks can be encoded separately and then merged later.
18. Parallel
Encode
first half
Encode
second half
Sequential
Concurrent
Encode first
half
Encode
second half
Some programming examples
Video encoding: encode a raw 2GB raw file to mp4.
• CPU bound.
• File chunks can be encoded separately and then merged later.
Parallelism is a plus, concurrency doesn't apply.
19. Some programming examples
An IRC server: handle up to 50k IRC users chatting.
• IO bound.
• A huge number of clients that must be handled concurrently and mostly
waiting.
20. Concurrent Parallel
Some programming examples
An IRC server: handle up to 50k IRC users chatting.
• IO bound.
• A huge number of clients that must be handled concurrently and mostly
waiting.
Concurrency is needed, parallelism is superfluous.
21. Know the difference
Parallelism
• Is never needed for correctness.
• Is about performances, not correct behavior.
• Is about exploiting multi-core and multi-CPU architectures.
Concurrent programming
• Can be needed for correctness.
• Is about correct behavior, sometimes about performances too.
• Is about multiple threads being responsive in concurrent.
22. Know the difference
Parallelism
• Is never needed for correctness.
• Is about performances, not correct behavior.
• Is about exploiting multi-core and multi-CPU architectures.
Concurrent programming
• Can be needed for correctness.
• Is about correct behavior, sometimes about performances too.
• Is about multiple threads being responsive in concurrent.
A good video encoding app:
• Encodes 4 times faster on a 4-core CPU. That's parallelism.
23. Know the difference
Parallelism
• Is never needed for correctness.
• Is about performances, not correct behavior.
• Is about exploiting multi-core and multi-CPU architectures.
Concurrent programming
• Can be needed for correctness.
• Is about correct behavior, sometimes about performances too.
• Is about multiple threads being responsive in concurrent.
A good video encoding app:
• Encodes 4 times faster on a 4-core CPU. That's parallelism.
• Has a responsive GUI while encoding. That's concurrency.
24. Who's best ?
If you are parallel, you are concurrent. So why bother ?
25. Who's best ?
If you are parallel, you are concurrent. So why bother ?
• Being parallel is much, much more difficult. That's time, money and
programmer misery.
26. Who's best ?
If you are parallel, you are concurrent. So why bother ?
• Being parallel is much, much more difficult. That's time, money and
programmer misery.
• You can't be efficiently parallel past your hardware limit. Those are system
calls, captain.
28. The sequential echo server
TCPServer server;
server.listen(4242);
while (true)
{
TCPSocket client = server.accept();
}
29. The sequential echo server
TCPServer server;
server.listen(4242);
while (true)
{
TCPSocket client = server.accept();
while (true)
{
std::string line = client.read_until("n");
client.send(line);
}
}
30. The sequential echo server
TCPServer server;
server.listen(4242);
while (true)
{
TCPSocket client = server.accept();
try
{
while (true)
{
std::string line = client.read_until("n");
client.send(line);
}
}
catch (ConnectionClosed const&)
{}
}
31. The sequential echo server
TCPServer server;
server.listen(4242);
while (true)
{
TCPSocket client = server.accept();
serve_client(client);
}
32. The sequential echo server
TCPServer server;
server.listen(4242);
while (true)
{
TCPSocket client = server.accept();
serve_client(client);
}
• Dead simple: you got it instantly. It's natural.
• But wrong: we handle only one client at a time.
• We need ...
33. The sequential echo server
TCPServer server;
server.listen(4242);
while (true)
{
TCPSocket client = server.accept();
serve_client(client);
}
• Dead simple: you got it instantly. It's natural.
• But wrong: we handle only one client at a time.
• We need ... concurrency !
34. The parallel echo server
TCPServer server;
server.listen(4242);
while (true)
{
TCPSocket client = server.accept();
serve_client(client);
}
35. The parallel echo server
TCPServer server;
server.listen(4242);
std::vector<std::thread> threads;
while (true)
{
TCPSocket client = server.accept();
std::thread client_thread(
[&]
{
serve_client(client);
});
client_thread.run();
vectors.push_back(std::move(client_thread));
}
36. The parallel echo server
TCPServer server;
server.listen(4242);
std::vector<std::thread> threads;
while (true)
{
TCPSocket client = server.accept();
std::thread client_thread(
[&]
{
serve_client(client);
});
client_thread.run();
vectors.push_back(std::move(client_thread));
}
• Almost as simple and still natural,
• To add the concurrency property, we just added a concurrency construct
to the existing.
38. But parallelism is too much
• Not scalable: you can't run 50k threads.
• Induces unwanted complexity: race conditions.
39. But parallelism is too much
• Not scalable: you can't run 50k threads.
• Induces unwanted complexity: race conditions.
int line_count = 0;
while (true)
{
TCPSocket client = server.accept();
while (true)
{
std::string line = client.read_until("n");
client.send(line);
++line_count;
}
}
40. But parallelism is too much
• Not scalable: you can't run 50k threads.
• Induces unwanted complexity: race conditions.
int line_count = 0;
while (true)
{
TCPSocket client = server.accept();
std::thread client_thread(
[&]
{
while (true)
{
std::string line = client.read_until("n");
client.send(line);
++line_count;
}
});
}
41. We need concurrency without threads
We need to accept, read and write to socket without threads so without
blocking.
42. We need concurrency without threads
We need to accept, read and write to socket without threads so without
blocking.
• Use select to monitor all sockets at once.
• Register actions to be done when something is ready.
• Wake up only when something needs to be performed.
43. We need concurrency without threads
We need to accept, read and write to socket without threads so without
blocking.
• Use select to monitor all sockets at once.
• Register actions to be done when something is ready.
• Wake up only when something needs to be performed.
This is abstracted with the reactor design pattern:
• libevent
• Boost ASIO
• Python Twisted
• ...
44. The callback-based echo server
Reactor reactor;
TCPServer server(reactor);
server.accept(&handle_connection);
reactor.run();
45. The callback-based echo server
Reactor reactor;
TCPServer server(reactor);
server.accept(&handle_connection);
reactor.run();
void
handle_connection(TCPSocket& client)
{
client.read_until("n", &handle_read);
}
46. The callback-based echo server
Reactor reactor;
TCPServer server(reactor);
server.accept(&handle_connection);
reactor.run();
void
handle_connection(TCPSocket& client);
void
handle_read(TCPSocket& c, std::string const& l, Error e)
{
if (!e)
c.send(l, &handle_sent);
}
48. How do we feel now ?
• This one scales to thousands of client.
49. How do we feel now ?
• This one scales to thousands of client.
• Yet to add the concurrency property, we had to completely change the way
we think.
50. How do we feel now ?
• This one scales to thousands of client.
• Yet to add the concurrency property, we had to completely change the way
we think.
• A bit more verbose and complex, but nothing too bad ... right ?
51. Counting lines with threads
try
{
while (true)
{
std::string line = client.read_until("n");
client.send(line);
}
}
catch (ConnectionClosed const&)
{
}
52. Counting lines with threads
int lines_count = 0;
try
{
while (true)
{
std::string line = client.read_until("n");
++lines_count;
client.send(line);
}
}
catch (ConnectionClosed const&)
{
std::cerr << "Client sent " << lines_count << "linesn";
}
53. Counting lines with callbacks
void
handle_connection(TCPSocket& client)
{
int* count = new int(0);
client.read_until(
"n", std::bind(&handle_read, count));
}
58. Callback-based programming considered harmful
• Code is structured with callbacks.
• Asynchronous operation break the flow arbitrarily.
• You lose all syntactic scoping expression (local variables, closure,
exceptions, ...).
59. Callback-based programming considered harmful
• Code is structured with callbacks.
• Asynchronous operation break the flow arbitrarily.
• You lose all syntactic scoping expression (local variables, closure,
exceptions, ...).
• This is not natural. Damn, this is pretty much as bad as GOTO.
60. Are we screwed ?
Threads
• Respect your beloved semantic and expressiveness.
• Don't scale and introduce race conditions.
61. Are we screwed ?
Threads
• Respect your beloved semantic and expressiveness.
• Don't scale and introduce race conditions.
Callbacks
• Scale.
• Ruins your semantic. Painful to write, close to impossible to maintain.
62. Are we screwed ?
Threads
• Respect your beloved semantic and expressiveness.
• Don't scale and introduce race conditions.
Callbacks
• Scale.
• Ruins your semantic. Painful to write, close to impossible to maintain.
I lied when I said: we need concurrency without threads.
63. Are we screwed ?
Threads
• Respect your beloved semantic and expressiveness.
• Don't scale and introduce race conditions.
Callbacks
• Scale.
• Ruins your semantic. Painful to write, close to impossible to maintain.
I lied when I said: we need concurrency without threads.
We need concurrency without system threads.
65. Coroutines
• Separate execution contexts like system threads.
• Userland: no need to ask the kernel.
• Non-parallel.
• Cooperative instead of preemptive: they yield to each other.
66. Coroutines
• Separate execution contexts like system threads.
• Userland: no need to ask the kernel.
• Non-parallel.
• Cooperative instead of preemptive: they yield to each other.
By building on top of that, we have:
• Scalability: no system thread involved.
• No arbitrary race-conditions: no parallelism.
• A stack, a context: the code is natural.
68. Coroutines-based scheduler
• Make a scheduler that holds coroutines .
• Embed a reactor in there.
• Write a neat Socket class. When read, it:
◦ Unschedules itself.
◦ Asks the reactor to read
◦ Pass a callback to reschedule itself
◦ Yield control back.
85. Mutexes
But you said no race conditions! You lied again!
reactor::Thread t([&] {
while (true)
{
for (auto& socket: sockets)
socket.send("YO");
}
});
{
socket.push_back(...);
}
86. Mutexes
But you said no race conditions! You lied again!
reactor::Mutex mutex;
reactor::Thread t([&] {
while (true)
{
reactor::wait(mutex);
for (auto& socket: sockets)
socket.send("YO");
mutex.unlock();
}
});
{
reactor::wait(mutex);
socket.push_back(...);
mutex.unlock();
}
87. Mutexes
But you said no race conditions! You lied again!
reactor::Mutex mutex;
reactor::Thread t([&] {
while (true)
{
reactor::Lock lock(mutex);
for (auto& socket: sockets)
socket.send("YO");
}
});
{
reactor::Lock lock(mutex);
socket.push_back(...);
}
88. Networking: TCP
We saw a good deal of TCP networking:
try
{
reactor::TCPSocket socket("battle.net", 4242, 10_sec);
// ...
}
catch (reactor::network::ResolutionFailure const&)
{
// ...
}
catch (reactor::network::Timeout const&)
{
// ...
}
89. Networking: TCP
We saw a good deal of TCP networking:
void
serve(TCPSocket& client)
{
try
{
std::string auth = server.read_until("n", 10_sec);
if (!check_auth(auth))
// Impossible with callbacks
throw InvalidCredentials();
while (true) { ... }
}
catch (reactor::network::Timeout const&)
{}
}
99. HTTP streaming
std::string content = reactor::http::get(
"my-api.infinit.io/transactions");
auto json = json::parse(content);
reactor::http::Request r(
"my-api.production.infinit.io/transactions");
assert(r.status() == reactor::http::Status::OK);
// JSON is parsed on the fly;
auto json = json::parse(r);
100. HTTP streaming
std::string content = reactor::http::get(
"my-api.infinit.io/transactions");
auto json = json::parse(content);
reactor::http::Request r(
"my-api.production.infinit.io/transactions");
assert(r.status() == reactor::http::Status::OK);
// JSON is parsed on the fly;
auto json = json::parse(r);
reactor::http::Request r(
"youtube.com/upload", http::reactor::Method::PUT);
std::ifstream input("~/A new hope - BrRIP.mp4");
std::copy(input, r);
101. Better concurrency: futures, ...
std::string transaction_id = reactor::http::put(
"my-api.production.infinit.io/transactions");
// Ask the user files to share.
reactor::http::post("my-api.infinit.io/transaction/", file_list);
std::string s3_token = reactor::http::get(
"s3.aws.amazon.com/get_token?key=...");
// Upload files to S3
102. Better concurrency: futures, ...
std::string transaction_id = reactor::http::put(
"my-api.production.infinit.io/transactions");
// Ask the user files to share.
reactor::http::post("my-api.infinit.io/transaction/", file_list);
std::string s3_token = reactor::http::get(
"s3.aws.amazon.com/get_token?key=...");
// Upload files to S3
reactor::http::Request transaction(
"my-api.production.infinit.io/transactions");
reactor::http::Request s3(
"s3.aws.amazon.com/get_token?key=...");
// Ask the user files to share.
auto transaction_id = transaction.content();
reactor::http::Request list(
"my-api.infinit.io/transaction/", file_list);
auto s3_token = transaction.content();
// Upload files to S3
103. Version 1
Wait meta
Ask files
Wait meta
Wait AWS
Version 2
Ask files
Better concurrency: futures, ...
Version 2
Ask files
104. How does it perform for us ?
• Notification server does perform:
◦ 10k clients per instance
◦ 0.01 load average
◦ 1G resident memory
◦ Cheap monocore 2.5 Ghz (EC2)
105. How does it perform for us ?
• Notification server does perform:
◦ 10k clients per instance
◦ 0.01 load average
◦ 1G resident memory
◦ Cheap monocore 2.5 Ghz (EC2)
• Life is so much better:
◦ Code is easy and pleasant to write and read
◦ Everything is maintainable
◦ Send metrics on login without slowdown? No biggie.
◦ Try connecting to several interfaces and keep the first to respond? No
biggie.