diff options
-rw-r--r-- | src/blockchain_db/lmdb/db_lmdb.cpp | 62 |
1 files changed, 30 insertions, 32 deletions
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index ee4368e86..931bbec4b 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -2623,6 +2623,12 @@ bool BlockchainLMDB::batch_start(uint64_t batch_num_blocks, uint64_t batch_bytes m_batch_active = true; memset(&m_wcursors, 0, sizeof(m_wcursors)); + if (m_tinfo.get()) + { + if (m_tinfo->m_ti_rflags.m_rf_txn) + mdb_txn_reset(m_tinfo->m_ti_rtxn); + memset(&m_tinfo->m_ti_rflags, 0, sizeof(m_tinfo->m_ti_rflags)); + } LOG_PRINT_L3("batch transaction: begin"); return true; @@ -2732,29 +2738,34 @@ void BlockchainLMDB::set_batch_transactions(bool batch_transactions) bool BlockchainLMDB::block_rtxn_start(MDB_txn **mtxn, mdb_txn_cursors **mcur) const { bool ret = false; + mdb_threadinfo *tinfo; if (m_write_txn && m_writer == boost::this_thread::get_id()) { *mtxn = m_write_txn->m_txn; *mcur = (mdb_txn_cursors *)&m_wcursors; return ret; } - if (!m_tinfo.get()) + /* Check for existing info and force reset if env doesn't match - + * only happens if env was opened/closed multiple times in same process + */ + if (!(tinfo = m_tinfo.get()) || mdb_txn_env(tinfo->m_ti_rtxn) != m_env) { - m_tinfo.reset(new mdb_threadinfo); - memset(&m_tinfo->m_ti_rcursors, 0, sizeof(m_tinfo->m_ti_rcursors)); - memset(&m_tinfo->m_ti_rflags, 0, sizeof(m_tinfo->m_ti_rflags)); - if (auto mdb_res = lmdb_txn_begin(m_env, NULL, MDB_RDONLY, &m_tinfo->m_ti_rtxn)) + tinfo = new mdb_threadinfo; + m_tinfo.reset(tinfo); + memset(&tinfo->m_ti_rcursors, 0, sizeof(tinfo->m_ti_rcursors)); + memset(&tinfo->m_ti_rflags, 0, sizeof(tinfo->m_ti_rflags)); + if (auto mdb_res = lmdb_txn_begin(m_env, NULL, MDB_RDONLY, &tinfo->m_ti_rtxn)) throw0(DB_ERROR_TXN_START(lmdb_error("Failed to create a read transaction for the db: ", mdb_res).c_str())); ret = true; - } else if (!m_tinfo->m_ti_rflags.m_rf_txn) + } else if (!tinfo->m_ti_rflags.m_rf_txn) { - if (auto mdb_res = lmdb_txn_renew(m_tinfo->m_ti_rtxn)) + if (auto mdb_res = lmdb_txn_renew(tinfo->m_ti_rtxn)) throw0(DB_ERROR_TXN_START(lmdb_error("Failed to renew a read transaction for the db: ", mdb_res).c_str())); ret = true; } if (ret) - m_tinfo->m_ti_rflags.m_rf_txn = true; - *mtxn = m_tinfo->m_ti_rtxn; - *mcur = &m_tinfo->m_ti_rcursors; + tinfo->m_ti_rflags.m_rf_txn = true; + *mtxn = tinfo->m_ti_rtxn; + *mcur = &tinfo->m_ti_rcursors; if (ret) LOG_PRINT_L3("BlockchainLMDB::" << __func__); @@ -2772,28 +2783,9 @@ void BlockchainLMDB::block_txn_start(bool readonly) { if (readonly) { - bool didit = false; - if (m_write_txn && m_writer == boost::this_thread::get_id()) - return; - if (!m_tinfo.get()) - { - m_tinfo.reset(new mdb_threadinfo); - memset(&m_tinfo->m_ti_rcursors, 0, sizeof(m_tinfo->m_ti_rcursors)); - memset(&m_tinfo->m_ti_rflags, 0, sizeof(m_tinfo->m_ti_rflags)); - if (auto mdb_res = lmdb_txn_begin(m_env, NULL, MDB_RDONLY, &m_tinfo->m_ti_rtxn)) - throw0(DB_ERROR_TXN_START(lmdb_error("Failed to create a read transaction for the db: ", mdb_res).c_str())); - didit = true; - } else if (!m_tinfo->m_ti_rflags.m_rf_txn) - { - if (auto mdb_res = lmdb_txn_renew(m_tinfo->m_ti_rtxn)) - throw0(DB_ERROR_TXN_START(lmdb_error("Failed to renew a read transaction for the db: ", mdb_res).c_str())); - didit = true; - } - if (didit) - { - m_tinfo->m_ti_rflags.m_rf_txn = true; - LOG_PRINT_L3("BlockchainLMDB::" << __func__ << " RO"); - } + MDB_txn *mtxn; + mdb_txn_cursors *mcur; + block_rtxn_start(&mtxn, &mcur); return; } @@ -2818,6 +2810,12 @@ void BlockchainLMDB::block_txn_start(bool readonly) throw0(DB_ERROR_TXN_START(lmdb_error("Failed to create a transaction for the db: ", mdb_res).c_str())); } memset(&m_wcursors, 0, sizeof(m_wcursors)); + if (m_tinfo.get()) + { + if (m_tinfo->m_ti_rflags.m_rf_txn) + mdb_txn_reset(m_tinfo->m_ti_rtxn); + memset(&m_tinfo->m_ti_rflags, 0, sizeof(m_tinfo->m_ti_rflags)); + } } else if (m_writer != boost::this_thread::get_id()) throw0(DB_ERROR_TXN_START((std::string("Attempted to start new write txn when batch txn already exists in ")+__FUNCTION__).c_str())); } |