Class: Mongo::Node
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#client ⇒ Object
Returns the value of attribute client.
-
#host ⇒ Object
Returns the value of attribute host.
-
#last_state ⇒ Object
Returns the value of attribute last_state.
-
#port ⇒ Object
Returns the value of attribute port.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #=~(other) ⇒ Object
- #active? ⇒ Boolean
- #arbiters ⇒ Object
-
#close ⇒ Object
This should only be called within a mutex.
- #config ⇒ Object
-
#connect ⇒ Object
Create a connection to the provided node, and, if successful, return the socket.
- #connected? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
- #healthy? ⇒ Boolean
- #host_port ⇒ Object
- #host_string ⇒ Object
-
#initialize(client, host_port) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
-
#node_list ⇒ Object
Return a list of replica set nodes from the config.
- #primary? ⇒ Boolean
- #secondary? ⇒ Boolean
-
#set_config ⇒ Object
Get the configuration for the provided node as returned by the ismaster command.
- #tags ⇒ Object
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
#address ⇒ Object
Returns the value of attribute address.
4 5 6 |
# File 'lib/mongo/util/node.rb', line 4 def address @address end |
#client ⇒ Object
Returns the value of attribute client.
4 5 6 |
# File 'lib/mongo/util/node.rb', line 4 def client @client end |
#host ⇒ Object
Returns the value of attribute host.
4 5 6 |
# File 'lib/mongo/util/node.rb', line 4 def host @host end |
#last_state ⇒ Object
Returns the value of attribute last_state.
4 5 6 |
# File 'lib/mongo/util/node.rb', line 4 def last_state @last_state end |
#port ⇒ Object
Returns the value of attribute port.
4 5 6 |
# File 'lib/mongo/util/node.rb', line 4 def port @port end |
#socket ⇒ Object
Returns the value of attribute socket.
4 5 6 |
# File 'lib/mongo/util/node.rb', line 4 def socket @socket end |
Instance Method Details
#=~(other) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/mongo/util/node.rb', line 20 def =~(other) if other.is_a?(String) h, p = split_node(other) h == @host && p == @port else false end end |
#active? ⇒ Boolean
72 73 74 75 76 77 78 79 |
# File 'lib/mongo/util/node.rb', line 72 def active? begin result = @client['admin'].command({:ping => 1}, :socket => @socket) rescue OperationFailure, SocketError, SystemCallError, IOError return nil end result['ok'] == 1 end |
#arbiters ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/mongo/util/node.rb', line 120 def arbiters return [] unless config['arbiters'] config['arbiters'].map do |arbiter| split_node(arbiter) end end |
#close ⇒ Object
This should only be called within a mutex
60 61 62 63 64 65 66 |
# File 'lib/mongo/util/node.rb', line 60 def close if @socket && !@socket.closed? @socket.close end @socket = nil @config = nil end |
#config ⇒ Object
33 34 35 36 37 |
# File 'lib/mongo/util/node.rb', line 33 def config connect unless connected? set_config unless @config || !connected? @config end |
#connect ⇒ Object
Create a connection to the provided node, and, if successful, return the socket. Otherwise, return nil.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mongo/util/node.rb', line 46 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.}.") close end end end |
#connected? ⇒ Boolean
68 69 70 |
# File 'lib/mongo/util/node.rb', line 68 def connected? @socket != nil && !@socket.closed? end |
#eql?(other) ⇒ Boolean Also known as: ==
15 16 17 |
# File 'lib/mongo/util/node.rb', line 15 def eql?(other) (other.is_a?(Node) && @address == other.address) end |
#hash ⇒ Object
144 145 146 |
# File 'lib/mongo/util/node.rb', line 144 def hash address.hash end |
#healthy? ⇒ Boolean
148 149 150 |
# File 'lib/mongo/util/node.rb', line 148 def healthy? connected? && config end |
#host_port ⇒ Object
140 141 142 |
# File 'lib/mongo/util/node.rb', line 140 def host_port [@host, @port] end |
#host_string ⇒ Object
29 30 31 |
# File 'lib/mongo/util/node.rb', line 29 def host_string address end |
#inspect ⇒ Object
39 40 41 |
# File 'lib/mongo/util/node.rb', line 39 def inspect "<Mongo::Node:0x#{self.object_id.to_s(16)} @host=#{@host} @port=#{@port}>" end |
#node_list ⇒ Object
Return a list of replica set nodes from the config. Note: this excludes arbiters.
112 113 114 115 116 117 118 |
# File 'lib/mongo/util/node.rb', line 112 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
128 129 130 |
# File 'lib/mongo/util/node.rb', line 128 def primary? config['ismaster'] == true || config['ismaster'] == 1 end |
#secondary? ⇒ Boolean
132 133 134 |
# File 'lib/mongo/util/node.rb', line 132 def secondary? config['secondary'] == true || config['secondary'] == 1 end |
#set_config ⇒ Object
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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mongo/util/node.rb', line 84 def set_config @node_mutex.synchronize do begin if @config @last_state = @config['ismaster'] ? :primary : :other end @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.}") # Socket may already be nil from issuing command close end end end |
#tags ⇒ Object
136 137 138 |
# File 'lib/mongo/util/node.rb', line 136 def config['tags'] || {} end |