mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-26 07:30:54 +00:00
2d9efc9ab2
Summary: In Linux, when reopening DB with many SST files, profiling shows that 100% system cpu time spent for a couple of seconds for `GetLogicalBufferSize`. This slows down MyRocks' recovery time when site is down. This PR introduces two new APIs: 1. `Env::RegisterDbPaths` and `Env::UnregisterDbPaths` lets `DB` tell the env when it starts or stops using its database directories . The `PosixFileSystem` takes this opportunity to set up a cache from database directories to the corresponding logical block sizes. 2. `LogicalBlockSizeCache` is defined only for OS_LINUX to cache the logical block sizes. Other modifications: 1. rename `logical buffer size` to `logical block size` to be consistent with Linux terms. 2. declare `GetLogicalBlockSize` in `PosixHelper` to expose it to `PosixFileSystem`. 3. change the functions `IOError` and `IOStatus` in `env/io_posix.h` to have external linkage since they are used in other translation units too. Pull Request resolved: https://github.com/facebook/rocksdb/pull/6457 Test Plan: 1. A new unit test is added for `LogicalBlockSizeCache` in `env/io_posix_test.cc`. 2. A new integration test is added for `DB` operations related to the cache in `db/db_logical_block_size_cache_test.cc`. `make check` Differential Revision: D20131243 Pulled By: cheng-chang fbshipit-source-id: 3077c50f8065c0bffb544d8f49fb10bba9408d04
141 lines
4.1 KiB
C++
141 lines
4.1 KiB
C++
// Copyright (c) 2020-present, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#ifdef ROCKSDB_LIB_IO_POSIX
|
|
#include "env/io_posix.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
#ifdef OS_LINUX
|
|
class LogicalBlockSizeCacheTest : public testing::Test {};
|
|
|
|
// Tests the caching behavior.
|
|
TEST_F(LogicalBlockSizeCacheTest, Cache) {
|
|
int ncall = 0;
|
|
auto get_fd_block_size = [&](int fd) {
|
|
ncall++;
|
|
return fd;
|
|
};
|
|
std::map<std::string, int> dir_fds{
|
|
{"/", 0},
|
|
{"/db", 1},
|
|
{"/db1", 2},
|
|
{"/db2", 3},
|
|
};
|
|
auto get_dir_block_size = [&](const std::string& dir, size_t* size) {
|
|
ncall++;
|
|
*size = dir_fds[dir];
|
|
return Status::OK();
|
|
};
|
|
LogicalBlockSizeCache cache(get_fd_block_size, get_dir_block_size);
|
|
ASSERT_EQ(0, ncall);
|
|
ASSERT_EQ(0, cache.Size());
|
|
|
|
ASSERT_EQ(6, cache.GetLogicalBlockSize("/sst", 6));
|
|
ASSERT_EQ(1, ncall);
|
|
ASSERT_EQ(7, cache.GetLogicalBlockSize("/db/sst1", 7));
|
|
ASSERT_EQ(2, ncall);
|
|
ASSERT_EQ(8, cache.GetLogicalBlockSize("/db/sst2", 8));
|
|
ASSERT_EQ(3, ncall);
|
|
|
|
ASSERT_OK(cache.RefAndCacheLogicalBlockSize({"/", "/db1/", "/db2"}));
|
|
ASSERT_EQ(3, cache.Size());
|
|
ASSERT_TRUE(cache.Contains("/"));
|
|
ASSERT_TRUE(cache.Contains("/db1"));
|
|
ASSERT_TRUE(cache.Contains("/db2"));
|
|
ASSERT_EQ(6, ncall);
|
|
// Block size for / is cached.
|
|
ASSERT_EQ(0, cache.GetLogicalBlockSize("/sst", 6));
|
|
ASSERT_EQ(6, ncall);
|
|
// No cached size for /db.
|
|
ASSERT_EQ(7, cache.GetLogicalBlockSize("/db/sst1", 7));
|
|
ASSERT_EQ(7, ncall);
|
|
ASSERT_EQ(8, cache.GetLogicalBlockSize("/db/sst2", 8));
|
|
ASSERT_EQ(8, ncall);
|
|
// Block size for /db1 is cached.
|
|
ASSERT_EQ(2, cache.GetLogicalBlockSize("/db1/sst1", 4));
|
|
ASSERT_EQ(8, ncall);
|
|
ASSERT_EQ(2, cache.GetLogicalBlockSize("/db1/sst2", 5));
|
|
ASSERT_EQ(8, ncall);
|
|
// Block size for /db2 is cached.
|
|
ASSERT_EQ(3, cache.GetLogicalBlockSize("/db2/sst1", 6));
|
|
ASSERT_EQ(8, ncall);
|
|
ASSERT_EQ(3, cache.GetLogicalBlockSize("/db2/sst2", 7));
|
|
ASSERT_EQ(8, ncall);
|
|
|
|
cache.RefAndCacheLogicalBlockSize({"/db"});
|
|
ASSERT_EQ(4, cache.Size());
|
|
ASSERT_TRUE(cache.Contains("/"));
|
|
ASSERT_TRUE(cache.Contains("/db1"));
|
|
ASSERT_TRUE(cache.Contains("/db2"));
|
|
ASSERT_TRUE(cache.Contains("/db"));
|
|
|
|
ASSERT_EQ(9, ncall);
|
|
// Block size for /db is cached.
|
|
ASSERT_EQ(1, cache.GetLogicalBlockSize("/db/sst1", 7));
|
|
ASSERT_EQ(9, ncall);
|
|
ASSERT_EQ(1, cache.GetLogicalBlockSize("/db/sst2", 8));
|
|
ASSERT_EQ(9, ncall);
|
|
}
|
|
|
|
// Tests the reference counting behavior.
|
|
TEST_F(LogicalBlockSizeCacheTest, Ref) {
|
|
int ncall = 0;
|
|
auto get_fd_block_size = [&](int fd) {
|
|
ncall++;
|
|
return fd;
|
|
};
|
|
std::map<std::string, int> dir_fds{
|
|
{"/db", 0},
|
|
};
|
|
auto get_dir_block_size = [&](const std::string& dir, size_t* size) {
|
|
ncall++;
|
|
*size = dir_fds[dir];
|
|
return Status::OK();
|
|
};
|
|
LogicalBlockSizeCache cache(get_fd_block_size, get_dir_block_size);
|
|
|
|
ASSERT_EQ(0, ncall);
|
|
|
|
ASSERT_EQ(1, cache.GetLogicalBlockSize("/db/sst0", 1));
|
|
ASSERT_EQ(1, ncall);
|
|
|
|
cache.RefAndCacheLogicalBlockSize({"/db"});
|
|
ASSERT_EQ(2, ncall);
|
|
ASSERT_EQ(1, cache.GetRefCount("/db"));
|
|
// Block size for /db is cached. Ref count = 1.
|
|
ASSERT_EQ(0, cache.GetLogicalBlockSize("/db/sst1", 1));
|
|
ASSERT_EQ(2, ncall);
|
|
|
|
// Ref count = 2, but won't recompute the cached buffer size.
|
|
cache.RefAndCacheLogicalBlockSize({"/db"});
|
|
ASSERT_EQ(2, cache.GetRefCount("/db"));
|
|
ASSERT_EQ(2, ncall);
|
|
|
|
// Ref count = 1.
|
|
cache.UnrefAndTryRemoveCachedLogicalBlockSize({"/db"});
|
|
ASSERT_EQ(1, cache.GetRefCount("/db"));
|
|
// Block size for /db is still cached.
|
|
ASSERT_EQ(0, cache.GetLogicalBlockSize("/db/sst2", 1));
|
|
ASSERT_EQ(2, ncall);
|
|
|
|
// Ref count = 0 and cached buffer size for /db is removed.
|
|
cache.UnrefAndTryRemoveCachedLogicalBlockSize({"/db"});
|
|
ASSERT_EQ(0, cache.Size());
|
|
ASSERT_EQ(1, cache.GetLogicalBlockSize("/db/sst0", 1));
|
|
ASSERT_EQ(3, ncall);
|
|
}
|
|
#endif
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
|
#endif
|
|
|
|
int main(int argc, char** argv) {
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|