Skip to content

[Link Event Damping] Add tracker to track the selectable timers used by link event damper#1323

Draft
Ashish1805 wants to merge 1 commit into
sonic-net:masterfrom
Ashish1805:ledamping_seh
Draft

[Link Event Damping] Add tracker to track the selectable timers used by link event damper#1323
Ashish1805 wants to merge 1 commit into
sonic-net:masterfrom
Ashish1805:ledamping_seh

Conversation

@Ashish1805

Copy link
Copy Markdown
Contributor
  • Link event damper running on each port will have its own SelectableTimer. This class helps create a mapping of SelectableTimer and object that created the SelectableTimer so that when timer is fired, proper EventHandler object can be invoked.

HLD: sonic-net/SONiC#1071

@Ashish1805

Copy link
Copy Markdown
Contributor Author

Hi @kcudnik CodeQL / Analyze (cpp) (pull_request_target) check is failing.

/usr/bin/ld: ../../syncd/libSyncd.a(libSyncd_a-PortStateChangeHandler.o): in function `syncd::PortStateChangeHandler::PortStateChangeHandler(std::shared_ptr<swss::SelectableEvent>)':
/home/runner/work/sonic-sairedis/sonic-sairedis/syncd/PortStateChangeHandler.cpp:21: undefined reference to `syncd::PortStateChangeHandler::PORT_STATE_CHANGE_QUEUE_SIZE'
collect2: error: ld returned 1 exit status

PORT_STATE_CHANGE_QUEUE_SIZE is defined as static and constexpr class data member (https://github.com/sonic-net/sonic-sairedis/blob/master/syncd/PortStateChangeHandler.h#L43). I see sairedis repo uses c++14, so it is necessary to also provide definition for this static data member in .cc file. Like:

// Defined in PortStateChangeHandler.cc
constexpr size_t PortStateChangeHandler::PORT_STATE_CHANGE_QUEUE_SIZE;

PortStateChangeHandler.cpp was added in #1310 PR and CodeQL / Analyze (cpp) (pull_request_target) check passed in that PR: https://github.com/sonic-net/sonic-sairedis/actions/runs/6885079249. Not sure why it didn't fail during CodeQL check of that PR.

I am not sure what is the protocol here: do we add fix PR for this or revert the culprit PR #1310 and merge it again with suggested changes?

@kcudnik

kcudnik commented Nov 18, 2023

Copy link
Copy Markdown
Collaborator

Hi @kcudnik CodeQL / Analyze (cpp) (pull_request_target) check is failing.

/usr/bin/ld: ../../syncd/libSyncd.a(libSyncd_a-PortStateChangeHandler.o): in function `syncd::PortStateChangeHandler::PortStateChangeHandler(std::shared_ptr<swss::SelectableEvent>)':
/home/runner/work/sonic-sairedis/sonic-sairedis/syncd/PortStateChangeHandler.cpp:21: undefined reference to `syncd::PortStateChangeHandler::PORT_STATE_CHANGE_QUEUE_SIZE'
collect2: error: ld returned 1 exit status

PORT_STATE_CHANGE_QUEUE_SIZE is defined as static and constexpr class data member (https://github.com/sonic-net/sonic-sairedis/blob/master/syncd/PortStateChangeHandler.h#L43). I see sairedis repo uses c++14, so it is necessary to also provide definition for this static data member in .cc file. Like:

// Defined in PortStateChangeHandler.cc
constexpr size_t PortStateChangeHandler::PORT_STATE_CHANGE_QUEUE_SIZE;

PortStateChangeHandler.cpp was added in #1310 PR and CodeQL / Analyze (cpp) (pull_request_target) check passed in that PR: https://github.com/sonic-net/sonic-sairedis/actions/runs/6885079249. Not sure why it didn't fail during CodeQL check of that PR.

I am not sure what is the protocol here: do we add fix PR for this or revert the culprit PR #1310 and merge it again with suggested changes?

SAME ISSUE CAUSES SWSS ERROR HERE: #1310 (comment)

please figure out why this is causing build error or revert the change !

Comment thread syncd/SelectablesTracker.h
Comment on lines +29 to +49
virtual bool addSelectableToTracker(
_In_ swss::Selectable *selectable,
_In_ SelectableEventHandler *eventHandler);

// Removes a Selectable from the map.
virtual bool removeSelectableFromTracker(
_In_ swss::Selectable *selectable);

// Checks if Selectable is present in the tracker map.
virtual bool selectableIsTracked(
_In_ swss::Selectable *selectable);

// Gets the EventHandler for a Selectable.
virtual SelectableEventHandler *getEventHandlerForSelectable(
_In_ swss::Selectable *selectable);

private:

using SelectableFdToEventHandlerMap = std::unordered_map<int, SelectableEventHandler *>;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use shared_ptr instead of pointers

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like w should be using shared_ptr here. Do you use any benefit of using shared_ptr?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, benefit is to not lose trakc of pointers and prevent memory leak, please use shared_ptr, as is used in entire syncd project

@kcudnik kcudnik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address comments

@kcudnik

kcudnik commented Nov 18, 2023

Copy link
Copy Markdown
Collaborator

change

static constexpr size_t PortStateChangeHandler::PORT_STATE_CHANGE_QUEUE_SIZE;

to

constexpr size_t PortStateChangeHandler::PORT_STATE_CHANGE_QUEUE_SIZE;

first keyword static requires declaration in cpp file since it's considered a field

link event damper.

- Link event damper running on each port will have its own
  SelectableTimer. This class helps create a mapping of SelectableTimer
  and object that created the SelectableTimer so that when timer is
  fired, proper EventHandler object can be invoked.

HLD: sonic-net/SONiC#1071
@Ashish1805

Copy link
Copy Markdown
Contributor Author

Adding @Junchao-Mellanox for review.

}

int fd = selectable->getFd();
if (eventHandler == nullptr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the check before line 20

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

althoug he wanted to log fd number in error message, anyway please add empty line before if()

@DendroLabs

Copy link
Copy Markdown
Contributor

Hi @Ashish1805 and @kcudnik,

This PR has been inactive for nearly 2 years. I'm picking up the link event damping feature and plan to open a new PR that supersedes this one, addressing all outstanding review comments:

  • Move class members to private
  • Replace raw pointers with shared_ptr (consistent with syncd codebase)
  • Fix code ordering per @Junchao-Mellanox's feedback
  • Add constexpr out-of-line definition for PORT_STATE_CHANGE_QUEUE_SIZE (fixes CodeQL build)
  • Rebase onto current master

Will credit @Ashish1805 as original author. See also my comment on #1334 regarding the overall design clarification.

DendroLabs added a commit to DendroLabs/sonic-sairedis that referenced this pull request Mar 17, 2026
…king

Supersedes PR sonic-net#1323 by @Ashish1805. Adds SelectablesTracker class that
tracks Selectable objects and their corresponding EventHandler mappings,
used by the link event damper feature.

Changes from original PR addressing review feedback:
- Move non-copyable/non-movable declarations to private section (@kcudnik)
- Replace raw pointers with shared_ptr for EventHandler to prevent memory
  leaks, consistent with syncd project conventions (@kcudnik)
- Reorder null checks: check eventHandler before accessing selectable fd
  (@Junchao-Mellanox)
- Add empty line before if() for consistent formatting (@kcudnik)

Co-Authored-By: Ashish Singh <ashish.singh@google.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
DendroLabs added a commit to DendroLabs/sonic-sairedis that referenced this pull request Mar 17, 2026
…king

Supersedes PR sonic-net#1323 by @Ashish1805. Adds SelectablesTracker class that
tracks Selectable objects and their corresponding EventHandler mappings,
used by the link event damper feature.

Changes from original PR addressing review feedback:
- Move non-copyable/non-movable declarations to private section (@kcudnik)
- Replace raw pointers with shared_ptr for EventHandler to prevent memory
  leaks, consistent with syncd project conventions (@kcudnik)
- Reorder null checks: check eventHandler before accessing selectable fd
  (@Junchao-Mellanox)
- Add empty line before if() for consistent formatting (@kcudnik)

Signed-off-by: DendroLabs <info@dendrolabs.com>
lolyu pushed a commit that referenced this pull request Jun 6, 2026
Summary
Supersedes #1323 by @Ashish1805. Adds the SelectablesTracker class for tracking selectable timers used by the link event damper in syncd.

This is part of the Link Event Damping feature (HLD: sonic-net/SONiC#1071).

All review feedback from #1323 has been addressed:

Moved class members to private (@kcudnik)
Replaced raw pointers with shared_ptr for memory safety (@kcudnik)
Fixed code ordering -- null checks before getFd() call (@Junchao-Mellanox)
Added empty line before if() blocks (@kcudnik)
CodeQL constexpr fix already present on master
Changes:

syncd/SelectablesTracker.h -- Header with private members, shared_ptr interface
syncd/SelectablesTracker.cpp -- Implementation with proper null checking order
unittest/syncd/TestSelectablesTracker.cpp -- 11 unit test cases
unittest/syncd/MockSelectablesTracker.h -- Mock class in separate file
Build integration in both syncd/Makefile.am and unittest/syncd/Makefile.am
Dependency chain: This PR is a prerequisite for the per-port link event damper (#1334) and the libsai RedisInterface (#1331).

Test plan
 11 new unit tests covering add/remove/get operations, null handling, duplicate detection
 Existing syncd unit tests pass (no regressions)
 Azure Pipelines CI green
🤖 Generated with Claude Code

Co-Authored-By: Ashish Singh ashish.singh@google.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants