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

Fix Race Condition in Memcached Store, Close Loadable to Stop Setter Routine #100

Merged
merged 1 commit into from Jun 18, 2021

Conversation

terev
Copy link
Contributor

@terev terev commented Jun 14, 2021

Summary

  • Updates memcached setTags method to use CompareAndSwap to update existing items to avoid overwriting other clients. And uses Add to create new tag keys.
  • Adds Close method to LoadableCache to stop setter goroutine

Fix setWithTags test, add Close method to stop setter goroutine
Copy link
Owner

@eko eko left a comment

Choose a reason for hiding this comment

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

Thank you for proposing this pull request

if err = s.addKeyToTagValue(tagKey, key); err == nil {
return nil
}
// loop to retry any failure (including race conditions)
Copy link
Owner

Choose a reason for hiding this comment

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

I think race conditions should not be handled like this. Could you please use a mutex instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eko sorry i shouldve specified what the race condition is. its a distributed race condition, because multiple clients can update the same key at any time

previously the setTags code was using the Set operation to update tags. the problem with this is that if client1 issues a Set after client2 has read the contents of the same key via Get. client2 will miss the update issued by client1. this means client2 will overwrite what client1 wrote and the update is lost.

to avoid this, synchronization is done via memcached's optimistic concurrency control mechanisms. using CompareAndSwap with the item returned from a previous Get ensures that the value has not changed since. if the item has changed, an error is returned. thus why addKeyToTagValue is retried

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, thank you for clarifying this! So let's use this 3-times retry mechanism :)

if err = s.addKeyToTagValue(tagKey, key); err == nil {
return nil
}
// loop to retry any failure (including race conditions)
Copy link
Owner

Choose a reason for hiding this comment

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

Ok, thank you for clarifying this! So let's use this 3-times retry mechanism :)

@eko eko merged commit 9813303 into eko:master Jun 18, 2021
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.

None yet

2 participants