Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IS-450 Reload rotation timestamp on each isExitTime request #1757

Merged
merged 11 commits into from
Jan 9, 2024
44 changes: 29 additions & 15 deletions libethereum/InstanceMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,57 @@
using namespace dev;
namespace fs = boost::filesystem;

const std::string InstanceMonitor::rotation_info_file_name = "rotation.txt";
const std::string InstanceMonitor::rotation_info_file_name = "rotation.json";

void InstanceMonitor::prepareRotation() {
reportExitTimeReached( true );
fs::remove( m_rotationInfoFilePath );
}

void InstanceMonitor::initRotationParams( uint64_t _finishTimestamp ) {
nlohmann::json rotationJson = nlohmann::json::object();
rotationJson["timestamp"] = _finishTimestamp;
try {
nlohmann::json rotationJson = nlohmann::json::object();
rotationJson["timestamp"] = _finishTimestamp;

std::ofstream rotationInfoFile( m_rotationInfoFilePath.string() );
rotationInfoFile << rotationJson;
std::ofstream rotationInfoFile( m_rotationInfoFilePath.string() );
rotationInfoFile << rotationJson;

m_finishTimestamp = _finishTimestamp;
LOG( m_logger ) << "Set rotation time to " << m_finishTimestamp;
LOG( m_infoLogger ) << "Set rotation time to " << _finishTimestamp;
} catch ( ... ) {
LOG( m_errorLogger ) << "Setting rotation timestamp failed";
throw_with_nested( std::runtime_error( "cannot save rotation timestamp" ) );
}
}

bool InstanceMonitor::isTimeToRotate( uint64_t _finishTimestamp ) {
bool InstanceMonitor::isTimeToRotate( uint64_t _blockTimestamp ) const {
if ( !fs::exists( m_rotationInfoFilePath ) ) {
return false;
}
return m_finishTimestamp <= _finishTimestamp;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to write information about rotation into logs

try {
auto _rotationTimestamp = rotationTimestamp();
return _rotationTimestamp <= _blockTimestamp;
} catch ( InvalidRotationInfoFileException& ex ) {
return false;
}
}

void InstanceMonitor::restoreRotationParams() {
if ( fs::exists( m_rotationInfoFilePath ) ) {
std::ifstream rotationInfoFile( m_rotationInfoFilePath.string() );
uint64_t InstanceMonitor::rotationTimestamp() const {
std::ifstream rotationInfoFile( m_rotationInfoFilePath.string() );
try {
auto rotationJson = nlohmann::json::parse( rotationInfoFile );
m_finishTimestamp = rotationJson["timestamp"].get< uint64_t >();
auto timestamp = rotationJson["timestamp"].get< uint64_t >();
LOG( m_infoLogger ) << "Rotation scheduled for " << timestamp;
return timestamp;
} catch ( ... ) {
LOG( m_errorLogger ) << "Rotation file is malformed or missing";
throw InvalidRotationInfoFileException( m_rotationInfoFilePath );
}
}

void InstanceMonitor::reportExitTimeReached( bool _reached ) {
if ( m_statusAndControl ) {
LOG( m_logger ) << "Setting ExitTimeReached = " << _reached;
LOG( m_infoLogger ) << "Setting ExitTimeReached = " << _reached;
m_statusAndControl->setExitState( StatusAndControl::ExitTimeReached, _reached );
} else
LOG( m_logger ) << "Simulating setting ExitTimeReached = " << _reached;
LOG( m_infoLogger ) << "Simulating setting ExitTimeReached = " << _reached;
}
28 changes: 19 additions & 9 deletions libethereum/InstanceMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,40 @@ class InstanceMonitor {
public:
explicit InstanceMonitor( const boost::filesystem::path& _rotationInfoFileDirPath,
std::shared_ptr< StatusAndControl > _statusAndControl = nullptr )
: m_finishTimestamp( 0 ),
m_rotationInfoFilePath( _rotationInfoFileDirPath / rotation_info_file_name ),
: m_rotationInfoFilePath( _rotationInfoFileDirPath / rotation_info_file_name ),
m_statusAndControl( _statusAndControl ) {
restoreRotationParams();
reportExitTimeReached( false );
}
void prepareRotation();
void initRotationParams( uint64_t _finishTimestamp );
bool isTimeToRotate( uint64_t _finishTimestamp );
bool isTimeToRotate( uint64_t _blockTimestamp ) const;

protected:
void restoreRotationParams();
[[nodiscard]] uint64_t finishTimestamp() const { return m_finishTimestamp; }

[[nodiscard]] uint64_t rotationTimestamp() const;
[[nodiscard]] fs::path rotationInfoFilePath() const { return m_rotationInfoFilePath; }

uint64_t m_finishTimestamp;
const fs::path m_rotationInfoFilePath;
std::shared_ptr< StatusAndControl > m_statusAndControl;

static const std::string rotation_info_file_name;

void reportExitTimeReached( bool _reached );

class InvalidRotationInfoFileException : public std::exception {
protected:
std::string what_str;

public:
boost::filesystem::path path;

InvalidRotationInfoFileException( const boost::filesystem::path& _path ) : path( _path ) {
what_str = "File " + path.string() + " is malformed or missing";
}
virtual const char* what() const noexcept override { return what_str.c_str(); }
};


private:
dev::Logger m_logger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) };
mutable dev::Logger m_infoLogger{ createLogger( dev::VerbosityInfo, "instance-monitor" ) };
mutable dev::Logger m_errorLogger{ createLogger( dev::VerbosityError, "instance-monitor" ) };
};
22 changes: 17 additions & 5 deletions test/unittests/libethereum/InstanceMonitorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class InstanceMonitorMock: public InstanceMonitor {
public:
explicit InstanceMonitorMock(fs::path const &rotationFlagFilePath, std::shared_ptr<StatusAndControl> statusAndControl) : InstanceMonitor(rotationFlagFilePath, statusAndControl) {};

uint64_t getFinishTimestamp() {
return this->finishTimestamp();
};

fs::path getRotationInfoFilePath() {
return this->rotationInfoFilePath();
}
Expand All @@ -32,6 +28,10 @@ class InstanceMonitorMock: public InstanceMonitor {
void removeFlagFileTest(){
this->reportExitTimeReached( false );
}

uint64_t getRotationTimestamp() const {
return this->rotationTimestamp();
}
};

class InstanceMonitorTestFixture : public TestOutputHelperFixture {
Expand Down Expand Up @@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE( test_initRotationParams ) {
uint64_t ts = 100;
BOOST_REQUIRE( !fs::exists(instanceMonitor->getRotationInfoFilePath() ) );
instanceMonitor->initRotationParams(ts);
BOOST_CHECK_EQUAL(instanceMonitor->getFinishTimestamp(), ts);
BOOST_CHECK_EQUAL(instanceMonitor->getRotationTimestamp(), ts);

BOOST_REQUIRE( fs::exists(instanceMonitor->getRotationInfoFilePath() ) );

Expand All @@ -80,6 +80,15 @@ BOOST_AUTO_TEST_CASE( test_initRotationParams ) {
BOOST_CHECK_EQUAL(rotateJson["timestamp"].get< uint64_t >(), ts);
}


BOOST_AUTO_TEST_CASE( test_isTimeToRotate_invalid_file ) {
uint64_t currentTime = 100;
std::ofstream rotationInfoFile(instanceMonitor->getRotationInfoFilePath().string() );
rotationInfoFile << "Broken file";
BOOST_REQUIRE( !instanceMonitor->isTimeToRotate( currentTime ) );
}


BOOST_AUTO_TEST_CASE( test_isTimeToRotate_false ) {
uint64_t currentTime = 100;
uint64_t finishTime = 200;
Expand All @@ -98,6 +107,9 @@ BOOST_AUTO_TEST_CASE( test_isTimeToRotate_true ) {

instanceMonitor->initRotationParams(50);
BOOST_REQUIRE( instanceMonitor->isTimeToRotate( currentTime ) );

currentTime = 49;
BOOST_REQUIRE( !instanceMonitor->isTimeToRotate( currentTime ) );
}

BOOST_AUTO_TEST_CASE( test_rotation ) {
Expand Down
Loading