Class ZookeeperClient

java.lang.Object
org.apache.drill.exec.coord.zk.ZookeeperClient
All Implemented Interfaces:
AutoCloseable

public class ZookeeperClient extends Object implements AutoCloseable
A namespace aware Zookeeper client. The implementation only operates under the given namespace and is safe to use. Note that instance of this class holds onto resources that must be released via #close().
  • Constructor Summary

    Constructors
    Constructor
    Description
    ZookeeperClient(org.apache.curator.framework.CuratorFramework curator, String root, org.apache.zookeeper.CreateMode mode)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    create(String path)
    Creates the given path without placing any data in.
    void
    delete(String path)
    Deletes the given node residing at the given path
    Returns an iterator of (key, value) pairs residing under root path.
    byte[]
    get(String path)
    Returns a value corresponding to the given path if path exists in the cache, null otherwise.
    byte[]
    get(String path, boolean consistent)
    Returns the value corresponding to the given key, null otherwise.
    byte[]
    get(String path, boolean consistent, DataChangeVersion version)
    Returns the value corresponding to the given key, null otherwise.
    byte[]
    get(String path, DataChangeVersion version)
    Returns the value corresponding to the given key, null otherwise.
    org.apache.curator.framework.recipes.cache.PathChildrenCache
     
    org.apache.zookeeper.CreateMode
     
     
    boolean
    Returns true if path exists in the cache, false otherwise.
    boolean
    hasPath(String path, boolean consistent)
    Returns true if path exists, false otherwise.
    boolean
    hasPath(String path, boolean consistent, DataChangeVersion version)
    Checks if the given path exists.
    void
    put(String path, byte[] data)
    Puts the given byte sequence into the given path.
    void
    put(String path, byte[] data, DataChangeVersion version)
    Puts the given byte sequence into the given path.
    byte[]
    putIfAbsent(String path, byte[] data)
    Puts the given byte sequence into the given path if path is does not exist.
    void
    Starts the client.

    Methods inherited from class java.lang.Object

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

    • ZookeeperClient

      public ZookeeperClient(org.apache.curator.framework.CuratorFramework curator, String root, org.apache.zookeeper.CreateMode mode)
  • Method Details

    • start

      public void start() throws Exception
      Starts the client. This call ensures the creation of the root path.
      Throws:
      Exception - if cache fails to start or root path creation fails.
      See Also:
    • getCache

      public org.apache.curator.framework.recipes.cache.PathChildrenCache getCache()
    • getRoot

      public String getRoot()
    • getMode

      public org.apache.zookeeper.CreateMode getMode()
    • hasPath

      public boolean hasPath(String path)
      Returns true if path exists in the cache, false otherwise. Note that calls to this method are eventually consistent.
      Parameters:
      path - path to check
      Returns:
      true if path exists, false otherwise
    • hasPath

      public boolean hasPath(String path, boolean consistent)
      Returns true if path exists, false otherwise. If consistent flag is set to true, check is done directly is made against Zookeeper directly, else check is done against local cache.
      Parameters:
      path - path to check
      consistent - whether the check should be consistent
      Returns:
      true if path exists, false otherwise
    • hasPath

      public boolean hasPath(String path, boolean consistent, DataChangeVersion version)
      Checks if the given path exists. If the flag consistent is set, the check is consistent as it is made against Zookeeper directly. Otherwise, the check is eventually consistent. If consistency flag is set to true and version holder is not null, passes version holder to get data change version. Data change version is retrieved from Stat object, it increases each time znode data change is performed. Link to Zookeeper documentation - https://zookeeper.apache.org/doc/r3.2.2/zookeeperProgrammers.html#sc_zkDataModel_znodes
      Parameters:
      path - path to check
      consistent - whether the check should be consistent
      version - version holder
      Returns:
      true if path exists, false otherwise
    • get

      public byte[] get(String path)
      Returns a value corresponding to the given path if path exists in the cache, null otherwise. Note that calls to this method are eventually consistent.
      Parameters:
      path - target path
    • get

      public byte[] get(String path, boolean consistent)
      Returns the value corresponding to the given key, null otherwise. If the flag consistent is set, the check is consistent as it is made against Zookeeper directly. Otherwise, the check is eventually consistent.
      Parameters:
      path - target path
      consistent - consistency flag
    • get

      public byte[] get(String path, DataChangeVersion version)
      Returns the value corresponding to the given key, null otherwise. The check is consistent as it is made against Zookeeper directly. Passes version holder to get data change version.
      Parameters:
      path - target path
      version - version holder
    • get

      public byte[] get(String path, boolean consistent, DataChangeVersion version)
      Returns the value corresponding to the given key, null otherwise. If the flag consistent is set, the check is consistent as it is made against Zookeeper directly. Otherwise, the check is eventually consistent. If consistency flag is set to true and version holder is not null, passes version holder to get data change version. Data change version is retrieved from Stat object, it increases each time znode data change is performed. Link to Zookeeper documentation - https://zookeeper.apache.org/doc/r3.2.2/zookeeperProgrammers.html#sc_zkDataModel_znodes
      Parameters:
      path - target path
      consistent - consistency check
      version - version holder
    • create

      public void create(String path)
      Creates the given path without placing any data in.
      Parameters:
      path - target path
    • put

      public void put(String path, byte[] data)
      Puts the given byte sequence into the given path. If path does not exists, this call creates it.
      Parameters:
      path - target path
      data - data to store
      Throws:
      IllegalArgumentException - if data size is bigger that jute.maxbuffer value
    • put

      public void put(String path, byte[] data, DataChangeVersion version)
      Puts the given byte sequence into the given path.

      If path does not exists, this call creates it.

      If version holder is not null and path already exists, passes given version for comparison. Zookeeper maintains stat structure that holds version number which increases each time znode data change is performed. If we pass version that doesn't match the actual version of the data, the update will fail KeeperException.BadVersionException. We catch such exception and re-throw it as VersionMismatchException. Link to documentation - https://zookeeper.apache.org/doc/r3.2.2/zookeeperProgrammers.html#sc_zkDataModel_znodes

      Parameters:
      path - target path
      data - data to store
      version - version holder
      Throws:
      IllegalArgumentException - if data size is bigger that jute.maxbuffer value
    • putIfAbsent

      public byte[] putIfAbsent(String path, byte[] data)
      Puts the given byte sequence into the given path if path is does not exist.
      Parameters:
      path - target path
      data - data to store
      Returns:
      null if path was created, else data stored for the given path
    • delete

      public void delete(String path)
      Deletes the given node residing at the given path
      Parameters:
      path - target path to delete
    • entries

      public Iterator<Map.Entry<String,byte[]>> entries()
      Returns an iterator of (key, value) pairs residing under root path.
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception