20#include <xrpld/unity/rocksdb.h>
22#if RIPPLE_ROCKSDB_AVAILABLE
23#include <xrpld/core/Config.h>
24#include <xrpld/nodestore/Factory.h>
25#include <xrpld/nodestore/Manager.h>
26#include <xrpld/nodestore/detail/BatchWriter.h>
27#include <xrpld/nodestore/detail/DecodedBlob.h>
28#include <xrpld/nodestore/detail/EncodedBlob.h>
30#include <xrpl/basics/ByteUtilities.h>
31#include <xrpl/basics/contract.h>
32#include <xrpl/basics/safe_cast.h>
33#include <xrpl/beast/core/CurrentThreadName.h>
41class 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);
85class 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 rocksdb::ConfigOptions config_options;
195 auto const s = rocksdb::GetBlockBasedTableOptionsFromString(
198 get(keyValues,
"bbt_options"),
201 Throw<std::runtime_error>(
202 std::string(
"Unable to set RocksDB bbt_options: ") +
206 m_options.table_factory.reset(NewBlockBasedTableFactory(table_options));
208 if (keyValues.exists(
"options"))
210 auto const s = rocksdb::GetOptionsFromString(
211 m_options,
get(keyValues,
"options"), &m_options);
213 Throw<std::runtime_error>(
219 rocksdb::GetStringFromDBOptions(&s1, m_options,
"; ");
220 rocksdb::GetStringFromColumnFamilyOptions(&s2, m_options,
"; ");
221 JLOG(m_journal.
debug()) <<
"RocksDB DBOptions: " << s1;
222 JLOG(m_journal.
debug()) <<
"RocksDB CFOptions: " << s2;
225 ~RocksDBBackend()
override
231 open(
bool createIfMissing)
override
237 "ripple::NodeStore::RocksDBBackend::open : database is already "
239 JLOG(m_journal.
error()) <<
"database is already open";
243 rocksdb::DB* db =
nullptr;
244 m_options.create_if_missing = createIfMissing;
245 rocksdb::Status
status = rocksdb::DB::Open(m_options, m_name, &db);
247 Throw<std::runtime_error>(
256 return static_cast<bool>(m_db);
267 boost::filesystem::path dir = m_name;
268 boost::filesystem::remove_all(dir);
286 "ripple::NodeStore::RocksDBBackend::fetch : non-null database");
291 rocksdb::ReadOptions
const options;
292 rocksdb::Slice
const slice(
static_cast<char const*
>(key), m_keyBytes);
296 rocksdb::Status getStatus = m_db->Get(options, slice, &
string);
300 DecodedBlob decoded(key,
string.
data(),
string.
size());
304 *pObject = decoded.createObject();
315 if (getStatus.IsCorruption())
319 else if (getStatus.IsNotFound())
326 Status(customCode + unsafe_cast<int>(getStatus.code()));
328 JLOG(m_journal.
error()) << getStatus.ToString();
340 for (
auto const& h : hashes)
350 return {results,
ok};
356 m_batch.store(
object);
360 storeBatch(Batch
const& batch)
override
364 "ripple::NodeStore::RocksDBBackend::storeBatch : non-null "
366 rocksdb::WriteBatch wb;
368 for (
auto const& e :
batch)
370 EncodedBlob encoded(e);
374 reinterpret_cast<char const*
>(encoded.getKey()),
377 reinterpret_cast<char const*
>(encoded.getData()),
381 rocksdb::WriteOptions
const options;
383 auto ret = m_db->Write(options, &wb);
386 Throw<std::runtime_error>(
"storeBatch failed: " + ret.ToString());
399 "ripple::NodeStore::RocksDBBackend::for_each : non-null database");
400 rocksdb::ReadOptions
const options;
404 for (it->SeekToFirst(); it->Valid(); it->Next())
406 if (it->key().size() == m_keyBytes)
409 it->key().data(), it->value().data(), it->value().size());
413 f(decoded.createObject());
418 JLOG(m_journal.
fatal())
419 <<
"Corrupt NodeObject #" << it->key().ToString(
true);
426 JLOG(m_journal.
fatal())
427 <<
"Bad key size = " << it->key().size();
433 getWriteLoad()
override
435 return m_batch.getWriteLoad();
439 setDeletePath()
override
447 writeBatch(Batch
const& batch)
override
454 fdRequired()
const override
462class RocksDBFactory :
public Factory
469 Manager::instance().insert(*
this);
472 ~RocksDBFactory()
override
474 Manager::instance().erase(*
this);
478 getName()
const override
486 Section
const& keyValues,
488 Scheduler& scheduler,
492 keyBytes, keyValues, scheduler, journal, &m_env);
496static RocksDBFactory rocksDBFactory;
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.
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.