Skip to content

Commit 9323300

Browse files
committed
synapse etcd service watcher
1 parent 01a2409 commit 9323300

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

lib/synapse/service_watcher.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
module Synapse
55
class ServiceWatcher
6+
67
# the method which actually dispatches watcher creation requests
78
def self.create(name, opts, synapse)
89
opts['name'] = name
@@ -24,3 +25,4 @@ def self.create(name, opts, synapse)
2425
end
2526
end
2627
end
28+
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
require "synapse/service_watcher/base"
2+
3+
require 'etcd'
4+
5+
module Synapse
6+
class EtcdWatcher < BaseWatcher
7+
NUMBERS_RE = /^\d+$/
8+
9+
def start
10+
etcd_hosts = @discovery['host']
11+
12+
log.info "synapse: starting etcd watcher #{@name} @ host: #{@discovery['host']}, path: #{@discovery['path']}"
13+
@should_exit = false
14+
@etcd = ::Etcd.client(:host => @discovery['host'], :port => @discovery['port'])
15+
16+
# call the callback to bootstrap the process
17+
discover
18+
@synapse.reconfigure!
19+
@watcher = Thread.new do
20+
watch
21+
end
22+
end
23+
24+
def stop
25+
log.warn "synapse: etcd watcher exiting"
26+
27+
@should_exit = true
28+
@etcd = nil
29+
30+
log.info "synapse: etcd watcher cleaned up successfully"
31+
end
32+
33+
def ping?
34+
@etcd.leader
35+
end
36+
37+
private
38+
def validate_discovery_opts
39+
raise ArgumentError, "invalid discovery method #{@discovery['method']}" \
40+
unless @discovery['method'] == 'etcd'
41+
raise ArgumentError, "missing or invalid etcd host for service #{@name}" \
42+
unless @discovery['host']
43+
raise ArgumentError, "missing or invalid etcd port for service #{@name}" \
44+
unless @discovery['port']
45+
raise ArgumentError, "invalid etcd path for service #{@name}" \
46+
unless @discovery['path']
47+
end
48+
49+
# helper method that ensures that the discovery path exists
50+
def create(path)
51+
log.debug "synapse: creating etcd path: #{path}"
52+
@etcd.create(path, dir: true)
53+
end
54+
55+
def each_node(node)
56+
begin
57+
host, port, name = deserialize_service_instance(node.value)
58+
rescue StandardError => e
59+
log.error "synapse: invalid data in etcd node #{node.inspect} at #{@discovery['path']}: #{e} DATA #{node.value}"
60+
nil
61+
else
62+
server_port = @server_port_override ? @server_port_override : port
63+
64+
# find the numberic id in the node name; used for leader elections if enabled
65+
numeric_id = node.key.split('/').last
66+
numeric_id = NUMBERS_RE =~ numeric_id ? numeric_id.to_i : nil
67+
68+
log.warn "synapse: discovered backend #{name} at #{host}:#{server_port} for service #{@name}"
69+
{ 'name' => name, 'host' => host, 'port' => server_port, 'id' => numeric_id}
70+
end
71+
end
72+
73+
def each_dir(d)
74+
new_backends = []
75+
d.children.each do |node|
76+
if node.directory?
77+
new_backends << each_dir(@etcd.get(node.key))
78+
else
79+
backend = each_node(node)
80+
if backend
81+
new_backends << backend
82+
end
83+
end
84+
end
85+
new_backends.flatten
86+
end
87+
88+
# find the current backends at the discovery path; sets @backends
89+
def discover
90+
log.info "synapse: discovering backends for service #{@name}"
91+
92+
d = nil
93+
begin
94+
d = @etcd.get(@discovery['path'])
95+
rescue Etcd::KeyNotFound
96+
create(@discovery['path'])
97+
d = @etcd.get(@discovery['path'])
98+
end
99+
100+
new_backends = []
101+
if d.directory?
102+
new_backends = each_dir(d)
103+
else
104+
log.warn "synapse: path #{@discovery['path']} is not a directory"
105+
end
106+
107+
if new_backends.empty?
108+
if @default_servers.empty?
109+
log.warn "synapse: no backends and no default servers for service #{@name}; using previous backends: #{@backends.inspect}"
110+
false
111+
else
112+
log.warn "synapse: no backends for service #{@name}; using default servers: #{@default_servers.inspect}"
113+
@backends = @default_servers
114+
true
115+
end
116+
else
117+
if @backends != new_backends
118+
log.info "synapse: discovered #{new_backends.length} backends (including new) for service #{@name}"
119+
@backends = new_backends
120+
true
121+
else
122+
log.info "synapse: discovered #{new_backends.length} backends for service #{@name}"
123+
false
124+
end
125+
end
126+
end
127+
128+
def watch
129+
while !@should_exit
130+
begin
131+
@etcd.watch(@discovery['path'], :timeout => 60, :recursive => true)
132+
rescue Timeout::Error
133+
else
134+
if discover
135+
@synapse.reconfigure!
136+
end
137+
end
138+
end
139+
end
140+
141+
# decode the data at a zookeeper endpoint
142+
def deserialize_service_instance(data)
143+
log.debug "synapse: deserializing process data"
144+
decoded = JSON.parse(data)
145+
146+
host = decoded['host'] || (raise ValueError, 'instance json data does not have host key')
147+
port = decoded['port'] || (raise ValueError, 'instance json data does not have port key')
148+
name = decoded['name'] || nil
149+
150+
return host, port, name
151+
end
152+
end
153+
end
154+

synapse.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
1919
gem.add_runtime_dependency "aws-sdk", "~> 1.39"
2020
gem.add_runtime_dependency "docker-api", "~> 1.7.2"
2121
gem.add_runtime_dependency "zk", "~> 1.9.4"
22+
gem.add_runtime_dependency "etcd", "~> 0.2.4"
2223

2324
gem.add_development_dependency "rake"
2425
gem.add_development_dependency "rspec", "~> 3.1.0"

0 commit comments

Comments
 (0)