Class: Moped::Node Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Represents a client to a node in a server cluster.
Instance Attribute Summary collapse
- #address ⇒ Object readonly private
- #address The address of the node.(Theaddressofthenode.) ⇒ Object readonly
- #down_at ⇒ Object readonly private
- #down_at The time the server node went down.(Thetimetheservernodewentdown.) ⇒ Object readonly
- #ip_address ⇒ Object readonly private
- #ip_address The node's ip.(Thenode's ip.) ⇒ Object readonly
- #options ⇒ Object readonly private
- #options Additional options for the node (ssl).(Additionaloptions) ⇒ Object readonly
- #peers ⇒ Object readonly private
- #peers Other peers in the replica set.(Otherpeers) ⇒ Object readonly
- #port ⇒ Object readonly private
- #port The connection port.(Theconnectionport.) ⇒ Object readonly
- #refreshed_at ⇒ Object readonly private
- #resolved_address ⇒ Object readonly private
- #resolved_address The host/port pair.(Thehost/portpair.) ⇒ Object readonly
- #timeout ⇒ Object readonly private
- #timeout The connection timeout.(Theconnectiontimeout.) ⇒ Object readonly
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
private
Is this node equal to another?.
-
#apply_auth(credentials) ⇒ Node
private
Apply the authentication details to this node.
-
#arbiter? ⇒ true, false
private
Is the node an arbiter?.
-
#auto_discovering? ⇒ true, false
private
Is the cluster auto-discovering new nodes in the cluster?.
-
#command(database, cmd, options = {}) ⇒ Hash
private
Execute a command against a database.
-
#disconnect ⇒ nil
private
Force the node to disconnect from the server.
-
#down? ⇒ Time?
private
Is the node down?.
-
#ensure_connected ⇒ nil
private
Yields the block if a connection can be established, retrying when a connection error is raised.
-
#ensure_primary ⇒ nil
private
Set a flag on the node for the duration of provided block so that an exception is raised if the node is no longer the primary node.
-
#get_more(database, collection, cursor_id, limit) ⇒ Message
private
Execute a get more operation on the node.
-
#hash ⇒ Integer
private
Get the hash identifier for the node.
-
#initialize(address, options = {}) ⇒ Node
constructor
private
Creat the new node.
-
#insert(database, collection, documents, options = {}) ⇒ Message
private
Insert documents into the database.
-
#inspect ⇒ String
private
Get the node as a nice formatted string.
-
#kill_cursors(cursor_ids) ⇒ Message
private
Kill all provided cursors on the node.
-
#needs_refresh?(time) ⇒ true, false
private
Does the node need to be refreshed?.
-
#passive? ⇒ true, false
private
Is the node passive?.
-
#pipeline ⇒ nil
private
Execute a pipeline of commands, for example a safe mode persist.
-
#primary? ⇒ true, false
private
Is the node the replica set primary?.
-
#query(database, collection, selector, options = {}) ⇒ Message
private
Execute a query on the node.
-
#refresh ⇒ nil
private
Refresh information about the node, such as it’s status in the replica set and it’s known peers.
-
#remove(database, collection, selector, options = {}) ⇒ Message
private
Execute a remove command for the provided selector.
-
#secondary? ⇒ true, false
private
Is the node a replica set secondary?.
-
#update(database, collection, selector, change, options = {}) ⇒ Message
private
Execute an update command for the provided selector.
Constructor Details
#initialize(address, options = {}) ⇒ Node
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creat the new node.
241 242 243 244 245 246 247 248 249 250 |
# File 'lib/moped/node.rb', line 241 def initialize(address, = {}) @address = address @options = @timeout = [:timeout] || 5 @down_at = nil @refreshed_at = nil @primary = nil @secondary = nil resolve_address end |
Instance Attribute Details
#address ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def address @address end |
#address The address of the node.(Theaddressofthenode.) ⇒ Object (readonly)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moped/node.rb', line 16 attr_reader \ :address, :down_at, :ip_address, :peers, :port, :resolved_address, :timeout, :options, :refreshed_at |
#down_at ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def down_at @down_at end |
#down_at The time the server node went down.(Thetimetheservernodewentdown.) ⇒ Object (readonly)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moped/node.rb', line 16 attr_reader \ :address, :down_at, :ip_address, :peers, :port, :resolved_address, :timeout, :options, :refreshed_at |
#ip_address ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def ip_address @ip_address end |
#ip_address The node's ip.(Thenode's ip.) ⇒ Object (readonly)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moped/node.rb', line 16 attr_reader \ :address, :down_at, :ip_address, :peers, :port, :resolved_address, :timeout, :options, :refreshed_at |
#options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def @options end |
#options Additional options for the node (ssl).(Additionaloptions) ⇒ Object (readonly)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moped/node.rb', line 16 attr_reader \ :address, :down_at, :ip_address, :peers, :port, :resolved_address, :timeout, :options, :refreshed_at |
#peers ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def peers @peers end |
#peers Other peers in the replica set.(Otherpeers) ⇒ Object (readonly)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moped/node.rb', line 16 attr_reader \ :address, :down_at, :ip_address, :peers, :port, :resolved_address, :timeout, :options, :refreshed_at |
#port ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def port @port end |
#port The connection port.(Theconnectionport.) ⇒ Object (readonly)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moped/node.rb', line 16 attr_reader \ :address, :down_at, :ip_address, :peers, :port, :resolved_address, :timeout, :options, :refreshed_at |
#refreshed_at ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def refreshed_at @refreshed_at end |
#resolved_address ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def resolved_address @resolved_address end |
#resolved_address The host/port pair.(Thehost/portpair.) ⇒ Object (readonly)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moped/node.rb', line 16 attr_reader \ :address, :down_at, :ip_address, :peers, :port, :resolved_address, :timeout, :options, :refreshed_at |
#timeout ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/moped/node.rb', line 16 def timeout @timeout end |
#timeout The connection timeout.(Theconnectiontimeout.) ⇒ Object (readonly)
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moped/node.rb', line 16 attr_reader \ :address, :down_at, :ip_address, :peers, :port, :resolved_address, :timeout, :options, :refreshed_at |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is this node equal to another?
37 38 39 |
# File 'lib/moped/node.rb', line 37 def ==(other) resolved_address == other.resolved_address end |
#apply_auth(credentials) ⇒ Node
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Apply the authentication details to this node.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/moped/node.rb', line 52 def apply_auth(credentials) unless auth == credentials logouts = auth.keys - credentials.keys logouts.each { |database| logout(database) } credentials.each do |database, (username, password)| login(database, username, password) unless auth[database] == [username, password] end end self end |
#arbiter? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the node an arbiter?
336 337 338 |
# File 'lib/moped/node.rb', line 336 def arbiter? @arbiter end |
#auto_discovering? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the cluster auto-discovering new nodes in the cluster?
71 72 73 |
# File 'lib/moped/node.rb', line 71 def auto_discovering? @auto_discovering ||= [:auto_discover].nil? ? true : [:auto_discover] end |
#command(database, cmd, options = {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute a command against a database.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/moped/node.rb', line 89 def command(database, cmd, = {}) operation = Protocol::Command.new(database, cmd, ) process(operation) do |reply| result = reply.documents.first if reply.command_failure? if reply. && auth.has_key?(database) login(database, *auth[database]) result = command(database, cmd, ) else raise Errors::OperationFailure.new(operation, result) end end result end end |
#disconnect ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Force the node to disconnect from the server.
111 112 113 114 |
# File 'lib/moped/node.rb', line 111 def disconnect auth.clear connection.disconnect end |
#down? ⇒ Time?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the node down?
124 125 126 |
# File 'lib/moped/node.rb', line 124 def down? @down_at end |
#ensure_connected ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Yields the block if a connection can be established, retrying when a connection error is raised.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/moped/node.rb', line 141 def ensure_connected # Don't run the reconnection login if we're already inside an # +ensure_connected+ block. return yield if Threaded.executing?(:connection) Threaded.begin(:connection) retry_on_failure = true begin connect unless connected? yield rescue Errors::PotentialReconfiguration => e if e.reconfiguring_replica_set? raise Errors::ReplicaSetReconfigured.new(e.command, e.details) end raise rescue Errors::DoNotDisconnect # These exceptions are "expected" in the normal course of events, and # don't necessitate disconnecting. raise rescue Errors::ConnectionFailure disconnect if retry_on_failure # Maybe there was a hiccup -- try reconnecting one more time retry_on_failure = false retry else # Nope, we failed to connect twice. Flag the node as down and re-raise # the exception. down! raise end rescue # Looks like we got an unexpected error, so we'll clean up the connection # and re-raise the exception. disconnect raise $!.extend(Errors::SocketError) end ensure Threaded.end(:connection) end |
#ensure_primary ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set a flag on the node for the duration of provided block so that an exception is raised if the node is no longer the primary node.
193 194 195 196 197 198 |
# File 'lib/moped/node.rb', line 193 def ensure_primary Threaded.begin(:ensure_primary) yield ensure Threaded.end(:ensure_primary) end |
#get_more(database, collection, cursor_id, limit) ⇒ Message
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute a get more operation on the node.
214 215 216 217 218 |
# File 'lib/moped/node.rb', line 214 def get_more(database, collection, cursor_id, limit) reply = process(Protocol::GetMore.new(database, collection, cursor_id, limit)) raise Moped::Errors::CursorNotFound.new("GET MORE", cursor_id) if reply.cursor_not_found? reply end |
#hash ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the hash identifier for the node.
228 229 230 |
# File 'lib/moped/node.rb', line 228 def hash resolved_address.hash end |
#insert(database, collection, documents, options = {}) ⇒ Message
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Insert documents into the database.
264 265 266 |
# File 'lib/moped/node.rb', line 264 def insert(database, collection, documents, = {}) process(Protocol::Insert.new(database, collection, documents, )) end |
#inspect ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the node as a nice formatted string.
485 486 487 |
# File 'lib/moped/node.rb', line 485 def inspect "<#{self.class.name} resolved_address=#{@resolved_address.inspect}>" end |
#kill_cursors(cursor_ids) ⇒ Message
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Kill all provided cursors on the node.
278 279 280 |
# File 'lib/moped/node.rb', line 278 def kill_cursors(cursor_ids) process(Protocol::KillCursors.new(cursor_ids)) end |
#needs_refresh?(time) ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Does the node need to be refreshed?
292 293 294 |
# File 'lib/moped/node.rb', line 292 def needs_refresh?(time) !refreshed_at || refreshed_at < time end |
#passive? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the node passive?
348 349 350 |
# File 'lib/moped/node.rb', line 348 def passive? @passive end |
#pipeline ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute a pipeline of commands, for example a safe mode persist.
306 307 308 309 310 311 312 313 314 |
# File 'lib/moped/node.rb', line 306 def pipeline Threaded.begin(:pipeline) begin yield ensure Threaded.end(:pipeline) end flush unless Threaded.executing?(:pipeline) end |
#primary? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the node the replica set primary?
324 325 326 |
# File 'lib/moped/node.rb', line 324 def primary? @primary end |
#query(database, collection, selector, options = {}) ⇒ Message
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute a query on the node.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/moped/node.rb', line 367 def query(database, collection, selector, = {}) operation = Protocol::Query.new(database, collection, selector, ) process(operation) do |reply| if reply.query_failed? if reply. && auth.has_key?(database) # If we got here, most likely this is the case of Moped # authenticating successfully against the node originally, but the # node has been reset or gone down and come back up. The most # common case here is a rs.stepDown() which will reinitialize the # connection. In this case we need to requthenticate and try again, # otherwise we'll just raise the error to the user. login(database, *auth[database]) reply = query(database, collection, selector, ) else raise Errors::QueryFailure.new(operation, reply.documents.first) end end reply end end |
#refresh ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Refresh information about the node, such as it’s status in the replica set and it’s known peers.
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/moped/node.rb', line 403 def refresh if resolve_address begin @refreshed_at = Time.now info = command("admin", ismaster: 1) primary = true if info["ismaster"] secondary = true if info["secondary"] generate_peers(info) @primary, @secondary = primary, secondary @arbiter = info["arbiterOnly"] @passive = info["passive"] if !primary && Threaded.executing?(:ensure_primary) raise Errors::ReplicaSetReconfigured.new("#{inspect} is no longer the primary node.", {}) elsif !primary && !secondary # not primary or secondary so mark it as down, since it's probably # a recovering node withing the replica set down! end rescue Timeout::Error @peers = [] down! end end end |
#remove(database, collection, selector, options = {}) ⇒ Message
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute a remove command for the provided selector.
443 444 445 |
# File 'lib/moped/node.rb', line 443 def remove(database, collection, selector, = {}) process(Protocol::Delete.new(database, collection, selector, )) end |
#secondary? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the node a replica set secondary?
455 456 457 |
# File 'lib/moped/node.rb', line 455 def secondary? @secondary end |
#update(database, collection, selector, change, options = {}) ⇒ Message
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute an update command for the provided selector.
473 474 475 |
# File 'lib/moped/node.rb', line 473 def update(database, collection, selector, change, = {}) process(Protocol::Update.new(database, collection, selector, change, )) end |