Skip to content

Commit

Permalink
use swss/warm_start.h
Browse files Browse the repository at this point in the history
  • Loading branch information
zjswhhh committed Jul 8, 2022
1 parent 027b446 commit 459adb4
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 111 deletions.
111 changes: 46 additions & 65 deletions src/DbInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,52 @@ void DbInterface::getSoCIpAddress(std::shared_ptr<swss::DBConnector> 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<swss::DBConnector> configDbPtr = std::make_shared<swss::DBConnector> ("CONFIG_DB", 0);
std::shared_ptr<swss::Table> configDbMuxCableTablePtr = std::make_shared<swss::Table> (
configDbPtr.get(), CFG_MUX_CABLE_TABLE_NAME
);
configDbMuxCableTablePtr->hset(portName, "state", state);
}

//
// ---> processMuxPortConfigNotifiction(std::deque<swss::KeyOpFieldsValuesTuple> &entries);
//
Expand Down Expand Up @@ -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<swss::DBConnector> 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<swss::Table> stateDbWarmRestartTablePtr = std::make_shared<swss::Table> (
mStateDbPtr.get(), WARM_RESTART_TABLE_NAME
);
stateDbWarmRestartTablePtr->hset("linkmgrd", "state", "reconciled");
}

//
// ---> setMuxMode
//
Expand Down
24 changes: 3 additions & 21 deletions src/DbInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -407,15 +398,6 @@ class DbInterface
const uint64_t expectedPacketCount
);

/**
* @method handleSetMuxReconciled
*
* @brief handle set linkmgrd reconciled
*
* @return none
*/
virtual void handleSetMuxReconciled();

/**
* @method handleSetMuxMode
*
Expand Down
9 changes: 9 additions & 0 deletions src/LinkMgrdMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>

#include "swss/warm_restart.h"

#include "MuxManager.h"
#include "MuxPort.h"
#include "common/MuxConfig.h"
Expand Down Expand Up @@ -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<mux::MuxManager> muxManagerPtr = std::make_shared<mux::MuxManager> ();
muxManagerPtr->initialize(measureSwitchover, defaultRoute);
muxManagerPtr->run();
Expand Down
17 changes: 14 additions & 3 deletions src/MuxManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include <boost/bind/bind.hpp>

#include "swss/warm_restart.h"

#include "common/MuxException.h"
#include "common/MuxLogger.h"
#include "MuxManager.h"
Expand Down Expand Up @@ -78,13 +80,19 @@ 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(
boost::bind(&boost::asio::io_service::run, &mIoService)
);
}

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);
Expand Down Expand Up @@ -471,6 +479,7 @@ void MuxManager::handleProcessTerminate()
mDbInterfacePtr->getBarrier().wait();
}

<<<<<<< HEAD
//
// ---> generateServerMac();
//
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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 */
51 changes: 33 additions & 18 deletions src/MuxManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -523,6 +505,38 @@ class MuxManager
*/
void setDbInterfacePtr(std::shared_ptr<mux::DbInterface> 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;

Expand All @@ -533,6 +547,7 @@ class MuxManager

boost::asio::io_service::strand mStrand;
boost::asio::deadline_timer mReconciliationTimer;
uint16_t mPortReconciliationCount = 0;

std::shared_ptr<mux::DbInterface> mDbInterfacePtr;

Expand Down
14 changes: 14 additions & 0 deletions src/MuxPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>

#include "swss/warm_restart.h"

#include "MuxPort.h"
#include "common/MuxException.h"
#include "common/MuxLogger.h"
Expand Down Expand Up @@ -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 */
6 changes: 3 additions & 3 deletions src/MuxPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,13 @@ class MuxPort: public std::enable_shared_from_this<MuxPort>
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;
Expand Down
2 changes: 2 additions & 0 deletions src/common/MuxConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/link_manager/LinkManagerStateMachineActiveStandby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ void ActiveStandbyStateMachine::activateStateMachine()

updateMuxLinkmgrState();

mMuxPortPtr->checkWarmRestart();
mMuxPortPtr->warmRestartReconciliation();
}
}

Expand Down

0 comments on commit 459adb4

Please sign in to comment.