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

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.

Since:

  • 2.0.0



127
128
129
130
131
132
# File 'lib/mongo/cluster/topology/replica_set.rb', line 127

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



184
185
186
187
# File 'lib/mongo/cluster/topology/replica_set.rb', line 184

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



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

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



199
200
201
202
203
204
205
# File 'lib/mongo/cluster/topology/replica_set.rb', line 199

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.

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



218
219
220
221
# File 'lib/mongo/cluster/topology/replica_set.rb', line 218

def remove_server?(description, server)
  remove_self?(description, server) ||
    (member_of_this_set?(description) && !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



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

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



152
153
154
# File 'lib/mongo/cluster/topology/replica_set.rb', line 152

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



166
167
168
169
170
171
# File 'lib/mongo/cluster/topology/replica_set.rb', line 166

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



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

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



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

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



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

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



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

def unknown?; false; end