Class: Mongo::Server::Description
- Inherits:
-
Object
- Object
- Mongo::Server::Description
- Defined in:
- lib/mongo/server/description.rb,
lib/mongo/server/description/features.rb
Overview
Represents a description of the server, populated by the result of the ismaster command.
Defined Under Namespace
Classes: Features
Constant Summary collapse
- ARBITER =
Constant for reading arbiter info from config.
'arbiterOnly'.freeze
- ARBITERS =
Constant for reading arbiters info from config.
'arbiters'.freeze
- HIDDEN =
Constant for reading hidden info from config.
'hidden'.freeze
- HOSTS =
Constant for reading hosts info from config.
'hosts'.freeze
- MESSAGE =
Constant for the key for the message value.
'msg'.freeze
- MONGOS_MESSAGE =
Constant for the message that indicates a sharded cluster.
'isdbgrid'.freeze
- REPLICA_SET =
Constant for determining ghost servers.
'isreplicaset'.freeze
- MAX_BSON_OBJECT_SIZE =
Constant for reading max bson size info from config.
'maxBsonObjectSize'.freeze
- MAX_MESSAGE_BYTES =
Constant for reading max message size info from config.
'maxMessageSizeBytes'.freeze
- MAX_WIRE_VERSION =
Constant for the max wire version.
'maxWireVersion'.freeze
- MIN_WIRE_VERSION =
Constant for min wire version.
'minWireVersion'.freeze
- MAX_WRITE_BATCH_SIZE =
Constant for reading max write batch size.
'maxWriteBatchSize'.freeze
- LAST_WRITE =
Constant for the lastWrite subdocument.
'lastWrite'.freeze
- LAST_WRITE_DATE =
Constant for the lastWriteDate field in the lastWrite subdocument.
'lastWriteDate'.freeze
- ME =
Constant for reading the me field.
'me'.freeze
- DEFAULT_MAX_WRITE_BATCH_SIZE =
Default max write batch size.
1000.freeze
- LEGACY_WIRE_VERSION =
Deprecated.
Will be removed in 3.0.
The legacy wire protocol version.
0.freeze
- PASSIVE =
Constant for reading passive info from config.
'passive'.freeze
- PASSIVES =
Constant for reading the passive server list.
'passives'.freeze
- PRIMARY =
Constant for reading primary info from config.
'ismaster'.freeze
- PRIMARY_HOST =
Constant for reading primary host field from config.
'primary'.freeze
- SECONDARY =
Constant for reading secondary info from config.
'secondary'.freeze
- SET_NAME =
Constant for reading replica set name info from config.
'setName'.freeze
- TAGS =
Constant for reading tags info from config.
'tags'.freeze
- ELECTION_ID =
Constant for reading electionId info from config.
'electionId'.freeze
- SET_VERSION =
Constant for reading setVersion info from config.
'setVersion'.freeze
- LOCAL_TIME =
Constant for reading localTime info from config.
'localTime'.freeze
- OPERATION_TIME =
Constant for reading operationTime info from config.
'operationTime'.freeze
- LOGICAL_SESSION_TIMEOUT_MINUTES =
Constant for reading logicalSessionTimeoutMinutes info from config.
'logicalSessionTimeoutMinutes'.freeze
- EXCLUDE_FOR_COMPARISON =
Fields to exclude when comparing two descriptions.
[ LOCAL_TIME, LAST_WRITE, OPERATION_TIME, Operation::CLUSTER_TIME ].freeze
Instance Attribute Summary collapse
-
#address ⇒ Address
readonly
Address The server’s address.
-
#average_round_trip_time ⇒ Float
readonly
The moving average time the ismaster call took to complete.
-
#config ⇒ Hash
readonly
The actual result from the ismaster command.
-
#last_update_time ⇒ Time
readonly
Time when the server was last checked.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Check equality of two descriptions.
-
#arbiter? ⇒ true, false
Will return true if the server is an arbiter.
-
#arbiters ⇒ Array<String>
Get a list of all arbiters in the replica set.
-
#data_bearing? ⇒ true, false
Whether this description is from a data-bearing server (standalone, mongos, primary or secondary).
-
#election_id ⇒ BSON::ObjectId
Get the electionId from the config.
-
#features ⇒ Features
Features The features for the server.
-
#ghost? ⇒ true, false
Is the server a ghost in a replica set?.
-
#hidden? ⇒ true, false
Will return true if the server is hidden.
-
#hosts ⇒ Array<String>
Get a list of all servers in the replica set.
-
#initialize(address, config = {}, average_round_trip_time = nil) ⇒ Description
constructor
Instantiate the new server description from the result of the ismaster command.
-
#inspect ⇒ String
Inspect the server description.
-
#is_server?(server) ⇒ true, false
Is this description from the given server.
-
#last_write_date ⇒ Time
Get the lastWriteDate from the lastWrite subdocument in the config.
-
#lists_server?(server) ⇒ true, false
Is a server included in this description’s list of servers.
-
#logical_session_timeout ⇒ Integer?
Get the logicalSessionTimeoutMinutes from the config.
-
#max_bson_object_size ⇒ Integer
Get the max BSON object size for this server version.
-
#max_message_size ⇒ Integer
Get the max message size for this server version.
-
#max_wire_version ⇒ Integer
Get the maximum wire version.
-
#max_write_batch_size ⇒ Integer
Get the maximum batch size for writes.
-
#me ⇒ String
Get the me field value.
-
#me_mismatch? ⇒ true, false
Check if there is a mismatch between the address host and the me field.
-
#min_wire_version ⇒ Integer
Get the minimum wire version.
-
#mongos? ⇒ true, false
Is the server a mongos?.
- #ok? ⇒ Boolean private
-
#op_time ⇒ BSON::Timestamp
opTime in lastWrite subdocument of the ismaster response.
-
#other? ⇒ true, false
Is the description of type other.
-
#passive? ⇒ true, false
Will return true if the server is passive.
-
#passives ⇒ Array<String>
Get a list of the passive servers in the cluster.
-
#primary? ⇒ true, false
Will return true if the server is a primary.
-
#primary_host ⇒ String | nil
Get the address of the primary host.
-
#replica_set_member? ⇒ true, false
Does this description correspond to a replica set member.
-
#replica_set_name ⇒ String?
Get the name of the replica set the server belongs to, returns nil if none.
-
#secondary? ⇒ true, false
Will return true if the server is a secondary.
-
#server_type ⇒ Symbol
Returns the server type as a symbol.
-
#servers ⇒ Array<String>
Get a list of all servers known to the cluster.
-
#set_version ⇒ Integer
Get the setVersion from the config.
-
#standalone? ⇒ true, false
Is this server a standalone server?.
-
#tags ⇒ Hash
Get the tags configured for the server.
-
#unknown? ⇒ true, false
Is the server description currently unknown?.
-
#wire_versions ⇒ Range
Get the range of supported wire versions for the server.
Constructor Details
#initialize(address, config = {}, average_round_trip_time = nil) ⇒ Description
Instantiate the new server description from the result of the ismaster command.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/mongo/server/description.rb', line 192 def initialize(address, config = {}, average_round_trip_time = nil) @address = address @config = config unless unknown? @features = Features.new(wire_versions, me || @address.to_s) end @average_round_trip_time = average_round_trip_time @last_update_time = Time.now.freeze if Mongo::Lint.enabled? # prepopulate cache instance variables hosts arbiters passives freeze end end |
Instance Attribute Details
#address ⇒ Address (readonly)
Returns address The server’s address.
212 213 214 |
# File 'lib/mongo/server/description.rb', line 212 def address @address end |
#average_round_trip_time ⇒ Float (readonly)
Returns The moving average time the ismaster call took to complete.
226 227 228 |
# File 'lib/mongo/server/description.rb', line 226 def average_round_trip_time @average_round_trip_time end |
#config ⇒ Hash (readonly)
Returns The actual result from the ismaster command.
215 216 217 |
# File 'lib/mongo/server/description.rb', line 215 def config @config end |
#last_update_time ⇒ Time (readonly)
Time when the server was last checked.
689 690 691 |
# File 'lib/mongo/server/description.rb', line 689 def last_update_time @last_update_time end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Check equality of two descriptions.
701 702 703 704 705 |
# File 'lib/mongo/server/description.rb', line 701 def ==(other) return false if self.class != other.class return false if unknown? || other.unknown? compare_config(other) end |
#arbiter? ⇒ true, false
Will return true if the server is an arbiter.
236 237 238 |
# File 'lib/mongo/server/description.rb', line 236 def arbiter? ok? && !!config[ARBITER] && !replica_set_name.nil? end |
#arbiters ⇒ Array<String>
Get a list of all arbiters in the replica set.
248 249 250 |
# File 'lib/mongo/server/description.rb', line 248 def arbiters @arbiters ||= (config[ARBITERS] || []).map { |s| s.downcase } end |
#data_bearing? ⇒ true, false
Whether this description is from a data-bearing server (standalone, mongos, primary or secondary).
657 658 659 |
# File 'lib/mongo/server/description.rb', line 657 def data_bearing? mongos? || primary? || secondary? || standalone? end |
#election_id ⇒ BSON::ObjectId
Get the electionId from the config.
392 393 394 |
# File 'lib/mongo/server/description.rb', line 392 def election_id config[ELECTION_ID] end |
#features ⇒ Features
Returns features The features for the server.
218 219 220 221 222 223 |
# File 'lib/mongo/server/description.rb', line 218 def features if unknown? raise ArgumentError, "An unknown server's features are not known" end @features end |
#ghost? ⇒ true, false
Is the server a ghost in a replica set?
260 261 262 |
# File 'lib/mongo/server/description.rb', line 260 def ghost? ok? && !!config[REPLICA_SET] end |
#hidden? ⇒ true, false
Will return true if the server is hidden.
272 273 274 |
# File 'lib/mongo/server/description.rb', line 272 def hidden? ok? && !!config[HIDDEN] end |
#hosts ⇒ Array<String>
Get a list of all servers in the replica set.
284 285 286 |
# File 'lib/mongo/server/description.rb', line 284 def hosts @hosts ||= (config[HOSTS] || []).map { |s| s.downcase } end |
#inspect ⇒ String
Inspect the server description.
296 297 298 |
# File 'lib/mongo/server/description.rb', line 296 def inspect "#<Mongo::Server:Description:0x#{object_id} config=#{config} average_round_trip_time=#{average_round_trip_time}>" end |
#is_server?(server) ⇒ true, false
Is this description from the given server.
620 621 622 |
# File 'lib/mongo/server/description.rb', line 620 def is_server?(server) address == server.address end |
#last_write_date ⇒ Time
Get the lastWriteDate from the lastWrite subdocument in the config.
416 417 418 |
# File 'lib/mongo/server/description.rb', line 416 def last_write_date config[LAST_WRITE][LAST_WRITE_DATE] if config[LAST_WRITE] end |
#lists_server?(server) ⇒ true, false
Is a server included in this description’s list of servers.
633 634 635 |
# File 'lib/mongo/server/description.rb', line 633 def lists_server?(server) servers.include?(server.address.to_s) end |
#logical_session_timeout ⇒ Integer?
Get the logicalSessionTimeoutMinutes from the config.
428 429 430 |
# File 'lib/mongo/server/description.rb', line 428 def logical_session_timeout config[LOGICAL_SESSION_TIMEOUT_MINUTES] if config[LOGICAL_SESSION_TIMEOUT_MINUTES] end |
#max_bson_object_size ⇒ Integer
Get the max BSON object size for this server version.
308 309 310 |
# File 'lib/mongo/server/description.rb', line 308 def max_bson_object_size config[MAX_BSON_OBJECT_SIZE] end |
#max_message_size ⇒ Integer
Get the max message size for this server version.
320 321 322 |
# File 'lib/mongo/server/description.rb', line 320 def config[MAX_MESSAGE_BYTES] end |
#max_wire_version ⇒ Integer
Get the maximum wire version.
344 345 346 |
# File 'lib/mongo/server/description.rb', line 344 def max_wire_version config[MAX_WIRE_VERSION] end |
#max_write_batch_size ⇒ Integer
Get the maximum batch size for writes.
332 333 334 |
# File 'lib/mongo/server/description.rb', line 332 def max_write_batch_size config[MAX_WRITE_BATCH_SIZE] || DEFAULT_MAX_WRITE_BATCH_SIZE end |
#me ⇒ String
Get the me field value.
368 369 370 |
# File 'lib/mongo/server/description.rb', line 368 def me config[ME] end |
#me_mismatch? ⇒ true, false
Check if there is a mismatch between the address host and the me field.
669 670 671 |
# File 'lib/mongo/server/description.rb', line 669 def me_mismatch? !!(address.to_s.downcase != me.downcase if me) end |
#min_wire_version ⇒ Integer
Get the minimum wire version.
356 357 358 |
# File 'lib/mongo/server/description.rb', line 356 def min_wire_version config[MIN_WIRE_VERSION] end |
#mongos? ⇒ true, false
Is the server a mongos?
440 441 442 |
# File 'lib/mongo/server/description.rb', line 440 def mongos? ok? && config[MESSAGE] == MONGOS_MESSAGE end |
#ok? ⇒ Boolean
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.
595 596 597 598 |
# File 'lib/mongo/server/description.rb', line 595 def ok? config[Operation::Result::OK] && config[Operation::Result::OK] == 1 || false end |
#op_time ⇒ BSON::Timestamp
opTime in lastWrite subdocument of the ismaster response.
678 679 680 681 682 |
# File 'lib/mongo/server/description.rb', line 678 def op_time if config['lastWrite'] && config['lastWrite']['opTime'] config['lastWrite']['opTime']['ts'] end end |
#other? ⇒ true, false
Is the description of type other.
452 453 454 455 456 457 458 459 460 461 |
# File 'lib/mongo/server/description.rb', line 452 def other? # The SDAM spec is slightly confusing on what "other" means, # but it's referred to it as "RSOther" which means a non-RS member # cannot be "other". if unknown? || replica_set_name.nil? return false end (!primary? && !secondary? && !passive? && !arbiter?) || (hidden? && !replica_set_name.nil?) end |
#passive? ⇒ true, false
Will return true if the server is passive.
471 472 473 |
# File 'lib/mongo/server/description.rb', line 471 def passive? ok? && !!config[PASSIVE] end |
#passives ⇒ Array<String>
Get a list of the passive servers in the cluster.
483 484 485 |
# File 'lib/mongo/server/description.rb', line 483 def passives @passives ||= (config[PASSIVES] || []).map { |s| s.downcase } end |
#primary? ⇒ true, false
Will return true if the server is a primary.
507 508 509 510 511 512 |
# File 'lib/mongo/server/description.rb', line 507 def primary? ok? && !!config[PRIMARY] && (primary_host.nil? || primary_host == address.to_s) && !replica_set_name.nil? end |
#primary_host ⇒ String | nil
Get the address of the primary host.
495 496 497 |
# File 'lib/mongo/server/description.rb', line 495 def primary_host config[PRIMARY_HOST] && config[PRIMARY_HOST].downcase end |
#replica_set_member? ⇒ true, false
Does this description correspond to a replica set member.
646 647 648 |
# File 'lib/mongo/server/description.rb', line 646 def replica_set_member? ok? && !(standalone? || mongos?) end |
#replica_set_name ⇒ String?
Get the name of the replica set the server belongs to, returns nil if none.
523 524 525 |
# File 'lib/mongo/server/description.rb', line 523 def replica_set_name config[SET_NAME] end |
#secondary? ⇒ true, false
Will return true if the server is a secondary.
547 548 549 |
# File 'lib/mongo/server/description.rb', line 547 def secondary? ok? && !!config[SECONDARY] && !replica_set_name.nil? end |
#server_type ⇒ Symbol
Returns the server type as a symbol.
559 560 561 562 563 564 565 566 567 568 |
# File 'lib/mongo/server/description.rb', line 559 def server_type return :arbiter if arbiter? return :ghost if ghost? return :sharded if mongos? return :primary if primary? return :secondary if secondary? return :standalone if standalone? return :other if other? :unknown end |
#servers ⇒ Array<String>
Get a list of all servers known to the cluster.
535 536 537 |
# File 'lib/mongo/server/description.rb', line 535 def servers hosts + arbiters + passives end |
#set_version ⇒ Integer
Get the setVersion from the config.
404 405 406 |
# File 'lib/mongo/server/description.rb', line 404 def set_version config[SET_VERSION] end |
#standalone? ⇒ true, false
Is this server a standalone server?
578 579 580 |
# File 'lib/mongo/server/description.rb', line 578 def standalone? replica_set_name.nil? && !mongos? && !ghost? && !unknown? end |
#tags ⇒ Hash
Get the tags configured for the server.
380 381 382 |
# File 'lib/mongo/server/description.rb', line 380 def config[TAGS] || {} end |
#unknown? ⇒ true, false
Is the server description currently unknown?
590 591 592 |
# File 'lib/mongo/server/description.rb', line 590 def unknown? config.empty? || !ok? end |
#wire_versions ⇒ Range
Get the range of supported wire versions for the server.
608 609 610 |
# File 'lib/mongo/server/description.rb', line 608 def wire_versions min_wire_version..max_wire_version end |