Skip to content

Commit

Permalink
Create an option to stop ignoring weights. By default weights will still
Browse files Browse the repository at this point in the history
be ignored. This commit is based on:
airbnb#131

but does the following things differently:
1. By default weights are ignored. This is to maintain current behavior
for safety.
2. HAProxy will reconfigure if weights changes.
  • Loading branch information
minkovich authored and Tiffany Low committed May 27, 2016
1 parent b904393 commit 44b7ae9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ This section is its own hash, which should contain the following keys:
* `backend_order`: optional: how backends should be ordered in the `backend` stanza. (default is shuffling). Setting to `asc` means sorting backends in ascending alphabetical order before generating stanza. `desc` means descending alphabetical order. `no_shuffle` means no shuffling or sorting.
* `shared_frontend`: optional: haproxy configuration directives for a shared http frontend (see below)
* `cookie_value_method`: optional: default value is `name`, it defines the way your backends receive a cookie value in http mode. If equal to `hash`, synapse hashes backend names on cookie value assignation of your discovered backends, useful when you want to use haproxy cookie feature but you do not want that your end users receive a Set-Cookie with your server name and ip readable in clear.
* `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 true so weights will *NOT* be used by default.

<a name="haproxy"/>
### Configuring HAProxy ###
Expand Down
5 changes: 5 additions & 0 deletions lib/synapse/haproxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,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 unless @opts.key?('ignore_weights')

# how to restart haproxy
@restart_interval = @opts.fetch('restart_interval', 2).to_i
Expand Down Expand Up @@ -747,6 +748,10 @@ def generate_backend_stanza(watcher, config)
b = "#{b} cookie #{backend_name}"
end
end
if !@opts['ignore_weights'] && backend.has_key?('weight')
weight = backend['weight'].to_i
b = "#{b} weight #{weight}"
end
b = "#{b} #{watcher.haproxy['server_options']}" if watcher.haproxy['server_options']
b = "#{b} #{backend['haproxy_server_options']}" if backend['haproxy_server_options']
b = "#{b} disabled" unless backend['enabled']
Expand Down
2 changes: 1 addition & 1 deletion lib/synapse/service_watcher/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def set_backends(new_backends)
# Aggregate and deduplicate all potential backend service instances.
new_backends = (new_backends + @default_servers) if @keep_default_servers
new_backends = new_backends.uniq {|b|
[b['host'], b['port'], b.fetch('name', '')]
[b['host'], b['port'], b.fetch('name', ''), b.fetch('weight', 1)]
}

if new_backends.to_set == @backends.to_set
Expand Down
27 changes: 25 additions & 2 deletions spec/lib/synapse/haproxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

let(:mockwatcher_with_server_options) do
mockWatcher = double(Synapse::ServiceWatcher)
allow(mockWatcher).to receive(:name).and_return('example_service2')
Expand Down Expand Up @@ -275,4 +278,24 @@ class MockWatcher; end;
expect(subject.generate_frontend_stanza(mockwatcher_frontend_with_bind_address, mockConfig)).to eql(["\nfrontend example_service5", [], "\tbind 127.0.0.3:2200", "\tdefault_backend example_service5"])
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 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 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
18 changes: 18 additions & 0 deletions spec/lib/synapse/service_watcher_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,23 @@ def remove_arg(name)
expect(subject.backends).to eq(matching_labeled_backends)
end
end

context 'with ignore_weights set to false' do
let(:backends) { [
{ 'name' => 'server1', 'host' => 'server1', 'port' => 1111, 'weight' => 11 },
{ 'name' => 'server2', 'host' => 'server2', 'port' => 2222, 'weight' => 22 },
] }
let(:non_matching_weight_backends) { [
{ 'name' => 'server1', 'host' => 'server1', 'port' => 1111, 'weight' => 33 },
{ 'name' => 'server2', 'host' => 'server2', 'port' => 2222, 'weight' => 22 },
] }
it 'updates backends only when weights change' do
expect(subject).to receive(:'reconfigure!').exactly(:twice)
expect(subject.send(:set_backends, backends)).to equal(true)
expect(subject.backends).to eq(backends)
expect(subject.send(:set_backends, non_matching_weight_backends)).to equal(true)
expect(subject.backends).to eq(non_matching_weight_backends)
end
end
end
end
1 change: 1 addition & 0 deletions spec/support/minimum.conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ haproxy:
config_file_path: "/etc/haproxy/haproxy.cfg"
do_writes: false
do_reloads: false
ignore_weights: false
global:
- global_test_option

Expand Down

0 comments on commit 44b7ae9

Please sign in to comment.