Skip to content

Commit

Permalink
Make earliest ledger sequence configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelportilla authored and nbougalis committed Mar 24, 2018
1 parent 8d9dffc commit 0b18b36
Show file tree
Hide file tree
Showing 22 changed files with 301 additions and 157 deletions.
4 changes: 4 additions & 0 deletions cfg/rippled-example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,10 @@
# require administrative RPC call "can_delete"
# to enable online deletion of ledger records.
#
# earliest_seq The default is 32570 to match the XRP ledger
# network's earliest allowed sequence. Alternate
# networks may set this value. Minimum value of 1.
#
# Notes:
# The 'node_db' entry configures the primary, persistent storage.
#
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/app/ledger/impl/InboundLedger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ InboundLedger::init(ScopedLockType& collectionLock)
if (mFailed)
return;
}
else if (shardStore && mSeq >= NodeStore::genesisSeq)
else if (shardStore && mSeq >= shardStore->earliestSeq())
{
if (auto l = shardStore->fetchLedger(mHash, mSeq))
{
Expand Down
15 changes: 8 additions & 7 deletions src/ripple/app/ledger/impl/LedgerMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,8 +1542,8 @@ LedgerMaster::fetchForHistory(
ledger = app_.getInboundLedgers().acquire(
*hash, missing, reason);
if (!ledger &&
missing > NodeStore::genesisSeq &&
missing != fetch_seq_)
missing != fetch_seq_ &&
missing > app_.getNodeStore().earliestSeq())
{
JLOG(m_journal.trace())
<< "fetchForHistory want fetch pack " << missing;
Expand Down Expand Up @@ -1603,12 +1603,12 @@ LedgerMaster::fetchForHistory(
if (reason == InboundLedger::Reason::SHARD)
// Do not fetch ledger sequences lower
// than the shard's first ledger sequence
fetchSz = NodeStore::DatabaseShard::firstSeq(
NodeStore::DatabaseShard::seqToShardIndex(missing));
fetchSz = app_.getShardStore()->firstLedgerSeq(
app_.getShardStore()->seqToShardIndex(missing));
else
// Do not fetch ledger sequences lower
// than the genesis ledger sequence
fetchSz = NodeStore::genesisSeq;
// than the earliest ledger sequence
fetchSz = app_.getNodeStore().earliestSeq();
fetchSz = missing >= fetchSz ?
std::min(ledger_fetch_size_, (missing - fetchSz) + 1) : 0;
try
Expand Down Expand Up @@ -1666,7 +1666,8 @@ void LedgerMaster::doAdvance (ScopedLockType& sl)
{
ScopedLockType sl(mCompleteLock);
missing = prevMissing(mCompleteLedgers,
mPubLedger->info().seq, NodeStore::genesisSeq);
mPubLedger->info().seq,
app_.getNodeStore().earliestSeq());
}
if (missing)
{
Expand Down
19 changes: 15 additions & 4 deletions src/ripple/app/misc/SHAMapStoreImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ SHAMapStoreImp::SHAMapStoreImp (
}
if (! setup_.shardDatabase.empty())
{
// The node and shard stores must use
// the same earliest ledger sequence
std::array<std::uint32_t, 2> seq;
if (get_if_exists<std::uint32_t>(
setup_.nodeDatabase, "earliest_seq", seq[0]))
{
if (get_if_exists<std::uint32_t>(
setup_.shardDatabase, "earliest_seq", seq[1]) &&
seq[0] != seq[1])
{
Throw<std::runtime_error>("earliest_seq set more than once");
}
}

boost::filesystem::path dbPath =
get<std::string>(setup_.shardDatabase, "path");
if (dbPath.empty())
Expand Down Expand Up @@ -231,9 +245,6 @@ SHAMapStoreImp::SHAMapStoreImp (
if (! setup_.standalone)
Throw<std::runtime_error>(
"ledgers_per_shard only honored in stand alone");
if (lps == 0 || lps % 256 != 0)
Throw<std::runtime_error>(
"ledgers_per_shard must be a multiple of 256");
}
}
}
Expand All @@ -259,7 +270,7 @@ SHAMapStoreImp::makeDatabase (std::string const& name,
auto dbr = std::make_unique<NodeStore::DatabaseRotatingImp>(
"NodeStore.main", scheduler_, readThreads, parent,
std::move(writableBackend), std::move(archiveBackend),
nodeStoreJournal_);
setup_.nodeDatabase, nodeStoreJournal_);
fdlimit_ += dbr->fdlimit();
dbRotating_ = dbr.get();
db.reset(dynamic_cast<NodeStore::Database*>(dbr.release()));
Expand Down
15 changes: 14 additions & 1 deletion src/ripple/nodestore/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ripple/nodestore/impl/Tuning.h>
#include <ripple/nodestore/Scheduler.h>
#include <ripple/nodestore/NodeObject.h>
#include <ripple/protocol/SystemParameters.h>

#include <thread>

Expand Down Expand Up @@ -63,7 +64,7 @@ class Database : public Stoppable
@param journal Destination for logging output.
*/
Database(std::string name, Stoppable& parent, Scheduler& scheduler,
int readThreads, beast::Journal j);
int readThreads, Section const& config, beast::Journal j);

/** Destroy the node store.
All pending operations are completed, pending writes flushed,
Expand Down Expand Up @@ -209,6 +210,14 @@ class Database : public Stoppable
void
onStop();

/** @return The earliest ledger sequence allowed
*/
std::uint32_t
earliestSeq() const
{
return earliestSeq_;
}

protected:
beast::Journal j_;
Scheduler& scheduler_;
Expand Down Expand Up @@ -265,6 +274,10 @@ class Database : public Stoppable
// current read generation
uint64_t readGen_ {0};

// The default is 32570 to match the XRP ledger network's earliest
// allowed sequence. Alternate networks may set this value.
std::uint32_t earliestSeq_ {XRP_LEDGER_EARLIEST_SEQ};

virtual
std::shared_ptr<NodeObject>
fetchFrom(uint256 const& hash, std::uint32_t seq) = 0;
Expand Down
11 changes: 8 additions & 3 deletions src/ripple/nodestore/DatabaseRotating.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ namespace NodeStore {
class DatabaseRotating : public Database
{
public:
DatabaseRotating(std::string const& name, Stoppable& parent,
Scheduler& scheduler, int readThreads, beast::Journal journal)
: Database(name, parent, scheduler, readThreads, journal)
DatabaseRotating(
std::string const& name,
Stoppable& parent,
Scheduler& scheduler,
int readThreads,
Section const& config,
beast::Journal journal)
: Database(name, parent, scheduler, readThreads, config, journal)
{}

virtual
Expand Down
55 changes: 25 additions & 30 deletions src/ripple/nodestore/DatabaseShard.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ class DatabaseShard : public Database
@param config The configuration for the database
@param journal Destination for logging output
*/
DatabaseShard(std::string const& name, Stoppable& parent,
Scheduler& scheduler, int readThreads,
Section const& config, beast::Journal journal)
: Database(name, parent, scheduler, readThreads, journal)
DatabaseShard(
std::string const& name,
Stoppable& parent,
Scheduler& scheduler,
int readThreads,
Section const& config,
beast::Journal journal)
: Database(name, parent, scheduler, readThreads, config, journal)
{
get_if_exists<std::uint32_t>(config, "ledgers_per_shard", lps_);
}

/** Initialize the database
Expand Down Expand Up @@ -120,55 +123,47 @@ class DatabaseShard : public Database
void
validate() = 0;

/** @return The number of ledgers stored in a shard
/** @return The maximum number of ledgers stored in a shard
*/
static
virtual
std::uint32_t
ledgersPerShard()
{
return lps_;
}
ledgersPerShard() const = 0;

/** @return The earliest shard index
*/
virtual
std::uint32_t
earliestShardIndex() const = 0;

/** Calculates the shard index for a given ledger sequence
@param seq ledger sequence
@return The shard index of the ledger sequence
*/
static
virtual
std::uint32_t
seqToShardIndex(std::uint32_t seq)
{
assert(seq >= genesisSeq);
return (seq - 1) / lps_;
}
seqToShardIndex(std::uint32_t seq) const = 0;

/** Calculates the first ledger sequence for a given shard index
@param shardIndex The shard index considered
@return The first ledger sequence pertaining to the shard index
*/
static
virtual
std::uint32_t
firstSeq(std::uint32_t shardIndex)
{
return 1 + (shardIndex * lps_);
}
firstLedgerSeq(std::uint32_t shardIndex) const = 0;

/** Calculates the last ledger sequence for a given shard index
@param shardIndex The shard index considered
@return The last ledger sequence pertaining to the shard index
*/
static
virtual
std::uint32_t
lastSeq(std::uint32_t shardIndex)
{
return (shardIndex + 1) * lps_;
}
lastLedgerSeq(std::uint32_t shardIndex) const = 0;

protected:
// The number of ledgers stored in a shard, default is 16384
static std::uint32_t lps_;
/** The number of ledgers in a shard */
static constexpr std::uint32_t ledgersPerShardDefault {16384u};
};

}
Expand Down
3 changes: 0 additions & 3 deletions src/ripple/nodestore/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ enum Status
/** A batch of NodeObjects to write at once. */
using Batch = std::vector <std::shared_ptr<NodeObject>>;

// System constant/invariant
static constexpr std::uint32_t genesisSeq {32570u};

}
}

Expand Down
17 changes: 15 additions & 2 deletions src/ripple/nodestore/impl/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@
namespace ripple {
namespace NodeStore {

Database::Database(std::string name, Stoppable& parent,
Scheduler& scheduler, int readThreads, beast::Journal journal)
Database::Database(
std::string name,
Stoppable& parent,
Scheduler& scheduler,
int readThreads,
Section const& config,
beast::Journal journal)
: Stoppable(name, parent)
, j_(journal)
, scheduler_(scheduler)
{
std::uint32_t seq;
if (get_if_exists<std::uint32_t>(config, "earliest_seq", seq))
{
if (seq < 1)
Throw<std::runtime_error>("Invalid earliest_seq");
earliestSeq_ = seq;
}

while (readThreads-- > 0)
readThreads_.emplace_back(&Database::threadEntry, this);
}
Expand Down
13 changes: 9 additions & 4 deletions src/ripple/nodestore/impl/DatabaseNodeImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ class DatabaseNodeImp : public Database
DatabaseNodeImp(DatabaseNodeImp const&) = delete;
DatabaseNodeImp& operator=(DatabaseNodeImp const&) = delete;

DatabaseNodeImp(std::string const& name,
Scheduler& scheduler, int readThreads, Stoppable& parent,
std::unique_ptr<Backend> backend, beast::Journal j)
: Database(name, parent, scheduler, readThreads, j)
DatabaseNodeImp(
std::string const& name,
Scheduler& scheduler,
int readThreads,
Stoppable& parent,
std::unique_ptr<Backend> backend,
Section const& config,
beast::Journal j)
: Database(name, parent, scheduler, readThreads, config, j)
, pCache_(std::make_shared<TaggedCache<uint256, NodeObject>>(
name, cacheTargetSize, cacheTargetSeconds, stopwatch(), j))
, nCache_(std::make_shared<KeyCache<uint256>>(
Expand Down
13 changes: 9 additions & 4 deletions src/ripple/nodestore/impl/DatabaseRotatingImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ namespace ripple {
namespace NodeStore {

DatabaseRotatingImp::DatabaseRotatingImp(
std::string const& name, Scheduler& scheduler, int readThreads,
Stoppable& parent, std::unique_ptr<Backend> writableBackend,
std::unique_ptr<Backend> archiveBackend, beast::Journal j)
: DatabaseRotating(name, parent, scheduler, readThreads, j)
std::string const& name,
Scheduler& scheduler,
int readThreads,
Stoppable& parent,
std::unique_ptr<Backend> writableBackend,
std::unique_ptr<Backend> archiveBackend,
Section const& config,
beast::Journal j)
: DatabaseRotating(name, parent, scheduler, readThreads, config, j)
, pCache_(std::make_shared<TaggedCache<uint256, NodeObject>>(
name, cacheTargetSize, cacheTargetSeconds, stopwatch(), j))
, nCache_(std::make_shared<KeyCache<uint256>>(
Expand Down
14 changes: 9 additions & 5 deletions src/ripple/nodestore/impl/DatabaseRotatingImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ class DatabaseRotatingImp : public DatabaseRotating
DatabaseRotatingImp(DatabaseRotatingImp const&) = delete;
DatabaseRotatingImp& operator=(DatabaseRotatingImp const&) = delete;

DatabaseRotatingImp(std::string const& name,
Scheduler& scheduler, int readThreads, Stoppable& parent,
std::unique_ptr<Backend> writableBackend,
std::unique_ptr<Backend> archiveBackend,
beast::Journal j);
DatabaseRotatingImp(
std::string const& name,
Scheduler& scheduler,
int readThreads,
Stoppable& parent,
std::unique_ptr<Backend> writableBackend,
std::unique_ptr<Backend> archiveBackend,
Section const& config,
beast::Journal j);

~DatabaseRotatingImp() override
{
Expand Down
Loading

0 comments on commit 0b18b36

Please sign in to comment.