2021-04-09 12:53:51 +00:00
|
|
|
#include <Interpreters/MergeTreeTransactionHolder.h>
|
|
|
|
#include <Interpreters/MergeTreeTransaction.h>
|
|
|
|
#include <Interpreters/TransactionLog.h>
|
2022-03-09 20:38:18 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-04-09 12:53:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-03-10 21:29:58 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-03-09 20:38:18 +00:00
|
|
|
MergeTreeTransactionHolder::MergeTreeTransactionHolder(const MergeTreeTransactionPtr & txn_, bool autocommit_ = false, const Context * owned_by_session_context_)
|
2021-04-09 12:53:51 +00:00
|
|
|
: txn(txn_)
|
|
|
|
, autocommit(autocommit_)
|
2022-03-09 20:38:18 +00:00
|
|
|
, owned_by_session_context(owned_by_session_context_)
|
2021-04-09 12:53:51 +00:00
|
|
|
{
|
|
|
|
assert(!txn || txn->getState() == MergeTreeTransaction::RUNNING);
|
2022-03-09 20:38:18 +00:00
|
|
|
assert(!owned_by_session_context || owned_by_session_context == owned_by_session_context->getSessionContext().get());
|
2021-04-09 12:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MergeTreeTransactionHolder::MergeTreeTransactionHolder(MergeTreeTransactionHolder && rhs) noexcept
|
|
|
|
{
|
2022-03-09 20:38:18 +00:00
|
|
|
*this = std::move(rhs);
|
2021-04-09 12:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MergeTreeTransactionHolder & MergeTreeTransactionHolder::operator=(MergeTreeTransactionHolder && rhs) noexcept
|
|
|
|
{
|
|
|
|
onDestroy();
|
2022-03-09 20:38:18 +00:00
|
|
|
txn = nullptr;
|
|
|
|
autocommit = false;
|
|
|
|
owned_by_session_context = nullptr;
|
|
|
|
std::swap(txn, rhs.txn);
|
|
|
|
std::swap(autocommit, rhs.autocommit);
|
|
|
|
std::swap(owned_by_session_context, rhs.owned_by_session_context);
|
2021-04-09 12:53:51 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MergeTreeTransactionHolder::~MergeTreeTransactionHolder()
|
|
|
|
{
|
|
|
|
onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergeTreeTransactionHolder::onDestroy() noexcept
|
|
|
|
{
|
|
|
|
if (!txn)
|
|
|
|
return;
|
|
|
|
if (txn->getState() != MergeTreeTransaction::RUNNING)
|
|
|
|
return;
|
|
|
|
|
2021-05-17 11:14:09 +00:00
|
|
|
if (autocommit && std::uncaught_exceptions() == 0)
|
2021-04-09 12:53:51 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
TransactionLog::instance().commitTransaction(txn);
|
2021-05-17 11:14:09 +00:00
|
|
|
return;
|
2021-04-09 12:53:51 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
2021-05-17 11:14:09 +00:00
|
|
|
|
|
|
|
TransactionLog::instance().rollbackTransaction(txn);
|
2021-04-09 12:53:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 20:38:18 +00:00
|
|
|
MergeTreeTransactionHolder::MergeTreeTransactionHolder(const MergeTreeTransactionHolder & rhs)
|
2021-04-09 12:53:51 +00:00
|
|
|
{
|
2022-03-09 20:38:18 +00:00
|
|
|
*this = rhs;
|
2021-04-09 12:53:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 20:38:18 +00:00
|
|
|
MergeTreeTransactionHolder & MergeTreeTransactionHolder::operator=(const MergeTreeTransactionHolder & rhs) // NOLINT
|
2021-04-09 12:53:51 +00:00
|
|
|
{
|
2022-03-09 20:38:18 +00:00
|
|
|
if (rhs.txn && !rhs.owned_by_session_context)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR,
|
|
|
|
"Tried to copy non-empty MergeTreeTransactionHolder that is not owned by session context. It's a bug");
|
|
|
|
assert(!txn);
|
|
|
|
assert(!autocommit);
|
|
|
|
assert(!owned_by_session_context);
|
2021-04-09 12:53:51 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|