22
22
import java .util .concurrent .ThreadFactory ;
23
23
import java .util .concurrent .TimeUnit ;
24
24
import java .util .concurrent .atomic .AtomicBoolean ;
25
+ import java .util .concurrent .atomic .AtomicInteger ;
25
26
26
27
public class UniqueKeysTrackerImp implements UniqueKeysTracker {
27
28
private static final Logger _log = LoggerFactory .getLogger (UniqueKeysTrackerImp .class );
28
29
private static final double MARGIN_ERROR = 0.01 ;
29
30
private static final int MAX_UNIQUE_KEYS_POST_SIZE = 5000 ;
30
31
private static final int MAX_AMOUNT_OF_KEYS = 10000000 ;
31
- private int trackerKeysSize = 0 ;
32
+ private final AtomicInteger trackerKeysSize = new AtomicInteger ( 0 ) ;
32
33
private FilterAdapter filterAdapter ;
33
34
private final TelemetrySynchronizer _telemetrySynchronizer ;
34
35
private final ScheduledExecutorService _uniqueKeysSyncScheduledExecutorService ;
@@ -61,11 +62,11 @@ public boolean track(String featureFlagName, String key) {
61
62
(feature , current ) -> {
62
63
HashSet <String > keysByFeature = Optional .ofNullable (current ).orElse (new HashSet <>());
63
64
keysByFeature .add (key );
64
- trackerKeysSize ++ ;
65
+ trackerKeysSize . incrementAndGet () ;
65
66
return keysByFeature ;
66
67
});
67
68
_logger .debug ("The feature flag " + featureFlagName + " and key " + key + " was added" );
68
- if (trackerKeysSize >= MAX_UNIQUE_KEYS_POST_SIZE ){
69
+ if (trackerKeysSize . intValue () >= MAX_UNIQUE_KEYS_POST_SIZE ){
69
70
_logger .warn ("The UniqueKeysTracker size reached the maximum limit" );
70
71
try {
71
72
sendUniqueKeys ();
@@ -110,7 +111,7 @@ public HashMap<String,HashSet<String>> popAll(){
110
111
HashSet <String > value = uniqueKeysTracker .remove (key );
111
112
toReturn .put (key , value );
112
113
}
113
- trackerKeysSize = 0 ;
114
+ trackerKeysSize . set ( 0 ) ;
114
115
return toReturn ;
115
116
}
116
117
@@ -121,7 +122,7 @@ private void sendUniqueKeys(){
121
122
}
122
123
123
124
try {
124
- if (uniqueKeysTracker .size () == 0 ) {
125
+ if (uniqueKeysTracker .isEmpty () ) {
125
126
_log .debug ("The Unique Keys Tracker is empty" );
126
127
return ;
127
128
}
0 commit comments