Skip to content

Commit

Permalink
Merge pull request #1860 from MonsieurNicolas/preserveHAS
Browse files Browse the repository at this point in the history
Preserve buckets referenced by the Last Closed Ledger

Reviewed-by: vogel
  • Loading branch information
latobarita committed Dec 10, 2018
2 parents aeac41a + f4b6572 commit 1fe2e8a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
48 changes: 38 additions & 10 deletions src/bucket/BucketManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "bucket/BucketList.h"
#include "crypto/Hex.h"
#include "history/HistoryManager.h"
#include "ledger/LedgerManager.h"
#include "main/Application.h"
#include "main/Config.h"
#include "overlay/StellarXDR.h"
Expand Down Expand Up @@ -237,28 +238,55 @@ BucketManagerImpl::getBucketByHash(uint256 const& hash)
std::set<Hash>
BucketManagerImpl::getReferencedBuckets() const
{
auto referenced = std::set<Hash>{};
std::set<Hash> referenced;
// retain current bucket list
for (uint32_t i = 0; i < BucketList::kNumLevels; ++i)
{
auto const& level = mBucketList.getLevel(i);
referenced.insert(level.getCurr()->getHash());
referenced.insert(level.getSnap()->getHash());
auto rit = referenced.insert(level.getCurr()->getHash());
if (rit.second)
{
CLOG(TRACE, "Bucket")
<< binToHex(*rit.first) << " referenced by bucket list";
}
rit = referenced.insert(level.getSnap()->getHash());
if (rit.second)
{
CLOG(TRACE, "Bucket")
<< binToHex(*rit.first) << " referenced by bucket list";
}
for (auto const& h : level.getNext().getHashes())
{
referenced.insert(hexToBin256(h));
rit = referenced.insert(hexToBin256(h));
if (rit.second)
{
CLOG(TRACE, "Bucket") << h << " referenced by bucket list";
}
}
}
// retain any bucket referenced by the last closed ledger as recorded in the
// database (as merge complete, the bucket list drifts from that state)
auto lclHas = mApp.getLedgerManager().getLastClosedLedgerHAS();
auto lclBuckets = lclHas.allBuckets();
for (auto const& h : lclBuckets)
{
auto rit = referenced.insert(hexToBin256(h));
if (rit.second)
{
CLOG(TRACE, "Bucket") << h << " referenced by LCL";
}
}

// Implicitly retain any buckets that are referenced by a state in
// the publish queue.
// retain buckets that are referenced by a state in the publish queue.
auto pub = mApp.getHistoryManager().getBucketsReferencedByPublishQueue();
{
for (auto const& h : pub)
{
CLOG(DEBUG, "Bucket")
<< "BucketManager::forgetUnreferencedBuckets: " << h
<< " referenced by publish queue";
referenced.insert(hexToBin256(h));
auto rit = referenced.insert(hexToBin256(h));
if (rit.second)
{
CLOG(TRACE, "Bucket") << h << " referenced by publish queue";
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/ledger/LedgerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ class LedgerManager
virtual LedgerHeaderHistoryEntry const&
getLastClosedLedgerHeader() const = 0;

// return the HAS that corresponds to the last closed ledger as persisted in
// the database
virtual HistoryArchiveState getLastClosedLedgerHAS() = 0;

// Return the sequence number of the LCL.
virtual uint32_t getLastClosedLedgerNum() const = 0;

Expand Down
15 changes: 11 additions & 4 deletions src/ledger/LedgerManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,7 @@ LedgerManagerImpl::loadLastKnownLedger(

if (handler)
{
string hasString = mApp.getPersistentState().getState(
PersistentState::kHistoryArchiveState);
HistoryArchiveState has;
has.fromString(hasString);
HistoryArchiveState has = getLastClosedLedgerHAS();

auto continuation = [this, handler,
has](asio::error_code const& ec) {
Expand Down Expand Up @@ -354,6 +351,16 @@ LedgerManagerImpl::getLastClosedLedgerHeader() const
return mLastClosedLedger;
}

HistoryArchiveState
LedgerManagerImpl::getLastClosedLedgerHAS()
{
string hasString = mApp.getPersistentState().getState(
PersistentState::kHistoryArchiveState);
HistoryArchiveState has;
has.fromString(hasString);
return has;
}

uint32_t
LedgerManagerImpl::getLastClosedLedgerNum() const
{
Expand Down
2 changes: 2 additions & 0 deletions src/ledger/LedgerManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class LedgerManagerImpl : public LedgerManager

LedgerHeaderHistoryEntry const& getLastClosedLedgerHeader() const override;

HistoryArchiveState getLastClosedLedgerHAS() override;

Database& getDatabase() override;

void startCatchup(CatchupConfiguration configuration,
Expand Down

0 comments on commit 1fe2e8a

Please sign in to comment.