Class: Mongo::Node

Inherits:
Object show all
Defined in:
lib/mongo/util/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, host_port) ⇒ Node

Returns a new instance of Node.



6
7
8
9
10
11
12
13
# File 'lib/mongo/util/node.rb', line 6

def initialize(client, host_port)
  @client = client
  @host, @port = split_node(host_port)
  @address = "#{@host}:#{@port}"
  @config = nil
  @socket = nil
  @node_mutex = Mutex.new
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



4
5
6
# File 'lib/mongo/util/node.rb', line 4

def address
  @address
end

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/mongo/util/node.rb', line 4

def client
  @client
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/mongo/util/node.rb', line 4

def host
  @host
end

#last_stateObject

Returns the value of attribute last_state.



4
5
6
# File 'lib/mongo/util/node.rb', line 4

def last_state
  @last_state
end

#portObject

Returns the value of attribute port.



4
5
6
# File 'lib/mongo/util/node.rb', line 4

def port
  @port
end

#socketObject

Returns the value of attribute socket.



4
5
6
# File 'lib/mongo/util/node.rb', line 4

def socket
  @socket
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/mongo/util/node.rb', line 63

def active?
  begin
    result = @client['admin'].command({:ping => 1}, :socket => @socket)
  rescue OperationFailure, SocketError, SystemCallError, IOError
    return nil
  end
  result['ok'] == 1
end

#arbitersObject



107
108
109
110
111
112
113
# File 'lib/mongo/util/node.rb', line 107

def arbiters
  return [] unless config['arbiters']

  config['arbiters'].map do |arbiter|
    split_node(arbiter)
  end
end

#closeObject

This should only be called within a mutex



51
52
53
54
55
56
57
# File 'lib/mongo/util/node.rb', line 51

def close
  if @socket && !@socket.closed?
    @socket.close
  end
  @socket = nil
  @config = nil
end

#configObject



24
25
26
27
28
# File 'lib/mongo/util/node.rb', line 24

def config
  connect unless connected?
  set_config unless @config
  @config
end

#connectObject

Create a connection to the provided node, and, if successful, return the socket. Otherwise, return nil.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mongo/util/node.rb', line 37

def connect
  @node_mutex.synchronize do
    begin
      @socket = @client.socket_class.new(@host, @port,
        @client.op_timeout, @client.connect_timeout
      )
    rescue OperationTimeout, ConnectionFailure, OperationFailure, SocketError, SystemCallError, IOError => ex
      @client.log(:debug, "Failed connection to #{host_string} with #{ex.class}, #{ex.message}.")
      close
    end
  end
end

#connected?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/mongo/util/node.rb', line 59

def connected?
  @socket != nil
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


15
16
17
# File 'lib/mongo/util/node.rb', line 15

def eql?(other)
  other.is_a?(Node) && @address == other.address
end

#hashObject



131
132
133
# File 'lib/mongo/util/node.rb', line 131

def hash
  address.hash
end

#healthy?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/mongo/util/node.rb', line 135

def healthy?
  connected? && config
end

#host_portObject



127
128
129
# File 'lib/mongo/util/node.rb', line 127

def host_port
  [@host, @port]
end

#host_stringObject



20
21
22
# File 'lib/mongo/util/node.rb', line 20

def host_string
  address
end

#inspectObject



30
31
32
# File 'lib/mongo/util/node.rb', line 30

def inspect
  "<Mongo::Node:0x#{self.object_id.to_s(16)} @host=#{@host} @port=#{@port}>"
end

#node_listObject

Return a list of replica set nodes from the config. Note: this excludes arbiters.



99
100
101
102
103
104
105
# File 'lib/mongo/util/node.rb', line 99

def node_list
  nodes = []
  nodes += config['hosts'] if config['hosts']
  nodes += config['passives'] if config['passives']
  nodes += ["#{@host}:#{@port}"] if @client.mongos?
  nodes
end

#primary?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/mongo/util/node.rb', line 115

def primary?
  config['ismaster'] == true || config['ismaster'] == 1
end

#secondary?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/mongo/util/node.rb', line 119

def secondary?
  config['secondary'] == true || config['secondary'] == 1
end

#set_configObject

Get the configuration for the provided node as returned by the ismaster command. Additionally, check that the replica set name matches with the name provided.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mongo/util/node.rb', line 75

def set_config
  @node_mutex.synchronize do
    begin
      @config = @client['admin'].command({:ismaster => 1}, :socket => @socket)

      if @config['msg']
        @client.log(:warn, "#{config['msg']}")
      end

      unless @client.mongos?
        check_set_membership(@config)
        check_set_name(@config)
      end
    rescue ConnectionFailure, OperationFailure, OperationTimeout, SocketError, SystemCallError, IOError => ex
      @client.log(:warn, "Attempted connection to node #{host_string} raised " +
                          "#{ex.class}: #{ex.message}")
      # Socket may already be nil from issuing command
      close
    end
  end
end

#tagsObject



123
124
125
# File 'lib/mongo/util/node.rb', line 123

def tags
  config['tags'] || {}
end