-
Notifications
You must be signed in to change notification settings - Fork 532
Fix RUBY-2558 :secondary_preferred read preference in 3.6+ sharded clusters performs primary read #2200
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
Fix RUBY-2558 :secondary_preferred read preference in 3.6+ sharded clusters performs primary read #2200
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Secondary reads' do | ||
before do | ||
root_authorized_client.use('sr')['secondary_reads'].drop | ||
root_authorized_client.use('sr')['secondary_reads'].insert_one(test: 1) | ||
end | ||
|
||
shared_examples 'performs reads as per read preference' do | ||
|
||
%i(primary primary_preferred).each do |mode| | ||
|
||
context mode.inspect do | ||
|
||
let(:client) do | ||
root_authorized_client.with(read: {mode: mode}).use('sr') | ||
end | ||
|
||
it 'reads from primary' do | ||
start_stats = get_read_counters | ||
|
||
30.times do | ||
client['secondary_reads'].find.to_a | ||
end | ||
|
||
end_stats = get_read_counters | ||
|
||
end_stats[:secondary].should be_within(10).of(start_stats[:secondary]) | ||
end_stats[:primary].should >= start_stats[:primary] + 30 | ||
end | ||
end | ||
end | ||
|
||
%i(secondary secondary_preferred).each do |mode| | ||
|
||
context mode.inspect do | ||
let(:client) do | ||
root_authorized_client.with(read: {mode: mode}).use('sr') | ||
end | ||
|
||
it 'reads from secondaries' do | ||
start_stats = get_read_counters | ||
|
||
30.times do | ||
client['secondary_reads'].find.to_a | ||
end | ||
|
||
end_stats = get_read_counters | ||
|
||
end_stats[:primary].should be_within(10).of(start_stats[:primary]) | ||
end_stats[:secondary].should >= start_stats[:secondary] + 30 | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'replica set' do | ||
require_topology :replica_set | ||
|
||
include_examples 'performs reads as per read preference' | ||
end | ||
|
||
context 'sharded cluster' do | ||
require_topology :sharded | ||
|
||
include_examples 'performs reads as per read preference' | ||
end | ||
|
||
def get_read_counters | ||
client = ClientRegistry.instance.global_client('root_authorized') | ||
addresses = [] | ||
if client.cluster.sharded? | ||
doc = client.use('admin').command(listShards: 1).documents.first | ||
doc['shards'].each do |shard| | ||
addresses += shard['host'].split('/').last.split(',') | ||
end | ||
else | ||
client.cluster.servers.each do |server| | ||
next unless server.primary? || server.secondary? | ||
addresses << server.address.seed | ||
end | ||
end | ||
stats = Hash.new(0) | ||
addresses.each do |address| | ||
ClientRegistry.instance.new_local_client( | ||
[address], | ||
SpecConfig.instance.all_test_options.merge(connect: :direct), | ||
) do |c| | ||
server = c.cluster.servers.first | ||
next unless server.primary? || server.secondary? | ||
stat = c.command(serverStatus: 1).documents.first | ||
queries = stat['opcounters']['query'] | ||
if server.primary? | ||
stats[:primary] += queries | ||
else | ||
stats[:secondary] += queries | ||
end | ||
end | ||
end | ||
stats | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question without knowing internal semantics, why does this require
|| false
to be included? Asking becauseA || false
will always evaluate toA
in boolean algebra, so I'm wondering about what Ruby does whenread
evaluates to a false-ish value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
|| false
converts nil to false (whenread
is nil, in this case).