From 3fb76e66a72fbb3466c806d1bd3b720b2b42025b Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Sun, 2 Jul 2017 01:03:42 -0400 Subject: [PATCH 1/5] fix running test on mongo 3.2 --- harness/daemons/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/harness/daemons/.env b/harness/daemons/.env index 96ee89e94..87325942a 100644 --- a/harness/daemons/.env +++ b/harness/daemons/.env @@ -43,7 +43,7 @@ COMMONSOPTS=" if versionAtLeast 3 2; then # 3.2 doesn't like --nojournal on config servers. - #COMMONCOPTS="$(echo "$COMMONCOPTS" | sed '/--nojournal/d')" + COMMONCOPTS="$(echo "$COMMONCOPTS" | sed '/--nojournal/d')" # Using a hacked version of MongoDB 3.2 for now. # Go back to MMAPv1 so it's not super sluggish. :-( From 531026af78bc767da55c13c832757480a26dc7a3 Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Sun, 2 Jul 2017 19:55:06 -0400 Subject: [PATCH 2/5] Added Hint and MaxTimeMS support to Count() --- session.go | 16 +++++++++++----- session_test.go | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/session.go b/session.go index 5801e225c..fc0e2c156 100644 --- a/session.go +++ b/session.go @@ -3941,10 +3941,12 @@ func (iter *Iter) getMoreCmd() *queryOp { } type countCmd struct { - Count string - Query interface{} - Limit int32 ",omitempty" - Skip int32 ",omitempty" + Count string + Query interface{} + Limit int32 ",omitempty" + Skip int32 ",omitempty" + Hint bson.D `bson:"hint,omitempty"` + MaxTimeMS int `bson:"maxTimeMS,omitempty"` } // Count returns the total number of documents in the result set. @@ -3966,8 +3968,12 @@ func (q *Query) Count() (n int, err error) { if query == nil { query = bson.D{} } + // not checking the error because if type assertion fails, we + // simply want a Zero bson.D + hint, _ := q.op.options.Hint.(bson.D) result := struct{ N int }{} - err = session.DB(dbname).Run(countCmd{cname, query, limit, op.skip}, &result) + err = session.DB(dbname).Run(countCmd{cname, query, limit, op.skip, hint, op.options.MaxTimeMS}, &result) + return result.N, err } diff --git a/session_test.go b/session_test.go index 86c8dfa8c..f3ccb665c 100644 --- a/session_test.go +++ b/session_test.go @@ -1187,6 +1187,41 @@ func (s *S) TestCountSkipLimit(c *C) { c.Assert(n, Equals, 4) } +func (s *S) TestCountMaxTimeMS(c *C) { + session, err := mgo.Dial("localhost:40001") + c.Assert(err, IsNil) + defer session.Close() + + coll := session.DB("mydb").C("mycoll") + + ns := make([]int, 100000) + for _, n := range ns { + err := coll.Insert(M{"n": n}) + c.Assert(err, IsNil) + } + _, err = coll.Find(M{"n": M{"$gt": 1}}).SetMaxTime(1 * time.Millisecond).Count() + e := err.(*mgo.QueryError) + // We hope this query took longer than 1 ms, which triggers an error code 50 + c.Assert(e.Code, Equals, 50) +} + +func (s *S) TestCountHint(c *C) { + session, err := mgo.Dial("localhost:40001") + c.Assert(err, IsNil) + defer session.Close() + + coll := session.DB("mydb").C("mycoll") + + err = coll.Insert(M{"n": 1}) + c.Assert(err, IsNil) + + _, err = coll.Find(M{"n": M{"$gt": 1}}).Hint("does_not_exists").Count() + e := err.(*mgo.QueryError) + // If Hint wasn't doing anything, then Count would ignore the non existent index hint + // and return the normal ount. But we instead get an error code 2: bad hint + c.Assert(e.Code, Equals, 2) +} + func (s *S) TestQueryExplain(c *C) { session, err := mgo.Dial("localhost:40001") c.Assert(err, IsNil) @@ -4159,11 +4194,11 @@ func (s *S) TestBypassValidation(c *C) { func (s *S) TestVersionAtLeast(c *C) { tests := [][][]int{ - {{3,2,1}, {3,2,0}}, - {{3,2,1}, {3,2}}, - {{3,2,1}, {2,5,5,5}}, - {{3,2,1}, {2,5,5}}, - {{3,2,1}, {2,5}}, + {{3, 2, 1}, {3, 2, 0}}, + {{3, 2, 1}, {3, 2}}, + {{3, 2, 1}, {2, 5, 5, 5}}, + {{3, 2, 1}, {2, 5, 5}}, + {{3, 2, 1}, {2, 5}}, } for _, pair := range tests { bi := mgo.BuildInfo{VersionArray: pair[0]} From 52674936cbc838eaf4d7e23c0a661c9155888eb6 Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Sun, 2 Jul 2017 21:00:17 -0400 Subject: [PATCH 3/5] Both features only wrk starting on 2.6 technically speaking, 2.5.5 for the Hint feature --- harness/daemons/.env | 2 +- session_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/harness/daemons/.env b/harness/daemons/.env index 87325942a..96ee89e94 100644 --- a/harness/daemons/.env +++ b/harness/daemons/.env @@ -43,7 +43,7 @@ COMMONSOPTS=" if versionAtLeast 3 2; then # 3.2 doesn't like --nojournal on config servers. - COMMONCOPTS="$(echo "$COMMONCOPTS" | sed '/--nojournal/d')" + #COMMONCOPTS="$(echo "$COMMONCOPTS" | sed '/--nojournal/d')" # Using a hacked version of MongoDB 3.2 for now. # Go back to MMAPv1 so it's not super sluggish. :-( diff --git a/session_test.go b/session_test.go index f3ccb665c..55dd2ce41 100644 --- a/session_test.go +++ b/session_test.go @@ -1188,6 +1188,10 @@ func (s *S) TestCountSkipLimit(c *C) { } func (s *S) TestCountMaxTimeMS(c *C) { + if !s.versionAtLeast(2, 6) { + c.Skip("SetMaxTime only supported in 2.6+") + } + session, err := mgo.Dial("localhost:40001") c.Assert(err, IsNil) defer session.Close() @@ -1203,15 +1207,19 @@ func (s *S) TestCountMaxTimeMS(c *C) { e := err.(*mgo.QueryError) // We hope this query took longer than 1 ms, which triggers an error code 50 c.Assert(e.Code, Equals, 50) + } func (s *S) TestCountHint(c *C) { + if !s.versionAtLeast(2, 6) { + c.Skip("Not implemented until mongo 2.5.5 https://jira.mongodb.org/browse/SERVER-2677") + } + session, err := mgo.Dial("localhost:40001") c.Assert(err, IsNil) defer session.Close() coll := session.DB("mydb").C("mycoll") - err = coll.Insert(M{"n": 1}) c.Assert(err, IsNil) From a399f95249512b47e7a01b0ff32f01abd6565b31 Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Sun, 2 Jul 2017 21:22:17 -0400 Subject: [PATCH 4/5] See if cleaning up mongo instances fixes the build travis is failing for most (all?) PRs even when the exit code is 0. This only happens for the two older mongo versions --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index e0c765158..f0a302480 100644 --- a/.travis.yml +++ b/.travis.yml @@ -41,5 +41,6 @@ script: - (cd bson && go test -check.v) - go test -check.v -fast - (cd txn && go test -check.v) + - make stopdb # vim:sw=4:ts=4:et From 08e7443d4a2725137d9398312a796949687fdf4d Mon Sep 17 00:00:00 2001 From: Dom Dwyer Date: Wed, 26 Jul 2017 10:14:46 +0100 Subject: [PATCH 5/5] Credit @fmpwizard in the README. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d8cf8cc1..e5f37adce 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili * Fixes timezone handling ([details](https://github.com/go-mgo/mgo/pull/464)) * Improved multi-document transaction performance ([details](https://github.com/globalsign/mgo/pull/10), [more](https://github.com/globalsign/mgo/pull/11), [more](https://github.com/globalsign/mgo/pull/16)) * Fixes cursor timeouts ([detials](https://jira.mongodb.org/browse/SERVER-24899)) +* Support index hints and timeouts for count queries ([details](https://github.com/globalsign/mgo/pull/17)) --- @@ -25,8 +26,9 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili * @BenLubar * @carter2000 * @cezarsa -* @eaglerayp * @drichelson +* @eaglerayp +* @fmpwizard * @jameinel * @Reenjii * @smoya