Class: Mongo::Cluster::Topology::ReplicaSet

Inherits:
Object
  • Object
show all
Includes:
Loggable, Monitoring::Publishable
Defined in:
lib/mongo/cluster/topology/replica_set.rb

Overview

Defines behaviour when a cluster is in replica set topology.

Since:

  • 2.0.0

Constant Summary collapse

REPLICA_SET_NAME =

Constant for the replica set name configuration option.

Since:

  • 2.0.0

:replica_set.freeze
NAME =

The display name for the topology.

Since:

  • 2.0.0

'Replica Set'.freeze

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Monitoring::Publishable

#publish_command, #publish_event, #publish_sdam_event

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initialize(options, monitoring, seeds = []) ⇒ ReplicaSet

Initialize the topology with the options.

Examples:

Initialize the topology.

ReplicaSet.new(options)

Parameters:

  • options (Hash)

    The options.

  • monitoring (Monitoring)

    The monitoring.

  • seeds (Array<String>) (defaults to: [])

    The seeds.

Options Hash (options):

  • :replica_set (Symbol)

    Name of the replica set to connect to. Can be left blank (either nil or the empty string are accepted) to discover the name from the seeds. If the seeds belong to different replica sets there is no guarantee which replica set is selected - in particular, the driver may choose the replica set name of a secondary if it returns its response prior to a primary belonging to a different replica set.

Since:

  • 2.0.0



135
136
137
138
139
140
# File 'lib/mongo/cluster/topology/replica_set.rb', line 135

def initialize(options, monitoring, seeds = [])
  @options = options
  @monitoring = monitoring
  @max_election_id = nil
  @max_set_version = nil
end

Instance Attribute Details

#monitoringMonitoring (readonly)

Returns monitoring The monitoring.

Returns:

Since:

  • 2.0.0



35
36
37
# File 'lib/mongo/cluster/topology/replica_set.rb', line 35

def monitoring
  @monitoring
end

#optionsHash (readonly)

Returns options The options.

Returns:

  • (Hash)

    options The options.

Since:

  • 2.0.0



32
33
34
# File 'lib/mongo/cluster/topology/replica_set.rb', line 32

def options
  @options
end

Instance Method Details

#add_hosts?(description, servers) ⇒ true, false

Whether a server description's hosts may be added to the cluster.

Examples:

Check if a description's hosts may be added to the cluster.

topology.add_hosts?(description, servers)

Parameters:

Returns:

  • (true, false)

    Whether a description's hosts may be added.

Since:

  • 2.0.6



192
193
194
195
# File 'lib/mongo/cluster/topology/replica_set.rb', line 192

def add_hosts?(description, servers)
  !!(member_of_this_set?(description) &&
      (!has_primary?(servers) || description.primary?))
end

#display_nameString

Get the display name.

Examples:

Get the display name.

ReplicaSet.display_name

Returns:

  • (String)

    The display name.

Since:

  • 2.0.0



50
51
52
# File 'lib/mongo/cluster/topology/replica_set.rb', line 50

def display_name
  NAME
end

#elect_primary(description, servers) ⇒ ReplicaSet

Elect a primary server within this topology.

Examples:

Elect a primary server.

topology.elect_primary(description, servers)

Parameters:

  • description (Server::Description)

    The description of the elected primary.

  • servers (Array<Server>)

    The list of known servers to the cluster.

Returns:

Since:

  • 2.0.0



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mongo/cluster/topology/replica_set.rb', line 65

def elect_primary(description, servers)
  if description.replica_set_name == replica_set_name
    unless detect_stale_primary!(description)
      servers.each do |server|
        if server.primary? && server.address != description.address
          server.description.unknown!
        end
      end
      update_max_election_id(description)
      update_max_set_version(description)
    end
  else
    log_warn(
      "Server #{description.address.to_s} has incorrect replica set name: " +
      "'#{description.replica_set_name}'. The current replica set name is '#{replica_set_name}'."
    )
  end
  self
end

#has_readable_server?(cluster, server_selector = nil) ⇒ true, false

Determine if the topology would select a readable server for the provided candidates and read preference.

Examples:

Is a readable server present?

topology.has_readable_server?(cluster, server_selector)

Parameters:

  • cluster (Cluster)

    The cluster.

  • server_selector (ServerSelector) (defaults to: nil)

    The server selector.

Returns:

  • (true, false)

    If a readable server is present.

Since:

  • 2.4.0



98
99
100
# File 'lib/mongo/cluster/topology/replica_set.rb', line 98

def has_readable_server?(cluster, server_selector = nil)
  (server_selector || ServerSelector.get(mode: :primary)).candidates(cluster).any?
end

#has_writable_server?(cluster) ⇒ true, false

Determine if the topology would select a writable server for the provided candidates.

Examples:

Is a writable server present?

topology.has_writable_server?(servers)

Parameters:

  • cluster (Cluster)

    The cluster.

Returns:

  • (true, false)

    If a writable server is present.

Since:

  • 2.4.0



113
114
115
# File 'lib/mongo/cluster/topology/replica_set.rb', line 113

def has_writable_server?(cluster)
  cluster.servers.any?{ |server| server.primary? }
end

#member_discoveredObject

Notify the topology that a member was discovered.

Examples:

Notify the topology that a member was discovered.

topology.member_discovered

Since:

  • 2.4.0



287
# File 'lib/mongo/cluster/topology/replica_set.rb', line 287

def member_discovered; end

#remove_hosts?(description) ⇒ true, false

Whether a description can be used to remove hosts from the cluster.

Examples:

Check if a description can be used to remove hosts from the cluster.

topology.remove_hosts?(description)

Parameters:

Returns:

  • (true, false)

    Whether hosts may be removed from the cluster.

Since:

  • 2.0.6



207
208
209
210
211
212
213
# File 'lib/mongo/cluster/topology/replica_set.rb', line 207

def remove_hosts?(description)
  !description.config.empty? &&
    (description.primary? ||
      description.me_mismatch? ||
        description.hosts.empty? ||
          !member_of_this_set?(description))
end

#remove_server?(description, server) ⇒ true, false

Whether a specific server in the cluster can be removed, given a description. As described in the SDAM spec, a server should be removed if the server's address does not match the “me” field of the isMaster response, if the server has a different replica set name, or if an isMaster response from the primary does not contain the server's address in the list of known hosts. Note that as described by the spec, a server determined to be of type Unknown from its isMaster response is NOT removed from the topology.

Examples:

Check if a specific server can be removed from the cluster.

topology.remove_server?(description, server)

Parameters:

Returns:

  • (true, false)

    Whether the server can be removed from the cluster.

Since:

  • 2.0.6



232
233
234
235
236
237
238
239
# File 'lib/mongo/cluster/topology/replica_set.rb', line 232

def remove_server?(description, server)
  ((server.address == description.address) && description.me_mismatch?) ||
  remove_self?(description, server) ||
    (member_of_this_set?(description) &&
        description.server_type == :primary &&
        !description.servers.empty? &&
          !description.lists_server?(server))
end

#replica_set?true

A replica set topology is a replica set.

Examples:

Is the topology a replica set?

ReplicaSet.replica_set?

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0



150
# File 'lib/mongo/cluster/topology/replica_set.rb', line 150

def replica_set?; true; end

#replica_set_nameString

Get the replica set name configured for this topology.

Examples:

Get the replica set name.

topology.replica_set_name

Returns:

  • (String)

    The name of the configured replica set.

Since:

  • 2.0.0



160
161
162
# File 'lib/mongo/cluster/topology/replica_set.rb', line 160

def replica_set_name
  @replica_set_name ||= options[REPLICA_SET_NAME]
end

#servers(servers) ⇒ Array<Server>

Select appropriate servers for this topology.

Examples:

Select the servers.

ReplicaSet.servers(servers)

Parameters:

  • servers (Array<Server>)

    The known servers.

Returns:

  • (Array<Server>)

    The servers in the replica set.

Since:

  • 2.0.0



174
175
176
177
178
179
# File 'lib/mongo/cluster/topology/replica_set.rb', line 174

def servers(servers)
  servers.select do |server|
    (replica_set_name.nil? || server.replica_set_name == replica_set_name) &&
      server.primary? || server.secondary?
  end
end

#sharded?false

A replica set topology is not sharded.

Examples:

Is the topology sharded?

ReplicaSet.sharded?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0



249
# File 'lib/mongo/cluster/topology/replica_set.rb', line 249

def sharded?; false; end

#single?false

A replica set topology is not single.

Examples:

Is the topology single?

ReplicaSet.single?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0



259
# File 'lib/mongo/cluster/topology/replica_set.rb', line 259

def single?; false; end

#standalone_discoveredTopology::ReplicaSet

Notify the topology that a standalone was discovered.

Examples:

Notify the topology that a standalone was discovered.

topology.standalone_discovered

Returns:

Since:

  • 2.0.6



279
# File 'lib/mongo/cluster/topology/replica_set.rb', line 279

def standalone_discovered; self; end

#unknown?false

A replica set topology is not unknown.

Examples:

Is the topology unknown?

ReplicaSet.unknown?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0



269
# File 'lib/mongo/cluster/topology/replica_set.rb', line 269

def unknown?; false; end