Skip to content

Commit

Permalink
node-manager: limit number of reguests to the registry during single …
Browse files Browse the repository at this point in the history
…node lookup

avoids useless transactions for already registered nodes
  • Loading branch information
niamster committed Mar 8, 2015
1 parent ff0da38 commit 1701294
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
4 changes: 3 additions & 1 deletion lib/dcell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def setup(options = {})
# Returns actors from multiple nodes
def find(actor)
actors = Array.new
Directory.each do |node|
Directory.each do |id|
node = Directory[id]
next unless node
next if node.id == DCell.id
next unless node.actors.include? actor
begin
Expand Down
4 changes: 1 addition & 3 deletions lib/dcell/directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def find(id)
# Iterates over all registered nodes
def each
DCell.registry.nodes.each do |id|
node = Directory[id]
next unless node
yield node
yield id
end
end

Expand Down
19 changes: 9 additions & 10 deletions lib/dcell/node_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ class NodeCache

class << self
# Finds a node by its node ID and adds to the cache
def register(id, addr)
def register(id)
return DCell.me if id == DCell.id
return nil unless addr
loop do
begin
node = nil
return @nodes.register(id) do
node = Node.new(id, addr) rescue nil
ninfo = Directory[id]
if ninfo and ninfo.alive? and ninfo.address
node = Node.new(id, ninfo.address) rescue nil
end
end
rescue ResourceManagerConflict => e
# :nocov:
Expand All @@ -38,19 +40,16 @@ def delete(id)
module NodeManager
# Iterate across all available nodes
def each
Directory.each do |node|
# skip dead nodes and nodes w/o an address, those might not be operational yet
next unless node.alive? and node.address
remote = NodeCache.register node.id, node.address
Directory.each do |id|
remote = NodeCache.register id
next unless remote
yield remote
end
end

# Find a node by its node ID
def find(id)
node = Directory[id]
return nil unless node and node.alive? and node.address
NodeCache.register id, node.address
NodeCache.register id
end
alias_method :[], :find
end
Expand Down
4 changes: 2 additions & 2 deletions spec/dcell/directory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
it "presents all stored addresses" do
DCell::Directory["foo"].address = "tcp://fooaddress"
DCell::Directory["bar"].address = "tcp://baraddress"
DCell::Directory.map{|node| node.id}.should include("foo")
DCell::Directory.map{|node| node.id}.should include("bar")
DCell::Directory.to_a.should include("foo")
DCell::Directory.to_a.should include("bar")
end

it "clears node addresses" do
Expand Down

0 comments on commit 1701294

Please sign in to comment.