Skip to content

Commit b931751

Browse files
stchengpavel-shirshov
authored andcommitted
[teamsyncd]: Add retry logic in teamsyncd to avoid team handler init failure (sonic-net#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 <[email protected]>
1 parent fc085ee commit b931751

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

teamsyncd/teamsync.cpp

+48-24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "warm_restart.h"
1313
#include "teamsync.h"
1414

15+
#include <unistd.h>
16+
1517
using namespace std;
1618
using namespace std::chrono;
1719
using namespace swss;
@@ -203,32 +205,54 @@ TeamSync::TeamPortSync::TeamPortSync(const string &lagName, int ifindex,
203205
m_lagName(lagName),
204206
m_ifindex(ifindex)
205207
{
206-
m_team = team_alloc();
207-
if (!m_team)
208-
{
209-
SWSS_LOG_ERROR("Unable to allocated team socket");
210-
throw system_error(make_error_code(errc::address_not_available),
211-
"Unable to allocated team socket");
212-
}
213-
214-
int err = team_init(m_team, ifindex);
215-
if (err)
216-
{
217-
team_free(m_team);
218-
m_team = NULL;
219-
SWSS_LOG_ERROR("Unable to init team socket");
220-
throw system_error(make_error_code(errc::address_not_available),
221-
"Unable to init team socket");
222-
}
208+
int count = 0;
209+
int max_retries = 3;
223210

224-
err = team_change_handler_register(m_team, &gPortChangeHandler, this);
225-
if (err)
211+
while (true)
226212
{
227-
team_free(m_team);
228-
m_team = NULL;
229-
SWSS_LOG_ERROR("Unable to register port change event");
230-
throw system_error(make_error_code(errc::address_not_available),
231-
"Unable to register port change event");
213+
try
214+
{
215+
m_team = team_alloc();
216+
if (!m_team)
217+
{
218+
throw system_error(make_error_code(errc::address_not_available),
219+
"Unable to allocate team socket");
220+
}
221+
222+
int err = team_init(m_team, ifindex);
223+
if (err)
224+
{
225+
team_free(m_team);
226+
m_team = NULL;
227+
throw system_error(make_error_code(errc::address_not_available),
228+
"Unable to initialize team socket");
229+
}
230+
231+
err = team_change_handler_register(m_team, &gPortChangeHandler, this);
232+
if (err)
233+
{
234+
team_free(m_team);
235+
m_team = NULL;
236+
throw system_error(make_error_code(errc::address_not_available),
237+
"Unable to register port change event");
238+
}
239+
240+
break;
241+
}
242+
catch (const system_error& e)
243+
{
244+
if (++count == max_retries)
245+
{
246+
throw;
247+
}
248+
else
249+
{
250+
SWSS_LOG_WARN("Failed to initialize team handler. LAG=%s error=%d:%s, attempt=%d",
251+
lagName.c_str(), e.code().value(), e.what(), count);
252+
}
253+
254+
sleep(1);
255+
}
232256
}
233257

234258
/* Sync LAG at first */

0 commit comments

Comments
 (0)