class OpenSearch::Transport::Transport::Sniffer

Handles node discovery (“sniffing”)

Constants

PROTOCOL

Attributes

timeout[RW]
transport[R]

Public Class Methods

new(transport) click to toggle source

@param transport [Object] A transport instance

# File lib/opensearch/transport/transport/sniffer.rb, line 41
def initialize(transport)
  @transport = transport
  @timeout   = transport.options[:sniffer_timeout] || 1
end

Public Instance Methods

hosts() click to toggle source

Retrieves the node list from the OpenSearch’s _Nodes Info API_ and returns a normalized Array of information suitable for passing to transport.

Shuffles the collection before returning it when the ‘randomize_hosts` option is set for transport.

@return [Array<Hash>] @raise [SnifferTimeoutError]

# File lib/opensearch/transport/transport/sniffer.rb, line 55
def hosts
  Timeout::timeout(timeout, SnifferTimeoutError) do
    nodes = perform_sniff_request.body

    hosts = nodes['nodes'].map do |id, info|
      next unless info[PROTOCOL]
      host, port = parse_publish_address(info[PROTOCOL]['publish_address'])

      {
        id: id,
        name: info['name'],
        version: info['version'],
        host: host,
        port: port,
        roles: info['roles'],
        attributes: info['attributes']
      }
    end.compact

    hosts.shuffle! if transport.options[:randomize_hosts]
    hosts
  end
end

Private Instance Methods

parse_address_port(publish_address) click to toggle source
# File lib/opensearch/transport/transport/sniffer.rb, line 98
def parse_address_port(publish_address)
  # address is ipv6
  if publish_address =~ /[\[\]]/
    if parts = publish_address.match(/\A\[(.+)\](?::(\d+))?\z/)
      [ parts[1], parts[2] ]
    end
  else
    publish_address.split(':')
  end
end
parse_publish_address(publish_address) click to toggle source
# File lib/opensearch/transport/transport/sniffer.rb, line 88
def parse_publish_address(publish_address)
  # publish_address is in the format hostname/ip:port
  if publish_address =~ /\//
    parts = publish_address.partition('/')
    [ parts[0], parse_address_port(parts[2])[1] ]
  else
    parse_address_port(publish_address)
  end
end
perform_sniff_request() click to toggle source
# File lib/opensearch/transport/transport/sniffer.rb, line 81
def perform_sniff_request
  transport.perform_request(
    'GET', '_nodes/http', {}, nil, nil,
    reload_on_failure: false
  )
end