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).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
10 #include "cache/lru_cache.h"
11 #include "db/db_test_util.h"
12 #include "port/stack_trace.h"
13 #include "util/compression.h"
15 namespace ROCKSDB_NAMESPACE
{
17 class DBBlockCacheTest
: public DBTestBase
{
19 size_t miss_count_
= 0;
20 size_t hit_count_
= 0;
21 size_t insert_count_
= 0;
22 size_t failure_count_
= 0;
23 size_t compression_dict_miss_count_
= 0;
24 size_t compression_dict_hit_count_
= 0;
25 size_t compression_dict_insert_count_
= 0;
26 size_t compressed_miss_count_
= 0;
27 size_t compressed_hit_count_
= 0;
28 size_t compressed_insert_count_
= 0;
29 size_t compressed_failure_count_
= 0;
32 const size_t kNumBlocks
= 10;
33 const size_t kValueSize
= 100;
35 DBBlockCacheTest() : DBTestBase("/db_block_cache_test") {}
37 BlockBasedTableOptions
GetTableOptions() {
38 BlockBasedTableOptions table_options
;
39 // Set a small enough block size so that each key-value get its own block.
40 table_options
.block_size
= 1;
44 Options
GetOptions(const BlockBasedTableOptions
& table_options
) {
45 Options options
= CurrentOptions();
46 options
.create_if_missing
= true;
47 options
.avoid_flush_during_recovery
= false;
48 // options.compression = kNoCompression;
49 options
.statistics
= ROCKSDB_NAMESPACE::CreateDBStatistics();
50 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
54 void InitTable(const Options
& /*options*/) {
55 std::string
value(kValueSize
, 'a');
56 for (size_t i
= 0; i
< kNumBlocks
; i
++) {
57 ASSERT_OK(Put(ToString(i
), value
.c_str()));
61 void RecordCacheCounters(const Options
& options
) {
62 miss_count_
= TestGetTickerCount(options
, BLOCK_CACHE_MISS
);
63 hit_count_
= TestGetTickerCount(options
, BLOCK_CACHE_HIT
);
64 insert_count_
= TestGetTickerCount(options
, BLOCK_CACHE_ADD
);
65 failure_count_
= TestGetTickerCount(options
, BLOCK_CACHE_ADD_FAILURES
);
66 compressed_miss_count_
=
67 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_MISS
);
68 compressed_hit_count_
=
69 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_HIT
);
70 compressed_insert_count_
=
71 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_ADD
);
72 compressed_failure_count_
=
73 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_ADD_FAILURES
);
76 void RecordCacheCountersForCompressionDict(const Options
& options
) {
77 compression_dict_miss_count_
=
78 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSION_DICT_MISS
);
79 compression_dict_hit_count_
=
80 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSION_DICT_HIT
);
81 compression_dict_insert_count_
=
82 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSION_DICT_ADD
);
85 void CheckCacheCounters(const Options
& options
, size_t expected_misses
,
86 size_t expected_hits
, size_t expected_inserts
,
87 size_t expected_failures
) {
88 size_t new_miss_count
= TestGetTickerCount(options
, BLOCK_CACHE_MISS
);
89 size_t new_hit_count
= TestGetTickerCount(options
, BLOCK_CACHE_HIT
);
90 size_t new_insert_count
= TestGetTickerCount(options
, BLOCK_CACHE_ADD
);
91 size_t new_failure_count
=
92 TestGetTickerCount(options
, BLOCK_CACHE_ADD_FAILURES
);
93 ASSERT_EQ(miss_count_
+ expected_misses
, new_miss_count
);
94 ASSERT_EQ(hit_count_
+ expected_hits
, new_hit_count
);
95 ASSERT_EQ(insert_count_
+ expected_inserts
, new_insert_count
);
96 ASSERT_EQ(failure_count_
+ expected_failures
, new_failure_count
);
97 miss_count_
= new_miss_count
;
98 hit_count_
= new_hit_count
;
99 insert_count_
= new_insert_count
;
100 failure_count_
= new_failure_count
;
103 void CheckCacheCountersForCompressionDict(
104 const Options
& options
, size_t expected_compression_dict_misses
,
105 size_t expected_compression_dict_hits
,
106 size_t expected_compression_dict_inserts
) {
107 size_t new_compression_dict_miss_count
=
108 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSION_DICT_MISS
);
109 size_t new_compression_dict_hit_count
=
110 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSION_DICT_HIT
);
111 size_t new_compression_dict_insert_count
=
112 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSION_DICT_ADD
);
113 ASSERT_EQ(compression_dict_miss_count_
+ expected_compression_dict_misses
,
114 new_compression_dict_miss_count
);
115 ASSERT_EQ(compression_dict_hit_count_
+ expected_compression_dict_hits
,
116 new_compression_dict_hit_count
);
118 compression_dict_insert_count_
+ expected_compression_dict_inserts
,
119 new_compression_dict_insert_count
);
120 compression_dict_miss_count_
= new_compression_dict_miss_count
;
121 compression_dict_hit_count_
= new_compression_dict_hit_count
;
122 compression_dict_insert_count_
= new_compression_dict_insert_count
;
125 void CheckCompressedCacheCounters(const Options
& options
,
126 size_t expected_misses
,
127 size_t expected_hits
,
128 size_t expected_inserts
,
129 size_t expected_failures
) {
130 size_t new_miss_count
=
131 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_MISS
);
132 size_t new_hit_count
=
133 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_HIT
);
134 size_t new_insert_count
=
135 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_ADD
);
136 size_t new_failure_count
=
137 TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_ADD_FAILURES
);
138 ASSERT_EQ(compressed_miss_count_
+ expected_misses
, new_miss_count
);
139 ASSERT_EQ(compressed_hit_count_
+ expected_hits
, new_hit_count
);
140 ASSERT_EQ(compressed_insert_count_
+ expected_inserts
, new_insert_count
);
141 ASSERT_EQ(compressed_failure_count_
+ expected_failures
, new_failure_count
);
142 compressed_miss_count_
= new_miss_count
;
143 compressed_hit_count_
= new_hit_count
;
144 compressed_insert_count_
= new_insert_count
;
145 compressed_failure_count_
= new_failure_count
;
149 TEST_F(DBBlockCacheTest
, IteratorBlockCacheUsage
) {
150 ReadOptions read_options
;
151 read_options
.fill_cache
= false;
152 auto table_options
= GetTableOptions();
153 auto options
= GetOptions(table_options
);
156 std::shared_ptr
<Cache
> cache
= NewLRUCache(0, 0, false);
157 table_options
.block_cache
= cache
;
158 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
160 RecordCacheCounters(options
);
162 std::vector
<std::unique_ptr
<Iterator
>> iterators(kNumBlocks
- 1);
163 Iterator
* iter
= nullptr;
165 ASSERT_EQ(0, cache
->GetUsage());
166 iter
= db_
->NewIterator(read_options
);
167 iter
->Seek(ToString(0));
168 ASSERT_LT(0, cache
->GetUsage());
171 ASSERT_EQ(0, cache
->GetUsage());
174 TEST_F(DBBlockCacheTest
, TestWithoutCompressedBlockCache
) {
175 ReadOptions read_options
;
176 auto table_options
= GetTableOptions();
177 auto options
= GetOptions(table_options
);
180 std::shared_ptr
<Cache
> cache
= NewLRUCache(0, 0, false);
181 table_options
.block_cache
= cache
;
182 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
184 RecordCacheCounters(options
);
186 std::vector
<std::unique_ptr
<Iterator
>> iterators(kNumBlocks
- 1);
187 Iterator
* iter
= nullptr;
189 // Load blocks into cache.
190 for (size_t i
= 0; i
< kNumBlocks
- 1; i
++) {
191 iter
= db_
->NewIterator(read_options
);
192 iter
->Seek(ToString(i
));
193 ASSERT_OK(iter
->status());
194 CheckCacheCounters(options
, 1, 0, 1, 0);
195 iterators
[i
].reset(iter
);
197 size_t usage
= cache
->GetUsage();
199 cache
->SetCapacity(usage
);
200 ASSERT_EQ(usage
, cache
->GetPinnedUsage());
202 // Test with strict capacity limit.
203 cache
->SetStrictCapacityLimit(true);
204 iter
= db_
->NewIterator(read_options
);
205 iter
->Seek(ToString(kNumBlocks
- 1));
206 ASSERT_TRUE(iter
->status().IsIncomplete());
207 CheckCacheCounters(options
, 1, 0, 0, 1);
211 // Release iterators and access cache again.
212 for (size_t i
= 0; i
< kNumBlocks
- 1; i
++) {
213 iterators
[i
].reset();
214 CheckCacheCounters(options
, 0, 0, 0, 0);
216 ASSERT_EQ(0, cache
->GetPinnedUsage());
217 for (size_t i
= 0; i
< kNumBlocks
- 1; i
++) {
218 iter
= db_
->NewIterator(read_options
);
219 iter
->Seek(ToString(i
));
220 ASSERT_OK(iter
->status());
221 CheckCacheCounters(options
, 0, 1, 0, 0);
222 iterators
[i
].reset(iter
);
227 TEST_F(DBBlockCacheTest
, TestWithCompressedBlockCache
) {
228 ReadOptions read_options
;
229 auto table_options
= GetTableOptions();
230 auto options
= GetOptions(table_options
);
231 options
.compression
= CompressionType::kSnappyCompression
;
234 std::shared_ptr
<Cache
> cache
= NewLRUCache(0, 0, false);
235 std::shared_ptr
<Cache
> compressed_cache
= NewLRUCache(1 << 25, 0, false);
236 table_options
.block_cache
= cache
;
237 table_options
.block_cache_compressed
= compressed_cache
;
238 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
240 RecordCacheCounters(options
);
242 std::vector
<std::unique_ptr
<Iterator
>> iterators(kNumBlocks
- 1);
243 Iterator
* iter
= nullptr;
245 // Load blocks into cache.
246 for (size_t i
= 0; i
< kNumBlocks
- 1; i
++) {
247 iter
= db_
->NewIterator(read_options
);
248 iter
->Seek(ToString(i
));
249 ASSERT_OK(iter
->status());
250 CheckCacheCounters(options
, 1, 0, 1, 0);
251 CheckCompressedCacheCounters(options
, 1, 0, 1, 0);
252 iterators
[i
].reset(iter
);
254 size_t usage
= cache
->GetUsage();
256 ASSERT_EQ(usage
, cache
->GetPinnedUsage());
257 size_t compressed_usage
= compressed_cache
->GetUsage();
258 ASSERT_LT(0, compressed_usage
);
259 // Compressed block cache cannot be pinned.
260 ASSERT_EQ(0, compressed_cache
->GetPinnedUsage());
262 // Set strict capacity limit flag. Now block will only load into compressed
264 cache
->SetCapacity(usage
);
265 cache
->SetStrictCapacityLimit(true);
266 ASSERT_EQ(usage
, cache
->GetPinnedUsage());
267 iter
= db_
->NewIterator(read_options
);
268 iter
->Seek(ToString(kNumBlocks
- 1));
269 ASSERT_TRUE(iter
->status().IsIncomplete());
270 CheckCacheCounters(options
, 1, 0, 0, 1);
271 CheckCompressedCacheCounters(options
, 1, 0, 1, 0);
275 // Clear strict capacity limit flag. This time we shall hit compressed block
277 cache
->SetStrictCapacityLimit(false);
278 iter
= db_
->NewIterator(read_options
);
279 iter
->Seek(ToString(kNumBlocks
- 1));
280 ASSERT_OK(iter
->status());
281 CheckCacheCounters(options
, 1, 0, 1, 0);
282 CheckCompressedCacheCounters(options
, 0, 1, 0, 0);
290 // Make sure that when options.block_cache is set, after a new table is
291 // created its index/filter blocks are added to block cache.
292 TEST_F(DBBlockCacheTest
, IndexAndFilterBlocksOfNewTableAddedToCache
) {
293 Options options
= CurrentOptions();
294 options
.create_if_missing
= true;
295 options
.statistics
= ROCKSDB_NAMESPACE::CreateDBStatistics();
296 BlockBasedTableOptions table_options
;
297 table_options
.cache_index_and_filter_blocks
= true;
298 table_options
.filter_policy
.reset(NewBloomFilterPolicy(20));
299 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
300 CreateAndReopenWithCF({"pikachu"}, options
);
302 ASSERT_OK(Put(1, "key", "val"));
303 // Create a new table.
306 // index/filter blocks added to block cache right after table creation.
307 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_INDEX_MISS
));
308 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_FILTER_MISS
));
309 ASSERT_EQ(2, /* only index/filter were added */
310 TestGetTickerCount(options
, BLOCK_CACHE_ADD
));
311 ASSERT_EQ(0, TestGetTickerCount(options
, BLOCK_CACHE_DATA_MISS
));
314 dbfull()->GetIntProperty("rocksdb.estimate-table-readers-mem", &int_num
));
315 ASSERT_EQ(int_num
, 0U);
317 // Make sure filter block is in cache.
320 db_
->KeyMayExist(ReadOptions(), handles_
[1], "key", &value
);
322 // Miss count should remain the same.
323 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_FILTER_MISS
));
324 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_FILTER_HIT
));
326 db_
->KeyMayExist(ReadOptions(), handles_
[1], "key", &value
);
327 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_FILTER_MISS
));
328 ASSERT_EQ(2, TestGetTickerCount(options
, BLOCK_CACHE_FILTER_HIT
));
330 // Make sure index block is in cache.
331 auto index_block_hit
= TestGetTickerCount(options
, BLOCK_CACHE_INDEX_HIT
);
332 value
= Get(1, "key");
333 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_INDEX_MISS
));
334 ASSERT_EQ(index_block_hit
+ 1,
335 TestGetTickerCount(options
, BLOCK_CACHE_INDEX_HIT
));
337 value
= Get(1, "key");
338 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_INDEX_MISS
));
339 ASSERT_EQ(index_block_hit
+ 2,
340 TestGetTickerCount(options
, BLOCK_CACHE_INDEX_HIT
));
343 // With fill_cache = false, fills up the cache, then iterates over the entire
344 // db, verify dummy entries inserted in `BlockBasedTable::NewDataBlockIterator`
345 // does not cause heap-use-after-free errors in COMPILE_WITH_ASAN=1 runs
346 TEST_F(DBBlockCacheTest
, FillCacheAndIterateDB
) {
347 ReadOptions read_options
;
348 read_options
.fill_cache
= false;
349 auto table_options
= GetTableOptions();
350 auto options
= GetOptions(table_options
);
353 std::shared_ptr
<Cache
> cache
= NewLRUCache(10, 0, true);
354 table_options
.block_cache
= cache
;
355 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
357 ASSERT_OK(Put("key1", "val1"));
358 ASSERT_OK(Put("key2", "val2"));
360 ASSERT_OK(Put("key3", "val3"));
361 ASSERT_OK(Put("key4", "val4"));
363 ASSERT_OK(Put("key5", "val5"));
364 ASSERT_OK(Put("key6", "val6"));
367 Iterator
* iter
= nullptr;
369 iter
= db_
->NewIterator(read_options
);
370 iter
->Seek(ToString(0));
371 while (iter
->Valid()) {
378 TEST_F(DBBlockCacheTest
, IndexAndFilterBlocksStats
) {
379 Options options
= CurrentOptions();
380 options
.create_if_missing
= true;
381 options
.statistics
= ROCKSDB_NAMESPACE::CreateDBStatistics();
382 BlockBasedTableOptions table_options
;
383 table_options
.cache_index_and_filter_blocks
= true;
385 // 500 bytes are enough to hold the first two blocks
387 co
.num_shard_bits
= 0;
388 co
.strict_capacity_limit
= false;
389 co
.metadata_charge_policy
= kDontChargeCacheMetadata
;
390 std::shared_ptr
<Cache
> cache
= NewLRUCache(co
);
391 table_options
.block_cache
= cache
;
392 table_options
.filter_policy
.reset(NewBloomFilterPolicy(20, true));
393 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
394 CreateAndReopenWithCF({"pikachu"}, options
);
396 ASSERT_OK(Put(1, "longer_key", "val"));
397 // Create a new table
399 size_t index_bytes_insert
=
400 TestGetTickerCount(options
, BLOCK_CACHE_INDEX_BYTES_INSERT
);
401 size_t filter_bytes_insert
=
402 TestGetTickerCount(options
, BLOCK_CACHE_FILTER_BYTES_INSERT
);
403 ASSERT_GT(index_bytes_insert
, 0);
404 ASSERT_GT(filter_bytes_insert
, 0);
405 ASSERT_EQ(cache
->GetUsage(), index_bytes_insert
+ filter_bytes_insert
);
406 // set the cache capacity to the current usage
407 cache
->SetCapacity(index_bytes_insert
+ filter_bytes_insert
);
408 // The index and filter eviction statistics were broken by the refactoring
409 // that moved the readers out of the block cache. Disabling these until we can
410 // bring the stats back.
411 // ASSERT_EQ(TestGetTickerCount(options, BLOCK_CACHE_INDEX_BYTES_EVICT), 0);
412 // ASSERT_EQ(TestGetTickerCount(options, BLOCK_CACHE_FILTER_BYTES_EVICT), 0);
413 // Note that the second key needs to be no longer than the first one.
414 // Otherwise the second index block may not fit in cache.
415 ASSERT_OK(Put(1, "key", "val"));
416 // Create a new table
418 // cache evicted old index and block entries
419 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_INDEX_BYTES_INSERT
),
421 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_FILTER_BYTES_INSERT
),
422 filter_bytes_insert
);
423 // The index and filter eviction statistics were broken by the refactoring
424 // that moved the readers out of the block cache. Disabling these until we can
425 // bring the stats back.
426 // ASSERT_EQ(TestGetTickerCount(options, BLOCK_CACHE_INDEX_BYTES_EVICT),
427 // index_bytes_insert);
428 // ASSERT_EQ(TestGetTickerCount(options, BLOCK_CACHE_FILTER_BYTES_EVICT),
429 // filter_bytes_insert);
434 // A mock cache wraps LRUCache, and record how many entries have been
435 // inserted for each priority.
436 class MockCache
: public LRUCache
{
438 static uint32_t high_pri_insert_count
;
439 static uint32_t low_pri_insert_count
;
442 : LRUCache((size_t)1 << 25 /*capacity*/, 0 /*num_shard_bits*/,
443 false /*strict_capacity_limit*/, 0.0 /*high_pri_pool_ratio*/) {
446 Status
Insert(const Slice
& key
, void* value
, size_t charge
,
447 void (*deleter
)(const Slice
& key
, void* value
), Handle
** handle
,
448 Priority priority
) override
{
449 if (priority
== Priority::LOW
) {
450 low_pri_insert_count
++;
452 high_pri_insert_count
++;
454 return LRUCache::Insert(key
, value
, charge
, deleter
, handle
, priority
);
458 uint32_t MockCache::high_pri_insert_count
= 0;
459 uint32_t MockCache::low_pri_insert_count
= 0;
461 } // anonymous namespace
463 TEST_F(DBBlockCacheTest
, IndexAndFilterBlocksCachePriority
) {
464 for (auto priority
: {Cache::Priority::LOW
, Cache::Priority::HIGH
}) {
465 Options options
= CurrentOptions();
466 options
.create_if_missing
= true;
467 options
.statistics
= ROCKSDB_NAMESPACE::CreateDBStatistics();
468 BlockBasedTableOptions table_options
;
469 table_options
.cache_index_and_filter_blocks
= true;
470 table_options
.block_cache
.reset(new MockCache());
471 table_options
.filter_policy
.reset(NewBloomFilterPolicy(20));
472 table_options
.cache_index_and_filter_blocks_with_high_priority
=
473 priority
== Cache::Priority::HIGH
? true : false;
474 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
475 DestroyAndReopen(options
);
477 MockCache::high_pri_insert_count
= 0;
478 MockCache::low_pri_insert_count
= 0;
480 // Create a new table.
481 ASSERT_OK(Put("foo", "value"));
482 ASSERT_OK(Put("bar", "value"));
484 ASSERT_EQ(1, NumTableFilesAtLevel(0));
486 // index/filter blocks added to block cache right after table creation.
487 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_INDEX_MISS
));
488 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_FILTER_MISS
));
489 ASSERT_EQ(2, /* only index/filter were added */
490 TestGetTickerCount(options
, BLOCK_CACHE_ADD
));
491 ASSERT_EQ(0, TestGetTickerCount(options
, BLOCK_CACHE_DATA_MISS
));
492 if (priority
== Cache::Priority::LOW
) {
493 ASSERT_EQ(0u, MockCache::high_pri_insert_count
);
494 ASSERT_EQ(2u, MockCache::low_pri_insert_count
);
496 ASSERT_EQ(2u, MockCache::high_pri_insert_count
);
497 ASSERT_EQ(0u, MockCache::low_pri_insert_count
);
500 // Access data block.
501 ASSERT_EQ("value", Get("foo"));
503 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_INDEX_MISS
));
504 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_FILTER_MISS
));
505 ASSERT_EQ(3, /*adding data block*/
506 TestGetTickerCount(options
, BLOCK_CACHE_ADD
));
507 ASSERT_EQ(1, TestGetTickerCount(options
, BLOCK_CACHE_DATA_MISS
));
509 // Data block should be inserted with low priority.
510 if (priority
== Cache::Priority::LOW
) {
511 ASSERT_EQ(0u, MockCache::high_pri_insert_count
);
512 ASSERT_EQ(3u, MockCache::low_pri_insert_count
);
514 ASSERT_EQ(2u, MockCache::high_pri_insert_count
);
515 ASSERT_EQ(1u, MockCache::low_pri_insert_count
);
520 TEST_F(DBBlockCacheTest
, ParanoidFileChecks
) {
521 Options options
= CurrentOptions();
522 options
.create_if_missing
= true;
523 options
.statistics
= ROCKSDB_NAMESPACE::CreateDBStatistics();
524 options
.level0_file_num_compaction_trigger
= 2;
525 options
.paranoid_file_checks
= true;
526 BlockBasedTableOptions table_options
;
527 table_options
.cache_index_and_filter_blocks
= false;
528 table_options
.filter_policy
.reset(NewBloomFilterPolicy(20));
529 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
530 CreateAndReopenWithCF({"pikachu"}, options
);
532 ASSERT_OK(Put(1, "1_key", "val"));
533 ASSERT_OK(Put(1, "9_key", "val"));
534 // Create a new table.
536 ASSERT_EQ(1, /* read and cache data block */
537 TestGetTickerCount(options
, BLOCK_CACHE_ADD
));
539 ASSERT_OK(Put(1, "1_key2", "val2"));
540 ASSERT_OK(Put(1, "9_key2", "val2"));
541 // Create a new SST file. This will further trigger a compaction
542 // and generate another file.
544 dbfull()->TEST_WaitForCompact();
545 ASSERT_EQ(3, /* Totally 3 files created up to now */
546 TestGetTickerCount(options
, BLOCK_CACHE_ADD
));
548 // After disabling options.paranoid_file_checks. NO further block
549 // is added after generating a new file.
551 dbfull()->SetOptions(handles_
[1], {{"paranoid_file_checks", "false"}}));
553 ASSERT_OK(Put(1, "1_key3", "val3"));
554 ASSERT_OK(Put(1, "9_key3", "val3"));
556 ASSERT_OK(Put(1, "1_key4", "val4"));
557 ASSERT_OK(Put(1, "9_key4", "val4"));
559 dbfull()->TEST_WaitForCompact();
560 ASSERT_EQ(3, /* Totally 3 files created up to now */
561 TestGetTickerCount(options
, BLOCK_CACHE_ADD
));
564 TEST_F(DBBlockCacheTest
, CompressedCache
) {
565 if (!Snappy_Supported()) {
570 // Run this test three iterations.
571 // Iteration 1: only a uncompressed block cache
572 // Iteration 2: only a compressed block cache
573 // Iteration 3: both block cache and compressed cache
574 // Iteration 4: both block cache and compressed cache, but DB is not
576 for (int iter
= 0; iter
< 4; iter
++) {
577 Options options
= CurrentOptions();
578 options
.write_buffer_size
= 64 * 1024; // small write buffer
579 options
.statistics
= ROCKSDB_NAMESPACE::CreateDBStatistics();
581 BlockBasedTableOptions table_options
;
584 // only uncompressed block cache
585 table_options
.block_cache
= NewLRUCache(8 * 1024);
586 table_options
.block_cache_compressed
= nullptr;
587 options
.table_factory
.reset(NewBlockBasedTableFactory(table_options
));
590 // no block cache, only compressed cache
591 table_options
.no_block_cache
= true;
592 table_options
.block_cache
= nullptr;
593 table_options
.block_cache_compressed
= NewLRUCache(8 * 1024);
594 options
.table_factory
.reset(NewBlockBasedTableFactory(table_options
));
597 // both compressed and uncompressed block cache
598 table_options
.block_cache
= NewLRUCache(1024);
599 table_options
.block_cache_compressed
= NewLRUCache(8 * 1024);
600 options
.table_factory
.reset(NewBlockBasedTableFactory(table_options
));
603 // both block cache and compressed cache, but DB is not compressed
604 // also, make block cache sizes bigger, to trigger block cache hits
605 table_options
.block_cache
= NewLRUCache(1024 * 1024);
606 table_options
.block_cache_compressed
= NewLRUCache(8 * 1024 * 1024);
607 options
.table_factory
.reset(NewBlockBasedTableFactory(table_options
));
608 options
.compression
= kNoCompression
;
613 CreateAndReopenWithCF({"pikachu"}, options
);
614 // default column family doesn't have block cache
615 Options no_block_cache_opts
;
616 no_block_cache_opts
.statistics
= options
.statistics
;
617 no_block_cache_opts
= CurrentOptions(no_block_cache_opts
);
618 BlockBasedTableOptions table_options_no_bc
;
619 table_options_no_bc
.no_block_cache
= true;
620 no_block_cache_opts
.table_factory
.reset(
621 NewBlockBasedTableFactory(table_options_no_bc
));
622 ReopenWithColumnFamilies(
623 {"default", "pikachu"},
624 std::vector
<Options
>({no_block_cache_opts
, options
}));
628 // Write 8MB (80 values, each 100K)
629 ASSERT_EQ(NumTableFilesAtLevel(0, 1), 0);
630 std::vector
<std::string
> values
;
632 for (int i
= 0; i
< num_iter
; i
++) {
633 if (i
% 4 == 0) { // high compression ratio
634 str
= RandomString(&rnd
, 1000);
636 values
.push_back(str
);
637 ASSERT_OK(Put(1, Key(i
), values
[i
]));
640 // flush all data from memtable so that reads are from block cache
643 for (int i
= 0; i
< num_iter
; i
++) {
644 ASSERT_EQ(Get(1, Key(i
)), values
[i
]);
647 // check that we triggered the appropriate code paths in the cache
650 // only uncompressed block cache
651 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_MISS
), 0);
652 ASSERT_EQ(TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_MISS
), 0);
655 // no block cache, only compressed cache
656 ASSERT_EQ(TestGetTickerCount(options
, BLOCK_CACHE_MISS
), 0);
657 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_MISS
), 0);
660 // both compressed and uncompressed block cache
661 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_MISS
), 0);
662 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_MISS
), 0);
665 // both compressed and uncompressed block cache
666 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_MISS
), 0);
667 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_HIT
), 0);
668 ASSERT_GT(TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_MISS
), 0);
669 // compressed doesn't have any hits since blocks are not compressed on
671 ASSERT_EQ(TestGetTickerCount(options
, BLOCK_CACHE_COMPRESSED_HIT
), 0);
677 options
.create_if_missing
= true;
678 DestroyAndReopen(options
);
682 TEST_F(DBBlockCacheTest
, CacheCompressionDict
) {
683 const int kNumFiles
= 4;
684 const int kNumEntriesPerFile
= 128;
685 const int kNumBytesPerEntry
= 1024;
687 // Try all the available libraries that support dictionary compression
688 std::vector
<CompressionType
> compression_types
;
689 if (Zlib_Supported()) {
690 compression_types
.push_back(kZlibCompression
);
692 if (LZ4_Supported()) {
693 compression_types
.push_back(kLZ4Compression
);
694 compression_types
.push_back(kLZ4HCCompression
);
696 if (ZSTD_Supported()) {
697 compression_types
.push_back(kZSTD
);
698 } else if (ZSTDNotFinal_Supported()) {
699 compression_types
.push_back(kZSTDNotFinalCompression
);
702 for (auto compression_type
: compression_types
) {
703 Options options
= CurrentOptions();
704 options
.compression
= compression_type
;
705 options
.compression_opts
.max_dict_bytes
= 4096;
706 options
.create_if_missing
= true;
707 options
.num_levels
= 2;
708 options
.statistics
= ROCKSDB_NAMESPACE::CreateDBStatistics();
709 options
.target_file_size_base
= kNumEntriesPerFile
* kNumBytesPerEntry
;
710 BlockBasedTableOptions table_options
;
711 table_options
.cache_index_and_filter_blocks
= true;
712 table_options
.block_cache
.reset(new MockCache());
713 options
.table_factory
.reset(new BlockBasedTableFactory(table_options
));
714 DestroyAndReopen(options
);
716 RecordCacheCountersForCompressionDict(options
);
718 for (int i
= 0; i
< kNumFiles
; ++i
) {
719 ASSERT_EQ(i
, NumTableFilesAtLevel(0, 0));
720 for (int j
= 0; j
< kNumEntriesPerFile
; ++j
) {
721 std::string value
= RandomString(&rnd
, kNumBytesPerEntry
);
722 ASSERT_OK(Put(Key(j
* kNumFiles
+ i
), value
.c_str()));
726 dbfull()->TEST_WaitForCompact();
727 ASSERT_EQ(0, NumTableFilesAtLevel(0));
728 ASSERT_EQ(kNumFiles
, NumTableFilesAtLevel(1));
730 // Compression dictionary blocks are preloaded.
731 CheckCacheCountersForCompressionDict(
732 options
, kNumFiles
/* expected_compression_dict_misses */,
733 0 /* expected_compression_dict_hits */,
734 kNumFiles
/* expected_compression_dict_inserts */);
736 // Seek to a key in a file. It should cause the SST's dictionary meta-block
738 RecordCacheCounters(options
);
739 RecordCacheCountersForCompressionDict(options
);
740 ReadOptions read_options
;
741 ASSERT_NE("NOT_FOUND", Get(Key(kNumFiles
* kNumEntriesPerFile
- 1)));
742 // Two block hits: index and dictionary since they are prefetched
743 // One block missed/added: data block
744 CheckCacheCounters(options
, 1 /* expected_misses */, 2 /* expected_hits */,
745 1 /* expected_inserts */, 0 /* expected_failures */);
746 CheckCacheCountersForCompressionDict(
747 options
, 0 /* expected_compression_dict_misses */,
748 1 /* expected_compression_dict_hits */,
749 0 /* expected_compression_dict_inserts */);
753 #endif // ROCKSDB_LITE
755 } // namespace ROCKSDB_NAMESPACE
757 int main(int argc
, char** argv
) {
758 ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
759 ::testing::InitGoogleTest(&argc
, argv
);
760 return RUN_ALL_TESTS();