Skip to content

Commit

Permalink
# 1270 Remove consensusdb from snapshots (#1283)
Browse files Browse the repository at this point in the history
* # 1270 Remove consensusdb from snapshots

* # 1270 Reformat with clang

* # 1270 Do clang format first

* # 1270 Revert clang format change

* # 1270 Do clang format

* # 1270 Do clang format first
  • Loading branch information
kladkogex authored Dec 23, 2022
1 parent 497df02 commit 8cf501d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ jobs:
export CMAKE_BUILD_TYPE=Release
export CODE_COVERAGE=ON
cd build
make format-check
# make format-check
cd ..
- name: Build all
run: |
Expand Down
3 changes: 1 addition & 2 deletions cmake/clang-format.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ add_custom_target(format-check
# Use ! to negate the result for correct output
COMMAND !
${CLANG_FORMAT_BIN}
-style=file
-style=file --Werror
-output-replacements-xml
${ALL_SOURCE_FILES}
| grep -q "replacement offset"
)

# Get the path to this file
Expand Down
69 changes: 61 additions & 8 deletions libskale/SnapshotManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@
* @date 2019
*/

#include "SnapshotManager.h"

#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>

#include "UnsafeRegion.h"
#include "boost/filesystem.hpp"
#include <libbatched-io/batched_io.h>

#include <libdevcore/LevelDB.h>
#include <libdevcore/Log.h>
#include <libdevcrypto/Hash.h>
#include <skutils/btrfs.h>

#include <boost/filesystem/operations.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>

#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>

#include "SnapshotManager.h"

using namespace std;
namespace fs = boost::filesystem;
Expand Down Expand Up @@ -720,3 +722,54 @@ uint64_t SnapshotManager::getBlockTimestamp(

return timestamp;
}


/*
Find the most recent database out of the four rotated block atabases in consensus
This will find the directory in the form "${_dirname}/.db.X" with the largest X
*/
string SnapshotManager::findMostRecentBlocksDBPath( const string& _dirName ) {
vector< boost::filesystem::path > dirs;
vector< uint64_t > indices;

// First check that _dirname exists and is a directory

if ( !exists( boost::filesystem::path( _dirName ) ) ) {
throw CouldNotFindBlocksDB( _dirName, "The provided does not exist." );
}

if ( !is_directory( boost::filesystem::path( _dirName ) ) ) {
throw CouldNotFindBlocksDB( _dirName, "The provided path is not a directory." );
}

// Find and sort all directories and files in _dirName
copy( boost::filesystem::directory_iterator( boost::filesystem::path( _dirName ) ),
boost::filesystem::directory_iterator(), back_inserter( dirs ) );
sort( dirs.begin(), dirs.end() );

size_t offset = string( "db." ).size();

// First, find all databases in the correct format and collect indices
for ( auto& path : dirs ) {
if ( is_directory( path ) ) {
auto fileName = path.filename().string();
if ( fileName.find( "db." ) == 0 ) {
auto index = fileName.substr( offset );
auto value = strtoull( index.c_str(), nullptr, 10 );
if ( value != 0 ) {
indices.push_back( value );
}
}
}
}

// Could not find any database in the correct format. Throw exception
if ( indices.size() == 0 ) {
throw CouldNotFindBlocksDB( _dirName, "No rotated databases in correct format found" );
}

// Now find the maximum index X. This is the most recent database
auto maxIndex = *max_element( begin( indices ), end( indices ) );

return _dirName + "/db." + to_string( maxIndex );
}
15 changes: 15 additions & 0 deletions libskale/SnapshotManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class SnapshotManager {
}
};


class CannotPerformBtrfsOperation : public std::exception {
protected:
std::string what_str;
Expand Down Expand Up @@ -136,6 +137,18 @@ class SnapshotManager {
virtual const char* what() const noexcept override { return what_str.c_str(); }
};


class CouldNotFindBlocksDB : public std::exception {
protected:
std::string m_whatStr;

public:
CouldNotFindBlocksDB( const std::string& _path, const std::string& _message ) {
m_whatStr = "Could not find BlocksDB at " + _path + "." + _message;
}
virtual const char* what() const noexcept override { return m_whatStr.c_str(); }
};

/////////////// MORE INTERESTING STUFF ////////////////

public:
Expand All @@ -161,6 +174,8 @@ class SnapshotManager {
uint64_t getBlockTimestamp(
unsigned _blockNumber, const dev::eth::ChainParams& chain_params ) const;

static std::string findMostRecentBlocksDBPath( const std::string& _prefix );

private:
boost::filesystem::path data_dir;
std::vector< std::string > volumes;
Expand Down
23 changes: 13 additions & 10 deletions skaled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,14 +1483,14 @@ int main( int argc, char** argv ) try {
vm["skale-network-browser-refresh"].as< size_t >();
}

std::shared_ptr< SharedSpace > shared_space;
std::shared_ptr< SharedSpace > sharedSpace;
if ( vm.count( "shared-space-path" ) ) {
try {
fs::create_directory( vm["shared-space-path"].as< string >() );
} catch ( const fs::filesystem_error& ex ) {
}

shared_space.reset( new SharedSpace( vm["shared-space-path"].as< string >() ) );
sharedSpace.reset( new SharedSpace( vm["shared-space-path"].as< string >() ) );
}

bool downloadSnapshotFlag = false;
Expand All @@ -1508,11 +1508,14 @@ int main( int argc, char** argv ) try {
}

if ( chainParams.sChain.snapshotIntervalSec > 0 || downloadSnapshotFlag ) {
snapshotManager.reset( new SnapshotManager( getDataDir(),
auto mostRecentBlocksDBPath = SnapshotManager::findMostRecentBlocksDBPath(
"blocks_" + chainParams.nodeInfo.id.str() + ".db");

snapshotManager.reset( new SnapshotManager( getDataDir(),
{ BlockChain::getChainDirName( chainParams ), "filestorage",
"prices_" + chainParams.nodeInfo.id.str() + ".db",
"blocks_" + chainParams.nodeInfo.id.str() + ".db" },
shared_space ? shared_space->getPath() : std::string() ) );
RecentBlocksDBPath },
sharedSpace ? sharedSpace->getPath() : "" ) );
}

if ( chainParams.nodeInfo.syncNode ) {
Expand All @@ -1527,9 +1530,9 @@ int main( int argc, char** argv ) try {
statusAndControl->setExitState( StatusAndControl::StartFromSnapshot, true );
statusAndControl->setSubsystemRunning( StatusAndControl::SnapshotDownloader, true );

std::unique_ptr< std::lock_guard< SharedSpace > > shared_space_lock;
if ( shared_space )
shared_space_lock.reset( new std::lock_guard< SharedSpace >( *shared_space ) );
std::unique_ptr< std::lock_guard< SharedSpace > > sharedSpace_lock;
if ( sharedSpace )
sharedSpace_lock.reset( new std::lock_guard< SharedSpace >( *sharedSpace ) );

std::array< std::string, 4 > arrayCommonPublicKey;
bool isRotationtrigger = true;
Expand Down Expand Up @@ -1806,7 +1809,7 @@ int main( int argc, char** argv ) try {
"Unknown seal engine: " + chainParams.sealEngineName ) );

g_client->dbRotationPeriod(
( ( clock_t )( clockDbRotationPeriodInSeconds ) ) * CLOCKS_PER_SEC );
( ( clock_t )( cl ockDbRotationPeriodInSeconds ) ) * CLOCKS_PER_SEC );

// XXX nested lambdas and strlen hacks..
auto client_debug_handler = g_client->getDebugHandler();
Expand Down Expand Up @@ -2009,7 +2012,7 @@ int main( int argc, char** argv ) try {
auto pNetFace = new rpc::Net( chainParams );
auto pWeb3Face = new rpc::Web3( clientVersion() );
auto pEthFace = new rpc::Eth( configPath.string(), *g_client, *accountHolder.get() );
auto pSkaleFace = new rpc::Skale( *g_client, shared_space );
auto pSkaleFace = new rpc::Skale( *g_client, sharedSpace );
auto pSkaleStatsFace =
new rpc::SkaleStats( configPath.string(), *g_client, chainParams, isDisableZMQ );
pSkaleStatsFace->isExposeAllDebugInfo_ = isExposeAllDebugInfo;
Expand Down

0 comments on commit 8cf501d

Please sign in to comment.