18
18
19
19
import static com .google .common .base .Preconditions .checkNotNull ;
20
20
21
- import com .google .common .collect . Sets ;
21
+ import com .google .common .base . Throwables ;
22
22
import io .grpc .ChannelCredentials ;
23
23
import io .grpc .InsecureChannelCredentials ;
24
+ import io .grpc .InternalLogId ;
24
25
import io .grpc .SynchronizationContext ;
25
26
import io .grpc .xds .RlqsFilterConfig ;
26
27
import io .grpc .xds .client .Bootstrapper .RemoteServerInfo ;
28
+ import io .grpc .xds .client .XdsLogger ;
29
+ import io .grpc .xds .client .XdsLogger .XdsLogLevel ;
27
30
import java .util .Objects ;
28
- import java .util .Set ;
29
31
import java .util .concurrent .ConcurrentHashMap ;
32
+ import java .util .concurrent .ConcurrentMap ;
30
33
import java .util .concurrent .ScheduledExecutorService ;
31
- import java .util .logging .Level ;
32
- import java .util .logging .Logger ;
33
34
34
35
public final class RlqsCache {
35
- private static final Logger logger = Logger .getLogger (RlqsCache .class .getName ());
36
-
37
36
// TODO(sergiitk): [QUESTION] always in sync context?
38
37
private volatile boolean shutdown = false ;
39
- private final SynchronizationContext syncContext = new SynchronizationContext ((thread , error ) -> {
40
- String message = "Uncaught exception in RlqsCache SynchronizationContext. Panic!" ;
41
- logger .log (Level .FINE , message , error );
42
- throw new RlqsPoolSynchronizationException (message , error );
43
- });
44
-
45
- private final ConcurrentHashMap <Long , RlqsEngine > enginePool = new ConcurrentHashMap <>();
46
- Set <String > enginesToShutdown = Sets .newConcurrentHashSet ();
38
+
39
+ private final XdsLogger logger ;
40
+ private final SynchronizationContext syncContext ;
41
+
42
+ private final ConcurrentMap <Long , RlqsFilterState > filterStateCache = new ConcurrentHashMap <>();
47
43
private final ScheduledExecutorService scheduler ;
48
44
49
45
50
46
private RlqsCache (ScheduledExecutorService scheduler ) {
51
47
this .scheduler = checkNotNull (scheduler , "scheduler" );
48
+ // TODO(sergiitk): should be filter name?
49
+ logger = XdsLogger .withLogId (InternalLogId .allocate (this .getClass (), null ));
50
+
51
+ syncContext = new SynchronizationContext ((thread , error ) -> {
52
+ String message = "Uncaught exception in RlqsCache SynchronizationContext. Panic!" ;
53
+ logger .log (XdsLogLevel .DEBUG ,
54
+ message + " {0} \n Trace:\n {1}" , error , Throwables .getStackTraceAsString (error ));
55
+ throw new RlqsCacheSynchronizationException (message , error );
56
+ });
52
57
}
53
58
54
59
/** Creates an instance. */
@@ -64,37 +69,38 @@ public void shutdown() {
64
69
}
65
70
syncContext .execute (() -> {
66
71
shutdown = true ;
67
- logger .log (Level .FINER , "Shutting down RlqsCache" );
68
- enginesToShutdown .clear ();
69
- for (long configHash : enginePool .keySet ()) {
70
- enginePool .get (configHash ).shutdown ();
72
+ logger .log (XdsLogLevel .DEBUG , "Shutting down RlqsCache" );
73
+ for (long configHash : filterStateCache .keySet ()) {
74
+ filterStateCache .get (configHash ).shutdown ();
71
75
}
72
- enginePool .clear ();
76
+ filterStateCache .clear ();
73
77
shutdown = false ;
74
78
});
75
79
}
76
80
77
- public void shutdownRlqsEngine (RlqsFilterConfig oldConfig ) {
81
+ public void shutdownFilterState (RlqsFilterConfig oldConfig ) {
78
82
// TODO(sergiitk): shutdown one
83
+ // make it async.
79
84
}
80
85
81
- public RlqsEngine getOrCreateRlqsEngine (final RlqsFilterConfig config ) {
82
- long configHash = hashRlqsFilterConfig (config );
83
- return enginePool .computeIfAbsent (configHash , k -> newRlqsEngine (k , config ));
86
+ public RlqsFilterState getOrCreateFilterState (final RlqsFilterConfig config ) {
87
+ // TODO(sergiitk): handle being shut down.
88
+ long configHash = hashFilterConfig (config );
89
+ return filterStateCache .computeIfAbsent (configHash , k -> newFilterState (k , config ));
84
90
}
85
91
86
- private RlqsEngine newRlqsEngine (long configHash , RlqsFilterConfig config ) {
92
+ private RlqsFilterState newFilterState (long configHash , RlqsFilterConfig config ) {
87
93
// TODO(sergiitk): [IMPL] get channel creds from the bootstrap.
88
94
ChannelCredentials creds = InsecureChannelCredentials .create ();
89
- return new RlqsEngine (
95
+ return new RlqsFilterState (
90
96
RemoteServerInfo .create (config .rlqsService ().targetUri (), creds ),
91
97
config .domain (),
92
98
config .bucketMatchers (),
93
99
configHash ,
94
100
scheduler );
95
101
}
96
102
97
- private long hashRlqsFilterConfig (RlqsFilterConfig config ) {
103
+ private long hashFilterConfig (RlqsFilterConfig config ) {
98
104
// TODO(sergiitk): [QUESTION] better name? - ask Eric.
99
105
// TODO(sergiitk): [DESIGN] the key should be hashed (domain + buckets) merged config?
100
106
// TODO(sergiitk): [IMPL] Hash buckets
@@ -111,13 +117,11 @@ private long hashRlqsFilterConfig(RlqsFilterConfig config) {
111
117
/**
112
118
* Throws when fail to bootstrap or initialize the XdsClient.
113
119
*/
114
- public static final class RlqsPoolSynchronizationException extends RuntimeException {
120
+ public static final class RlqsCacheSynchronizationException extends RuntimeException {
115
121
private static final long serialVersionUID = 1L ;
116
122
117
- public RlqsPoolSynchronizationException (String message , Throwable cause ) {
123
+ public RlqsCacheSynchronizationException (String message , Throwable cause ) {
118
124
super (message , cause );
119
125
}
120
126
}
121
-
122
-
123
127
}
0 commit comments