multi-language fbp with flowex

50
Multi-language FBP with Flowex #pivorak 21, Elixir edition, March 24, 2017

Upload: anton-mishchuk

Post on 11-Apr-2017

20 views

Category:

Software


5 download

TRANSCRIPT

Page 1: Multi-language FBP with flowex

Multi-language FBP with Flowex

#pivorak 21, Elixir edition, March 24, 2017

Page 2: Multi-language FBP with flowex

Hello!I am Anton Mishchuk

▷ Ruby developer at Matic Insurance Services(we are hiring!)

▷ A big fan of Elixir programming language▷ Author and maintainer of:

ESpec and Flowex libraries

github: antonmi

2

Page 5: Multi-language FBP with flowex

What’s next?

▷ Let Elixir do what it does best

▷ The rest can be implemented in any other programming language

5

Page 6: Multi-language FBP with flowex

Agenda

▷ Quick intro to FBP

▷ Flowex library overview

▷ Tools for running external programs

▷ Multi-language components example:Ruby, Python, shell

6

Page 7: Multi-language FBP with flowex

1.Flow-Based Programming

Basic concepts

7

Page 8: Multi-language FBP with flowex

Flow-Based Programming is

a programming paradigm which models application as a graph of independent processes exchanging data across predefined connections

8

Page 9: Multi-language FBP with flowex

FBP diagram

A

B

D

C

IN 1

IN 1

IN 1

IN 2

IN 1

IN 2

OUT 1

OUT 2

OUT 1

OUT 1

OUT 1

9

Page 10: Multi-language FBP with flowex

FBP advantages

▷ Parallelism are natural

▷ No problems associated with shared memory and locks

▷ Visualisation of data flow networks is simple

▷ Components are independent and reusable

10

Page 11: Multi-language FBP with flowex

Two layers in FBP application

▷ “Bottom layer” - set of components implementing parts of business logic

▷ “Top layer” - “communication logic”  -  an organization of data flows from one component to another

11

Page 12: Multi-language FBP with flowex

“Bottom layer” - components

▷ Component is independent unit with input and output

▷ The only thing it does - transform input IP to output IP

▷ In general, can be implemented using any programming language

12

Page 13: Multi-language FBP with flowex

“Top layer” - communication

▷ Need “coordination language”

▷ Actor model of Elixir/Erlang

▷ GenStage

13

Page 14: Multi-language FBP with flowex

GenStage

▷ behavior for exchanging events with back-pressure between Elixir processes

▷ José Valim defined GenStage as “better abstractions for working with collections”

▷ but we can use it for “better interprocess communication”

14

Page 15: Multi-language FBP with flowex

2.Flowex

Railway Flow-Based Programming

15

Page 16: Multi-language FBP with flowex

Flowex

▷ a set of abstractions built on top Elixir GenStage which allows writing program with FBP paradigm

▷ a mix of FBP and Railway Oriented Programming (ROP) approach

16

Page 17: Multi-language FBP with flowex

Railway oriented programming

17

Page 18: Multi-language FBP with flowex

Railway FBP

Let’s place each function inside separate process

18

Page 19: Multi-language FBP with flowex

use Flowex.Pipeline

19

Page 20: Multi-language FBP with flowex

Functions’ definitions

20

Page 21: Multi-language FBP with flowex

Control and evaluation

21

Some new functions appear in the FunPipeline module:

▷ “start” and “stop”

▷ “call” and “cast”

Page 22: Multi-language FBP with flowex

FunPipeline.start

22

Page 23: Multi-language FBP with flowex

“call” and “cast” (sync and async)

23

Page 24: Multi-language FBP with flowex

FunPipeline.call

24

add_one

minus_three

mult_by_two

producer

consumer

number: 2

number: 2

number: 3

number: 6number: 3number: 3

self()

Page 25: Multi-language FBP with flowex

“error_pipe”

If an error happens in some pipe, functions in next pipes will not be called and IP will be bypassed to the “error_pipe”

25

Page 26: Multi-language FBP with flowex

Run pipeline via Flowex.Client

▷ Use Clients for parallel processing of many IPs

▷ Allows effective using of pipeline even with synchronous “call”

26

Page 27: Multi-language FBP with flowex

Fight bottlenecks

27

Page 28: Multi-language FBP with flowex

“Controlled parallelism”

28

Page 29: Multi-language FBP with flowex

Reusable components with module pipes

29

One should implement “init” and “call” functions(like in Elixir’s Plug library)

Page 30: Multi-language FBP with flowex

Advantages of Flowex approach

30

▷ Explicit definition of:- structure of data will be processed- way the data will come

▷ Easy to maintain and reuse:- predefined set of working processes- components are isolated and reusable

▷ Controlled parallelism:- one can control number of clients supplying data - one can control the number of processes available for each component

Page 31: Multi-language FBP with flowex

3.Running external programs

Tools

31

Page 32: Multi-language FBP with flowex

Why do we need this?

▷ Elixir/Erlang ecosystem is not so big as Java, Ruby, Python, etc.

▷ Elixir/Erlang is not the fastest platform

▷ There are not so many Elixir/Erlang developers

32

Page 33: Multi-language FBP with flowex

Erlang ports

▷ provide a byte-oriented interface to an external program over STDIN and STDOUT

▷ are process-specific resources - only owner process can talk to external process

▷ are flexible, safe and allows error trapping

33

Page 34: Multi-language FBP with flowex

ErlPort project

▷ helps connect Erlang to other programming languages

▷ currently supported external languages are Python and Ruby

▷ has Elixir wrapper - export

Read more on erlport.org

34

Page 35: Multi-language FBP with flowex

Porcelain project

▷ helps launching and communicating with external OS processes from Elixir

▷ launches external programs in a synchronous or asynchronous manner

▷ implements multiple ways of passing input to the program and getting back its output

Read more on alco/porcelain35

Page 36: Multi-language FBP with flowex

4.Multi-language components

Example project

36

https://github.com/antonmi/multi_flowex

Page 37: Multi-language FBP with flowex

MultiFlowex pipeline

▷ There will be Ruby, Python and shell “pipes”

▷ Each will say “Hello”

▷ The result will be:“Hello from Ruby, Hello from Python, Hello from shell”

37

Page 38: Multi-language FBP with flowex

multi_flowex dependencies

38

Page 39: Multi-language FBP with flowex

Ruby component

Demonstrates how one can create components using Ruby, Python or Shell

Read more on ….

39

Page 40: Multi-language FBP with flowex

Ruby code in main.rb file

40

Page 41: Multi-language FBP with flowex

Ruby component specs

41

Page 42: Multi-language FBP with flowex

Python component

Demonstrates how one can create components using Ruby, Python or Shell

Read more on ….

42

Page 43: Multi-language FBP with flowex

Python code in main.py file

43

Page 44: Multi-language FBP with flowex

Shell component

44

Page 45: Multi-language FBP with flowex

The last, Elixir component

45

Page 46: Multi-language FBP with flowex

MultiPipeline

46

Page 47: Multi-language FBP with flowex

Pipeline starts external processes

47

▷ 3 Ruby processes

▷ 4 Python processes

Page 48: Multi-language FBP with flowex

MultiPipeline specs

48

Page 49: Multi-language FBP with flowex

Conclusion

49

▷ FBP paradigm is an advantageous alternative to “conventional programming”

▷ Flowex makes it easy to convert sequential code into a pipeline of independent components

▷ Erlang ports open new horizons, so you can leverage the advantages of other programming languages

Page 50: Multi-language FBP with flowex

Thanks!Any questions?

50