erlang - carc.unm.edu · erlang matures and is demonstrated. 1995 collapse of axe-n. 1998 the...

67
Erlang Jack Moffitt @metajack [email protected] http://metajack.im Superpowers

Upload: others

Post on 13-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Erlang

Jack Moffitt@metajack

[email protected]://metajack.im

Superpowers

Page 2: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Syntax

Page 3: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Prolog derived

Page 5: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

VariablesVar

FooBar_

Page 6: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Lists[1, 2, apple]

Page 7: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Tuples{1, 2, apple}

Page 8: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Strings"abc"

"abc" = [97, 98, 99].

Page 9: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Binaries<<"something">>

<<Data:160/big-unsigned-integer>>

Page 10: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Functionsfoo(Arg1, Arg2) -> io:format("hi!"), ok.

Page 11: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Functionsfoo(0) -> abc;foo(1) -> def;foo(_) -> oops.

Page 12: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

MessagesPid ! some_message.name ! {hello, 1}.receive {hello, N} -> N; _ -> 0end.

Page 13: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Overview

Page 14: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Functional

Page 15: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Pattern matching

Page 16: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Foo = bar.

[Head | Tail] = foo().

{[H | _] = List} = bar().

Page 17: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

handle_call({submit_word, Word}, {Player, _}, State = #game{ words=Words, scores=[Score1, Score2], player_data=[ #player{rating=Rating1} = PlayerData1, #player{rating=Rating2} = PlayerData2]}) ->

%% ...

{reply, ok, NewState}.

Page 18: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Binary matching

Page 19: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

IPv4 Packet<<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16, ID:16, Flgs:3, FragOff:13, TTL:8, Proto:8, HdrChkSum:16, SrcIP:32, DestIP:32, RestDgram/binary>> = Packet.

Page 20: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

MP3 Frame Headerdecode_header(<<2#11111111111:11,B:2,C:2,_D:1,E:4,F:2,G:1,Bits:9>>) ->

%% header code

Page 21: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Light-weight processes

Page 22: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Actor model

Page 23: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Distributed

Page 24: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Fault tolerance

Page 25: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

OTP library

Page 26: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

History

Page 27: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

1982–1985Exploration begins at

Ericsson Computer Science Lab

Page 28: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

1985–1988Birth of Erlang

Page 29: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

1989JAM speeds up Erlang

Page 30: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

1989-1993Erlang matures and is

demonstrated

Page 31: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

1995Collapse of AXE-N

Page 32: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

1998The bi-polar year

Page 33: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Erlang in action

Page 34: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Snack Words

Page 35: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Protocol design

Page 36: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Length-prefixed JSON2 bytes for packet length (L)L bytes of raw JSON data

Page 37: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

In Erlang:gen_tcp option{packet, N}N=1,2, or 4

Page 38: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Receive:{tcp, socket, Data}

Decode:mochijson2:decode(Data)

Page 39: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Encode:Data = mochijson2:encode(Json)

Send:gen_tcp:send(Socket, Data)

Page 40: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

In Objective-C:- - (void)sendAvailableData { NSUInteger sentBytes, maxLen; do { if (outBuffer == nil) { if ([sendQueue count] > 0) { id jsonObj = [sendQueue objectAtIndex:0]; [sendQueue removeObjectAtIndex:0]; NSString *jsonString = [jsonObj JSONRepresentation];

NSLog(@"SENDING: %@", jsonString); NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSUInteger len = htons([data length]); [outBuffer release]; outBuffer = [[NSMutableData dataWithCapacity:[data length] + 2] retain]; [outBuffer appendBytes:&len length:2]; [outBuffer appendData:data]; outPos = 0; } else { // no more data to send break; } } maxLen = ([outBuffer length] - outPos) < 4096 ? ([outBuffer length] - outPos) : 4096; sentBytes = [outStream write:([outBuffer mutableBytes] + outPos) maxLength:maxLen]; outPos += sentBytes; if ((outPos + 1) >= [outBuffer length]) { [outBuffer release]; outBuffer = nil; outPos = 0; } } while (sentBytes >= maxLen); }

Page 41: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

JSON payload:["play"]

["submit", {"word": "erlang"}]

Page 42: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

In Erlang:idle({data, [<<"play">> | _]}, StateData = #sd{player=Player}) ->

%% handle command

Page 43: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

In Objective-C:- (id)addHandlerForCommand:(NSString *)command notifyObject:(id)object selector:(SEL)selector repeat:(BOOL)repeat;

- (id)addHandlerForCommand:(NSString *)command withBlock:(LLSWClientBlock)block repeat:(BOOL)repeat;

Page 44: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Game logic

Page 45: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

PlayersEach player is a finite state

machine

Page 46: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Statesstarting, authing, idle, waiting,

playing

Page 47: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

authing({data, [<<"auth">>, Props]}, State) ->

%% authenticate

waiting({game_start, Game, Letters, Time, Players}, State) ->

%% send notification to client

playing({data, [<<"submit">>, Props]}, StateData = #sd{game=Game}) ->

%% handle word

Page 48: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

GamesEach game must track and

communicate moves, time, and scoring

Page 49: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Games receive:timer events - tick

moves - {submit_word, Word}

Page 50: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Games send:{game_start, ...}{clock, TimeLeft}

{word_found, Word, Who, ...}{game_end, Winner, ...}

Page 51: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Server design

Page 52: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

OTP application

Page 53: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Parts:players, games, waiting pool,

socket listener, bots

Page 54: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Supervision tree:

Page 55: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Process math:1000 players500 games

4 service processes5 supervisors

= 1509 processes

Page 56: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

PersistenceA small database API stores data

in Mnesia

Page 57: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Code Tour

Page 58: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

handle_info({tcp, Socket, Data}, StateName, StateData) -> %% set the socket so we can receive another data packet ok = inet:setopts(Socket, [{active, once}]), %% verify that Data is a JSON object case catch mochijson2:decode(Data) of {'EXIT', _} -> {stop, invalid_json, StateData}; [_Cmd] = Json -> ?MODULE:StateName({data, Json}, StateData); [_Cmd, _Props] = Json -> ?MODULE:StateName({data, Json}, StateData); _ -> {stop, bad_request, StateData} end;

Page 59: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

- - (void)sendAvailableData { NSUInteger sentBytes, maxLen; do { if (outBuffer == nil) { if ([sendQueue count] > 0) { id jsonObj = [sendQueue objectAtIndex:0]; [sendQueue removeObjectAtIndex:0]; NSString *jsonString = [jsonObj JSONRepresentation];

NSLog(@"SENDING: %@", jsonString); NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSUInteger len = htons([data length]); [outBuffer release]; outBuffer = [[NSMutableData dataWithCapacity:[data length] + 2] retain]; [outBuffer appendBytes:&len length:2]; [outBuffer appendData:data]; outPos = 0; } else { // no more data to send break; } } maxLen = ([outBuffer length] - outPos) < 4096 ? ([outBuffer length] - outPos) : 4096; sentBytes = [outStream write:([outBuffer mutableBytes] + outPos) maxLength:maxLen]; outPos += sentBytes; if ((outPos + 1) >= [outBuffer length]) { [outBuffer release]; outBuffer = nil; outPos = 0; } } while (sentBytes >= maxLen); }

Page 60: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

idle({data, [<<"play">> | _]}, StateData = #sd{player=Player}) -> case sw_pool:join_pool(Player) of {ok, joined} -> send([play_ok], StateData), {next_state, waiting, StateData, ?PING_INTERVAL}; {error, _} -> send([play_fail, {struct, [{reason, <<"Internal error">>}]}], StateData), {next_state, idle, StateData, ?PING_INTERVAL} end;

Page 61: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

-export([start/0, stop/1, get_player/3, create_player/2, create_player/5, save_player/2, move_player/3, authenticate/2, pick_puzzle/1, get_words/2, ...]}.

Page 62: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Code Size:sw_client.erl - 439 linessw_game.erl - 259 lines

elo.erl - 177 linessw_db_mnesia.erl - 566 lines

All code - 2416 linesAll tests - 857 lines

Page 63: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

PerformanceLoad tested to 2000 simultaneous

players on my laptop.Runs on the cheapest Linode VPS.

Page 64: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Scaling

Page 65: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Changing persistence

Page 66: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

Horizontal scaling

Page 67: Erlang - carc.unm.edu · Erlang matures and is demonstrated. 1995 Collapse of AXE-N. 1998 The bi-polar year. Erlang in action. Snack Words. Protocol design. Length-prefixed JSON

[email protected]

@metajackhttp://metajack.im

http://snackwords.com