blob_file_cache_test.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "db/blob/blob_file_cache.h"
  6. #include <cassert>
  7. #include <string>
  8. #include "db/blob/blob_log_format.h"
  9. #include "db/blob/blob_log_writer.h"
  10. #include "env/mock_env.h"
  11. #include "file/filename.h"
  12. #include "file/read_write_util.h"
  13. #include "file/writable_file_writer.h"
  14. #include "options/cf_options.h"
  15. #include "rocksdb/cache.h"
  16. #include "rocksdb/env.h"
  17. #include "rocksdb/file_system.h"
  18. #include "rocksdb/options.h"
  19. #include "rocksdb/statistics.h"
  20. #include "test_util/sync_point.h"
  21. #include "test_util/testharness.h"
  22. namespace ROCKSDB_NAMESPACE {
  23. namespace {
  24. // Creates a test blob file with a single blob in it.
  25. void WriteBlobFile(uint32_t column_family_id,
  26. const ImmutableOptions& immutable_options,
  27. uint64_t blob_file_number) {
  28. assert(!immutable_options.cf_paths.empty());
  29. const std::string blob_file_path =
  30. BlobFileName(immutable_options.cf_paths.front().path, blob_file_number);
  31. std::unique_ptr<FSWritableFile> file;
  32. ASSERT_OK(NewWritableFile(immutable_options.fs.get(), blob_file_path, &file,
  33. FileOptions()));
  34. std::unique_ptr<WritableFileWriter> file_writer(new WritableFileWriter(
  35. std::move(file), blob_file_path, FileOptions(), immutable_options.clock));
  36. constexpr Statistics* statistics = nullptr;
  37. constexpr bool use_fsync = false;
  38. constexpr bool do_flush = false;
  39. BlobLogWriter blob_log_writer(std::move(file_writer), immutable_options.clock,
  40. statistics, blob_file_number, use_fsync,
  41. do_flush);
  42. constexpr bool has_ttl = false;
  43. constexpr ExpirationRange expiration_range;
  44. BlobLogHeader header(column_family_id, kNoCompression, has_ttl,
  45. expiration_range);
  46. ASSERT_OK(blob_log_writer.WriteHeader(WriteOptions(), header));
  47. constexpr char key[] = "key";
  48. constexpr char blob[] = "blob";
  49. std::string compressed_blob;
  50. uint64_t key_offset = 0;
  51. uint64_t blob_offset = 0;
  52. ASSERT_OK(blob_log_writer.AddRecord(WriteOptions(), key, blob, &key_offset,
  53. &blob_offset));
  54. BlobLogFooter footer;
  55. footer.blob_count = 1;
  56. footer.expiration_range = expiration_range;
  57. std::string checksum_method;
  58. std::string checksum_value;
  59. ASSERT_OK(blob_log_writer.AppendFooter(WriteOptions(), footer,
  60. &checksum_method, &checksum_value));
  61. }
  62. } // anonymous namespace
  63. class BlobFileCacheTest : public testing::Test {
  64. protected:
  65. BlobFileCacheTest() { mock_env_.reset(MockEnv::Create(Env::Default())); }
  66. std::unique_ptr<Env> mock_env_;
  67. };
  68. TEST_F(BlobFileCacheTest, GetBlobFileReader) {
  69. Options options;
  70. options.env = mock_env_.get();
  71. options.statistics = CreateDBStatistics();
  72. options.cf_paths.emplace_back(
  73. test::PerThreadDBPath(mock_env_.get(),
  74. "BlobFileCacheTest_GetBlobFileReader"),
  75. 0);
  76. options.enable_blob_files = true;
  77. constexpr uint32_t column_family_id = 1;
  78. ImmutableOptions immutable_options(options);
  79. constexpr uint64_t blob_file_number = 123;
  80. WriteBlobFile(column_family_id, immutable_options, blob_file_number);
  81. constexpr size_t capacity = 10;
  82. std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity);
  83. FileOptions file_options;
  84. constexpr HistogramImpl* blob_file_read_hist = nullptr;
  85. BlobFileCache blob_file_cache(backing_cache.get(), &immutable_options,
  86. &file_options, column_family_id,
  87. blob_file_read_hist, nullptr /*IOTracer*/);
  88. // First try: reader should be opened and put in cache
  89. CacheHandleGuard<BlobFileReader> first;
  90. const ReadOptions read_options;
  91. ASSERT_OK(blob_file_cache.GetBlobFileReader(read_options, blob_file_number,
  92. &first));
  93. ASSERT_NE(first.GetValue(), nullptr);
  94. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1);
  95. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 0);
  96. // Second try: reader should be served from cache
  97. CacheHandleGuard<BlobFileReader> second;
  98. ASSERT_OK(blob_file_cache.GetBlobFileReader(read_options, blob_file_number,
  99. &second));
  100. ASSERT_NE(second.GetValue(), nullptr);
  101. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1);
  102. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 0);
  103. ASSERT_EQ(first.GetValue(), second.GetValue());
  104. }
  105. TEST_F(BlobFileCacheTest, GetBlobFileReader_Race) {
  106. Options options;
  107. options.env = mock_env_.get();
  108. options.statistics = CreateDBStatistics();
  109. options.cf_paths.emplace_back(
  110. test::PerThreadDBPath(mock_env_.get(),
  111. "BlobFileCacheTest_GetBlobFileReader_Race"),
  112. 0);
  113. options.enable_blob_files = true;
  114. constexpr uint32_t column_family_id = 1;
  115. ImmutableOptions immutable_options(options);
  116. constexpr uint64_t blob_file_number = 123;
  117. WriteBlobFile(column_family_id, immutable_options, blob_file_number);
  118. constexpr size_t capacity = 10;
  119. std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity);
  120. FileOptions file_options;
  121. constexpr HistogramImpl* blob_file_read_hist = nullptr;
  122. BlobFileCache blob_file_cache(backing_cache.get(), &immutable_options,
  123. &file_options, column_family_id,
  124. blob_file_read_hist, nullptr /*IOTracer*/);
  125. CacheHandleGuard<BlobFileReader> first;
  126. CacheHandleGuard<BlobFileReader> second;
  127. const ReadOptions read_options;
  128. SyncPoint::GetInstance()->SetCallBack(
  129. "BlobFileCache::GetBlobFileReader:DoubleCheck", [&](void* /* arg */) {
  130. // Disabling sync points to prevent infinite recursion
  131. SyncPoint::GetInstance()->DisableProcessing();
  132. ASSERT_OK(blob_file_cache.GetBlobFileReader(read_options,
  133. blob_file_number, &second));
  134. ASSERT_NE(second.GetValue(), nullptr);
  135. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1);
  136. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 0);
  137. });
  138. SyncPoint::GetInstance()->EnableProcessing();
  139. ASSERT_OK(blob_file_cache.GetBlobFileReader(read_options, blob_file_number,
  140. &first));
  141. ASSERT_NE(first.GetValue(), nullptr);
  142. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1);
  143. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 0);
  144. ASSERT_EQ(first.GetValue(), second.GetValue());
  145. SyncPoint::GetInstance()->DisableProcessing();
  146. SyncPoint::GetInstance()->ClearAllCallBacks();
  147. }
  148. TEST_F(BlobFileCacheTest, GetBlobFileReader_IOError) {
  149. Options options;
  150. options.env = mock_env_.get();
  151. options.statistics = CreateDBStatistics();
  152. options.cf_paths.emplace_back(
  153. test::PerThreadDBPath(mock_env_.get(),
  154. "BlobFileCacheTest_GetBlobFileReader_IOError"),
  155. 0);
  156. options.enable_blob_files = true;
  157. constexpr size_t capacity = 10;
  158. std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity);
  159. ImmutableOptions immutable_options(options);
  160. FileOptions file_options;
  161. constexpr uint32_t column_family_id = 1;
  162. constexpr HistogramImpl* blob_file_read_hist = nullptr;
  163. BlobFileCache blob_file_cache(backing_cache.get(), &immutable_options,
  164. &file_options, column_family_id,
  165. blob_file_read_hist, nullptr /*IOTracer*/);
  166. // Note: there is no blob file with the below number
  167. constexpr uint64_t blob_file_number = 123;
  168. CacheHandleGuard<BlobFileReader> reader;
  169. const ReadOptions read_options;
  170. ASSERT_TRUE(
  171. blob_file_cache.GetBlobFileReader(read_options, blob_file_number, &reader)
  172. .IsIOError());
  173. ASSERT_EQ(reader.GetValue(), nullptr);
  174. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1);
  175. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 1);
  176. }
  177. TEST_F(BlobFileCacheTest, GetBlobFileReader_CacheFull) {
  178. Options options;
  179. options.env = mock_env_.get();
  180. options.statistics = CreateDBStatistics();
  181. options.cf_paths.emplace_back(
  182. test::PerThreadDBPath(mock_env_.get(),
  183. "BlobFileCacheTest_GetBlobFileReader_CacheFull"),
  184. 0);
  185. options.enable_blob_files = true;
  186. constexpr uint32_t column_family_id = 1;
  187. ImmutableOptions immutable_options(options);
  188. constexpr uint64_t blob_file_number = 123;
  189. WriteBlobFile(column_family_id, immutable_options, blob_file_number);
  190. constexpr size_t capacity = 0;
  191. constexpr int num_shard_bits = -1; // determined automatically
  192. constexpr bool strict_capacity_limit = true;
  193. std::shared_ptr<Cache> backing_cache =
  194. NewLRUCache(capacity, num_shard_bits, strict_capacity_limit);
  195. FileOptions file_options;
  196. constexpr HistogramImpl* blob_file_read_hist = nullptr;
  197. BlobFileCache blob_file_cache(backing_cache.get(), &immutable_options,
  198. &file_options, column_family_id,
  199. blob_file_read_hist, nullptr /*IOTracer*/);
  200. // Insert into cache should fail since it has zero capacity and
  201. // strict_capacity_limit is set
  202. CacheHandleGuard<BlobFileReader> reader;
  203. const ReadOptions read_options;
  204. ASSERT_TRUE(
  205. blob_file_cache.GetBlobFileReader(read_options, blob_file_number, &reader)
  206. .IsMemoryLimit());
  207. ASSERT_EQ(reader.GetValue(), nullptr);
  208. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_OPENS), 1);
  209. ASSERT_EQ(options.statistics->getTickerCount(NO_FILE_ERRORS), 1);
  210. }
  211. } // namespace ROCKSDB_NAMESPACE
  212. int main(int argc, char** argv) {
  213. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  214. ::testing::InitGoogleTest(&argc, argv);
  215. return RUN_ALL_TESTS();
  216. }