Skip to content

Commit

Permalink
[teamsyncd]: Add retry logic in teamsyncd to avoid team handler init …
Browse files Browse the repository at this point in the history
…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 <shuche@microsoft.com>
  • Loading branch information
stcheng authored and pavel-shirshov committed Nov 27, 2019
1 parent fc085ee commit b931751
Showing 1 changed file with 48 additions and 24 deletions.
72 changes: 48 additions & 24 deletions teamsyncd/teamsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "warm_restart.h"
#include "teamsync.h"

#include <unistd.h>

using namespace std;
using namespace std::chrono;
using namespace swss;
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit b931751

Please sign in to comment.