2017-05-10 21:54:35 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00: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).
|
2017-06-14 20:08:54 +00:00
|
|
|
|
2016-08-09 17:16:32 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
#include "utilities/blob_db/blob_db.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include <cstdlib>
|
2017-05-31 05:16:32 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "db/db_test_util.h"
|
2017-05-31 05:16:32 +00:00
|
|
|
#include "port/port.h"
|
2016-08-09 17:16:32 +00:00
|
|
|
#include "util/random.h"
|
2017-05-31 05:16:32 +00:00
|
|
|
#include "util/string_util.h"
|
2016-08-09 17:16:32 +00:00
|
|
|
#include "util/testharness.h"
|
2017-05-31 05:16:32 +00:00
|
|
|
#include "utilities/blob_db/blob_db_impl.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "utilities/blob_db/blob_db_options_impl.h"
|
2016-08-09 17:16:32 +00:00
|
|
|
|
|
|
|
namespace rocksdb {
|
2017-05-10 21:54:35 +00:00
|
|
|
namespace blob_db {
|
|
|
|
|
2016-08-09 17:16:32 +00:00
|
|
|
class BlobDBTest : public testing::Test {
|
|
|
|
public:
|
2017-05-31 05:16:32 +00:00
|
|
|
const int kMaxBlobSize = 1 << 14;
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
BlobDBTest() : dbname_(test::TmpDir() + "/blob_db_test"), blob_db_(nullptr) {
|
|
|
|
Status s = DestroyBlobDB(dbname_, Options(), BlobDBOptions());
|
|
|
|
assert(s.ok());
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
~BlobDBTest() { Destroy(); }
|
|
|
|
|
|
|
|
void Open(BlobDBOptionsImpl bdb_options = BlobDBOptionsImpl(),
|
|
|
|
Options options = Options()) {
|
|
|
|
options.create_if_missing = true;
|
|
|
|
ASSERT_OK(BlobDB::Open(options, bdb_options, dbname_, &blob_db_));
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
void Destroy() {
|
|
|
|
if (blob_db_) {
|
|
|
|
Options options = blob_db_->GetOptions();
|
|
|
|
BlobDBOptions bdb_options = blob_db_->GetBlobDBOptions();
|
|
|
|
delete blob_db_;
|
|
|
|
ASSERT_OK(DestroyBlobDB(dbname_, options, bdb_options));
|
|
|
|
blob_db_ = nullptr;
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
void PutRandomWithTTL(const std::string &key, int32_t ttl, Random *rnd,
|
|
|
|
std::map<std::string, std::string> *data = nullptr) {
|
|
|
|
int len = rnd->Next() % kMaxBlobSize + 1;
|
|
|
|
std::string value = test::RandomHumanReadableString(rnd, len);
|
2017-06-14 20:08:54 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
blob_db_->PutWithTTL(WriteOptions(), Slice(key), Slice(value), ttl));
|
2017-05-31 05:16:32 +00:00
|
|
|
if (data != nullptr) {
|
|
|
|
(*data)[key] = value;
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
void PutRandom(const std::string &key, Random *rnd,
|
|
|
|
std::map<std::string, std::string> *data = nullptr) {
|
|
|
|
PutRandomWithTTL(key, -1, rnd, data);
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
void PutRandomToWriteBatch(
|
|
|
|
const std::string &key, Random *rnd, WriteBatch *batch,
|
|
|
|
std::map<std::string, std::string> *data = nullptr) {
|
|
|
|
int len = rnd->Next() % kMaxBlobSize + 1;
|
|
|
|
std::string value = test::RandomHumanReadableString(rnd, len);
|
|
|
|
ASSERT_OK(batch->Put(key, value));
|
|
|
|
if (data != nullptr) {
|
|
|
|
(*data)[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Delete(const std::string &key,
|
|
|
|
std::map<std::string, std::string> *data = nullptr) {
|
|
|
|
ASSERT_OK(blob_db_->Delete(WriteOptions(), key));
|
|
|
|
if (data != nullptr) {
|
|
|
|
data->erase(key);
|
|
|
|
}
|
2017-05-31 05:16:32 +00:00
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
// Verify blob db contain expected data and nothing more.
|
|
|
|
// TODO(yiwu): Verify blob files are consistent with data in LSM.
|
|
|
|
void VerifyDB(const std::map<std::string, std::string> &data) {
|
2017-06-14 20:08:54 +00:00
|
|
|
Iterator *iter = blob_db_->NewIterator(ReadOptions());
|
2017-05-31 05:16:32 +00:00
|
|
|
iter->SeekToFirst();
|
|
|
|
for (auto &p : data) {
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
ASSERT_EQ(p.first, iter->key().ToString());
|
|
|
|
ASSERT_EQ(p.second, iter->value().ToString());
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
ASSERT_FALSE(iter->Valid());
|
|
|
|
ASSERT_OK(iter->status());
|
|
|
|
delete iter;
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
void InsertBlobs() {
|
|
|
|
WriteOptions wo;
|
|
|
|
std::string value;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
Random rnd(301);
|
|
|
|
for (size_t i = 0; i < 100000; i++) {
|
|
|
|
int32_t ttl = rnd.Next() % 86400;
|
|
|
|
PutRandomWithTTL("key" + ToString(i % 500), ttl, &rnd, nullptr);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 10; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
Delete("key" + ToString(i % 500));
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-09 17:16:32 +00:00
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
const std::string dbname_;
|
|
|
|
BlobDB *blob_db_;
|
2016-08-09 17:16:32 +00:00
|
|
|
}; // class BlobDBTest
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
TEST_F(BlobDBTest, Put) {
|
2017-05-10 21:54:35 +00:00
|
|
|
Random rnd(301);
|
2017-06-14 20:08:54 +00:00
|
|
|
BlobDBOptionsImpl bdb_options;
|
|
|
|
bdb_options.disable_background_tasks = true;
|
|
|
|
Open(bdb_options);
|
|
|
|
std::map<std::string, std::string> data;
|
2017-05-10 21:54:35 +00:00
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-06-14 20:08:54 +00:00
|
|
|
PutRandom("key" + ToString(i), &rnd, &data);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-06-14 20:08:54 +00:00
|
|
|
VerifyDB(data);
|
2016-08-09 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 00:56:40 +00:00
|
|
|
TEST_F(BlobDBTest, StackableDBGet) {
|
|
|
|
Random rnd(301);
|
|
|
|
BlobDBOptionsImpl bdb_options;
|
|
|
|
bdb_options.disable_background_tasks = true;
|
|
|
|
Open(bdb_options);
|
|
|
|
std::map<std::string, std::string> data;
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
|
|
|
PutRandom("key" + ToString(i), &rnd, &data);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
|
|
|
StackableDB *db = blob_db_;
|
|
|
|
ColumnFamilyHandle *column_family = db->DefaultColumnFamily();
|
|
|
|
std::string key = "key" + ToString(i);
|
|
|
|
PinnableSlice pinnable_value;
|
|
|
|
ASSERT_OK(db->Get(ReadOptions(), column_family, key, &pinnable_value));
|
|
|
|
std::string string_value;
|
|
|
|
ASSERT_OK(db->Get(ReadOptions(), column_family, key, &string_value));
|
|
|
|
ASSERT_EQ(string_value, pinnable_value.ToString());
|
|
|
|
ASSERT_EQ(string_value, data[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
TEST_F(BlobDBTest, WriteBatch) {
|
2017-05-10 21:54:35 +00:00
|
|
|
Random rnd(301);
|
2017-06-14 20:08:54 +00:00
|
|
|
BlobDBOptionsImpl bdb_options;
|
|
|
|
bdb_options.disable_background_tasks = true;
|
|
|
|
Open(bdb_options);
|
2017-05-31 05:16:32 +00:00
|
|
|
std::map<std::string, std::string> data;
|
2017-06-14 20:08:54 +00:00
|
|
|
for (size_t i = 0; i < 100; i++) {
|
|
|
|
WriteBatch batch;
|
|
|
|
for (size_t j = 0; j < 10; j++) {
|
|
|
|
PutRandomToWriteBatch("key" + ToString(j * 100 + i), &rnd, &batch, &data);
|
|
|
|
}
|
|
|
|
blob_db_->Write(WriteOptions(), &batch);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-05-31 05:16:32 +00:00
|
|
|
VerifyDB(data);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
TEST_F(BlobDBTest, Delete) {
|
2017-05-10 21:54:35 +00:00
|
|
|
Random rnd(301);
|
2017-06-14 20:08:54 +00:00
|
|
|
BlobDBOptionsImpl bdb_options;
|
|
|
|
bdb_options.disable_background_tasks = true;
|
|
|
|
Open(bdb_options);
|
2017-05-31 05:16:32 +00:00
|
|
|
std::map<std::string, std::string> data;
|
2017-05-10 21:54:35 +00:00
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
PutRandom("key" + ToString(i), &rnd, &data);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < 100; i += 5) {
|
2017-06-14 20:08:54 +00:00
|
|
|
Delete("key" + ToString(i), &data);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-05-31 05:16:32 +00:00
|
|
|
VerifyDB(data);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
TEST_F(BlobDBTest, DeleteBatch) {
|
2017-05-10 21:54:35 +00:00
|
|
|
Random rnd(301);
|
2017-06-14 20:08:54 +00:00
|
|
|
BlobDBOptionsImpl bdb_options;
|
|
|
|
bdb_options.disable_background_tasks = true;
|
|
|
|
Open(bdb_options);
|
2017-05-10 21:54:35 +00:00
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-06-14 20:08:54 +00:00
|
|
|
PutRandom("key" + ToString(i), &rnd);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-06-14 20:08:54 +00:00
|
|
|
WriteBatch batch;
|
2017-05-10 21:54:35 +00:00
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-06-14 20:08:54 +00:00
|
|
|
batch.Delete("key" + ToString(i));
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-06-14 20:08:54 +00:00
|
|
|
ASSERT_OK(blob_db_->Write(WriteOptions(), &batch));
|
|
|
|
// DB should be empty.
|
|
|
|
VerifyDB({});
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
TEST_F(BlobDBTest, Override) {
|
2017-05-10 21:54:35 +00:00
|
|
|
Random rnd(301);
|
2017-06-14 20:08:54 +00:00
|
|
|
BlobDBOptionsImpl bdb_options;
|
|
|
|
bdb_options.disable_background_tasks = true;
|
|
|
|
Open(bdb_options);
|
|
|
|
std::map<std::string, std::string> data;
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
|
|
|
PutRandom("key" + ToString(i), &rnd, nullptr);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-06-14 20:08:54 +00:00
|
|
|
// override all the keys
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
|
|
|
PutRandom("key" + ToString(i), &rnd, &data);
|
|
|
|
}
|
|
|
|
VerifyDB(data);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 20:08:54 +00:00
|
|
|
#ifdef SNAPPY
|
2017-06-14 20:44:36 +00:00
|
|
|
TEST_F(BlobDBTest, Compression) {
|
2017-05-10 21:54:35 +00:00
|
|
|
Random rnd(301);
|
2017-06-14 20:08:54 +00:00
|
|
|
BlobDBOptionsImpl bdb_options;
|
|
|
|
bdb_options.disable_background_tasks = true;
|
|
|
|
bdb_options.compression = CompressionType::kSnappyCompression;
|
|
|
|
Open(bdb_options);
|
|
|
|
std::map<std::string, std::string> data;
|
2017-05-10 21:54:35 +00:00
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-06-14 20:08:54 +00:00
|
|
|
PutRandom("put-key" + ToString(i), &rnd, &data);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-06-14 20:08:54 +00:00
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
WriteBatch batch;
|
|
|
|
for (size_t j = 0; j < 10; j++) {
|
|
|
|
PutRandomToWriteBatch("write-batch-key" + ToString(j * 100 + i), &rnd,
|
|
|
|
&batch, &data);
|
|
|
|
}
|
|
|
|
blob_db_->Write(WriteOptions(), &batch);
|
|
|
|
}
|
|
|
|
VerifyDB(data);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-06-14 20:08:54 +00:00
|
|
|
#endif
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
TEST_F(BlobDBTest, DISABLED_MultipleWriters) {
|
2017-06-14 20:08:54 +00:00
|
|
|
Open();
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-06-20 17:16:24 +00:00
|
|
|
std::vector<port::Thread> workers;
|
2017-05-10 21:54:35 +00:00
|
|
|
for (size_t ii = 0; ii < 10; ii++)
|
2017-06-20 17:16:24 +00:00
|
|
|
workers.push_back(port::Thread(&BlobDBTest::InsertBlobs, this));
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-06-20 17:16:24 +00:00
|
|
|
for (auto& t : workers) {
|
2017-05-10 21:54:35 +00:00
|
|
|
if (t.joinable()) {
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 17:45:47 +00:00
|
|
|
// Test sequence number store in blob file is correct.
|
|
|
|
TEST_F(BlobDBTest, SequenceNumber) {
|
2017-06-14 20:08:54 +00:00
|
|
|
Random rnd(301);
|
|
|
|
BlobDBOptionsImpl bdb_options;
|
|
|
|
bdb_options.disable_background_tasks = true;
|
|
|
|
Open(bdb_options);
|
|
|
|
SequenceNumber sequence = blob_db_->GetLatestSequenceNumber();
|
|
|
|
BlobDBImpl *blob_db_impl = reinterpret_cast<BlobDBImpl *>(blob_db_);
|
2017-05-31 17:45:47 +00:00
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
std::string key = "key" + ToString(i);
|
|
|
|
PutRandom(key, &rnd);
|
|
|
|
sequence += 1;
|
2017-06-14 20:08:54 +00:00
|
|
|
ASSERT_EQ(sequence, blob_db_->GetLatestSequenceNumber());
|
2017-05-31 17:45:47 +00:00
|
|
|
SequenceNumber actual_sequence = 0;
|
2017-06-14 20:08:54 +00:00
|
|
|
ASSERT_OK(blob_db_impl->TEST_GetSequenceNumber(key, &actual_sequence));
|
2017-05-31 17:45:47 +00:00
|
|
|
ASSERT_EQ(sequence, actual_sequence);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
WriteBatch batch;
|
|
|
|
size_t batch_size = rnd.Next() % 10 + 1;
|
|
|
|
for (size_t k = 0; k < batch_size; k++) {
|
|
|
|
std::string value = test::RandomHumanReadableString(&rnd, 1000);
|
|
|
|
ASSERT_OK(batch.Put("key" + ToString(i) + "-" + ToString(k), value));
|
|
|
|
}
|
2017-06-14 20:08:54 +00:00
|
|
|
ASSERT_OK(blob_db_->Write(WriteOptions(), &batch));
|
2017-05-31 17:45:47 +00:00
|
|
|
for (size_t k = 0; k < batch_size; k++) {
|
|
|
|
std::string key = "key" + ToString(i) + "-" + ToString(k);
|
2017-06-13 19:37:59 +00:00
|
|
|
sequence++;
|
2017-05-31 17:45:47 +00:00
|
|
|
SequenceNumber actual_sequence;
|
2017-06-14 20:08:54 +00:00
|
|
|
ASSERT_OK(blob_db_impl->TEST_GetSequenceNumber(key, &actual_sequence));
|
2017-06-13 19:37:59 +00:00
|
|
|
ASSERT_EQ(sequence, actual_sequence);
|
2017-05-31 17:45:47 +00:00
|
|
|
}
|
2017-06-14 20:08:54 +00:00
|
|
|
ASSERT_EQ(sequence, blob_db_->GetLatestSequenceNumber());
|
2017-05-31 17:45:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
} // namespace blob_db
|
2016-08-09 17:16:32 +00:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
// A black-box test for the ttl wrapper around rocksdb
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
fprintf(stderr, "SKIPPED as BlobDB is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|