what is sobjectizer 5.5

28
What is SObjectizer-5.5? SObjectizer Team, Feb 2015

Upload: yauheni-akhotnikau

Post on 14-Aug-2015

109 views

Category:

Software


6 download

TRANSCRIPT

Page 1: What is SObjectizer 5.5

What is SObjectizer-5.5?

SObjectizer Team, Feb 2015

Page 2: What is SObjectizer 5.5

SObjectizer is a small tool for simplification of development of concurrent and event-driven applications in C++.

SObjectizer is responsible for:● in-process message dispatching;● providing working context for message processing;● SObjectizer Run-Time’s parameters tuning.

SObjectizer Team, Feb 2015

Page 3: What is SObjectizer 5.5

SObjectizer can be used for development of not only small utilities but also of a large, distributed and highly loaded software systems.

The whole application can be build upon SObjectizer.

Or SObjectizer can be used only as small part of an application developed on the top of Qt, wxWidgets, ACE, Boost, etc.

SObjectizer Team, Feb 2015

Page 4: What is SObjectizer 5.5

SObjectizer allows to build a concurrent application as a set of agent-objects which interact with each other only by means of asynchronous messages.

SObjectizer was designed under the influence of several approaches. And the Actor Model* is one of them. But the history of SObjectizer started long before this model became widely known.

* http://en.wikipedia.org/wiki/Actor_model SObjectizer Team, Feb 2015

Page 5: What is SObjectizer 5.5

SObjectizer’s main ideas and principles were formulated in the middle of 1990s, during the development of SCADA Objectizer project in Development Bureau of System Programming in Homyel, Belarus (1996-2000).

SCADA Objectizer’s ideas were reused in the new project SObjectizer-4 in 2002.

Evolution of SObjectizer-4 was stopped in 2010 and the development of SObjectizer-5 started.

SObjectizer Team, Feb 2015

Page 6: What is SObjectizer 5.5

SObjectizer was an in-house project of JSC Intervale* for the long time.

SObjectizer was used in the development of the following software systems:● SMS/USSD traffic service;● financial transaction handling;● software parameters monitoring.

SObjectizer Team, Feb 2015*www.intervale.ru

Page 7: What is SObjectizer 5.5

SObjectizer was published as an OpenSource project on SourceForge under 3-clauses BSD-licence in 2006.

Since 2013 SObjectizer’s development completely moved to SourceForge.

SObjectizer now is an independent project which a totally separated from JSC Intervale.

SObjectizer Team, Feb 2015

Page 8: What is SObjectizer 5.5

SObjectizer-5 is developed using C++11 with broad usage of C++11 standard library.

Supported platforms and compilers:● Visual C++ 12.0, GCC 4.9 on Windows● GCC 4.9, clang 3.4/3.5 on Linux and FreeBSD.

Support of other platforms is possible in the case when SObjectizer’s developers will have an access to those platforms.

SObjectizer Team, Feb 2015

Page 9: What is SObjectizer 5.5

12 releases of SObjectizer-5 were published since May 2013.

The last stable version ‒ 5.5.3, was published in Feb 2015.

Version 5.5.3 is ~12 KLOC of SObjectizer core.Plus ~12.5 KLOC of tests.Plus ~5 KLOC of samples.

Plus SObjectizer’s core documentation*.Plus articles and presentations** (some of them in Russian).

*http://sourceforge.net/p/sobjectizer/wiki/Basics/**http://sourceforge.net/p/sobjectizer/wiki/Articles/ SObjectizer Team, Feb 2015

Page 10: What is SObjectizer 5.5

Using SObjectizer-5.5 a programmer must define messages/signals and implement agents for processing them.

Agents are created by the programmer and are bound to dispatchers. Dispatchers are responsible for message dispatching and providing of working context on which agents handle messages.

SObjectizer Team, Feb 2015

Page 11: What is SObjectizer 5.5

SObjectizer has several ready-to-use dispatchers:● one_thread. All agents work on a single working thread;● active_obj. Every agent works on a single dedicated working thread;● active_group. A single dedicated working thread is allocated for a group

of agents;● thread_pool. A working thread is selected from thread pool. Agents can

be moved from one working thread to another. But an agent can’t work on two threads at the same time;

● adv_thread_pool. A working thread is selected from thread pool. Agents can be moved from one working thread to another. Moreover an agent can work on several threads at the same time (if the agent’s event handlers are marked as thread safe).

SObjectizer Team, Feb 2015

Page 12: What is SObjectizer 5.5

A programmer can create any number of dispatchers needed. This allows to bind agents to different context in such a way that the impact of one agent on another will be minimal. For example:

● one one_thread dispatcher for AMQP-client agent;● one thread_pool dispatcher for handling requests from AMQP-queues;● one active_obj dispatcher for DBMS-related agents;● yet another active_obj dispatcher for agents whose work with HSMs

connected to the computer;● and yet another thread_pool dispatcher for agents for managing all the

stuff described above.

SObjectizer Team, Feb 2015

Page 13: What is SObjectizer 5.5

A programmer can create as many agents as he needs.

Agent is a lightweight entity.

There could be thousands, millions and hundreds of millions of agents.

Number of agents is limited only by amount of RAM and the common sense of a programmer.

SObjectizer Team, Feb 2015

Page 14: What is SObjectizer 5.5

There is an SObjectizer example which demonstrates almost all key features of SObjectizer-5.5:

● agent’s states,● messages and signals,● parent and children cooperations,● dispatchers,● delayed messages and so on...

SObjectizer Team, Feb 2015

Page 15: What is SObjectizer 5.5

Parent agent creates pinger and pointer agent pair. They do message exchange for one second.

After one second the parent agent deregisters pinger and ponger.

On finish pinger and ponger tell the parent how many messages they received.

SObjectizer Team, Feb 2015

Page 16: What is SObjectizer 5.5

Example Code (signals and message definition):#include <iostream>#include <string>

// Main SObjectizer header file.#include <so_5/all.hpp>

// Ping signal.// Signal is a special type of message which has no actual data.// Sending of signals is like sending only atoms in Erlang.struct ping : public so_5::rt::signal_t {};

// Pong signal.struct pong : public so_5::rt::signal_t {};

// Message with result of pinger/ponger run.// Unlike signal message must have actual data.struct run_result : public so_5::rt::message_t { std::string m_result;

// Messages usually have a constructor for // simplification of message construction. run_result( std::string result ) : m_result( std::move( result ) ) {}};

SObjectizer Team, Feb 2015

Page 17: What is SObjectizer 5.5

Example Code (pinger agent, beginning):// Pinger agent.class pinger : public so_5::rt::agent_t {public : pinger( // SObjectizer Environment to work within. so_5::rt::environment_t & env, // Parent's mbox for result sending. const so_5::rt::mbox_t & parent ) : so_5::rt::agent_t( env ) , m_parent( parent ) {}

// Ponger mbox will be available only after // creation of pinger/ponger pair. // This method allows to set it up later. void set_ponger_mbox( const so_5::rt::mbox_t & mbox ) { m_ponger = mbox; }

SObjectizer Team, Feb 2015

Page 18: What is SObjectizer 5.5

Example Code (pinger agent, continued): // This method is automatically called by SObjectizer // during agent's registration procedure. virtual void so_define_agent() override { // Subscription for only one signal. so_default_state().event< pong >( [this]{ ++m_pongs; so_5::send< ping >( m_ponger ); } ); }

// This method is automatically called by SObjectizer // just after successful registration. // Pinger uses this method to initiate message exchange. virtual void so_evt_start() override { // Sending signal by corresponding function. so_5::send< ping >( m_ponger ); }

SObjectizer Team, Feb 2015

Page 19: What is SObjectizer 5.5

Example Code (pinger agent, end): // This method is automatically called by SObjectizer // just after successful agent's deregistration. virtual void so_evt_finish() override { // Sending result message by corresponding function. so_5::send< run_result >( // Receiver of the message. m_parent, // This string will be forwarded to run_result's constructor. "pongs: " + std::to_string( m_pongs ) ); }

private : const so_5::rt::mbox_t m_parent; so_5::rt::mbox_t m_ponger; unsigned int m_pongs = 0;};

SObjectizer Team, Feb 2015

Page 20: What is SObjectizer 5.5

Example Code (pоnger agent, beginning):// Ponger agent is very similar to pinger.// But it hasn't so_evt_start method because it will// wait the first ping signal from the pinger.class ponger : public so_5::rt::agent_t {public : ponger( so_5::rt::environment_t & env, const so_5::rt::mbox_t & parent ) : so_5::rt::agent_t( env ) , m_parent( parent ) {}

void set_pinger_mbox( const so_5::rt::mbox_t & mbox ) { m_pinger = mbox; }

virtual void so_define_agent() override { so_default_state().event< ping >( [this]{ ++m_pings; so_5::send< pong >( m_pinger ); } ); }

SObjectizer Team, Feb 2015

Page 21: What is SObjectizer 5.5

Example Code (pоnger agent, end): virtual void so_evt_finish() override { so_5::send< run_result >( m_parent, "pings: " + std::to_string( m_pings ) ); }

private : const so_5::rt::mbox_t m_parent; so_5::rt::mbox_t m_pinger; unsigned int m_pings = 0;};

SObjectizer Team, Feb 2015

Page 22: What is SObjectizer 5.5

Example Code (parent agent, beginning):// Parent agent.// Creates pair of pinger/ponger agents,// then limits their working time,// then handles their run results.class parent : public so_5::rt::agent_t {private : // Time limit signal. struct stop : public so_5::rt::signal_t {};

// Additional state for the agent. // This state means that the first result from children agents // has been received and that the parent expects the last one. const so_5::rt::state_t st_first_result_got = so_make_state();

// Result's accumulator. std::string m_results;

public : parent( so_5::rt::environment_t & env ) : so_5::rt::agent_t( env ) {}

SObjectizer Team, Feb 2015

Page 23: What is SObjectizer 5.5

Example Code (parent agent, continued-1): virtual void so_define_agent() override { // Arrival of time limit signal means that // child cooperation must be deregistered. so_default_state().event< stop >( [this] { so_environment().deregister_coop( // Name of cooperation for deregistration. "pinger_ponger", // The reason of deregistration, // this value means that deregistration is caused // by application logic. so_5::rt::dereg_reason::normal ); } );

// NOTE: type of message is automatically deduced from // event handlers signatures.

// The first result will be received in default state. so_default_state().event( &parent::evt_first_result ); // But the second one will be received in the next state. st_first_result_got.event( &parent::evt_second_result ); }

SObjectizer Team, Feb 2015

Page 24: What is SObjectizer 5.5

Example Code (parent agent, continued-2): virtual void so_evt_start() { // Child cooperation will use active_obj dispatcher. So pinger and ponger // will work on the different working threads. so_environment().add_dispatcher_if_not_exists( "active_obj", &so_5::disp::active_obj::create_disp );

// Creation of child cooperation with pinger and ponger. auto coop = so_5::rt::create_child_coop(*this, "pinger_ponger", // active_obj dispatcher will be used as a primary dispatcher for that cooperation. so_5::disp::active_obj::create_disp_binder("active_obj") );

// Filling the child cooperation. auto a_pinger = coop->add_agent( new pinger( so_environment(), so_direct_mbox() ) ); auto a_ponger = coop->add_agent( new ponger( so_environment(), so_direct_mbox() ) ); a_pinger->set_ponger_mbox( a_ponger->so_direct_mbox() ); a_ponger->set_pinger_mbox( a_pinger->so_direct_mbox() );

// Cooperation registration. so_environment().register_coop( std::move( coop ) ); // Limit the pinger/ponger exchange time. so_5::send_delayed_to_agent< stop >( *this, std::chrono::seconds( 1 ) ); }

SObjectizer Team, Feb 2015

Page 25: What is SObjectizer 5.5

Example Code (parent agent, end):private : // Event handler for the first result. void evt_first_result( const run_result & evt ) { m_results = evt.m_result + "; ";

// State of the agent must be changed. this >>= st_first_result_got; }

// Event handler for the next (and last) result. void evt_second_result( const run_result & evt ) { m_results += evt.m_result;

// Show the results and finish work. std::cout << m_results << std::endl;

// This is deregistration of the last live cooperation. // SO Environment will finish its work. so_deregister_agent_coop_normally(); }};

SObjectizer Team, Feb 2015

Page 26: What is SObjectizer 5.5

Example Code (main function):int main() { try { // Create SO Environment objects and run SO Run-Time inside it. so_5::launch( // SO Environment initialization routine. []( so_5::rt::environment_t & env ) { // We must have the cooperation with just one // agent inside it. env.register_agent_as_coop( // The name for the cooperation will be generated // automatically by SO Environment. so_5::autoname, // The single agent in the cooperation. new parent( env ) ); } ); } catch( const std::exception & x ) { std::cerr << "Exception: " << x.what() << std::endl; }}

SObjectizer Team, Feb 2015

Page 27: What is SObjectizer 5.5

Result:

pongs: 1005040; pings: 1005041

Just about 2M messages per seconds with the exchange by a single message between two different threads (pinger and ponger work on different threads).

Core i7 2.4GHz, 8GiB RAM, Win8.1 64-bit,Visual C++ 2013 64-bit

SObjectizer Team, Feb 2015

Page 28: What is SObjectizer 5.5

Additional Information:

Project’s home: http://sourceforge.net/projects/sobjectizer

Documentation: http://sourceforge.net/p/sobjectizer/wiki/

Forum: http://sourceforge.net/p/sobjectizer/discussion/

Google-group: https://groups.google.com/forum/#!forum/sobjectizer