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

feat: improve locking: lock only after fetch completed #90

Merged
merged 4 commits into from
Mar 4, 2025

Conversation

faqiharifian
Copy link
Contributor

What this PR does / why we need it:

When pubsub disabled and poll enabled, every time poller runs XP treatment service tries to acquire write lock and the treatment service refreshes the local storage by doing multiple calls to management service and update the data/maps.

This causes other part that use RLock to be blocked until all the calls and update are done.

This MR moved the write lock to after all the calls are completed, this reduces duration of RLock being blocked

@mbruner mbruner requested a review from bthari March 3, 2025 03:56
@mbruner
Copy link
Member

mbruner commented Mar 3, 2025

@faqiharifian I have one concern: right now the Init method does three things:

  1. getAllProjects()
  2. initProjectSegmenters()
  3. initExperiments()
    All three steps are in one critical section.

With the proposed change, we split it into three independent critical sections, and it is possible, for example, new requests will be processed after initProjectSegmenters() and before initExperiments(). What is the chance that the state will be inconsistent?

What do you think about refactoring it to something like:

newSegmenters = s.initProjectSegmenters()
newExperiments = s.initExperiments()

s.Lock()
defer s.Unlock()
s.subscribedProjectSettings = ...
s.projectSegmenters = newSegmenters
s.Experiments = newExperiments

@mbruner mbruner merged commit aca8aa1 into caraml-dev:main Mar 4, 2025
7 checks passed
if existingSettings.GetProjectId() == projectSettings.GetProjectId() {
return nil
}
existingProjectSettings := s.findProjectSettingsById(ProjectId(projectSettings.GetProjectId()))
Copy link

@bthari bthari Mar 5, 2025

Choose a reason for hiding this comment

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

if the locking mechanism is not running in here yet, is it possible that there will be two client who requests the same ProjectID, and therefore the s.ProjectSettings in here will have duplicate item?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just moving things around to optimize the locking, there's no new logic added in this MR and this method is not used by the Poller changes we introduced previously. Will not do it in this MR.


// In case new project was just created and we are subscribed to its ID
// we'll try to retrieve it from management service
projectSettings, err := s.fetchProjectSettingsWithId(projectId)
Copy link

@bthari bthari Mar 5, 2025

Choose a reason for hiding this comment

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

shouldn't we put the !ContainsProjectId(s.subscribedProjectIds, projectId) before this function? It is possible that projectSettings above is still nil, not because just the project can't be find, but also we don't subscribe to this project. If this is intended with the new change, perhaps can modify the comment above this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about the intention of this, I just moving things around to optimize the locking, including the comment

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