• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
DRb at the Ruby Drink-up of Sophia, December 2011

DRb at the Ruby Drink-up of Sophia, December 2011



Presented at the Ruby Drink-up of Sophia Antipolis on the 6th of December 2011 by Nicolas Bondoux (@nicolas_bondoux).

Presented at the Ruby Drink-up of Sophia Antipolis on the 6th of December 2011 by Nicolas Bondoux (@nicolas_bondoux).



Total Views
Views on SlideShare
Embed Views



2 Embeds 56

http://rubylive.fr 55
http://news.humancoders.com 1



Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

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

    DRb at the Ruby Drink-up of Sophia, December 2011 DRb at the Ruby Drink-up of Sophia, December 2011 Presentation Transcript

    • DRb: an easy distributed objects framework for Ruby Ruby Drink-up of Sophia-Antipolis Nicolas Bondoux 06/12/2011
    • Quick Presentation (1)
      • Allows do share ruby objects between remote processes
      • Transparent usage
      • No IDL; no need to share class definitions
      • DRb main classes are in standard library
    • Quick Presentation (2)
      • A light library implemented in ruby
      • Implemented network layers tcp, unix sockets, tcp+ssl
    • Quick example (1)
      • On Server side:
      require 'drb' MyURI = "druby://" class SimpleServer include DRb::DRbUndumped def initialize @i = 0 end def helloWorld puts "Hello World" @i += 1 return @i end def broken puts i end end # start a server with a new SimpleServer as front object DRb.start_service(MyURI, SimpleServer.new) # wait for the server thread to suicide DRb.thread.join
    • Quick example (2)
      • On client side
      r equire 'drb' MyURI = &quot;druby://; server = DRb::DRbObject.new(nil,MyURI) puts server.helloWorld # display 1 # server puts “Hello World” puts server.broken #NameError: undefined local variable or method `i' for #<SimpleServer:0x7ffe9814af20 @i=4> # from (druby:// (irb):40:in `broken' # from (irb):24 # from :0
    • DRb classes: DRbObject
      • The fundamental type in Drb
      • Acts as a proxy for a remote object
      • Concretely, its fields are:
        • @uri: address of the DRbServer providing the object
        • @ref: id of the object within the remote process
      • Forwards method calls to the remote object
    • The URIs
      • “druby://” -> tcp uri
      • “drbunix:/tmp/toto” -> unix socket
    • DRb classes: DRbServer
      • Any process publishing a local must have a DrbServer thread running
        • Launched with DRb.start_service
      • A front object may be associated to a DrbServer (act as default object when building a DRbObject from the URI)
      • DRbObject.new (obj, nil) will holds the ref of obj as well as the URI of current server (Drb.uri)
    • DRbObject: method calls
      • parameters and return value transmission:
        • Either can be copied between process
          • (both processes need then to know their definitions)
        • Or can have a DrbObject referencing them copied instead
          • These objects will then be shared between processes
      • exceptions are propagated the same way than with local calls
    • DrbObject example # server side require 'drb' MyURI = &quot;druby://; $s = &quot;Toto&quot; class SimpleServer include DRb::DRbUndumped def s_DRbObject return DRbObject.new($s) end def s return $s end def s_clone return String.new($s) end end DRb.start_service(MyURI, SimpleServer.new) #on client side: require 'drb' MyURI = &quot;druby://; server = DRb::DRbObject.new(nil,MyURI) s = server.s puts s puts s.class #String s.concat(&quot; updated!&quot;) puts server.s_clone # display &quot;Toto&quot; s = server.s_DRbObject # DRb::DRbObject s.concat(&quot; updated !&quot;) puts server.s_clone # display &quot;Toto updated !&quot;
    • Undumped objects
      • By default, methods calls copy their parameters without conversion to DRbObject
      • Objects that cannot be marshalled are always passed as DRbObjects: Proc, IO, singletons, or any object referencing one of them
      • Objects that include DRb::DRbUndumped are always passed as DRbObject
    • DRbUndumped example
      • Continuing previous example:
      # server side $s = &quot;Titi&quot; $s.extend DRbUndumped #on client side: s = server.s puts s.class # DRb::DRbObject s.concat(&quot; updated !&quot;) puts server.s_clone # display &quot;Titi updated !&quot;
    • Complex example with a Proc # server require 'drb' MyURI = &quot;druby://; class Container def initialize @t = [] end def push o @t.push o end def each(&block) @t.each(&block) end end s = Container.new DRb.start_service(MyURI, s,{:safe_level => 1}) # client require 'drb' MyURI = &quot;druby://; class Box include DRbUndumped attr_accessor :v def initialize v @v = v end end # needed to share local objects with the server: DRb.start_service s = DRbObject.new(nil, MyURI) a = Box.new(1) b = Box.new(2) s.push a s.push b s.each{ |i| puts i.class i.v = (i.v * 2) } # Box # Box # => block is called in local # => its parameters are converted back to local objects puts a.v # 2 puts b.v # 4
    • Local DRbObjects
      • Methods call on DRbObject referencing a local instance are just normal method calls
      • Unmarshaller converts a DrbObject on local object as a ref on local object
    • Things good to know
      • Beware garbage collection! An object won't be kept alive because a DRbObject points on it.
      • A server creates a thread by client connection
        • -> do not forget your mutexes!
      • Security:
        • ACL system to control authorized clients
        • $SAFE variable should be set to 1 to prevent remote eval (can be set as parameter of the DRbServer)
    • IdConv
      • Drb allows to choose the mapping engine between id and objects.
      • Three implementations provided:
        • DRb::DRbIdConv
          • Basic object space conversion
        • Drb::TimerIdConv
          • Prevent GC of an object for a certain amount of time after the last access
        • Drb::GWIdConv
          • The server act as a gateway for remote objects
    • Any Question ?