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/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 collapse

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
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
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, ELECTION_ID ].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 (ms) the ismaster call took to complete.

Since:

  • 2.0.0



231
232
233
234
235
236
# File 'lib/mongo/server/description.rb', line 231

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



148
149
150
# File 'lib/mongo/server/description.rb', line 148

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



157
158
159
# File 'lib/mongo/server/description.rb', line 157

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



151
152
153
# File 'lib/mongo/server/description.rb', line 151

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



154
155
156
# File 'lib/mongo/server/description.rb', line 154

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



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

def ==(other)
  return false if self.class != other.class
  return true if config == other.config
  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



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

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



179
180
181
# File 'lib/mongo/server/description.rb', line 179

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



342
343
344
# File 'lib/mongo/server/description.rb', line 342

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



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

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



203
204
205
# File 'lib/mongo/server/description.rb', line 203

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



215
216
217
# File 'lib/mongo/server/description.rb', line 215

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



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

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



502
503
504
# File 'lib/mongo/server/description.rb', line 502

def is_server?(server)
  address == server.address
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



515
516
517
# File 'lib/mongo/server/description.rb', line 515

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



258
259
260
# File 'lib/mongo/server/description.rb', line 258

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



270
271
272
# File 'lib/mongo/server/description.rb', line 270

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



294
295
296
# File 'lib/mongo/server/description.rb', line 294

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



282
283
284
# File 'lib/mongo/server/description.rb', line 282

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



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

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



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

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



306
307
308
# File 'lib/mongo/server/description.rb', line 306

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



354
355
356
# File 'lib/mongo/server/description.rb', line 354

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



366
367
368
369
# File 'lib/mongo/server/description.rb', line 366

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



379
380
381
# File 'lib/mongo/server/description.rb', line 379

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



391
392
393
# File 'lib/mongo/server/description.rb', line 391

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



403
404
405
# File 'lib/mongo/server/description.rb', line 403

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



528
529
530
# File 'lib/mongo/server/description.rb', line 528

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



416
417
418
# File 'lib/mongo/server/description.rb', line 416

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



440
441
442
# File 'lib/mongo/server/description.rb', line 440

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



428
429
430
# File 'lib/mongo/server/description.rb', line 428

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



452
453
454
# File 'lib/mongo/server/description.rb', line 452

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



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

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



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

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



464
465
466
# File 'lib/mongo/server/description.rb', line 464

def unknown?
  config.empty? || 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



490
491
492
# File 'lib/mongo/server/description.rb', line 490

def wire_versions
  min_wire_version..max_wire_version
end