Method: Mongo::Server::Description#initialize

Defined in:
lib/mongo/server/description.rb

#initialize(address, config = {}, average_round_trip_time: nil, minimum_round_trip_time: 0, load_balancer: false, force_load_balancer: false) ⇒ Description

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.

Instantiate the new server description from the result of the hello command or fabricate a placeholder description for Unknown and LoadBalancer servers.

Examples:

Instantiate the new description.

Description.new(address, { 'isWritablePrimary' => true }, 0.5)

Parameters:

  • address (Address)

    The server address.

  • config (Hash) (defaults to: {})

    The result of the hello command.

  • average_round_trip_time (Float) (defaults to: nil)

    The moving average time (sec) the hello command took to complete.

  • minimum_round_trip_time (Float) (defaults to: 0)

    The minimum round trip time of ten last hello commands.

  • load_balancer (true | false) (defaults to: false)

    Whether the server is treated as a load balancer.

  • force_load_balancer (true | false) (defaults to: false)

    Whether the server is forced to be a load balancer.

Since:

  • 2.0.0



220
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
# File 'lib/mongo/server/description.rb', line 220

def initialize(address, config = {}, average_round_trip_time: nil,
  minimum_round_trip_time: 0, load_balancer: false,
  force_load_balancer: false
)
  @address = address
  @config = config
  @load_balancer = !!load_balancer
  @force_load_balancer = !!force_load_balancer
  @features = Features.new(wire_versions, me || @address.to_s)
  @average_round_trip_time = average_round_trip_time
  @minimum_round_trip_time = minimum_round_trip_time
  @last_update_time = Time.now.freeze
  @last_update_monotime = Utils.monotonic_time

  if load_balancer
    # When loadBalanced=true URI option is set, the driver will refuse
    # to work if the server it communicates with does not set serviceId
    # in ismaster/hello response.
    #
    # At the moment we cannot run a proper load balancer setup on evergreen
    #
    # Therefore, when connect=:load_balanced Ruby option is used instead
    # of the loadBalanced=true URI option, if serviceId is not set in
    # ismaster/hello response, the driver fabricates a serviceId and
    # proceeds to treat a server that does not report itself as being
    # behind a load balancer as a server that is behind a load balancer.
    #
    # 5.0+ servers should provide topologyVersion.processId which
    # is specific to the particular process instance. We can use that
    # field as a proxy for serviceId.
    #
    # If the topologyVersion isn't provided for whatever reason, we
    # fabricate a serviceId locally.
    #
    # In either case, a serviceId provided by an actual server behind
    # a load balancer is supposed to be a BSON::ObjectId. The fabricated
    # service ids are strings, to distinguish them from the real ones.
    # In particular processId is also a BSON::ObjectId, but will be
    # mapped to a string for clarity that this is a fake service id.
    #
    # TODO: Remove this when https://jira.mongodb.org/browse/RUBY-2881 is done.
    if ok? && !service_id
      unless force_load_balancer
        raise Error::MissingServiceId, "The server at #{address.seed} did not provide a service id in handshake response"
      end

      fake_service_id = if process_id = topology_version && topology_version['processId']
        "process:#{process_id}"
      else
        "fake:#{rand(2**32-1)+1}"
      end
      @config = @config.merge('serviceId' => fake_service_id)
    end
  end

  if Mongo::Lint.enabled?
    # prepopulate cache instance variables
    hosts
    arbiters
    passives
    topology_version

    freeze
  end
end