Class | Mongo::Node |
In: |
lib/mongo/util/node.rb
|
Parent: | Object |
address | [RW] | |
config | [RW] | |
connection | [RW] | |
host | [RW] | |
port | [RW] | |
socket | [RW] |
# File lib/mongo/util/node.rb, line 6 def initialize(connection, data) @connection = connection if data.is_a?(String) @host, @port = split_nodes(data) else @host = data[0] @port = data[1].nil? ? Connection::DEFAULT_PORT : data[1].to_i end @address = "#{host}:#{port}" @config = nil end
# File lib/mongo/util/node.rb, line 71 def active? begin result = @connection['admin'].command({:ping => 1}, :socket => @socket) return result['ok'] == 1 rescue OperationFailure, SocketError, SystemCallError, IOError => ex return nil end end
# File lib/mongo/util/node.rb, line 116 def arbiters connect unless connected? set_config unless @config return [] unless config['arbiters'] config['arbiters'].map do |arbiter| split_nodes(arbiter) end end
# File lib/mongo/util/node.rb, line 59 def close if @socket @socket.close @socket = nil @config = nil end end
Create a connection to the provided node, and, if successful, return the socket. Otherwise, return nil.
# File lib/mongo/util/node.rb, line 34 def connect begin socket = nil if @connection.connect_timeout Mongo::TimeoutHandler.timeout(@connection.connect_timeout, OperationTimeout) do socket = @connection.socket_class.new(@host, @port) end else socket = @connection.socket_class.new(@host, @port) end if socket.nil? return nil else socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) end rescue OperationTimeout, OperationFailure, SocketError, SystemCallError, IOError => ex @connection.log(:debug, "Failed connection to #{host_string} with #{ex.class}, #{ex.message}.") socket.close if socket return nil end @socket = socket end
# File lib/mongo/util/node.rb, line 18 def eql?(other) other.is_a?(Node) && host == other.host && port == other.port end
# File lib/mongo/util/node.rb, line 27 def inspect "<Mongo::Node:0x#{self.object_id.to_s(16)} @host=#{@host} @port=#{@port}>" end
Return a list of replica set nodes from the config. Note: this excludes arbiters.
# File lib/mongo/util/node.rb, line 104 def node_list connect unless connected? set_config unless @config return [] unless config nodes = [] nodes += config['hosts'] if config['hosts'] nodes += config['passives'] if config['passives'] nodes end
# File lib/mongo/util/node.rb, line 134 def primary? @config['ismaster'] == true || @config['ismaster'] == 1 end
# File lib/mongo/util/node.rb, line 138 def secondary? @config['secondary'] == true || @config['secondary'] == 1 end
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.
# File lib/mongo/util/node.rb, line 83 def set_config begin @config = @connection['admin'].command({:ismaster => 1}, :socket => @socket) if @config['msg'] && @logger @connection.log(:warn, "#{config['msg']}") end check_set_membership(config) check_set_name(config) rescue ConnectionFailure, OperationFailure, SocketError, SystemCallError, IOError => ex @connection.log(:warn, "Attempted connection to node #{host_string} raised " + "#{ex.class}: #{ex.message}") return nil end @config end