Class DistributedAtomicValue


  • public class DistributedAtomicValue
    extends java.lang.Object

    A distributed value that attempts atomic sets. It first tries uses optimistic locking. If that fails, an optional InterProcessMutex is taken. For both optimistic and mutex, a retry policy is used to retry the increment.

    The various methods return an AtomicValue object. You must always check AtomicValue.succeeded(). None of the methods (other than get()) are guaranteed to succeed.

    • Constructor Summary

      Constructors 
      Constructor Description
      DistributedAtomicValue​(org.apache.curator.framework.CuratorFramework client, java.lang.String path, org.apache.curator.RetryPolicy retryPolicy)
      Creates in optimistic mode only - i.e.
      DistributedAtomicValue​(org.apache.curator.framework.CuratorFramework client, java.lang.String path, org.apache.curator.RetryPolicy retryPolicy, PromotedToLock promotedToLock)
      Creates in mutex promotion mode.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      AtomicValue<byte[]> compareAndSet​(byte[] expectedValue, byte[] newValue)
      Atomically sets the value to the given updated value if the current value == the expected value.
      void forceSet​(byte[] newValue)
      Forcibly sets the value any guarantees of atomicity.
      AtomicValue<byte[]> get()
      Returns the current value of the counter.
      boolean initialize​(byte[] value)
      Atomic values are initially set to the equivalent of NULL in a database.
      AtomicValue<byte[]> trySet​(byte[] newValue)
      Attempt to atomically set the value to the given value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DistributedAtomicValue

        public DistributedAtomicValue​(org.apache.curator.framework.CuratorFramework client,
                                      java.lang.String path,
                                      org.apache.curator.RetryPolicy retryPolicy)
        Creates in optimistic mode only - i.e. the promotion to a mutex is not done
        Parameters:
        client - the client
        path - path to hold the value
        retryPolicy - the retry policy to use
      • DistributedAtomicValue

        public DistributedAtomicValue​(org.apache.curator.framework.CuratorFramework client,
                                      java.lang.String path,
                                      org.apache.curator.RetryPolicy retryPolicy,
                                      PromotedToLock promotedToLock)
        Creates in mutex promotion mode. The optimistic lock will be tried first using the given retry policy. If the increment does not succeed, a InterProcessMutex will be tried with its own retry policy
        Parameters:
        client - the client
        path - path to hold the value
        retryPolicy - the retry policy to use
        promotedToLock - the arguments for the mutex promotion
    • Method Detail

      • get

        public AtomicValue<byte[]> get()
                                throws java.lang.Exception
        Returns the current value of the counter. NOTE: if the value has never been set, 0 is returned.
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • forceSet

        public void forceSet​(byte[] newValue)
                      throws java.lang.Exception
        Forcibly sets the value any guarantees of atomicity.
        Parameters:
        newValue - the new value
        Throws:
        java.lang.Exception - ZooKeeper errors
      • compareAndSet

        public AtomicValue<byte[]> compareAndSet​(byte[] expectedValue,
                                                 byte[] newValue)
                                          throws java.lang.Exception
        Atomically sets the value to the given updated value if the current value == the expected value. Remember to always check AtomicValue.succeeded().
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • trySet

        public AtomicValue<byte[]> trySet​(byte[] newValue)
                                   throws java.lang.Exception
        Attempt to atomically set the value to the given value. Remember to always check AtomicValue.succeeded().
        Parameters:
        newValue - the value to set
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • initialize

        public boolean initialize​(byte[] value)
                           throws java.lang.Exception
        Atomic values are initially set to the equivalent of NULL in a database. Use this method to initialize the value. The value will be set if and only iff the node does not exist.
        Parameters:
        value - the initial value to set
        Returns:
        true if the value was set, false if the node already existed
        Throws:
        java.lang.Exception - ZooKeeper errors