From 655f33a0811668dff928ab1578b0026ebb6a6314 Mon Sep 17 00:00:00 2001 From: Alan Smith Date: Fri, 15 Aug 2014 12:40:23 -0400 Subject: [PATCH 1/2] Added etcd hosts failover. --- lib/nerve/reporter/etcd.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/nerve/reporter/etcd.rb b/lib/nerve/reporter/etcd.rb index 43006482..f4153fba 100644 --- a/lib/nerve/reporter/etcd.rb +++ b/lib/nerve/reporter/etcd.rb @@ -4,11 +4,10 @@ class Nerve::Reporter class Etcd < Base def initialize(service) - %w{etcd_host instance_id host port}.each do |required| + %w{etcd_hosts instance_id host port}.each do |required| raise ArgumentError, "missing required argument #{required} for new service watcher" unless service[required] end - @host = service['etcd_host'] - @port = service['etcd_port'] || 4003 + @hosts = service['etcd_hosts'].shuffle path = service['etcd_path'] || '/' @key = path.split('/').push(service['instance_id']).join('/') @data = parse_data({'host' => service['host'], 'port' => service['port'], 'name' => service['instance_id']}) @@ -16,7 +15,19 @@ def initialize(service) def start() log.info "nerve: waiting to connect to etcd at #{@path}" - @etcd = ::Etcd.client(:host => @host, :port => @port) + @hosts.each do |h| + host, port = h.split(':') + port = port || 4003 + @etcd = ::Etcd.client(:host => host, :port => port) + + connected = + begin + @etcd.leader + rescue + false + end + break if connected + end log.info "nerve: successfully created etcd connection to #{@key}" end From 837562fe34772df42d07649b3b4cdaee764e310c Mon Sep 17 00:00:00 2001 From: Alan Smith Date: Fri, 15 Aug 2014 13:44:47 -0400 Subject: [PATCH 2/2] Modified tests to reflect new etcd parameters. --- example/nerve_services/etcd_service1.json | 3 +-- spec/lib/nerve/reporter_etcd_spec.rb | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/example/nerve_services/etcd_service1.json b/example/nerve_services/etcd_service1.json index 2336f145..55b6760f 100644 --- a/example/nerve_services/etcd_service1.json +++ b/example/nerve_services/etcd_service1.json @@ -2,8 +2,7 @@ "host": "1.2.3.4", "port": 3000, "reporter_type": "etcd", - "etcd_host": "localhost", - "etcd_port": 4001, + "etcd_hosts": ["localhost"], "etcd_path": "/nerve/services/your_http_service/services", "check_interval": 2, "checks": [ diff --git a/spec/lib/nerve/reporter_etcd_spec.rb b/spec/lib/nerve/reporter_etcd_spec.rb index c01d3dda..6a94d579 100644 --- a/spec/lib/nerve/reporter_etcd_spec.rb +++ b/spec/lib/nerve/reporter_etcd_spec.rb @@ -3,8 +3,7 @@ describe Nerve::Reporter::Etcd do let(:subject) { { - 'etcd_host' => 'etcdhost1', - 'etcd_port' => 4001, + 'etcd_hosts' => ['etcdhost1:4001'], 'etcd_path' => '/path', 'instance_id' => 'instance_id', 'host' => 'host',