Skip to content

Commit 34dde80

Browse files
authored
GODRIVER-2205 Retain empty tag sets when building a read preference document. (#787)
1 parent 02572e2 commit 34dde80

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

x/mongo/driver/operation.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,9 +1257,6 @@ func (op Operation) createReadPref(desc description.SelectedServer, isOpQuery bo
12571257

12581258
sets := make([]bsoncore.Document, 0, len(rp.TagSets()))
12591259
for _, ts := range rp.TagSets() {
1260-
if len(ts) == 0 {
1261-
continue
1262-
}
12631260
i, set := bsoncore.AppendDocumentStart(nil)
12641261
for _, t := range ts {
12651262
set = bsoncore.AppendStringElement(set, t.Name, t.Value)

x/mongo/driver/operation_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"go.mongodb.org/mongo-driver/mongo/readconcern"
1818
"go.mongodb.org/mongo-driver/mongo/readpref"
1919
"go.mongodb.org/mongo-driver/mongo/writeconcern"
20+
"go.mongodb.org/mongo-driver/tag"
2021
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2122
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
2223
"go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
@@ -398,6 +399,25 @@ func TestOperation(t *testing.T) {
398399
readpref.SecondaryPreferred(readpref.WithTags("disk", "ssd", "use", "reporting")),
399400
description.RSSecondary, description.ReplicaSet, false, rpWithTags,
400401
},
402+
// GODRIVER-2205: Ensure empty tag sets are written as an empty document in the read
403+
// preference document. Empty tag sets match any server and are used as a fallback when
404+
// no other tag sets match any servers.
405+
{
406+
"secondaryPreferred/withTags/emptyTagSet",
407+
readpref.SecondaryPreferred(readpref.WithTagSets(
408+
tag.Set{{Name: "disk", Value: "ssd"}},
409+
tag.Set{})),
410+
description.RSSecondary,
411+
description.ReplicaSet,
412+
false,
413+
bsoncore.NewDocumentBuilder().
414+
AppendString("mode", "secondaryPreferred").
415+
AppendArray("tags", bsoncore.NewArrayBuilder().
416+
AppendDocument(bsoncore.NewDocumentBuilder().AppendString("disk", "ssd").Build()).
417+
AppendDocument(bsoncore.NewDocumentBuilder().Build()).
418+
Build()).
419+
Build(),
420+
},
401421
{
402422
"secondaryPreferred/withMaxStaleness",
403423
readpref.SecondaryPreferred(readpref.WithMaxStaleness(25 * time.Second)),

0 commit comments

Comments
 (0)