Skip to content

Commit a4d58e6

Browse files
matthewdaletsedgwick
authored andcommitted
GODRIVER-1937 Update legacy ListCollections to support the BatchSize option for server version 2.6 (mongodb#656)
1 parent e9e928c commit a4d58e6

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

mongo/integration/database_test.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,15 @@ func TestDatabase(t *testing.T) {
221221
})
222222
}
223223
})
224-
mt.RunOpts("batch size", mtest.NewOptions().MinServerVersion("3.0"), func(mt *mtest.T) {
224+
225+
// For server versions below 3.0, we internally execute ListCollections() as a legacy
226+
// OP_QUERY against the system.namespaces collection. Command monitoring upconversions
227+
// translate this to a "find" command rather than "listCollections".
228+
cmdMonitoringCmdName := "listCollections"
229+
if mtest.CompareServerVersions(mtest.ServerVersion(), "3.0") < 0 {
230+
cmdMonitoringCmdName = "find"
231+
}
232+
mt.Run("batch size", func(mt *mtest.T) {
225233
// Create two new collections so there will be three total.
226234
createCollections(mt, 2)
227235

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

233241
evt := mt.GetStartedEvent()
234-
assert.Equal(mt, "listCollections", evt.CommandName, "expected 'listCollections' command to be sent, got %q",
242+
assert.Equal(
243+
mt,
244+
cmdMonitoringCmdName,
245+
evt.CommandName,
246+
"expected %q command to be sent, got %q",
247+
cmdMonitoringCmdName,
235248
evt.CommandName)
236249
_, err = evt.Command.LookupErr("cursor", "batchSize")
237250
assert.Nil(mt, err, "expected command %s to contain key 'batchSize'", evt.Command)
238251
})
239-
240-
// The BatchSize option is not honored for ListCollections operations on server version 2.6 due to an
241-
// inconsistency in the legacy OP_QUERY code path (GODRIVER-1937).
242-
cmdMonitoringMtOpts := mtest.NewOptions().MinServerVersion("3.0")
243-
mt.RunOpts("getMore commands are monitored", cmdMonitoringMtOpts, func(mt *mtest.T) {
252+
mt.Run("getMore commands are monitored", func(mt *mtest.T) {
244253
createCollections(mt, 2)
245-
assertGetMoreCommandsAreMonitored(mt, "listCollections", func() (*mongo.Cursor, error) {
254+
assertGetMoreCommandsAreMonitored(mt, cmdMonitoringCmdName, func() (*mongo.Cursor, error) {
246255
return mt.DB.ListCollections(mtest.Background, bson.D{}, options.ListCollections().SetBatchSize(2))
247256
})
248257
})
249-
mt.RunOpts("killCursors commands are monitored", cmdMonitoringMtOpts, func(mt *mtest.T) {
258+
mt.Run("killCursors commands are monitored", func(mt *mtest.T) {
250259
createCollections(mt, 2)
251-
assertKillCursorsCommandsAreMonitored(mt, "listCollections", func() (*mongo.Cursor, error) {
260+
assertKillCursorsCommandsAreMonitored(mt, cmdMonitoringCmdName, func() (*mongo.Cursor, error) {
252261
return mt.DB.ListCollections(mtest.Background, bson.D{}, options.ListCollections().SetBatchSize(2))
253262
})
254263
})

x/mongo/driver/operation_legacy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,17 @@ func (op Operation) createLegacyListCollectionsWiremessage(dst []byte, desc desc
438438
optsElems = bsoncore.AppendDocumentElement(optsElems, "$readPreference", rp)
439439
}
440440

441+
var batchSize int32
442+
if val, ok := cmdDoc.Lookup("cursor", "batchSize").AsInt32OK(); ok {
443+
batchSize = val
444+
}
445+
441446
var wmIdx int32
442447
wmIdx, dst = wiremessage.AppendHeaderStart(dst, info.requestID, 0, wiremessage.OpQuery)
443448
dst = wiremessage.AppendQueryFlags(dst, op.slaveOK(desc))
444449
dst = wiremessage.AppendQueryFullCollectionName(dst, op.getFullCollectionName(listCollectionsNamespace))
445450
dst = wiremessage.AppendQueryNumberToSkip(dst, 0)
446-
dst = wiremessage.AppendQueryNumberToReturn(dst, 0)
451+
dst = wiremessage.AppendQueryNumberToReturn(dst, batchSize)
447452
dst = op.appendLegacyQueryDocument(dst, filter, optsElems)
448453
// leave out returnFieldsSelector because it is optional
449454

0 commit comments

Comments
 (0)