2016-04-22 00:59:17 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// 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).
|
2016-04-22 00:59:17 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "db/db_test_util.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "memory/arena.h"
|
|
|
|
#include "port/port.h"
|
2016-04-22 00:59:17 +02:00
|
|
|
#include "rocksdb/cache.h"
|
2019-05-30 23:47:29 +02:00
|
|
|
#include "table/block_based/block_builder.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/testharness.h"
|
2016-04-22 00:59:17 +02:00
|
|
|
#include "utilities/persistent_cache/volatile_tier_impl.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Unit tests for testing PersistentCacheTier
|
|
|
|
//
|
|
|
|
class PersistentCacheTierTest : public testing::Test {
|
|
|
|
public:
|
2016-05-03 20:05:42 +02:00
|
|
|
PersistentCacheTierTest();
|
2016-04-22 00:59:17 +02:00
|
|
|
virtual ~PersistentCacheTierTest() {
|
|
|
|
if (cache_) {
|
|
|
|
Status s = cache_->Close();
|
|
|
|
assert(s.ok());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Flush cache
|
|
|
|
void Flush() {
|
|
|
|
if (cache_) {
|
2016-05-03 20:05:42 +02:00
|
|
|
cache_->TEST_Flush();
|
2016-04-22 00:59:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create threaded workload
|
|
|
|
template <class T>
|
2017-02-06 23:43:55 +01:00
|
|
|
std::list<port::Thread> SpawnThreads(const size_t n, const T& fn) {
|
|
|
|
std::list<port::Thread> threads;
|
2016-04-22 00:59:17 +02:00
|
|
|
for (size_t i = 0; i < n; i++) {
|
2017-02-06 23:43:55 +01:00
|
|
|
port::Thread th(fn);
|
2016-04-22 00:59:17 +02:00
|
|
|
threads.push_back(std::move(th));
|
|
|
|
}
|
2016-06-08 01:55:24 +02:00
|
|
|
return threads;
|
2016-04-22 00:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for threads to join
|
2017-02-06 23:43:55 +01:00
|
|
|
void Join(std::list<port::Thread>&& threads) {
|
2016-04-22 00:59:17 +02:00
|
|
|
for (auto& th : threads) {
|
|
|
|
th.join();
|
|
|
|
}
|
|
|
|
threads.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run insert workload in threads
|
|
|
|
void Insert(const size_t nthreads, const size_t max_keys) {
|
|
|
|
key_ = 0;
|
|
|
|
max_keys_ = max_keys;
|
|
|
|
// spawn threads
|
|
|
|
auto fn = std::bind(&PersistentCacheTierTest::InsertImpl, this);
|
2016-06-08 01:55:24 +02:00
|
|
|
auto threads = SpawnThreads(nthreads, fn);
|
2016-04-22 00:59:17 +02:00
|
|
|
// join with threads
|
|
|
|
Join(std::move(threads));
|
|
|
|
// Flush cache
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run verification on the cache
|
|
|
|
void Verify(const size_t nthreads = 1, const bool eviction_enabled = false) {
|
|
|
|
stats_verify_hits_ = 0;
|
|
|
|
stats_verify_missed_ = 0;
|
|
|
|
key_ = 0;
|
|
|
|
// spawn threads
|
|
|
|
auto fn =
|
|
|
|
std::bind(&PersistentCacheTierTest::VerifyImpl, this, eviction_enabled);
|
2016-06-08 01:55:24 +02:00
|
|
|
auto threads = SpawnThreads(nthreads, fn);
|
2016-04-22 00:59:17 +02:00
|
|
|
// join with threads
|
|
|
|
Join(std::move(threads));
|
|
|
|
}
|
|
|
|
|
|
|
|
// pad 0 to numbers
|
|
|
|
std::string PaddedNumber(const size_t data, const size_t pad_size) {
|
|
|
|
assert(pad_size);
|
|
|
|
char* ret = new char[pad_size];
|
|
|
|
int pos = static_cast<int>(pad_size) - 1;
|
|
|
|
size_t count = 0;
|
|
|
|
size_t t = data;
|
|
|
|
// copy numbers
|
|
|
|
while (t) {
|
|
|
|
count++;
|
|
|
|
ret[pos--] = '0' + t % 10;
|
|
|
|
t = t / 10;
|
|
|
|
}
|
|
|
|
// copy 0s
|
|
|
|
while (pos >= 0) {
|
|
|
|
ret[pos--] = '0';
|
|
|
|
}
|
|
|
|
// post condition
|
|
|
|
assert(count <= pad_size);
|
|
|
|
assert(pos == -1);
|
|
|
|
std::string result(ret, pad_size);
|
|
|
|
delete[] ret;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert workload implementation
|
|
|
|
void InsertImpl() {
|
|
|
|
const std::string prefix = "key_prefix_";
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
size_t i = key_++;
|
|
|
|
if (i >= max_keys_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char data[4 * 1024];
|
|
|
|
memset(data, '0' + (i % 10), sizeof(data));
|
|
|
|
auto k = prefix + PaddedNumber(i, /*count=*/8);
|
|
|
|
Slice key(k);
|
2016-11-22 19:26:08 +01:00
|
|
|
while (true) {
|
|
|
|
Status status = cache_->Insert(key, data, sizeof(data));
|
|
|
|
if (status.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(status.IsTryAgain());
|
2016-06-08 01:07:30 +02:00
|
|
|
Env::Default()->SleepForMicroseconds(1 * 1000 * 1000);
|
2016-04-22 00:59:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verification implementation
|
|
|
|
void VerifyImpl(const bool eviction_enabled = false) {
|
|
|
|
const std::string prefix = "key_prefix_";
|
|
|
|
while (true) {
|
|
|
|
size_t i = key_++;
|
|
|
|
if (i >= max_keys_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char edata[4 * 1024];
|
|
|
|
memset(edata, '0' + (i % 10), sizeof(edata));
|
|
|
|
auto k = prefix + PaddedNumber(i, /*count=*/8);
|
|
|
|
Slice key(k);
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<char[]> block;
|
2016-04-22 00:59:17 +02:00
|
|
|
size_t block_size;
|
|
|
|
|
|
|
|
if (eviction_enabled) {
|
|
|
|
if (!cache_->Lookup(key, &block, &block_size).ok()) {
|
|
|
|
// assume that the key is evicted
|
|
|
|
stats_verify_missed_++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_OK(cache_->Lookup(key, &block, &block_size));
|
|
|
|
ASSERT_EQ(block_size, sizeof(edata));
|
|
|
|
ASSERT_EQ(memcmp(edata, block.get(), sizeof(edata)), 0);
|
|
|
|
stats_verify_hits_++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// template for insert test
|
|
|
|
void RunInsertTest(const size_t nthreads, const size_t max_keys) {
|
|
|
|
Insert(nthreads, max_keys);
|
|
|
|
Verify(nthreads);
|
|
|
|
ASSERT_EQ(stats_verify_hits_, max_keys);
|
|
|
|
ASSERT_EQ(stats_verify_missed_, 0);
|
|
|
|
|
|
|
|
cache_->Close();
|
|
|
|
cache_.reset();
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:26:08 +01:00
|
|
|
// template for negative insert test
|
|
|
|
void RunNegativeInsertTest(const size_t nthreads, const size_t max_keys) {
|
|
|
|
Insert(nthreads, max_keys);
|
|
|
|
Verify(nthreads, /*eviction_enabled=*/true);
|
|
|
|
ASSERT_LT(stats_verify_hits_, max_keys);
|
|
|
|
ASSERT_GT(stats_verify_missed_, 0);
|
|
|
|
|
|
|
|
cache_->Close();
|
|
|
|
cache_.reset();
|
|
|
|
}
|
|
|
|
|
2016-04-22 00:59:17 +02:00
|
|
|
// template for insert with eviction test
|
|
|
|
void RunInsertTestWithEviction(const size_t nthreads, const size_t max_keys) {
|
|
|
|
Insert(nthreads, max_keys);
|
|
|
|
Verify(nthreads, /*eviction_enabled=*/true);
|
|
|
|
ASSERT_EQ(stats_verify_hits_ + stats_verify_missed_, max_keys);
|
|
|
|
ASSERT_GT(stats_verify_hits_, 0);
|
|
|
|
ASSERT_GT(stats_verify_missed_, 0);
|
|
|
|
|
|
|
|
cache_->Close();
|
|
|
|
cache_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string path_;
|
2018-11-09 20:17:34 +01:00
|
|
|
std::shared_ptr<Logger> log_;
|
2016-04-22 00:59:17 +02:00
|
|
|
std::shared_ptr<PersistentCacheTier> cache_;
|
|
|
|
std::atomic<size_t> key_{0};
|
|
|
|
size_t max_keys_ = 0;
|
|
|
|
std::atomic<size_t> stats_verify_hits_{0};
|
|
|
|
std::atomic<size_t> stats_verify_missed_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// RocksDB tests
|
|
|
|
//
|
|
|
|
class PersistentCacheDBTest : public DBTestBase {
|
|
|
|
public:
|
2016-05-03 20:05:42 +02:00
|
|
|
PersistentCacheDBTest();
|
2016-04-22 00:59:17 +02:00
|
|
|
|
2016-06-08 01:07:30 +02:00
|
|
|
static uint64_t TestGetTickerCount(const Options& options,
|
2016-04-22 00:59:17 +02:00
|
|
|
Tickers ticker_type) {
|
2016-06-08 01:55:24 +02:00
|
|
|
return static_cast<uint32_t>(
|
|
|
|
options.statistics->getTickerCount(ticker_type));
|
2016-04-22 00:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert data to table
|
|
|
|
void Insert(const Options& options,
|
2018-03-05 22:08:17 +01:00
|
|
|
const BlockBasedTableOptions& /*table_options*/,
|
|
|
|
const int num_iter, std::vector<std::string>* values) {
|
2016-04-22 00:59:17 +02:00
|
|
|
CreateAndReopenWithCF({"pikachu"}, options);
|
|
|
|
// default column family doesn't have block cache
|
|
|
|
Options no_block_cache_opts;
|
|
|
|
no_block_cache_opts.statistics = options.statistics;
|
|
|
|
no_block_cache_opts = CurrentOptions(no_block_cache_opts);
|
|
|
|
BlockBasedTableOptions table_options_no_bc;
|
|
|
|
table_options_no_bc.no_block_cache = true;
|
|
|
|
no_block_cache_opts.table_factory.reset(
|
|
|
|
NewBlockBasedTableFactory(table_options_no_bc));
|
|
|
|
ReopenWithColumnFamilies(
|
|
|
|
{"default", "pikachu"},
|
|
|
|
std::vector<Options>({no_block_cache_opts, options}));
|
|
|
|
|
|
|
|
Random rnd(301);
|
|
|
|
|
|
|
|
// Write 8MB (80 values, each 100K)
|
|
|
|
ASSERT_EQ(NumTableFilesAtLevel(0, 1), 0);
|
|
|
|
std::string str;
|
|
|
|
for (int i = 0; i < num_iter; i++) {
|
|
|
|
if (i % 4 == 0) { // high compression ratio
|
|
|
|
str = RandomString(&rnd, 1000);
|
|
|
|
}
|
|
|
|
values->push_back(str);
|
|
|
|
ASSERT_OK(Put(1, Key(i), (*values)[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush all data from memtable so that reads are from block cache
|
|
|
|
ASSERT_OK(Flush(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify data
|
|
|
|
void Verify(const int num_iter, const std::vector<std::string>& values) {
|
|
|
|
for (int j = 0; j < 2; ++j) {
|
|
|
|
for (int i = 0; i < num_iter; i++) {
|
|
|
|
ASSERT_EQ(Get(1, Key(i)), values[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test template
|
|
|
|
void RunTest(const std::function<std::shared_ptr<PersistentCacheTier>(bool)>&
|
2016-08-10 22:26:15 +02:00
|
|
|
new_pcache,
|
|
|
|
const size_t max_keys, const size_t max_usecase);
|
2016-04-22 00:59:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
#endif
|