1818 */
1919package org .apache .polaris .core .context ;
2020
21- import jakarta .annotation .Nonnull ;
22- import java .io .IOException ;
23- import java .util .HashMap ;
24- import java .util .Map ;
25- import java .util .stream .Collectors ;
26- import org .apache .iceberg .io .CloseableGroup ;
2721import org .apache .polaris .core .PolarisCallContext ;
2822import org .apache .polaris .core .PolarisDiagnostics ;
29- import org .slf4j .Logger ;
30- import org .slf4j .LoggerFactory ;
3123
3224/**
3325 * Stores elements associated with an individual REST request such as RealmContext, caller
3729 * principal/role entities may be defined within a Realm-specific persistence layer, and the
3830 * underlying nature of the persistence layer may differ between different realms.
3931 */
40- public interface CallContext extends AutoCloseable {
32+ public interface CallContext {
4133 InheritableThreadLocal <CallContext > CURRENT_CONTEXT = new InheritableThreadLocal <>();
4234
43- // For requests that make use of a Catalog instance, this holds the instance that was
44- // created, scoped to the current call context.
45- String REQUEST_PATH_CATALOG_INSTANCE_KEY = "REQUEST_PATH_CATALOG_INSTANCE" ;
46-
47- String CLOSEABLES = "closeables" ;
48-
4935 static CallContext setCurrentContext (CallContext context ) {
5036 CURRENT_CONTEXT .set (context );
5137 return context ;
@@ -65,7 +51,6 @@ static void unsetCurrentContext() {
6551
6652 static CallContext of (
6753 final RealmContext realmContext , final PolarisCallContext polarisCallContext ) {
68- Map <String , Object > map = new HashMap <>();
6954 return new CallContext () {
7055 @ Override
7156 public RealmContext getRealmContext () {
@@ -76,28 +61,14 @@ public RealmContext getRealmContext() {
7661 public PolarisCallContext getPolarisCallContext () {
7762 return polarisCallContext ;
7863 }
79-
80- @ Override
81- public Map <String , Object > contextVariables () {
82- return map ;
83- }
8464 };
8565 }
8666
87- /**
88- * Copy the {@link CallContext}. {@link #contextVariables()} will be copied except for {@link
89- * #closeables()}. The original {@link #contextVariables()} map is untouched and {@link
90- * #closeables()} in the original {@link CallContext} should be closed along with the {@link
91- * CallContext}.
92- */
67+ /** Copy the {@link CallContext}. */
9368 static CallContext copyOf (CallContext base ) {
9469 String realmId = base .getRealmContext ().getRealmIdentifier ();
9570 RealmContext realmContext = () -> realmId ;
9671 PolarisCallContext polarisCallContext = PolarisCallContext .copyOf (base .getPolarisCallContext ());
97- Map <String , Object > contextVariables =
98- base .contextVariables ().entrySet ().stream ()
99- .filter (e -> !e .getKey ().equals (CLOSEABLES ))
100- .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
10172 return new CallContext () {
10273 @ Override
10374 public RealmContext getRealmContext () {
@@ -108,11 +79,6 @@ public RealmContext getRealmContext() {
10879 public PolarisCallContext getPolarisCallContext () {
10980 return polarisCallContext ;
11081 }
111-
112- @ Override
113- public Map <String , Object > contextVariables () {
114- return contextVariables ;
115- }
11682 };
11783 }
11884
@@ -122,28 +88,4 @@ public Map<String, Object> contextVariables() {
12288 * @return the inner context used for delegating services
12389 */
12490 PolarisCallContext getPolarisCallContext ();
125-
126- Map <String , Object > contextVariables ();
127-
128- default @ Nonnull CloseableGroup closeables () {
129- return (CloseableGroup )
130- contextVariables ().computeIfAbsent (CLOSEABLES , key -> new CloseableGroup ());
131- }
132-
133- @ Override
134- default void close () {
135- if (CURRENT_CONTEXT .get () == this ) {
136- unsetCurrentContext ();
137- CloseableGroup closeables = closeables ();
138- try {
139- closeables .close ();
140- } catch (IOException e ) {
141- Logger logger = LoggerFactory .getLogger (CallContext .class );
142- logger
143- .atWarn ()
144- .addKeyValue ("closeableGroup" , closeables )
145- .log ("Unable to close closeable group" , e );
146- }
147- }
148- }
14991}
0 commit comments