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

simplifies concurrency in ZooSession and ZooCache #5303

Merged
merged 1 commit into from
Feb 7, 2025

Conversation

keith-turner
Copy link
Contributor

Made a few changes to how ZooCache and ZooSession handle concurrency in order to make it easier to reason about the code.

ZooCache and ZooSession had code that used Atomics to do computations in addition to synchronized blocks. This adds complexity w/o an apparent benefit. Stopped doing the computations in both places and moved all computations in to the synchronized block.

ZooSession has a counter that was used by ZooCache to know when zookeeper changed. ZooSession updated this counter independently from updating the zookeeper reference and ZooCache read the refrence and counter at different times. Reorganized the code so that read and write of the counter and zookeeper are done at the same times by ZooSession and ZooCache.

ZooSession.close() could run conucurrently with the code that created new ZooKeeper objects in ZooSession. Made the close method synchronized and tweaked how the closed variable is checked.

Made a few changes to how ZooCache and ZooSession handle concurrency in
order to make it easier to reason about the code.

ZooCache and ZooSession had code that used Atomics to do computations in
addition to synchronized blocks.  This adds complexity w/o an apparent
benefit.  Stopped doing the computations in both places and moved all
computations in to the synchronized block.

ZooSession has a counter that was used by ZooCache to know when
zookeeper changed.  ZooSession updated this counter independently from
updating the zookeeper reference and ZooCache read the refrence and
counter at different times.  Reorganized the code so that read and write
of the counter and zookeeper are done at the same times by ZooSession
and ZooCache.

ZooSession.close() could run conucurrently with the code that created
new ZooKeeper objects in ZooSession.  Made the close method synchronized
and tweaked how the closed variable is checked.
Copy link
Contributor

@cshannon cshannon left a comment

Choose a reason for hiding this comment

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

LGTM, I just had the one comment/question

@@ -348,7 +377,13 @@ public ZooReaderWriter asReaderWriter() {
* @return connection counter
*/
public long getConnectionCounter() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this method also be marked as synchronized like the others?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method does not need to be synchronized as it reads the latest from a volatile. Could possibly make it synchronized and further simplify the code, but that would require synchronizing a few other methods. The zookeeper ref and zk counter need to be read and written as one. That is done by the way the current atomic ref is structured. It could also be done by having two instance variables and always reading/writing either of those variables in a sync block. Since the synchronization would cascade for making two instance variables will leave this as-is for now.

@keith-turner keith-turner merged commit 30083c3 into apache:main Feb 7, 2025
8 checks passed
@keith-turner keith-turner deleted the zoo-sync-updates branch February 7, 2025 16:18
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.

2 participants