Skip to content

Commit

Permalink
Rebase 5.14 (facebook#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianZheng committed Nov 24, 2018
1 parent 63876bd commit 8806cc1
Show file tree
Hide file tree
Showing 55 changed files with 5,848 additions and 2 deletions.
34 changes: 33 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,29 @@ set(SOURCES
utilities/ttl/db_ttl_impl.cc
utilities/write_batch_with_index/write_batch_with_index.cc
utilities/write_batch_with_index/write_batch_with_index_internal.cc
$<TARGET_OBJECTS:build_version>)
utilities/titandb/blob_file_builder.cc
utilities/titandb/blob_file_cache.cc
utilities/titandb/blob_file_iterator.cc
utilities/titandb/blob_file_reader.cc
utilities/titandb/blob_file_size_collector.cc
utilities/titandb/blob_format.cc
utilities/titandb/blob_gc.cc
utilities/titandb/blob_gc_job.cc
utilities/titandb/blob_gc_picker.cc
utilities/titandb/db.cc
utilities/titandb/db_impl.cc
utilities/titandb/db_impl_files.cc
utilities/titandb/db_impl_gc.cc
utilities/titandb/options.cc
utilities/titandb/table_builder.cc
utilities/titandb/table_factory.cc
utilities/titandb/util.cc
utilities/titandb/version.cc
utilities/titandb/version_builder.cc
utilities/titandb/version_edit.cc
utilities/titandb/version_set.cc
$<TARGET_OBJECTS:build_version>
)

if(HAVE_SSE42 AND NOT MSVC)
set_source_files_properties(
Expand Down Expand Up @@ -965,6 +987,16 @@ if(WITH_TESTS)
utilities/transactions/write_unprepared_transaction_test.cc
utilities/ttl/ttl_test.cc
utilities/write_batch_with_index/write_batch_with_index_test.cc
utilities/titandb/blob_file_iterator_test.cc
utilities/titandb/blob_file_size_collector_test.cc
utilities/titandb/blob_file_test.cc
utilities/titandb/blob_format_test.cc
utilities/titandb/blob_gc_job_test.cc
utilities/titandb/blob_gc_picker_test.cc
utilities/titandb/table_builder_test.cc
utilities/titandb/titan_db_test.cc
utilities/titandb/util_test.cc
utilities/titandb/version_test.cc
)
if(WITH_LIBRADOS)
list(APPEND TESTS utilities/env_librados_test.cc)
Expand Down
60 changes: 59 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ rocksdb.h rocksdb.cc: build_tools/amalgamate.py Makefile $(LIB_SOURCES) unity.cc
build_tools/amalgamate.py -I. -i./include unity.cc -x include/rocksdb/c.h -H rocksdb.h -o rocksdb.cc

clean:
rm -f $(BENCHMARKS) $(TOOLS) $(TESTS) $(LIBRARY) $(SHARED)
rm -f $(BENCHMARKS) $(TOOLS) $(TESTS) $(LIBRARY) $(SHARED) $(TITANDB_TESTS)
rm -rf $(CLEAN_FILES) ios-x86 ios-arm scan_build_report
$(FIND) . -name "*.[oda]" -exec rm -f {} \;
$(FIND) . -type f -regex ".*\.\(\(gcda\)\|\(gcno\)\)" -exec rm {} \;
Expand Down Expand Up @@ -1530,6 +1530,64 @@ range_del_aggregator_test: db/range_del_aggregator_test.o db/db_test_util.o $(LI
blob_db_test: utilities/blob_db/blob_db_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

TITANDB_TESTS = \
titandb_blob_file_iterator_test \
titandb_blob_file_size_collector_test \
titandb_blob_file_test \
titandb_blob_format_test \
titandb_blob_gc_job_test \
titandb_blob_gc_picker_test \
titandb_db_test \
titandb_table_builder_test \
titandb_util_test \
titandb_version_test \

titandb_blob_file_iterator_test: utilities/titandb/blob_file_iterator_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_blob_file_size_collector_test: utilities/titandb/blob_file_size_collector_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_blob_file_test: utilities/titandb/blob_file_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_blob_format_test: utilities/titandb/blob_format_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_blob_gc_job_test: utilities/titandb/blob_gc_job_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_blob_gc_picker_test: utilities/titandb/blob_gc_picker_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_db_test: utilities/titandb/titan_db_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_table_builder_test: utilities/titandb/table_builder_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_util_test: utilities/titandb/util_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_version_test: utilities/titandb/version_test.o $(LIBOBJECTS) $(TESTHARNESS)
$(AM_LINK)

titandb_check: $(TITANDB_TESTS)
for t in $(TITANDB_TESTS); \
do \
echo "======== Running $$t ========"; \
./$$t || exit 1; \
done;

titandb_valgrind_check: $(TITANDB_TESTS)
for t in $(TITANDB_TESTS); do \
$(VALGRIND_VER) $(VALGRIND_OPTS) ./$$t; \
code=$$?; \
if [ $$code -ne 0 ]; then \
exit $$code; \
fi; \
done;

#-------------------------------------------------
# make install related stuff
INSTALL_PATH ?= /usr/local
Expand Down
21 changes: 21 additions & 0 deletions src.mk
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,27 @@ LIB_SOURCES = \
utilities/ttl/db_ttl_impl.cc \
utilities/write_batch_with_index/write_batch_with_index.cc \
utilities/write_batch_with_index/write_batch_with_index_internal.cc \
utilities/titandb/blob_file_builder.cc \
utilities/titandb/blob_file_cache.cc \
utilities/titandb/blob_file_iterator.cc \
utilities/titandb/blob_file_reader.cc \
utilities/titandb/blob_file_size_collector.cc \
utilities/titandb/blob_format.cc \
utilities/titandb/blob_gc.cc \
utilities/titandb/blob_gc_job.cc \
utilities/titandb/blob_gc_picker.cc \
utilities/titandb/db.cc \
utilities/titandb/db_impl.cc \
utilities/titandb/db_impl_files.cc \
utilities/titandb/db_impl_gc.cc \
utilities/titandb/options.cc \
utilities/titandb/table_builder.cc \
utilities/titandb/table_factory.cc \
utilities/titandb/util.cc \
utilities/titandb/version.cc \
utilities/titandb/version_builder.cc \
utilities/titandb/version_edit.cc \
utilities/titandb/version_set.cc \

ifeq (,$(shell $(CXX) -fsyntax-only -maltivec -xc /dev/null 2>&1))
LIB_SOURCES_ASM =\
Expand Down
57 changes: 57 additions & 0 deletions utilities/titandb/blob_file_builder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "utilities/titandb/blob_file_builder.h"

#include "util/crc32c.h"
#include "utilities/titandb/util.h"

namespace rocksdb {
namespace titandb {

// HEADER: 8 bytes length
// BODY: variable length
// TAIL: 5 bytes length
void BlobFileBuilder::Add(const BlobRecord& record, BlobHandle* handle) {
if (!ok()) return;

buffer_.clear();
assert(!record.key.empty());
assert(!record.value.empty());
record.EncodeTo(&buffer_);

CompressionType compression = options_.blob_file_compression;
auto output = Compress(&compression, buffer_, &compressed_buffer_);

uint64_t body_length = output.size();
status_ = file_->Append(
Slice{reinterpret_cast<const char*>(&body_length), kBlobHeaderSize});
if (!ok()) return;

handle->offset = file_->GetFileSize();
handle->size = output.size();

status_ = file_->Append(output);
if (ok()) {
char tailer[kBlobTailerSize];
tailer[0] = compression;
EncodeFixed32(tailer+1, crc32c::Value(output.data(), output.size()));
status_ = file_->Append(Slice(tailer, sizeof(tailer)));
}
}

Status BlobFileBuilder::Finish() {
if (!ok()) return status();

BlobFileFooter footer;
buffer_.clear();
footer.EncodeTo(&buffer_);

status_ = file_->Append(buffer_);
if (ok()) {
status_ = file_->Flush();
}
return status();
}

void BlobFileBuilder::Abandon() {}

} // namespace titandb
} // namespace rocksdb
69 changes: 69 additions & 0 deletions utilities/titandb/blob_file_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include "util/file_reader_writer.h"
#include "utilities/titandb/blob_format.h"
#include "utilities/titandb/options.h"

namespace rocksdb {
namespace titandb {

// Blob file format:
//
// <begin>
// [blob record 1]
// [blob record 2]
// ...
// [blob record N]
// [meta block 1]
// [meta block 2]
// ...
// [meta block K]
// [meta index block]
// [footer]
// <end>
//
// 1. The sequence of blob records in the file are stored in sorted
// order. These records come one after another at the beginning of the
// file, and are compressed according to the compression options.
//
// 2. After the blob records we store a bunch of meta blocks, and a
// meta index block with block handles pointed to the meta blocks. The
// meta block and the meta index block are formatted the same as the
// BlockBasedTable.

class BlobFileBuilder {
public:
// Constructs a builder that will store the contents of the file it
// is building in "*file". Does not close the file. It is up to the
// caller to sync and close the file after calling Finish().
BlobFileBuilder(const TitanCFOptions& options, WritableFileWriter* file)
: options_(options), file_(file) {}

// Adds the record to the file and points the handle to it.
void Add(const BlobRecord& record, BlobHandle* handle);

// Returns non-ok iff some error has been detected.
Status status() const { return status_; }

// Finishes building the table.
// REQUIRES: Finish(), Abandon() have not been called.
Status Finish();

// Abandons building the table. If the caller is not going to call
// Finish(), it must call Abandon() before destroying this builder.
// REQUIRES: Finish(), Abandon() have not been called.
void Abandon();

private:
bool ok() const { return status().ok(); }

TitanCFOptions options_;
WritableFileWriter* file_;

Status status_;
std::string buffer_;
std::string compressed_buffer_;
};

} // namespace titandb
} // namespace rocksdb
89 changes: 89 additions & 0 deletions utilities/titandb/blob_file_cache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "utilities/titandb/blob_file_cache.h"

#include "util/filename.h"
#include "util/file_reader_writer.h"
#include "utilities/titandb/util.h"

namespace rocksdb {
namespace titandb {

namespace {

Slice EncodeFileNumber(const uint64_t* number) {
return Slice(reinterpret_cast<const char*>(number), sizeof(*number));
}

} // namespace

BlobFileCache::BlobFileCache(const TitanDBOptions& db_options,
const TitanCFOptions& cf_options,
std::shared_ptr<Cache> cache)
: env_(db_options.env),
env_options_(db_options),
db_options_(db_options),
cf_options_(cf_options),
cache_(cache) {}

Status BlobFileCache::Get(const ReadOptions& options, uint64_t file_number,
uint64_t file_size, const BlobHandle& handle,
BlobRecord* record, PinnableSlice* buffer) {
Cache::Handle* cache_handle = nullptr;
Status s = FindFile(file_number, file_size, &cache_handle);
if (!s.ok()) return s;

auto reader = reinterpret_cast<BlobFileReader*>(cache_->Value(cache_handle));
s = reader->Get(options, handle, record, buffer);
cache_->Release(cache_handle);
return s;
}

Status BlobFileCache::NewPrefetcher(
uint64_t file_number,
uint64_t file_size,
std::unique_ptr<BlobFilePrefetcher>* result) {
Cache::Handle* cache_handle = nullptr;
Status s = FindFile(file_number, file_size, &cache_handle);
if (!s.ok()) return s;

auto reader = reinterpret_cast<BlobFileReader*>(cache_->Value(cache_handle));
auto prefetcher = new BlobFilePrefetcher(reader);
prefetcher->RegisterCleanup(&UnrefCacheHandle, cache_.get(), cache_handle);
result->reset(prefetcher);
return s;
}

void BlobFileCache::Evict(uint64_t file_number) {
cache_->Erase(EncodeFileNumber(&file_number));
}

Status BlobFileCache::FindFile(uint64_t file_number,
uint64_t file_size,
Cache::Handle** handle) {
Status s;
Slice cache_key = EncodeFileNumber(&file_number);
*handle = cache_->Lookup(cache_key);
if (*handle) return s;

std::unique_ptr<RandomAccessFileReader> file;
{
std::unique_ptr<RandomAccessFile> f;
auto file_name = BlobFileName(db_options_.dirname, file_number);
s = env_->NewRandomAccessFile(file_name, &f, env_options_);
if (!s.ok()) return s;
if (db_options_.advise_random_on_open) {
f->Hint(RandomAccessFile::RANDOM);
}
file.reset(new RandomAccessFileReader(std::move(f), file_name));
}

std::unique_ptr<BlobFileReader> reader;
s = BlobFileReader::Open(cf_options_, std::move(file), file_size, &reader);
if (!s.ok()) return s;

cache_->Insert(cache_key, reader.release(), 1,
&DeleteCacheValue<BlobFileReader>, handle);
return s;
}

} // namespace titandb
} // namespace rocksdb
Loading

0 comments on commit 8806cc1

Please sign in to comment.