Class: Mongo::Server::Description

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/server/description.rb,
lib/mongo/server/description/features.rb,
lib/mongo/server/description/inspector.rb,
lib/mongo/server/description/inspector/primary_elected.rb,
lib/mongo/server/description/inspector/member_discovered.rb,
lib/mongo/server/description/inspector/description_changed.rb,
lib/mongo/server/description/inspector/standalone_discovered.rb

Overview

Represents a description of the server, populated by the result of the ismaster command.

Since:

  • 2.0.0

Defined Under Namespace

Classes: Features, Inspector

Constant Summary

ARBITER =

Constant for reading arbiter info from config.

Since:

  • 2.0.0

'arbiterOnly'.freeze
ARBITERS =

Constant for reading arbiters info from config.

Since:

  • 2.0.0

'arbiters'.freeze
HIDDEN =

Constant for reading hidden info from config.

Since:

  • 2.0.0

'hidden'.freeze
HOSTS =

Constant for reading hosts info from config.

Since:

  • 2.0.0

'hosts'.freeze
MESSAGE =

Constant for the key for the message value.

Since:

  • 2.0.0

'msg'.freeze
MONGOS_MESSAGE =

Constant for the message that indicates a sharded cluster.

Since:

  • 2.0.0

'isdbgrid'.freeze
REPLICA_SET =

Constant for determining ghost servers.

Since:

  • 2.0.0

'isreplicaset'.freeze
MAX_BSON_OBJECT_SIZE =

Constant for reading max bson size info from config.

Since:

  • 2.0.0

'maxBsonObjectSize'.freeze
MAX_MESSAGE_BYTES =

Constant for reading max message size info from config.

Since:

  • 2.0.0

'maxMessageSizeBytes'.freeze
MAX_WIRE_VERSION =

Constant for the max wire version.

Since:

  • 2.0.0

'maxWireVersion'.freeze
MIN_WIRE_VERSION =

Constant for min wire version.

Since:

  • 2.0.0

'minWireVersion'.freeze
MAX_WRITE_BATCH_SIZE =

Constant for reading max write batch size.

Since:

  • 2.0.0

'maxWriteBatchSize'.freeze
LAST_WRITE =

Constant for the lastWrite subdocument.

Since:

  • 2.4.0

'lastWrite'.freeze
LAST_WRITE_DATE =

Constant for the lastWriteDate field in the lastWrite subdocument.

Since:

  • 2.4.0

'lastWriteDate'.freeze
ME =

Constant for reading the me field.

Since:

  • 2.1.0

'me'.freeze
DEFAULT_MAX_WRITE_BATCH_SIZE =

Default max write batch size.

Since:

  • 2.0.0

1000.freeze
LEGACY_WIRE_VERSION =

The legacy wire protocol version.

Since:

  • 2.0.0

0.freeze
PASSIVE =

Constant for reading passive info from config.

Since:

  • 2.0.0

'passive'.freeze
PASSIVES =

Constant for reading the passive server list.

Since:

  • 2.0.0

'passives'.freeze
PRIMARY =

Constant for reading primary info from config.

Since:

  • 2.0.0

'ismaster'.freeze
PRIMARY_HOST =

Constant for reading primary host field from config.

Since:

  • 2.5.0

'primary'.freeze
SECONDARY =

Constant for reading secondary info from config.

Since:

  • 2.0.0

'secondary'.freeze
SET_NAME =

Constant for reading replica set name info from config.

Since:

  • 2.0.0

'setName'.freeze
TAGS =

Constant for reading tags info from config.

Since:

  • 2.0.0

'tags'.freeze
ELECTION_ID =

Constant for reading electionId info from config.

Since:

  • 2.1.0

'electionId'.freeze
SET_VERSION =

Constant for reading setVersion info from config.

Since:

  • 2.2.2

'setVersion'.freeze
LOCAL_TIME =

Constant for reading localTime info from config.

Since:

  • 2.1.0

'localTime'.freeze
OPERATION_TIME =

Constant for reading operationTime info from config.

Since:

  • 2.5.0

'operationTime'.freeze
LOGICAL_SESSION_TIMEOUT_MINUTES =

Constant for reading logicalSessionTimeoutMinutes info from config.

Since:

  • 2.5.0

'logicalSessionTimeoutMinutes'.freeze
EXCLUDE_FOR_COMPARISON =

Fields to exclude when comparing two descriptions.

Since:

  • 2.0.6

[ LOCAL_TIME,
LAST_WRITE,
OPERATION_TIME,
Operation::CLUSTER_TIME ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, config = {}, average_round_trip_time = 0) ⇒ Description

Instantiate the new server description from the result of the ismaster command.

Examples:

Instantiate the new description.

Description.new(address, { 'ismaster' => true }, 0.5)

Parameters:

  • address (Address)

    The server address.

  • config (Hash) (defaults to: {})

    The result of the ismaster command.

  • average_round_trip_time (Float) (defaults to: 0)

    The moving average time (sec) the ismaster call took to complete.

Since:

  • 2.0.0



264
265
266
267
268
269
# File 'lib/mongo/server/description.rb', line 264

def initialize(address, config = {}, average_round_trip_time = 0)
  @address = address
  @config = config
  @features = Features.new(wire_versions, me || @address.to_s)
  @average_round_trip_time = average_round_trip_time
end

Instance Attribute Details

#addressAddress (readonly)

Returns address The server's address.

Returns:

  • (Address)

    address The server's address.

Since:

  • 2.0.0



181
182
183
# File 'lib/mongo/server/description.rb', line 181

def address
  @address
end

#average_round_trip_timeFloat (readonly)

Returns The moving average time the ismaster call took to complete.

Returns:

  • (Float)

    The moving average time the ismaster call took to complete.

Since:

  • 2.0.0



190
191
192
# File 'lib/mongo/server/description.rb', line 190

def average_round_trip_time
  @average_round_trip_time
end

#configHash (readonly)

Returns The actual result from the ismaster command.

Returns:

  • (Hash)

    The actual result from the ismaster command.

Since:

  • 2.0.0



184
185
186
# File 'lib/mongo/server/description.rb', line 184

def config
  @config
end

#featuresFeatures (readonly)

Returns features The features for the server.

Returns:

  • (Features)

    features The features for the server.

Since:

  • 2.0.0



187
188
189
# File 'lib/mongo/server/description.rb', line 187

def features
  @features
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



644
645
646
647
648
# File 'lib/mongo/server/description.rb', line 644

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.

Examples:

Is the server an arbiter?

description.arbiter?

Returns:

  • (true, false)

    If the server is an arbiter.

Since:

  • 2.0.0



200
201
202
# File 'lib/mongo/server/description.rb', line 200

def arbiter?
  !!config[ARBITER] && !replica_set_name.nil?
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



212
213
214
# File 'lib/mongo/server/description.rb', line 212

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



375
376
377
# File 'lib/mongo/server/description.rb', line 375

def election_id
  config[ELECTION_ID]
end

#ghost?true, false

Is the server a ghost in a replica set?

Examples:

Is the server a ghost?

description.ghost?

Returns:

  • (true, false)

    If the server is a ghost.

Since:

  • 2.0.0



224
225
226
# File 'lib/mongo/server/description.rb', line 224

def ghost?
  !!config[REPLICA_SET]
end

#hidden?true, false

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



236
237
238
# File 'lib/mongo/server/description.rb', line 236

def hidden?
  !!config[HIDDEN]
end

#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



248
249
250
# File 'lib/mongo/server/description.rb', line 248

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



279
280
281
# File 'lib/mongo/server/description.rb', line 279

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.

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



592
593
594
# File 'lib/mongo/server/description.rb', line 592

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



399
400
401
# File 'lib/mongo/server/description.rb', line 399

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.

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



605
606
607
# File 'lib/mongo/server/description.rb', line 605

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



411
412
413
# File 'lib/mongo/server/description.rb', line 411

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



291
292
293
# File 'lib/mongo/server/description.rb', line 291

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



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

def max_message_size
  config[MAX_MESSAGE_BYTES]
end

#max_wire_versionInteger

Get the maximum wire version.

Examples:

Get the max wire version.

description.max_wire_version

Returns:

  • (Integer)

    The max wire version supported.

Since:

  • 2.0.0



327
328
329
# File 'lib/mongo/server/description.rb', line 327

def max_wire_version
  config[MAX_WIRE_VERSION] || LEGACY_WIRE_VERSION
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



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

def max_write_batch_size
  config[MAX_WRITE_BATCH_SIZE] || DEFAULT_MAX_WRITE_BATCH_SIZE
end

#meString

Get the me field value.

Examples:

Get the me field value.

description.me

Returns:

  • (String)

    The me field.

Since:

  • 2.1.0



351
352
353
# File 'lib/mongo/server/description.rb', line 351

def me
  config[ME]
end

#me_mismatch?true, false

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



630
631
632
# File 'lib/mongo/server/description.rb', line 630

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

#min_wire_versionInteger

Get the minimum wire version.

Examples:

Get the min wire version.

description.min_wire_version

Returns:

  • (Integer)

    The min wire version supported.

Since:

  • 2.0.0



339
340
341
# File 'lib/mongo/server/description.rb', line 339

def min_wire_version
  config[MIN_WIRE_VERSION] || LEGACY_WIRE_VERSION
end

#mongos?true, false

Is the server a mongos?

Examples:

Is the server a mongos?

description.mongos?

Returns:

  • (true, false)

    If the server is a mongos.

Since:

  • 2.0.0



423
424
425
# File 'lib/mongo/server/description.rb', line 423

def mongos?
  config[MESSAGE] == MONGOS_MESSAGE
end

#other?true, false

Is the description of type other.

Examples:

Is the description of type other.

description.other?

Returns:

  • (true, false)

    If the description is other.

Since:

  • 2.0.0



435
436
437
438
# File 'lib/mongo/server/description.rb', line 435

def other?
  (!primary? && !secondary? && !passive? && !arbiter?) ||
    (hidden? && !replica_set_name.nil?)
end

#passive?true, false

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



448
449
450
# File 'lib/mongo/server/description.rb', line 448

def passive?
  !!config[PASSIVE]
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



460
461
462
# File 'lib/mongo/server/description.rb', line 460

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

#primary?true, false

Will return true if the server is a primary.

Examples:

Is the server a primary?

description.primary?

Returns:

  • (true, false)

    If the server is a primary.

Since:

  • 2.0.0



472
473
474
475
476
# File 'lib/mongo/server/description.rb', line 472

def primary?
  !!config[PRIMARY] &&
    (config[PRIMARY_HOST].nil? || config[PRIMARY_HOST] == address.to_s) &&
      !replica_set_name.nil?
end

#replica_set_member?true, false

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



618
619
620
# File 'lib/mongo/server/description.rb', line 618

def replica_set_member?
  !(standalone? || mongos?)
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



487
488
489
# File 'lib/mongo/server/description.rb', line 487

def replica_set_name
  config[SET_NAME]
end

#secondary?true, false

Will return true if the server is a secondary.

Examples:

Is the server a secondary?

description.secondary?

Returns:

  • (true, false)

    If the server is a secondary.

Since:

  • 2.0.0



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

def secondary?
  !!config[SECONDARY] && !replica_set_name.nil?
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



523
524
525
526
527
528
529
530
531
# File 'lib/mongo/server/description.rb', line 523

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?
  :unknown
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



499
500
501
# File 'lib/mongo/server/description.rb', line 499

def servers
  hosts + arbiters + passives
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



387
388
389
# File 'lib/mongo/server/description.rb', line 387

def set_version
  config[SET_VERSION]
end

#standalone?true, false

Is this server a standalone server?

Examples:

Is the server standalone?

description.standalone?

Returns:

  • (true, false)

    If the server is standalone.

Since:

  • 2.0.0



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

def standalone?
  replica_set_name.nil? && !mongos? && !ghost? && !unknown?
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



363
364
365
# File 'lib/mongo/server/description.rb', line 363

def tags
  config[TAGS] || {}
end

#unknown!true

A result from another server's ismaster command before this server has refreshed causes the need for this description to become unknown before the next refresh.

Examples:

Force an unknown state.

description.unknown!

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0



568
569
570
# File 'lib/mongo/server/description.rb', line 568

def unknown!
  @config = {} and true
end

#unknown?true, false

Is the server description currently unknown?

Examples:

Is the server description unknown?

description.unknown?

Returns:

  • (true, false)

    If the server description is unknown.

Since:

  • 2.0.0



553
554
555
556
# File 'lib/mongo/server/description.rb', line 553

def unknown?
  config.empty? || (config[Operation::Result::OK] &&
                      config[Operation::Result::OK] != 1)
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



580
581
582
# File 'lib/mongo/server/description.rb', line 580

def wire_versions
  min_wire_version..max_wire_version
end