Package org.apache.pulsar.zookeeper
Class ZooKeeperCache
- java.lang.Object
-
- org.apache.pulsar.zookeeper.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.WatcherPer 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 givenZooKeeperCache.Deserializerargument.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceZooKeeperCache.CacheUpdater<T>static interfaceZooKeeperCache.Deserializer<T>
-
Field Summary
Fields Modifier and Type Field Description protected com.github.benmanes.caffeine.cache.AsyncLoadingCache<java.lang.String,java.util.Set<java.lang.String>>childrenCacheprotected 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>>dataCacheprotected com.github.benmanes.caffeine.cache.AsyncLoadingCache<java.lang.String,java.lang.Boolean>existsCachestatic java.lang.StringZK_CACHE_INSTANCEprotected 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 voidasyncInvalidate(java.lang.String path)booleancheckRegNodeAndWaitExpired(java.lang.String regPath)booleanexists(java.lang.String path)Returns if the node at the given path exists in the cachejava.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-loadingjava.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 entryjava.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> TgetDataIfPresent(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)intgetZkOperationTimeoutSeconds()org.apache.zookeeper.ZooKeepergetZooKeeper()voidinvalidate(java.lang.String path)voidinvalidateAll()voidinvalidateAllChildren()voidinvalidateAllData()voidinvalidateChildren(java.lang.String path)voidinvalidateData(java.lang.String path)voidinvalidateRoot(java.lang.String root)voidprocess(org.apache.zookeeper.WatchedEvent event)<T> voidprocess(org.apache.zookeeper.WatchedEvent event, ZooKeeperCache.CacheUpdater<T> updater)voidstop()
-
-
-
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()
-
process
public <T> void process(org.apache.zookeeper.WatchedEvent event, ZooKeeperCache.CacheUpdater<T> updater)
-
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.InterruptedExceptionReturns 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.KeeperExceptionjava.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.ExceptionSimple 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.ExceptionCache 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.InterruptedExceptionSimple ZooKeeperChildrenCache use this method to invalidate cache entry on watch event w/o automatic re-loading- Parameters:
path-- Returns:
- Throws:
org.apache.zookeeper.KeeperExceptionjava.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:
processin interfaceorg.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
-
-