Class: Mongo::Cluster::Topology::Base

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

Overview

Defines behavior common to all topologies.

Since:

  • 2.7.0

Direct Known Subclasses

ReplicaSetNoPrimary, Sharded, Single, Unknown

Constant Summary

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Monitoring::Publishable

#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, cluster) ⇒ Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the topology with the options.

Parameters:

  • options (Hash)

    The options.

  • monitoring (Monitoring)

    The monitoring.

  • cluster (Cluster)

    The cluster.

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 cluster. If the addresses 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. This option can only be specified when instantiating a replica set topology.

  • :max_election_id (BSON::ObjectId)

    Max election id per the SDAM specification. This option can only be specified when instantiating a replica set topology.

  • :max_set_version (Integer)

    Max set version per the SDAM specification. This option can only be specified when instantiating a replica set topology.

Since:

  • 2.7.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mongo/cluster/topology/base.rb', line 53

def initialize(options, monitoring, cluster)
  options = validate_options(options, cluster)

  @options = options
  @monitoring = monitoring
  @cluster = cluster
  # The list of server descriptions is simply fixed at the time of
  # topology creation. If server description change later, a
  # new topology instance should be created.
  @server_descriptions = {}
  (servers = cluster.servers_list).each do |server|
    @server_descriptions[server.address.to_s] = server.description
  end

  begin
    server_descriptions.each do |address_str, desc|
      unless desc.unknown?
        desc.features.check_driver_support!
      end
    end
  rescue Error::UnsupportedFeatures => e
    @compatible = false
    @compatibility_error = e
  else
    @compatible = true
  end

  @have_data_bearing_servers = false
  @logical_session_timeout = server_descriptions.inject(nil) do |min, (address_str, desc)|
    # LST is only read from data-bearing servers
    if desc.data_bearing?
      @have_data_bearing_servers = true
      break unless timeout = desc.logical_session_timeout
      [timeout, (min || timeout)].min
    else
      min
    end
  end

  if Mongo::Lint.enabled?
    freeze
  end
end

Instance Attribute Details

#compatibility_errorException (readonly)

Returns compatibility_error If topology is incompatible with the driver, an exception with information regarding the incompatibility. If topology is compatible with the driver, nil.

Returns:

  • (Exception)

    compatibility_error If topology is incompatible with the driver, an exception with information regarding the incompatibility. If topology is compatible with the driver, nil.

Since:

  • 2.7.0



144
145
146
# File 'lib/mongo/cluster/topology/base.rb', line 144

def compatibility_error
  @compatibility_error
end

#logical_session_timeoutInteger? (readonly)

Note:

The value is in minutes, unlike most other times in the driver which are returned in seconds.

The logical session timeout value in minutes.

Returns:

  • (Integer, nil)

    The logical session timeout.

Since:

  • 2.7.0



154
155
156
# File 'lib/mongo/cluster/topology/base.rb', line 154

def logical_session_timeout
  @logical_session_timeout
end

#monitoringmonitoring (readonly)

Returns monitoring the monitoring.

Returns:

Since:

  • 2.7.0



111
112
113
# File 'lib/mongo/cluster/topology/base.rb', line 111

def monitoring
  @monitoring
end

#optionsHash (readonly)

Returns options The options.

Returns:

  • (Hash)

    options The options.

Since:

  • 2.7.0



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

def options
  @options
end

#server_descriptionsHash (readonly)

Returns server_descriptions The map of address strings to server descriptions, one for each server in the cluster.

Returns:

  • (Hash)

    server_descriptions The map of address strings to server descriptions, one for each server in the cluster.

Since:

  • 2.7.0



129
130
131
# File 'lib/mongo/cluster/topology/base.rb', line 129

def server_descriptions
  @server_descriptions
end

Instance Method Details

#addressesArray<String>

Returns addresses Server addresses.

Returns:

  • (Array<String>)

    addresses Server addresses.

Since:

  • 2.7.0



106
107
108
# File 'lib/mongo/cluster/topology/base.rb', line 106

def addresses
  cluster.addresses.map(&:seed)
end

#compatible?true|false

Returns compatible Whether topology is compatible with the driver.

Returns:

  • (true|false)

    compatible Whether topology is compatible with the driver.

Since:

  • 2.7.0



135
136
137
# File 'lib/mongo/cluster/topology/base.rb', line 135

def compatible?
  @compatible
end

#data_bearing_servers?true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns have_data_bearing_servers Whether the topology has any data bearing servers, for the purposes of logical session timeout calculation.

Returns:

  • (true | false)

    have_data_bearing_servers Whether the topology has any data bearing servers, for the purposes of logical session timeout calculation.

Since:

  • 2.7.0



161
162
163
# File 'lib/mongo/cluster/topology/base.rb', line 161

def data_bearing_servers?
  @have_data_bearing_servers
end

#max_election_idBSON::ObjectId

The largest electionId ever reported by a primary. May be nil.

Returns:

  • (BSON::ObjectId)

    The election id.

Since:

  • 2.7.0



171
172
173
# File 'lib/mongo/cluster/topology/base.rb', line 171

def max_election_id
  options[:max_election_id]
end

#max_set_versionInteger

The largest setVersion ever reported by a primary. May be nil.

Returns:

  • (Integer)

    The set version.

Since:

  • 2.7.0



181
182
183
# File 'lib/mongo/cluster/topology/base.rb', line 181

def max_set_version
  options[:max_set_version]
end

#new_max_election_id(description) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.7.0



186
187
188
189
190
191
192
193
194
# File 'lib/mongo/cluster/topology/base.rb', line 186

def new_max_election_id(description)
  if description.election_id &&
      (max_election_id.nil? ||
          description.election_id > max_election_id)
    description.election_id
  else
    max_election_id
  end
end

#new_max_set_version(description) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.7.0



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

def new_max_set_version(description)
  if description.set_version &&
      (max_set_version.nil? ||
          description.set_version > max_set_version)
    description.set_version
  else
    max_set_version
  end
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



121
122
123
# File 'lib/mongo/cluster/topology/base.rb', line 121

def replica_set_name
  options[:replica_set_name]
end