Skip to content

Commit

Permalink
Merge remote-tracking branch 'noggi/synapse/haproxy_avoid_noop_reenab…
Browse files Browse the repository at this point in the history
…le' into rebase__noggi/synapse/haproxy_avoid_noop_reenable

* noggi/synapse/haproxy_avoid_noop_reenable:
  [haproxy] Avoid unnecessary HAProxy re-configuration.
  • Loading branch information
Dmitriy Samovskiy committed Mar 21, 2017
2 parents c759ef1 + 61aee69 commit 29ecc8a
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions lib/synapse/config_generator/haproxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
require 'json'
require 'socket'
require 'digest/sha1'
require 'set'

class Synapse::ConfigGenerator
class Haproxy < BaseGenerator
include Synapse::Logging

NAME = 'haproxy'.freeze

HAPROXY_CMD_BATCH_SIZE = 4

# these come from the documentation for haproxy (1.5 and 1.6)
# http://haproxy.1wt.eu/download/1.5/doc/configuration.txt
# http://haproxy.1wt.eu/download/1.6/doc/configuration.txt
Expand Down Expand Up @@ -1102,20 +1105,23 @@ def update_backends_at(socket_file_path, watchers)

# parse the stats output to get current backends
cur_backends = {}
re = Regexp.new('^(.+?),(.+?),(?:.*?,){15}(.+?),')

info.split("\n").each do |line|
next if line[0] == '#'

parts = line.split(',')
next if ['FRONTEND', 'BACKEND'].include?(parts[1])
name, addr, state = re.match(line)[1..3]

next if ['FRONTEND', 'BACKEND'].include?(address)

cur_backends[parts[0]] ||= []
cur_backends[parts[0]] << parts[1]
cur_backends[name] ||= {}
cur_backends[name][address] = state
end

# build a list of backends that should be enabled
enabled_backends = {}
watchers.each do |watcher|
enabled_backends[watcher.name] = []
enabled_backends[watcher.name] = Set.new
next if watcher.backends.empty?
next if watcher.config_for_generator[name]['disabled']

Expand All @@ -1136,28 +1142,36 @@ def update_backends_at(socket_file_path, watchers)
end
end

commands = []

# actually enable the enabled backends, and disable the disabled ones
cur_backends.each do |section, backends|
backends.each do |backend|
if enabled_backends.fetch(section, []).include? backend
command = "enable server #{section}/#{backend}\n"
backends.each do |backend, state|
if enabled_backends.fetch(section, Set.new).include? backend
next if state == 'UP'
command = "enable server #{section}/#{backend}"
else
command = "disable server #{section}/#{backend}\n"
command = "disable server #{section}/#{backend}"
end
# Batch commands so that we don't need to re-open the connection
# for every command.
commands << command
end
end

# actually write the command to the socket
begin
output = talk_to_socket(socket_file_path, command)
rescue StandardError => e
log.warn "synapse: restart required because socket command #{command} failed with "\
"error #{e.inspect}"
commands.each_slice(HAPROXY_CMD_BATCH_SIZE) do |batch|
# actually write the command to the socket
begin
output = talk_to_socket(socket_file_path, batch.join(';') + "\n")
rescue StandardError => e
log.warn "synapse: restart required because socket command #{batch.join(';')} failed with "\
"error #{e.inspect}"
@restart_required = true
else
unless output == "\n" * batch.size
log.warn "synapse: restart required because socket command #{batch.join(';')} failed with "\
"output #{output}"
@restart_required = true
else
unless output == "\n"
log.warn "synapse: restart required because socket command #{command} failed with "\
"output #{output}"
@restart_required = true
end
end
end
end
Expand Down

0 comments on commit 29ecc8a

Please sign in to comment.