Skip to content

Commit

Permalink
Merge pull request #1576 from MonsieurNicolas/maintenancePerfFix
Browse files Browse the repository at this point in the history
improve query used to delete old entries during maintenance

Reviewed-by: jonjove
  • Loading branch information
latobarita authored Mar 1, 2018
2 parents 8d37e95 + 2e4be8a commit 139d548
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Builds/VisualStudio/stellar-core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ exit /b 0
<ClCompile Include="..\..\src\database\DatabaseConnectionString.cpp" />
<ClCompile Include="..\..\src\database\DatabaseConnectionStringTest.cpp" />
<ClCompile Include="..\..\src\database\DatabaseTests.cpp" />
<ClCompile Include="..\..\src\database\DatabaseUtils.cpp" />
<ClCompile Include="..\..\src\herder\Herder.cpp" />
<ClCompile Include="..\..\src\herder\HerderImpl.cpp" />
<ClCompile Include="..\..\src\herder\HerderPersistenceImpl.cpp" />
Expand Down Expand Up @@ -550,6 +551,7 @@ exit /b 0
<ClInclude Include="..\..\src\crypto\StrKey.h" />
<ClInclude Include="..\..\src\database\Database.h" />
<ClInclude Include="..\..\src\database\DatabaseConnectionString.h" />
<ClInclude Include="..\..\src\database\DatabaseUtils.h" />
<ClInclude Include="..\..\src\herder\HerderPersistence.h" />
<ClInclude Include="..\..\src\herder\HerderPersistenceImpl.h" />
<ClInclude Include="..\..\src\herder\HerderSCPDriver.h" />
Expand Down
6 changes: 6 additions & 0 deletions Builds/VisualStudio/stellar-core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,9 @@
<ClCompile Include="..\..\src\main\Maintainer.cpp">
<Filter>main</Filter>
</ClCompile>
<ClCompile Include="..\..\src\database\DatabaseUtils.cpp">
<Filter>database</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\ledger\LedgerManager.h">
Expand Down Expand Up @@ -1451,6 +1454,9 @@
<ClInclude Include="..\..\src\main\Maintainer.h">
<Filter>main</Filter>
</ClInclude>
<ClInclude Include="..\..\src\database\DatabaseUtils.h">
<Filter>database</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\AUTHORS" />
Expand Down
31 changes: 31 additions & 0 deletions src/database/DatabaseUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#include "DatabaseUtils.h"

namespace stellar
{
namespace DatabaseUtils
{
void
deleteOldEntriesHelper(soci::session& sess, uint32_t ledgerSeq, uint32_t count,
std::string const& tableName,
std::string const& ledgerSeqColumn)
{
uint32_t curMin = 0;
soci::indicator gotMin;
soci::statement st = (sess.prepare << "SELECT MIN(" << ledgerSeqColumn
<< ") FROM " << tableName,
soci::into(curMin, gotMin));

st.execute(true);
if (st.got_data() && gotMin == soci::i_ok)
{
uint32 m = std::min(curMin + count, ledgerSeq);
sess << "DELETE FROM " << tableName << " WHERE " << ledgerSeqColumn
<< " <= " << m;
}
}
}
}
17 changes: 17 additions & 0 deletions src/database/DatabaseUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

// Copyright 2018 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#include "Database.h"

namespace stellar
{
namespace DatabaseUtils
{
void deleteOldEntriesHelper(soci::session& sess, uint32_t ledgerSeq,
uint32_t count, std::string const& tableName,
std::string const& ledgerSeqColumn);
}
}
11 changes: 5 additions & 6 deletions src/herder/HerderPersistenceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "herder/HerderPersistenceImpl.h"
#include "crypto/Hex.h"
#include "database/Database.h"
#include "database/DatabaseUtils.h"
#include "herder/Herder.h"
#include "main/Application.h"
#include "scp/Slot.h"
Expand Down Expand Up @@ -269,11 +270,9 @@ void
HerderPersistence::deleteOldEntries(Database& db, uint32_t ledgerSeq,
uint32_t count)
{
db.getSession() << "DELETE FROM scphistory WHERE ledgerseq IN (SELECT "
"ledgerseq FROM scphistory WHERE ledgerseq <= "
<< ledgerSeq << " LIMIT " << count << ")";
db.getSession() << "DELETE FROM scpquorums WHERE lastledgerseq IN (SELECT "
"lastledgerseq FROM scpquorums WHERE lastledgerseq <= "
<< ledgerSeq << " LIMIT " << count << ")";
DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count,
"scphistory", "ledgerseq");
DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count,
"scpquorums", "lastledgerseq");
}
}
6 changes: 3 additions & 3 deletions src/ledger/LedgerHeaderFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "crypto/Hex.h"
#include "crypto/SHA.h"
#include "database/Database.h"
#include "database/DatabaseUtils.h"
#include "util/Logging.h"
#include "util/XDRStream.h"
#include "util/format.h"
Expand Down Expand Up @@ -243,9 +244,8 @@ void
LedgerHeaderFrame::deleteOldEntries(Database& db, uint32_t ledgerSeq,
uint32_t count)
{
db.getSession() << "DELETE FROM ledgerheaders WHERE ledgerseq IN (SELECT "
"ledgerseq FROM ledgerheaders WHERE ledgerseq <= "
<< ledgerSeq << " LIMIT " << count << ")";
DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count,
"ledgerheaders", "ledgerseq");
}

void
Expand Down
11 changes: 5 additions & 6 deletions src/transactions/TransactionFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "crypto/SHA.h"
#include "crypto/SignerKey.h"
#include "database/Database.h"
#include "database/DatabaseUtils.h"
#include "herder/TxSetFrame.h"
#include "invariant/InvariantManager.h"
#include "ledger/LedgerDelta.h"
Expand Down Expand Up @@ -841,11 +842,9 @@ void
TransactionFrame::deleteOldEntries(Database& db, uint32_t ledgerSeq,
uint32_t count)
{
db.getSession() << "DELETE FROM txhistory WHERE ledgerseq IN (SELECT "
"ledgerseq FROM txhistory WHERE ledgerseq <= "
<< ledgerSeq << " LIMIT " << count << ")";
db.getSession() << "DELETE FROM txfeehistory WHERE ledgerseq IN (SELECT "
"ledgerseq FROM txfeehistory WHERE ledgerseq <= "
<< ledgerSeq << " LIMIT " << count << ")";
DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count,
"txhistory", "ledgerseq");
DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count,
"txfeehistory", "ledgerseq");
}
}

0 comments on commit 139d548

Please sign in to comment.