# File lib/mongo/server_selector/selectable.rb, line 157 def primary(candidates) candidates.select do |server| server.primary? end end
module Mongo::ServerSelector::Selectable
Provides common behavior for filtering a list of servers by server mode or tag set.
@since 2.0.0
Attributes
@return [ Hash ] options The options.
@return [ Array ] #tag_sets The tag sets used to select servers.
Public Class Methods
Initialize the server selector.
@example Initialize the selector.
Mongo::ServerSelector::Secondary.new(:tag_sets => [{'dc' => 'nyc'}])
@example Initialize the preference with no options.
Mongo::ServerSelector::Secondary.new
@param [ Hash ] options The server preference options.
@option options [ Integer ] :server_selection_timeout The timeout in seconds
for selecting a server.
@option options [ Integer ] :local_threshold The local threshold boundary for
nearest selection in seconds.
@raise [ Error::InvalidServerPreference ] If tag sets are specified
but not allowed.
@since 2.0.0
# File lib/mongo/server_selector/selectable.rb, line 63 def initialize(options = {}) @options = (options || {}).freeze tag_sets = options[:tag_sets] || [] validate_tag_sets!(tag_sets) @tag_sets = tag_sets.freeze end
Public Instance Methods
Check equality of two server selector.
@example Check server selector equality.
preference == other
@param [ Object ] other The other preference.
@return [ true, false ] Whether the objects are equal.
@since 2.0.0
# File lib/mongo/server_selector/selectable.rb, line 39 def ==(other) name == other.name && tag_sets == other.tag_sets end
Inspect the server selector.
@example Inspect the server selector.
selector.inspect
@return [ String ] The inspection.
@since 2.2.0
# File lib/mongo/server_selector/selectable.rb, line 78 def inspect "#<#{self.class.name}:0x#{object_id} tag_sets=#{tag_sets.inspect} " + "server_selection_timeout=#{server_selection_timeout} local_threshold=#{local_threshold}>" end
Get the local threshold boundary for nearest selection in seconds.
@example Get the local threshold.
selector.local_threshold
@return [ Float ] The local threshold.
@since 2.0.0
# File lib/mongo/server_selector/selectable.rb, line 133 def local_threshold @local_threshold ||= (options[:local_threshold] || ServerSelector::LOCAL_THRESHOLD) end
Select a server from eligible candidates.
@example Select a server from the cluster.
selector.select_server(cluster)
@param [ Mongo::Cluster ] cluster The cluster from which to select an eligible server.
@return [ Mongo::Server ] A server matching the server preference.
@since 2.0.0
# File lib/mongo/server_selector/selectable.rb, line 93 def select_server(cluster, ping = true) 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
Get the timeout for server selection.
@example Get the server selection timeout, in seconds.
selector.server_selection_timeout
@return [ Float ] The timeout.
@since 2.0.0
# File lib/mongo/server_selector/selectable.rb, line 120 def server_selection_timeout @server_selection_timeout ||= (options[:server_selection_timeout] || ServerSelector::SERVER_SELECTION_TIMEOUT) end
Private Instance Methods
# File lib/mongo/server_selector/selectable.rb, line 139 def candidates(cluster) if cluster.single? cluster.servers elsif cluster.sharded? near_servers(cluster.servers) else select(cluster.servers) end end
Select the servers matching the defined tag sets.
@param [ Array ] candidates List of candidate servers from which those
matching the defined tag sets should be selected.
@return [ Array ] The servers matching the defined tag sets.
@since 2.0.0
# File lib/mongo/server_selector/selectable.rb, line 201 def match_tag_sets(candidates) matches = [] tag_sets.find do |tag_set| matches = candidates.select { |server| server.matches_tag_set?(tag_set) } !matches.empty? end matches || [] end
Select the near servers from a list of provided candidates, taking the
local threshold into account.
@param [ Array ] candidates List of candidate servers to select the
near servers from.
@return [ Array ] The near servers.
@since 2.0.0
# File lib/mongo/server_selector/selectable.rb, line 186 def near_servers(candidates = []) return candidates if candidates.empty? nearest_server = candidates.min_by(&:average_round_trip_time) threshold = nearest_server.average_round_trip_time + (local_threshold * 1000) candidates.select { |server| server.average_round_trip_time <= threshold }.shuffle! end
Select the primary from a list of provided candidates.
@param [ Array ] candidates List of candidate servers to select the
primary from.
@return [ Array ] The primary.
@since 2.0.0
Select the secondaries from a list of provided candidates.
@param [ Array ] candidates List of candidate servers to select the
secondaries from.
@return [ Array ] The secondary servers.
@since 2.0.0
# File lib/mongo/server_selector/selectable.rb, line 171 def secondaries(candidates) matching_servers = candidates.select(&:secondary?) matching_servers = match_tag_sets(matching_servers) unless tag_sets.empty? matching_servers end
# File lib/mongo/server_selector/selectable.rb, line 210 def validate_tag_sets!(tag_sets) if !tag_sets.all? { |set| set.empty? } && !tags_allowed? raise Error::InvalidServerPreference.new(name) end end