Performed ledger input output writing inside a transaction. (#290)

This commit is contained in:
Ravin Perera
2021-04-13 12:34:38 +05:30
committed by GitHub
parent 7a837ede7c
commit 2283d2bf89
5 changed files with 63 additions and 28 deletions

View File

@@ -9,19 +9,23 @@
#include "ledger_common.hpp"
#include "ledger_serve.hpp"
#define RAW_DATA_RETURN(ret) \
{ \
if (users_stmt != NULL) \
sqlite3_finalize(users_stmt); \
if (outputs_stmt != NULL) \
sqlite3_finalize(outputs_stmt); \
if (inputs_stmt != NULL) \
sqlite3_finalize(inputs_stmt); \
if (in_fd != -1) \
close(in_fd); \
if (out_fd != -1) \
close(out_fd); \
return ret; \
#define RAW_DATA_RETURN(ret) \
{ \
if (ret == -1) \
sqlite::rollback_transaction(db); \
else \
sqlite::commit_transaction(db); \
if (users_stmt != NULL) \
sqlite3_finalize(users_stmt); \
if (outputs_stmt != NULL) \
sqlite3_finalize(outputs_stmt); \
if (inputs_stmt != NULL) \
sqlite3_finalize(inputs_stmt); \
if (in_fd != -1) \
close(in_fd); \
if (out_fd != -1) \
close(out_fd); \
return ret; \
}
namespace ledger
@@ -299,6 +303,10 @@ namespace ledger
size_t in_pos = 0; // Current writing position offset of the inputs file.
size_t out_pos = 0; // Current writing position offset of the outputs file.
// Group all row insertions within a transaction for consistency.
if (sqlite::begin_transaction(db) == -1)
RAW_DATA_RETURN(-1);
for (const auto &[pubkey, cu] : consensed_users)
{
if (sqlite::insert_user_record(users_stmt, lcl_id.seq_no, pubkey) == -1)
@@ -416,11 +424,11 @@ namespace ledger
* Creates or open a db connection to the shard based on the params. This is used to create primary and raw shards.
* @param db Database connection to be opened.
* @param ledger_seq_no Ledger sequence number.
* @param open_db Whether a connection to the sql db must be opened or not.
* @param keep_db_connection Whether the sqlite db connection must be kept open or not.
* @return 0 if shard already exists. 1 if new shard got created. -1 on failure.
*/
int prepare_shard(sqlite3 **db, uint64_t &shard_seq_no, const uint64_t ledger_seq_no, const uint64_t shard_size,
const char *shard_dir, const char *db_name, const bool open_db)
const char *shard_dir, const char *db_name, const bool keep_db_connection)
{
// Construct shard path.
shard_seq_no = (ledger_seq_no - 1) / shard_size;
@@ -439,7 +447,7 @@ namespace ledger
}
// Creating ledger database and open a database connection.
if (sqlite::open_db(db_path, db) == -1)
if (sqlite::open_db(db_path, db, true) == -1)
{
LOG_ERROR << errno << ": Error creating the database " << db_name;
return -1;
@@ -460,7 +468,7 @@ namespace ledger
}
// Close the connection if it doesn't need to be retained.
if (!open_db)
if (!keep_db_connection)
sqlite::close_db(db);
util::h32 prev_shard_hash;
@@ -511,7 +519,7 @@ namespace ledger
}
else
{
if (open_db && sqlite::open_db(db_path, db) == -1)
if (keep_db_connection && sqlite::open_db(db_path, db, true) == -1)
{
LOG_ERROR << errno << ": Error openning the shard database " << db_path;
return -1;
@@ -779,7 +787,7 @@ namespace ledger
ledger::ledger_record ledger;
if (sqlite::get_ledger_by_seq_no(db, seq_no, ledger) == -1)
{
LOG_ERROR << "Error getting ledger by sequence number: " << std::to_string(seq_no);
LOG_ERROR << "Error getting ledger by sequence number: " << seq_no;
sqlite::close_db(&db);
ledger_fs.stop_ro_session(session_name);
return -1;