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

Add in support for a TryLock to attempt to get a lock within a timeout #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rjemanuele
Copy link

I was looking for this feature also as we use ZK to assign workers by taking locks if available.

#116

@rjemanuele rjemanuele force-pushed the try_lock branch 2 times, most recently from de630ae to 9dcb775 Compare May 24, 2024 04:10
@rjemanuele rjemanuele force-pushed the try_lock branch 3 times, most recently from fd86978 to 6928b24 Compare September 9, 2024 22:48
@rjemanuele
Copy link
Author

Tests added.

@rjemanuele
Copy link
Author

@jeffbean can I get a review?

}
val <- 3
}()
time.Sleep(time.Millisecond * 100)

Choose a reason for hiding this comment

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

Tests that use sleep will produce indeterministic results. If we need to test with time, we will need to either coordinate with events or have something controlling time.

}
case <-delay.C:
// remove the pending lock path on timeout
delErr := l.c.Delete(path, -1)

Choose a reason for hiding this comment

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

This would delete a lock from any node that has acquired the lock. we do not want to delete any node during a lock function.

ev := <-ch
if ev.Err != nil {
return ev.Err
if timeout >= 0 {

Choose a reason for hiding this comment

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

This is a timeout waiting for events of acquiring a lock. It does not actually bound the lock timing at all outside of how long we wait. If this pattern of Trying to lock within a time boundary it would also bound the creating of znodes as well, or be very clearly documented that this duration is only applied on how long we watch for events on failure to acquire a lock.

// It will wait to return until the lock is acquired, an error occurs, or the timeout. If
// this instance already has the lock then ErrDeadlock is returned. A negative
// timeout is waiting forever
func (l *Lock) TryLockWithData(data []byte, timeout time.Duration) error {

Choose a reason for hiding this comment

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

in place of any timeout here it may be best to use context.Context since this function has multiple places where we wait or loop forever. both the unconditional for loop on children, and the channel read from watch events after failing a lock.

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