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

Wait on locks #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Wait on locks #15

wants to merge 2 commits into from

Conversation

Jane-Fan
Copy link

@Jane-Fan Jane-Fan commented Jun 7, 2018

Add a key locking condition "wait-on-locks"

  1. When the "wait-on-locks" is True, it waits indefinitely and keeps re-acquiring the key.
  2. When it is False, it only wait for the existing key's ttl to expire and acquire the key once.

# Check if the key is locked or not
if not check_lock(record):
if not wait_on_locks:
raise ResourceLocked(
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't make sense: throw an exception if its not locked, and we don't want to wait on locks?

Copy link
Author

Choose a reason for hiding this comment

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

It throws the exception only if the "wait_on_locks" is False. If it is true, it will wait on locks.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you're missing the point. This reads, to me as "If not locked and we're not waiting on locks, throw an error that we're locked".

Copy link
Author

Choose a reason for hiding this comment

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

The check_lock returns True if there is no existing lock. Otherwise, it returns False if there is an existing lock.
The intended logic is "If locked and not waiting on lock, throw an error we are locked"

Copy link
Contributor

@vodik vodik Jun 13, 2018

Choose a reason for hiding this comment

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

You're missing the point - I've read the code and I understand what it does. That's not where I'm confused.

My point is, in isolation, if I see if not check_lock(...), and I have no idea what check_lock does because I haven't read the function in depth yet - would I be more likely to assume that it checks if something is currently locked? Or would I be more likely to assume it checks that something isn't locked.

How does it read as English to you?

There's no negative in the function name to make me think you're going to return true if we don't have a lock. And - if that was the case - its weird to make this constructed with a double negative.

This is how you write self documenting code.

raise ResourceLocked(
'{} is currently locked by {}'.format(key, record.value))
while True:
if check_lock(record):
Copy link
Contributor

Choose a reason for hiding this comment

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

You never reread the key. How do you know its current state?

record = self.backend.read(key)
if not record:
return True
time.sleep(0.5)
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't have to poll this aggressively. You can sleep for half of ttl's length.

Copy link
Author

Choose a reason for hiding this comment

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

Polling every half a second is from previous code.
I agree, half of ttl should be frequent enough in this scenario.

Copy link
Contributor

Choose a reason for hiding this comment

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

I looked it up, you're right. Tyler made the change.

raise ResourceLocked(
'{} is currently locked by {}'.format(key, record.value))
while wait_on_locks:
if check_lock(record):
Copy link
Contributor

Choose a reason for hiding this comment

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

while not check_lock(record):
    pass

But you really don't need to have the helper function here.

@@ -122,22 +122,31 @@ def __init__(self, config, backend, ttl=30):
self._thread = None
self._stop = threading.Event()

def aquire(self, key, user=None):
def aquire(self, key, wait_on_locks, user=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

Something I forgot to mention the first time around - don't change the public API here. Its a mater of global policy. This should be set in the constructor once when the plugin is initialised.

@Jane-Fan Jane-Fan force-pushed the wait-forever-lock branch from 256e90a to 61a7ed5 Compare June 15, 2018 03:55
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