20 #include <ripple/unity/rocksdb.h>
22 #if RIPPLE_ROCKSDB_AVAILABLE
24 #include <ripple/basics/ByteUtilities.h>
25 #include <ripple/basics/contract.h>
26 #include <ripple/basics/safe_cast.h>
27 #include <ripple/beast/core/CurrentThreadName.h>
28 #include <ripple/core/Config.h>
29 #include <ripple/nodestore/Factory.h>
30 #include <ripple/nodestore/Manager.h>
31 #include <ripple/nodestore/impl/BatchWriter.h>
32 #include <ripple/nodestore/impl/DecodedBlob.h>
33 #include <ripple/nodestore/impl/EncodedBlob.h>
41 class RocksDBEnv :
public rocksdb::EnvWrapper
44 RocksDBEnv() : EnvWrapper(rocksdb::Env::Default())
50 ThreadParams(
void (*f_)(
void*),
void* a_) : f(f_), a(a_)
59 thread_entry(
void* ptr)
61 ThreadParams*
const p(
reinterpret_cast<ThreadParams*
>(ptr));
62 void (*f)(
void*) = p->f;
69 ss <<
"rocksdb #" << id;
76 StartThread(
void (*f)(
void*),
void* a)
override
78 ThreadParams*
const p(
new ThreadParams(f, a));
79 EnvWrapper::StartThread(&RocksDBEnv::thread_entry, p);
85 class RocksDBBackend :
public Backend,
public BatchWriter::Callback
92 size_t const m_keyBytes;
96 int fdRequired_ = 2048;
97 rocksdb::Options m_options;
101 Section
const& keyValues,
102 Scheduler& scheduler,
105 : m_deletePath(false)
107 , m_keyBytes(keyBytes)
108 , m_batch(*this, scheduler)
111 Throw<std::runtime_error>(
"Missing path in RocksDBFactory backend");
113 rocksdb::BlockBasedTableOptions table_options;
117 keyValues.exists(
"hard_set") && get<bool>(keyValues,
"hard_set");
119 if (keyValues.exists(
"cache_mb"))
121 auto size = get<int>(keyValues,
"cache_mb");
123 if (!hard_set && size == 256)
126 table_options.block_cache = rocksdb::NewLRUCache(
megabytes(size));
129 if (
auto const v = get<int>(keyValues,
"filter_bits"))
131 bool const filter_blocks = !keyValues.exists(
"filter_full") ||
132 (get<int>(keyValues,
"filter_full") == 0);
133 table_options.filter_policy.reset(
134 rocksdb::NewBloomFilterPolicy(v, filter_blocks));
137 if (
get_if_exists(keyValues,
"open_files", m_options.max_open_files))
139 if (!hard_set && m_options.max_open_files == 2000)
140 m_options.max_open_files = 8000;
142 fdRequired_ = m_options.max_open_files + 128;
145 if (keyValues.exists(
"file_size_mb"))
147 auto file_size_mb = get<int>(keyValues,
"file_size_mb");
149 if (!hard_set && file_size_mb == 8)
152 m_options.target_file_size_base =
megabytes(file_size_mb);
153 m_options.max_bytes_for_level_base =
154 5 * m_options.target_file_size_base;
155 m_options.write_buffer_size = 2 * m_options.target_file_size_base;
159 keyValues,
"file_size_mult", m_options.target_file_size_multiplier);
161 if (keyValues.exists(
"bg_threads"))
163 m_options.env->SetBackgroundThreads(
164 get<int>(keyValues,
"bg_threads"), rocksdb::Env::LOW);
167 if (keyValues.exists(
"high_threads"))
169 auto const highThreads = get<int>(keyValues,
"high_threads");
170 m_options.env->SetBackgroundThreads(
171 highThreads, rocksdb::Env::HIGH);
176 m_options.max_background_flushes = highThreads;
179 m_options.compression = rocksdb::kSnappyCompression;
181 get_if_exists(keyValues,
"block_size", table_options.block_size);
183 if (keyValues.exists(
"universal_compaction") &&
184 (get<int>(keyValues,
"universal_compaction") != 0))
186 m_options.compaction_style = rocksdb::kCompactionStyleUniversal;
187 m_options.min_write_buffer_number_to_merge = 2;
188 m_options.max_write_buffer_number = 6;
189 m_options.write_buffer_size = 6 * m_options.target_file_size_base;
192 if (keyValues.exists(
"bbt_options"))
194 auto const s = rocksdb::GetBlockBasedTableOptionsFromString(
195 table_options,
get(keyValues,
"bbt_options"), &table_options);
197 Throw<std::runtime_error>(
198 std::string(
"Unable to set RocksDB bbt_options: ") +
202 m_options.table_factory.reset(NewBlockBasedTableFactory(table_options));
204 if (keyValues.exists(
"options"))
206 auto const s = rocksdb::GetOptionsFromString(
207 m_options,
get(keyValues,
"options"), &m_options);
209 Throw<std::runtime_error>(
215 rocksdb::GetStringFromDBOptions(&s1, m_options,
"; ");
216 rocksdb::GetStringFromColumnFamilyOptions(&s2, m_options,
"; ");
217 JLOG(m_journal.
debug()) <<
"RocksDB DBOptions: " << s1;
218 JLOG(m_journal.
debug()) <<
"RocksDB CFOptions: " << s2;
221 ~RocksDBBackend()
override
227 open(
bool createIfMissing)
override
232 JLOG(m_journal.
error()) <<
"database is already open";
235 rocksdb::DB* db =
nullptr;
236 m_options.create_if_missing = createIfMissing;
237 rocksdb::Status
status = rocksdb::DB::Open(m_options, m_name, &db);
239 Throw<std::runtime_error>(
248 return static_cast<bool>(m_db);
259 boost::filesystem::path dir = m_name;
260 boost::filesystem::remove_all(dir);
281 rocksdb::ReadOptions
const options;
282 rocksdb::Slice
const slice(
static_cast<char const*
>(key), m_keyBytes);
286 rocksdb::Status getStatus = m_db->Get(options, slice, &
string);
290 DecodedBlob decoded(key,
string.
data(),
string.
size());
294 *pObject = decoded.createObject();
305 if (getStatus.IsCorruption())
309 else if (getStatus.IsNotFound())
318 JLOG(m_journal.
error()) << getStatus.ToString();
330 for (
auto const& h : hashes)
340 return {results,
ok};
346 m_batch.store(
object);
350 storeBatch(
Batch const& batch)
override
353 rocksdb::WriteBatch wb;
355 for (
auto const& e : batch)
357 EncodedBlob encoded(e);
361 reinterpret_cast<char const*
>(encoded.getKey()),
364 reinterpret_cast<char const*
>(encoded.getData()),
368 rocksdb::WriteOptions
const options;
370 auto ret = m_db->Write(options, &wb);
373 Throw<std::runtime_error>(
"storeBatch failed: " + ret.ToString());
385 rocksdb::ReadOptions
const options;
389 for (it->SeekToFirst(); it->Valid(); it->Next())
391 if (it->key().size() == m_keyBytes)
394 it->key().data(), it->value().data(), it->value().size());
398 f(decoded.createObject());
403 JLOG(m_journal.
fatal())
404 <<
"Corrupt NodeObject #" << it->key().ToString(
true);
411 JLOG(m_journal.
fatal())
412 <<
"Bad key size = " << it->key().size();
418 getWriteLoad()
override
420 return m_batch.getWriteLoad();
424 setDeletePath()
override
432 writeBatch(
Batch const& batch)
override
439 fdRequired()
const override
447 class RocksDBFactory :
public Factory
457 ~RocksDBFactory()
override
463 getName()
const override
471 Section
const& keyValues,
473 Scheduler& scheduler,
476 return std::make_unique<RocksDBBackend>(
477 keyBytes, keyValues, scheduler, journal, &m_env);
481 static RocksDBFactory rocksDBFactory;