20#include <xrpl/basics/rocksdb.h>
22#if XRPL_ROCKSDB_AVAILABLE
23#include <xrpl/basics/ByteUtilities.h>
24#include <xrpl/basics/contract.h>
25#include <xrpl/basics/safe_cast.h>
26#include <xrpl/beast/core/CurrentThreadName.h>
27#include <xrpl/nodestore/Factory.h>
28#include <xrpl/nodestore/Manager.h>
29#include <xrpl/nodestore/detail/BatchWriter.h>
30#include <xrpl/nodestore/detail/DecodedBlob.h>
31#include <xrpl/nodestore/detail/EncodedBlob.h>
39class RocksDBEnv :
public rocksdb::EnvWrapper
42 RocksDBEnv() : EnvWrapper(rocksdb::Env::Default())
48 ThreadParams(
void (*f_)(
void*),
void* a_) : f(f_), a(a_)
57 thread_entry(
void* ptr)
59 ThreadParams*
const p(
reinterpret_cast<ThreadParams*
>(ptr));
60 void (*f)(
void*) = p->f;
67 ss <<
"rocksdb #" << id;
74 StartThread(
void (*f)(
void*),
void* a)
override
76 ThreadParams*
const p(
new ThreadParams(f, a));
77 EnvWrapper::StartThread(&RocksDBEnv::thread_entry, p);
83class RocksDBBackend :
public Backend,
public BatchWriter::Callback
90 size_t const m_keyBytes;
94 int fdRequired_ = 2048;
95 rocksdb::Options m_options;
99 Section
const& keyValues,
100 Scheduler& scheduler,
103 : m_deletePath(false)
105 , m_keyBytes(keyBytes)
106 , m_batch(*this, scheduler)
109 Throw<std::runtime_error>(
"Missing path in RocksDBFactory backend");
111 rocksdb::BlockBasedTableOptions table_options;
115 keyValues.exists(
"hard_set") && get<bool>(keyValues,
"hard_set");
117 if (keyValues.exists(
"cache_mb"))
119 auto size = get<int>(keyValues,
"cache_mb");
121 if (!hard_set && size == 256)
124 table_options.block_cache = rocksdb::NewLRUCache(
megabytes(size));
127 if (
auto const v = get<int>(keyValues,
"filter_bits"))
129 bool const filter_blocks = !keyValues.exists(
"filter_full") ||
130 (get<int>(keyValues,
"filter_full") == 0);
131 table_options.filter_policy.reset(
132 rocksdb::NewBloomFilterPolicy(v, filter_blocks));
135 if (
get_if_exists(keyValues,
"open_files", m_options.max_open_files))
137 if (!hard_set && m_options.max_open_files == 2000)
138 m_options.max_open_files = 8000;
140 fdRequired_ = m_options.max_open_files + 128;
143 if (keyValues.exists(
"file_size_mb"))
145 auto file_size_mb = get<int>(keyValues,
"file_size_mb");
147 if (!hard_set && file_size_mb == 8)
150 m_options.target_file_size_base =
megabytes(file_size_mb);
151 m_options.max_bytes_for_level_base =
152 5 * m_options.target_file_size_base;
153 m_options.write_buffer_size = 2 * m_options.target_file_size_base;
157 keyValues,
"file_size_mult", m_options.target_file_size_multiplier);
159 if (keyValues.exists(
"bg_threads"))
161 m_options.env->SetBackgroundThreads(
162 get<int>(keyValues,
"bg_threads"), rocksdb::Env::LOW);
165 if (keyValues.exists(
"high_threads"))
167 auto const highThreads = get<int>(keyValues,
"high_threads");
168 m_options.env->SetBackgroundThreads(
169 highThreads, rocksdb::Env::HIGH);
174 m_options.max_background_flushes = highThreads;
177 m_options.compression = rocksdb::kSnappyCompression;
179 get_if_exists(keyValues,
"block_size", table_options.block_size);
181 if (keyValues.exists(
"universal_compaction") &&
182 (get<int>(keyValues,
"universal_compaction") != 0))
184 m_options.compaction_style = rocksdb::kCompactionStyleUniversal;
185 m_options.min_write_buffer_number_to_merge = 2;
186 m_options.max_write_buffer_number = 6;
187 m_options.write_buffer_size = 6 * m_options.target_file_size_base;
190 if (keyValues.exists(
"bbt_options"))
192 rocksdb::ConfigOptions config_options;
193 auto const s = rocksdb::GetBlockBasedTableOptionsFromString(
196 get(keyValues,
"bbt_options"),
199 Throw<std::runtime_error>(
200 std::string(
"Unable to set RocksDB bbt_options: ") +
204 m_options.table_factory.reset(NewBlockBasedTableFactory(table_options));
206 if (keyValues.exists(
"options"))
208 auto const s = rocksdb::GetOptionsFromString(
209 m_options,
get(keyValues,
"options"), &m_options);
211 Throw<std::runtime_error>(
217 rocksdb::GetStringFromDBOptions(&s1, m_options,
"; ");
218 rocksdb::GetStringFromColumnFamilyOptions(&s2, m_options,
"; ");
219 JLOG(m_journal.
debug()) <<
"RocksDB DBOptions: " << s1;
220 JLOG(m_journal.
debug()) <<
"RocksDB CFOptions: " << s2;
223 ~RocksDBBackend()
override
229 open(
bool createIfMissing)
override
235 "ripple::NodeStore::RocksDBBackend::open : database is already "
237 JLOG(m_journal.
error()) <<
"database is already open";
241 rocksdb::DB* db =
nullptr;
242 m_options.create_if_missing = createIfMissing;
243 rocksdb::Status
status = rocksdb::DB::Open(m_options, m_name, &db);
245 Throw<std::runtime_error>(
254 return static_cast<bool>(m_db);
265 boost::filesystem::path dir = m_name;
266 boost::filesystem::remove_all(dir);
284 "ripple::NodeStore::RocksDBBackend::fetch : non-null database");
289 rocksdb::ReadOptions
const options;
290 rocksdb::Slice
const slice(
static_cast<char const*
>(key), m_keyBytes);
294 rocksdb::Status getStatus = m_db->Get(options, slice, &
string);
298 DecodedBlob decoded(key,
string.
data(),
string.
size());
302 *pObject = decoded.createObject();
313 if (getStatus.IsCorruption())
317 else if (getStatus.IsNotFound())
324 Status(customCode + unsafe_cast<int>(getStatus.code()));
326 JLOG(m_journal.
error()) << getStatus.ToString();
338 for (
auto const& h : hashes)
348 return {results,
ok};
354 m_batch.store(
object);
358 storeBatch(Batch
const& batch)
override
362 "ripple::NodeStore::RocksDBBackend::storeBatch : non-null "
364 rocksdb::WriteBatch wb;
366 for (
auto const& e :
batch)
368 EncodedBlob encoded(e);
372 reinterpret_cast<char const*
>(encoded.getKey()),
375 reinterpret_cast<char const*
>(encoded.getData()),
379 rocksdb::WriteOptions
const options;
381 auto ret = m_db->Write(options, &wb);
384 Throw<std::runtime_error>(
"storeBatch failed: " + ret.ToString());
397 "ripple::NodeStore::RocksDBBackend::for_each : non-null database");
398 rocksdb::ReadOptions
const options;
402 for (it->SeekToFirst(); it->Valid(); it->Next())
404 if (it->key().size() == m_keyBytes)
407 it->key().data(), it->value().data(), it->value().size());
411 f(decoded.createObject());
416 JLOG(m_journal.
fatal())
417 <<
"Corrupt NodeObject #" << it->key().ToString(
true);
424 JLOG(m_journal.
fatal())
425 <<
"Bad key size = " << it->key().size();
431 getWriteLoad()
override
433 return m_batch.getWriteLoad();
437 setDeletePath()
override
445 writeBatch(Batch
const& batch)
override
452 fdRequired()
const override
460class RocksDBFactory :
public Factory
468 RocksDBFactory(Manager& manager) : manager_(manager)
470 manager_.insert(*
this);
474 getName()
const override
482 Section
const& keyValues,
484 Scheduler& scheduler,
488 keyBytes, keyValues, scheduler, journal, &m_env);
495 static RocksDBFactory instance{manager};
A generic endpoint for log messages.
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
void registerRocksDBFactory(Manager &manager)
Status
Return codes from Backend operations.
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.
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.
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.