You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@curator.apache.org by "Jordan Zimmerman (JIRA)" <ji...@apache.org> on 2018/03/20 14:07:00 UTC

[jira] [Commented] (CURATOR-459) Support for asynchronous locks in Curator Async

    [ https://issues.apache.org/jira/browse/CURATOR-459?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16406382#comment-16406382 ] 

Jordan Zimmerman commented on CURATOR-459:
------------------------------------------

Curator has an async lock with {{LeaderSelector}}. What's the advantage of adding this?

> Support for asynchronous locks in Curator Async
> -----------------------------------------------
>
>                 Key: CURATOR-459
>                 URL: https://issues.apache.org/jira/browse/CURATOR-459
>             Project: Apache Curator
>          Issue Type: Wish
>          Components: Recipes
>            Reporter: Peter Rietzler
>            Priority: Minor
>
> It would be great to have a support for asynchronous locks. Here's an interface suggestion
> {code:java}
> /**
>  * A non-reentrant Lock with the ability to react when the lock is acquired async, so that the thread which
>  * acquires the lock is not blocked.
>  */
> public interface CompletableLock {
>  /**
>  * Acquire the lock non-blocking. The resulting {@link CompletableFuture} can then be used to block
>  * until the lock is actually acquired. The {@link CompletableFuture#cancel(boolean)} can be used
>  * to abort the acquisition of the lock. As soon as the lock is actually acquired, this method has no
>  * effect. The Lock itself is then released with {@link Lease#unlock()}.
>  * <p>
>  * Several calls results in several independent locking requests. So it is equivalent to
>  * call this method multiple times or get a new lock and then call the method once.
>  *
>  * @return an instance of {@link CompletableFuture} which represents the actual lock.
>  */
>  CompletableFuture<Lease> lock();
>  default void withLock(Runnable body) {
>  lock().thenAccept(lock -> {
>  try {
>  body.run();
>  } finally {
>  lock.unlock();
>  }
>  });
>  }
>  /**
>  * Same as {@link #lock()} but if the lock can't be acquired within the given amount of time,
>  * the {@link CompletableFuture} throws a {@link java.util.concurrent.ExecutionException} with a nested
>  * {@link java.util.concurrent.TimeoutException} when it is accessed the next time.
>  *
>  * @param time the maximum waiting time for acquiring the lock.
>  * @param unit the {@link TimeUnit} for the given time.
>  * @return an instance of {@link CompletableFuture} which represents the actual lock.
>  */
>  CompletableFuture<Lease> lock(int time, TimeUnit unit);
>  /**
>  * Gets the lock only if possible. If lock is currently acquired, this returns immediately with an undefined option.
>  */
>  Optional<Lease> tryLock();
>  /**
>  * Gets the lock only if possible. If lock is currently acquired, this returns an undefined option after the
>  * given timeout.
>  */
>  Lease tryLock(int time, TimeUnit unit);
>  /**
>  * For unlocking a successfully acquired lock.
>  */
>  interface Lease {
>  /**
>  * Releases the lock.
>  */
>  void unlock();
>  }
> }{code}
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)