Class Curator

  • All Implemented Interfaces:
    java.lang.AutoCloseable
    Direct Known Subclasses:
    MockCurator

    public class Curator
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Curator interface for Vespa. This contains method for constructing common recipes and utilities as well as a small wrapper API for common operations which uses typed paths and avoids throwing checked exceptions.

    There is a mock implementation in MockCurator.

    Author:
    vegardh, bratseth
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Curator.CompletionWaiter
      Interface for waiting for completion of an operation
      static interface  Curator.DirectoryCache
      A listenable cache of all the immediate children of a curator path.
      static interface  Curator.FileCache
      A listenable cache of the content of a single curator path.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected org.apache.curator.RetryPolicy retryPolicy  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        Curator​(com.yahoo.cloud.config.ConfigserverConfig configserverConfig, com.yahoo.vespa.zookeeper.VespaZooKeeperServer server)  
      protected Curator​(java.lang.String connectionSpec, java.lang.String zooKeeperEnsembleConnectionSpec, java.util.function.Function<org.apache.curator.RetryPolicy,​org.apache.curator.framework.CuratorFramework> curatorFactory)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      java.lang.String connectionSpec()
      Returns the ZooKeeper "connect string" used by curator: a comma-separated list of host:port of ZooKeeper endpoints to connect to.
      boolean create​(com.yahoo.path.Path path)
      Creates an empty node at a path, creating any parents as necessary.
      static Curator create​(java.lang.String connectionSpec)
      Creates a curator instance from a comma-separated string of ZooKeeper host:port strings
      static Curator create​(java.lang.String connectionSpec, java.util.Optional<java.io.File> clientConfigFile)  
      void createAtomically​(com.yahoo.path.Path... paths)
      Creates all the given paths in a single transaction.
      org.apache.curator.framework.recipes.atomic.DistributedAtomicLong createAtomicCounter​(java.lang.String path)
      For internal use; prefer creating a CuratorCounter
      Curator.CompletionWaiter createCompletionWaiter​(com.yahoo.path.Path parentPath, java.lang.String waiterNode, int numMembers, java.lang.String id)  
      Curator.DirectoryCache createDirectoryCache​(java.lang.String path, boolean cacheData, boolean dataIsCompressed, java.util.concurrent.ExecutorService executorService)
      Creates a listenable cache which keeps in sync with changes to all the immediate children of a path
      Curator.FileCache createFileCache​(java.lang.String path, boolean dataIsCompressed)
      Creates a listenable cache which keeps in sync with changes to a given node
      org.apache.curator.framework.recipes.locks.InterProcessLock createMutex​(java.lang.String lockPath)
      For internal use; prefer creating a Lock
      void delete​(com.yahoo.path.Path path)
      Deletes the given path and any children it may have.
      boolean exists​(com.yahoo.path.Path path)
      A convenience method which returns whether the given path exists
      org.apache.curator.framework.CuratorFramework framework()
      Returns the curator framework API
      java.util.List<java.lang.String> getChildren​(com.yahoo.path.Path path)
      Returns the names of the children at the given path.
      Curator.CompletionWaiter getCompletionWaiter​(com.yahoo.path.Path waiterPath, int numMembers, java.lang.String id)  
      java.util.Optional<byte[]> getData​(com.yahoo.path.Path path)
      Returns the data at the given path, which may be a zero-length buffer if the node exists but have no data.
      java.util.Optional<org.apache.zookeeper.data.Stat> getStat​(com.yahoo.path.Path path)
      Returns the stat data at the given path.
      Lock lock​(com.yahoo.path.Path path, java.time.Duration timeout)
      Create and acquire a re-entrant lock in given path
      void set​(com.yahoo.path.Path path, byte[] data)
      A convenience method which sets some content at a path.
      java.lang.String zooKeeperEnsembleConnectionSpec()  
      int zooKeeperEnsembleCount()
      Returns the number of zooKeeper servers in this ensemble.
      • Methods inherited from class java.lang.Object

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

      • retryPolicy

        protected final org.apache.curator.RetryPolicy retryPolicy
    • Constructor Detail

      • Curator

        @Inject
        public Curator​(com.yahoo.cloud.config.ConfigserverConfig configserverConfig,
                       com.yahoo.vespa.zookeeper.VespaZooKeeperServer server)
      • Curator

        protected Curator​(java.lang.String connectionSpec,
                          java.lang.String zooKeeperEnsembleConnectionSpec,
                          java.util.function.Function<org.apache.curator.RetryPolicy,​org.apache.curator.framework.CuratorFramework> curatorFactory)
    • Method Detail

      • create

        public static Curator create​(java.lang.String connectionSpec)
        Creates a curator instance from a comma-separated string of ZooKeeper host:port strings
      • create

        public static Curator create​(java.lang.String connectionSpec,
                                     java.util.Optional<java.io.File> clientConfigFile)
      • connectionSpec

        public java.lang.String connectionSpec()
        Returns the ZooKeeper "connect string" used by curator: a comma-separated list of host:port of ZooKeeper endpoints to connect to. This may be a subset of zooKeeperEnsembleConnectionSpec() if there's some affinity, e.g. for performance reasons. This may be empty but never null
      • createAtomicCounter

        public org.apache.curator.framework.recipes.atomic.DistributedAtomicLong createAtomicCounter​(java.lang.String path)
        For internal use; prefer creating a CuratorCounter
      • createMutex

        public org.apache.curator.framework.recipes.locks.InterProcessLock createMutex​(java.lang.String lockPath)
        For internal use; prefer creating a Lock
      • getCompletionWaiter

        public Curator.CompletionWaiter getCompletionWaiter​(com.yahoo.path.Path waiterPath,
                                                            int numMembers,
                                                            java.lang.String id)
      • createCompletionWaiter

        public Curator.CompletionWaiter createCompletionWaiter​(com.yahoo.path.Path parentPath,
                                                               java.lang.String waiterNode,
                                                               int numMembers,
                                                               java.lang.String id)
      • createDirectoryCache

        public Curator.DirectoryCache createDirectoryCache​(java.lang.String path,
                                                           boolean cacheData,
                                                           boolean dataIsCompressed,
                                                           java.util.concurrent.ExecutorService executorService)
        Creates a listenable cache which keeps in sync with changes to all the immediate children of a path
      • createFileCache

        public Curator.FileCache createFileCache​(java.lang.String path,
                                                 boolean dataIsCompressed)
        Creates a listenable cache which keeps in sync with changes to a given node
      • exists

        public boolean exists​(com.yahoo.path.Path path)
        A convenience method which returns whether the given path exists
      • set

        public void set​(com.yahoo.path.Path path,
                        byte[] data)
        A convenience method which sets some content at a path. If the path and any of its parents does not exists they are created.
      • create

        public boolean create​(com.yahoo.path.Path path)
        Creates an empty node at a path, creating any parents as necessary. If the node already exists nothing is done. Returns whether a change was attempted.
      • createAtomically

        public void createAtomically​(com.yahoo.path.Path... paths)
        Creates all the given paths in a single transaction. Any paths which already exists are ignored.
      • delete

        public void delete​(com.yahoo.path.Path path)
        Deletes the given path and any children it may have. If the path does not exists nothing is done.
      • getChildren

        public java.util.List<java.lang.String> getChildren​(com.yahoo.path.Path path)
        Returns the names of the children at the given path. If the path does not exist or have no children an empty list (never null) is returned.
      • getData

        public java.util.Optional<byte[]> getData​(com.yahoo.path.Path path)
        Returns the data at the given path, which may be a zero-length buffer if the node exists but have no data. Empty is returned if the path does not exist.
      • getStat

        public java.util.Optional<org.apache.zookeeper.data.Stat> getStat​(com.yahoo.path.Path path)
        Returns the stat data at the given path. Empty is returned if the path does not exist.
      • lock

        public Lock lock​(com.yahoo.path.Path path,
                         java.time.Duration timeout)
        Create and acquire a re-entrant lock in given path
      • framework

        public org.apache.curator.framework.CuratorFramework framework()
        Returns the curator framework API
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • zooKeeperEnsembleConnectionSpec

        public java.lang.String zooKeeperEnsembleConnectionSpec()
        Returns:
        The non-null connect string containing all ZooKeeper servers in the ensemble. WARNING: This may be different from the servers this Curator may connect to. TODO: Move method out of this class.
      • zooKeeperEnsembleCount

        public int zooKeeperEnsembleCount()
        Returns the number of zooKeeper servers in this ensemble. WARNING: This may be different from the number of servers this Curator may connect to. TODO: Move method out of this class.