class Synapse::ZookeeperDnsWatcher

Public Instance Methods

backends() click to toggle source
# File lib/synapse/service_watcher/zookeeper_dns.rb, line 185
def backends
  @dns.backends
end
ping?() click to toggle source
# File lib/synapse/service_watcher/zookeeper_dns.rb, line 174
def ping?
  @watcher.alive? && @dns.ping? && @zk.ping?
end
start() click to toggle source
# File lib/synapse/service_watcher/zookeeper_dns.rb, line 130
def start
  dns_discovery_opts = @discovery.select do |k,_|
    k == 'nameserver'
  end

  zookeeper_discovery_opts = @discovery.select do |k,_|
    k == 'hosts' || k == 'path'
  end

  @check_interval = @discovery['check_interval'] || 30.0

  @message_queue = Queue.new

  @dns = Dns.new(
    mk_child_watcher_opts(dns_discovery_opts),
    @synapse,
    @message_queue
  )

  @zk = Zookeeper.new(
    mk_child_watcher_opts(zookeeper_discovery_opts),
    @synapse,
    @message_queue
  )

  @zk.start
  @dns.start

  @watcher = Thread.new do
    until @should_exit
      # Trigger a DNS resolve every @check_interval seconds
      sleep @check_interval

      # Only trigger the resolve if the queue is empty, every other message
      # on the queue would either cause a resolve or stop the watcher
      if @message_queue.empty?
        @message_queue.push(Messages::CHECK_INTERVAL_MESSAGE)
      end

    end
    log.info "synapse: zookeeper_dns watcher exited successfully"
  end
end
stop() click to toggle source
Calls superclass method Synapse::BaseWatcher#stop
# File lib/synapse/service_watcher/zookeeper_dns.rb, line 178
def stop
  super

  @dns.stop
  @zk.stop
end

Private Instance Methods

mk_child_watcher_opts(discovery_opts) click to toggle source

Method to generate a full config for the children (Dns and Zookeeper) watchers

Notes on passing in the default_servers:

Setting the default_servers here allows the Zookeeper watcher to return
a list of backends based on the default servers when it fails to find
any matching servers.  These are passed on as the discovered backends
to the DNS watcher, which will then watch them as normal for DNS
changes.  The default servers can also come into play if none of the
hostnames from Zookeeper resolve to addresses in the DNS watcher.  This
should generally result in the expected behavior, but caution should be
taken when deciding that this is the desired behavior.
# File lib/synapse/service_watcher/zookeeper_dns.rb, line 218
def mk_child_watcher_opts(discovery_opts)
  {
    'name' => @name,
    'haproxy' => @haproxy,
    'discovery' => discovery_opts,
    'default_servers' => @default_servers,
  }
end
reconfigure!() click to toggle source

Override reconfigure! as this class should not explicitly reconfigure synapse

# File lib/synapse/service_watcher/zookeeper_dns.rb, line 229
def reconfigure!
end
validate_discovery_opts() click to toggle source
# File lib/synapse/service_watcher/zookeeper_dns.rb, line 191
def validate_discovery_opts
  unless @discovery['method'] == 'zookeeper_dns'
    raise ArgumentError, "invalid discovery method #{@discovery['method']}"
  end

  unless @discovery['hosts']
    raise ArgumentError, "missing or invalid zookeeper host for service #{@name}"
  end

  unless @discovery['path']
    raise ArgumentError, "invalid zookeeper path for service #{@name}"
  end
end