Skip to content

Commit 362ae10

Browse files
committed
Fix the IsDup retry error handling on Apply.
Reported by Cezar Sá Espinola.
1 parent 0108465 commit 362ae10

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

session.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4305,11 +4305,10 @@ func (q *Query) Apply(change Change, result interface{}) (info *ChangeInfo, err
43054305
var doc valueResult
43064306
for i := 0; i < maxUpsertRetries; i++ {
43074307
err = session.DB(dbname).Run(&cmd, &doc)
4308-
43094308
if err == nil {
43104309
break
43114310
}
4312-
if change.Upsert && IsDup(err) {
4311+
if change.Upsert && IsDup(err) && i+1 < maxUpsertRetries {
43134312
// Retry duplicate key errors on upserts.
43144313
// https://docs.mongodb.com/v3.2/reference/method/db.collection.update/#use-unique-indexes
43154314
continue

session_test.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,26 @@ func (s *S) TestIsDupFindAndModify(c *C) {
10011001
c.Assert(mgo.IsDup(err), Equals, true)
10021002
}
10031003

1004+
func (s *S) TestIsDupRetryUpsert(c *C) {
1005+
session, err := mgo.Dial("localhost:40001")
1006+
c.Assert(err, IsNil)
1007+
defer session.Close()
1008+
1009+
coll := session.DB("mydb").C("mycoll")
1010+
1011+
err = coll.Insert(bson.M{"_id": 1, "x": 1})
1012+
c.Assert(err, IsNil)
1013+
1014+
_, err = coll.Upsert(bson.M{"_id": 1, "x": 2}, bson.M{"$set": bson.M{"x": 3}})
1015+
c.Assert(mgo.IsDup(err), Equals, true)
1016+
1017+
_, err = coll.Find(bson.M{"_id": 1, "x": 2}).Apply(mgo.Change{
1018+
Update: bson.M{"$set": bson.M{"x": 3}},
1019+
Upsert: true,
1020+
}, nil)
1021+
c.Assert(mgo.IsDup(err), Equals, true)
1022+
}
1023+
10041024
func (s *S) TestFindAndModify(c *C) {
10051025
session, err := mgo.Dial("localhost:40011")
10061026
c.Assert(err, IsNil)
@@ -4159,11 +4179,11 @@ func (s *S) TestBypassValidation(c *C) {
41594179

41604180
func (s *S) TestVersionAtLeast(c *C) {
41614181
tests := [][][]int{
4162-
{{3,2,1}, {3,2,0}},
4163-
{{3,2,1}, {3,2}},
4164-
{{3,2,1}, {2,5,5,5}},
4165-
{{3,2,1}, {2,5,5}},
4166-
{{3,2,1}, {2,5}},
4182+
{{3, 2, 1}, {3, 2, 0}},
4183+
{{3, 2, 1}, {3, 2}},
4184+
{{3, 2, 1}, {2, 5, 5, 5}},
4185+
{{3, 2, 1}, {2, 5, 5}},
4186+
{{3, 2, 1}, {2, 5}},
41674187
}
41684188
for _, pair := range tests {
41694189
bi := mgo.BuildInfo{VersionArray: pair[0]}

0 commit comments

Comments
 (0)