From b9317511bac3de0f55fa1a7ecdab5742d6e4eabb Mon Sep 17 00:00:00 2001 From: Shuotian Cheng Date: Tue, 26 Nov 2019 18:26:14 -0800 Subject: [PATCH] [teamsyncd]: Add retry logic in teamsyncd to avoid team handler init failure (#854) * [teamsyncd]: Add retry logic in teamsyncd to avoid team handler init failure team_alloc and team_init fail occasionally when they start the same time as teamd instances. Add the retry logic to avoid such cases. Signed-off-by: Shu0T1an ChenG --- teamsyncd/teamsync.cpp | 72 ++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/teamsyncd/teamsync.cpp b/teamsyncd/teamsync.cpp index a194f5dd9c27..64016ff55715 100644 --- a/teamsyncd/teamsync.cpp +++ b/teamsyncd/teamsync.cpp @@ -12,6 +12,8 @@ #include "warm_restart.h" #include "teamsync.h" +#include + using namespace std; using namespace std::chrono; using namespace swss; @@ -203,32 +205,54 @@ TeamSync::TeamPortSync::TeamPortSync(const string &lagName, int ifindex, m_lagName(lagName), m_ifindex(ifindex) { - m_team = team_alloc(); - if (!m_team) - { - SWSS_LOG_ERROR("Unable to allocated team socket"); - throw system_error(make_error_code(errc::address_not_available), - "Unable to allocated team socket"); - } - - int err = team_init(m_team, ifindex); - if (err) - { - team_free(m_team); - m_team = NULL; - SWSS_LOG_ERROR("Unable to init team socket"); - throw system_error(make_error_code(errc::address_not_available), - "Unable to init team socket"); - } + int count = 0; + int max_retries = 3; - err = team_change_handler_register(m_team, &gPortChangeHandler, this); - if (err) + while (true) { - team_free(m_team); - m_team = NULL; - SWSS_LOG_ERROR("Unable to register port change event"); - throw system_error(make_error_code(errc::address_not_available), - "Unable to register port change event"); + try + { + m_team = team_alloc(); + if (!m_team) + { + throw system_error(make_error_code(errc::address_not_available), + "Unable to allocate team socket"); + } + + int err = team_init(m_team, ifindex); + if (err) + { + team_free(m_team); + m_team = NULL; + throw system_error(make_error_code(errc::address_not_available), + "Unable to initialize team socket"); + } + + err = team_change_handler_register(m_team, &gPortChangeHandler, this); + if (err) + { + team_free(m_team); + m_team = NULL; + throw system_error(make_error_code(errc::address_not_available), + "Unable to register port change event"); + } + + break; + } + catch (const system_error& e) + { + if (++count == max_retries) + { + throw; + } + else + { + SWSS_LOG_WARN("Failed to initialize team handler. LAG=%s error=%d:%s, attempt=%d", + lagName.c_str(), e.code().value(), e.what(), count); + } + + sleep(1); + } } /* Sync LAG at first */