Module: Mongo::ServerSelector::Selectable

Included in:
Nearest, Primary, PrimaryPreferred, Secondary, SecondaryPreferred
Defined in:
lib/mongo/server_selector/selectable.rb

Overview

Provides common behavior for filtering a list of servers by server mode or tag set.

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#max_stalenessInteger (readonly)

Returns max_staleness The maximum replication lag, in seconds, that a secondary can suffer and still be eligible for a read.

Returns:

  • (Integer)

    max_staleness The maximum replication lag, in seconds, that a secondary can suffer and still be eligible for a read.

Since:

  • 2.4.0



33
34
35
# File 'lib/mongo/server_selector/selectable.rb', line 33

def max_staleness
  @max_staleness
end

#optionsHash (readonly)

Returns options The options.

Returns:

  • (Hash)

    options The options.

Since:

  • 2.0.0



24
25
26
# File 'lib/mongo/server_selector/selectable.rb', line 24

def options
  @options
end

#tag_setsArray (readonly)

Returns tag_sets The tag sets used to select servers.

Returns:

  • (Array)

    tag_sets The tag sets used to select servers.

Since:

  • 2.0.0



27
28
29
# File 'lib/mongo/server_selector/selectable.rb', line 27

def tag_sets
  @tag_sets
end

Instance Method Details

#==(other) ⇒ true, false

Check equality of two server selector.

Examples:

Check server selector equality.

preference == other

Parameters:

  • other (Object)

    The other preference.

Returns:

  • (true, false)

    Whether the objects are equal.

Since:

  • 2.0.0



45
46
47
48
49
# File 'lib/mongo/server_selector/selectable.rb', line 45

def ==(other)
  name == other.name &&
    tag_sets == other.tag_sets &&
      max_staleness == other.max_staleness
end

#candidates(cluster) ⇒ Array<Server>

Get the potential candidates to select from the cluster.

Examples:

Get the server candidates.

selectable.candidates(cluster)

Parameters:

  • cluster (Cluster)

    The cluster.

Returns:

  • (Array<Server>)

    The candidate servers.

Since:

  • 2.4.0



159
160
161
162
163
164
165
166
167
168
# File 'lib/mongo/server_selector/selectable.rb', line 159

def candidates(cluster)
  if cluster.single?
    cluster.servers.each { |server| validate_max_staleness_support!(server) }
  elsif cluster.sharded?
    near_servers(cluster.servers).each { |server| validate_max_staleness_support!(server) }
  else
    validate_max_staleness_value!(cluster) unless cluster.unknown?
    select(cluster.servers)
  end
end

#initialize(options = {}) ⇒ Object

Initialize the server selector.

Examples:

Initialize the selector.

Mongo::ServerSelector::Secondary.new(:tag_sets => [{'dc' => 'nyc'}])

Initialize the preference with no options.

Mongo::ServerSelector::Secondary.new

Parameters:

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

    The server preference options.

Options Hash (options):

  • :local_threshold (Integer)

    The local threshold boundary for nearest selection in seconds.

Raises:

Since:

  • 2.0.0



68
69
70
71
72
73
# File 'lib/mongo/server_selector/selectable.rb', line 68

def initialize(options = {})
  @options = (options || {}).freeze
  @tag_sets = (options[:tag_sets] || []).freeze
  @max_staleness = options[:max_staleness] unless options[:max_staleness] == -1
  validate!
end

#inspectString

Inspect the server selector.

Examples:

Inspect the server selector.

selector.inspect

Returns:

  • (String)

    The inspection.

Since:

  • 2.2.0



83
84
85
# File 'lib/mongo/server_selector/selectable.rb', line 83

def inspect
  "#<#{self.class.name}:0x#{object_id} tag_sets=#{tag_sets.inspect} max_staleness=#{max_staleness.inspect}>"
end

#local_thresholdFloat

Deprecated.

This setting is now taken from the cluster options when a server is selected. Will be removed in 3.0.

Get the local threshold boundary for nearest selection in seconds.

Examples:

Get the local threshold.

selector.local_threshold

Returns:

  • (Float)

    The local threshold.

Since:

  • 2.0.0



145
146
147
# File 'lib/mongo/server_selector/selectable.rb', line 145

def local_threshold
  @local_threshold ||= (options[:local_threshold] || ServerSelector::LOCAL_THRESHOLD)
end

#select_server(cluster, ping = true) ⇒ Mongo::Server

Select a server from eligible candidates.

Examples:

Select a server from the cluster.

selector.select_server(cluster)

Parameters:

  • cluster (Mongo::Cluster)

    The cluster from which to select an eligible server.

Returns:

Raises:

Since:

  • 2.0.0



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mongo/server_selector/selectable.rb', line 97

def select_server(cluster, ping = true)
  @local_threshold = cluster.options[:local_threshold] || LOCAL_THRESHOLD
  @server_selection_timeout = cluster.options[:server_selection_timeout] || SERVER_SELECTION_TIMEOUT
  deadline = Time.now + server_selection_timeout
  while (deadline - Time.now) > 0
    servers = candidates(cluster)
    if servers && !servers.compact.empty?
      server = servers.first
      # There is no point pinging a standalone as the subsequent scan is
      # not going to change anything about the cluster.
      if ping && !cluster.single?
        return server if server.connectable?
      else
        return server
      end
    end
    cluster.scan!
  end
  raise Error::NoServerAvailable.new(self)
end

#server_selection_timeoutFloat

Deprecated.

This setting is now taken from the cluster options when a server is selected. Will be removed in 3.0.

Get the timeout for server selection.

Examples:

Get the server selection timeout, in seconds.

selector.server_selection_timeout

Returns:

  • (Float)

    The timeout.

Since:

  • 2.0.0



129
130
131
132
# File 'lib/mongo/server_selector/selectable.rb', line 129

def server_selection_timeout
  @server_selection_timeout ||=
    (options[:server_selection_timeout] || ServerSelector::SERVER_SELECTION_TIMEOUT)
end