RingServer and RingFinger: finding services with Rinda

Rinda's ring lets processes find a shared tuple space by UDP broadcast instead of a hard-coded URI, then register and look up services in it.

Updated

Rinda's ring classes solve the "what is the URI?" problem. A Rinda::RingServer answers UDP broadcasts on the local network and hands out a reference to a tuple space. A Rinda::RingFinger sends that broadcast and returns the tuple space of the first server that answers. Services then advertise themselves by writing tuples into that space, and clients find them by reading.

The ring server

# ring.rb
require 'drb/drb'
require 'rinda/ring'
require 'rinda/tuplespace'

DRb.start_service
Rinda::RingServer.new(Rinda::TupleSpace.new)
DRb.thread.join

Rinda::RingServer.new takes the tuple space to hand out and starts listening for lookups. By default it uses UDP port 7647, which is the value of Rinda::Ring_PORT. Run one of these on the network.

Advertising a service

Services are registered as tuples of the form [:name, ServiceClass, reference, description]. Rinda::RingProvider writes that tuple for you and keeps renewing it for as long as the process runs.

# clock_service.rb
require 'drb/drb'
require 'rinda/ring'

class Clock
  include DRb::DRbUndumped

  def now
    Time.now
  end
end

DRb.start_service

provider = Rinda::RingProvider.new(:Clock, Clock.new, 'A clock')
provider.provide

DRb.thread.join

Clock includes DRb::DRbUndumped so the tuple holds a reference to the live object, not a copy of it. See pass by value and pass by reference.

Finding and using it

# client.rb
require 'drb/drb'
require 'rinda/ring'

DRb.start_service

ring = Rinda::RingFinger.primary
_, _, clock, description = ring.read([:name, :Clock, nil, nil])

puts description
puts clock.now

Rinda::RingFinger.primary broadcasts, waits for an answer and returns the ring server's tuple space as a remote object. The read then waits until a :Clock service has been registered and returns its tuple. The third element is the DRbObject to call.

No URI appears in any of the three scripts. Each process starts dRuby on whatever port it is given and the ring connects them.

Things to know

  • It is local-network discovery. A UDP broadcast normally stays within one network segment, so the ring finds servers nearby, not across the internet.
  • If no ring server answers, the lookup fails. Rinda::RingFinger.primary raises an error after its timeout, so start the ring server first.
  • Broadcasts do not always arrive, even on one machine. Firewalls and some network setups drop them, and then every lookup fails as above. To test on a single machine you can skip the broadcast and name the address: Rinda::RingFinger.new(['127.0.0.1']).lookup_ring_any returns the ring server's tuple space.
  • Registrations expire. RingProvider writes its tuple with a limited lifetime and renews it, so a service that has stopped drops out of the space after a while instead of staying listed forever.
  • Anyone on the segment can answer or register. Discovery by broadcast assumes every machine that can hear it is trusted. On a network where that is not true, use fixed URIs and the measures on the safety pages.

Sharing a tuple space for work

The ring server's own tuple space is meant for registrations. For a job queue, create a second Rinda::TupleSpace, register it with Rinda::RingProvider.new(:TupleSpace, space, 'Job space'), and have workers look it up the same way. Wrap what they get back in Rinda::TupleSpaceProxy, as shown on TupleSpace: write, take and read.