From 234a8b738b71f177bc176af08fcaae029f50f125 Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Tue, 28 Jul 2015 12:09:16 +0100 Subject: [PATCH 1/4] Add weight if available to the config generated by synapse --- lib/synapse/haproxy.rb | 2 ++ spec/lib/synapse/haproxy_spec.rb | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/synapse/haproxy.rb b/lib/synapse/haproxy.rb index 89c50077..8ca6630f 100644 --- a/lib/synapse/haproxy.rb +++ b/lib/synapse/haproxy.rb @@ -533,6 +533,7 @@ def initialize(opts) @opts['do_writes'] = true unless @opts.key?('do_writes') @opts['do_socket'] = true unless @opts.key?('do_socket') @opts['do_reloads'] = true unless @opts.key?('do_reloads') + @opts['ignore_weights'] = true if @opts.key?('ignore_weights') and @opts['ignore_weights'] # how to restart haproxy @restart_interval = @opts.fetch('restart_interval', 2).to_i @@ -719,6 +720,7 @@ def generate_backend_stanza(watcher, config) backend = backends[backend_name] b = "\tserver #{backend_name} #{backend['host']}:#{backend['port']}" b = "#{b} cookie #{backend_name}" unless config.include?('mode tcp') + b = "#{b} weight #{backend['weight']}" if backend['weight'] && backend['weight'].is_a?(Fixnum) and backend['weight'] > 0 and !@opts['ignore_weights'] b = "#{b} #{watcher.haproxy['server_options']}" b = "#{b} disabled" unless backend['enabled'] b } diff --git a/spec/lib/synapse/haproxy_spec.rb b/spec/lib/synapse/haproxy_spec.rb index a90d2ec7..852b386b 100644 --- a/spec/lib/synapse/haproxy_spec.rb +++ b/spec/lib/synapse/haproxy_spec.rb @@ -5,15 +5,18 @@ class MockWatcher; end; describe Synapse::Haproxy do subject { Synapse::Haproxy.new(config['haproxy']) } - let(:mockwatcher) do + def createmockwatcher(backends) mockWatcher = double(Synapse::ServiceWatcher) allow(mockWatcher).to receive(:name).and_return('example_service') - backends = [{ 'host' => 'somehost', 'port' => '5555'}] allow(mockWatcher).to receive(:backends).and_return(backends) allow(mockWatcher).to receive(:haproxy).and_return({'server_options' => "check inter 2000 rise 3 fall 2"}) mockWatcher end + let(:mockwatcher) do + createmockwatcher [{ 'host' => 'somehost', 'port' => '5555'}] + end + it 'updating the config' do expect(subject).to receive(:generate_config) subject.update_config([mockwatcher]) @@ -29,4 +32,14 @@ class MockWatcher; end; expect(subject.generate_backend_stanza(mockwatcher, mockConfig)).to eql(["\nbackend example_service", ["\tmode tcp"], ["\tserver somehost:5555 somehost:5555 check inter 2000 rise 3 fall 2"]]) end + it 'generates backend stanza with weight' do + mockConfig = [] + expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 1, 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 weight 1 check inter 2000 rise 3 fall 2"]]) + end + + it 'generates backend stanza withour bad weight' do + mockConfig = [] + expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 'hi', 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 check inter 2000 rise 3 fall 2"]]) + end + end From dccb0e8d78e6c88b02b384a0533967dfa1cb6551 Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Tue, 28 Jul 2015 12:11:51 +0100 Subject: [PATCH 2/4] Add some documentation to the README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 32b503f0..e5a5aa3d 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ This section is its own hash, which should contain the following keys: * `backend`: additional lines passed to the HAProxy config in the `backend` stanza of this service * `listen`: these lines will be parsed and placed in the correct `frontend`/`backend` section as applicable; you can put lines which are the same for the frontend and backend here. * `shared_frontend`: optional: haproxy configuration directives for a shared http frontend (see below) +* `ignore_weights`: optional: stops haproxy backend 'weight' options being generated, even if the Nerve registrations contain this information. This will cause all backend servers to be treated equally by haproxy. ### Configuring HAProxy ### From f29c5e51e59e5e6c3cabb8ed6647d6d9cd5c2e3a Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Mon, 3 Aug 2015 09:14:37 -0700 Subject: [PATCH 3/4] Respond to initial code review comments --- README.md | 2 +- lib/synapse/haproxy.rb | 5 ++++- spec/lib/synapse/haproxy_spec.rb | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e5a5aa3d..8826890b 100644 --- a/README.md +++ b/README.md @@ -222,7 +222,7 @@ This section is its own hash, which should contain the following keys: * `backend`: additional lines passed to the HAProxy config in the `backend` stanza of this service * `listen`: these lines will be parsed and placed in the correct `frontend`/`backend` section as applicable; you can put lines which are the same for the frontend and backend here. * `shared_frontend`: optional: haproxy configuration directives for a shared http frontend (see below) -* `ignore_weights`: optional: stops haproxy backend 'weight' options being generated, even if the Nerve registrations contain this information. This will cause all backend servers to be treated equally by haproxy. +* `ignore_weights`: optional: stops haproxy backend 'weight' options being generated, even if the Nerve registrations contain this information. This will cause all backend servers to be treated equally by haproxy. This defaults to off (and so weights *will* be used by default). ### Configuring HAProxy ### diff --git a/lib/synapse/haproxy.rb b/lib/synapse/haproxy.rb index 8ca6630f..0fcf0602 100644 --- a/lib/synapse/haproxy.rb +++ b/lib/synapse/haproxy.rb @@ -720,7 +720,10 @@ def generate_backend_stanza(watcher, config) backend = backends[backend_name] b = "\tserver #{backend_name} #{backend['host']}:#{backend['port']}" b = "#{b} cookie #{backend_name}" unless config.include?('mode tcp') - b = "#{b} weight #{backend['weight']}" if backend['weight'] && backend['weight'].is_a?(Fixnum) and backend['weight'] > 0 and !@opts['ignore_weights'] + if !@opts['ignore_weights'] && backend.has_key?('weight') + weight = backend['weight'].to_i + b = "#{b} weight #{weight}" + end b = "#{b} #{watcher.haproxy['server_options']}" b = "#{b} disabled" unless backend['enabled'] b } diff --git a/spec/lib/synapse/haproxy_spec.rb b/spec/lib/synapse/haproxy_spec.rb index 852b386b..165bb1ba 100644 --- a/spec/lib/synapse/haproxy_spec.rb +++ b/spec/lib/synapse/haproxy_spec.rb @@ -37,7 +37,7 @@ def createmockwatcher(backends) expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 1, 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 weight 1 check inter 2000 rise 3 fall 2"]]) end - it 'generates backend stanza withour bad weight' do + it 'generates backend stanza without bad weight' do mockConfig = [] expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 'hi', 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 check inter 2000 rise 3 fall 2"]]) end From bc76050d716c7894b5916e2b8c1db49db8a55693 Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Mon, 3 Aug 2015 09:19:11 -0700 Subject: [PATCH 4/4] Fix the tests, and write some more for the behaviour we want --- Gemfile.lock | 10 +++++++--- spec/lib/synapse/haproxy_spec.rb | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 07b0aba3..ce332ef4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,6 +24,7 @@ GEM excon (0.45.4) ffi (1.9.3-java) json (1.8.3) + json (1.8.3-java) little-plugger (1.1.3) logging (1.8.2) little-plugger (>= 1.1.3) @@ -33,6 +34,7 @@ GEM multi_json (1.11.2) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) + nokogiri (1.6.6.2-java) pry (0.9.12.2) coderay (~> 1.0.5) method_source (~> 0.8) @@ -58,12 +60,17 @@ GEM rspec-support (~> 3.1.0) rspec-support (3.1.2) slop (3.4.6) + slyphon-log4j (1.2.15) + slyphon-zookeeper_jar (3.3.5-java) spoon (0.0.4) ffi zk (1.9.5) logging (~> 1.8.2) zookeeper (~> 1.4.0) zookeeper (1.4.10) + zookeeper (1.4.10-java) + slyphon-log4j (= 1.2.15) + slyphon-zookeeper_jar (= 3.3.5) PLATFORMS java @@ -75,6 +82,3 @@ DEPENDENCIES rake rspec (~> 3.1.0) synapse! - -BUNDLED WITH - 1.10.5 diff --git a/spec/lib/synapse/haproxy_spec.rb b/spec/lib/synapse/haproxy_spec.rb index 165bb1ba..b686253a 100644 --- a/spec/lib/synapse/haproxy_spec.rb +++ b/spec/lib/synapse/haproxy_spec.rb @@ -37,9 +37,21 @@ def createmockwatcher(backends) expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 1, 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 weight 1 check inter 2000 rise 3 fall 2"]]) end - it 'generates backend stanza without bad weight' do + it 'generates backend stanza with bad weight = 0' do mockConfig = [] - expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 'hi', 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 check inter 2000 rise 3 fall 2"]]) + expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => 'hi', 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 weight 0 check inter 2000 rise 3 fall 2"]]) end + it 'generates backend stanza with nil weight = 0' do + mockConfig = [] + expect(subject.generate_backend_stanza(createmockwatcher([{ 'weight' => nil, 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 weight 0 check inter 2000 rise 3 fall 2"]]) + end + + it 'generates backend stanza without weight' do + mockConfig = [] + expect(subject.generate_backend_stanza(createmockwatcher([{ 'host' => 'somehost', 'port' => '5555'}]), mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 cookie somehost:5555 check inter 2000 rise 3 fall 2"]]) + end + + + end