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

refactor: Use native Lock timeout instead of reimplementing #676

Merged

Conversation

a-ungurianu
Copy link
Member

Fixes #605

Why is this needed?

Not needed per se, but removes unnecessary complexity from the lock acquiring code

Proposed Changes

  • use timeout parameter from the lock object instead of reimplementing timeout
  • rename self._lock to self._thread_lock (suggestions encouraged), as having a lock inside a lock made this hard to parse
  • ...

Does this PR introduce any breaking change?

Nope

@a-ungurianu a-ungurianu marked this pull request as ready for review October 23, 2022 21:17
kazoo/recipe/lock.py Outdated Show resolved Hide resolved
@a-ungurianu a-ungurianu force-pushed the refactor/thread_timeout branch 3 times, most recently from 6d9422f to e4e2362 Compare October 24, 2022 18:49
@codecov-commenter
Copy link

codecov-commenter commented Oct 24, 2022

Codecov Report

Base: 94.38% // Head: 94.40% // Increases project coverage by +0.02% 🎉

Coverage data is based on head (e978bd7) compared to base (a43ef2b).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #676      +/-   ##
==========================================
+ Coverage   94.38%   94.40%   +0.02%     
==========================================
  Files          57       57              
  Lines        8399     8389      -10     
==========================================
- Hits         7927     7920       -7     
+ Misses        472      469       -3     
Impacted Files Coverage Δ
kazoo/recipe/lock.py 98.94% <100.00%> (+0.98%) ⬆️
kazoo/handlers/eventlet.py 99.01% <0.00%> (-0.99%) ⬇️
kazoo/protocol/connection.py 96.28% <0.00%> (-0.21%) ⬇️
kazoo/tests/test_client.py 98.70% <0.00%> (-0.20%) ⬇️
kazoo/client.py 98.56% <0.00%> (+0.31%) ⬆️
kazoo/handlers/utils.py 95.49% <0.00%> (+0.45%) ⬆️
kazoo/handlers/gevent.py 94.62% <0.00%> (+1.07%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

locked = self._lock.acquire(False)
if not locked and not blocking:
thread_locked = self._thread_lock.acquire(
blocking=blocking, timeout=timeout if timeout is not None else -1
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'm not a fan that python's threading chose to require timeout to be a number. Thankfully, it seems the other handler implementation's underlying Lock replacement also follows the python API, so this should be safe.

I could just replace the default parameter to -1 instead. Don't have a strong opinion either way

Copy link
Member

Choose a reason for hiding this comment

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

IMO the way you implemented it is better: it does not change the method signature, so no chance to break someone's current code.

@a-ungurianu
Copy link
Member Author

The test that's failing seems to suggest it's already flaky to begin with. Not sure how my change could affect that specific test. It seems that the client is failing to connect to all zookeeper instances, not just the ones we stopped.

@StephenSorriaux
Copy link
Member

Hey, thank you for this. Yes the tests around the "read only" feature is kind of flaky, mostly because one testcase is stopping the cluster. I have a hard time reproducing it locally so yeah...

Copy link
Member

@StephenSorriaux StephenSorriaux 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.

locked = self._lock.acquire(False)
if not locked and not blocking:
thread_locked = self._thread_lock.acquire(
blocking=blocking, timeout=timeout if timeout is not None else -1
Copy link
Member

Choose a reason for hiding this comment

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

IMO the way you implemented it is better: it does not change the method signature, so no chance to break someone's current code.

@@ -134,7 +134,7 @@ def __init__(self, client, path, identifier=None, extra_lock_patterns=()):
self._retry = KazooRetry(
max_tries=None, sleep_func=client.handler.sleep_func
)
self._lock = client.handler.lock_object()
self._thread_lock = client.handler.lock_object()
Copy link
Contributor

Choose a reason for hiding this comment

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

You asked for a suggestion: how about _handler_lock?
Just to recognize that other handlers are not thread based

Copy link
Member Author

@a-ungurianu a-ungurianu Oct 28, 2022

Choose a reason for hiding this comment

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

Actually, how about _acquire_method_lock which describes what it does instead of what it is.

@ceache
Copy link
Contributor

ceache commented Oct 28, 2022

LGTM
Thanks a lot for this.

@a-ungurianu
Copy link
Member Author

My pleasure. I'm trying to learn more about Zookeeper and the Kazoo recipes. Happy give back :)

@ceache
Copy link
Contributor

ceache commented Oct 28, 2022 via email

@StephenSorriaux StephenSorriaux merged commit 51b875a into python-zk:master Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify lock recipe in a post Python 3.2 threading world.
5 participants