Refactored consensus to run on a separate thread. (#123)

This commit is contained in:
Savinda Senevirathne
2020-09-18 15:53:15 +05:30
committed by GitHub
parent 180b1ec714
commit 37629471c5
4 changed files with 164 additions and 133 deletions

View File

@@ -22,8 +22,8 @@ namespace cons
{
/**
* Voting thresholds for consensus stages.
*/
* Voting thresholds for consensus stages.
*/
constexpr float STAGE1_THRESHOLD = 0.5;
constexpr float STAGE2_THRESHOLD = 0.65;
constexpr float STAGE3_THRESHOLD = 0.8;
@@ -33,6 +33,11 @@ namespace cons
bool init_success = false;
bool is_shutting_down = false;
// Consensus processing thread.
std::thread consensus_thread;
int init()
{
//load lcl details from lcl history.
@@ -58,28 +63,56 @@ namespace cons
ctx.contract_ctx.args.state_dir = conf::ctx.state_rw_dir;
ctx.contract_ctx.args.readonly = false;
// Starting consensus processing thread.
consensus_thread = std::thread(cons::run_consensus);
init_success = true;
return 0;
}
/**
* Cleanup any resources.
*/
* Cleanup any resources.
*/
void deinit()
{
// Stop the contract if running.
sc::stop(ctx.contract_ctx);
if (init_success)
{
// Making the consensus while loop stop.
is_shutting_down = true;
// Stop the contract if running.
sc::stop(ctx.contract_ctx);
// Joining consensus processing thread.
if (consensus_thread.joinable())
consensus_thread.join();
}
}
int run_consensus()
/**
* Joins the consensus processing thread.
*/
void wait()
{
while (true)
consensus_thread.join();
}
void run_consensus()
{
util::mask_signal();
LOG_INFO << "Consensus processor started.";
while (!is_shutting_down)
{
if (consensus() == -1)
return -1;
{
LOG_ERR << "Consensus thread exited due to an error.";
break;
}
}
return 0;
LOG_INFO << "Consensus processor stopped.";
}
int consensus()