concurrent programming XXL

Similar documents
Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

Concurrent programmin. ng xp. The industrial experience

<Urban.Boquist. com> Rev PA

Erlang. Joe Armstrong

Message-passing concurrency in Erlang

Erlang. Joe Armstrong.

An Introduction to Erlang

FRANCESCO CESARINI. presents ERLANG/OTP. Francesco Cesarini Erlang

An Introduction to Erlang

Erlang 101. Google Doc

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part

Erlang: distributed programming

ERLANG EVOLVES FOR MULTI-CORE AND CLOUD ENVIRONMENTS

An Introduction to Erlang

All you need is fun. Cons T Åhs Keeper of The Code

An Introduction to Erlang

MapReduce in Erlang. Tom Van Cutsem

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

Robust Erlang (PFP Lecture 11) John Hughes

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Parallel Programming in Erlang (PFP Lecture 10) John Hughes

The do s and don ts of error handling. Joe Armstrong

Lecture 8. Linda and Erlang

Erlang functional programming in a concurrent world

Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov

20 Years of Commercial Functional Programming

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

Lecture 9. Part I. Overview of Message Passing. Communication Coupling. Decoupling Blackboard. Decoupling Broadcast. Linda and Erlang.

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg

Programming Paradigms

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

DISTRIBUTED COMPUTER SYSTEMS

Reminder from last time

Sistemi in Tempo Reale

Erlang - functional programming in a concurrent world. Johan Montelius KTH

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Data Centers. Tom Anderson

Scaling Erlang to 10,000 cores.!!! Simon Thompson, University of Kent

The GSN node - a design example

Potential new case studies for behavioural types: Switching software systems

Erlang and Concurrency. André Pang Rising Sun Research

Experiments in OTP-Compliant Dataflow Programming

Windows Azure Services - At Different Levels

DISTRIBUTED SYSTEMS [COMP9243] Lecture 1.5: Erlang INTRODUCTION TO ERLANG BASICS: SEQUENTIAL PROGRAMMING 2. Slide 1

Concurrency: what, why, how

MODELS OF DISTRIBUTED SYSTEMS

Operating Systems (2INC0) 2018/19. Introduction (01) Dr. Tanir Ozcelebi. Courtesy of Prof. Dr. Johan Lukkien. System Architecture and Networking Group

Announcements. me your survey: See the Announcements page. Today. Reading. Take a break around 10:15am. Ack: Some figures are from Coulouris

The OSI Model. Level 3 Unit 9 Computer Networks

Rudy: a small web server. Johan Montelius. October 2, 2016

Beautiful Concurrency with Erlang

Death by Accidental Complexity

Chapter 1: Distributed Systems: What is a distributed system? Fall 2013

Remote Procedure Call. Tom Anderson

Scripting with Luerl

Learning and Development. UWE Staff Profiles (USP) User Guide

HiPE Copyright Ericsson AB. All Rights Reserved. HiPE March 11, 2019

SNMP and Network Management

Dual-Stack Connections in 3GPP Networks. Jari Arkko and Fredrik Garneij, Ericsson

IPv6 transition for mobile networks. Tomas lynch ip & convergence Lacnog, sao paulo, brazil October 2010

An Erlang-based Hierarchical Distributed VoD System

Custom Connect. All Area Networks. customer s guide to how it works version 1.0

The Golden Trinity of Erlang How Something Simple Has Real Business Value

BEAMJIT: An LLVM based just-in-time compiler for Erlang. Frej Drejhammar

CAS 703 Software Design

Declarative concurrency. March 3, 2014

Design of Embedded Systems

Logic Programming II & Revision


Holistic IPv6 Transition Yanick Pouffary HP Distinguished Technologist HP IPv6 Global Leader, HP Technology Services Office of the CTO

SNMP and Network Management

Kent Academic Repository

MODELS OF DISTRIBUTED SYSTEMS

Raghav Karol Motorola Solutions. Torben Hoffmann Motorola Solutions

Programming Languages 3. Definition and Proof by Induction

Lecture 7: February 10

CS 3 Introduction to Software Engineering. 3: Exceptions

StoryStylus Scripting Help

1. a) Discuss primitive recursive functions with an example? 15M Or b) Statements and applications of Euler s and Fermat s Theorems?

CPSC 426/526. Cloud Computing. Ennan Zhai. Computer Science Department Yale University

Software System Design and Implementation

Fault Tolerance Part I. CS403/534 Distributed Systems Erkay Savas Sabanci University

Chapter 1: Introduction Operating Systems MSc. Ivan A. Escobar

Stacks and queues (chapters 6.6, 15.1, 15.5)

The case for reactive objects

Distributed Object-Based Systems The WWW Architecture Web Services Handout 11 Part(a) EECS 591 Farnam Jahanian University of Michigan.

Functional Programming In Real Life

Motivation. Map in Lisp (Scheme) Map/Reduce. MapReduce: Simplified Data Processing on Large Clusters

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 19: Networks and Distributed Systems

DS 2009: middleware. David Evans

Network Communication and Remote Procedure Calls

The Lion of storage systems

MORE RECURSIVE FUNCTIONS

Chapter 17: Distributed Systems (DS)

Topic 5: Examples and higher-order functions

Network Security and Cryptography. 2 September Marking Scheme

Choose an internet connection to suit your business

Transcription:

concurrent programming XXL Industrial Use of Erlang Introduction Karol Ostrovský (karol.ostrovsky@gmail.com)

Motivation Machine Ericsson Blade System 3 sub-racks 14 blades 2 routers 12 compute nodes 6 core Intel x86 12 SMT threads total 432 simultaneously running processes Backplane: 1 or 10Gbps Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 2

Motivation Machine Open Compute System New OpenRack design Triplet rack 3 sub-racks 18 dual-cpu blades 8 core Intel x86 16 SMT threads total 2592 simultaneously running processes Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 3

Motivation The task Design on of the following systems: Payment transaction handling Instant messaging back-end Multi-player game back-end Cloud infrastructure message-passing middleware Cloud routing/switching Telephone exchange Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 4

Motivation consider What are the main challenges? What distinguishes those systems from others (see the picture on the right)? What tools might be appropriate? Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 5

Karol Ostrovský M.Sc. Comenius University, Bratislava Ph.D. Chalmers Post-doc Chalmers System Designer Dfind, on assignment at Ericsson Operations & Maintenance Subsystem Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 6

My Chalmers Years Research in static analysis of concurrent programming languages Type systems Protocol analysis Main course responsible PPxT Developed the course between 2005 and 2010 Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 7

Phd thesis Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 8

KO@Ericsson Business Unit Networks Development Unit IP and Broadband Product Development Unit Packet Core - SGSN-MME O&M sub-system 2G sub-system 3G sub-system... - EPG - CPG -... Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 9

Mobile telecom network Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 10

Packet core network 3GPP Defines standards (mostly protocols) Interoperability is essential SGSN-MME Servicing GPRS Support Node (2G/3G) Mobility Management Entity (4G) Control signalling Admission control, Authentication Mobility, roaming Payload transport (not in 4G) Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 11

ChallengeS Open soft real-time system Resilience HW failures SW failures External Internal Appropriate tools? Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 12

Enter erlang Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open Check the source code of generic behaviours Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 13

recursion -module(list_stuff). -export([append/2, reverse/1]). append([x Xs], Ys) -> [X append(xs, Ys)]; append([], Ys) -> Ys. reverse([h T]) -> append(reverse(t), [H]); reverse([]) -> []. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 14

Tail recursion -module(list_stuff). -export([reverse/1, append/2]). reverse(xs) -> reverse_a(xs, []). reverse_a([x Xs], Acc) -> reverse_a(xs, [X Acc]); reverse_a([], Acc) -> Acc. %%continues Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 15

Tail recursion %%continuation append(xs, Ys) -> reverse_a(r?verse(xs),ys). reverse_a(reverse(xs),ys). Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 16

Concurrency Based on Message Passing: q: What form of synchronisation? A: Asynchronous q: What form of process naming? A: Direct, asymmetric A send MSG to B B receive anything Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 17

Asynchronous MP Asynchronous send, receive from mailbox send receive Pid! Msg receive Msg-> receive Msg-> Pid! Msg Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 18

Concurrent execution Spawn a function evaluation in a separate thread Callout Pid = spawn( fun() -> loop(42) end) Function must use constant space Tail recursive fun() -> loop(42) end Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 19

Receive order A receive statement tries to find a match as early in the mailbox as it can msg_1 msg_2 msg_1 msg_3 msg_2 msg_4 S msg_4 S receive msg_3 -> end Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 20

Receive order A receive statement tries to find a match as early in the mailbox as it can msg_1 msg_2 msg_1 msg_4 S msg_4 S receive msg_4 -> msg_2 -> end Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 21

Client-server Common asynchronous communication pattern For example: a web server handles requests for web pages from clients (web browsers) client {request, Reg} client {result, Rep} server client client Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 22

Modelling c-s Computational server Off-loading heavy mathematical operations For example: factorial -module(math_server). -export([start/0]). -export([compute_factorial/2, get_count/2]). -export([loop/1]). start() -> spawn(fun() -> loop(0) end). %%continues Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 23

Main server loop %%continuation loop(count) -> receive {factorial, From, N} -> Result = factorial(n), From! {result, Result},?MODULE:loop(Count+1); {get_count, From} -> From! {result, Count},?MODULE:loop(Count); stop -> true end. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 24

Client Interface Encapsulating client access in a function Private channel for receiving replies? %%possible continuation compute_factorial(pid, N) -> Pid!{factorial, self(), N}, receive {result, Result} -> Result end. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 25

References Private channel for receiving replies? Find the corresponding reply in the mailbox receive {result, Result} -> Result end BIF make_ref() Provides globally unique object different from every other object in the Erlang system including remote nodes Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 26

RPC Client Interface References to uniquely identify messages References allow only matching %%usual continuation compute_factorial(pid, N) -> Ref = make_ref(), Pid!{factorial, self(), Ref, N}, receive {result, Ref, Result} -> Result end. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 27

RPC Main Server Loop %%continuation loop(count) -> receive {factorial, From, Ref, N} -> Result = factorial(n), From! {result, Ref, Result},?MODULE:loop(Count+1); {get_count, From, Ref} -> From! {result, Ref, Count}, loop(count); stop -> true end. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 28

A Generic Server Desired features Proper reply behaviour RPC Parameterised by the engine function F Allows the engine to be upgraded dynamically Robust: does not crash if the engine goes wrong Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 29

Generic Client-Server Higher-order functions can be used to capture the design pattern of a client-server in a single module The main engine of a server has type : F(State, Data) -> {Result, NewState} The state of the server before computing the query The new state of the server after the query Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 30

Generic server loop loop(state, F) -> receive {request, From, Ref, Data} -> {R, NS} = F(State, Data), From! {result, Ref, R}, loop(ns, F); {update, From, Ref, NewF} -> From! {ok, Ref}, loop(state, NewF); stop -> true end. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 31

client interface Generic client can make a generic request request(pid, Data) -> Ref = make_ref(), Pid!{request, self(), Ref, Data}, receive {result, Ref, Result} -> Result end. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 32

Registered Processes Centrally register a process under a name BIF: register(atom(), pid()) -> true start(name, State, F) -> Pid = spawn(fun() -> loop(state, F) end), register(name, Pid), Pid. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 33

Registered Processes Sending to a registered process math_server! stop. For registered processes the name is the same as its pid Almost (subtle differences in error handling) Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 34

server instance -module(g_math_server). -export([start/0]). -export([compute_factorial/1, get_count/0]). start() -> g_server:start( math_server, 0, fun fx/2). fx(count, {factorial, N}) -> Result = factorial(n), {Result, Count+1}; fx(count, get_count) -> {Count, Count}. %%continues Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 35

Server instance %%continuation compute_factorial(n) -> g_server:request(math_server, {factorial, N}). get_count() -> g_server:request(math_server, get_count). Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 36

Parallelised Server Compute the heavy computations in separate worker processes New engine function New error handling issues client {request, Reg} worker {result, Rep} server client worker Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 37

State diagram receive request -> result ->?result?request receive new_state ->?new_state Worker!new_state Worker!result Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 38

New engine function Higher-order functions can be used to capture the design pattern of a client-server in a single module The engine function will run a as process The main engine of a server has type : F(State, Data, SPid, Ref) -> Proc() SPid!{new_state,Ref,NewState}, SPid!{result,Ref,Result} Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 39

Parallel server loop loop(count, F, Pending) -> receive {request, From, Ref, Data} -> Self = self(), Pid = spawn( fun() -> F(State, Data, Self, Ref) end), receive {new_state, Ref, NewState} ->?MODULE:loop(NewState, F, [{From,Ref,Pid} Pending]); end; Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 40

Parallel server loop {result, Ref, Result} -> case keysearch(ref, 2, Pending) of {value, {From, Ref, _}} -> From!{result, Ref, Result},?MODULE:loop(State, F, Module lists keydelete(ref,2,pending)); false ->?MODULE:loop(State,F,Pending) end; %% update and stop as before end. Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 41

Dynamic code update We want to update the running math server from V1 to the parallelised V2 Compile and load new version, Send any message, And the new loop code will be called Problems? New engine function type New internal state (pending list) loop/2 versus loop/3 Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 42

Dynamic code update Updating the loop between versions loop(oldstate, OldF) -> io:format("this is V2 starting...~n",[]), loop(oldstate, fun(state, Data, SPid, Ref) -> {Result,NewState} = OldF(State,Data), SPid!{new_state, Ref, NewState}, SPid!{result, Ref, Result} end, []). Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 43

New math engine fx(count, {factorial, N}, SPid, Ref) -> SPid!{new_state, Ref, Count+1}, SPid!{result, Ref, factorial(n)}; fx(count, get_count, SPid, Ref) -> SPid!{new_state, Ref, Count}, SPid!{result, Ref, Count}. update_fx() -> g_server:update(math_server, fun fx/4). Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 44

Erlang/OTP Framework for defining applications Defines several general behaviours, examples: Servers Finite state machines Event handlers Supervisors Applications Behaviour captured once and for all Remains to define application-specific functions, Which are often without concurrency problems Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 45

Erlang/OTP The main source http://www.erlang.org/ Excellent learning resource http://learnyousomeerlang.com/ Plenty of useful blogs and mailing lists Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 46

Erlang strengths Well suited for Control handling of telecom traffic Application layer (OSI model) applications Web servers, etc. Domain Specific Language framework Test scripting Reasonably high-level (as compared to for example C) Good for software maintenance OTP includes useful building blocks Supervisor Generic server Finite state machine Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 47

Erlang weaknesses A bit too low-level language Given current HW limitations one must sometimes optimise to the point where the code is not portable (with the same performance) Raise the abstraction and provide a customisable compiler, VM Where is the type system? A static type system of Haskell-nature would be a hindrance But good static analysis tools are desperately needed Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 48

The missing subject Software maintenance Software lives long Especially telecom systems (decades) Banking systems live even longer (think COBOL) People change Organisations change Hardware changes Requirements change Documentation often does not change Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 49

maintenance The developer s challenge Write simple (readable) and efficient code: 1. Write a straightforward and working solution first 2. Optimise later (or even better skip this step) Think smart but do not over-optimise Optimisations complicate maintenance The code is often the only reliable document Types can be very good documentation Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 50

Synthesis vs. analysis Emphasis on synthesis so far Software development Around 30% of manpower is spent on testing Analytical work Do you like to break a system? But testing can also be synthetical Testing frameworks Quickcheck SGSN-MME has its own Would you like to formally prove the system correct? Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 51

More than true Sayings The greatest performance improvement of all is when a system goes from not-working to working The only thing worse than a problem that happens all the time is a problem that doesn't happen all the time PPVT10 Introduction 54 Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 52

My favourite The Message from this Course Should you forget everything from this course, please, remember at least this saying: Use the right tool for the job. 3 Introduction to Large Scale Concurrent Programming Karol Ostrovský Ericsson AB 2012 2012-09-18 Page 53