lib/mongo/cluster/topology/replica_set.rb in mongo-2.4.0.rc1 vs lib/mongo/cluster/topology/replica_set.rb in mongo-2.4.0

- old
+ new

@@ -19,19 +19,23 @@ # Defines behaviour when a cluster is in replica set topology. # # @since 2.0.0 class ReplicaSet include Loggable + include Monitoring::Publishable # Constant for the replica set name configuration option. # # @since 2.0.0 REPLICA_SET_NAME = :replica_set.freeze # @return [ Hash ] options The options. attr_reader :options + # @return [ Monitoring ] monitoring The monitoring. + attr_reader :monitoring + # The display name for the topology. # # @since 2.0.0 NAME = 'Replica Set'.freeze @@ -59,11 +63,10 @@ # # @return [ ReplicaSet ] The topology. def elect_primary(description, servers) if description.replica_set_name == replica_set_name unless detect_stale_primary!(description) - log_debug("Server #{description.address.to_s} elected as primary in #{replica_set_name}.") servers.each do |server| if server.primary? && server.address != description.address server.description.unknown! end end @@ -77,20 +80,55 @@ ) end self end + # Determine if the topology would select a readable server for the + # provided candidates and read preference. + # + # @example Is a readable server present? + # topology.has_readable_server?(cluster, server_selector) + # + # @param [ Cluster ] cluster The cluster. + # @param [ ServerSelector ] server_selector The server + # selector. + # + # @return [ true, false ] If a readable server is present. + # + # @since 2.4.0 + def has_readable_server?(cluster, server_selector = nil) + (server_selector || ServerSelector.get(mode: :primary)).candidates(cluster).any? + end + + # Determine if the topology would select a writable server for the + # provided candidates. + # + # @example Is a writable server present? + # topology.has_writable_server?(servers) + # + # @param [ Cluster ] cluster The cluster. + # + # @return [ true, false ] If a writable server is present. + # + # @since 2.4.0 + def has_writable_server?(cluster) + cluster.servers.any?{ |server| server.primary? } + end + # Initialize the topology with the options. # # @example Initialize the topology. # ReplicaSet.new(options) # # @param [ Hash ] options The options. + # @param [ Monitoring ] monitoring The monitoring. + # @param [ Array<String> ] seeds The seeds. # # @since 2.0.0 - def initialize(options, seeds = []) + def initialize(options, monitoring, seeds = []) @options = options + @monitoring = monitoring @max_election_id = nil @max_set_version = nil end # A replica set topology is a replica set. @@ -219,9 +257,17 @@ # # @return [ Topology::ReplicaSet ] Always returns self. # # @since 2.0.6 def standalone_discovered; self; end + + # Notify the topology that a member was discovered. + # + # @example Notify the topology that a member was discovered. + # topology.member_discovered + # + # @since 2.4.0 + def member_discovered; end; private def update_max_election_id(description) if description.election_id &&