summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLingzhi Deng <lingzhi.deng@mongodb.com>2020-01-14 19:14:39 +0000
committerA. Jesse Jiryu Davis <jesse@mongodb.com>2020-01-27 15:40:32 -0500
commit91a13c934b588c4de02c406a6546a27794c28842 (patch)
tree1d98104ed3d80d68303864622f572a7d72c38fa0
parentbe0e0cfab113a53517869c9fe4cd853b55260e77 (diff)
downloadmongo-91a13c934b588c4de02c406a6546a27794c28842.tar.gz
SERVER-45557: DBClientCursor should call metadataReader once per batch
-rw-r--r--src/mongo/client/dbclient_cursor.cpp6
-rw-r--r--src/mongo/client/dbclient_cursor_test.cpp45
2 files changed, 45 insertions, 6 deletions
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.