1#include <xrpl/basics/rocksdb.h>
3#if XRPL_ROCKSDB_AVAILABLE
4#include <xrpl/basics/ByteUtilities.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/basics/safe_cast.h>
7#include <xrpl/beast/core/CurrentThreadName.h>
8#include <xrpl/nodestore/Factory.h>
9#include <xrpl/nodestore/Manager.h>
10#include <xrpl/nodestore/detail/BatchWriter.h>
11#include <xrpl/nodestore/detail/DecodedBlob.h>
12#include <xrpl/nodestore/detail/EncodedBlob.h>
20class RocksDBEnv :
public rocksdb::EnvWrapper
23 RocksDBEnv() : EnvWrapper(rocksdb::Env::Default())
29 ThreadParams(
void (*f_)(
void*),
void* a_) : f(f_), a(a_)
38 thread_entry(
void* ptr)
40 ThreadParams*
const p(
reinterpret_cast<ThreadParams*
>(ptr));
41 void (*f)(
void*) = p->f;
48 ss <<
"rocksdb #" << id;
55 StartThread(
void (*f)(
void*),
void* a)
override
57 ThreadParams*
const p(
new ThreadParams(f, a));
58 EnvWrapper::StartThread(&RocksDBEnv::thread_entry, p);
64class RocksDBBackend :
public Backend,
public BatchWriter::Callback
71 size_t const m_keyBytes;
75 int fdRequired_ = 2048;
76 rocksdb::Options m_options;
80 Section
const& keyValues,
84 : m_deletePath(false), m_journal(journal), m_keyBytes(keyBytes), m_batch(*this, scheduler)
87 Throw<std::runtime_error>(
"Missing path in RocksDBFactory backend");
89 rocksdb::BlockBasedTableOptions table_options;
92 bool hard_set = keyValues.exists(
"hard_set") && get<bool>(keyValues,
"hard_set");
94 if (keyValues.exists(
"cache_mb"))
96 auto size = get<int>(keyValues,
"cache_mb");
98 if (!hard_set && size == 256)
101 table_options.block_cache = rocksdb::NewLRUCache(
megabytes(size));
104 if (
auto const v = get<int>(keyValues,
"filter_bits"))
106 bool const filter_blocks = !keyValues.exists(
"filter_full") || (get<int>(keyValues,
"filter_full") == 0);
107 table_options.filter_policy.reset(rocksdb::NewBloomFilterPolicy(v, filter_blocks));
110 if (
get_if_exists(keyValues,
"open_files", m_options.max_open_files))
112 if (!hard_set && m_options.max_open_files == 2000)
113 m_options.max_open_files = 8000;
115 fdRequired_ = m_options.max_open_files + 128;
118 if (keyValues.exists(
"file_size_mb"))
120 auto file_size_mb = get<int>(keyValues,
"file_size_mb");
122 if (!hard_set && file_size_mb == 8)
125 m_options.target_file_size_base =
megabytes(file_size_mb);
126 m_options.max_bytes_for_level_base = 5 * m_options.target_file_size_base;
127 m_options.write_buffer_size = 2 * m_options.target_file_size_base;
130 get_if_exists(keyValues,
"file_size_mult", m_options.target_file_size_multiplier);
132 if (keyValues.exists(
"bg_threads"))
134 m_options.env->SetBackgroundThreads(get<int>(keyValues,
"bg_threads"), rocksdb::Env::LOW);
137 if (keyValues.exists(
"high_threads"))
139 auto const highThreads = get<int>(keyValues,
"high_threads");
140 m_options.env->SetBackgroundThreads(highThreads, rocksdb::Env::HIGH);
145 m_options.max_background_flushes = highThreads;
148 m_options.compression = rocksdb::kSnappyCompression;
150 get_if_exists(keyValues,
"block_size", table_options.block_size);
152 if (keyValues.exists(
"universal_compaction") && (get<int>(keyValues,
"universal_compaction") != 0))
154 m_options.compaction_style = rocksdb::kCompactionStyleUniversal;
155 m_options.min_write_buffer_number_to_merge = 2;
156 m_options.max_write_buffer_number = 6;
157 m_options.write_buffer_size = 6 * m_options.target_file_size_base;
160 if (keyValues.exists(
"bbt_options"))
162 rocksdb::ConfigOptions config_options;
163 auto const s = rocksdb::GetBlockBasedTableOptionsFromString(
164 config_options, table_options,
get(keyValues,
"bbt_options"), &table_options);
166 Throw<std::runtime_error>(
std::string(
"Unable to set RocksDB bbt_options: ") + s.ToString());
169 m_options.table_factory.reset(NewBlockBasedTableFactory(table_options));
171 if (keyValues.exists(
"options"))
173 auto const s = rocksdb::GetOptionsFromString(m_options,
get(keyValues,
"options"), &m_options);
175 Throw<std::runtime_error>(
std::string(
"Unable to set RocksDB options: ") + s.ToString());
179 rocksdb::GetStringFromDBOptions(&s1, m_options,
"; ");
180 rocksdb::GetStringFromColumnFamilyOptions(&s2, m_options,
"; ");
181 JLOG(m_journal.
debug()) <<
"RocksDB DBOptions: " << s1;
182 JLOG(m_journal.
debug()) <<
"RocksDB CFOptions: " << s2;
185 ~RocksDBBackend()
override
191 open(
bool createIfMissing)
override
197 "xrpl::NodeStore::RocksDBBackend::open : database is already "
199 JLOG(m_journal.
error()) <<
"database is already open";
203 rocksdb::DB* db =
nullptr;
204 m_options.create_if_missing = createIfMissing;
205 rocksdb::Status
status = rocksdb::DB::Open(m_options, m_name, &db);
207 Throw<std::runtime_error>(
std::string(
"Unable to open/create RocksDB: ") +
status.ToString());
214 return static_cast<bool>(m_db);
225 boost::filesystem::path dir = m_name;
226 boost::filesystem::remove_all(dir);
242 XRPL_ASSERT(m_db,
"xrpl::NodeStore::RocksDBBackend::fetch : non-null database");
247 rocksdb::ReadOptions
const options;
248 rocksdb::Slice
const slice(
static_cast<char const*
>(key), m_keyBytes);
252 rocksdb::Status getStatus = m_db->Get(options, slice, &
string);
256 DecodedBlob decoded(key,
string.
data(),
string.
size());
260 *pObject = decoded.createObject();
271 if (getStatus.IsCorruption())
275 else if (getStatus.IsNotFound())
281 status =
Status(customCode + unsafe_cast<int>(getStatus.code()));
283 JLOG(m_journal.
error()) << getStatus.ToString();
295 for (
auto const& h : hashes)
305 return {results,
ok};
311 m_batch.store(
object);
315 storeBatch(Batch
const& batch)
override
319 "xrpl::NodeStore::RocksDBBackend::storeBatch : non-null "
321 rocksdb::WriteBatch wb;
323 for (
auto const& e :
batch)
325 EncodedBlob encoded(e);
328 rocksdb::Slice(
reinterpret_cast<char const*
>(encoded.getKey()), m_keyBytes),
329 rocksdb::Slice(
reinterpret_cast<char const*
>(encoded.getData()), encoded.getSize()));
332 rocksdb::WriteOptions
const options;
334 auto ret = m_db->Write(options, &wb);
337 Throw<std::runtime_error>(
"storeBatch failed: " + ret.ToString());
348 XRPL_ASSERT(m_db,
"xrpl::NodeStore::RocksDBBackend::for_each : non-null database");
349 rocksdb::ReadOptions
const options;
353 for (it->SeekToFirst(); it->Valid(); it->Next())
355 if (it->key().size() == m_keyBytes)
357 DecodedBlob decoded(it->key().data(), it->value().data(), it->value().size());
361 f(decoded.createObject());
366 JLOG(m_journal.
fatal()) <<
"Corrupt NodeObject #" << it->key().ToString(
true);
373 JLOG(m_journal.
fatal()) <<
"Bad key size = " << it->key().size();
379 getWriteLoad()
override
381 return m_batch.getWriteLoad();
385 setDeletePath()
override
393 writeBatch(Batch
const& batch)
override
400 fdRequired()
const override
408class RocksDBFactory :
public Factory
416 RocksDBFactory(Manager& manager) : manager_(manager)
418 manager_.insert(*
this);
422 getName()
const override
438 static RocksDBFactory instance{manager};
A generic endpoint for log messages.
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
Status
Return codes from Backend operations.
void registerRocksDBFactory(Manager &manager)
auto const data
General field definitions, or fields used in multiple transaction namespaces.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
constexpr auto megabytes(T value) noexcept
bool get_if_exists(Section const §ion, std::string const &name, T &v)
void open(soci::session &s, BasicConfig const &config, std::string const &dbName)
Open a soci session.