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
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
EXCLUDE_FOR_COMPARISON =

Fields to exclude when comparing two descriptions.

Since:

  • 2.0.6

[ LOCAL_TIME, LAST_WRITE ].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



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

def initialize(address, config = {}, average_round_trip_time = 0)
  @address = address
  @config = config
  @features = Features.new(wire_versions)
  @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



163
164
165
# File 'lib/mongo/server/description.rb', line 163

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



172
173
174
# File 'lib/mongo/server/description.rb', line 172

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



166
167
168
# File 'lib/mongo/server/description.rb', line 166

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



169
170
171
# File 'lib/mongo/server/description.rb', line 169

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



612
613
614
615
616
# File 'lib/mongo/server/description.rb', line 612

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



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

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



194
195
196
# File 'lib/mongo/server/description.rb', line 194

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



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

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



206
207
208
# File 'lib/mongo/server/description.rb', line 206

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



218
219
220
# File 'lib/mongo/server/description.rb', line 218

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



230
231
232
# File 'lib/mongo/server/description.rb', line 230

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



261
262
263
# File 'lib/mongo/server/description.rb', line 261

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



560
561
562
# File 'lib/mongo/server/description.rb', line 560

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



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

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



573
574
575
# File 'lib/mongo/server/description.rb', line 573

def lists_server?(server)
  servers.include?(server.address.to_s)
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



273
274
275
# File 'lib/mongo/server/description.rb', line 273

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



285
286
287
# File 'lib/mongo/server/description.rb', line 285

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



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

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



297
298
299
# File 'lib/mongo/server/description.rb', line 297

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



333
334
335
# File 'lib/mongo/server/description.rb', line 333

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



598
599
600
# File 'lib/mongo/server/description.rb', line 598

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



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

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



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

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



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

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



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

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



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

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



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

def primary?
  !!config[PRIMARY] && !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



586
587
588
# File 'lib/mongo/server/description.rb', line 586

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



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

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



479
480
481
# File 'lib/mongo/server/description.rb', line 479

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



491
492
493
494
495
496
497
498
499
# File 'lib/mongo/server/description.rb', line 491

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



467
468
469
# File 'lib/mongo/server/description.rb', line 467

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



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

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



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

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



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

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



536
537
538
# File 'lib/mongo/server/description.rb', line 536

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



521
522
523
524
# File 'lib/mongo/server/description.rb', line 521

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



548
549
550
# File 'lib/mongo/server/description.rb', line 548

def wire_versions
  min_wire_version..max_wire_version
end