Commit 5ac8f9ab authored by Dominik Charousset's avatar Dominik Charousset

Add manual section covering actor pools

parent f42f143e
\section{Managing Groups of Workers}
\label{Sec::WorkerGroups}
When managing a set of workers, requests are often dispatched from a central actor to a set of workers.
The class \lstinline^actor_pool^ implements a lightweight abstraction for managing a set of workers using a dispatching policy.
Pools are created using the static member function \lstinline^make^. It takes either one argument (the policy) or three (nr. of workers, factory function for workers, and policy).
After construction, new workers can be added via $('SYS', 'PUT', worker)$ messages.
For example, adding \lstinline^worker^ to \lstinline^my_pool^ could be done using \lstinline^send(my_pool, sys_atom::value, put_atom::value, worker)^.
$('SYS', 'DELETE', worker)$ messages remove a worker from the set and $('SYS', 'GET')$ returns a \lstinline^vector<actor>^ containing all workers.
An actor pool assumes ownership of its workers.
When forced to quit, it sends an exit messages to all of its workers, forcing them to quit as well.
The pool also monitors all of its workers.
It is wort mentioning that the pool does not cache any messages per default.
Messages queued up in a worker's mailbox are lost per default, i.e., not moved to another worker.
Advanced caching or resend strategies can be implemented in a policy.
\subsection{Predefined Dispatching Policies}
The actor pool class contains a set of predefined policies for convenience.
\subsubsection{\lstinline^actor_pool::round_robin^}
This policy forwards incoming requests in a round-robin manner to workers.
This policy does not guarantee that messages are consumed, i.e., work items are lost if the worker exits before processing all of its messages.
\subsubsection{\lstinline^actor_pool::broadcast^}
This policy forwards \emph{each} message to \emph{all} workers.
Synchronous messages to the pool will be received by all workers, but the client will only recognize the first arriving response message---or error---and discard other results.
Note that this is not caused by the policy itself, but a consequence of forwarding synchronous messages to more than one actor.
\subsubsection{\lstinline^actor_pool::random^}
This policy forwards incoming requests in a random order.
Analogous to \lstinline^round_robin^, this policy does not cache or redispatch messages.
\subsection{Example}
\begin{lstlisting}
actor new_worker() {
return spawn([]() -> behavior {
return {
[](int x, int y) {
return x + y;
}
};
});
}
void broadcast_example() {
scoped_actor self;
// spawns a pool with 5 workers
auto pool5 = [] {
return actor_pool::make(5, new_worker, actor_pool::broadcast{});
};
// spawns a pool with 5 pools with 5 workers each
auto w = actor_pool::make(5, pool5, actor_pool::broadcast{});
// will be broadcasted to 25 workers
self->send(w, 1, 2);
std::vector<int> results;
int i = 0;
self->receive_for(i, 25)(
[&](int res) {
results.push_back(res);
}
);
assert(results.size(), 25);
assert(std::all_of(results.begin(), results.end(),
[](int res) { return res == 3; }));
// terminate pool(s) and all workers
self->send_exit(w, exit_reason::user_shutdown);
}
\end{lstlisting}
No preview for this file type
...@@ -125,6 +125,7 @@ ...@@ -125,6 +125,7 @@
\include{NetworkTransparency} \include{NetworkTransparency}
\include{NetworkIO} \include{NetworkIO}
\include{GroupCommunication} \include{GroupCommunication}
\include{ManagingGroupsOfWorkers}
\include{ActorCompanions} \include{ActorCompanions}
\include{TypeSystem} \include{TypeSystem}
\include{BlockingAPI} \include{BlockingAPI}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment