Class ZooKeeperCache

  • All Implemented Interfaces:
    org.apache.zookeeper.Watcher
    Direct Known Subclasses:
    GlobalZooKeeperCache, LocalZooKeeperCache

    public abstract class ZooKeeperCache
    extends java.lang.Object
    implements org.apache.zookeeper.Watcher
    Per ZK client ZooKeeper cache supporting ZNode data and children list caches. A cache entry is identified, accessed and invalidated by the ZNode path. For the data cache, ZNode data parsing is done at request time with the given ZooKeeperCache.Deserializer argument.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ZooKeeperCache.CacheUpdater<T>  
      static interface  ZooKeeperCache.Deserializer<T>  
      • Nested classes/interfaces inherited from interface org.apache.zookeeper.Watcher

        org.apache.zookeeper.Watcher.Event, org.apache.zookeeper.Watcher.WatcherType
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected com.github.benmanes.caffeine.cache.AsyncLoadingCache<java.lang.String,​java.util.Set<java.lang.String>> childrenCache  
      protected com.github.benmanes.caffeine.cache.AsyncLoadingCache<java.lang.String,​org.apache.commons.lang3.tuple.Pair<java.util.Map.Entry<java.lang.Object,​org.apache.zookeeper.data.Stat>,​java.lang.Long>> dataCache  
      protected com.github.benmanes.caffeine.cache.AsyncLoadingCache<java.lang.String,​java.lang.Boolean> existsCache  
      static java.lang.String ZK_CACHE_INSTANCE  
      protected java.util.concurrent.atomic.AtomicReference<org.apache.zookeeper.ZooKeeper> zkSession  
    • Constructor Summary

      Constructors 
      Constructor Description
      ZooKeeperCache​(java.lang.String cacheName, org.apache.zookeeper.ZooKeeper zkSession, int zkOperationTimeoutSeconds)  
      ZooKeeperCache​(java.lang.String cacheName, org.apache.zookeeper.ZooKeeper zkSession, int zkOperationTimeoutSeconds, org.apache.bookkeeper.common.util.OrderedExecutor executor)  
      ZooKeeperCache​(java.lang.String cacheName, org.apache.zookeeper.ZooKeeper zkSession, int zkOperationTimeoutSeconds, org.apache.bookkeeper.common.util.OrderedExecutor executor, int cacheExpirySeconds)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void asyncInvalidate​(java.lang.String path)  
      boolean checkRegNodeAndWaitExpired​(java.lang.String regPath)  
      boolean exists​(java.lang.String path)
      Returns if the node at the given path exists in the cache
      java.util.concurrent.CompletableFuture<java.lang.Boolean> existsAsync​(java.lang.String path, org.apache.zookeeper.Watcher watcher)  
      java.util.Set<java.lang.String> getChildren​(java.lang.String path)
      Simple ZooKeeperChildrenCache use this method to invalidate cache entry on watch event w/o automatic re-loading
      java.util.concurrent.CompletableFuture<java.util.Set<java.lang.String>> getChildrenAsync​(java.lang.String path, org.apache.zookeeper.Watcher watcher)
      ZooKeeperChildrenCache implementing automatic re-loading on update use this method by passing in a different Watcher object to reload cache entry
      java.util.Set<java.lang.String> getChildrenIfPresent​(java.lang.String path)  
      <T> java.util.Optional<T> getData​(java.lang.String path, ZooKeeperCache.Deserializer<T> deserializer)
      Simple ZooKeeperCache use this method to invalidate the cache entry on watch event w/o automatic reloading the cache
      <T> java.util.Optional<java.util.Map.Entry<T,​org.apache.zookeeper.data.Stat>> getData​(java.lang.String path, org.apache.zookeeper.Watcher watcher, ZooKeeperCache.Deserializer<T> deserializer)
      Cache that implements automatic reloading on update will pass a different Watcher object to reload cache entry automatically
      <T> java.util.concurrent.CompletableFuture<java.util.Optional<T>> getDataAsync​(java.lang.String path, ZooKeeperCache.Deserializer<T> deserializer)  
      <T> java.util.concurrent.CompletableFuture<java.util.Optional<java.util.Map.Entry<T,​org.apache.zookeeper.data.Stat>>> getDataAsync​(java.lang.String path, org.apache.zookeeper.Watcher watcher, ZooKeeperCache.Deserializer<T> deserializer)  
      <T> T getDataIfPresent​(java.lang.String path)  
      <T> java.util.Optional<java.util.Map.Entry<T,​org.apache.zookeeper.data.Stat>> getEntry​(java.lang.String path, ZooKeeperCache.Deserializer<T> deserializer)  
      <T> java.util.concurrent.CompletableFuture<java.util.Optional<java.util.Map.Entry<T,​org.apache.zookeeper.data.Stat>>> getEntryAsync​(java.lang.String path, ZooKeeperCache.Deserializer<T> deserializer)  
      int getZkOperationTimeoutSeconds()  
      org.apache.zookeeper.ZooKeeper getZooKeeper()  
      void invalidate​(java.lang.String path)  
      void invalidateAll()  
      void invalidateAllChildren()  
      void invalidateAllData()  
      void invalidateChildren​(java.lang.String path)  
      void invalidateData​(java.lang.String path)  
      void invalidateRoot​(java.lang.String root)  
      void process​(org.apache.zookeeper.WatchedEvent event)  
      <T> void process​(org.apache.zookeeper.WatchedEvent event, ZooKeeperCache.CacheUpdater<T> updater)  
      void stop()  
      • Methods inherited from class java.lang.Object

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

      • ZK_CACHE_INSTANCE

        public static final java.lang.String ZK_CACHE_INSTANCE
        See Also:
        Constant Field Values
      • dataCache

        protected final com.github.benmanes.caffeine.cache.AsyncLoadingCache<java.lang.String,​org.apache.commons.lang3.tuple.Pair<java.util.Map.Entry<java.lang.Object,​org.apache.zookeeper.data.Stat>,​java.lang.Long>> dataCache
      • childrenCache

        protected final com.github.benmanes.caffeine.cache.AsyncLoadingCache<java.lang.String,​java.util.Set<java.lang.String>> childrenCache
      • existsCache

        protected final com.github.benmanes.caffeine.cache.AsyncLoadingCache<java.lang.String,​java.lang.Boolean> existsCache
      • zkSession

        protected java.util.concurrent.atomic.AtomicReference<org.apache.zookeeper.ZooKeeper> zkSession
    • Constructor Detail

      • ZooKeeperCache

        public ZooKeeperCache​(java.lang.String cacheName,
                              org.apache.zookeeper.ZooKeeper zkSession,
                              int zkOperationTimeoutSeconds,
                              org.apache.bookkeeper.common.util.OrderedExecutor executor)
      • ZooKeeperCache

        public ZooKeeperCache​(java.lang.String cacheName,
                              org.apache.zookeeper.ZooKeeper zkSession,
                              int zkOperationTimeoutSeconds,
                              org.apache.bookkeeper.common.util.OrderedExecutor executor,
                              int cacheExpirySeconds)
      • ZooKeeperCache

        public ZooKeeperCache​(java.lang.String cacheName,
                              org.apache.zookeeper.ZooKeeper zkSession,
                              int zkOperationTimeoutSeconds)
    • Method Detail

      • getZooKeeper

        public org.apache.zookeeper.ZooKeeper getZooKeeper()
      • invalidateAll

        public void invalidateAll()
      • invalidateAllData

        public void invalidateAllData()
      • invalidateAllChildren

        public void invalidateAllChildren()
      • invalidateData

        public void invalidateData​(java.lang.String path)
      • invalidateChildren

        public void invalidateChildren​(java.lang.String path)
      • asyncInvalidate

        public void asyncInvalidate​(java.lang.String path)
      • getZkOperationTimeoutSeconds

        public int getZkOperationTimeoutSeconds()
      • invalidate

        public void invalidate​(java.lang.String path)
      • exists

        public boolean exists​(java.lang.String path)
                       throws org.apache.zookeeper.KeeperException,
                              java.lang.InterruptedException
        Returns if the node at the given path exists in the cache
        Parameters:
        path - path of the node
        Returns:
        true if node exists, false if it does not
        Throws:
        org.apache.zookeeper.KeeperException
        java.lang.InterruptedException
      • existsAsync

        public java.util.concurrent.CompletableFuture<java.lang.Boolean> existsAsync​(java.lang.String path,
                                                                                     org.apache.zookeeper.Watcher watcher)
      • getData

        public <T> java.util.Optional<T> getData​(java.lang.String path,
                                                 ZooKeeperCache.Deserializer<T> deserializer)
                                          throws java.lang.Exception
        Simple ZooKeeperCache use this method to invalidate the cache entry on watch event w/o automatic reloading the cache
        Parameters:
        path -
        deserializer -
        Returns:
        Throws:
        java.lang.Exception
      • getEntry

        public <T> java.util.Optional<java.util.Map.Entry<T,​org.apache.zookeeper.data.Stat>> getEntry​(java.lang.String path,
                                                                                                            ZooKeeperCache.Deserializer<T> deserializer)
                                                                                                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getEntryAsync

        public <T> java.util.concurrent.CompletableFuture<java.util.Optional<java.util.Map.Entry<T,​org.apache.zookeeper.data.Stat>>> getEntryAsync​(java.lang.String path,
                                                                                                                                                         ZooKeeperCache.Deserializer<T> deserializer)
      • getDataAsync

        public <T> java.util.concurrent.CompletableFuture<java.util.Optional<T>> getDataAsync​(java.lang.String path,
                                                                                              ZooKeeperCache.Deserializer<T> deserializer)
      • getData

        public <T> java.util.Optional<java.util.Map.Entry<T,​org.apache.zookeeper.data.Stat>> getData​(java.lang.String path,
                                                                                                           org.apache.zookeeper.Watcher watcher,
                                                                                                           ZooKeeperCache.Deserializer<T> deserializer)
                                                                                                    throws java.lang.Exception
        Cache that implements automatic reloading on update will pass a different Watcher object to reload cache entry automatically
        Parameters:
        path -
        watcher -
        deserializer -
        Returns:
        Throws:
        java.lang.Exception
      • getDataAsync

        public <T> java.util.concurrent.CompletableFuture<java.util.Optional<java.util.Map.Entry<T,​org.apache.zookeeper.data.Stat>>> getDataAsync​(java.lang.String path,
                                                                                                                                                        org.apache.zookeeper.Watcher watcher,
                                                                                                                                                        ZooKeeperCache.Deserializer<T> deserializer)
      • getChildren

        public java.util.Set<java.lang.String> getChildren​(java.lang.String path)
                                                    throws org.apache.zookeeper.KeeperException,
                                                           java.lang.InterruptedException
        Simple ZooKeeperChildrenCache use this method to invalidate cache entry on watch event w/o automatic re-loading
        Parameters:
        path -
        Returns:
        Throws:
        org.apache.zookeeper.KeeperException
        java.lang.InterruptedException
      • getChildrenAsync

        public java.util.concurrent.CompletableFuture<java.util.Set<java.lang.String>> getChildrenAsync​(java.lang.String path,
                                                                                                        org.apache.zookeeper.Watcher watcher)
        ZooKeeperChildrenCache implementing automatic re-loading on update use this method by passing in a different Watcher object to reload cache entry
        Parameters:
        path -
        watcher -
        Returns:
      • getDataIfPresent

        public <T> T getDataIfPresent​(java.lang.String path)
      • getChildrenIfPresent

        public java.util.Set<java.lang.String> getChildrenIfPresent​(java.lang.String path)
      • process

        public void process​(org.apache.zookeeper.WatchedEvent event)
        Specified by:
        process in interface org.apache.zookeeper.Watcher
      • invalidateRoot

        public void invalidateRoot​(java.lang.String root)
      • stop

        public void stop()
      • checkRegNodeAndWaitExpired

        public boolean checkRegNodeAndWaitExpired​(java.lang.String regPath)
                                           throws java.io.IOException
        Throws:
        java.io.IOException