Skip to content

RUBY-2841 Ignore read pref if any pre-5.0 #2370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/mongo/server_selector/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,21 @@ def select_server(cluster, ping = nil, session = nil, write_aggregation: false)
#
# @api private
def try_select_server(cluster, write_aggregation: false)
servers = suitable_servers(cluster)
servers = if write_aggregation && cluster.replica_set?
# 1. Check if ALL servers in cluster support secondary writes.
is_write_supported = cluster.servers.reduce(true) do |res, server|
res && server.features.merge_out_on_secondary_enabled?
end

if write_aggregation && cluster.replica_set?
# If secondary preferred, list of servers has is ordered in a way
# that secondaries go first, and the primary is the last one.
servers.select! do |server|
server.features.merge_out_on_secondary_enabled? || server.primary?
if is_write_supported
# 2. If all servers support secondary writes, we respect read preference.
suitable_servers(cluster)
else
# 3. Otherwise we fallback to primary for replica set.
[cluster.servers.detect(&:primary?)]
end
servers = [cluster.servers.detect(&:primary?)] if servers.empty?
else
suitable_servers(cluster)
end

# This list of servers may be ordered in a specific way
Expand Down