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