123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Server::Description

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/mongo/server/description.rb,
lib/mongo/server/description/features.rb,
lib/mongo/server/description/load_balancer.rb

Overview

Represents a description of the server, populated by the result of the hello 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 (https://jira.mongodb.org/browse/RUBY-1805), this may no longer be necessary.

Since:

  • 2.0.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#addressAddress (readonly)

Returns:

  • (Address)

    address The server's address.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 302

attr_reader :address

#arbiter?true, false (readonly)

Returns whether this server is an arbiter, per the SDAM spec.

Examples:

Is the server an arbiter?

description.arbiter?

Returns:

  • (true, false)

    If the server is an arbiter.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 332

def arbiter?
  ok? &&
    config['arbiterOnly'] == true &&
    !!config['setName']
end

#average_round_trip_timeFloat (readonly)

Returns:

  • (Float)

    The moving average time the hello call took to complete.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 318

attr_reader :average_round_trip_time

#configHash (readonly)

Returns:

  • (Hash)

    The actual result from the hello command.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 305

attr_reader :config

#data_bearing?true, false (readonly)

Whether this description is from a data-bearing server (standalone, mongos, primary or secondary).

Returns:

  • (true, false)

    Whether the description is from a data-bearing server.

Since:

  • 2.7.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 809

def data_bearing?
  mongos? || primary? || secondary? || standalone?
end

#featuresFeatures (readonly)

Returns:

  • (Features)

    features The features for the server.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 315

attr_reader :features

#ghost?true, false (readonly)

Whether this server is a ghost, per the SDAM spec.

Examples:

Is the server a ghost?

description.ghost?

Returns:

  • (true, false)

    If the server is a ghost.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 358

def ghost?
  ok? &&
    config['isreplicaset'] == true
end

#hidden?true, false (readonly)

Will return true if the server is hidden.

Examples:

Is the server hidden?

description.hidden?

Returns:

  • (true, false)

    If the server is hidden.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 371

def hidden?
  ok? && !!config[HIDDEN]
end

#last_update_monotimeFloat (readonly)

This method is for internal use only.

Time when this server description was created according to monotonic clock.

Returns:

See Also:

  • for more detail

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 863

attr_reader :last_update_monotime

#last_update_timeTime (readonly)

Note:

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.

Returns:

Since:

  • 2.7.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 854

attr_reader :last_update_time

#load_balancer?true | false (readonly)

Returns whether this server is a load balancer.

Returns:

  • (true | false)

    Whether this server is a load balancer.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 310

def load_balancer?
  @load_balancer
end

#me_mismatch?true, false (readonly)

Check if there is a mismatch between the address host and the me field.

Examples:

Check if there is a mismatch.

description.me_mismatch?

Returns:

  • (true, false)

    If there is a mismatch between the me field and the address host.

Since:

  • 2.0.6

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 821

def me_mismatch?
  !!(address.to_s.downcase != me.downcase if me)
end

#minimum_round_trip_timeFloat (readonly)

Returns:

  • (Float)

    The minimum time that the ten last hello calls took to complete.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 322

attr_reader :minimum_round_trip_time

#mongocryptd?true, false (readonly)

Whether this description is from a mongocryptd server.

Returns:

  • (true, false)

    Whether this description is from a mongocryptd server.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 829

def mongocryptd?
  ok? && config['iscryptd'] == true
end

#mongos?true, false (readonly)

Returns whether this server is a mongos, per the SDAM spec.

Examples:

Is the server a mongos?

description.mongos?

Returns:

  • (true, false)

    If the server is a mongos.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 583

def mongos?
  ok? && config['msg'] == 'isdbgrid'
end

#ok?Boolean (readonly)

This method is for internal use only.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 746

def ok?
  config[Operation::Result::OK] == 1
end

#other?true, false (readonly)

Returns whether the server is an other, per the SDAM spec.

Examples:

Is the description of type other.

description.other?

Returns:

  • (true, false)

    If the description is other.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 595

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 (readonly)

Will return true if the server is passive.

Examples:

Is the server passive?

description.passive?

Returns:

  • (true, false)

    If the server is passive.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 614

def passive?
  ok? && !!config[PASSIVE]
end

#primary?true, false (readonly)

Returns whether this server is a primary, per the SDAM spec.

Examples:

Is the server a primary?

description.primary?

Returns:

  • (true, false)

    If the server is a primary.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 650

def primary?
  ok? &&
    (config['ismaster'] == true || config['isWritablePrimary'] == true) &&
    !!config['setName']
end

#replica_set_member?true, false (readonly)

Does this description correspond to a replica set member.

Examples:

Check if the description is from a replica set member.

description.replica_set_member?

Returns:

  • (true, false)

    If the description is from a replica set member.

Since:

  • 2.0.6

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 798

def replica_set_member?
  ok? && !(standalone? || mongos?)
end

#secondary?true, false (readonly)

Returns whether this server is a secondary, per the SDAM spec.

Examples:

Is the server a secondary?

description.secondary?

Returns:

  • (true, false)

    If the server is a secondary.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 689

def secondary?
  ok? &&
    config['secondary'] == true &&
    !!config['setName']
end

#standalone?true, false (readonly)

Returns whether this server is a standalone, per the SDAM spec.

Examples:

Is the server standalone?

description.standalone?

Returns:

  • (true, false)

    If the server is standalone.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 724

def standalone?
  ok? &&
    config['msg'] != 'isdbgrid' &&
    config['setName'].nil? &&
    config['isreplicaset'] != true
end

#unknown?true, false (readonly)

Returns whether this server is an unknown, per the SDAM spec.

Examples:

Is the server description unknown?

description.unknown?

Returns:

  • (true, false)

    If the server description is unknown.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 739

def unknown?
  return false if load_balancer?

  config.empty? || config.keys == %w[topologyVersion] || !ok?
end

Instance Method Details

#==(other) ⇒ true, false Also known as: #eql?

Check equality of two descriptions.

Examples:

Check description equality.

description == other

Parameters:

  • other (Object)

    The other description.

Returns:

  • (true, false)

    Whether the objects are equal.

Since:

  • 2.0.6

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 887

def ==(other)
  return false if self.class != other.class
  return false if unknown? || other.unknown?
  return false if server_type != other.server_type

  (config.keys + other.config.keys).uniq.all? do |k|
    config[k] == other.config[k] || EXCLUDE_FOR_COMPARISON.include?(k)
  end
end

#arbitersArray<String>

Get a list of all arbiters in the replica set.

Examples:

Get the arbiters in the replica set.

description.arbiters

Returns:

  • (Array<String>)

    The arbiters in the set.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 346

def arbiters
  @arbiters ||= (config[ARBITERS] || []).map { |s| s.downcase }
end

#election_idBSON::ObjectId

Get the electionId from the config.

Examples:

Get the electionId.

description.election_id

Returns:

  • (BSON::ObjectId)

    The election id.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 494

def election_id
  config[ELECTION_ID]
end

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 896

alias eql? ==

#hostsArray<String>

Get a list of all servers in the replica set.

Examples:

Get the servers in the replica set.

description.hosts

Returns:

  • (Array<String>)

    The servers in the set.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 383

def hosts
  @hosts ||= (config[HOSTS] || []).map { |s| s.downcase }
end

#inspectString

Inspect the server description.

Examples:

Inspect the server description

description.inspect

Returns:

  • (String)

    The inspection.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 395

def inspect
  "#<Mongo::Server:Description:0x#{object_id} config=#{config} average_round_trip_time=#{average_round_trip_time}>"
end

#is_server?(server) ⇒ true, false

Deprecated.

Is this description from the given server.

Examples:

Check if the description is from a given server.

description.is_server?(server)

Returns:

  • (true, false)

    If the description is from the server.

Since:

  • 2.0.6

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 771

def is_server?(server)
  address == server.address
end

#last_write_dateTime

Get the lastWriteDate from the lastWrite subdocument in the config.

Examples:

Get the lastWriteDate value.

description.last_write_date

Returns:

  • (Time)

    The last write date.

Since:

  • 2.4.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 559

def last_write_date
  config[LAST_WRITE][LAST_WRITE_DATE] if config[LAST_WRITE]
end

#lists_server?(server) ⇒ true, false

Deprecated.

Is a server included in this description's list of servers.

Examples:

Check if a server is in the description list of servers.

description.lists_server?(server)

Returns:

  • (true, false)

    If a server is in the description's list of servers.

Since:

  • 2.0.6

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 785

def lists_server?(server)
  servers.include?(server.address.to_s)
end

#logical_session_timeoutInteger?

Get the logicalSessionTimeoutMinutes from the config.

Examples:

Get the logicalSessionTimeoutMinutes value in minutes.

description.logical_session_timeout

Returns:

  • (Integer, nil)

    The logical session timeout in minutes.

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 571

def logical_session_timeout
  config[LOGICAL_SESSION_TIMEOUT_MINUTES] if config[LOGICAL_SESSION_TIMEOUT_MINUTES]
end

#max_bson_object_sizeInteger

Get the max BSON object size for this server version.

Examples:

Get the max BSON object size.

description.max_bson_object_size

Returns:

  • (Integer)

    The maximum object size in bytes.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 407

def max_bson_object_size
  config[MAX_BSON_OBJECT_SIZE]
end

#max_message_sizeInteger

Get the max message size for this server version.

Examples:

Get the max message size.

description.max_message_size

Returns:

  • (Integer)

    The maximum message size in bytes.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 419

def max_message_size
  config[MAX_MESSAGE_BYTES]
end

#max_wire_versionInteger

Get the maximum wire version. Defaults to zero.

Examples:

Get the max wire version.

description.max_wire_version

Returns:

  • (Integer)

    The max wire version supported.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 443

def max_wire_version
  config[MAX_WIRE_VERSION] || 0
end

#max_write_batch_sizeInteger

Get the maximum batch size for writes.

Examples:

Get the max batch size.

description.max_write_batch_size

Returns:

  • (Integer)

    The max batch size.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 431

def max_write_batch_size
  config[MAX_WRITE_BATCH_SIZE] || DEFAULT_MAX_WRITE_BATCH_SIZE
end

#meString

Note:

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.

Returns:

  • (String)

    The me field.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 470

def me
  config[ME]
end

#min_wire_versionInteger

Get the minimum wire version. Defaults to zero.

Examples:

Get the min wire version.

description.min_wire_version

Returns:

  • (Integer)

    The min wire version supported.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 455

def min_wire_version
  config[MIN_WIRE_VERSION] || 0
end

#op_timeBSON::Timestamp

opTime in lastWrite subdocument of the hello response.

Returns:

  • (BSON::Timestamp)

    The timestamp.

Since:

  • 2.7.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 838

def op_time
  return unless config['lastWrite'] && config['lastWrite']['opTime']

  config['lastWrite']['opTime']['ts']
end

#passivesArray<String>

Get a list of the passive servers in the cluster.

Examples:

Get the passives.

description.passives

Returns:

  • (Array<String>)

    The list of passives.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 626

def passives
  @passives ||= (config[PASSIVES] || []).map { |s| s.downcase }
end

#primary_hostString | nil

Get the address of the primary host.

Examples:

Get the address of the primary.

description.primary_host

Returns:

  • (String | nil)

    The address of the primary.

Since:

  • 2.6.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 638

def primary_host
  config[PRIMARY_HOST] && config[PRIMARY_HOST].downcase
end

#replica_set_nameString?

Get the name of the replica set the server belongs to, returns nil if none.

Examples:

Get the replica set name.

description.replica_set_name

Returns:

  • (String, nil)

    The name of the replica set.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 665

def replica_set_name
  config[SET_NAME]
end

#server_connection_id

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 866

def server_connection_id
  config['connectionId']
end

#server_typeSymbol

Returns the server type as a symbol.

Examples:

Get the server type.

description.server_type

Returns:

  • (Symbol)

    The server type.

Since:

  • 2.4.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 703

def server_type
  return :load_balancer if load_balancer?
  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 for internal use only.

Raises:

  • (NotImplementedError)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 912

def server_version_gte?(version)
  if load_balancer?
    # If we are talking to a load balancer, there is no monitoring
    # and we don't know what server is behind the load balancer.
    # Assume everything is supported.
    # TODO remove this when RUBY-2220 is implemented.
    return true
  end

  required_wv = SERVER_VERSION_WIRE_VERSION_MAP[version]
  raise NotImplementedError, "Unexpected server version #{version.inspect}" unless required_wv

  required_wv <= max_wire_version
end

#serversArray<String>

Get a list of all servers known to the cluster.

Examples:

Get all servers.

description.servers

Returns:

  • (Array<String>)

    The list of all servers.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 677

def servers
  hosts + arbiters + passives
end

#service_idnil | Object

Returns:

  • (nil | Object)

    The service id, if any.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 873

def service_id
  config['serviceId']
end

#set_versionInteger

Get the setVersion from the config.

Examples:

Get the setVersion.

description.set_version

Returns:

  • (Integer)

    The set version.

Since:

  • 2.2.2

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 506

def set_version
  config[SET_VERSION]
end

#tagsHash

Get the tags configured for the server.

Examples:

Get the tags.

description.tags

Returns:

  • (Hash)

    The tags of the server.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 482

def tags
  config[TAGS] || {}
end

#topology_versionTopologyVersion | nil

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 511

def topology_version
  unless defined?(@topology_version)
    @topology_version = config['topologyVersion'] &&
                        TopologyVersion.new(config['topologyVersion'])
  end
  @topology_version
end

#topology_version_gt?(other_desc) ⇒ true | false

This method is for internal use only.

Returns whether topology version in this description is potentially newer than or equal to topology version in another description.

Parameters:

Returns:

  • (true | false)

    Whether topology version in this description is potentially newer or equal.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 527

def topology_version_gt?(other_desc)
  if topology_version.nil? || other_desc.topology_version.nil?
    true
  else
    topology_version.gt?(other_desc.topology_version)
  end
end

#topology_version_gte?(other_desc) ⇒ true | false

This method is for internal use only.

Returns whether topology version in this description is potentially newer than topology version in another description.

Parameters:

Returns:

  • (true | false)

    Whether topology version in this description is potentially newer.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 543

def topology_version_gte?(other_desc)
  if topology_version.nil? || other_desc.topology_version.nil?
    true
  else
    topology_version.gte?(other_desc.topology_version)
  end
end

#wire_versionsRange

Get the range of supported wire versions for the server.

Examples:

Get the wire version range.

description.wire_versions

Returns:

  • (Range)

    The wire version range.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/description.rb', line 758

def wire_versions
  min_wire_version..max_wire_version
end