Skip to content

Commit 6bb0f15

Browse files
author
Divjot Arora
committed
GODRIVER-1549 Select all servers when an empty tag set is given (#352)
1 parent df8f93d commit 6bb0f15

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

x/mongo/driver/description/selector_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/google/go-cmp/cmp"
1414
"github.com/stretchr/testify/require"
15+
"go.mongodb.org/mongo-driver/internal/testutil/assert"
1516
"go.mongodb.org/mongo-driver/mongo/readpref"
1617
"go.mongodb.org/mongo-driver/tag"
1718
"go.mongodb.org/mongo-driver/x/mongo/driver/address"
@@ -506,6 +507,43 @@ func TestSelector_Secondary_with_tags(t *testing.T) {
506507
require.Equal([]Server{readPrefTestSecondary2}, result)
507508
}
508509

510+
func TestSelector_Secondary_with_empty_tag_set(t *testing.T) {
511+
t.Parallel()
512+
513+
primaryNoTags := Server{
514+
Addr: address.Address("localhost:27017"),
515+
Kind: RSPrimary,
516+
WireVersion: &VersionRange{Min: 0, Max: 5},
517+
}
518+
firstSecondaryNoTags := Server{
519+
Addr: address.Address("localhost:27018"),
520+
Kind: RSSecondary,
521+
WireVersion: &VersionRange{Min: 0, Max: 5},
522+
}
523+
secondSecondaryNoTags := Server{
524+
Addr: address.Address("localhost:27019"),
525+
Kind: RSSecondary,
526+
WireVersion: &VersionRange{Min: 0, Max: 5},
527+
}
528+
topologyNoTags := Topology{
529+
Kind: ReplicaSetWithPrimary,
530+
Servers: []Server{primaryNoTags, firstSecondaryNoTags, secondSecondaryNoTags},
531+
}
532+
533+
nonMatchingSet := tag.Set{
534+
{Name: "foo", Value: "bar"},
535+
}
536+
emptyTagSet := tag.Set{}
537+
rp := readpref.Secondary(
538+
readpref.WithTagSets(nonMatchingSet, emptyTagSet),
539+
)
540+
541+
result, err := ReadPrefSelector(rp).SelectServer(topologyNoTags, topologyNoTags.Servers)
542+
assert.Nil(t, err, "SelectServer error: %v", err)
543+
expectedResult := []Server{firstSecondaryNoTags, secondSecondaryNoTags}
544+
assert.Equal(t, expectedResult, result, "expected result %v, got %v", expectedResult, result)
545+
}
546+
509547
func TestSelector_Secondary_with_tags_that_do_not_match(t *testing.T) {
510548
t.Parallel()
511549

x/mongo/driver/description/server_selector.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,15 @@ func selectByTagSet(candidates []Server, tagSets []tag.Set) []Server {
223223
}
224224

225225
for _, ts := range tagSets {
226+
// If this tag set is empty, we can take a fast path because the empty list is a subset of all tag sets, so
227+
// all candidate servers will be selected.
228+
if len(ts) == 0 {
229+
return candidates
230+
}
231+
226232
var results []Server
227233
for _, s := range candidates {
234+
// ts is non-empty, so only servers with a non-empty set of tags need to be checked.
228235
if len(s.Tags) > 0 && s.Tags.ContainsAll(ts) {
229236
results = append(results, s)
230237
}

0 commit comments

Comments
 (0)