Skip to content

Commit

Permalink
#1545 state locking
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Jun 21, 2024
1 parent d562cde commit 71de8a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
14 changes: 5 additions & 9 deletions libdevcore/LevelDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ namespace dev::db {

unsigned c_maxOpenLeveldbFiles = 25;

const size_t LevelDB::BATCH_CHUNK_SIZE = 10000;
const size_t LevelDB::MAX_OLD_SNAPS_LIFETIME_MS = 10000;
const size_t LevelDB::FORCE_CLOSE_TIME_MS = 3000;

namespace {
inline leveldb::Slice toLDBSlice( Slice _slice ) {
return leveldb::Slice( _slice.data(), _slice.size() );
Expand Down Expand Up @@ -296,14 +292,14 @@ void LevelDB::reopenDataBaseIfNeeded() {

auto startTimeMs = getCurrentTimeMs();

while (getCurrentTimeMs() <= startTimeMs + FORCE_CLOSE_TIME_MS) {
cleanOldSnapsUnsafe(FORCE_CLOSE_TIME_MS);
while (getCurrentTimeMs() <= startTimeMs + FORCE_SNAP_CLOSE_TIME_MS ) {
cleanUnusedOldSnapsUnsafe( FORCE_SNAP_CLOSE_TIME_MS );
}

if (oldSnaps.empty()) {
// there are still open snaps. Close all of them not waiting for
// eth_calls to complete by passing 0 as wait time
cleanOldSnapsUnsafe(0);
cleanUnusedOldSnapsUnsafe( 0 );
}

LDB_CHECK(oldSnaps.empty());
Expand Down Expand Up @@ -463,12 +459,12 @@ void LevelDB::createBlockSnap( uint64_t _blockId ) {

// we clean unneeded old snaps that no-one used or that exist for more that max
// lifetime we give for eth_calls to complete
cleanOldSnapsUnsafe( MAX_OLD_SNAPS_LIFETIME_MS );
cleanUnusedOldSnapsUnsafe( OLD_SNAP_LIFETIME_MS );
}


// this function should be called while holding database reopen lock
void LevelDB::cleanOldSnapsUnsafe( uint64_t _maxSnapLifetimeMs ) {
void LevelDB::cleanUnusedOldSnapsUnsafe( uint64_t _maxSnapLifetimeMs ) {
std::unique_lock< std::shared_mutex > snapLock( m_snapMutex );

//now we iterate over oldSnaps closing the ones that are not more in use
Expand Down
11 changes: 7 additions & 4 deletions libdevcore/LevelDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ class LevelDB : public DatabaseFace {
std::shared_mutex m_snapMutex;


static const size_t BATCH_CHUNK_SIZE;
static const size_t MAX_OLD_SNAPS_LIFETIME_MS;
static const size_t FORCE_CLOSE_TIME_MS;
static constexpr size_t BATCH_CHUNK_SIZE = 10000;
// time after an existing old snap will be closed if no-one is using it
static const size_t OLD_SNAP_LIFETIME_MS = 10000;
// time after an existing old snap will be closed it is used in eth_call
// this will cause the eth_call to return an error
static const size_t FORCE_SNAP_CLOSE_TIME_MS = 3000;

class SharedDBGuard {
const LevelDB& m_levedlDB;
Expand Down Expand Up @@ -161,7 +164,7 @@ class LevelDB : public DatabaseFace {
};
void openDBInstanceUnsafe();
void reopenDataBaseIfNeeded();
void cleanOldSnapsUnsafe( uint64_t _maxSnapLifetimeMs );
void cleanUnusedOldSnapsUnsafe( uint64_t _maxSnapLifetimeMs );
};

} // namespace dev::db

0 comments on commit 71de8a5

Please sign in to comment.