модели акторов в с++ миф или реальность

56
Модели акторов в С++ миф или реальность Боргардт Александр Александрович https://github.com/smart-cloud

Upload: corehardby

Post on 21-Feb-2017

94 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: модели акторов в с++ миф или реальность

Модели акторов в С++ миф или реальностьБоргардт Александр Александрович

https://github.com/smart-cloud

Page 2: модели акторов в с++ миф или реальность

What is in the report:

1. The general concept of the actor model;

2. Consider the example of a solved problem with Productions;

3. The internal device of the actor;

4. A little about the life cycle of threadpool;

5. About Different strategies work threadpool.

Page 3: модели акторов в с++ миф или реальность

Mythology

Page 4: модели акторов в с++ миф или реальность

Mythology

Page 5: модели акторов в с++ миф или реальность

Intro

Page 6: модели акторов в с++ миф или реальность

Intro Project Architecture

Smart Proxy Smart Proxy Smart Proxy Smart Proxy

Nginx Nginx Nginx

Service Service Service ServiceService

Client Client Client

Page 7: модели акторов в с++ миф или реальность

Smart Proxy

Other Service

ServiceStatisticShard1

ServiceUser

ServiceStatisticShard N

Queuedb

CACHE

Intro Part of the project Architecture

Page 8: модели акторов в с++ миф или реальность

Intro The components that hold the system

EventLoopdb

EventLoopqueue

EventLoopnetwork

ThreadPool

1 2 3 4

PipeLine

PipeLinePipeLine

PipeLine

PipeLine

Page 9: модели акторов в с++ миф или реальность

Intro

Page 10: модели акторов в с++ миф или реальность

● Single Thread -> Nodejs, python3.4+

● Actor model -> Erlang, Elixir, jvm family, C#

● Communicating sequential processes -> go

● Software transactional memory -> clojure

● etc ...

What models of concurrent programming are?

Page 11: модели акторов в с++ миф или реальность

Actor Model what kind of animal?

Page 12: модели акторов в с++ миф или реальность

Formally, the description of the actor modelIt has an ID on which the actor can be identified by other actors;

May interact with other actors only by sending messages with the identifiers of

the actors addresses;

Implements its behavior depending on the incoming messages;

May create one or more new actors;

Can change its state;

Can terminate its execution.

https://en.wikipedia.org/wiki/Actor_model

Page 13: модели акторов в с++ миф или реальность

Actor ModelActor

C++OPP

SmalltalkClass ≈ ≠

What looks like an actor?

Page 14: модели акторов в с++ миф или реальность

What are the implementation?

C++ Actor Framework: CAFhttps://www.actor-framework.org/CAF -> КАФ

SObjectizerhttps://github.com/eao197/so-5-5SObjectizer -> собжектайзер

Page 15: модели акторов в с++ миф или реальность

Message

AsyncSend

ThreadPool

Environment

General Schematic

Actor

Page 16: модели акторов в с++ миф или реальность

One of the main ideas of the actor model

Actor

Actor

Actor

Message

Message

Message

Page 17: модели акторов в с++ миф или реальность

One of the main ideas of the actor model

Actorping {};

request response

pong {};

does_not_understand{};Actorother_method{};

Page 18: модели акторов в с++ миф или реальность

Analogy

Browse service

http://mysuperservice.ru/update?d=1&ds=3

Page 19: модели акторов в с++ миф или реальность

Supervisor

Supervisord

Case : restored after the fall of the actor

Time

Supervisord Supervisord

Page 20: модели акторов в с++ миф или реальность

Supervisor

Case : balancing messaging

Supervisord

MSG

Time

Page 21: модели акторов в с++ миф или реальность

Supervisor

Page 22: модели акторов в с++ миф или реальность

Analogy

Browse

Page 23: модели акторов в с++ миф или реальность

Description of a problem Productions

Counters

Json

User service

Case 1:We obtain the unique identifier and increase the counter by one Case 2:Return a list of all the stored hash from uid and the counter

service

Page 24: модели акторов в с++ миф или реальность

Description of possible types of messages.

ActorCountersput {

string };

get_all {};

request response

get_all {vector<string,std::size_t>};

put {};

does_not_understand{};

ActorCounters

ActorCountersother_method{};

Page 25: модели акторов в с++ миф или реальность

Scheme of the initial solution

Dispatcher

Counters

gen-hash

BrokerIOClient

Page 26: модели акторов в с++ миф или реальность

Driving the initial decision to supervisord

SupervisordCounters

Supervisordgen-hash

BrokerIO

Counters

gen-hash

Client

Client

Client

Page 27: модели акторов в с++ миф или реальность

SupervisordCounters

Supervisordgen-hash

BrokerIO

SupervisordCounters

Counters

CountersCounters

gen-hash

Counters

Driving the initial decision to supervisord

Page 28: модели акторов в с++ миф или реальность

General Schematic

Environment

Actor Actor

Actor Actor

cooperation

executable*

ThreadPool

Page 29: модели акторов в с++ миф или реальность

Actor

state

events loop

UserMethod

Page 30: модели акторов в с++ миф или реальность

Actor

state

events loop

UserMethod

Page 31: модели акторов в с++ миф или реальность

Data locality and Isolated

HashMap

Message(“update”,{1,1,2})

Actor

Page 32: модели акторов в с++ миф или реальность

Data locality and Isolated

HashMapHashMap

HashMap

Message(“get”)

Message(“merge”, )

Page 33: модели акторов в с++ миф или реальность

Shared State

Page 34: модели акторов в с++ миф или реальность

Data locality and Isolated Shared State

HashMapHashMap

Message(“get”)

Message(“merge”, hm*) or Message(“merge”, hm&)

Page 35: модели акторов в с++ миф или реальность

Actor

state

events loop

UserMethod

Page 36: модели акторов в с++ миф или реальность

Queue …Un-bound Queue ????

Page 37: модели акторов в с++ миф или реальность

System + Un-bound Queue≈Key word:Queueing theory

Un-bound Queue in Your systems

Page 38: модели акторов в с++ миф или реальность

Un-bound Queue in Your systems

...

Size ?

Size ?

Page 39: модели акторов в с++ миф или реальность

Actor

state

events loop

UserMethod

Page 40: модели акторов в с++ миф или реальность

EventLoop in Actor blocking approach

void run(args ..., args ...,) { message *msg_ptr = nullptr; while (has_next_message()) { msg_ptr = next_message(); if (msg_ptr != nullptr) {

*************************************** auto response = life.invoke(msg_ptr); ***************************************

} else { *************************************** } }

Page 41: модели акторов в с++ миф или реальность

EventLoop in Actor Pseudo non-blocking

auto run(args ..., size_t max_throughput, args ...,) -> event_type { message *msg_ptr = nullptr; for (size_t handled_msgs = 0; handled_msgs < max_throughput;) {

msg_ptr = next_message(); if (msg_ptr != nullptr) {

************************************ auto response = life.invoke(request); ************************************ ++handled_msgs; ************************************

} else { return executable_result::awaiting; }}

return executable_result::resume; }

Page 42: модели акторов в с++ миф или реальность

EventLoop in Actor Pseudo non-blocking

auto run(args ..., size_tmax_throughput, args ...,) -> event_type { message *msg_ptr = nullptr; for (size_t handled_msgs = 0; handled_msgs < max_throughput;) {

msg_ptr = next_message(); if (msg_ptr != nullptr) { ************************************ auto response = life.invoke(request); ************************************ ++handled_msgs; ************************************

} else { return executable_result::awaiting; }}return executable_result::resume; }

Page 43: модели акторов в с++ миф или реальность

struct magick_method final : public abstract_action { *********************************** response *operator()(request *msg) override final { ***********************************

a lot of data processing *********************************** return nullptr; }};

EventLoop in Actor Pseudo non-blocking

Page 44: модели акторов в с++ миф или реальность

EventLoop in RunOnce

auto run_once(args ..., args ...,) {return run(args ..., 1, args ...,)

}

How to run once?

Page 45: модели акторов в с++ миф или реальность

General Schematic

Environment

Actor Actor

Actor Actor

cooperation

executable*

ThreadPool

Page 46: модели акторов в с++ миф или реальность

EventLoop in Actor System

Job Shared Queue

Worker Worker Worker

Strategy: shared-work

Page 47: модели акторов в с++ миф или реальность

EventLoop in Actor SystemStrategy: shared-work

Page 48: модели акторов в с++ миф или реальность

EventLoop in Actor System

Page 49: модели акторов в с++ миф или реальность

EventLoop in Actor System

asynchronous synchronous

proactive Work-balancing Work-distribution

reactive Work-stealing Work-requesting

And what are the tasks scheduling strategy?

Page 50: модели акторов в с++ миф или реальность

EventLoop in Actor System

asynchronous synchronous

proactive Work-balancing Work-distribution

reactive Work-stealing Work-requesting

And what are the tasks scheduling strategy?

Page 51: модели акторов в с++ миф или реальность

EventLoop in Actor System

asynchronous synchronous

proactive Work-balancing Work-distribution

reactive Work-stealing Work-requesting

SCHEDULER = (STEALING ^ REQUESTING) [+DISTRIBUTION] [+BALANCING]

Page 52: модели акторов в с++ миф или реальность

RealityPossible applications:● back-end game dev;● micro-service;● service;● macro-service;● distribution system;● etc … .

Page 53: модели акторов в с++ миф или реальность

? Questions ?

Page 54: модели акторов в с++ миф или реальность

abstarct_actor

local_actor

scheduler_actor

actor

bloking_actor

group

cooperation

Bonus slide

Inheritance Hierarchyabstract_actor*

The hierarchy of nesting

Page 55: модели акторов в с++ миф или реальность

What do you want to change?

abstarct_actor

local_actor

template<ParentActor=local_actor, Supervisor=base_supervisor, async=true >scheduler_actor: public ParentActor,

public Supervisor, public executable{};

base_supervisor vs fake_supervisor Pseudo non-blocking aka async vs blocking

Bonus slide

Page 56: модели акторов в с++ миф или реальность

Urls:http://www.ponylang.org/

http://junior.highload.ru/2015/#301215

http://www.highload.ru/2015/abstracts/1964.html

https://en.wikipedia.org/wiki/Actor_model

https://en.wikipedia.org/wiki/Work_stealing

http://www.1024cores.net

http://www.slideshare.net/gpadovani/c-actor-model

https://habrahabr.ru/post/216049/