Top Banner
Distributed Actor System in Rust Zimon Dai
34

Zimon Dai - distributed actor system in rust

Oct 01, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Zimon Dai - distributed actor system in rust

Distributed Actor System in RustZimon Dai

Page 2: Zimon Dai - distributed actor system in rust

About me

• Zimon Dai

• Senior Engineer @ Alibaba Inc

• Full-time Rust dev since 2016

[email protected]

Page 3: Zimon Dai - distributed actor system in rust

This talk is not about

• Details of full featured actor system

• Comparison with other popular actor systems (Actix, etc.)

• Feature introduction of any crate

Page 4: Zimon Dai - distributed actor system in rust

This talk is about

• How to solve common problems when building an actor system in Rust

• Compilation-stable type id

• Proc macros

• Specialization

• Tick-Based actor system

Page 5: Zimon Dai - distributed actor system in rust

The Type-id Problem

Broker BBroker A

Messages

Actor BActor A

BUFFER Messages

Page 6: Zimon Dai - distributed actor system in rust

The Type-id Problem

• Messages need to be encoded into buffers ( Vec<u8> ) so they could be transferred in a network

• How could a receiver actor recover the type information of a message?

Page 7: Zimon Dai - distributed actor system in rust

Give each message payload type a Type Id

Page 8: Zimon Dai - distributed actor system in rust
Page 9: Zimon Dai - distributed actor system in rust

Type id

• Type ID needs to be stable across the network, or it could lead to decoding error

• We could not use std::any::TypeId

• It generates different type ids with each compilation

• Network could be running software from different compilations

Page 10: Zimon Dai - distributed actor system in rust

Proc Macro to the Rescue

• Get the ident of target struct payload

• Save the ident + id combo to a local file

• Read the file on next compilation to recover the type id

Page 11: Zimon Dai - distributed actor system in rust
Page 12: Zimon Dai - distributed actor system in rust

The payload struct we need to assign a unique type id to

Load id from local file

Page 13: Zimon Dai - distributed actor system in rust

Type Id

• Once we get a stable type id, we could use it to erase / recover type information for networking

• T ———(serialization)——— Vec<u8>

• &[u8] ——— (Type Id matching deserialization) ——— T

Page 14: Zimon Dai - distributed actor system in rust

This is more or less similar to Reflection in Java

Page 15: Zimon Dai - distributed actor system in rust
Page 16: Zimon Dai - distributed actor system in rust

You still need to match against all types !

Page 17: Zimon Dai - distributed actor system in rust

Solve it with proc macros (again…)

Page 18: Zimon Dai - distributed actor system in rust
Page 19: Zimon Dai - distributed actor system in rust

Need declarations of message types

Page 20: Zimon Dai - distributed actor system in rust
Page 21: Zimon Dai - distributed actor system in rust

Using proc macros we get:

• A super clean, self-explaining actor design

• Separating actor declaration / private logic with message handling logic

• Hiding dangerous type casting behind the curtain

• Minimal runtime cost (only an integer comparison)

Page 22: Zimon Dai - distributed actor system in rust

The Codec Problem

PayloadTypeA

bincode

Vec<u8>

PayloadTypeB

Proto-buffer

Vec<u8>

Page 23: Zimon Dai - distributed actor system in rust

The Codec Problem• Messages could use different codecs

• We are adopting a fast se/de crate: abomonation by Frank McSherry

• Super fast, but quite unsafe

• Do not support HashMaps

• We could use different codecs for different messages

• Important ones with hash maps: Bincode

• Small, not-so-important messages: Abomonation

Page 24: Zimon Dai - distributed actor system in rust

Specialization (RFC #1210)

• Allows trait impls to overlap with each other

• Allows a default impl of a trait

Page 25: Zimon Dai - distributed actor system in rust

SpecializationSerde se/de traits

Page 26: Zimon Dai - distributed actor system in rust

SpecializationDefault to serde/bincode

Page 27: Zimon Dai - distributed actor system in rust

Specialization

• Available on nightly

• #![feature(specialization)]

Page 28: Zimon Dai - distributed actor system in rust

Tick-based actor system

Handler M1 Handler M2 Handler M3

Service B

Handler M1 Handler M2

Service AService Registry(Singleton)

Actor Bundle(Instances)

Actor A1 Actor A2

Actor A3 Actor A4

Handler M1 Handler M1

Handler M1Handler M1

Actor B1 Actor B2

Actor C1 Actor C2

Handler M3 Handler M3

Handler M1Handler M1

Worker 2 (Thread 2, with service registry, actor bundle)

Worker N (Thread N, with service registry, actor bundle)

Worker 1 (Thread 1, with service registry, actor bundle)Broker

System Module

Messages

Messages

Messages

Messages

Worker Module

Messages

Broker 2

Messages

Page 29: Zimon Dai - distributed actor system in rust

Tick - Why?

• Tick is useful for many use cases

• Game design ( logics are executed per frame)

• Dataflow / Stream computation

• Easier logic / waiting / event hook

Page 30: Zimon Dai - distributed actor system in rust

Future with ticks• Block tick for specific message

• Create a Stream, with each output, step tick forward by 1

• Maintain a map of each tick’s waits

• If all waits are resolved, return Poll::Ready(messages)

Page 31: Zimon Dai - distributed actor system in rust

Feature with ticks

• Wait for response

• By setting deadline = 1

• User-defined pre-fetching

• By setting dynamic deadline based on current traffic

Page 32: Zimon Dai - distributed actor system in rust

Distributed Actor System

• Tick based message system

• Support multiple codecs with specialization

• Use compilation-stable type ids for arbitrary message type reflection

• We are working on open sourcing this actor framework in 2019

Page 33: Zimon Dai - distributed actor system in rust

• Alibaba 🧡 Rust

• We are building quite some frameworks with Rust

• Looking forward to be a better participant of the community in 2019

Page 34: Zimon Dai - distributed actor system in rust

Thanks for your time