Skip to content

Modify policy handler mechanism #1102

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
merged 22 commits into from
Jun 20, 2025
Merged

Modify policy handler mechanism #1102

merged 22 commits into from
Jun 20, 2025

Conversation

Juanadelacuesta
Copy link
Member

@Juanadelacuesta Juanadelacuesta commented Jun 6, 2025

This PR is a continuation of PR-1100

In that one, the source interface and all its implementations where updated to include a new non blocking query to get the latest version of a policy.

In this PR, we change the mechanism for monitoring and executing policies and move from using 2 blocking queries, one in the monitor for the overall number of policies and on in the policy handler to monitor changes on each specific policy, to having the manager monitor the total number of policies with a blocking query, and when it signals any changes, use the newly added function to get the latest version of any updated policy and feed id to the corresponding handler, that way the policy monitor is in charge of reading and updating any policy changes, while the policy handler just executes it.

@Juanadelacuesta Juanadelacuesta changed the title func: update handlers to only care about the ticks and not the policy… Modify policy handler mechanism Jun 11, 2025
@Juanadelacuesta Juanadelacuesta force-pushed the core-monitoring-2 branch 3 times, most recently from 8160551 to 5be5f65 Compare June 13, 2025 10:17
@Juanadelacuesta Juanadelacuesta marked this pull request as ready for review June 13, 2025 10:17
@Juanadelacuesta Juanadelacuesta requested review from a team as code owners June 13, 2025 10:17
@Juanadelacuesta Juanadelacuesta force-pushed the core-monitoring-2 branch 3 times, most recently from 2e43312 to 9ff8605 Compare June 13, 2025 10:57
Copy link
Member

@tgross tgross left a comment

Choose a reason for hiding this comment

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

This looks great @Juanadelacuesta! I've left a few comments but only the one about the read lock is really blocking.

}
}

if pht := m.handlers[policyID]; pht != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to hold a read lock on the m.handlers here in case of a concurrent ReloadSources call.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking, do we even need the lock? Only the monitor will be modifying the handlers.

Copy link
Member

Choose a reason for hiding this comment

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

The EnforceCooldown and ReloadSources methods (which I think get called from outside the monitor loop?) reads from the handlers. Mutating the map isn't atomic, so if you read it without the lock you can get weird stuff like a valid key but no value (which the runtime avoids by panicking with the message "fatal error: concurrent map read and map write").

tgross
tgross previously approved these changes Jun 19, 2025
Copy link
Member

@tgross tgross left a comment

Choose a reason for hiding this comment

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

LGTM. The remaining comments I have are performance improvements rather than correctness improvements. Would be nice to get those in.


// Mark policy as must-keep so it doesn't get removed.
m.keep[policyID] = true
m.handlersLock.Lock()
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be a Rlock()?

m.log.Trace("handler already exists",
"policy_id", policyID, "policy_source", policyIDs.Source)
pht.updates <- updatedPolicy
m.handlersLock.Unlock()
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to wait until we've sent the update to unlock, or can we unlock immediately after the read? If we can unlock right after pht := m.handlers[policyID] there'd be less contention.

Comment on lines 288 to 289
m.handlersLock.Lock()
defer m.handlersLock.Unlock()
Copy link
Member

Choose a reason for hiding this comment

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

Now that we're not sending the reload to the handlers in this method, do we still need this lock?

It looks like policySources never gets mutated once we create the Manager so we can use it without the lock. But if I've missed it somewhere then we'd probably need the lock on all the other policySources reads too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this lock was a left over of the previous implementation, lets remove it

Copy link
Collaborator

@pkazmierczak pkazmierczak left a comment

Choose a reason for hiding this comment

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

:shipit:

@Juanadelacuesta Juanadelacuesta merged commit 4b87659 into main Jun 20, 2025
33 checks passed
@Juanadelacuesta Juanadelacuesta deleted the core-monitoring-2 branch June 20, 2025 12:29
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.

3 participants