From e48ee34609cd5450edf1a3de8a8f75ae55db3f34 Mon Sep 17 00:00:00 2001 From: Lingzhi Deng Date: Tue, 14 Jan 2020 19:14:39 +0000 Subject: SERVER-45557: DBClientCursor should call metadataReader once per batch --- src/mongo/client/dbclient_cursor.cpp | 6 ----- src/mongo/client/dbclient_cursor_test.cpp | 45 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) (limited to 'src/mongo/client') diff --git a/src/mongo/client/dbclient_cursor.cpp b/src/mongo/client/dbclient_cursor.cpp index fe677d9f00c..244e893f88b 100644 --- a/src/mongo/client/dbclient_cursor.cpp +++ b/src/mongo/client/dbclient_cursor.cpp @@ -310,12 +310,6 @@ BSONObj DBClientCursor::commandDataReceived(const Message& reply) { wasError = true; } - auto opCtx = haveClient() ? cc().getOperationContext() : nullptr; - if (_client->getReplyMetadataReader()) { - uassertStatusOK(_client->getReplyMetadataReader()( - opCtx, commandReply->getCommandReply(), _client->getServerAddress())); - } - return commandReply->getCommandReply().getOwned(); } diff --git a/src/mongo/client/dbclient_cursor_test.cpp b/src/mongo/client/dbclient_cursor_test.cpp index 9df9dd01405..d4cf194caba 100644 --- a/src/mongo/client/dbclient_cursor_test.cpp +++ b/src/mongo/client/dbclient_cursor_test.cpp @@ -146,6 +146,51 @@ protected: } }; +TEST_F(DBClientCursorTest, DBClientCursorCallsMetaDataReaderOncePerBatch) { + // Set up the DBClientCursor and a mock client connection. + DBClientConnectionForTest conn; + const NamespaceString nss("test", "coll"); + DBClientCursor cursor(&conn, NamespaceStringOrUUID(nss), Query().obj, 0, 0, nullptr, 0, 0); + cursor.setBatchSize(2); + + // Set up mock 'find' response. + const long long cursorId = 42; + Message findResponseMsg = mockFindResponse(nss, cursorId, {docObj(1), docObj(2)}); + conn.setCallResponse(findResponseMsg); + + int numMetaRead = 0; + conn.setReplyMetadataReader( + [&](OperationContext* opCtx, const BSONObj& metadataObj, StringData target) { + numMetaRead++; + return Status::OK(); + }); + + // Trigger a find command. + ASSERT(cursor.init()); + + // First batch from the initial find command. + ASSERT_BSONOBJ_EQ(docObj(1), cursor.next()); + ASSERT_BSONOBJ_EQ(docObj(2), cursor.next()); + ASSERT_FALSE(cursor.moreInCurrentBatch()); + // Test that the metadata reader callback is called once. + ASSERT_EQ(1, numMetaRead); + + // Set a terminal getMore response with cursorId 0. + auto getMoreResponseMsg = mockGetMoreResponse(nss, 0, {docObj(3), docObj(4)}); + conn.setCallResponse(getMoreResponseMsg); + + // Trigger a subsequent getMore command. + ASSERT_TRUE(cursor.more()); + + // Second batch from the getMore command. + ASSERT_BSONOBJ_EQ(docObj(3), cursor.next()); + ASSERT_BSONOBJ_EQ(docObj(4), cursor.next()); + ASSERT_FALSE(cursor.moreInCurrentBatch()); + ASSERT_TRUE(cursor.isDead()); + // Test that the metadata reader callback is called twice. + ASSERT_EQ(2, numMetaRead); +} + TEST_F(DBClientCursorTest, DBClientCursorHandlesOpMsgExhaustCorrectly) { // Set up the DBClientCursor and a mock client connection. -- cgit v1.2.1