Method: Mongo::Cluster::SdamFlow#update_rs_from_primary

Defined in:
lib/mongo/cluster/sdam_flow.rb

#update_rs_from_primaryObject

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.

Updates topology which must be a ReplicaSetWithPrimary with information from the primary’s server description.

This method does not change topology type to ReplicaSetWithPrimary - this needs to have been done prior to calling this method.

If the primary whose description is being processed is determined to be stale, this method will change the server description and topology type to unknown.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/mongo/cluster/sdam_flow.rb', line 221

def update_rs_from_primary
  if topology.replica_set_name.nil?
    @topology = Topology::ReplicaSetWithPrimary.new(
      topology.options.merge(replica_set_name: updated_desc.replica_set_name),
      topology.monitoring, self)
  end

  if topology.replica_set_name != updated_desc.replica_set_name
    log_warn(
      "Removing server #{updated_desc.address.to_s} because it has an " +
      "incorrect replica set name '#{updated_desc.replica_set_name}'; " +
      "expected '#{topology.replica_set_name}'"
    )
    remove
    check_if_has_primary
    return
  end

  if stale_primary?
    @updated_desc = ::Mongo::Server::Description.new(
      updated_desc.address,
      {},
      average_round_trip_time: updated_desc.average_round_trip_time,
      minimum_round_trip_time: updated_desc.minimum_round_trip_time
    )
    update_server_descriptions
    check_if_has_primary
    return
  end

  if updated_desc.max_wire_version >= 17
    @topology = Topology::ReplicaSetWithPrimary.new(
      topology.options.merge(
        max_election_id: updated_desc.election_id,
        max_set_version: updated_desc.set_version
      ), topology.monitoring, self)
  else
    max_election_id = topology.new_max_election_id(updated_desc)
    max_set_version = topology.new_max_set_version(updated_desc)

    if max_election_id != topology.max_election_id ||
      max_set_version != topology.max_set_version
    then
      @topology = Topology::ReplicaSetWithPrimary.new(
        topology.options.merge(
          max_election_id: max_election_id,
          max_set_version: max_set_version
        ), topology.monitoring, self)
    end
  end

  # At this point we have accepted the updated server description
  # and the topology (both are primary). Commit these changes so that
  # their respective SDAM events are published before SDAM events for
  # server additions/removals that follow
  publish_description_change_event

  servers_list.each do |server|
    if server.address != updated_desc.address
      if server.primary?
        server.update_description(
          ::Mongo::Server::Description.new(
            server.address,
            {},
            average_round_trip_time: server.description.average_round_trip_time,
            minimum_round_trip_time: updated_desc.minimum_round_trip_time
          )
        )
      end
    end
  end

  servers = add_servers_from_desc(updated_desc)
  remove_servers_not_in_desc(updated_desc)

  check_if_has_primary

  servers.each do |server|
    server.start_monitoring
  end
end