Skip to content

Memory Statistics Config and Show Commands#3575

Merged
qiluo-msft merged 31 commits intosonic-net:masterfrom
kanza-latif:show-config
Feb 27, 2025
Merged

Memory Statistics Config and Show Commands#3575
qiluo-msft merged 31 commits intosonic-net:masterfrom
kanza-latif:show-config

Conversation

@kanza-latif
Copy link
Contributor

@kanza-latif kanza-latif commented Oct 14, 2024

What I did

Provided support for the configuration and show commands of memory statistics feature.

How I did it

I added a new config file config/memory_statistics.py for the configuration commands of memory statistics feature.
Added a show file show/memory_statistics.py for the show commands of memory statistics feature.
Added a test file tests/memory_statistics_test.py to test the config and show commands of memory statistics feature

How to verify it

Cli support for config and show file is available

@kanza-latif kanza-latif changed the title added show and config commands Memory Statistics Config and Show Commands Oct 16, 2024
@ridahanif96
Copy link
Contributor

/azpw run Azure.sonic-utilities

@kanza-latif
Copy link
Contributor Author

/azpw run

@mssonicbld
Copy link
Collaborator

/AzurePipelines run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ridahanif96
Copy link
Contributor

@qiluo-msft @xincunli-sonic @zbud-msft @FengPan-Frank We have linked the code PRs. Please help review the feature.
Target 202411, please help with expedite review.

@FengPan-Frank
Copy link
Contributor

pls fix pretest analysis failure. thanks

@kanza-latif
Copy link
Contributor Author

@FengPan-Frank I've resolved it, kindly review.

@FengPan-Frank
Copy link
Contributor

@FengPan-Frank I've resolved it, kindly review.

seems python test is failed. could you double check?

@kanza-latif
Copy link
Contributor Author

@FengPan-Frank there are some test cases failing but those are not related to memory statistics feature. The memory_statistics_test have passed, could you guide what should i do

@FengPan-Frank
Copy link
Contributor

@FengPan-Frank there are some test cases failing but those are not related to memory statistics feature. The memory_statistics_test have passed, could you guide what should i do
you can create dummy PR for verify if it's current pipeline issue or your code issue.

@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@zhangyanzhao
Copy link
Collaborator

Reviewers,
Can you please help to review this PR? Thanks.

xincunli-sonic
xincunli-sonic previously approved these changes Jan 21, 2025
click.echo(msg)
log_to_syslog(msg)
return True, None
except Exception as e:
Copy link
Contributor

@qiluo-msft qiluo-msft Jan 21, 2025

Choose a reason for hiding this comment

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

Exception

Could you except more specific exception type? #Closed

Choose a reason for hiding this comment

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

Thank you for your suggestion. I have updated the code to catch more specific exception types, such as KeyError, ValueError, and ConnectionError, instead of using a generic Exception.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the new code! however, I still see the "except Exception as e" branch. Is it really needed?

Copy link

@Arham-Nasir Arham-Nasir Feb 12, 2025

Choose a reason for hiding this comment

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

Thanks for the feedback! I have now removed the except Exception as e branch, and only specific exceptions (e.g., KeyError, ConnectionError, RuntimeError, OSError, ValueError) are handled.

the action and returns a tuple indicating whether the operation was successful.

Args:
status (str): The status to set for memory statistics ("true" or "false").
Copy link
Contributor

@qiluo-msft qiluo-msft Jan 21, 2025

Choose a reason for hiding this comment

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

str

Is it better to use boolean type? #Closed

Choose a reason for hiding this comment

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

Thank you for your suggestion. I have modified the code to use a boolean type instead of a string when setting the memory statistics status. This ensures better type safety and improves code readability.

click.echo(success_msg)
log_to_syslog(success_msg)
click.echo("Reminder: Please run 'config save' to persist changes.")
except Exception as e:
Copy link
Contributor

@qiluo-msft qiluo-msft Jan 21, 2025

Choose a reason for hiding this comment

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

Exception

Could you except more specific exception type? #Closed

Choose a reason for hiding this comment

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

I have updated the exception handling to catch specific exceptions rather than using generic Exception handling.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the new code! however, I still see the "except Exception as e" branch. Is it really needed?

Choose a reason for hiding this comment

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

Thanks for the feedback! I have now removed the except Exception as e branch, and only specific exceptions (e.g., KeyError, ConnectionError, RuntimeError, OSError, ValueError) are handled. Let me know if you have any further suggestions.

self.config_db.connect()
syslog.syslog(syslog.LOG_INFO, "Successfully connected to SONiC config database")
return
except Exception as e:
Copy link
Contributor

@qiluo-msft qiluo-msft Jan 21, 2025

Choose a reason for hiding this comment

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

Exception

Could you except more specific exception type? #Closed

Choose a reason for hiding this comment

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

Thank you for the suggestion. I have updated the exception handling to catch specific exceptions rather than using generic Exception handling.

syslog.syslog(syslog.LOG_WARNING,
f"Failed to connect to database"
f"(attempt {retries}/{max_retries}): {str(e)}")
time.sleep(retry_delay)
Copy link
Contributor

@qiluo-msft qiluo-msft Jan 21, 2025

Choose a reason for hiding this comment

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

sleep

Could you make except block minimal size? Normally there is no complex retry logic or sleep inside this block. #Closed

Choose a reason for hiding this comment

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

I have refactored the exception handling to keep the except block minimal. The retry logic and logging are now handled outside the exception block, ensuring cleaner code and better separation of concerns.

except Exception as e:
error_msg = f"Error retrieving memory statistics configuration: {str(e)}"
syslog.syslog(syslog.LOG_ERR, error_msg)
raise RuntimeError(error_msg)
Copy link
Contributor

@qiluo-msft qiluo-msft Jan 21, 2025

Choose a reason for hiding this comment

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

raise

lose the exception stack, which make debug harder. #Closed

Choose a reason for hiding this comment

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

Thank You your suggestion. To preserve the full exception stack trace, I have updated the code to use raise ... from e. This ensures that the original exception context is retained, making debugging easier by retaining full context while adding additional error messages when necessary.


self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.settimeout(Config.SOCKET_TIMEOUT)
self.sock.connect(self.socket_path)
Copy link
Contributor

@qiluo-msft qiluo-msft Jan 21, 2025

Choose a reason for hiding this comment

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

socket_path

What is the socket file permission by design? If allow RO user read and write, is it possible to be exploited by malicious RO user? #Closed

Choose a reason for hiding this comment

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

Thank you for pointing this out.The socket file permissions are now explicitly set to 0o600 (read and write for the owner only), ensuring that unauthorized users cannot access or modify the socket. Additionally, ownership is restricted to the root user, preventing potential security risks.

…c, and enhance socket security.

Signed-off-by: Arham-Nasir <[email protected]>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Arham-Nasir
Copy link

Hi everyone, I have addressed all review comments and pushed the updated code. Some previous approvals were dismissed due to the new push. Kindly review the latest changes at your convenience. Thank you!

xincunli-sonic
xincunli-sonic previously approved these changes Feb 10, 2025
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@kanza-latif
Copy link
Contributor Author

@qiluo-msft We have incorporated all the suggested changes. Please help review and merge this

@qiluo-msft qiluo-msft merged commit 9f1eab4 into sonic-net:master Feb 27, 2025
7 checks passed
nmoray pushed a commit to nmoray/sonic-utilities that referenced this pull request Jun 25, 2025
What I did
Provided support for the configuration and show commands of memory statistics feature.

How I did it
I added a new config file config/memory_statistics.py for the configuration commands of memory statistics feature.
Added a show file show/memory_statistics.py for the show commands of memory statistics feature.
Added a test file tests/memory_statistics_test.py to test the config and show commands of memory statistics feature

How to verify it
Cli support for config and show file is available
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.

10 participants