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
# 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
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

#configObject

Returns the value of attribute config.



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

def config
  @config
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)


55
56
57
58
59
60
61
62
# File 'lib/mongo/util/node.rb', line 55

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

#arbitersObject



102
103
104
105
106
107
108
109
110
# File 'lib/mongo/util/node.rb', line 102

def arbiters
  connect unless connected?
  set_config unless @config
  return [] unless config['arbiters']

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

#closeObject



43
44
45
46
47
48
49
# File 'lib/mongo/util/node.rb', line 43

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

#connectObject

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



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mongo/util/node.rb', line 30

def connect
  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}.")
    socket.close if socket
  end

  @socket = socket
end

#connected?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/mongo/util/node.rb', line 51

def connected?
  @socket != nil
end

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

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash
  address.hash
end

#healthy?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
# File 'lib/mongo/util/node.rb', line 132

def healthy?
  if @config.has_key?('secondary')
    @config['ismaster'] || @config['secondary']
  else
    true
  end
end

#host_portObject



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

def host_port
  [@host, @port]
end

#host_stringObject



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

def host_string
  address
end

#inspectObject



23
24
25
# File 'lib/mongo/util/node.rb', line 23

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.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mongo/util/node.rb', line 90

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?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/mongo/util/node.rb', line 112

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

#secondary?Boolean

Returns:

  • (Boolean)


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

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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mongo/util/node.rb', line 67

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

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

    check_set_membership(config)
    check_set_name(config)
  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

  @config
end

#tagsObject



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

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