Skip to content

Commit 6452727

Browse files
authored
Make ZooCache close() and clear() more idempotent (apache#5338)
* Return early in clear() and close() if this ZooCache has already been closed. This avoids throwing an exception
1 parent 30272f7 commit 6452727

File tree

1 file changed

+11
-6
lines changed
  • core/src/main/java/org/apache/accumulo/core/zookeeper

1 file changed

+11
-6
lines changed

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ private ZcInterruptedException(InterruptedException e) {
349349
* @return children list, or null if node has no children or does not exist
350350
*/
351351
public List<String> getChildren(final String zPath) {
352-
Preconditions.checkState(!closed);
352+
Preconditions.checkState(!closed, "Operation not allowed: ZooCache is already closed.");
353353
ensureWatched(zPath);
354354
ZooRunnable<List<String>> zr = new ZooRunnable<>() {
355355

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

@@ -474,7 +474,7 @@ public byte[] run() throws KeeperException, InterruptedException {
474474
* @param cachedStat cached statistic, that is or will be cached
475475
*/
476476
protected void copyStats(ZcStat userStat, ZcStat cachedStat) {
477-
Preconditions.checkState(!closed);
477+
Preconditions.checkState(!closed, "Operation not allowed: ZooCache is already closed.");
478478
if (userStat != null && cachedStat != null) {
479479
userStat.set(cachedStat);
480480
}
@@ -484,13 +484,18 @@ protected void copyStats(ZcStat userStat, ZcStat cachedStat) {
484484
* Clears this cache.
485485
*/
486486
protected void clear() {
487-
Preconditions.checkState(!closed);
487+
if (closed) {
488+
return;
489+
}
488490
nodeCache.clear();
489491
updateCount.incrementAndGet();
490492
log.trace("{} cleared all from cache", cacheId);
491493
}
492494

493495
public void close() {
496+
if (closed) {
497+
return;
498+
}
494499
clear();
495500
closed = true;
496501
}
@@ -500,7 +505,7 @@ public void close() {
500505
* count is the same, then it means cache did not change.
501506
*/
502507
public long getUpdateCount() {
503-
Preconditions.checkState(!closed);
508+
Preconditions.checkState(!closed, "Operation not allowed: ZooCache is already closed.");
504509
return updateCount.get();
505510
}
506511

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

0 commit comments

Comments
 (0)