2017-05-10 21:54:35 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
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 {
|
|
|
|
Random s_rnd(301);
|
|
|
|
|
|
|
|
void gen_random(char *s, const int len) {
|
|
|
|
static const char alphanum[] =
|
|
|
|
"0123456789"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
s[i] = alphanum[s_rnd.Next() % (sizeof(alphanum) - 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
s[len] = 0;
|
|
|
|
}
|
|
|
|
|
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-05-10 21:54:35 +00:00
|
|
|
BlobDBTest() : blobdb_(nullptr) {
|
2016-08-09 17:16:32 +00:00
|
|
|
dbname_ = test::TmpDir() + "/blob_db_test";
|
2017-05-31 05:16:32 +00:00
|
|
|
// Reopen(BlobDBOptionsImpl());
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~BlobDBTest() {
|
|
|
|
if (blobdb_) {
|
|
|
|
delete blobdb_;
|
|
|
|
blobdb_ = nullptr;
|
|
|
|
}
|
2016-08-09 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
void Reopen(const BlobDBOptionsImpl &bdboptions,
|
|
|
|
const Options &options = Options()) {
|
2017-05-10 21:54:35 +00:00
|
|
|
if (blobdb_) {
|
|
|
|
delete blobdb_;
|
|
|
|
blobdb_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobDBOptionsImpl bblobdb_options = bdboptions;
|
|
|
|
Options myoptions = options;
|
|
|
|
BlobDB::DestroyBlobDB(dbname_, myoptions, bblobdb_options);
|
|
|
|
|
|
|
|
DestroyDB(dbname_, myoptions);
|
|
|
|
|
|
|
|
myoptions.create_if_missing = true;
|
|
|
|
EXPECT_TRUE(
|
|
|
|
BlobDB::Open(myoptions, bblobdb_options, dbname_, &blobdb_).ok());
|
2017-05-31 05:16:32 +00:00
|
|
|
ASSERT_NE(nullptr, blobdb_);
|
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);
|
|
|
|
ColumnFamilyHandle *cfh = blobdb_->DefaultColumnFamily();
|
|
|
|
ASSERT_OK(blobdb_->PutWithTTL(WriteOptions(), cfh, Slice(key), Slice(value),
|
|
|
|
ttl));
|
|
|
|
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-05-31 05:16:32 +00:00
|
|
|
void Delete(const std::string &key) {
|
|
|
|
ColumnFamilyHandle *cfh = blobdb_->DefaultColumnFamily();
|
|
|
|
ASSERT_OK(blobdb_->Delete(WriteOptions(), cfh, key));
|
|
|
|
}
|
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) {
|
|
|
|
Iterator *iter = blobdb_->NewIterator(ReadOptions());
|
|
|
|
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-05-10 21:54:35 +00:00
|
|
|
BlobDB *blobdb_;
|
2016-08-09 17:16:32 +00:00
|
|
|
std::string dbname_;
|
|
|
|
}; // class BlobDBTest
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
TEST_F(BlobDBTest, DeleteComplex) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
Random rnd(301);
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
PutRandom("key" + ToString(i), &rnd, nullptr);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-05-31 05:16:32 +00:00
|
|
|
for (size_t i = 0; i < 100; i++) {
|
|
|
|
Delete("key" + ToString(i));
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
2017-05-31 05:16:32 +00:00
|
|
|
// DB should be empty.
|
|
|
|
VerifyDB({});
|
2016-08-09 17:16:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
TEST_F(BlobDBTest, OverrideTest) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
|
|
|
bdboptions.num_concurrent_simple_blobs = 2;
|
|
|
|
bdboptions.blob_file_size = 876 * 1024 * 10;
|
|
|
|
|
|
|
|
Options options;
|
|
|
|
options.write_buffer_size = 256 * 1024;
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions, options);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
Random rnd(301);
|
2017-05-31 05:16:32 +00:00
|
|
|
std::map<std::string, std::string> data;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
PutRandom("key" + ToString(i), &rnd, nullptr);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
// override all the keys
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
PutRandom("key" + ToString(i), &rnd, &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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlobDBTest, DeleteTest) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
|
|
|
bdboptions.num_concurrent_simple_blobs = 1;
|
|
|
|
bdboptions.blob_file_size = 876 * 1024;
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
Random rnd(301);
|
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-05-31 05:16:32 +00:00
|
|
|
Delete("key" + ToString(i));
|
|
|
|
data.erase("key" + ToString(i));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlobDBTest, GCTestWithWrite) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
|
|
|
bdboptions.ttl_range_secs = 30;
|
|
|
|
bdboptions.gc_file_pct = 100;
|
|
|
|
bdboptions.gc_check_period_millisecs = 20 * 1000;
|
|
|
|
bdboptions.default_ttl_extractor = true;
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
WriteOptions wo;
|
|
|
|
ReadOptions ro;
|
|
|
|
std::string value;
|
|
|
|
|
|
|
|
ColumnFamilyHandle *dcfh = blobdb_->DefaultColumnFamily();
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
WriteBatch batch;
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
Random rnd(301);
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
int len = rnd.Next() % kMaxBlobSize;
|
2017-05-10 21:54:35 +00:00
|
|
|
if (!len) continue;
|
|
|
|
|
|
|
|
int ttl = 30;
|
|
|
|
|
|
|
|
char *val = new char[len + BlobDB::kTTLSuffixLength];
|
|
|
|
gen_random(val, len);
|
|
|
|
strncpy(val + len, "ttl:", 4);
|
|
|
|
EncodeFixed32(val + len + 4, ttl);
|
|
|
|
|
|
|
|
std::string key("key");
|
|
|
|
key += std::to_string(i);
|
|
|
|
|
|
|
|
Slice keyslice(key);
|
|
|
|
Slice valslice(val, len + BlobDB::kTTLSuffixLength);
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
batch.Put(dcfh, keyslice, valslice);
|
2017-05-10 21:54:35 +00:00
|
|
|
delete[] val;
|
|
|
|
}
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
ASSERT_OK(blobdb_->Write(wo, &batch));
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
// TODO(yiwu): Use sync point to properly trigger GC and check result.
|
|
|
|
// Env::Default()->SleepForMicroseconds(120 * 1000 * 1000);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cb_evict(const ColumnFamilyHandle *cfh, const Slice &key,
|
|
|
|
const Slice &val) {
|
|
|
|
fprintf(stderr, "key evicted: %s\n", key.ToString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *LONG_STRING =
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFJFJFJTWFNLLFKFFMFMFMFMFMFMFMFMFMFMFMFMFMMF "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJAJFJFJFJFJTWBFNMFLLWMFMFMFMWKWMFMFMFMFMFMFM "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH "
|
|
|
|
"AJFJFJFFFFFFFFFFFFFFFFFFFJFHFHFHFHFHFHFHHFHHFHHFH ";
|
|
|
|
|
|
|
|
TEST_F(BlobDBTest, GetWithCompression) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
|
|
|
bdboptions.gc_file_pct = 100;
|
|
|
|
bdboptions.gc_check_period_millisecs = 20 * 1000;
|
|
|
|
bdboptions.default_ttl_extractor = true;
|
|
|
|
bdboptions.gc_evict_cb_fn = &cb_evict;
|
|
|
|
bdboptions.compression = CompressionType::kLZ4Compression;
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
WriteOptions wo;
|
|
|
|
ReadOptions ro;
|
|
|
|
std::string value;
|
|
|
|
Random rnd(301);
|
|
|
|
|
|
|
|
ColumnFamilyHandle *dcfh = blobdb_->DefaultColumnFamily();
|
|
|
|
|
|
|
|
std::string orig(LONG_STRING);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 10000; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
size_t len = orig.length();
|
2017-05-10 21:54:35 +00:00
|
|
|
int ttl = 3000 * (rnd.Next() % 10);
|
|
|
|
|
|
|
|
char *val = new char[len + BlobDB::kTTLSuffixLength];
|
|
|
|
strncpy(val, LONG_STRING, len);
|
|
|
|
strncpy(val + len, "ttl:", 4);
|
|
|
|
EncodeFixed32(val + len + 4, ttl);
|
|
|
|
|
|
|
|
std::string key("key");
|
|
|
|
key += std::to_string(i);
|
|
|
|
|
|
|
|
Slice keyslice(key);
|
|
|
|
Slice valslice(val, len + BlobDB::kTTLSuffixLength);
|
|
|
|
|
|
|
|
ASSERT_OK(blobdb_->Put(wo, dcfh, keyslice, valslice));
|
|
|
|
delete[] val;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 10000; i++) {
|
|
|
|
std::string key("key");
|
|
|
|
key += std::to_string(i);
|
|
|
|
|
|
|
|
Slice keyslice(key);
|
|
|
|
std::string val;
|
|
|
|
Status s = blobdb_->Get(ro, dcfh, keyslice, &val);
|
|
|
|
ASSERT_TRUE(orig == val);
|
|
|
|
}
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
// TODO(yiwu): Use sync point to properly trigger GC and check result.
|
|
|
|
// Env::Default()->SleepForMicroseconds(120 * 1000 * 1000);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlobDBTest, GCTestWithPutAndCompression) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
|
|
|
bdboptions.ttl_range_secs = 30;
|
|
|
|
bdboptions.gc_file_pct = 100;
|
|
|
|
bdboptions.gc_check_period_millisecs = 20 * 1000;
|
|
|
|
bdboptions.default_ttl_extractor = true;
|
|
|
|
bdboptions.gc_evict_cb_fn = &cb_evict;
|
|
|
|
bdboptions.compression = CompressionType::kLZ4Compression;
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
WriteOptions wo;
|
|
|
|
ReadOptions ro;
|
|
|
|
std::string value;
|
|
|
|
Random rnd(301);
|
|
|
|
|
|
|
|
ColumnFamilyHandle *dcfh = blobdb_->DefaultColumnFamily();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
int len = rnd.Next() % kMaxBlobSize;
|
2017-05-10 21:54:35 +00:00
|
|
|
if (!len) continue;
|
|
|
|
|
|
|
|
int ttl = 30;
|
|
|
|
|
|
|
|
char *val = new char[len + BlobDB::kTTLSuffixLength];
|
|
|
|
gen_random(val, len);
|
|
|
|
strncpy(val + len, "ttl:", 4);
|
|
|
|
EncodeFixed32(val + len + 4, ttl);
|
|
|
|
|
|
|
|
std::string key("key");
|
|
|
|
key += std::to_string(i);
|
|
|
|
|
|
|
|
Slice keyslice(key);
|
|
|
|
Slice valslice(val, len + BlobDB::kTTLSuffixLength);
|
|
|
|
|
|
|
|
ASSERT_OK(blobdb_->Put(wo, dcfh, keyslice, valslice));
|
|
|
|
delete[] val;
|
|
|
|
}
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
// TODO(yiwu): Use sync point to properly trigger GC and check result.
|
|
|
|
// Env::Default()->SleepForMicroseconds(120 * 1000 * 1000);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlobDBTest, GCTestWithPut) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
|
|
|
bdboptions.ttl_range_secs = 30;
|
|
|
|
bdboptions.gc_file_pct = 100;
|
|
|
|
bdboptions.gc_check_period_millisecs = 20 * 1000;
|
|
|
|
bdboptions.default_ttl_extractor = true;
|
|
|
|
bdboptions.gc_evict_cb_fn = &cb_evict;
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
WriteOptions wo;
|
|
|
|
ReadOptions ro;
|
|
|
|
std::string value;
|
|
|
|
Random rnd(301);
|
|
|
|
|
|
|
|
ColumnFamilyHandle *dcfh = blobdb_->DefaultColumnFamily();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
int len = rnd.Next() % kMaxBlobSize;
|
2017-05-10 21:54:35 +00:00
|
|
|
if (!len) continue;
|
|
|
|
|
|
|
|
int ttl = 30;
|
|
|
|
|
|
|
|
char *val = new char[len + BlobDB::kTTLSuffixLength];
|
|
|
|
gen_random(val, len);
|
|
|
|
strncpy(val + len, "ttl:", 4);
|
|
|
|
EncodeFixed32(val + len + 4, ttl);
|
|
|
|
|
|
|
|
std::string key("key");
|
|
|
|
key += std::to_string(i);
|
|
|
|
|
|
|
|
Slice keyslice(key);
|
|
|
|
Slice valslice(val, len + BlobDB::kTTLSuffixLength);
|
|
|
|
|
|
|
|
ASSERT_OK(blobdb_->Put(wo, dcfh, keyslice, valslice));
|
|
|
|
delete[] val;
|
|
|
|
}
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
// TODO(yiwu): Use sync point to properly trigger GC and check result.
|
|
|
|
// Env::Default()->SleepForMicroseconds(120 * 1000 * 1000);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlobDBTest, GCTest) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
|
|
|
bdboptions.ttl_range_secs = 30;
|
|
|
|
bdboptions.gc_file_pct = 100;
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
WriteOptions wo;
|
|
|
|
ReadOptions ro;
|
|
|
|
std::string value;
|
|
|
|
Random rnd(301);
|
|
|
|
|
|
|
|
ColumnFamilyHandle *dcfh = blobdb_->DefaultColumnFamily();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
2017-05-31 05:16:32 +00:00
|
|
|
int len = rnd.Next() % kMaxBlobSize;
|
2017-05-10 21:54:35 +00:00
|
|
|
if (!len) continue;
|
|
|
|
|
|
|
|
char *val = new char[len + 1];
|
|
|
|
gen_random(val, len);
|
|
|
|
|
|
|
|
std::string key("key");
|
|
|
|
key += std::to_string(i);
|
|
|
|
|
|
|
|
Slice keyslice(key);
|
|
|
|
Slice valslice(val, len + 1);
|
|
|
|
|
|
|
|
int ttl = 30;
|
|
|
|
|
|
|
|
ASSERT_OK(blobdb_->PutWithTTL(wo, dcfh, keyslice, valslice, ttl));
|
|
|
|
delete[] val;
|
|
|
|
}
|
|
|
|
|
2017-05-31 05:16:32 +00:00
|
|
|
// TODO(yiwu): Use sync point to properly trigger GC and check result.
|
|
|
|
// Env::Default()->SleepForMicroseconds(240 * 1000 * 1000);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlobDBTest, DISABLED_MultipleWriters) {
|
|
|
|
BlobDBOptionsImpl bdboptions;
|
2017-05-31 05:16:32 +00:00
|
|
|
Reopen(bdboptions);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(blobdb_ != nullptr);
|
|
|
|
|
|
|
|
std::vector<std::thread> workers;
|
|
|
|
for (size_t ii = 0; ii < 10; ii++)
|
2017-05-31 05:16:32 +00:00
|
|
|
workers.push_back(std::thread(&BlobDBTest::InsertBlobs, this));
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
for (std::thread &t : workers) {
|
|
|
|
if (t.joinable()) {
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Env::Default()->SleepForMicroseconds(180 * 1000 * 1000);
|
|
|
|
// ASSERT_OK(blobdb_->PutWithTTL(wo, dcfh, "bar", "v2", 60));
|
|
|
|
// ASSERT_OK(blobdb_->Get(ro, dcfh, "foo", &value));
|
|
|
|
// ASSERT_EQ("v1", value);
|
|
|
|
// ASSERT_OK(blobdb_->Get(ro, dcfh, "bar", &value));
|
|
|
|
// ASSERT_EQ("v2", value);
|
|
|
|
}
|
|
|
|
|
2016-08-09 17:16:32 +00:00
|
|
|
TEST_F(BlobDBTest, Large) {
|
2017-05-31 05:16:32 +00:00
|
|
|
BlobDBOptionsImpl bdboptions;
|
|
|
|
Options options;
|
|
|
|
Reopen(bdboptions, options);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2016-08-09 17:16:32 +00:00
|
|
|
WriteOptions wo;
|
|
|
|
ReadOptions ro;
|
|
|
|
std::string value1, value2, value3;
|
|
|
|
Random rnd(301);
|
2017-05-10 21:54:35 +00:00
|
|
|
ColumnFamilyHandle* dcfh = blobdb_->DefaultColumnFamily();
|
2016-08-09 17:16:32 +00:00
|
|
|
|
|
|
|
value1.assign(8999, '1');
|
2017-05-10 21:54:35 +00:00
|
|
|
ASSERT_OK(blobdb_->PutWithTTL(wo, dcfh, "foo", value1, 3600));
|
2016-08-09 17:16:32 +00:00
|
|
|
value2.assign(9001, '2');
|
2017-05-10 21:54:35 +00:00
|
|
|
ASSERT_OK(blobdb_->PutWithTTL(wo, dcfh, "bar", value2, 3600));
|
2016-08-09 17:16:32 +00:00
|
|
|
test::RandomString(&rnd, 13333, &value3);
|
2017-05-10 21:54:35 +00:00
|
|
|
ASSERT_OK(blobdb_->PutWithTTL(wo, dcfh, "barfoo", value3, 3600));
|
2016-08-09 17:16:32 +00:00
|
|
|
|
|
|
|
std::string value;
|
2017-05-10 21:54:35 +00:00
|
|
|
ASSERT_OK(blobdb_->Get(ro, dcfh, "foo", &value));
|
2016-08-09 17:16:32 +00:00
|
|
|
ASSERT_EQ(value1, value);
|
2017-05-10 21:54:35 +00:00
|
|
|
ASSERT_OK(blobdb_->Get(ro, dcfh, "bar", &value));
|
2016-08-09 17:16:32 +00:00
|
|
|
ASSERT_EQ(value2, value);
|
2017-05-10 21:54:35 +00:00
|
|
|
ASSERT_OK(blobdb_->Get(ro, dcfh, "barfoo", &value));
|
2016-08-09 17:16:32 +00:00
|
|
|
ASSERT_EQ(value3, value);
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:45:47 +00:00
|
|
|
// Test sequence number store in blob file is correct.
|
|
|
|
TEST_F(BlobDBTest, SequenceNumber) {
|
|
|
|
Random rnd(223);
|
|
|
|
Reopen(BlobDBOptionsImpl(), Options());
|
|
|
|
SequenceNumber sequence = blobdb_->GetLatestSequenceNumber();
|
|
|
|
BlobDBImpl *blobdb_impl = reinterpret_cast<BlobDBImpl *>(blobdb_);
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
std::string key = "key" + ToString(i);
|
|
|
|
PutRandom(key, &rnd);
|
|
|
|
sequence += 1;
|
|
|
|
ASSERT_EQ(sequence, blobdb_->GetLatestSequenceNumber());
|
|
|
|
SequenceNumber actual_sequence = 0;
|
|
|
|
ASSERT_OK(blobdb_impl->TEST_GetSequenceNumber(key, &actual_sequence));
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
ASSERT_OK(blobdb_->Write(WriteOptions(), &batch));
|
|
|
|
sequence += batch_size;
|
|
|
|
ASSERT_EQ(sequence, blobdb_->GetLatestSequenceNumber());
|
|
|
|
for (size_t k = 0; k < batch_size; k++) {
|
|
|
|
std::string key = "key" + ToString(i) + "-" + ToString(k);
|
|
|
|
SequenceNumber actual_sequence;
|
|
|
|
ASSERT_OK(blobdb_impl->TEST_GetSequenceNumber(key, &actual_sequence));
|
|
|
|
// We only write sequence for the last key in a batch.
|
|
|
|
if (k + 1 < batch_size) {
|
|
|
|
ASSERT_EQ(0, actual_sequence);
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(sequence, actual_sequence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|