Skip to content

Commit

Permalink
Add handlers for pck loss updates
Browse files Browse the repository at this point in the history
  • Loading branch information
zjswhhh committed Jan 24, 2022
1 parent 9967c0b commit 4a2405c
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 14 deletions.
49 changes: 47 additions & 2 deletions src/DbInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ constexpr auto DEFAULT_TIMEOUT_MSEC = 1000;
std::vector<std::string> DbInterface::mMuxState = {"active", "standby", "unknown", "Error"};
std::vector<std::string> DbInterface::mMuxLinkmgrState = {"uninitialized", "unhealthy", "healthy"};
std::vector<std::string> DbInterface::mMuxMetrics = {"start", "end"};
std::vector<std::string> DbInterface::mLinkProbeMetrics = {"pck_loss_start", "pck_loss_end"};
std::vector<std::string> DbInterface::mLinkProbeMetrics = {"link_prober_unknown_start", "link_prober_unknown_end"};

//
// ---> DbInterface(mux::MuxManager *muxManager);
Expand Down Expand Up @@ -189,6 +189,31 @@ void DbInterface::postLinkProberMetricsEvent(
)));
}

//
// ---> postPckLossRatio(
// const std::string &portName,
// const double_t ratio
// );
// post pck loss ratio update to state db
void DbInterface::postPckLossRatio(
const std::string &portName,
const double_t ratio
)
{
MUXLOGDEBUG(boost::format("%s: posting pck loss ratio: %.2f %%") %
portName %
ratio
);

boost::asio::io_service &ioService = mStrand.context();
ioService.post(mStrand.wrap(boost::bind(
&DbInterface::handlePostPckLossRatio,
this,
portName,
ratio
)));
}

//
// ---> initialize();
//
Expand Down Expand Up @@ -369,14 +394,34 @@ void DbInterface::handlePostLinkProberMetrics(
mLinkProbeMetrics[static_cast<int> (metrics)]
);

if (metrics == link_manager::LinkManagerStateMachine::LinkProberMetrics::PckLossStart) {
if (metrics == link_manager::LinkManagerStateMachine::LinkProberMetrics::LinkProberUnknownStart) {
mStateDbLinkProbeStatsTablePtr.hdel(portName, mLinkProbeMetrics[0]);
mStateDbLinkProbeStatsTablePtr.hdel(portName, mLinkProbeMetrics[1]);
}

mStateDbLinkProbeStatsTablePtr.hset(portName, mLinkProbeMetrics[static_cast<int> (metrics)], boost::posix_time::to_simple_string(time));
}

//
// ---> handlePostPckLossRatio(
// const std::string portName,
// const double_t ratio
// );
//
// handle post pck loss ratio
void DbInterface::handlePostPckLossRatio(
const std::string portName,
const double_t ratio
)
{
MUXLOGDEBUG(boost::format("%s: posting pck loss ratio: %.2f %%") %
portName %
ratio
);

mStateDbLinkProbeStatsTablePtr.hset(portName, "pck_loss_ratio", std::to_string(ratio));
}

//
// ---> processTorMacAddress(std::string& mac);
//
Expand Down
29 changes: 29 additions & 0 deletions src/DbInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ class DbInterface
link_manager::LinkManagerStateMachine::LinkProberMetrics metrics
);

/**
* @method postPckLossRatio
*
* @brief post pck loss ratio update to state db
*
* @param portName (in) port name
* @param ratio (in) pck loss ratio
*
* @return none
*/
virtual void postPckLossRatio(
const std::string &portName,
const double_t ratio
);

/**
*@method initialize
Expand Down Expand Up @@ -313,6 +327,21 @@ class DbInterface
boost::posix_time::ptime time
);

/**
* @method handlePostPckLossRatio
*
* @brief handle post pck loss ratio update
*
* @param portName (in) port name
* @param ratio (in) pck loss ratio
*
* @return none
*/
void handlePostPckLossRatio(
const std::string portName,
const double_t ratio
);

/**
*@method processTorMacAddress
*
Expand Down
13 changes: 13 additions & 0 deletions src/MuxPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ class MuxPort: public std::enable_shared_from_this<MuxPort>
mDbInterfacePtr->postLinkProberMetricsEvent(mMuxPortConfig.getPortName(), metrics);
};

/**
* @method postPckLossRatio
*
* @brief post pck loss ratio update to state db
*
* @param ratio (in) pck loss ratio
*
* @return none
*/
inline void postPckLossRatio(const double_t ratio) {
mDbInterfacePtr->postPckLossRatio(mMuxPortConfig.getPortName(), ratio);
};

/**
*@method setServerIpv4Address
*
Expand Down
9 changes: 8 additions & 1 deletion src/link_manager/LinkManagerStateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ void LinkManagerStateMachine::activateStateMachine()
//
// ---> handleStateChange(LinkProberEvent &event, link_prober::LinkProberState::Label state);
//
// handles LinkProverEvent
// handles LinkProberEvent
//
void LinkManagerStateMachine::handleStateChange(LinkProberEvent &event, link_prober::LinkProberState::Label state)
{
Expand All @@ -468,6 +468,13 @@ void LinkManagerStateMachine::handleStateChange(LinkProberEvent &event, link_pro
mLinkProberStateName[state]
);

// update state db link prober metrics to collect pck loss data
if (ps(mCompositeState) == link_prober::LinkProberState::Unknown && state != link_prober::LinkProberState::Unknown) {
mMuxPortPtr->postLinkProberMetricsEvent(link_manager::LinkManagerStateMachine::LinkProberMetrics::LinkProberUnknownEnd);
} else if (state == link_prober::LinkProberState::Label::Unknown) {
mMuxPortPtr->postLinkProberMetricsEvent(link_manager::LinkManagerStateMachine::LinkProberMetrics::LinkProberUnknownStart);
}

CompositeState nextState = mCompositeState;
ps(nextState) = state;
mStateTransitionHandler[ps(nextState)][ms(nextState)][ls(nextState)](this, nextState);
Expand Down
17 changes: 15 additions & 2 deletions src/link_manager/LinkManagerStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class LinkManagerStateMachine: public common::StateMachine,
};

enum class LinkProberMetrics {
PckLossStart,
PckLossEnd,
LinkProberUnknownStart,
LinkProberUnknownEnd,

Count
};
Expand Down Expand Up @@ -401,6 +401,19 @@ class LinkManagerStateMachine: public common::StateMachine,
*/
void handleGetServerMacAddressNotification(std::array<uint8_t, ETHER_ADDR_LEN> address);

/**
* @method handlePostPckLossRatio
*
* @brief handle get post pck loss ratio
*
* @param ratio (in) pck loss ratio
*
* @return none
*/
inline void handlePostPckLossRatio(const double_t ratio) {
mMuxPortPtr->postPckLossRatio(ratio);
};

/**
*@method handleGetMuxStateNotification
*
Expand Down
12 changes: 4 additions & 8 deletions src/link_prober/LinkProber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ void LinkProber::handleRecv(
// echo reply for an echo request generated by this/active ToR
mRxSelfSeqNo = mTxSeqNo;
mLinkProberStateMachine.postLinkProberStateEvent(LinkProberStateMachine::getIcmpSelfEvent());
mContinuousIcmpUnknownEventCount = 0;
} else {
mRxPeerSeqNo = mTxSeqNo;
mLinkProberStateMachine.postLinkProberStateEvent(LinkProberStateMachine::getIcmpPeerEvent());
Expand Down Expand Up @@ -440,14 +439,12 @@ void LinkProber::handleTimeout(boost::system::error_code errorCode)
if (mTxSeqNo != mRxSelfSeqNo && mTxSeqNo != mRxPeerSeqNo) {
// post unknown event
mLinkProberStateMachine.postLinkProberStateEvent(LinkProberStateMachine::getIcmpUnknownEvent());
mContinuousIcmpUnknownEventCount++;
mIcmpUnknownEventCount++;
}

MUXLOGINFO(boost::format("%s: continusous ICMP Unknown event count: %d, pck loss rate since initialization: %.2f %%") %
mMuxPortConfig.getPortName() %
mContinuousIcmpUnknownEventCount %
(static_cast<double_t>(mIcmpUnknownEventCount) / mIcmpPacketCount * 100)
);
mIcmpPacketCount++;
if (mIcmpPacketCount % mMuxPortConfig.getNegativeStateChangeRetryCount() == 0) {
mLinkProberStateMachine.postPckLossRatio(static_cast<double_t>(mIcmpUnknownEventCount) / mIcmpPacketCount);
}

// start another cycle of send/recv
Expand Down Expand Up @@ -487,7 +484,6 @@ void LinkProber::startRecv()
{
MUXLOGTRACE(mMuxPortConfig.getPortName());

mIcmpPacketCount++;

mStream.async_read_some(
boost::asio::buffer(mRxBuffer, MUX_MAX_ICMP_BUFFER_SIZE),
Expand Down
1 change: 0 additions & 1 deletion src/link_prober/LinkProber.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ class LinkProber

bool mSuspendTx = false;

uint32_t mContinuousIcmpUnknownEventCount = 0;
uint64_t mIcmpUnknownEventCount = 0;
uint64_t mIcmpPacketCount = 0;
};
Expand Down
16 changes: 16 additions & 0 deletions src/link_prober/LinkProberStateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,20 @@ void LinkProberStateMachine::handleMackAddressUpdate(const std::array<uint8_t, E
)));
}

//
// ---> postPckLossRatio(const double_t ratio);
//
// post pck loss ratio update to link manager
//
void LinkProberStateMachine::postPckLossRatio(const double_t ratio)
{
boost::asio::io_service::strand &strand = mLinkManagerStateMachine.getStrand();
boost::asio::io_service &ioService = strand.context();
ioService.post(strand.wrap(boost::bind(
&link_manager::LinkManagerStateMachine::handlePostPckLossRatio,
&mLinkManagerStateMachine,
ratio
)));
}

} /* namespace link_prober */
11 changes: 11 additions & 0 deletions src/link_prober/LinkProberStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ class LinkProberStateMachine: public common::StateMachine
*/
static SwitchActiveRequestEvent& getSwitchActiveRequestEvent() {return mSwitchActiveRequestEvent;};

/**
* @method postPckLossRatio
*
* @brief post pck loss ratio update to link manager
*
* @param ratio (in) pck loss ratio
*
* @return none
*/
void postPckLossRatio(const double_t ratio);

private:
/**
*@method postLinkManagerEvent
Expand Down

0 comments on commit 4a2405c

Please sign in to comment.