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.
Note: Unknown servers do not have wire versions, but for legacy reasons we return 0 for min_wire_version and max_wire_version of any server that does not have them. Presently the driver sometimes constructs commands when the server is unknown, so references to min_wire_version and max_wire_version should not be nil. When driver behavior is changed (jira.mongodb.org/browse/RUBY-1805), this may no longer be necessary.
Defined Under Namespace
Classes: Features
Constant Summary collapse
- ARBITER =
Deprecated.
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 =
Deprecated.
Constant for the key for the message value.
'msg'.freeze
- MONGOS_MESSAGE =
Deprecated.
Constant for the message that indicates a sharded cluster.
'isdbgrid'.freeze
- REPLICA_SET =
Deprecated.
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 =
Deprecated.
Constant for reading primary info from config.
'ismaster'.freeze
- PRIMARY_HOST =
Constant for reading primary host field from config.
'primary'.freeze
- SECONDARY =
Deprecated.
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 this server description was created.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Check equality of two descriptions.
-
#arbiter? ⇒ true, false
Returns whether this server is an arbiter, per the SDAM spec.
-
#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
Whether this server is a ghost, per the SDAM spec.
-
#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 deprecated Deprecated.
-
#last_write_date ⇒ Time
Get the lastWriteDate from the lastWrite subdocument in the config.
- #lists_server?(server) ⇒ true, false deprecated Deprecated.
-
#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
Returns whether this server is a mongos, per the SDAM spec.
- #ok? ⇒ Boolean private
-
#op_time ⇒ BSON::Timestamp
opTime in lastWrite subdocument of the ismaster response.
-
#other? ⇒ true, false
Returns whether the server is an other, per the SDAM spec.
-
#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
Returns whether this server is a primary, per the SDAM spec.
-
#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
Returns whether this server is a secondary, per the SDAM spec.
-
#server_type ⇒ Symbol
Returns the server type as a symbol.
- #server_version_gte?(version) ⇒ Boolean private
-
#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
Returns whether this server is a standalone, per the SDAM spec.
-
#tags ⇒ Hash
Get the tags configured for the server.
-
#unknown? ⇒ true, false
Returns whether this server is an unknown, per the SDAM spec.
-
#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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/mongo/server/description.rb', line 205 def initialize(address, config = {}, average_round_trip_time = nil) @address = address @config = config @features = Features.new(wire_versions, me || @address.to_s) @average_round_trip_time = average_round_trip_time @last_update_time = Time.now.dup.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.
223 224 225 |
# File 'lib/mongo/server/description.rb', line 223 def address @address end |
#average_round_trip_time ⇒ Float (readonly)
Returns The moving average time the ismaster call took to complete.
234 235 236 |
# File 'lib/mongo/server/description.rb', line 234 def average_round_trip_time @average_round_trip_time end |
#config ⇒ Hash (readonly)
Returns The actual result from the ismaster command.
226 227 228 |
# File 'lib/mongo/server/description.rb', line 226 def config @config end |
#last_update_time ⇒ Time (readonly)
This time does not indicate when a successful server check
Time when this server description was created.
completed, because marking a server unknown updates its description and last_update_time. Use Server#last_scan to find out when the server was last successfully checked by its Monitor.
714 715 716 |
# File 'lib/mongo/server/description.rb', line 714 def last_update_time @last_update_time end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Check equality of two descriptions.
726 727 728 729 730 731 732 733 |
# File 'lib/mongo/server/description.rb', line 726 def ==(other) return false if self.class != other.class return false if unknown? || other.unknown? (config.keys + other.config.keys).uniq.all? do |k| config[k] == other.config[k] || EXCLUDE_FOR_COMPARISON.include?(k) end end |
#arbiter? ⇒ true, false
Returns whether this server is an arbiter, per the SDAM spec.
244 245 246 247 248 |
# File 'lib/mongo/server/description.rb', line 244 def arbiter? ok? && config['arbiterOnly'] == true && !!config['setName'] end |
#arbiters ⇒ Array<String>
Get a list of all arbiters in the replica set.
258 259 260 |
# File 'lib/mongo/server/description.rb', line 258 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).
677 678 679 |
# File 'lib/mongo/server/description.rb', line 677 def data_bearing? mongos? || primary? || secondary? || standalone? end |
#election_id ⇒ BSON::ObjectId
Get the electionId from the config.
406 407 408 |
# File 'lib/mongo/server/description.rb', line 406 def election_id config[ELECTION_ID] end |
#features ⇒ Features
Returns features The features for the server.
229 230 231 |
# File 'lib/mongo/server/description.rb', line 229 def features @features end |
#ghost? ⇒ true, false
Whether this server is a ghost, per the SDAM spec.
270 271 272 273 |
# File 'lib/mongo/server/description.rb', line 270 def ghost? ok? && config['isreplicaset'] == true end |
#hidden? ⇒ true, false
Will return true if the server is hidden.
283 284 285 |
# File 'lib/mongo/server/description.rb', line 283 def hidden? ok? && !!config[HIDDEN] end |
#hosts ⇒ Array<String>
Get a list of all servers in the replica set.
295 296 297 |
# File 'lib/mongo/server/description.rb', line 295 def hosts @hosts ||= (config[HOSTS] || []).map { |s| s.downcase } end |
#inspect ⇒ String
Inspect the server description.
307 308 309 |
# File 'lib/mongo/server/description.rb', line 307 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.
639 640 641 |
# File 'lib/mongo/server/description.rb', line 639 def is_server?(server) address == server.address end |
#last_write_date ⇒ Time
Get the lastWriteDate from the lastWrite subdocument in the config.
430 431 432 |
# File 'lib/mongo/server/description.rb', line 430 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.
653 654 655 |
# File 'lib/mongo/server/description.rb', line 653 def lists_server?(server) servers.include?(server.address.to_s) end |
#logical_session_timeout ⇒ Integer?
Get the logicalSessionTimeoutMinutes from the config.
442 443 444 |
# File 'lib/mongo/server/description.rb', line 442 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.
319 320 321 |
# File 'lib/mongo/server/description.rb', line 319 def max_bson_object_size config[MAX_BSON_OBJECT_SIZE] end |
#max_message_size ⇒ Integer
Get the max message size for this server version.
331 332 333 |
# File 'lib/mongo/server/description.rb', line 331 def config[MAX_MESSAGE_BYTES] end |
#max_wire_version ⇒ Integer
Get the maximum wire version. Defaults to zero.
355 356 357 |
# File 'lib/mongo/server/description.rb', line 355 def max_wire_version config[MAX_WIRE_VERSION] || 0 end |
#max_write_batch_size ⇒ Integer
Get the maximum batch size for writes.
343 344 345 |
# File 'lib/mongo/server/description.rb', line 343 def max_write_batch_size config[MAX_WRITE_BATCH_SIZE] || DEFAULT_MAX_WRITE_BATCH_SIZE end |
#me ⇒ String
The value in me field may differ from the server description’s address. This can happen, for example, in split horizon configurations. The SDAM spec only requires removing servers whose me does not match their address in some of the situations (e.g. when the server in question is an RS member but not a primary).
Get the me field value.
382 383 384 |
# File 'lib/mongo/server/description.rb', line 382 def me config[ME] end |
#me_mismatch? ⇒ true, false
Check if there is a mismatch between the address host and the me field.
689 690 691 |
# File 'lib/mongo/server/description.rb', line 689 def me_mismatch? !!(address.to_s.downcase != me.downcase if me) end |
#min_wire_version ⇒ Integer
Get the minimum wire version. Defaults to zero.
367 368 369 |
# File 'lib/mongo/server/description.rb', line 367 def min_wire_version config[MIN_WIRE_VERSION] || 0 end |
#mongos? ⇒ true, false
Returns whether this server is a mongos, per the SDAM spec.
454 455 456 |
# File 'lib/mongo/server/description.rb', line 454 def mongos? ok? && config['msg'] == 'isdbgrid' 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.
613 614 615 616 |
# File 'lib/mongo/server/description.rb', line 613 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.
698 699 700 701 702 |
# File 'lib/mongo/server/description.rb', line 698 def op_time if config['lastWrite'] && config['lastWrite']['opTime'] config['lastWrite']['opTime']['ts'] end end |
#other? ⇒ true, false
Returns whether the server is an other, per the SDAM spec.
466 467 468 469 470 471 472 473 474 475 |
# File 'lib/mongo/server/description.rb', line 466 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". ok? && !!config['setName'] && ( config['hidden'] == true || !primary? && !secondary? && !arbiter? ) end |
#passive? ⇒ true, false
Will return true if the server is passive.
485 486 487 |
# File 'lib/mongo/server/description.rb', line 485 def passive? ok? && !!config[PASSIVE] end |
#passives ⇒ Array<String>
Get a list of the passive servers in the cluster.
497 498 499 |
# File 'lib/mongo/server/description.rb', line 497 def passives @passives ||= (config[PASSIVES] || []).map { |s| s.downcase } end |
#primary? ⇒ true, false
Returns whether this server is a primary, per the SDAM spec.
521 522 523 524 525 |
# File 'lib/mongo/server/description.rb', line 521 def primary? ok? && config['ismaster'] == true && !!config['setName'] end |
#primary_host ⇒ String | nil
Get the address of the primary host.
509 510 511 |
# File 'lib/mongo/server/description.rb', line 509 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.
666 667 668 |
# File 'lib/mongo/server/description.rb', line 666 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.
536 537 538 |
# File 'lib/mongo/server/description.rb', line 536 def replica_set_name config[SET_NAME] end |
#secondary? ⇒ true, false
Returns whether this server is a secondary, per the SDAM spec.
560 561 562 563 564 |
# File 'lib/mongo/server/description.rb', line 560 def secondary? ok? && config['secondary'] == true && !!config['setName'] end |
#server_type ⇒ Symbol
Returns the server type as a symbol.
574 575 576 577 578 579 580 581 582 583 |
# File 'lib/mongo/server/description.rb', line 574 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 |
#server_version_gte?(version) ⇒ 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.
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
# File 'lib/mongo/server/description.rb', line 737 def server_version_gte?(version) required_wv = case version when '4.2' 8 when '4.0' 7 when '3.6' 6 when '3.4' 5 when '3.2' 4 when '3.0' 3 when '2.6' 2 else raise ArgumentError, "Bogus required version #{version}" end required_wv >= min_wire_version && required_wv <= max_wire_version end |
#servers ⇒ Array<String>
Get a list of all servers known to the cluster.
548 549 550 |
# File 'lib/mongo/server/description.rb', line 548 def servers hosts + arbiters + passives end |
#set_version ⇒ Integer
Get the setVersion from the config.
418 419 420 |
# File 'lib/mongo/server/description.rb', line 418 def set_version config[SET_VERSION] end |
#standalone? ⇒ true, false
Returns whether this server is a standalone, per the SDAM spec.
593 594 595 596 597 598 |
# File 'lib/mongo/server/description.rb', line 593 def standalone? ok? && config['msg'] != 'isdbgrid' && config['setName'].nil? && config['isreplicaset'] != true end |
#tags ⇒ Hash
Get the tags configured for the server.
394 395 396 |
# File 'lib/mongo/server/description.rb', line 394 def config[TAGS] || {} end |
#unknown? ⇒ true, false
Returns whether this server is an unknown, per the SDAM spec.
608 609 610 |
# File 'lib/mongo/server/description.rb', line 608 def unknown? config.empty? || !ok? end |
#wire_versions ⇒ Range
Get the range of supported wire versions for the server.
626 627 628 |
# File 'lib/mongo/server/description.rb', line 626 def wire_versions min_wire_version..max_wire_version end |