Skip to content

GODRIVER-1549 Select all servers when an empty tag set is given #352

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
Show file tree
Hide file tree
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
38 changes: 38 additions & 0 deletions x/mongo/driver/description/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/tag"
"go.mongodb.org/mongo-driver/x/mongo/driver/address"
Expand Down Expand Up @@ -506,6 +507,43 @@ func TestSelector_Secondary_with_tags(t *testing.T) {
require.Equal([]Server{readPrefTestSecondary2}, result)
}

func TestSelector_Secondary_with_empty_tag_set(t *testing.T) {
t.Parallel()

primaryNoTags := Server{
Addr: address.Address("localhost:27017"),
Kind: RSPrimary,
WireVersion: &VersionRange{Min: 0, Max: 5},
}
firstSecondaryNoTags := Server{
Addr: address.Address("localhost:27018"),
Kind: RSSecondary,
WireVersion: &VersionRange{Min: 0, Max: 5},
}
secondSecondaryNoTags := Server{
Addr: address.Address("localhost:27019"),
Kind: RSSecondary,
WireVersion: &VersionRange{Min: 0, Max: 5},
}
topologyNoTags := Topology{
Kind: ReplicaSetWithPrimary,
Servers: []Server{primaryNoTags, firstSecondaryNoTags, secondSecondaryNoTags},
}

nonMatchingSet := tag.Set{
{Name: "foo", Value: "bar"},
}
emptyTagSet := tag.Set{}
rp := readpref.Secondary(
readpref.WithTagSets(nonMatchingSet, emptyTagSet),
)

result, err := ReadPrefSelector(rp).SelectServer(topologyNoTags, topologyNoTags.Servers)
assert.Nil(t, err, "SelectServer error: %v", err)
expectedResult := []Server{firstSecondaryNoTags, secondSecondaryNoTags}
assert.Equal(t, expectedResult, result, "expected result %v, got %v", expectedResult, result)
}

func TestSelector_Secondary_with_tags_that_do_not_match(t *testing.T) {
t.Parallel()

Expand Down
7 changes: 7 additions & 0 deletions x/mongo/driver/description/server_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,15 @@ func selectByTagSet(candidates []Server, tagSets []tag.Set) []Server {
}

for _, ts := range tagSets {
// If this tag set is empty, we can take a fast path because the empty list is a subset of all tag sets, so
// all candidate servers will be selected.
if len(ts) == 0 {
return candidates
}

var results []Server
for _, s := range candidates {
// ts is non-empty, so only servers with a non-empty set of tags need to be checked.
if len(s.Tags) > 0 && s.Tags.ContainsAll(ts) {
results = append(results, s)
}
Expand Down