Skip to content

Commit 3185adf

Browse files
authored
Refactor getConfiguration to use RealmContext (Part 2) (#1783)
1 parent 187d700 commit 3185adf

File tree

7 files changed

+24
-110
lines changed

7 files changed

+24
-110
lines changed

polaris-core/src/main/java/org/apache/polaris/core/config/PolarisConfigurationStore.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626
import java.util.Map;
27-
import org.apache.polaris.core.PolarisCallContext;
2827
import org.apache.polaris.core.context.RealmContext;
2928
import org.apache.polaris.core.entity.CatalogEntity;
3029
import org.slf4j.Logger;
@@ -37,48 +36,6 @@
3736
public interface PolarisConfigurationStore {
3837
Logger LOGGER = LoggerFactory.getLogger(PolarisConfigurationStore.class);
3938

40-
/**
41-
* Retrieve the current value for a configuration key. May be null if not set.
42-
*
43-
* <p>This function will be deprecated, it can not be called outside of active request scope, such
44-
* as background tasks (TaskExecutor). Please use the function getConfiguration(RealmContext
45-
* realmContext, String configName) to get the configuration value in a more robust way. TODO:
46-
* Remove this function and replace the usage with the function takes realm. Github issue
47-
* https://github.com/apache/polaris/issues/1775
48-
*
49-
* @param ctx the current call context
50-
* @param configName the name of the configuration key to check
51-
* @return the current value set for the configuration key or null if not set
52-
* @param <T> the type of the configuration value
53-
*/
54-
@Deprecated
55-
default <T> @Nullable T getConfiguration(PolarisCallContext ctx, String configName) {
56-
return null;
57-
}
58-
59-
/**
60-
* Retrieve the current value for a configuration key. If not set, return the non-null default
61-
* value.
62-
*
63-
* <p>This function will be deprecated, it can not be called outside of active request scope, such
64-
* as background tasks (TaskExecutor). Please use the function getConfiguration(RealmContext
65-
* realmContext, String configName, T defaultValue) to get the configuration value in a more
66-
* robust way. TODO: Remove this function and replace the usage with the function takes realm.
67-
* Github issue https://github.com/apache/polaris/issues/1775
68-
*
69-
* @param ctx the current call context
70-
* @param configName the name of the configuration key to check
71-
* @param defaultValue the default value if the configuration key has no value
72-
* @return the current value or the supplied default value
73-
*/
74-
@Deprecated
75-
default <T> @Nonnull T getConfiguration(
76-
PolarisCallContext ctx, String configName, @Nonnull T defaultValue) {
77-
Preconditions.checkNotNull(defaultValue, "Cannot pass null as a default value");
78-
T configValue = getConfiguration(ctx, configName);
79-
return configValue != null ? configValue : defaultValue;
80-
}
81-
8239
/**
8340
* Retrieve the current value for a configuration key for a given realm. May be null if not set.
8441
*
@@ -102,7 +59,7 @@ public interface PolarisConfigurationStore {
10259
* @param <T> the type of the configuration value
10360
*/
10461
default <T> @Nonnull T getConfiguration(
105-
RealmContext realmContext, String configName, @Nonnull T defaultValue) {
62+
@Nonnull RealmContext realmContext, String configName, @Nonnull T defaultValue) {
10663
Preconditions.checkNotNull(defaultValue, "Cannot pass null as a default value");
10764
T configValue = getConfiguration(realmContext, configName);
10865
return configValue != null ? configValue : defaultValue;
@@ -141,7 +98,7 @@ public interface PolarisConfigurationStore {
14198
* @param <T> the type of the configuration value
14299
*/
143100
default <T> @Nonnull T getConfiguration(
144-
RealmContext realmContext, PolarisConfiguration<T> config) {
101+
@Nonnull RealmContext realmContext, PolarisConfiguration<T> config) {
145102
T result = getConfiguration(realmContext, config.key, config.defaultValue);
146103
return tryCast(config, result);
147104
}
@@ -157,7 +114,7 @@ public interface PolarisConfigurationStore {
157114
* @param <T> the type of the configuration value
158115
*/
159116
default <T> @Nonnull T getConfiguration(
160-
RealmContext realmContext,
117+
@Nonnull RealmContext realmContext,
161118
@Nonnull CatalogEntity catalogEntity,
162119
PolarisConfiguration<T> config) {
163120
if (config.hasCatalogConfig() || config.hasCatalogConfigUnsafe()) {

polaris-core/src/main/java/org/apache/polaris/core/persistence/AtomicOperationMetaStoreManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ private void revokeGrantRecord(
15171517
callCtx
15181518
.getConfigurationStore()
15191519
.getConfiguration(
1520-
callCtx,
1520+
callCtx.getRealmContext(),
15211521
PolarisTaskConstants.TASK_TIMEOUT_MILLIS_CONFIG,
15221522
PolarisTaskConstants.TASK_TIMEOUT_MILLIS);
15231523
return taskState == null

polaris-core/src/main/java/org/apache/polaris/core/persistence/transactional/TransactionalMetaStoreManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
19561956
callCtx
19571957
.getConfigurationStore()
19581958
.getConfiguration(
1959-
callCtx,
1959+
callCtx.getRealmContext(),
19601960
PolarisTaskConstants.TASK_TIMEOUT_MILLIS_CONFIG,
19611961
PolarisTaskConstants.TASK_TIMEOUT_MILLIS);
19621962
return taskState == null

polaris-core/src/test/java/org/apache/polaris/core/storage/InMemoryStorageIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public void testValidateAccessToLocationsWithWildcard() {
102102
new PolarisConfigurationStore() {
103103
@SuppressWarnings("unchecked")
104104
@Override
105-
public <T> @Nullable T getConfiguration(RealmContext ctx, String configName) {
105+
public <T> @Nullable T getConfiguration(
106+
@Nonnull RealmContext ctx, String configName) {
106107
return (T) config.get(configName);
107108
}
108109
},

polaris-core/src/test/java/org/apache/polaris/service/storage/PolarisConfigurationStoreTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.polaris.service.storage;
2020

21+
import jakarta.annotation.Nonnull;
2122
import jakarta.annotation.Nullable;
2223
import java.util.List;
2324
import java.util.Map;
@@ -53,7 +54,7 @@ public void testConfigsCanBeCastedFromString() {
5354
*/
5455
@SuppressWarnings("unchecked")
5556
@Override
56-
public <T> @Nullable T getConfiguration(RealmContext ctx, String configName) {
57+
public <T> @Nullable T getConfiguration(@Nonnull RealmContext ctx, String configName) {
5758
for (PolarisConfiguration<?> c : configs) {
5859
if (c.key.equals(configName)) {
5960
return (T) String.valueOf(c.defaultValue);
@@ -84,7 +85,7 @@ public void testInvalidCastThrowsException() {
8485
new PolarisConfigurationStore() {
8586
@SuppressWarnings("unchecked")
8687
@Override
87-
public <T> T getConfiguration(RealmContext ctx, String configName) {
88+
public <T> T getConfiguration(@Nonnull RealmContext ctx, String configName) {
8889
return (T) "abc123";
8990
}
9091
};
@@ -163,7 +164,8 @@ public void testEntityOverrides() {
163164
PolarisConfigurationStore store =
164165
new PolarisConfigurationStore() {
165166
@Override
166-
public <T> @Nullable T getConfiguration(RealmContext realmContext, String configName) {
167+
public <T> @Nullable T getConfiguration(
168+
@Nonnull RealmContext realmContext, String configName) {
167169
//noinspection unchecked
168170
return (T) Map.of("key2", "config-value2").get(configName);
169171
}

quarkus/service/src/test/java/org/apache/polaris/service/quarkus/config/DefaultConfigurationStoreTest.java

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,15 @@
2020

2121
import static org.assertj.core.api.Assertions.assertThat;
2222

23-
import io.quarkus.test.junit.QuarkusMock;
2423
import io.quarkus.test.junit.QuarkusTest;
2524
import io.quarkus.test.junit.QuarkusTestProfile;
2625
import io.quarkus.test.junit.TestProfile;
2726
import jakarta.inject.Inject;
28-
import java.time.Clock;
2927
import java.util.Map;
30-
import org.apache.polaris.core.PolarisCallContext;
31-
import org.apache.polaris.core.PolarisDiagnostics;
3228
import org.apache.polaris.core.config.FeatureConfiguration;
3329
import org.apache.polaris.core.config.PolarisConfigurationStore;
3430
import org.apache.polaris.core.context.RealmContext;
3531
import org.apache.polaris.core.entity.CatalogEntity;
36-
import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
3732
import org.apache.polaris.service.config.DefaultConfigurationStore;
3833
import org.apache.polaris.service.config.FeaturesConfiguration;
3934
import org.assertj.core.api.Assertions;
@@ -73,12 +68,9 @@ public Map<String, String> getConfigOverrides() {
7368
}
7469
}
7570

76-
private PolarisCallContext polarisContext;
7771
private RealmContext realmContext;
7872

79-
@Inject MetaStoreManagerFactory managerFactory;
8073
@Inject PolarisConfigurationStore configurationStore;
81-
@Inject PolarisDiagnostics diagServices;
8274
@Inject FeaturesConfiguration featuresConfiguration;
8375

8476
@BeforeEach
@@ -89,60 +81,42 @@ public void before(TestInfo testInfo) {
8981
testInfo.getTestMethod().map(java.lang.reflect.Method::getName).orElse("test"),
9082
System.nanoTime());
9183
realmContext = () -> realmName;
92-
polarisContext =
93-
new PolarisCallContext(
94-
realmContext,
95-
managerFactory.getOrCreateSessionSupplier(realmContext).get(),
96-
diagServices,
97-
configurationStore,
98-
Clock.systemDefaultZone());
99-
}
100-
101-
@Test
102-
public void testGetConfigurationWithNoRealmContext() {
103-
Assertions.assertThatThrownBy(
104-
() -> configurationStore.getConfiguration(polarisContext, "missingKeyWithoutDefault"))
105-
.isInstanceOf(IllegalStateException.class);
10684
}
10785

10886
@Test
10987
public void testGetConfiguration() {
110-
QuarkusMock.installMockForType(realmContext, RealmContext.class);
111-
Object value = configurationStore.getConfiguration(polarisContext, "missingKeyWithoutDefault");
88+
Object value = configurationStore.getConfiguration(realmContext, "missingKeyWithoutDefault");
11289
assertThat(value).isNull();
11390
Object defaultValue =
114-
configurationStore.getConfiguration(
115-
polarisContext, "missingKeyWithDefault", "defaultValue");
91+
configurationStore.getConfiguration(realmContext, "missingKeyWithDefault", "defaultValue");
11692
assertThat(defaultValue).isEqualTo("defaultValue");
11793

11894
// the falseByDefaultKey is set to false for all realms in Profile.getConfigOverrides
119-
assertThat((Boolean) configurationStore.getConfiguration(polarisContext, falseByDefaultKey))
95+
assertThat((Boolean) configurationStore.getConfiguration(realmContext, falseByDefaultKey))
12096
.isFalse();
12197
// the trueByDefaultKey is set to true for all realms in Profile.getConfigOverrides
122-
assertThat((Boolean) configurationStore.getConfiguration(polarisContext, trueByDefaultKey))
98+
assertThat((Boolean) configurationStore.getConfiguration(realmContext, trueByDefaultKey))
12399
.isTrue();
124100
}
125101

126102
@Test
127103
public void testGetRealmConfiguration() {
128104
// check the realmOne configuration
129-
QuarkusMock.installMockForType(realmOneContext, RealmContext.class);
130105
// the falseByDefaultKey is set to `false` for all realms, but overwrite with value `true` for
131106
// realmOne.
132-
assertThat((Boolean) configurationStore.getConfiguration(polarisContext, falseByDefaultKey))
107+
assertThat((Boolean) configurationStore.getConfiguration(realmOneContext, falseByDefaultKey))
133108
.isTrue();
134109
// the trueByDefaultKey is set to `false` for all realms, no overwrite for realmOne
135-
assertThat((Boolean) configurationStore.getConfiguration(polarisContext, trueByDefaultKey))
110+
assertThat((Boolean) configurationStore.getConfiguration(realmOneContext, trueByDefaultKey))
136111
.isTrue();
137112

138113
// check the realmTwo configuration
139-
QuarkusMock.installMockForType(realmTwoContext, RealmContext.class);
140114
// the falseByDefaultKey is set to `false` for all realms, no overwrite for realmTwo
141-
assertThat((Boolean) configurationStore.getConfiguration(polarisContext, falseByDefaultKey))
115+
assertThat((Boolean) configurationStore.getConfiguration(realmTwoContext, falseByDefaultKey))
142116
.isFalse();
143117
// the trueByDefaultKey is set to `false` for all realms, and overwrite with value `false` for
144118
// realmTwo
145-
assertThat((Boolean) configurationStore.getConfiguration(polarisContext, trueByDefaultKey))
119+
assertThat((Boolean) configurationStore.getConfiguration(realmTwoContext, trueByDefaultKey))
146120
.isFalse();
147121
}
148122

@@ -167,28 +141,24 @@ void testGetConfigurationWithRealm() {
167141

168142
@Test
169143
public void testInjectedConfigurationStore() {
170-
QuarkusMock.installMockForType(realmContext, RealmContext.class);
171144
// the default value for trueByDefaultKey is `true`
172-
boolean featureDefaultValue =
173-
configurationStore.getConfiguration(polarisContext, trueByDefaultKey);
145+
Boolean featureDefaultValue =
146+
configurationStore.getConfiguration(realmContext, trueByDefaultKey);
174147
assertThat(featureDefaultValue).isTrue();
175148

176-
QuarkusMock.installMockForType(realmTwoContext, RealmContext.class);
177149
// the value for falseByDefaultKey is `false`, and no realm override for realmTwo
178-
boolean realmTwoValue = configurationStore.getConfiguration(polarisContext, falseByDefaultKey);
150+
Boolean realmTwoValue = configurationStore.getConfiguration(realmTwoContext, falseByDefaultKey);
179151
assertThat(realmTwoValue).isFalse();
180152

181153
// Now, realmOne override falseByDefaultKey to `True`
182-
QuarkusMock.installMockForType(realmOneContext, RealmContext.class);
183-
boolean realmOneValue = configurationStore.getConfiguration(polarisContext, falseByDefaultKey);
154+
Boolean realmOneValue = configurationStore.getConfiguration(realmOneContext, falseByDefaultKey);
184155
assertThat(realmOneValue).isTrue();
185156

186157
assertThat(configurationStore).isInstanceOf(DefaultConfigurationStore.class);
187158
}
188159

189160
@Test
190161
public void testInjectedFeaturesConfiguration() {
191-
QuarkusMock.installMockForType(realmContext, RealmContext.class);
192162
assertThat(featuresConfiguration).isInstanceOf(QuarkusResolvedFeaturesConfiguration.class);
193163

194164
assertThat(featuresConfiguration.defaults())
@@ -209,7 +179,6 @@ public void testInjectedFeaturesConfiguration() {
209179

210180
@Test
211181
public void testRegisterAndUseFeatureConfigurations() {
212-
QuarkusMock.installMockForType(realmContext, RealmContext.class);
213182
String prefix = "testRegisterAndUseFeatureConfigurations";
214183

215184
FeatureConfiguration<Boolean> safeConfig =

service/common/src/main/java/org/apache/polaris/service/config/DefaultConfigurationStore.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
import jakarta.annotation.Nonnull;
2323
import jakarta.annotation.Nullable;
2424
import jakarta.enterprise.context.ApplicationScoped;
25-
import jakarta.enterprise.inject.Instance;
2625
import jakarta.inject.Inject;
2726
import java.util.Map;
2827
import java.util.Optional;
29-
import org.apache.polaris.core.PolarisCallContext;
3028
import org.apache.polaris.core.config.PolarisConfigurationStore;
3129
import org.apache.polaris.core.context.RealmContext;
3230
import org.slf4j.Logger;
@@ -38,7 +36,6 @@ public class DefaultConfigurationStore implements PolarisConfigurationStore {
3836

3937
private final Map<String, Object> defaults;
4038
private final Map<String, Map<String, Object>> realmOverrides;
41-
@Inject private Instance<RealmContext> realmContextInstance;
4239

4340
// FIXME the whole PolarisConfigurationStore + PolarisConfiguration needs to be refactored
4441
// to become a proper Quarkus configuration object
@@ -61,18 +58,6 @@ public DefaultConfigurationStore(
6158
return confgValue;
6259
}
6360

64-
@Override
65-
public <T> @Nullable T getConfiguration(@Nonnull PolarisCallContext ctx, String configName) {
66-
if (realmContextInstance.isResolvable()) {
67-
RealmContext realmContext = realmContextInstance.get();
68-
return getConfiguration(realmContext, configName);
69-
} else {
70-
LOGGER.debug(
71-
"No RealmContext is injected when lookup value for configuration {} ", configName);
72-
return getDefaultConfiguration(configName);
73-
}
74-
}
75-
7661
private <T> @Nullable T getDefaultConfiguration(String configName) {
7762
@SuppressWarnings("unchecked")
7863
T confgValue = (T) defaults.get(configName);

0 commit comments

Comments
 (0)