Class: Elasticsearch::Transport::Transport::Sniffer

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/transport/transport/sniffer.rb

Overview

Handles node discovery (“sniffing”)

Constant Summary collapse

PROTOCOL =
'http'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ Sniffer

Returns a new instance of Sniffer.

Parameters:

  • transport (Object)

    A transport instance


15
16
17
18
# File 'lib/elasticsearch/transport/transport/sniffer.rb', line 15

def initialize(transport)
  @transport = transport
  @timeout   = transport.options[:sniffer_timeout] || 1
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout


11
12
13
# File 'lib/elasticsearch/transport/transport/sniffer.rb', line 11

def timeout
  @timeout
end

#transportObject (readonly)

Returns the value of attribute transport


10
11
12
# File 'lib/elasticsearch/transport/transport/sniffer.rb', line 10

def transport
  @transport
end

Instance Method Details

#hostsArray<Hash>

Retrieves the node list from the Elasticsearch's [_Nodes Info API_](www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-info/) 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.

Returns:

  • (Array<Hash>)

Raises:


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/elasticsearch/transport/transport/sniffer.rb', line 29

def hosts
  Timeout::timeout(timeout, SnifferTimeoutError) do
    nodes = transport.perform_request('GET', '_nodes/http').body

    hosts = nodes['nodes'].map do |id,info|
      if info[PROTOCOL]
        host, port = info[PROTOCOL]['publish_address'].split(':')

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

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