Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-1937 Update legacy ListCollections to support the BatchSize option for server version 2.6 #656

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions mongo/integration/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,32 @@ func TestDatabase(t *testing.T) {
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")
cmdMonitoringMtOpts := mtest.NewOptions().MinServerVersion("2.6")
matthewdale marked this conversation as resolved.
Show resolved Hide resolved
mt.RunOpts("getMore commands are monitored", cmdMonitoringMtOpts, func(mt *mtest.T) {
createCollections(mt, 2)
assertGetMoreCommandsAreMonitored(mt, "listCollections", func() (*mongo.Cursor, error) {

matthewdale marked this conversation as resolved.
Show resolved Hide resolved
matthewdale marked this conversation as resolved.
Show resolved Hide resolved
// 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".
matthewdale marked this conversation as resolved.
Show resolved Hide resolved
cmdName := "listCollections"
if mtest.CompareServerVersions(mtest.ServerVersion(), "3.0") < 0 {
cmdName = "find"
}
assertGetMoreCommandsAreMonitored(mt, cmdName, 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) {
createCollections(mt, 2)
assertKillCursorsCommandsAreMonitored(mt, "listCollections", func() (*mongo.Cursor, error) {

matthewdale marked this conversation as resolved.
Show resolved Hide resolved
// 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".
cmdName := "listCollections"
if mtest.CompareServerVersions(mtest.ServerVersion(), "3.0") < 0 {
cmdName = "find"
}
assertKillCursorsCommandsAreMonitored(mt, cmdName, 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 @@ -459,12 +459,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