lib/mongo/collection/view/aggregation.rb in mongo-2.1.0.beta vs lib/mongo/collection/view/aggregation.rb in mongo-2.1.0.rc0

- old
+ new

@@ -23,10 +23,11 @@ extend Forwardable include Enumerable include Immutable include Iterable include Explainable + include Loggable # @return [ View ] view The collection view. attr_reader :view # @return [ Array<Hash> ] pipeline The aggregation pipeline. attr_reader :pipeline @@ -130,19 +131,23 @@ def initial_query_op Operation::Aggregate.new(aggregate_spec) end + def valid_server?(server) + server.standalone? || server.mongos? || server.primary? || secondary_ok? + end + + def secondary_ok? + pipeline.none? { |op| op.key?('$out') || op.key?(:$out) } + end + def send_initial_query(server) - begin - initial_query_op.execute(server.context) - rescue Mongo::Error::NeedPrimaryServer - log_warn([ - 'Rerouting the Aggregation operation to the primary server.' - ]) - server = ServerSelector.get(mode: :primary).select_server(cluster) - initial_query_op.execute(server.context) + unless valid_server?(server) + log_warn('Rerouting the Aggregation operation to the primary server.') + server = cluster.next_primary end + initial_query_op.execute(server.context) end end end end end