diff --git a/src/gausskernel/process/threadpool/knl_thread.cpp b/src/gausskernel/process/threadpool/knl_thread.cpp index d1521f3be..878ba9e9d 100755 --- a/src/gausskernel/process/threadpool/knl_thread.cpp +++ b/src/gausskernel/process/threadpool/knl_thread.cpp @@ -1407,7 +1407,6 @@ static void knl_t_mot_init(knl_t_mot_context* mot_cxt) mot_cxt->bindPolicy = 2; // MPOL_BIND mot_cxt->mbindFlags = 0; - mot_cxt->mot_startup = false; } void knl_thread_mot_init() diff --git a/src/gausskernel/storage/access/transam/xlog.cpp b/src/gausskernel/storage/access/transam/xlog.cpp index 602ba4a00..a308a0cd5 100755 --- a/src/gausskernel/storage/access/transam/xlog.cpp +++ b/src/gausskernel/storage/access/transam/xlog.cpp @@ -8243,9 +8243,7 @@ void StartupXLOG(void) /* * Recover MOT */ - if (t_thrd.mot_cxt.mot_startup == true) { - MOTRecover(); - } + MOTRecover(); /* initialize shared memory variables from the checkpoint record */ t_thrd.xact_cxt.ShmemVariableCache->nextXid = checkPoint.nextXid; @@ -9227,9 +9225,7 @@ void StartupXLOG(void) /* * Cleanup MOT recovery */ - if (t_thrd.mot_cxt.mot_startup == true) { - MOTRecoveryDone(); - } + MOTRecoveryDone(); } void sendPMBeginHotStby() diff --git a/src/gausskernel/storage/mot/core/src/system/recovery/recovery_manager.cpp b/src/gausskernel/storage/mot/core/src/system/recovery/recovery_manager.cpp index 3f382ce2a..b1d393b8e 100644 --- a/src/gausskernel/storage/mot/core/src/system/recovery/recovery_manager.cpp +++ b/src/gausskernel/storage/mot/core/src/system/recovery/recovery_manager.cpp @@ -519,9 +519,16 @@ bool RecoveryManager::RecoverFromCheckpoint() bool RecoveryManager::RecoverDbStart() { MOT_LOG_INFO("Starting MOT recovery"); + + if (m_recoverFromCkptDone) { + return true; + } + if (!RecoverFromCheckpoint()) { return false; } + + m_recoverFromCkptDone = true; return true; } diff --git a/src/gausskernel/storage/mot/core/src/system/recovery/recovery_manager.h b/src/gausskernel/storage/mot/core/src/system/recovery/recovery_manager.h index 7fd391690..bf5517733 100644 --- a/src/gausskernel/storage/mot/core/src/system/recovery/recovery_manager.h +++ b/src/gausskernel/storage/mot/core/src/system/recovery/recovery_manager.h @@ -171,6 +171,7 @@ public: RecoveryManager() : m_logStats(nullptr), m_initialized(false), + m_recoverFromCkptDone(false), m_checkpointId(0), m_lsn(0), m_numWorkers(GetGlobalConfiguration().m_checkpointRecoveryWorkers), @@ -1010,6 +1011,8 @@ private: bool m_initialized; + bool m_recoverFromCkptDone; + uint64_t m_checkpointId; uint64_t m_lsn; diff --git a/src/gausskernel/storage/mot/fdw_adapter/src/mot_fdw.cpp b/src/gausskernel/storage/mot/fdw_adapter/src/mot_fdw.cpp index 0a27ed19a..008cacfaf 100644 --- a/src/gausskernel/storage/mot/fdw_adapter/src/mot_fdw.cpp +++ b/src/gausskernel/storage/mot/fdw_adapter/src/mot_fdw.cpp @@ -230,6 +230,11 @@ int MOTXlateRecoveryErr(int err) void MOTRecover() { + if (!MOTAdaptor::m_initialized) { + // This is the case when StartupXLOG is called during bootstrap. + return; + } + EnsureSafeThreadAccess(); if (!MOT::MOTEngine::GetInstance()->StartRecovery()) { // we treat errors fatally. @@ -247,6 +252,11 @@ void MOTRecover() void MOTRecoveryDone() { + if (!MOTAdaptor::m_initialized) { + // This is the case when StartupXLOG is called during bootstrap. + return; + } + EnsureSafeThreadAccess(); if (!MOT::MOTEngine::GetInstance()->EndRecovery()) { // we treat errors fatally. @@ -261,6 +271,10 @@ void MOTRecoveryDone() */ void MOTBeginRedoRecovery() { + if (!MOTAdaptor::m_initialized) { + return; + } + EnsureSafeThreadAccess(); if (!MOT::MOTEngine::GetInstance()->CreateRecoverySessionContext()) { // we treat errors fatally. @@ -276,6 +290,10 @@ void MOTBeginRedoRecovery() void MOTEndRedoRecovery() { + if (!MOTAdaptor::m_initialized) { + return; + } + EnsureSafeThreadAccess(); MOT::MOTEngine::GetInstance()->DestroyRecoverySessionContext(); knl_thread_mot_init(); // reset all thread locals @@ -287,9 +305,13 @@ void MOTEndRedoRecovery() */ void InitMOT() { - JitExec::JitInitialize(); + if (MOTAdaptor::m_initialized) { + // MOT is already initialized, probably it's primary switch-over to standby. + return; + } + InitMOTHandler(); - t_thrd.mot_cxt.mot_startup = true; + JitExec::JitInitialize(); } /** @@ -297,11 +319,20 @@ void InitMOT() */ void TermMOT() { - MOTAdaptor::Fini(); + if (!MOTAdaptor::m_initialized) { + return; + } + + JitExec::JitDestroy(); + MOTAdaptor::Destroy(); } void MOTProcessRecoveredTransaction(uint64_t txid, bool isCommit) { + if (!MOTAdaptor::m_initialized) { + return; + } + if (MOT::MOTEngine::GetInstance()->IsInProcessTx(txid)) { elog(LOG, "MOTProcessRecoveredTransaction: %lu - %s", txid, isCommit ? "commit" : "abort"); MOT::TxnManager* mgr = GetSafeTxn(); @@ -1898,7 +1929,6 @@ static int MOTIsForeignRelationUpdatable(Relation rel) static void InitMOTHandler() { - MOTAdaptor::Fini(); MOTAdaptor::Init(); MOT::GetGlobalConfiguration().m_enableIncrementalCheckpoint = g_instance.attr.attr_storage.enableIncrementalCheckpoint; diff --git a/src/gausskernel/storage/mot/fdw_adapter/src/mot_internal.cpp b/src/gausskernel/storage/mot/fdw_adapter/src/mot_internal.cpp index 9b5a3dd9f..c40a12dc3 100644 --- a/src/gausskernel/storage/mot/fdw_adapter/src/mot_internal.cpp +++ b/src/gausskernel/storage/mot/fdw_adapter/src/mot_internal.cpp @@ -702,7 +702,8 @@ static void WakeupWalWriter() void MOTAdaptor::Init() { if (m_initialized) { - return; + // This is highly unexpected, and should especially be guarded in scenario of switch-over to standby. + elog(FATAL, "Double attempt to initialize MOT engine, it is already initialized"); } MOT::GetGlobalConfiguration().SetTotalMemoryMb(g_instance.attr.attr_memory.max_process_memory / KILO_BYTE); @@ -852,7 +853,7 @@ void MOTAdaptor::InitDataNodeId() MOT::GetGlobalConfiguration().SetPgNodes(1, 1); } -void MOTAdaptor::Fini() +void MOTAdaptor::Destroy() { if (!m_initialized) { return; diff --git a/src/gausskernel/storage/mot/fdw_adapter/src/mot_internal.h b/src/gausskernel/storage/mot/fdw_adapter/src/mot_internal.h index fe6fe6244..4c92b7b80 100644 --- a/src/gausskernel/storage/mot/fdw_adapter/src/mot_internal.h +++ b/src/gausskernel/storage/mot/fdw_adapter/src/mot_internal.h @@ -285,7 +285,7 @@ struct MOTFdwState_St { class MOTAdaptor { public: static void Init(); - static void Fini(); + static void Destroy(); static void NotifyConfigChange(); static void InitDataNodeId(); diff --git a/src/include/knl/knl_thread.h b/src/include/knl/knl_thread.h index 1ceff51bb..a823451ed 100644 --- a/src/include/knl/knl_thread.h +++ b/src/include/knl/knl_thread.h @@ -2693,7 +2693,6 @@ typedef struct knl_t_mot_context { // misc uint8_t log_level; bool init_codegen_once; - bool mot_startup; uint16_t currentThreadId; int currentNumaNodeId;