1
1
package io .envoyproxy .controlplane .cache ;
2
2
3
3
import com .google .common .collect .ImmutableSet ;
4
- import java .util .HashMap ;
5
- import java .util .Map ;
6
4
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 ;
10
7
import java .util .function .BiFunction ;
11
- import javax .annotation .concurrent .GuardedBy ;
12
8
import javax .annotation .concurrent .ThreadSafe ;
13
9
14
10
/**
@@ -20,14 +16,7 @@ public class CacheStatusInfo<T> implements StatusInfo<T> {
20
16
21
17
private final T nodeGroup ;
22
18
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 <>();
31
20
private long lastWatchRequestTime ;
32
21
33
22
public CacheStatusInfo (T nodeGroup ) {
@@ -39,13 +28,7 @@ public CacheStatusInfo(T nodeGroup) {
39
28
*/
40
29
@ Override
41
30
public long lastWatchRequestTime () {
42
- readLock .lock ();
43
-
44
- try {
45
- return lastWatchRequestTime ;
46
- } finally {
47
- readLock .unlock ();
48
- }
31
+ return lastWatchRequestTime ;
49
32
}
50
33
51
34
/**
@@ -61,13 +44,7 @@ public T nodeGroup() {
61
44
*/
62
45
@ Override
63
46
public int numWatches () {
64
- readLock .lock ();
65
-
66
- try {
67
- return watches .size ();
68
- } finally {
69
- readLock .unlock ();
70
- }
47
+ return watches .size ();
71
48
}
72
49
73
50
/**
@@ -76,13 +53,7 @@ public int numWatches() {
76
53
* @param watchId the ID for the watch that should be removed
77
54
*/
78
55
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 );
86
57
}
87
58
88
59
/**
@@ -91,42 +62,24 @@ public void removeWatch(long watchId) {
91
62
* @param lastWatchRequestTime the latest watch request timestamp
92
63
*/
93
64
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 ;
101
66
}
102
67
103
68
/**
104
69
* Adds the given watch to the tracked collection of watches.
105
70
*
106
71
* @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
108
73
*/
109
74
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 );
117
76
}
118
77
119
78
/**
120
79
* Returns the set of IDs for all watched currently being tracked.
121
80
*/
122
81
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 ());
130
83
}
131
84
132
85
/**
@@ -136,12 +89,6 @@ public Set<Long> watchIds() {
136
89
* @param filter the function to execute on each watch
137
90
*/
138
91
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 ()));
146
93
}
147
94
}
0 commit comments