Skip to content

refactor(monitor): replace RWMutex with atomic.Pointer for snapshot management #2022

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

Merged

Conversation

sthaha
Copy link
Collaborator

@sthaha sthaha commented Apr 19, 2025

This commit replaces sync.RWMutex with atomic.Pointer for thread-safe snapshot
access in PowerMonitor.

  • Move zone initialization to initZones method and defer snapshot creation.
  • Update calculateNodePower to accept previous node data, decoupling it from snapshot locking.
  • update tests to reflect new logic.

Copy link

codecov bot commented Apr 19, 2025

Codecov Report

Attention: Patch coverage is 94.11765% with 3 lines in your changes missing coverage. Please review.

Project coverage is 93.15%. Comparing base (2ad09ba) to head (80f91e4).
Report is 5 commits behind head on reboot.

Files with missing lines Patch % Lines
internal/monitor/monitor.go 92.68% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           reboot    #2022      +/-   ##
==========================================
+ Coverage   89.78%   93.15%   +3.36%     
==========================================
  Files          17       17              
  Lines         999     1008       +9     
==========================================
+ Hits          897      939      +42     
+ Misses         81       54      -27     
+ Partials       21       15       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sthaha sthaha force-pushed the refactor-monitor-snapshot branch 2 times, most recently from 005cd50 to 11a01a6 Compare April 20, 2025 13:06
@sthaha sthaha added the refactor Code refactoring without changing functionality label Apr 20, 2025
@sthaha sthaha force-pushed the refactor-monitor-snapshot branch 2 times, most recently from e835739 to 582546b Compare April 23, 2025 02:56
Comment on lines +214 to +216
if prevSnapshot == nil {
prevSnapshot = NewSnapshot()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

cant we simplify it to

if prevSnapshot == nil {
  	snapshot := NewSnapshot()
      	snapshot.Timestamp = time.Now()
	pm.snapshot.Store(snapshot)
        return err
}

otherwise both prevSnapshot and newSnapshot are same

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sorry, I am not getting it :(
When monitor starts, we don't allocate any memory for the snapshot. In the computePower we ensure that previousSnapshot which is going to be referred from now on has a blank (0 ed out) snapshot. We specifically don't want to set the timestamp of the previous snapshot and it must be 0. This information is useful to compute the first - collection.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ref: #2025

pm.snapshotMu.RUnlock()

if prevReadTime.IsZero() {
func (pm *PowerMonitor) calculateNodePower(snapshot *Snapshot, prevNode *Node) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldnt computeNodePower have prev and current *Node as parameters?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

+1 why not .. we can change it back if other snapshot members need to be accessed.


if prevReadTime.IsZero() {
func (pm *PowerMonitor) calculateNodePower(snapshot *Snapshot, prevNode *Node) error {
if prevNode == nil || prevNode.Timestamp.IsZero() {
// No previous data, nothing to do
return pm.firstNodeRead(snapshot)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this can still trigger pm.signalNewData()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, it can. Something wrong with that?

sthaha added 2 commits April 23, 2025 08:30
…anagement

This commit replaces sync.RWMutex with atomic.Pointer for thread-safe snapshot
access in PowerMonitor.

* Move zone initialization to initZones method and defer snapshot creation.
* Update calculateNodePower to accept previous node data, decoupling it from snapshot locking.
* update tests to reflect new logic.

Signed-off-by: Sunil Thaha <[email protected]>
Comment on lines +214 to +216
if prevSnapshot == nil {
prevSnapshot = NewSnapshot()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

ref: #2025

@vimalk78 vimalk78 merged commit 4e0726b into sustainable-computing-io:reboot Apr 28, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Code refactoring without changing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants