forked from celluloid/dcell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Refactor the Registry API (celluloid#49)
- Loading branch information
Showing
6 changed files
with
77 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
module DCell | ||
# Directory of nodes connected to the DCell cluster | ||
# | ||
# WARNING: This is an internal API and kept for backwards-compatibility. | ||
# It may be removed at some point. | ||
module Directory | ||
extend self | ||
|
||
# Get the URL for a particular Node ID | ||
def get(node_id) | ||
DCell.registry.get_node node_id | ||
DCell.registry.get(:nodes, node_id) | ||
end | ||
alias_method :[], :get | ||
|
||
# Set the address of a particular Node ID | ||
def set(node_id, addr) | ||
DCell.registry.set_node node_id, addr | ||
DCell.registry.set(:nodes, node_id, addr) | ||
end | ||
alias_method :[]=, :set | ||
|
||
# List all of the node IDs in the directory | ||
def all | ||
DCell.registry.nodes | ||
DCell.registry.keys(:nodes) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class DCell::Registry::Base | ||
include Celluloid | ||
|
||
def get(namespace, key) | ||
abort NotImplementedError.new | ||
end | ||
alias_method :[], :get | ||
|
||
def set(namespace, key, value) | ||
abort NotImplementedError.new | ||
end | ||
alias_method :[]=, :set | ||
|
||
def delete(namespace, key) | ||
abort NotImplementedError.new | ||
end | ||
|
||
def keys(namespace) | ||
abort NotImplementedError.new | ||
end | ||
|
||
def clear(namespace) | ||
abort NotImplementedError.new | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,44 @@ | ||
require 'celluloid/redis' | ||
require 'redis/connection/celluloid' | ||
require 'redis-namespace' | ||
require 'dcell/registry' | ||
require 'dcell/registries/base' | ||
|
||
module DCell | ||
module Registry | ||
class RedisAdapter | ||
class RedisAdapter < Registry::Base | ||
def initialize(options) | ||
# Convert all options to symbols :/ | ||
options = options.inject({}) { |h,(k,v)| h[k.to_sym] = v; h } | ||
|
||
@env = options[:env] || 'production' | ||
@namespace = options[:namespace] || "dcell_#{@env}" | ||
|
||
redis = Redis.new options | ||
@redis = Redis::Namespace.new @namespace, :redis => redis | ||
|
||
@node_registry = NodeRegistry.new(@redis) | ||
@global_registry = GlobalRegistry.new(@redis) | ||
redis = ::Redis.new options | ||
@redis = ::Redis::Namespace.new @namespace, :redis => redis | ||
end | ||
|
||
def clear_nodes | ||
@node_registry.clear | ||
def get(namespace, key) | ||
string = @redis.hget(namespace, key.to_s) | ||
Marshal.load(string) if string | ||
end | ||
|
||
def clear_globals | ||
@global_registry.clear | ||
def set(namespace, key, value) | ||
string = Marshal.dump(value) | ||
@redis.hset(namespace, key.to_s, string) | ||
end | ||
|
||
class NodeRegistry | ||
def initialize(redis) | ||
@redis = redis | ||
end | ||
|
||
def get(node_id) | ||
@redis.hget 'nodes', node_id | ||
end | ||
|
||
def set(node_id, addr) | ||
@redis.hset 'nodes', node_id, addr | ||
end | ||
|
||
def nodes | ||
@redis.hkeys 'nodes' | ||
end | ||
|
||
def clear | ||
@redis.del 'nodes' | ||
end | ||
def delete(namespace, key) | ||
@redis.hdel(namespace, key.to_s) | ||
end | ||
|
||
def get_node(node_id); @node_registry.get(node_id) end | ||
def set_node(node_id, addr); @node_registry.set(node_id, addr) end | ||
def nodes; @node_registry.nodes end | ||
|
||
class GlobalRegistry | ||
def initialize(redis) | ||
@redis = redis | ||
end | ||
|
||
def get(key) | ||
string = @redis.hget 'globals', key.to_s | ||
Marshal.load string if string | ||
end | ||
|
||
# Set a global value | ||
def set(key, value) | ||
string = Marshal.dump value | ||
@redis.hset 'globals', key.to_s, string | ||
end | ||
|
||
# The keys to all globals in the system | ||
def global_keys | ||
@redis.hkeys 'globals' | ||
end | ||
|
||
def clear | ||
@redis.del 'globals' | ||
end | ||
def keys(namespace) | ||
@redis.hkeys(namespace) | ||
end | ||
|
||
def get_global(key); @global_registry.get(key) end | ||
def set_global(key, value); @global_registry.set(key, value) end | ||
def global_keys; @global_registry.global_keys end | ||
def clear(namespace) | ||
@redis.del(namespace) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module DCell::Registry | ||
end |
b51d622
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if the marshalling is too much overhead.
b51d622
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Neonlex I think it isn't,
Marshal
is quite fast. Do you have an application that you can profile?b51d622
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aflatter I could try it but right now I don't have much time. I only wrote this benchmark https://gist.github.com/neonlex/5974704 the result was:
I have to think a bit about this and I will try to run this over the weekend on my application.
b51d622
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Neonlex Yes, please benchmark in the context of a DCell application.