Skip to content

Commit

Permalink
Make ZooCache close() and clear() more idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
DomGarguilo committed Feb 14, 2025
1 parent 7f7cf05 commit aad0082
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Predicate;
Expand Down Expand Up @@ -79,7 +80,7 @@ public interface ZooCacheWatcher extends Consumer<WatchedEvent> {}

private final ZooSession zk;

private volatile boolean closed = false;
private final AtomicBoolean closed = new AtomicBoolean(false);

private final AtomicLong updateCount = new AtomicLong();

Expand Down Expand Up @@ -349,7 +350,7 @@ private ZcInterruptedException(InterruptedException e) {
* @return children list, or null if node has no children or does not exist
*/
public List<String> getChildren(final String zPath) {
Preconditions.checkState(!closed);
Preconditions.checkState(!closed.get(), "Operation not allowed: ZooCache is already closed.");
ensureWatched(zPath);
ZooRunnable<List<String>> zr = new ZooRunnable<>() {

Expand Down Expand Up @@ -409,7 +410,7 @@ public byte[] get(final String zPath) {
* @return path data, or null if non-existent
*/
public byte[] get(final String zPath, final ZcStat status) {
Preconditions.checkState(!closed);
Preconditions.checkState(!closed.get(), "Operation not allowed: ZooCache is already closed.");
ensureWatched(zPath);
ZooRunnable<byte[]> zr = new ZooRunnable<>() {

Expand Down Expand Up @@ -474,7 +475,7 @@ public byte[] run() throws KeeperException, InterruptedException {
* @param cachedStat cached statistic, that is or will be cached
*/
protected void copyStats(ZcStat userStat, ZcStat cachedStat) {
Preconditions.checkState(!closed);
Preconditions.checkState(!closed.get(), "Operation not allowed: ZooCache is already closed.");
if (userStat != null && cachedStat != null) {
userStat.set(cachedStat);
}
Expand All @@ -484,23 +485,30 @@ protected void copyStats(ZcStat userStat, ZcStat cachedStat) {
* Clears this cache.
*/
protected void clear() {
Preconditions.checkState(!closed);
if (closed.get()) {
log.trace("clear() called on closed ZooCache {}. Returning.", cacheId);
return;
}
nodeCache.clear();
updateCount.incrementAndGet();
log.trace("{} cleared all from cache", cacheId);
}

public void close() {
clear();
closed = true;
if (!closed.get()) {
clear();
closed.set(true);
} else {
log.trace("close() called on already closed ZooCache {}", cacheId);
}
}

/**
* Returns a monotonically increasing count of the number of time the cache was updated. If the
* count is the same, then it means cache did not change.
*/
public long getUpdateCount() {
Preconditions.checkState(!closed);
Preconditions.checkState(!closed.get(), "Operation not allowed: ZooCache is already closed.");
return updateCount.get();
}

Expand Down Expand Up @@ -534,7 +542,7 @@ public boolean childrenCached(String zPath) {
* Removes all paths in the cache match the predicate.
*/
public void clear(Predicate<String> pathPredicate) {
Preconditions.checkState(!closed);
Preconditions.checkState(!closed.get(), "Operation not allowed: ZooCache is already closed.");
Predicate<String> pathPredicateWrapper = path -> {
boolean testResult = pathPredicate.test(path);
if (testResult) {
Expand Down

0 comments on commit aad0082

Please sign in to comment.