This document discusses an Erlang coding dojo focused on the "Grep" kata using Erlang clusters. It describes joining an Erlang cluster, sending the grep module and files to peers, workers receiving and processing the files in parallel, and aggregating the results. It also discusses using OTP behaviors like gen_server and supervisor for the worker workflow.
1 of 27
More Related Content
Coding Dojo: Erlang
1. Coding Dojo:
Erlang
Arkadi.Shishlov@gmail.com
Vladimir Tarasow netrat@netrat.eu
2. Today's tools
Erlang/OTP
EUnit
Rebar
IntelliJ IDEA 11 CE + Erlang plugin
ERL shell
MochiWeb's reloader.erl
https://github.com/mochi/mochiweb/blob/master/src/reloader.erl
erldocs.com
3. Some other day tools
Agner
Sublime Text 2 + SublimErl
Eclipse + ErlIDE
Emacs
Elixir
4. Erlang in nutshell
Functional language
Unusual syntax
Forces into concurrent
programming paradigm
Robust VM
Patterns for scalable services
OTP
6. Kata: Grep
1. Local file-system directory and its
subdirectories contains large
number of text files
2. Extract unobfuscated e-mail
addresses; together with people
names, if possible
3. Write the list of unique e-mails into
specified output file
8. What is the Coding Dojo?
Coding Dojo is a meeting where a
bunch of coders get together to work
on a programming challenge.
They are there to have fun and to
engage in Deliberate Practice in
order to improve their skills.
9. Roles
Sensei
Moderator
Pair:
Pilot
Navigator
Audience
10. Sensei
Responsible for:
No long discussions without the code.
Working code by the end of the iteration.
Tests are written during the iteration.
Green tests by the end of the iteration,
otherwise the next iteration begins by
making tests green.
Progress on fulfilling the Kata.
12. Pair
Pair of two developers working and talking at
the PC in order to complete the Kata.
Pair consists of the pilot and the navigator.
The pilot codes and the navigator helps the
pilot to understand what has been done in
the previous iterations and what the pair
should do now.
13. Principles
Working on Kata
At the Dojo one can't discuss a form
without code, and one can't show
code without tests.
14. Principles
Timeboxing
Pair works during a fixed period of time.
Switching
The person who enters into the coding
timebox begins to act as a pilot. The person
who was the pilot during the previous
iteration switches to the position of the
navigator.
15. Respect
Ringtones of mobile phones are off. A
person can answer a call, but he or she
must leave the room to do it.
Only the pair at the PC can talk loud.
Sensei can interfere and talk loud by his or
her own decision.
16. Respect
All discussions away from the PC must be
made in the way not to spoil the pairs
deep diving into the Kata.
If a person wants to ask or suggest smth
to the pair, he or she should raise the
hand and wait for the moderator for
permit.
Only one person from the observers can
talk at a time.
18. Working agreement
? Randori, Parisian, Pairs ?
Applicability of unit and functional
tests
Randori round time
Switching: round-robin, by request
19. Kata: Grep - Erlang cluster
1. Join Erlang cluster
2. Send grep module to peers and
start grep worker there
3. Send messages with files content
4. Receive list of e-mail
5. Aggregate results
20. Join Erlang cluster
1. Ping LAN bootstrap nodes
- bootstrap@extensa
- bootstrap@hal9011
2. Workes are:
+ nodes()
- worker if name starts with "worker"
*except coordinator local VM [node()] to
simplify programming and prevent
aggregator bottlenecks
21. Send code to peers
1. The functionality is implemented by
module 'grep'
2. 'grep' is also an (Erlang) application
3. use code:get_object/1 to read the module
beam
4. use rpc:(multi)call/4, code:load_binary/1,
erlang:spawn/4 to load beam and start
application remotely
22. Send work
1. API signature:
grep:grep(Dir) ->
[ email1@domain, email2@domain2 ]
2. Obtain list of workers by calling nodes/0
3. Scan directory recursively and create a
tuple:
Work = { self(), Content }
4. Send message to next peer:
{ grep_worker, Node } ! { self(), Work }
23. Receive result
1. After all work units are sent - receive all
replies or timeout occur:
recv(N) -> c:flush(), recv([], N).
recv(A, 0) -> A.
recv(A, N) ->
receive
{emails, Mails} -> recv([Mails | A], N-1)
after 1000 -> A
end.
2. Flatten, sort, uniq results
24. Worker workflow with OTP
1. Use OTP gen_server and supervisor
behaviour-s with simple_one_for_one
strategy
2. application:start/1 calls grep:init/1
3. grep:init/1 spawns a number of workers
that is equal to core count as returned by
erlang:system_info(schedulers)
4. grep:init/1 registers all workers with global:
register_name/2
26. Latvian Developers Network
http://www.ldn.lv
http://fb.me/LatvianDevelopersNetwork
@LVDevNet
Arkadi Shislov
arkadi.shishlov@gmail.com
http://bitbucket.org/arkadi
Vladimir Tarasow
http://about.me/NetRat
27. This work is licensed under the Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/