Skip to content

Commit

Permalink
Remove SQLite Validations table
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelportilla authored and nbougalis committed Aug 5, 2019
1 parent e1adbd7 commit c5a95f1
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 672 deletions.
126 changes: 0 additions & 126 deletions src/ripple/app/consensus/RCLValidations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ mismatch(RCLValidatedLedger const& a, RCLValidatedLedger const& b)
RCLValidationsAdaptor::RCLValidationsAdaptor(Application& app, beast::Journal j)
: app_(app), j_(j)
{
staleValidations_.reserve(512);
}

NetClock::time_point
Expand Down Expand Up @@ -148,131 +147,6 @@ RCLValidationsAdaptor::acquire(LedgerHash const & hash)
return RCLValidatedLedger(std::move(ledger), j_);
}

void
RCLValidationsAdaptor::onStale(RCLValidation&& v)
{
// Store the newly stale validation; do not do significant work in this
// function since this is a callback from Validations, which may be
// doing other work.

ScopedLockType sl(staleLock_);
staleValidations_.emplace_back(std::move(v));
if (staleWriting_)
return;

// addJob() may return false (Job not added) at shutdown.
staleWriting_ = app_.getJobQueue().addJob(
jtWRITE, "Validations::doStaleWrite", [this](Job&) {
auto event =
app_.getJobQueue().makeLoadEvent(jtDISK, "ValidationWrite");
ScopedLockType sl(staleLock_);
doStaleWrite(sl);
});
}

void
RCLValidationsAdaptor::flush(hash_map<NodeID, RCLValidation>&& remaining)
{
bool anyNew = false;
{
ScopedLockType sl(staleLock_);

for (auto const& keyVal : remaining)
{
staleValidations_.emplace_back(std::move(keyVal.second));
anyNew = true;
}

// If we have new validations to write and there isn't a write in
// progress already, then write to the database synchronously.
if (anyNew && !staleWriting_)
{
staleWriting_ = true;
doStaleWrite(sl);
}

// In the case when a prior asynchronous doStaleWrite was scheduled,
// this loop will block until all validations have been flushed.
// This ensures that all validations are written upon return from
// this function.

while (staleWriting_)
{
ScopedUnlockType sul(staleLock_);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
}

// NOTE: doStaleWrite() must be called with staleLock_ *locked*. The passed
// ScopedLockType& acts as a reminder to future maintainers.
void
RCLValidationsAdaptor::doStaleWrite(ScopedLockType&)
{
static const std::string insVal(
"INSERT INTO Validations "
"(InitialSeq, LedgerSeq, LedgerHash,NodePubKey,SignTime,RawData) "
"VALUES (:initialSeq, :ledgerSeq, "
":ledgerHash,:nodePubKey,:signTime,:rawData);");
static const std::string findSeq(
"SELECT LedgerSeq FROM Ledgers WHERE Ledgerhash=:ledgerHash;");

assert(staleWriting_);

while (!staleValidations_.empty())
{
std::vector<RCLValidation> currentStale;
currentStale.reserve(512);
staleValidations_.swap(currentStale);

{
ScopedUnlockType sul(staleLock_);
{
auto db = app_.getLedgerDB().checkoutDb();

Serializer s(1024);
soci::transaction tr(*db);
for (RCLValidation const& wValidation : currentStale)
{
// Only save full validations until we update the schema
if(!wValidation.full())
continue;
s.erase();
STValidation::pointer const& val = wValidation.unwrap();
val->add(s);

auto const ledgerHash = to_string(val->getLedgerHash());

boost::optional<std::uint64_t> ledgerSeq;
*db << findSeq, soci::use(ledgerHash),
soci::into(ledgerSeq);

auto const initialSeq = ledgerSeq.value_or(
app_.getLedgerMaster().getCurrentLedgerIndex());
auto const nodePubKey = toBase58(
TokenType::NodePublic, val->getSignerPublic());
auto const signTime =
val->getSignTime().time_since_epoch().count();

soci::blob rawData(*db);
rawData.append(
reinterpret_cast<const char*>(s.peekData().data()),
s.peekData().size());
assert(rawData.get_len() == s.peekData().size());

*db << insVal, soci::use(initialSeq), soci::use(ledgerSeq),
soci::use(ledgerHash), soci::use(nodePubKey),
soci::use(signTime), soci::use(rawData);
}

tr.commit();
}
}
}

staleWriting_ = false;
}

bool
handleNewValidation(Application& app,
STValidation::ref val,
Expand Down
29 changes: 0 additions & 29 deletions src/ripple/app/consensus/RCLValidations.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,6 @@ class RCLValidationsAdaptor
NetClock::time_point
now() const;

/** Handle a newly stale validation.
@param v The newly stale validation
@warning This should do minimal work, as it is expected to be called
by the generic Validations code while it may be holding an
internal lock
*/
void
onStale(RCLValidation&& v);

/** Flush current validations to disk before shutdown.
@param remaining The remaining validations to flush
*/
void
flush(hash_map<NodeID, RCLValidation>&& remaining);

/** Attempt to acquire the ledger with given id from the network */
boost::optional<RCLValidatedLedger>
acquire(LedgerHash const & id);
Expand All @@ -245,17 +227,6 @@ class RCLValidationsAdaptor

Application& app_;
beast::Journal j_;

// Lock for managing staleValidations_ and writing_
std::mutex staleLock_;
std::vector<RCLValidation> staleValidations_;
bool staleWriting_ = false;

// Write the stale validations to sqlite DB, the scoped lock argument
// is used to remind callers that the staleLock_ must be *locked* prior
// to making the call
void
doStaleWrite(ScopedLockType&);
};

/// Alias for RCL-specific instantiation of generic Validations
Expand Down
9 changes: 0 additions & 9 deletions src/ripple/app/ledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,9 +948,6 @@ static bool saveValidatedLedger (
VALUES
(:ledgerHash,:ledgerSeq,:prevHash,:totalCoins,:closingTime,:prevClosingTime,
:closeTimeRes,:closeFlags,:accountSetHash,:transSetHash);)sql");
static std::string updateVal(
R"sql(UPDATE Validations SET LedgerSeq = :ledgerSeq, InitialSeq = :initialSeq
WHERE LedgerHash = :ledgerHash;)sql");

auto db (app.getLedgerDB ().checkoutDb ());

Expand Down Expand Up @@ -981,12 +978,6 @@ static bool saveValidatedLedger (
soci::use(accountHash),
soci::use(txHash);


*db << updateVal,
soci::use(seq),
soci::use(seq),
soci::use(hash);

tr.commit();
}

Expand Down
Loading

0 comments on commit c5a95f1

Please sign in to comment.