Skip to content

Commit aad0082

Browse files
committed
Make ZooCache close() and clear() more idempotent
1 parent 7f7cf05 commit aad0082

File tree

1 file changed

+17
-9
lines changed
  • core/src/main/java/org/apache/accumulo/core/zookeeper

1 file changed

+17
-9
lines changed

core/src/main/java/org/apache/accumulo/core/zookeeper/ZooCache.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Set;
3232
import java.util.TreeSet;
3333
import java.util.concurrent.ConcurrentMap;
34+
import java.util.concurrent.atomic.AtomicBoolean;
3435
import java.util.concurrent.atomic.AtomicLong;
3536
import java.util.function.Consumer;
3637
import java.util.function.Predicate;
@@ -79,7 +80,7 @@ public interface ZooCacheWatcher extends Consumer<WatchedEvent> {}
7980

8081
private final ZooSession zk;
8182

82-
private volatile boolean closed = false;
83+
private final AtomicBoolean closed = new AtomicBoolean(false);
8384

8485
private final AtomicLong updateCount = new AtomicLong();
8586

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

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

@@ -474,7 +475,7 @@ public byte[] run() throws KeeperException, InterruptedException {
474475
* @param cachedStat cached statistic, that is or will be cached
475476
*/
476477
protected void copyStats(ZcStat userStat, ZcStat cachedStat) {
477-
Preconditions.checkState(!closed);
478+
Preconditions.checkState(!closed.get(), "Operation not allowed: ZooCache is already closed.");
478479
if (userStat != null && cachedStat != null) {
479480
userStat.set(cachedStat);
480481
}
@@ -484,23 +485,30 @@ protected void copyStats(ZcStat userStat, ZcStat cachedStat) {
484485
* Clears this cache.
485486
*/
486487
protected void clear() {
487-
Preconditions.checkState(!closed);
488+
if (closed.get()) {
489+
log.trace("clear() called on closed ZooCache {}. Returning.", cacheId);
490+
return;
491+
}
488492
nodeCache.clear();
489493
updateCount.incrementAndGet();
490494
log.trace("{} cleared all from cache", cacheId);
491495
}
492496

493497
public void close() {
494-
clear();
495-
closed = true;
498+
if (!closed.get()) {
499+
clear();
500+
closed.set(true);
501+
} else {
502+
log.trace("close() called on already closed ZooCache {}", cacheId);
503+
}
496504
}
497505

498506
/**
499507
* Returns a monotonically increasing count of the number of time the cache was updated. If the
500508
* count is the same, then it means cache did not change.
501509
*/
502510
public long getUpdateCount() {
503-
Preconditions.checkState(!closed);
511+
Preconditions.checkState(!closed.get(), "Operation not allowed: ZooCache is already closed.");
504512
return updateCount.get();
505513
}
506514

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

0 commit comments

Comments
 (0)