Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Statistics Config and Show Commands #3575

Open
wants to merge 31 commits into
base: master
Choose a base branch
from

Conversation

kanza-latif
Copy link

@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
Author

/azpw run

@mssonicbld
Copy link
Collaborator

/AzurePipelines run

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
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
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.

xincunli-sonic
xincunli-sonic previously approved these changes Jan 7, 2025
@wajahatrazi
Copy link
Contributor

@qiluo-msft

This PR has been approved with the latest updates and modifications, kindly help merge this into the SONiC master.

@mssonicbld
Copy link
Collaborator

/azp run

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

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?

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

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?

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

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

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@kanza-latif
Copy link
Author

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

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