-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnode.rb
More file actions
164 lines (145 loc) · 3.99 KB
/
node.rb
File metadata and controls
164 lines (145 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require 'json'
require 'zmq'
require './hash'
require './vclock'
require './threads'
require './config'
require './services'
require './coordinator'
# an object to store in a server node
class NodeObject
attr :value, :vclock
def initialize(value, vclock)
@value = value
@vclock = vclock
end
def <=>(nobject2)
vclock <=> nobject2.vclock
end
def to_s
{:value=>value, :vclock=>vclock}.to_json
end
def self.deserialize(serialized)
data = JSON.parse(serialized)
if Array === data
data.map{|json|
vclock = VectorClock.deserialize(json['vclock'])
NodeObject.new(json['value'], vclock)
}
else
vclock = VectorClock.deserialize(data['vclock'])
NodeObject.new(data['value'], vclock)
end
end
end
# Manages a hash ring as well as a hash of data
class Node
include Threads
include Configuration
include Services
include Coordinator
def initialize(name, nodes=[], partitions=32)
@name = name
@ring = PartitionedConsistentHash.new(nodes+[name], partitions)
@data = {}
end
def start(leader)
coordination_services( leader )
service( config("port") )
puts "#{@name} started"
join_threads()
end
def stop
inform_coordinator( 'down', config("coord_req") )
ensure
exit!
end
def put(socket, payload)
n, key, vc, value = payload.split(' ', 4)
socket.send( do_put(key, vc, value, n.to_i).to_s )
end
def get(socket, payload)
n, key = payload.split(' ', 2)
socket.send( do_get(key, n.to_i).to_s )
end
def do_put(key, vc, value, n=1)
# 0 means insert locally
if @ring.pref_list(key, n).include?(@name) || n == 0
vclock = VectorClock.deserialize(vc)
node_objects = []
if current_obj = @data[@ring.hash(key)]
if vclock.empty? && current_obj.length == 1
# if no clock was given, use the old one
vclock = current_obj.first.vclock
elsif !vclock.empty? && !current_obj.find{|o| o.vclock.descends_from?(vclock)}
# not a decendant of anything, ie conflict. add as a sibling
node_objects += current_obj
end
end
vclock.increment(@name)
node_objects += [NodeObject.new(value, vclock)]
puts "put #{n} #{key} #{vclock} #{value}"
@data[@ring.hash(key)] = node_objects
replicate("put 0 #{key} #{vclock} #{value}", key, n) unless n == 0
node_objects
else
remote_call(@ring.node(key), "put #{n} #{key} #{vc} #{value}")
end
end
def do_get(key, n=1)
if n == 0
puts "get 0 #{key}"
@data[@ring.hash(key)] || 'null'
# rather than checking if this is the correct node,
# check that this node is in the pref list.
# if not, pass it through to the proper node
elsif @ring.pref_list(key, n).include?(@name)
puts "get #{n} #{key}"
results = []
if n != 0
results = replicate("get 0 #{key}", key, n)
results.map! do |r|
r == 'null' ? nil : NodeObject.deserialize(r)
end
end
results << @data[@ring.hash(key)]
results.flatten!
results.uniq! {|o| o && o.value }
results.compact!
begin
# we get the most recent value if we can resolve this
[results.sort.first]
rescue
# a conflic forces sublings
puts "Conflict!"
return results
end
else
results = remote_call(@ring.node(key), "get #{n} #{key}")
NodeObject.deserialize(results)
end
end
def replicate(message, key, n)
list = @ring.pref_list(key, n)
list -= [@name]
results = []
while replicate_node = list.shift
results << remote_call(replicate_node, message)
end
results
end
def remote_call(remote, message)
puts "#{remote} <= #{message}"
req = connect(ZMQ::REQ, config("port",remote), config("ip",remote))
resp = req.send(message) && req.recv
req.close
resp
end
end
begin
name, leader = ARGV
leader = !leader.nil?
$node = Node.new(name)
trap("SIGINT") { $node.stop }
$node.start(leader)
end