• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Combining the strength of erlang and Ruby
 

Combining the strength of erlang and Ruby

on

  • 2,295 views

Use erlang for state and concurrency control while implementing business logic rapidly with Ruby's unique ability to have DSL like syntax.

Use erlang for state and concurrency control while implementing business logic rapidly with Ruby's unique ability to have DSL like syntax.

Statistics

Views

Total Views
2,295
Views on SlideShare
2,242
Embed Views
53

Actions

Likes
6
Downloads
20
Comments
0

3 Embeds 53

http://coderwall.com 49
http://us-w1.rockmelt.com 2
https://twitter.com 2

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Combining the strength of erlang and Ruby Combining the strength of erlang and Ruby Presentation Transcript

    • COMBINING  THE  STRENGTHS  OF   ERLANG  AND  RUBY erlounge  Berlin  February  2012  –  Mar4n  Rehfeld
    • Game  Server  for  Upcoming  Wooga  GameWhat  is  a  game  server?• provide  HTTP  API  to  actual  game  („client“)• validate  API  calls  against  current  state  &  game  logic • API  calls  will  modify  the  game  state• make  game  state  persistent
    • Game  Server  for  Upcoming  Wooga  GameIt  will  be  a  stateful  game  server• one  process  per  ac4ve  user  gaming  session • the  process  holds  the  current  state  and  is  the  only  one  that  can  modify  it   (strong  encapsula:on) • the  process  handles  all  API  calls  for  the  given  user  one  a=er  the  other   (concurrency  control  through  actor  model) • the  process  loads  the  game  state  from  storage  and  writes  it  back   periodically  and  on  process  termina:on  (:meout  =  user  stopped  playing)
    • Game  Server  for  Upcoming  Wooga  GameDetails  on  the  basic  idea:Awesome  presenta4on  on  theMagic  Land  game  server  by@knu4n  &  @hungryblankhZp://www.slideshare.net/wooga/from-­‐0-­‐to-­‐1000000-­‐daily-­‐users-­‐with-­‐erlang
    • Our  Goals• Get  most  of  the  benefits  from  wooga’s  pure-­‐erlang  game   server  (Magic  Land) • especially  func:onal  approach  for  game  state   encapsula:on  and  transforma:on  +  concurrency  control• But:  Keep  Object-­‐Oriented  approach  for  modelling  the   game  logic
    • Expected  OO-­‐Benefits• Rapid  development• concise  &  expressive  syntax• leverage  exis4ng  know  how• but:  keep  the  OO  part  stateless  and  side-­‐effect  free to  avoid  the  usual  traps  &  pidalls
    • Target  Architecture Authority for: Load Balancer "What app server is responsible for current session?" App Server Node App Server Node App Server Node Erlang VM Erlang VM Erlang VM ... Ruby Ruby Ruby Ruby Ruby Ruby Ruby Ruby Ruby ... ... ... Worker Worker Worker Worker Worker Worker Worker Worker Worker Shared Storage for game state snapshots and for storing cold game sessions
    • Example  Game  Ac4on  in  Ruby URLgame_action /:actor/fruit_tree/self/shake, :affects => [:fruit_trees, :user] do |response| x, y = params[:x], params[:y] fruit_trees[x, y].shake affected  parts  ofend the  game  state• DSL-­‐like  defini4on  of  game  ac4on• skinny  as  controllers  should  be  8-­‐)
    • Example  Model  in  Ruby Inheritanceclass FruitTree < Tree property :last_shake_time, :type => Integer, :default => 0 DSL-­‐like  defini4on property :collectable_fruit_count, :type => Integer, :default => 0 of  persistent  state def shake raise Error::Validation, "FruitTree at (#{x}, #{y}) has no fruit" unless carries_fruit? session.user.xp += 1 session.user.energy -= 1 self.last_shake_time = game_time self.collectable_fruit_count = config.fruit_count end # ...end• easily  unit  testable• minimal  amount  of  code
    • erlang  talking  to  RubySome  op4ons• erlectricity  hZps://github.com/mojombo/erlectricity: Can  talk  erlang  binary  protocol  to  Ruby  processes  through  erlang   ports• ernie  hZps://github.com/mojombo/ernie: Remote  func4on  call  using  the  BERT-­‐RPC  protocol  to  either  Ruby  or   na4ve  erlang  processes• ZeroMQ  <hZp://www.zeromq.org/>: awesome  brokerless  queue  transport  layer,  connects  30+  languages
    • ZeroMQPro Con• loose  coupling  of  erlang  and   • yet  another  layer Ruby  through  queues • easily  deploy  new  Ruby   code  without  touching   erlang• allows  flexible  transports,   even  accross  machines
    • Connec4ng  the  Dots• use  Mongrel2  hZp://mongrel2.org/  protocol  &  ZeroMQ  setup• erlang:  emongrel2  hZps://github.com/hungryblank/emongrel2• Ruby • rack-­‐mongrel2  fork  hEps://github.com/khiltd/khi-­‐rack-­‐mongrel2 • rack  protocol  hEp://rack.rubyforge.org • Sinatra  hEp://www.sinatrarb.com/➡ essen4ally  we  are  speaking  HTTP  over  ZeroMQ and  can  hook  up  any  Rack-­‐based  Ruby  web  framework
    • Setup  Overview server worker session worker sender Push/Pull  Queue 1:n worker session worker ... receiver Pub/Sub  Queue m:n ... session worker
    • How  does  the  game  state  look  like?• Has  many  parts,  each  part  has  a  name  (e.g.  fruit_trees)  and  some   content • this  is  a  performance  op:miza:on,  so  that  we  dont  need  to  send  the   complete  state  back  and  forth  for  every  call• erlang  does  not  care  about  the  content  (binary  data)• actually  the  content  contains  serialized  objects  (e.g.  a   MapObjectCollection  containing  FruitTree  members)• erlang  does  need  to  know,  what  game  ac4on  needs  what  state  parts
    • Looking  back  at  the  Game  Ac4ongame_action /:actor/fruit_tree/self/shake, :affects => [:fruit_trees, :user] do |response| x, y = params[:x], params[:y] fruit_trees[x, y].shake affected  parts  ofend the  game  state• Ruby  knows  the  mapping  of  game  ac4ons  to  affected  state  parts• pushes  the  mapping  on  startup  &  makes  it  available  on  request
    • Aside:  Efficient  Game  State  Access• considered  different  serializa4on  formats  (e.g.  JSON,   MessagePack)• especially  for  large  collec4ons  of  objects  we  wanted  to  avoid   parsing  everything  to  extract  a  single  object• chose  TNetstrings  hZp://tnetstrings.org/  as  a  serializa4on   format  (length  prefix  helps  searching/lazy  parsing)• built  a  lazy  parser/encoder  for  it    in  C  for  speed  :-­‐) hZps://github.com/wooga/lazy_tnetstring
    • Q  &  AMar4n  Rehfeld @klickmich