Skip to content

Commit fe97b16

Browse files
BenHuddlestondaverigby
authored andcommitted
MB-41857: Only adjust file cache limit if open db successful
We should only adjust the file cache limit if the open db was successful, currently we will decrement it for the file even if we cannot open it. Change-Id: I8f8366012f4441073260f2e8065e48badf7240a2 Reviewed-on: http://review.couchbase.org/c/kv_engine/+/137788 Tested-by: Build Bot <[email protected]> Reviewed-by: Dave Rigby <[email protected]>
1 parent 0ad4430 commit fe97b16

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

engines/ep/src/couch-kvstore/couch-kvstore.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,10 +2017,9 @@ CouchKVStore::OpenResult CouchKVStore::openDb(Vbid vbucketId,
20172017
return {COUCHSTORE_ERROR_OPEN_FILE, std::move(handle)};
20182018
}
20192019

2020-
fc->decrementCacheSize();
2021-
20222020
auto result = openSpecificDB(
20232021
vbucketId, fileRev, handle->getDbHolder(), options, ops);
2022+
20242023
if (result != COUCHSTORE_SUCCESS) {
20252024
logger.debug(
20262025
"CouchKVStore::openDbForRead: {} rev:{} - openSpecificDb "
@@ -2031,6 +2030,8 @@ CouchKVStore::OpenResult CouchKVStore::openDb(Vbid vbucketId,
20312030
return {result, std::move(handle)};
20322031
}
20332032

2033+
fc->decrementCacheSize();
2034+
20342035
return {COUCHSTORE_SUCCESS, std::move(handle)};
20352036
}
20362037

engines/ep/tests/module_tests/couch-kvstore_test.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,39 @@ TEST_F(CouchKVStoreErrorInjectionTest,
622622
}
623623
}
624624

625+
// Test that if we fail to open the db we don't segfault by accessing a bad
626+
// ptr when we log the fileRev
627+
TEST_F(CouchKVStoreErrorInjectionTest,
628+
FileCacheLimitIncreasedIfOpenForReadFails) {
629+
vbid = Vbid(10);
630+
631+
// Make sure the vBucket does not exist before this test
632+
ASSERT_FALSE(kvstore->getVBucketState(vbid));
633+
ASSERT_THROW(kvstore->readVBState(vbid), std::logic_error);
634+
ASSERT_EQ(0, kvstore->getKVStoreStat().numVbSetFailure);
635+
636+
auto cap = CouchKVStoreFileCache::get().getHandle()->capacity();
637+
{
638+
EXPECT_CALL(logger, mlog(_, _)).Times(AnyNumber());
639+
EXPECT_CALL(logger, mlog(_, _)).Times(AnyNumber());
640+
EXPECT_CALL(logger,
641+
mlog(Ge(spdlog::level::level_enum::warn),
642+
VCE(COUCHSTORE_ERROR_OPEN_FILE)))
643+
.Times(1)
644+
.RetiresOnSaturation();
645+
646+
/* Establish FileOps expectation */
647+
EXPECT_CALL(ops, open(_, _, _, _))
648+
.WillOnce(Return(COUCHSTORE_ERROR_OPEN_FILE))
649+
.RetiresOnSaturation();
650+
651+
vbucket_state state;
652+
kvstore->get(makeDiskDocKey("key"), vbid);
653+
}
654+
655+
EXPECT_EQ(cap, CouchKVStoreFileCache::get().getHandle()->capacity());
656+
}
657+
625658
/**
626659
* Injects error during CouchKVStore::openDB_retry/couchstore_open_db_ex
627660
*/

0 commit comments

Comments
 (0)