Skip to content

Commit 4a0646e

Browse files
sschepensjakubdyszkiewicz
authored andcommitted
Replace lock on CacheStatusInfo with ConcurrentMap
Signed-off-by: Sebastian Schepens <[email protected]>
1 parent 96c1389 commit 4a0646e

File tree

1 file changed

+11
-64
lines changed

1 file changed

+11
-64
lines changed
Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package io.envoyproxy.controlplane.cache;
22

33
import com.google.common.collect.ImmutableSet;
4-
import java.util.HashMap;
5-
import java.util.Map;
64
import java.util.Set;
7-
import java.util.concurrent.locks.Lock;
8-
import java.util.concurrent.locks.ReadWriteLock;
9-
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
import java.util.concurrent.ConcurrentMap;
107
import java.util.function.BiFunction;
11-
import javax.annotation.concurrent.GuardedBy;
128
import javax.annotation.concurrent.ThreadSafe;
139

1410
/**
@@ -20,14 +16,7 @@ public class CacheStatusInfo<T> implements StatusInfo<T> {
2016

2117
private final T nodeGroup;
2218

23-
@GuardedBy("lock")
24-
private final Map<Long, Watch> watches = new HashMap<>();
25-
26-
private final ReadWriteLock lock = new ReentrantReadWriteLock();
27-
private final Lock readLock = lock.readLock();
28-
private final Lock writeLock = lock.writeLock();
29-
30-
@GuardedBy("lock")
19+
private final ConcurrentMap<Long, Watch> watches = new ConcurrentHashMap<>();
3120
private long lastWatchRequestTime;
3221

3322
public CacheStatusInfo(T nodeGroup) {
@@ -39,13 +28,7 @@ public CacheStatusInfo(T nodeGroup) {
3928
*/
4029
@Override
4130
public long lastWatchRequestTime() {
42-
readLock.lock();
43-
44-
try {
45-
return lastWatchRequestTime;
46-
} finally {
47-
readLock.unlock();
48-
}
31+
return lastWatchRequestTime;
4932
}
5033

5134
/**
@@ -61,13 +44,7 @@ public T nodeGroup() {
6144
*/
6245
@Override
6346
public int numWatches() {
64-
readLock.lock();
65-
66-
try {
67-
return watches.size();
68-
} finally {
69-
readLock.unlock();
70-
}
47+
return watches.size();
7148
}
7249

7350
/**
@@ -76,13 +53,7 @@ public int numWatches() {
7653
* @param watchId the ID for the watch that should be removed
7754
*/
7855
public void removeWatch(long watchId) {
79-
writeLock.lock();
80-
81-
try {
82-
watches.remove(watchId);
83-
} finally {
84-
writeLock.unlock();
85-
}
56+
watches.remove(watchId);
8657
}
8758

8859
/**
@@ -91,42 +62,24 @@ public void removeWatch(long watchId) {
9162
* @param lastWatchRequestTime the latest watch request timestamp
9263
*/
9364
public void setLastWatchRequestTime(long lastWatchRequestTime) {
94-
writeLock.lock();
95-
96-
try {
97-
this.lastWatchRequestTime = lastWatchRequestTime;
98-
} finally {
99-
writeLock.unlock();
100-
}
65+
this.lastWatchRequestTime = lastWatchRequestTime;
10166
}
10267

10368
/**
10469
* Adds the given watch to the tracked collection of watches.
10570
*
10671
* @param watchId the ID for the watch that should be added
107-
* @param watch the watch that should be added
72+
* @param watch the watch that should be added
10873
*/
10974
public void setWatch(long watchId, Watch watch) {
110-
writeLock.lock();
111-
112-
try {
113-
watches.put(watchId, watch);
114-
} finally {
115-
writeLock.unlock();
116-
}
75+
watches.put(watchId, watch);
11776
}
11877

11978
/**
12079
* Returns the set of IDs for all watched currently being tracked.
12180
*/
12281
public Set<Long> watchIds() {
123-
readLock.lock();
124-
125-
try {
126-
return ImmutableSet.copyOf(watches.keySet());
127-
} finally {
128-
readLock.unlock();
129-
}
82+
return ImmutableSet.copyOf(watches.keySet());
13083
}
13184

13285
/**
@@ -136,12 +89,6 @@ public Set<Long> watchIds() {
13689
* @param filter the function to execute on each watch
13790
*/
13891
public void watchesRemoveIf(BiFunction<Long, Watch, Boolean> filter) {
139-
writeLock.lock();
140-
141-
try {
142-
watches.entrySet().removeIf(entry -> filter.apply(entry.getKey(), entry.getValue()));
143-
} finally {
144-
writeLock.unlock();
145-
}
92+
watches.entrySet().removeIf(entry -> filter.apply(entry.getKey(), entry.getValue()));
14693
}
14794
}

0 commit comments

Comments
 (0)