2017-05-10 23:54:35 +02:00
|
|
|
// Copyright (c) 2011-present, 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).
|
2017-05-10 23:54:35 +02:00
|
|
|
//
|
2016-08-09 19:16:32 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2017-08-01 21:48:22 +02:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
2017-05-10 23:54:35 +02:00
|
|
|
#include "utilities/blob_db/blob_db.h"
|
2017-08-01 21:48:22 +02:00
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2016-08-09 19:16:32 +02:00
|
|
|
#include "db/write_batch_internal.h"
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/instrumented_mutex.h"
|
|
|
|
#include "options/cf_options.h"
|
2017-05-10 23:54:35 +02:00
|
|
|
#include "rocksdb/compaction_filter.h"
|
2016-08-09 19:16:32 +02:00
|
|
|
#include "rocksdb/convenience.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
|
|
|
#include "rocksdb/utilities/stackable_db.h"
|
|
|
|
#include "table/block.h"
|
|
|
|
#include "table/block_based_table_builder.h"
|
|
|
|
#include "table/block_builder.h"
|
|
|
|
#include "util/file_reader_writer.h"
|
2017-04-04 03:27:24 +02:00
|
|
|
#include "util/filename.h"
|
2017-11-03 01:26:46 +01:00
|
|
|
#include "utilities/blob_db/blob_compaction_filter.h"
|
2017-05-10 23:54:35 +02:00
|
|
|
#include "utilities/blob_db/blob_db_impl.h"
|
2016-08-09 19:16:32 +02:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
2017-05-10 23:54:35 +02:00
|
|
|
namespace blob_db {
|
|
|
|
port::Mutex listener_mutex;
|
|
|
|
typedef std::shared_ptr<BlobDBFlushBeginListener> FlushBeginListener_t;
|
|
|
|
typedef std::shared_ptr<BlobReconcileWalFilter> ReconcileWalFilter_t;
|
|
|
|
typedef std::shared_ptr<EvictAllVersionsCompactionListener>
|
|
|
|
CompactionListener_t;
|
|
|
|
|
|
|
|
// to ensure the lifetime of the listeners
|
|
|
|
std::vector<std::shared_ptr<EventListener>> all_blobdb_listeners;
|
|
|
|
std::vector<ReconcileWalFilter_t> all_wal_filters;
|
|
|
|
|
|
|
|
Status BlobDB::OpenAndLoad(const Options& options,
|
|
|
|
const BlobDBOptions& bdb_options,
|
|
|
|
const std::string& dbname, BlobDB** blob_db,
|
|
|
|
Options* changed_options) {
|
2017-11-03 01:26:46 +01:00
|
|
|
if (options.compaction_filter != nullptr ||
|
|
|
|
options.compaction_filter_factory != nullptr) {
|
|
|
|
return Status::NotSupported("Blob DB doesn't support compaction filter.");
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:35 +02:00
|
|
|
*changed_options = options;
|
|
|
|
*blob_db = nullptr;
|
|
|
|
|
|
|
|
FlushBeginListener_t fblistener =
|
|
|
|
std::make_shared<BlobDBFlushBeginListener>();
|
|
|
|
ReconcileWalFilter_t rw_filter = std::make_shared<BlobReconcileWalFilter>();
|
|
|
|
CompactionListener_t ce_listener =
|
|
|
|
std::make_shared<EvictAllVersionsCompactionListener>();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexLock l(&listener_mutex);
|
|
|
|
all_blobdb_listeners.push_back(fblistener);
|
2017-11-02 23:47:30 +01:00
|
|
|
if (bdb_options.enable_garbage_collection) {
|
|
|
|
all_blobdb_listeners.push_back(ce_listener);
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
all_wal_filters.push_back(rw_filter);
|
2016-08-09 19:16:32 +02:00
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2017-11-03 01:26:46 +01:00
|
|
|
changed_options->compaction_filter_factory.reset(
|
|
|
|
new BlobIndexCompactionFilterFactory(options.env));
|
2017-05-10 23:54:35 +02:00
|
|
|
changed_options->listeners.emplace_back(fblistener);
|
2017-11-02 23:47:30 +01:00
|
|
|
if (bdb_options.enable_garbage_collection) {
|
|
|
|
changed_options->listeners.emplace_back(ce_listener);
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
changed_options->wal_filter = rw_filter.get();
|
|
|
|
|
|
|
|
DBOptions db_options(*changed_options);
|
|
|
|
|
|
|
|
// we need to open blob db first so that recovery can happen
|
|
|
|
BlobDBImpl* bdb = new BlobDBImpl(dbname, bdb_options, db_options);
|
|
|
|
|
|
|
|
fblistener->SetImplPtr(bdb);
|
2017-11-02 23:47:30 +01:00
|
|
|
if (bdb_options.enable_garbage_collection) {
|
|
|
|
ce_listener->SetImplPtr(bdb);
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
rw_filter->SetImplPtr(bdb);
|
|
|
|
|
|
|
|
Status s = bdb->OpenPhase1();
|
|
|
|
if (!s.ok()) return s;
|
|
|
|
|
2016-08-09 19:16:32 +02:00
|
|
|
*blob_db = bdb;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:35 +02:00
|
|
|
Status BlobDB::Open(const Options& options, const BlobDBOptions& bdb_options,
|
|
|
|
const std::string& dbname, BlobDB** blob_db) {
|
|
|
|
*blob_db = nullptr;
|
|
|
|
DBOptions db_options(options);
|
|
|
|
ColumnFamilyOptions cf_options(options);
|
|
|
|
std::vector<ColumnFamilyDescriptor> column_families;
|
|
|
|
column_families.push_back(
|
|
|
|
ColumnFamilyDescriptor(kDefaultColumnFamilyName, cf_options));
|
|
|
|
std::vector<ColumnFamilyHandle*> handles;
|
|
|
|
Status s = BlobDB::Open(db_options, bdb_options, dbname, column_families,
|
|
|
|
&handles, blob_db);
|
|
|
|
if (s.ok()) {
|
|
|
|
assert(handles.size() == 1);
|
|
|
|
// i can delete the handle since DBImpl is always holding a reference to
|
|
|
|
// default column family
|
|
|
|
delete handles[0];
|
2016-08-09 19:16:32 +02:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-08-01 21:48:22 +02:00
|
|
|
Status BlobDB::Open(const DBOptions& db_options_input,
|
2017-05-10 23:54:35 +02:00
|
|
|
const BlobDBOptions& bdb_options, const std::string& dbname,
|
|
|
|
const std::vector<ColumnFamilyDescriptor>& column_families,
|
|
|
|
std::vector<ColumnFamilyHandle*>* handles, BlobDB** blob_db,
|
|
|
|
bool no_base_db) {
|
2017-11-03 01:26:46 +01:00
|
|
|
if (column_families.size() != 1 ||
|
|
|
|
column_families[0].name != kDefaultColumnFamilyName) {
|
|
|
|
return Status::NotSupported(
|
|
|
|
"Blob DB doesn't support non-default column family.");
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
*blob_db = nullptr;
|
2017-08-01 21:48:22 +02:00
|
|
|
Status s;
|
|
|
|
|
|
|
|
DBOptions db_options(db_options_input);
|
|
|
|
if (db_options.info_log == nullptr) {
|
|
|
|
s = CreateLoggerFromOptions(dbname, db_options, &db_options.info_log);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 19:16:32 +02:00
|
|
|
|
2017-05-10 23:54:35 +02:00
|
|
|
FlushBeginListener_t fblistener =
|
|
|
|
std::make_shared<BlobDBFlushBeginListener>();
|
|
|
|
CompactionListener_t ce_listener =
|
|
|
|
std::make_shared<EvictAllVersionsCompactionListener>();
|
|
|
|
ReconcileWalFilter_t rw_filter = std::make_shared<BlobReconcileWalFilter>();
|
2016-08-09 19:16:32 +02:00
|
|
|
|
2017-08-01 21:48:22 +02:00
|
|
|
db_options.listeners.emplace_back(fblistener);
|
2017-11-02 23:47:30 +01:00
|
|
|
if (bdb_options.enable_garbage_collection) {
|
|
|
|
db_options.listeners.emplace_back(ce_listener);
|
|
|
|
}
|
2017-08-01 21:48:22 +02:00
|
|
|
db_options.wal_filter = rw_filter.get();
|
2016-08-09 19:16:32 +02:00
|
|
|
|
|
|
|
{
|
2017-05-10 23:54:35 +02:00
|
|
|
MutexLock l(&listener_mutex);
|
|
|
|
all_blobdb_listeners.push_back(fblistener);
|
2017-11-02 23:47:30 +01:00
|
|
|
if (bdb_options.enable_garbage_collection) {
|
|
|
|
all_blobdb_listeners.push_back(ce_listener);
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
all_wal_filters.push_back(rw_filter);
|
2016-08-09 19:16:32 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 01:26:46 +01:00
|
|
|
ColumnFamilyOptions cf_options(column_families[0].options);
|
|
|
|
if (cf_options.compaction_filter != nullptr ||
|
|
|
|
cf_options.compaction_filter_factory != nullptr) {
|
|
|
|
return Status::NotSupported("Blob DB doesn't support compaction filter.");
|
|
|
|
}
|
|
|
|
cf_options.compaction_filter_factory.reset(
|
|
|
|
new BlobIndexCompactionFilterFactory(db_options.env));
|
|
|
|
ColumnFamilyDescriptor cf_descriptor(kDefaultColumnFamilyName, cf_options);
|
|
|
|
|
2017-05-10 23:54:35 +02:00
|
|
|
// we need to open blob db first so that recovery can happen
|
2017-08-01 21:48:22 +02:00
|
|
|
BlobDBImpl* bdb = new BlobDBImpl(dbname, bdb_options, db_options);
|
2017-05-10 23:54:35 +02:00
|
|
|
fblistener->SetImplPtr(bdb);
|
2017-11-02 23:47:30 +01:00
|
|
|
if (bdb_options.enable_garbage_collection) {
|
|
|
|
ce_listener->SetImplPtr(bdb);
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
rw_filter->SetImplPtr(bdb);
|
|
|
|
|
2017-08-01 21:48:22 +02:00
|
|
|
s = bdb->OpenPhase1();
|
|
|
|
if (!s.ok()) {
|
2017-09-01 23:08:19 +02:00
|
|
|
delete bdb;
|
2017-08-01 21:48:22 +02:00
|
|
|
return s;
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
|
2017-08-01 21:48:22 +02:00
|
|
|
if (no_base_db) {
|
2017-09-01 23:08:19 +02:00
|
|
|
*blob_db = bdb;
|
2017-08-01 21:48:22 +02:00
|
|
|
return s;
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
|
|
|
|
DB* db = nullptr;
|
2017-11-03 01:26:46 +01:00
|
|
|
s = DB::Open(db_options, dbname, {cf_descriptor}, handles, &db);
|
2017-08-01 21:48:22 +02:00
|
|
|
if (!s.ok()) {
|
2017-09-01 23:08:19 +02:00
|
|
|
delete bdb;
|
2017-08-01 21:48:22 +02:00
|
|
|
return s;
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
|
|
|
|
// set the implementation pointer
|
|
|
|
s = bdb->LinkToBaseDB(db);
|
2016-08-09 19:16:32 +02:00
|
|
|
if (!s.ok()) {
|
2017-05-10 23:54:35 +02:00
|
|
|
delete bdb;
|
|
|
|
bdb = nullptr;
|
2016-08-09 19:16:32 +02:00
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
*blob_db = bdb;
|
2017-08-01 21:48:22 +02:00
|
|
|
bdb_options.Dump(db_options.info_log.get());
|
2016-08-09 19:16:32 +02:00
|
|
|
return s;
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
|
|
|
|
BlobDB::BlobDB(DB* db) : StackableDB(db) {}
|
|
|
|
|
2017-08-01 21:48:22 +02:00
|
|
|
void BlobDBOptions::Dump(Logger* log) const {
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.blob_dir: %s",
|
2017-08-01 21:48:22 +02:00
|
|
|
blob_dir.c_str());
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.path_relative: %d",
|
2017-08-01 21:48:22 +02:00
|
|
|
path_relative);
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.is_fifo: %d",
|
2017-08-01 21:48:22 +02:00
|
|
|
is_fifo);
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.blob_dir_size: %" PRIu64,
|
2017-08-01 21:48:22 +02:00
|
|
|
blob_dir_size);
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.ttl_range_secs: %" PRIu32,
|
2017-08-01 21:48:22 +02:00
|
|
|
ttl_range_secs);
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.bytes_per_sync: %" PRIu64,
|
2017-08-01 21:48:22 +02:00
|
|
|
bytes_per_sync);
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.blob_file_size: %" PRIu64,
|
2017-08-01 21:48:22 +02:00
|
|
|
blob_file_size);
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.ttl_extractor: %p",
|
2017-08-01 21:48:22 +02:00
|
|
|
ttl_extractor.get());
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.compression: %d",
|
2017-08-01 21:48:22 +02:00
|
|
|
static_cast<int>(compression));
|
2017-11-02 23:47:30 +01:00
|
|
|
ROCKS_LOG_HEADER(log, "blob_db_options.enable_garbage_collection: %d",
|
|
|
|
enable_garbage_collection);
|
|
|
|
ROCKS_LOG_HEADER(log, " blob_db_options.disable_background_tasks: %d",
|
2017-08-01 21:48:22 +02:00
|
|
|
disable_background_tasks);
|
|
|
|
}
|
2017-05-10 23:54:35 +02:00
|
|
|
|
|
|
|
} // namespace blob_db
|
2016-08-09 19:16:32 +02:00
|
|
|
} // namespace rocksdb
|
2017-05-10 23:54:35 +02:00
|
|
|
#endif
|