Seastar
High performance C++ framework for concurrent servers
Classes | Public Member Functions | List of all members
seastar::sharded< Service > Class Template Reference

Detailed Description

template<typename Service>
class seastar::sharded< Service >

Template helper to distribute a service across all logical cores.

The sharded template manages a sharded service, by creating a copy of the service on each logical core, providing mechanisms to communicate with each shard's copy, and a way to stop the service.

Template Parameters
Servicea class to be instantiated on each core. Must expose a stop() method that returns a future<>, to be called when the service is stopped.

#include <seastar/core/sharded.hh>

Public Member Functions

 sharded ()
 
 sharded (const sharded &other)=delete
 
 sharded (sharded &&other) noexcept
 Moves a sharded object.
 
shardedoperator= (const sharded &other)=delete
 
shardedoperator= (sharded &&other)=default
 Moves a sharded object.
 
 ~sharded ()
 Destroyes a sharded object. Must not be in a started state.
 
template<typename... Args>
future start (Args &&... args)
 
template<typename... Args>
future start_single (Args &&... args)
 
future stop ()
 
future invoke_on_all (smp_service_group ssg, std::function< future<>(Service &)> func)
 
future invoke_on_all (std::function< future<>(Service &)> func)
 
template<typename... Args>
future invoke_on_all (smp_service_group ssg, future<>(Service::*func)(Args...), Args... args)
 
template<typename... Args>
future invoke_on_all (future<>(Service::*func)(Args...), Args... args)
 
template<typename... Args>
future invoke_on_all (smp_service_group ssg, void(Service::*func)(Args...), Args... args)
 
template<typename... Args>
future invoke_on_all (void(Service::*func)(Args...), Args... args)
 
template<typename Func >
future invoke_on_all (smp_service_group ssg, Func &&func)
 
template<typename Func >
future invoke_on_all (Func &&func)
 
template<typename Func >
future invoke_on_others (smp_service_group ssg, Func &&func)
 
template<typename Func >
future invoke_on_others (Func &&func)
 
template<typename Reducer , typename Ret , typename... FuncArgs, typename... Args>
auto map_reduce (Reducer &&r, Ret(Service::*func)(FuncArgs...), Args &&... args) -> typename reducer_traits< Reducer >::future_type
 
template<typename Reducer , typename Func >
auto map_reduce (Reducer &&r, Func &&func) -> typename reducer_traits< Reducer >::future_type
 
template<typename Mapper , typename Initial , typename Reduce >
future< Initial > map_reduce0 (Mapper map, Initial initial, Reduce reduce)
 
template<typename Mapper , typename return_type = std::result_of_t<Mapper(Service&)>>
future< std::vector< return_type > > map (Mapper mapper)
 
template<typename Ret , typename... FuncArgs, typename... Args, typename FutureRet = futurize_t<Ret>>
FutureRet invoke_on (unsigned id, smp_service_group ssg, Ret(Service::*func)(FuncArgs...), Args &&... args)
 
template<typename Ret , typename... FuncArgs, typename... Args, typename FutureRet = futurize_t<Ret>>
FutureRet invoke_on (unsigned id, Ret(Service::*func)(FuncArgs...), Args &&... args)
 
template<typename Func , typename Ret = futurize_t<std::result_of_t<Func(Service&)>>>
Ret invoke_on (unsigned id, smp_service_group ssg, Func &&func)
 
template<typename Func , typename Ret = futurize_t<std::result_of_t<Func(Service&)>>>
Ret invoke_on (unsigned id, Func &&func)
 
const Service & local () const
 Gets a reference to the local instance.
 
Service & local ()
 Gets a reference to the local instance.
 
shared_ptr< Service > local_shared ()
 Gets a shared pointer to the local instance.
 
bool local_is_initialized () const
 Checks whether the local instance has been initialized.
 

Constructor & Destructor Documentation

◆ sharded()

template<typename Service>
seastar::sharded< Service >::sharded ( )
inline

Constructs an empty sharded object. No instances of the service are created.

Member Function Documentation

◆ invoke_on() [1/4]

template<typename Service>
template<typename Ret , typename... FuncArgs, typename... Args, typename FutureRet = futurize_t<Ret>>
FutureRet seastar::sharded< Service >::invoke_on ( unsigned  id,
smp_service_group  ssg,
Ret(Service::*)(FuncArgs...)  func,
Args &&...  args 
)
inline

Invoke a method on a specific instance of Service.

Parameters
idshard id to call
ssgAn smp_service_group that controls concurrency on the server side of the call
funca method of Service
argsarguments to be passed to func
Returns
result of calling func(args) on the designated instance

◆ invoke_on() [2/4]

template<typename Service>
template<typename Ret , typename... FuncArgs, typename... Args, typename FutureRet = futurize_t<Ret>>
FutureRet seastar::sharded< Service >::invoke_on ( unsigned  id,
Ret(Service::*)(FuncArgs...)  func,
Args &&...  args 
)
inline

Invoke a method on a specific instance of Service.

Parameters
idshard id to call
funca method of Service
argsarguments to be passed to func
Returns
result of calling func(args) on the designated instance

◆ invoke_on() [3/4]

template<typename Service>
template<typename Func , typename Ret = futurize_t<std::result_of_t<Func(Service&)>>>
Ret seastar::sharded< Service >::invoke_on ( unsigned  id,
smp_service_group  ssg,
Func &&  func 
)
inline

Invoke a callable on a specific instance of Service.

Parameters
idshard id to call
ssgAn smp_service_group that controls concurrency on the server side of the call
funca callable with signature Value (Service&) or future<Value> (Service&) (for some Value type)
Returns
result of calling func(instance) on the designated instance

◆ invoke_on() [4/4]

template<typename Service>
template<typename Func , typename Ret = futurize_t<std::result_of_t<Func(Service&)>>>
Ret seastar::sharded< Service >::invoke_on ( unsigned  id,
Func &&  func 
)
inline

Invoke a callable on a specific instance of Service.

Parameters
idshard id to call
funca callable with signature Value (Service&) or future<Value> (Service&) (for some Value type)
Returns
result of calling func(instance) on the designated instance

◆ invoke_on_all() [1/5]

template<typename Service>
template<typename... Args>
future seastar::sharded< Service >::invoke_on_all ( smp_service_group  ssg,
future<>(Service::*)(Args...)  func,
Args...  args 
)
inline

Invoke a method on all instances of . The return value becomes ready when all instances have processed the message.

Parameters
ssgAn smp_service_group that controls concurrency on the server side of the call
funcMember function of Service to be invoked on all shards
Returns
Future that becomes ready once all calls have completed

◆ invoke_on_all() [2/5]

template<typename Service>
template<typename... Args>
future seastar::sharded< Service >::invoke_on_all ( smp_service_group  ssg,
void(Service::*)(Args...)  func,
Args...  args 
)
inline

Invoke a method on all Service instances in parallel.

Parameters
ssgAn smp_service_group that controls concurrency on the server side of the call
funcmember function to be called. Must return void or future<>.
argsarguments to be passed to func.
Returns
future that becomes ready when the method has been invoked on all instances.

◆ invoke_on_all() [3/5]

template<typename Service>
template<typename... Args>
future seastar::sharded< Service >::invoke_on_all ( void(Service::*)(Args...)  func,
Args...  args 
)
inline

Invoke a method on all Service instances in parallel.

Parameters
funcmember function to be called. Must return void or future<>.
argsarguments to be passed to func.
Returns
future that becomes ready when the method has been invoked on all instances.

◆ invoke_on_all() [4/5]

template<typename Service>
template<typename Func>
future seastar::sharded< Service >::invoke_on_all ( smp_service_group  ssg,
Func &&  func 
)
inline

Invoke a callable on all instances of Service.

Parameters
ssgAn smp_service_group that controls concurrency on the server side of the call
funca callable with the signature void (Service&) or future<> (Service&), to be called on each core with the local instance as an argument.
Returns
a future<> that becomes ready when all cores have processed the message.

◆ invoke_on_all() [5/5]

template<typename Service>
template<typename Func >
future seastar::sharded< Service >::invoke_on_all ( Func &&  func)
inline

Invoke a callable on all instances of Service.

Parameters
funca callable with the signature void (Service&) or future<> (Service&), to be called on each core with the local instance as an argument.
Returns
a future<> that becomes ready when all cores have processed the message.

◆ invoke_on_others() [1/2]

template<typename Service >
template<typename Func >
future seastar::sharded< Service >::invoke_on_others ( smp_service_group  ssg,
Func &&  func 
)
inline

Invoke a callable on all instances of Service except the instance which is allocated on current shard.

Parameters
ssgAn smp_service_group that controls concurrency on the server side of the call
funca callable with the signature void (Service&) or future<> (Service&), to be called on each core with the local instance as an argument.
Returns
a future<> that becomes ready when all cores but the current one have processed the message.

◆ invoke_on_others() [2/2]

template<typename Service>
template<typename Func >
future seastar::sharded< Service >::invoke_on_others ( Func &&  func)
inline

Invoke a callable on all instances of Service except the instance which is allocated on current shard.

Parameters
funca callable with the signature void (Service&) or future<> (Service&), to be called on each core with the local instance as an argument.
Returns
a future<> that becomes ready when all cores but the current one have processed the message.

◆ map()

template<typename Service>
template<typename Mapper , typename return_type = std::result_of_t<Mapper(Service&)>>
future<std::vector<return_type> > seastar::sharded< Service >::map ( Mapper  mapper)
inline

Applies a map function to all shards, and return a vector of the result.

Parameters
mappercallable with the signature Value (Service&) or future<Value> (Service&) (for some Value type).

Each map invocation runs on the shard associated with the service.

Template Parameters
Mapperunary function taking Service& and producing some result.
Returns
Result vector of applying map to each instance in parallel

◆ map_reduce() [1/2]

template<typename Service>
template<typename Reducer , typename Ret , typename... FuncArgs, typename... Args>
auto seastar::sharded< Service >::map_reduce ( Reducer &&  r,
Ret(Service::*)(FuncArgs...)  func,
Args &&...  args 
) -> typename reducer_traits<Reducer>::future_type
inline

Invoke a method on all instances of Service and reduce the results using Reducer.

See also
map_reduce(Iterator begin, Iterator end, Mapper&& mapper, Reducer&& r)

◆ map_reduce() [2/2]

template<typename Service>
template<typename Reducer , typename Func >
auto seastar::sharded< Service >::map_reduce ( Reducer &&  r,
Func &&  func 
) -> typename reducer_traits<Reducer>::future_type
inline

Invoke a callable on all instances of Service and reduce the results using Reducer.

See also
map_reduce(Iterator begin, Iterator end, Mapper&& mapper, Reducer&& r)

◆ map_reduce0()

template<typename Service>
template<typename Mapper , typename Initial , typename Reduce >
future<Initial> seastar::sharded< Service >::map_reduce0 ( Mapper  map,
Initial  initial,
Reduce  reduce 
)
inline

Applies a map function to all shards, then reduces the output by calling a reducer function.

Parameters
mapcallable with the signature Value (Service&) or future<Value> (Service&) (for some Value type). used as the second input to reduce
initialinitial value used as the first input to reduce.
reducebinary function used to left-fold the return values of map into initial .

Each map invocation runs on the shard associated with the service.

Template Parameters
Mapperunary function taking Service& and producing some result.
Initialany value type
Reducea binary function taking two Initial values and returning an Initial
Returns
Result of applying map to each instance in parallel, reduced by calling reduce() on each adjacent pair of results.

◆ start()

template<typename Service >
template<typename... Args>
future seastar::sharded< Service >::start ( Args &&...  args)

Starts Service by constructing an instance on every logical core with a copy of args passed to the constructor.

Parameters
argsArguments to be forwarded to Service constructor
Returns
a future<> that becomes ready when all instances have been constructed.

◆ start_single()

template<typename Service >
template<typename... Args>
future seastar::sharded< Service >::start_single ( Args &&...  args)

Starts Service by constructing an instance on a single logical core with a copy of args passed to the constructor.

Parameters
argsArguments to be forwarded to Service constructor
Returns
a future<> that becomes ready when the instance has been constructed.

◆ stop()

template<typename Service >
future seastar::sharded< Service >::stop ( )

Stops all started instances and destroys them.

For every started instance, its stop() method is called, and then it is destroyed.


The documentation for this class was generated from the following file: