From 459adb404c5cc14be3c9ed53b87a3ea927717dc1 Mon Sep 17 00:00:00 2001 From: zhangjing Date: Fri, 8 Jul 2022 20:00:25 +0000 Subject: [PATCH] use swss/warm_start.h --- src/DbInterface.cpp | 111 ++++++++---------- src/DbInterface.h | 24 +--- src/LinkMgrdMain.cpp | 9 ++ src/MuxManager.cpp | 17 ++- src/MuxManager.h | 51 +++++--- src/MuxPort.cpp | 14 +++ src/MuxPort.h | 6 +- src/common/MuxConfig.h | 2 + .../LinkManagerStateMachineActiveStandby.cpp | 2 +- 9 files changed, 125 insertions(+), 111 deletions(-) diff --git a/src/DbInterface.cpp b/src/DbInterface.cpp index 5914fba8..6a08c8ed 100644 --- a/src/DbInterface.cpp +++ b/src/DbInterface.cpp @@ -723,6 +723,52 @@ void DbInterface::getSoCIpAddress(std::shared_ptr configDbCon processSoCIpAddress(entries); } +// ---> warmRestartReconciliation(const std::string &portName); +// +// port warm restart reconciliation procedure +// +void DbInterface::warmRestartReconciliation(const std::string &portName) +{ + MUXLOGDEBUG(portName); + + setMuxMode(portName, "auto"); + mMuxManagerPtr->updateWarmRestartReconciliationCount(-1); +} + +// +// ---> setMuxMode +// +// set config db mux mode +// +void DbInterface::setMuxMode(const std::string &portName, const std::string state) +{ + MUXLOGDEBUG(portName); + + boost::asio::io_service &ioService = mStrand.context(); + ioService.post(mStrand.wrap(boost::bind( + &DbInterface::handleSetMuxMode, + this, + portName, + state + ))); +} + +// +// ---> handleSetMuxmode +// +// handle set mux mode +// +void DbInterface::handleSetMuxMode(const std::string &portName, const std::string state) +{ + MUXLOGWARNING(boost::format("%s: configuring mux mode to %s after warm restart") % portName % state); + + std::shared_ptr configDbPtr = std::make_shared ("CONFIG_DB", 0); + std::shared_ptr configDbMuxCableTablePtr = std::make_shared ( + configDbPtr.get(), CFG_MUX_CABLE_TABLE_NAME + ); + configDbMuxCableTablePtr->hset(portName, "state", state); +} + // // ---> processMuxPortConfigNotifiction(std::deque &entries); // @@ -1145,71 +1191,6 @@ void DbInterface::handleDefaultRouteStateNotification(swss::SubscriberStateTable processDefaultRouteStateNotification(entries); } -// -// ---> getWarmRestartFlag(); -// -// get flag to check if system is in warm reboot context -// -void DbInterface::getWarmRestartFlag(std::shared_ptr stateDbConnector) -{ - MUXLOGINFO("Reading Warm Restart Flag"); - - swss::Table stateDbWarmRestartEnableTable(stateDbConnector.get(), WARM_RESTART_ENABLE_TABLE_NAME); - const std::string key = "system"; - const std::string field = "enable"; - std::string flag; - - if (stateDbWarmRestartEnableTable.hget(key, field, flag)) { - mMuxManagerPtr->processWarmRestartFlag(flag); - } else { - MUXLOGINFO("Warm restart flag is not found"); - } -} - -// -// ---> checkWarmRestart(const std::string portName); -// -// check if warm restart is on when port completes reconciliation -// -void DbInterface::checkWarmRestart(const std::string &portName) -{ - MUXLOGDEBUG(portName); - - if (mMuxManagerPtr->getWarmRestartFlag()) { - setMuxMode(portName, "auto"); - mMuxManagerPtr->updateWarmRestartReconciliationCount(-1); - } -} - -// -// ---> setMuxReconciled -// -// set warm reboot linkmgrd table entry to reconciled -// -void DbInterface::setMuxReconciled() -{ - boost::asio::io_service &ioService = mStrand.context(); - ioService.post(mStrand.wrap(boost::bind( - &DbInterface::handleSetMuxReconciled, - this - ))); -} - -// -// ---> handleSetMuxReconciled -// -// handle set linkmgrd reconciled -// -void DbInterface::handleSetMuxReconciled() -{ - MUXLOGWARNING("Setting linkmgrd as reconciled in state db now"); - - std::shared_ptr stateDbWarmRestartTablePtr = std::make_shared ( - mStateDbPtr.get(), WARM_RESTART_TABLE_NAME - ); - stateDbWarmRestartTablePtr->hset("linkmgrd", "state", "reconciled"); -} - // // ---> setMuxMode // diff --git a/src/DbInterface.h b/src/DbInterface.h index 0922a6ef..fe93f430 100644 --- a/src/DbInterface.h +++ b/src/DbInterface.h @@ -274,24 +274,15 @@ class DbInterface void setMuxMode(const std::string &portName, const std::string state); /** - * @method setMuxReconciled + * @method warmRestartReconciliation * - * @brief set warm reboot linkmgrd table entry to reconciled - * - * @return none - */ - void setMuxReconciled(); - - /** - * @method checkWarmRestart - * - * @brief check if warm restart is on when port completes reconciliation + * @brief port warm restart reconciliation procedure * * @param portName(in) Mux port name * * @return none */ - virtual void checkWarmRestart(const std::string &portName); + virtual void warmRestartReconciliation(const std::string &portName); private: friend class test::MuxManagerTest; @@ -407,15 +398,6 @@ class DbInterface const uint64_t expectedPacketCount ); - /** - * @method handleSetMuxReconciled - * - * @brief handle set linkmgrd reconciled - * - * @return none - */ - virtual void handleSetMuxReconciled(); - /** * @method handleSetMuxMode * diff --git a/src/LinkMgrdMain.cpp b/src/LinkMgrdMain.cpp index dbf8f429..9d070f2a 100644 --- a/src/LinkMgrdMain.cpp +++ b/src/LinkMgrdMain.cpp @@ -26,6 +26,8 @@ #include #include +#include "swss/warm_restart.h" + #include "MuxManager.h" #include "MuxPort.h" #include "common/MuxConfig.h" @@ -123,6 +125,13 @@ int main(int argc, const char* argv[]) // initialize static data link_prober::IcmpPayload::generateGuid(); + // warm restart static + swss::WarmStart::initialize("linkmgrd", "mux"); + swss::WarmStart::checkWarmStart("linkmgrd", "mux"); + if (swss::WarmStart::isWarmStart()) { + swss::WarmStart::setWarmStartState("linkmgrd", swss::WarmStart::INITIALIZED); + } + std::shared_ptr muxManagerPtr = std::make_shared (); muxManagerPtr->initialize(measureSwitchover, defaultRoute); muxManagerPtr->run(); diff --git a/src/MuxManager.cpp b/src/MuxManager.cpp index 5f07e261..e5e879c0 100644 --- a/src/MuxManager.cpp +++ b/src/MuxManager.cpp @@ -28,6 +28,8 @@ #include +#include "swss/warm_restart.h" + #include "common/MuxException.h" #include "common/MuxLogger.h" #include "MuxManager.h" @@ -78,6 +80,7 @@ void MuxManager::setUseWellKnownMacActiveActive(bool useWellKnownMac) // void MuxManager::initialize(bool enable_feature_measurement, bool enable_feature_default_route) { + for (uint8_t i = 0; (mMuxConfig.getNumberOfThreads() > 2) && (i < mMuxConfig.getNumberOfThreads() - 2); i++) { mThreadGroup.create_thread( @@ -85,6 +88,11 @@ void MuxManager::initialize(bool enable_feature_measurement, bool enable_feature ); } + if (swss::WarmStart::isWarmStart()) { + MUXLOGINFO("Detected warm restart context, starting reconciliation timer."); + startWarmRestartReconciliationTimer(swss::WarmStart::getWarmStartTimer("linkmgrd", "mux")); + } + mDbInterfacePtr->initialize(); mMuxConfig.enableSwitchoverMeasurement(enable_feature_measurement); @@ -471,6 +479,7 @@ void MuxManager::handleProcessTerminate() mDbInterfacePtr->getBarrier().wait(); } +<<<<<<< HEAD // // ---> generateServerMac(); // @@ -504,6 +513,8 @@ void MuxManager::processWarmRestartFlag(const std::string &flag) } } +======= +>>>>>>> c8fdafd... use swss/warm_start.h // ---> updateWarmRestartReconciliationCount(int increment); // // update warm restart reconciliation count @@ -540,10 +551,10 @@ void MuxManager::handleUpdateReconciliationCount(int increment) // // start warm restart reconciliation timer // -void MuxManager::startWarmRestartReconciliationTimer() +void MuxManager::startWarmRestartReconciliationTimer(uint32_t timeout) { mReconciliationTimer.expires_from_now(boost::posix_time::seconds( - mMuxConfig.getMuxReconciliationTimeout_sec() + timeout == 0? mMuxConfig.getMuxReconciliationTimeout_sec():timeout )); mReconciliationTimer.async_wait(mStrand.wrap(boost::bind( &MuxManager::handleWarmRestartReconciliationTimeout, @@ -562,7 +573,7 @@ void MuxManager::handleWarmRestartReconciliationTimeout(const boost::system::err MUXLOGWARNING("Reconciliation timed out after warm restart, set service to reconciled now."); } - mDbInterfacePtr->setMuxReconciled(); + swss::WarmStart::setWarmStartState("linkmgrd", swss::WarmStart::RECONCILED); } } /* namespace mux */ diff --git a/src/MuxManager.h b/src/MuxManager.h index 3d38f2d0..cb184304 100644 --- a/src/MuxManager.h +++ b/src/MuxManager.h @@ -399,24 +399,6 @@ class MuxManager */ void addOrUpdateDefaultRouteState(bool is_v4, const std::string &routeState); - /** - * @method processWarmRestartFlag - * - * @brief process warm restart flag - * - * @return - */ - void processWarmRestartFlag(const std::string &flag); - - /** - * @method getWarmRestartFlag - * - * @brief getter for warm restart flag - * - * @return warm restart flag - */ - bool getWarmRestartFlag() {return mWarmRestartFlag == "true"? true:false;}; - /** * @method updateWarmRestartReconciliationCount * @@ -523,6 +505,38 @@ class MuxManager */ void setDbInterfacePtr(std::shared_ptr dbInterfacePtr) {mDbInterfacePtr = dbInterfacePtr;}; +private: + /** + * @method startWarmRestartReconciliationTimer + * + * @brief start warm restart reconciliation timer + * + * @return none + */ + void startWarmRestartReconciliationTimer(uint32_t timeout=0); + + /** + * @method handleWarmRestartReconciliationTimeout + * + * @brief handle warm restart reconciliationTimeout + * + * @param errorCode (in) Boost error code + * + * @return none + */ + void handleWarmRestartReconciliationTimeout(const boost::system::error_code errorCode); + + /** + * @method handleUpdateReconciliationCount + * + * @brief handler of updating reconciliation port count + * + * @param increment + * + * @return none + */ + void handleUpdateReconciliationCount(int increment); + private: common::MuxConfig mMuxConfig; @@ -533,6 +547,7 @@ class MuxManager boost::asio::io_service::strand mStrand; boost::asio::deadline_timer mReconciliationTimer; + uint16_t mPortReconciliationCount = 0; std::shared_ptr mDbInterfacePtr; diff --git a/src/MuxPort.cpp b/src/MuxPort.cpp index 1b2d2f11..8dd4acf2 100644 --- a/src/MuxPort.cpp +++ b/src/MuxPort.cpp @@ -36,6 +36,8 @@ #include #include +#include "swss/warm_restart.h" + #include "MuxPort.h" #include "common/MuxException.h" #include "common/MuxLogger.h" @@ -364,4 +366,16 @@ void MuxPort::resetPckLossCount() ))); } +// +// ---> warmRestartReconciliation(); +// +// brief port warm restart reconciliation procedure +// +void MuxPort::warmRestartReconciliation() +{ + if (swss::WarmStart::isWarmStart() && mMuxPortConfig.getMode() != common::MuxPortConfig::Mode::Auto) { + mDbInterfacePtr->warmRestartReconciliation(mMuxPortConfig.getPortName()); + } +} + } /* namespace mux */ diff --git a/src/MuxPort.h b/src/MuxPort.h index f0f07a67..f9f18e8a 100644 --- a/src/MuxPort.h +++ b/src/MuxPort.h @@ -380,13 +380,13 @@ class MuxPort: public std::enable_shared_from_this void resetPckLossCount(); /** - * @method checkWarmRestart + * @method warmRestartReconciliation * - * @brief check if warm restart was going on, if yes, set config db mux mode to auto + * @brief port warm restart reconciliation procedure * * @return none */ - void checkWarmRestart(){mDbInterfacePtr->checkWarmRestart(mMuxPortConfig.getPortName());}; + void warmRestartReconciliation(); protected: friend class test::MuxManagerTest; diff --git a/src/common/MuxConfig.h b/src/common/MuxConfig.h index 51c459f8..81cb7656 100644 --- a/src/common/MuxConfig.h +++ b/src/common/MuxConfig.h @@ -356,6 +356,8 @@ class MuxConfig bool mEnableSwitchoverMeasurement = false; uint32_t mDecreasedTimeoutIpv4_msec = 10; + uint32_t mMuxReconciliationTimeout_sec = 10; + bool mEnableDefaultRouteFeature = false; bool mUseWellKnownMacActiveActive = true; diff --git a/src/link_manager/LinkManagerStateMachineActiveStandby.cpp b/src/link_manager/LinkManagerStateMachineActiveStandby.cpp index 753704b4..d1b34fa2 100644 --- a/src/link_manager/LinkManagerStateMachineActiveStandby.cpp +++ b/src/link_manager/LinkManagerStateMachineActiveStandby.cpp @@ -427,7 +427,7 @@ void ActiveStandbyStateMachine::activateStateMachine() updateMuxLinkmgrState(); - mMuxPortPtr->checkWarmRestart(); + mMuxPortPtr->warmRestartReconciliation(); } }