class Mongo::Node

Attributes

address[RW]
config[RW]
connection[RW]
host[RW]
last_state[RW]
port[RW]
socket[RW]

Public Class Methods

new(connection, data) click to toggle source
# File lib/mongo/util/node.rb, line 7
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
  @socket = nil
end

Public Instance Methods

==(other)
Alias for: eql?
active?() click to toggle source
# File lib/mongo/util/node.rb, line 65
def active?
  begin
    result = @connection['admin'].command({:ping => 1}, :socket => @socket)
    return result['ok'] == 1
  rescue OperationFailure, SocketError, SystemCallError, IOError
    return nil
  end
end
arbiters() click to toggle source
# 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
close() click to toggle source
# File lib/mongo/util/node.rb, line 53
def close
  if @socket && !@socket.closed?
    @socket.close
  end
  @socket = nil
  @config = nil
end
connect() click to toggle source

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

# File lib/mongo/util/node.rb, line 36
def connect
  begin
    socket = nil
    socket = @connection.socket_class.new(@host, @port, 
      @connection.op_timeout, @connection.connect_timeout
    )

    return nil if socket.nil?
  rescue OperationTimeout, ConnectionFailure, 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
connected?() click to toggle source
# File lib/mongo/util/node.rb, line 61
def connected?
  @socket != nil
end
eql?(other) click to toggle source
# File lib/mongo/util/node.rb, line 20
def eql?(other)
  other.is_a?(Node) && host == other.host && port == other.port
end
Also aliased as: ==
hash() click to toggle source
# File lib/mongo/util/node.rb, line 146
def hash
  address.hash
end
host_port() click to toggle source
# File lib/mongo/util/node.rb, line 142
def host_port
  [@host, @port]
end
host_string() click to toggle source
# File lib/mongo/util/node.rb, line 25
def host_string
  address
end
inspect() click to toggle source
# File lib/mongo/util/node.rb, line 29
def inspect
  "<Mongo::Node:0x#{self.object_id.to_s(16)} @host=#{@host} @port=#{@port}>"
end
node_list() click to toggle source

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
primary?() click to toggle source
# File lib/mongo/util/node.rb, line 134
def primary?
  @config['ismaster'] == true || @config['ismaster'] == 1
end
secondary?() click to toggle source
# File lib/mongo/util/node.rb, line 138
def secondary?
  @config['secondary'] == true || @config['secondary'] == 1
end
set_config() click to toggle source

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 77
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, OperationTimeout, SocketError, SystemCallError, IOError => ex
    @connection.log(:warn, "Attempted connection to node #{host_string} raised " +
                        "#{ex.class}: #{ex.message}")

    # Socket may already be nil from issuing command
    if @socket && !@socket.closed?
      @socket.close
    end

    return nil
  end

  @config
end
tags() click to toggle source
# File lib/mongo/util/node.rb, line 126
def tags
  connect unless connected?
  set_config unless @config
  return {} unless config['tags'] && !config['tags'].empty?

  config['tags']
end

Private Instance Methods

check_set_membership(config) click to toggle source

Ensure that this node is a healty member of a replica set.

# File lib/mongo/util/node.rb, line 161
def check_set_membership(config)
  if !config['hosts']
    message = "Will not connect to #{host_string} because it's not a member " +
      "of a replica set."
    raise ConnectionFailure, message
  elsif config['hosts'].length == 1 && !config['ismaster'] &&
    !config['secondary']
    message = "Attempting to connect to an unhealthy, single-node replica set."
    raise ConnectionFailure, message
  end
end
check_set_name(config) click to toggle source

Ensure that this node is part of a replica set of the expected name.

# File lib/mongo/util/node.rb, line 174
def check_set_name(config)
  if @connection.replica_set_name
    if !config['setName']
      @connection.log(:warn, "Could not verify replica set name for member #{host_string} " +
        "because ismaster does not return name in this version of MongoDB")
    elsif @connection.replica_set_name != config['setName']
      message = "Attempting to connect to replica set '#{config['setName']}' on member #{host_string} " +
        "but expected '#{@connection.replica_set_name}'"
      raise ReplicaSetConnectionError, message
    end
  end
end
split_nodes(host_string) click to toggle source
# File lib/mongo/util/node.rb, line 152
def split_nodes(host_string)
  data = host_string.split(":")
  host = data[0]
  port = data[1].nil? ? Connection::DEFAULT_PORT : data[1].to_i

  [host, port]
end