Title: Erlang
1????? Erlang
- ??????? ???????? (ekirpichov_at_gmail.com)
- SPb Haskell User Group
- 2008
2????
- ????????, ??? ?????? ?????
- ????????, ??? ?? ????? ????? ?????
- ????????, ??? ?? ??? ????? (????? ??????
??????????) - ????????, ??? ?? ?? ????? ??? ????? ?????
- ????????, ??? ??????? ?? ??? ??????
- ????????, ??? ?????? ? ??? ??????
3?? ????
- ???? ????????? ??????? ??? ???? ?????????? ??? ?
???? ??????????? ?????????? - ?????????? ? ???? ????????? ? ????????????
- ??????? ???? ?????? ?????? ?????? ?? ???????
4??????????
- ????? ????? ? ?????????
- ?????? ?????
- ??????, ???? ????????
- ?????? ????????
- Mnesia
- OTP
- Guidelines
5????? ?????
- ??????????? ? ?????????? ????????????????
- ?? ????, ?? ???????? (????????? ??????????, ???
shared state) - ? ??????? ???????? ???? ???? ? ???? GC
- Message-passing
6????? ?????
- ??????????????
- ?????????? ???????????
- ???? ??????? ??????? ???????
- ????????? ???????, ?? ????????????
- ????????????
- ???? apply, eval ? ?.?
7????? ?????????
- Open-source
- ?????????, ???? ?????? ?????????
- OTP (Open Telecom Platform)
- Mnesia ??????????? ??
- CORBA ? ??.
- ???????? ? C, Java
- ???????? ? ??????? ??
8????? ?????????
- ???? ????????????
- dialyzer ????? ?????? ????????? ? ?.?.
- ????????, ????????????
- ????????? ???????????
- ?????????????????
- ?????? yacc/flex
9????? ?????
- ?????, ??????, ??????, ??????, ??????, ???????
- ????????? ??????? ??? ??????? map/2
- ???? (??? ? ?????)
- apple, answer_is_42, a Long Atom
- PID
- Port ID
- ??? IPC ? ???????? ??????????, pipe
- Reference ( GUID)
- Binary
- ??????? ????? ???????? ??????
- (??????-?? ?????? ??? ????????)
10????? ?????
- ??????
- ????? ??????? ???? namespace
- 1 ???? 1 ??????
- ???-?? ???????????, ???-?? ????????????
- ??? ?????? - ????
- First-class object!
- apply(Module,Function,Args) ModuleFunction(Args
) - MFA Module,Function,Args
- ??????? ????? ?????? ??????? ? ?????????
11???????????????
- ???????? ????????
- Pid spawn (fun operate/0)
- spawn(M,F,A)
- self() PID ???????? ????????
- ??????? ????????
- exit(Pid, Reason)
- ??????? ?????????
- Pid ! msg
12???????????????
loop() -gt receive Pattern -gt Action
Pid, ping -gt Pid ! self(),
pong, loop() Pid,
please_double, N -gt Pid !
here_you_are, 2N, loop()
die -gt ioformat("Goodbye cruel
world!n") after 10000 -gt
ioformat("I'm tired") end.
13???????????????
- ?????????????????? ????????
- register(Atom, Pid)
- register(the_server, spawn(fun server_loop/0))
- whereis(Atom) ? Pid
- the_server ! Msg
- ????????????????
- spawn(Node,Fun)
- spawn(Node,M,F,A)
14????????? ??????
- ????????? ????????? (????? ???? ??????????? ?
???? ??????) - ???????? ??????
- ??? ?? ?????, ??? ????? ? ????????? ????
????????? - spawn
- ? ???? ???????, ???? ???? ????????? ???? ???????
- spawn_link
- ? ????????? ?????? ?????????? ???? ????????
(????????, ?????????? ???) - trap_exit, spawn_link
15????????? ??????
- ?????????
- ????? (link)
- ?????????? ???? ??????
- A,B ??????? A ???? ? B ??????? ?????? ??????
- ?????? ?????? (exit signal)
- ?????-?? ???? ??????-??
- kill , normal, ??? ?????????
- ????????? ???????
- ?? ??????? ??? ?????? ????-?? ?? linked set
(????? kill) - process_flag(trap_exit, true)
- ???????? EXIT, Pid, Reason
16????????? ????????Linked processes
- ????????? ?????????
- ???? ???? ????? ????
- spawn_link(Fun), ...
- link(Pid)
- ????, ????? ?????????
- ?????????
- ??? ??? - monitor
17????????? ????????Linked processes
exit(normal)
exit(X)
exit(killed)
exit(kill)
exit(X)
18??????
???????? ????????
-module(ping). -export(main/0). main() -gt
Server spawn(fun server/0), Client
spawn(fun () -gt client(Server) end).
server() -gt receive Client, ping
-gt ioformat("Ping from pn",
Client), Client ! self(), pong,
server() end. client(Server) -gt
Server ! self(), ping, receive
Server, pong -gt ioformat("Pong
from pn", Server),
client(Server) end.
?????? ?? ??????? 0 - ???????
????????? ???????
??? ?????????? ? ??????? ?????
? ????????? ????? ?????
??????? ????????? ?? ???????
PID ???????? ????????
??????? ?????????
Pattern matching
19??????
-module(ping). -export(main/0). main() -gt
Server spawn(fun server/0), Client
spawn(fun () -gt client(Server) end).
server() -gt receive Client, ping
-gt ioformat("Ping from pn",
Client), Client ! self(), pong,
server() end. client(Server) -gt
Server ! self(), ping, receive
Server, pong -gt ioformat("Pong
from pn", Server),
client(Server) end.
20????????? ??????
- ?????????? RPC, API ????? RPC
- computePeculiarValue(Srv, Args) -gt
- rpc(Srv, computePeculiarValue, Args).
- doGoodThing(Srv, Args) -gt
- cast(Srv, doGoodThing, Args).
- rpc(Pid,Msg) -gt
- Pid ! self(), Msg,
- receive
- Pid, Response -gt Response
- end.
- cast(Pid,Msg) -gt Pid ! self(), Msg.
????? ? ?????? ????????? ?????? PID ???????????
21????????? ??????
- ??????????? RPC
- Srv ! self(), connect, Username, Password,
- prepareOurInternalStructuresMeanwhile(),
- receive
- Srv, connected -gt
- doMainLoop()
- Srv, refused -gt
- reportError()
- end
22????????? ??????
dialing(Digits) -gt receive key,
backspace -gt dialing(liststail(Dig
its)) key, Digit -gt
dialing(DigitDigits) hangUp -gt
disconnected() off -gt off() after
?DIAL_TIMEOUT -gt connecting(listsreverse(
Digits)) end
off() -gt receive on -gt
disconnected() end
disconnected() -gt receive ring,
Someone -gt ringing(Someone)
pickupReceiver -gt dialing()
off -gt off() end
ringing(Who) -gt receive
pickupReceiver -gt talking(Who) abort,
Someone -gt disconnected() off -gt off()
end
23Reader-writer lock
-module(rwl). -export(new/0, lockRead/1,
lockWrite/1, unlockRead/1, unlockWrite/1). new()
-gt spawn(fun free/0). lockRead(L) -gt rpc(L,
lockRead). lockWrite(L) -gt rpc(L,
lockWrite). unlockRead(L) -gt cast(L,
unlockRead). unlockWrite(L) -gt cast(L,
unlockWrite). rpc(Pid,Msg) -gt Pid !
self(), Msg, receive Pid,
Response -gt Response end. cast(Pid, Msg) -gt
Pid ! self(), Msg.
API
24Reader-writer lock
free() -gt receive Pid, lockRead -gt
Pid ! self(),ok,
readers(1) Pid, lockWrite -gt
Pid ! self(),ok, writer(Pid)
end. readers(0) -gt free() readers(NReaders) -gt
receive Pid, lockRead -gt
Pid ! self(),ok, readers(NReaders1)
Pid, unlockRead -gt
readers(NReaders-1) end. writer(Writer) -gt
receive Writer, unlockWrite -gt
free() end.
Free
N0
unlockWrite
lockRead/N1
lockWrite
Readers
Writer
lockRead/N
unlockRead/N--
25Reader-writer lock
- Performance
- 400 000 lock/unlock ? ???????
- ?? ??????????? ?? 20 ??????? ???????????
?????????
26?????????????? map
- pmap(Fun, List, Nodes) -gt
- SpawnFun spawner(Nodes), ????????,
???????? ??? ????????????? - Parent self(),
- ????????? ??????????? ????????? ?????
- Pids map(fun (Item) -gt
- SpawnFun(fun() -gt Parent !
self(), (catch Fun(Item)) end) - end, List),
- ???????? ??????????
- reverse(collect(Pids, ))
- collect(,Res) -gt Res
- collect(PRest, Res) -gt
- receive
- ???????? ????????? ????????? (????????
?? ??????????? ?? ???????!) - P, Val -gt collect(Rest, Val Res)
- end
- spawner() -gt fun spawn/1
- spawner(Nodes) -gt
27plists
- ?????????? ??? distributed fault-tolerant
???????? ?? ???????? - mapreduce, sort, filter, ... (????? 11)
- ????????? ?? ???????/????? ????, ???????
????????? ?? ????, ???????? - 400 ????? ????? ????????????
28IRC Lite (c)Joe Armstrong
- ??????? IRC-??????
- ????????????, ??????
- Headless server, ?? ??????? ??????? GUI
- ?????? Erlang, ??? ????????????? OTP
- 700 ????? 300 ?? ????????????, 400 ??
?????????? lib_chan
29????? ???????????
Client
Server
lib_chan
UI
C
MM
MM
G
MM
Client
S
UI
C
MM
G
Client
UI
C
MM
30??????
Client
Widget
start_widget
set up
Connector
start_connector
lib_chanconnect
Middleman
error/ sleep
disconnected
ack
connected,MM
lib_chancontrol( MM,Client)
put, OK!
login,Grp,Nick
Server
TCP
wait_auth
ok
active
31??????
MM
Widget
Client
Evil user
active
ltltrelay,Nick,Strgtgt
relay,Nick,Str
put,NickStr
EXIT,Widget
close
32??????
chan
Server
C1,login,G1,Nick1
G1
control(C,self())
C2,login,G1,Nick2
login,C2,Nick2
control(C,self())
C3,login,G2,Nick2
G2
logout,C1
control(C,self())
logout,C2
EXIT,G1,allGone
allGone
allGone
33????????? ??????????
Group
C1
Other client chans
C1
control(C1,self())
ack
Server
relay,Nick,Str
msg,Nick,C1,Str
login,C2,nick
control(C2,self())
closed
34Mnesia
- ??????????? ??, ???????? ?? ???????
- (????? 20000 ?????)
- ??????????????
- ???????????????
- ??????????????
- Fault-tolerant
35Mnesia
- ??????? ? ?????? ?/??? ?? ?????
- ???????? ?? ETS/DETS
- ????????????? ? ??????
- ??????? ?????? ?????
- ??????? list comprehensions
- ????????????? ???
- ????? ??????? ??????? ??????,
- ???????????? ?????? (???????????? ??????????????)
- ? ???????? ??????? (joinless) ????????
- ?? ??????? ???? ?????
36??????
1 ??? ?????? 1 ??????? -record(person, name,
???? ?????????? ????
data, ???????????? ??????
married_to, ?????? ??? undefined
children). ?????? ????? X
personname klacke, data male,
36, 971191, married_to eva,
children marten, maja, klara. mnesiawr
ite(X).
37??????
- ????????
- disk_copies disk_only_copies ram_copies,
?????? ????? - access_mode, read_only read_write
- index, ?????? ?????
- ???? ??????
- set ????????? ? ?????????? ??????
- ordered_set ????????????? ?????????
- ????. ?? ?????????????? ??? disk-only
- bag ????????? ? ????????????? ??????, ?? ???
?????? ?????????? - mnesiacreate_table(person,
- ram_copies, N1, N2,
- index, address,
- attributes, record_info(fields,person)).
- person ????????? ??? record(person, ...)
- record_info(fields, some_rec) ??????????
?????? ????? (reflection)
38???????
- QLC (Query List Comprehension)
- Query Handle ????????????? (????????????????)
?????? - qlcq(P.name P lt- table(person),
- length(P.children) gt X) ?????????? QH
- qlce(q) ????????? QH
- maturep(Sex, Age, Phone) when Age gt 30 -gt true
- maturep(Sex, Age, Phone) -gt false.
- qlcq(P.name P lt- table(person),
- maturep(P.data),
- length(P.children) gt X)
39???????
- ????? ?????? pattern matching
- mnesiamatch_object(person, person, '_', 36,
'_', '_', read) - (?????????? ???????, ?? ?? ???????? ???????????
??????) - mnesiaselect
- mnesiaselect(person,
- personname1, sexmale, age2, _
_, Pattern - gt, 2, 30, Guard
- 1)
Result - ???? ???????? ???????
- foldl,foldr
- traverse
- ...
40???????
- ??????????? ???????? ???????????? ? ??????????
???????. ?? ???????????. - ??? ???????????? join-??
- ???? ????? ????????? explain plan ??????????
?????? ??? ?? ???????, ??????? ????? ?????????
??????
41??????????
- divorce(Name) -gt
- F fun() -gt
- case mnesiaread(Name) of
- -gt
- mnesiaabort(no_such_person)
- Pers -gt
- Partner mnesiaread(Persperson.
married_to), - mnesiawrite(Perspersonmarried_t
o undefined), - mnesiawrite(Partnerpersonmarrie
d_to undefined) - end
- end,
- mnesiatransaction(F).
- ACID
- ?????????
- ? ???????????? ???????? ????????? (??? ?? ???
????????!) - ???? dirty ????? ? ????????? ??? ???????,
soft-realtime. - ?????, ??? ? dirty ?????? O(1) ?????? ? ??????
42Fault tolerance
- ????? ????????????????? ????? ?????
- ??? ?????? ?????? ??????????? ??????
- ? ?????? 2006 ???? ?????????????? 16??? ???????
???????
43??????????????????
- ???? ?????? id, param, value
- ?????? 2 ???? ?? 2???, 2?? RAM
- ????????/?????????
- ??????? ?? ?????
- ??????????
- ??? ??? (??????? ?????)
- 1 ?????????? ?? ??????
- 1 ?????????? ?? ???? ??????
44??????????????????
- ?? ????????
- ??????????
- ?? ?? ?????? ??????
- ??????? ? ??????
- ??????????????
45??????????????????
- ?????? 10000 ???????
- dirty 2.3?, ????? 78???
- one_tran 7-8?
- n_tran 9?, ????? 0.8c
- ?????? 10000 ???????
- dirty 0.3c, ?? ???????? overloaded
- one_tran 4c
- n_tran 1.1c
46Behaviours
- ?????????? ?????? ? ????????
-module(image). -export(behaviour_info/1). beha
viour_info(callbacks) -gt init,0,shutdown,0
,load_image,1,image_info,1,scale_image,2
behaviour_info(_Other) -gt undefined.
-module(gif). -behavior(image). -expor(init/0,shu
tdown/0,...,scale_image/2). init () -gt
... shutdown () -gt ... .. scale_image (Img,
Options) -gt ...
47OTP
- Open Telecom Platform
- ????????? ??? ?????????? ??????? ??????
- ????????????????
- ????????????
- ??????????
- ???? ?????? - ?????????? ???????
48OTP
- supervision trees
- gen_server
- gen_fsm
- gen_event
- code replacement
49Supervision trees
- ???????? ? Workers ? Supervisors
- ???-?? ???? ? ?????????? ????????????
- ???? ?????????
- permanent ?????? ?????????????
- temporary ??????? ?? ?????????????
- transient ?????? ???? ??????? ???? ???????
- ????????? ???????????
- one for one
- one for all
- rest for one
50Supervision trees
one for one
all for one
rest for one
51Supervision trees
- start_link(M,Args) ??????? ???????????
- ???????? Minit(Args), ???????? ?????????
- start_child(Svr,ChildSpec) ????????? ????
- -behavior(supervisor).
- Minit(Args) ??????????
- ????????? (????????? ????????)
- ???? ??? ?????????
52gen_server
- ??? ?????????? ???????? )
- ?????? ????? ???????? ? SupervisionTree
- ?????????? ? ?????????? ??????
- Broadcast
- ????? ????
- ??????? ????????? ????????? ????
53gen_server ??????? API
- start/start_link ????? ?????????? / ?
supervision tree - call/cast(Server,Request) ??????????/???????????
????? - multi_call/abcast(Servers,Request)
??????????/??????????? ???????????
54gen_server behaviour
- init(Args)
- handle_call(Request, From, State)
- handle_cast(Request, State)
- handle_info(Info, State)
- terminate(Reason, State)
- code_change(OldVsn, State, Extra) ? NewState
- ??? upgrade/downgrade ????, ???????????????
?????????
55gen_event
- ?????????? ? ????????? ???????
- ????????? ???????
- ?????????
- ??????????
- ??????????/????????/????? ???????????? (callback
module) - ????? ????
- ?????????? (supervision) ?? ?????? ??????????? ?
???????????
56gen_event ??????? API
- start/start_link(Name?) ????? ???/??????
supervision tree, ????????/????????? - add_sup_handler(Mgr, Handler, InitArgs)
???????? ??????-?????????? ????????? ?? ?????? - ?????????? ??????????????? ???????, ?????
???????/???????? ??????????/???????? ? ?.?. - notify/sync_notify(Mgr, Event) ??????? ???????
- call(Mgr, Handler, Request) ???????????? ??????
- delete_handler(Mgr, Handler, TermArgs) ???????
- swap_sup_handler(Mgr, Handler1,TA,
Handler2,IA) ????????
57gen_event behaviour
- handle_event(Event)
- handle_call/info
- init/terminate
- code_change
58gen_fsm API
- start_link
- sync_send_all_state_event
- sync ????????? ?????????
- all_state ?????????? ??????? (????????
??????????? callback) - start_timer(Time,Msg) ? Ref
- cancel_timer(Ref)
59gen_fsm behaviour
- init,handle_info,terminate,code_change
- StateName(Event, StateData) ? Result
- ????????? ????????? Data
- ????? (???? sync)
- ????????
- hibernate?
- stop?
- handle_event(Evt,StateName,StateData)
- ??? send_all_state_event
60code replacement
- OTP-?????????? SASL
- Online upgrade/downgrade
- ?????????? ? ????? .appup
-
- Vsn,
- UpFromVsn, UpFromScript, ...,
- DownToVsn, DownToScript, ....
61code replacement
- ???????
- ???????? ?????? ?????? ? ????????????
- load_module, the_mod, lib_1, lib_2
- ????????? ??????
- update, the_mod, advanced, Extra
- the_modcode_change(Vsn, State, Extra)
- ???????????? ???
- apply, the_mod, do_this, with_that
- ...
- ??????
- ?????????????? ????? ??????
- ??????????? ??????
62Guidelines
- 1 ??????? 1 ?????? 1 ??????? ????????????
???????? - ?????????????? ?????? ????????????
- ???????? ????-????????
- ???????? ?????? ??????? ?????????
- ????????????????? ???
- ??????????????? ??? ???????????? ???????? ? ??
?????????
63??????????
- Ericsson ????????????????
- Ejabberd ???? ?? ????? ??????????
???????-???????? - Copypaste ?? FAQ
- Bluetail/Alteon/Nortel (distributed, fault
tolerant email system, SSL accelerator) - Cellpoint (Location-based Mobile Services)
- Corelatus (SS7 monitoring).
- Finnish Meteorological Institute (Data
acquisition and real-time monitoring) - IDT corp. (Real-time least-cost routing expert
systems) - Kreditor (Electronic payment systems)
- Mobilearts (GSM and UMTS services)
- Netkit Solutions (Network Equipment Monitoring
Operations Support Systems) - Schlund Partner (Messaging and Interactive
Voice Response services) - Quviq (Software Test Tool)
- T-Mobile (previously one2one) (advanced call
control services) - Telia (a telecomms operator)
- Vail Systems (Interactive Voice Response systems)
- Wavenet (SS7 and IVR applications)
64??????
- ????
- http//www.erlang.org
- http//www.scribd.com/doc/125015/07erlangintro -
???????? ???????? ? ???? - ??????, wiki
- http//www.trapexit.org/
- ??????????
- http//jungerl.sourceforge.net/ - Erlang Jungle,
???? ????????? - http//cean.process-one.net/ CEAN (Comprehensive
Erlang Archive Network) - http//www.erlang.org/user.html - User
contributions - http//code.google.com/p/plists - ??????????
plists - Mnesia
- http//www.infoq.com/news/2007/08/mnesia
- http//www.erlang.org/doc/man/mnesia.html
- http//static.scribd.com/docs/c0e904c7997g8.pdf -
??????? ?????? ?? mnesia - ??????????