31
31
import java .util .Set ;
32
32
import java .util .TreeSet ;
33
33
import java .util .concurrent .ConcurrentMap ;
34
+ import java .util .concurrent .atomic .AtomicBoolean ;
34
35
import java .util .concurrent .atomic .AtomicLong ;
35
36
import java .util .function .Consumer ;
36
37
import java .util .function .Predicate ;
@@ -79,7 +80,7 @@ public interface ZooCacheWatcher extends Consumer<WatchedEvent> {}
79
80
80
81
private final ZooSession zk ;
81
82
82
- private volatile boolean closed = false ;
83
+ private final AtomicBoolean closed = new AtomicBoolean ( false ) ;
83
84
84
85
private final AtomicLong updateCount = new AtomicLong ();
85
86
@@ -349,7 +350,7 @@ private ZcInterruptedException(InterruptedException e) {
349
350
* @return children list, or null if node has no children or does not exist
350
351
*/
351
352
public List <String > getChildren (final String zPath ) {
352
- Preconditions .checkState (!closed );
353
+ Preconditions .checkState (!closed . get (), "Operation not allowed: ZooCache is already closed." );
353
354
ensureWatched (zPath );
354
355
ZooRunnable <List <String >> zr = new ZooRunnable <>() {
355
356
@@ -409,7 +410,7 @@ public byte[] get(final String zPath) {
409
410
* @return path data, or null if non-existent
410
411
*/
411
412
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." );
413
414
ensureWatched (zPath );
414
415
ZooRunnable <byte []> zr = new ZooRunnable <>() {
415
416
@@ -474,7 +475,7 @@ public byte[] run() throws KeeperException, InterruptedException {
474
475
* @param cachedStat cached statistic, that is or will be cached
475
476
*/
476
477
protected void copyStats (ZcStat userStat , ZcStat cachedStat ) {
477
- Preconditions .checkState (!closed );
478
+ Preconditions .checkState (!closed . get (), "Operation not allowed: ZooCache is already closed." );
478
479
if (userStat != null && cachedStat != null ) {
479
480
userStat .set (cachedStat );
480
481
}
@@ -484,23 +485,30 @@ protected void copyStats(ZcStat userStat, ZcStat cachedStat) {
484
485
* Clears this cache.
485
486
*/
486
487
protected void clear () {
487
- Preconditions .checkState (!closed );
488
+ if (closed .get ()) {
489
+ log .trace ("clear() called on closed ZooCache {}. Returning." , cacheId );
490
+ return ;
491
+ }
488
492
nodeCache .clear ();
489
493
updateCount .incrementAndGet ();
490
494
log .trace ("{} cleared all from cache" , cacheId );
491
495
}
492
496
493
497
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
+ }
496
504
}
497
505
498
506
/**
499
507
* Returns a monotonically increasing count of the number of time the cache was updated. If the
500
508
* count is the same, then it means cache did not change.
501
509
*/
502
510
public long getUpdateCount () {
503
- Preconditions .checkState (!closed );
511
+ Preconditions .checkState (!closed . get (), "Operation not allowed: ZooCache is already closed." );
504
512
return updateCount .get ();
505
513
}
506
514
@@ -534,7 +542,7 @@ public boolean childrenCached(String zPath) {
534
542
* Removes all paths in the cache match the predicate.
535
543
*/
536
544
public void clear (Predicate <String > pathPredicate ) {
537
- Preconditions .checkState (!closed );
545
+ Preconditions .checkState (!closed . get (), "Operation not allowed: ZooCache is already closed." );
538
546
Predicate <String > pathPredicateWrapper = path -> {
539
547
boolean testResult = pathPredicate .test (path );
540
548
if (testResult ) {
0 commit comments