Skip to content

Commit

Permalink
GODRIVER-1937 Update legacy ListCollections to support the BatchSize …
Browse files Browse the repository at this point in the history
…option for server version 2.6 (mongodb#656)
  • Loading branch information
matthewdale authored and tsedgwick committed Jun 1, 2021
1 parent e9e928c commit a4d58e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
29 changes: 19 additions & 10 deletions mongo/integration/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,15 @@ func TestDatabase(t *testing.T) {
})
}
})
mt.RunOpts("batch size", mtest.NewOptions().MinServerVersion("3.0"), func(mt *mtest.T) {

// For server versions below 3.0, we internally execute ListCollections() as a legacy
// OP_QUERY against the system.namespaces collection. Command monitoring upconversions
// translate this to a "find" command rather than "listCollections".
cmdMonitoringCmdName := "listCollections"
if mtest.CompareServerVersions(mtest.ServerVersion(), "3.0") < 0 {
cmdMonitoringCmdName = "find"
}
mt.Run("batch size", func(mt *mtest.T) {
// Create two new collections so there will be three total.
createCollections(mt, 2)

Expand All @@ -231,24 +239,25 @@ func TestDatabase(t *testing.T) {
assert.Nil(mt, err, "ListCollectionNames error: %v", err)

evt := mt.GetStartedEvent()
assert.Equal(mt, "listCollections", evt.CommandName, "expected 'listCollections' command to be sent, got %q",
assert.Equal(
mt,
cmdMonitoringCmdName,
evt.CommandName,
"expected %q command to be sent, got %q",
cmdMonitoringCmdName,
evt.CommandName)
_, err = evt.Command.LookupErr("cursor", "batchSize")
assert.Nil(mt, err, "expected command %s to contain key 'batchSize'", evt.Command)
})

// The BatchSize option is not honored for ListCollections operations on server version 2.6 due to an
// inconsistency in the legacy OP_QUERY code path (GODRIVER-1937).
cmdMonitoringMtOpts := mtest.NewOptions().MinServerVersion("3.0")
mt.RunOpts("getMore commands are monitored", cmdMonitoringMtOpts, func(mt *mtest.T) {
mt.Run("getMore commands are monitored", func(mt *mtest.T) {
createCollections(mt, 2)
assertGetMoreCommandsAreMonitored(mt, "listCollections", func() (*mongo.Cursor, error) {
assertGetMoreCommandsAreMonitored(mt, cmdMonitoringCmdName, func() (*mongo.Cursor, error) {
return mt.DB.ListCollections(mtest.Background, bson.D{}, options.ListCollections().SetBatchSize(2))
})
})
mt.RunOpts("killCursors commands are monitored", cmdMonitoringMtOpts, func(mt *mtest.T) {
mt.Run("killCursors commands are monitored", func(mt *mtest.T) {
createCollections(mt, 2)
assertKillCursorsCommandsAreMonitored(mt, "listCollections", func() (*mongo.Cursor, error) {
assertKillCursorsCommandsAreMonitored(mt, cmdMonitoringCmdName, func() (*mongo.Cursor, error) {
return mt.DB.ListCollections(mtest.Background, bson.D{}, options.ListCollections().SetBatchSize(2))
})
})
Expand Down
7 changes: 6 additions & 1 deletion x/mongo/driver/operation_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,17 @@ func (op Operation) createLegacyListCollectionsWiremessage(dst []byte, desc desc
optsElems = bsoncore.AppendDocumentElement(optsElems, "$readPreference", rp)
}

var batchSize int32
if val, ok := cmdDoc.Lookup("cursor", "batchSize").AsInt32OK(); ok {
batchSize = val
}

var wmIdx int32
wmIdx, dst = wiremessage.AppendHeaderStart(dst, info.requestID, 0, wiremessage.OpQuery)
dst = wiremessage.AppendQueryFlags(dst, op.slaveOK(desc))
dst = wiremessage.AppendQueryFullCollectionName(dst, op.getFullCollectionName(listCollectionsNamespace))
dst = wiremessage.AppendQueryNumberToSkip(dst, 0)
dst = wiremessage.AppendQueryNumberToReturn(dst, 0)
dst = wiremessage.AppendQueryNumberToReturn(dst, batchSize)
dst = op.appendLegacyQueryDocument(dst, filter, optsElems)
// leave out returnFieldsSelector because it is optional

Expand Down

0 comments on commit a4d58e6

Please sign in to comment.