Skip to content

Commit 2c2052c

Browse files
authored
Use application-scoped StorageCredentialCache (#2022)
Since `StorageCredentialCache` is application scoped and after 6ddd148 its constructor no longer uses the `RealmContext` passed into `getOrCreateStorageCredentialCache` we can now let all `PolarisEntityManager` instances share the same `StorageCredentialCache`.
1 parent 1b073bc commit 2c2052c

File tree

15 files changed

+118
-57
lines changed

15 files changed

+118
-57
lines changed

persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetaStoreManagerFactory.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import org.apache.polaris.core.persistence.dao.entity.EntityResult;
5555
import org.apache.polaris.core.persistence.dao.entity.PrincipalSecretsResult;
5656
import org.apache.polaris.core.storage.PolarisStorageIntegrationProvider;
57-
import org.apache.polaris.core.storage.cache.StorageCredentialCache;
5857
import org.slf4j.Logger;
5958
import org.slf4j.LoggerFactory;
6059

@@ -70,7 +69,6 @@ public class JdbcMetaStoreManagerFactory implements MetaStoreManagerFactory {
7069
private static final Logger LOGGER = LoggerFactory.getLogger(JdbcMetaStoreManagerFactory.class);
7170

7271
final Map<String, PolarisMetaStoreManager> metaStoreManagerMap = new HashMap<>();
73-
final Map<String, StorageCredentialCache> storageCredentialCacheMap = new HashMap<>();
7472
final Map<String, EntityCache> entityCacheMap = new HashMap<>();
7573
final Map<String, Supplier<BasePersistence>> sessionSupplierMap = new HashMap<>();
7674
protected final PolarisDiagnostics diagServices = new PolarisDefaultDiagServiceImpl();
@@ -183,7 +181,6 @@ public Map<String, BaseResult> purgeRealms(Iterable<String> realms) {
183181
BaseResult result = metaStoreManager.purge(callContext);
184182
results.put(realm, result);
185183

186-
storageCredentialCacheMap.remove(realm);
187184
sessionSupplierMap.remove(realm);
188185
metaStoreManagerMap.remove(realm);
189186
}
@@ -218,17 +215,6 @@ public synchronized Supplier<BasePersistence> getOrCreateSessionSupplier(
218215
return sessionSupplierMap.get(realmContext.getRealmIdentifier());
219216
}
220217

221-
@Override
222-
public synchronized StorageCredentialCache getOrCreateStorageCredentialCache(
223-
RealmContext realmContext, RealmConfig realmConfig) {
224-
if (!storageCredentialCacheMap.containsKey(realmContext.getRealmIdentifier())) {
225-
storageCredentialCacheMap.put(
226-
realmContext.getRealmIdentifier(), new StorageCredentialCache());
227-
}
228-
229-
return storageCredentialCacheMap.get(realmContext.getRealmIdentifier());
230-
}
231-
232218
@Override
233219
public synchronized EntityCache getOrCreateEntityCache(
234220
RealmContext realmContext, RealmConfig realmConfig) {

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.apache.polaris.core.persistence.dao.entity.PrincipalSecretsResult;
4242
import org.apache.polaris.core.persistence.transactional.TransactionalMetaStoreManagerImpl;
4343
import org.apache.polaris.core.persistence.transactional.TransactionalPersistence;
44-
import org.apache.polaris.core.storage.cache.StorageCredentialCache;
4544
import org.slf4j.Logger;
4645
import org.slf4j.LoggerFactory;
4746

@@ -54,7 +53,6 @@ public abstract class LocalPolarisMetaStoreManagerFactory<StoreType>
5453
implements MetaStoreManagerFactory {
5554

5655
final Map<String, PolarisMetaStoreManager> metaStoreManagerMap = new HashMap<>();
57-
final Map<String, StorageCredentialCache> storageCredentialCacheMap = new HashMap<>();
5856
final Map<String, EntityCache> entityCacheMap = new HashMap<>();
5957
final Map<String, StoreType> backingStoreMap = new HashMap<>();
6058
final Map<String, Supplier<TransactionalPersistence>> sessionSupplierMap = new HashMap<>();
@@ -138,7 +136,6 @@ public Map<String, BaseResult> purgeRealms(Iterable<String> realms) {
138136
BaseResult result = metaStoreManager.purge(callContext);
139137
results.put(realm, result);
140138

141-
storageCredentialCacheMap.remove(realm);
142139
backingStoreMap.remove(realm);
143140
sessionSupplierMap.remove(realm);
144141
metaStoreManagerMap.remove(realm);
@@ -172,17 +169,6 @@ public synchronized Supplier<TransactionalPersistence> getOrCreateSessionSupplie
172169
return sessionSupplierMap.get(realmContext.getRealmIdentifier());
173170
}
174171

175-
@Override
176-
public synchronized StorageCredentialCache getOrCreateStorageCredentialCache(
177-
RealmContext realmContext, RealmConfig realmConfig) {
178-
if (!storageCredentialCacheMap.containsKey(realmContext.getRealmIdentifier())) {
179-
storageCredentialCacheMap.put(
180-
realmContext.getRealmIdentifier(), new StorageCredentialCache());
181-
}
182-
183-
return storageCredentialCacheMap.get(realmContext.getRealmIdentifier());
184-
}
185-
186172
@Override
187173
public synchronized EntityCache getOrCreateEntityCache(
188174
RealmContext realmContext, RealmConfig realmConfig) {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.apache.polaris.core.persistence.cache.EntityCache;
2828
import org.apache.polaris.core.persistence.dao.entity.BaseResult;
2929
import org.apache.polaris.core.persistence.dao.entity.PrincipalSecretsResult;
30-
import org.apache.polaris.core.storage.cache.StorageCredentialCache;
3130

3231
/** Configuration interface for configuring the {@link PolarisMetaStoreManager}. */
3332
public interface MetaStoreManagerFactory {
@@ -36,9 +35,6 @@ public interface MetaStoreManagerFactory {
3635

3736
Supplier<? extends BasePersistence> getOrCreateSessionSupplier(RealmContext realmContext);
3837

39-
StorageCredentialCache getOrCreateStorageCredentialCache(
40-
RealmContext realmContext, RealmConfig realmConfig);
41-
4238
EntityCache getOrCreateEntityCache(RealmContext realmContext, RealmConfig realmConfig);
4339

4440
Map<String, PrincipalSecretsResult> bootstrapRealms(

polaris-core/src/main/java/org/apache/polaris/core/storage/cache/StorageCredentialCache.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ public class StorageCredentialCache {
4646

4747
private static final Logger LOGGER = LoggerFactory.getLogger(StorageCredentialCache.class);
4848

49-
private static final long CACHE_MAX_NUMBER_OF_ENTRIES = 10_000L;
50-
5149
private final LoadingCache<StorageCredentialCacheKey, StorageCredentialCacheEntry> cache;
5250

5351
/** Initialize the creds cache */
54-
public StorageCredentialCache() {
52+
public StorageCredentialCache(StorageCredentialCacheConfig cacheConfig) {
5553
cache =
5654
Caffeine.newBuilder()
57-
.maximumSize(CACHE_MAX_NUMBER_OF_ENTRIES)
55+
.maximumSize(cacheConfig.maxEntries())
5856
.expireAfter(
5957
Expiry.creating(
6058
(StorageCredentialCacheKey key, StorageCredentialCacheEntry entry) -> {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.polaris.core.storage.cache;
21+
22+
public interface StorageCredentialCacheConfig {
23+
24+
long maxEntries();
25+
}

polaris-core/src/test/java/org/apache/polaris/core/storage/cache/StorageCredentialCacheTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@
5454

5555
public class StorageCredentialCacheTest {
5656

57-
// polaris call context
5857
private final PolarisCallContext callCtx;
59-
60-
// the meta store manager
58+
private final StorageCredentialCacheConfig storageCredentialCacheConfig;
6159
private final PolarisMetaStoreManager metaStoreManager;
6260

6361
private StorageCredentialCache storageCredentialCache;
@@ -71,12 +69,13 @@ public StorageCredentialCacheTest() {
7169
TransactionalPersistence metaStore =
7270
new TreeMapTransactionalPersistenceImpl(store, Mockito.mock(), RANDOM_SECRETS);
7371
callCtx = new PolarisCallContext(() -> "testRealm", metaStore, diagServices);
72+
storageCredentialCacheConfig = () -> 10_000;
7473
metaStoreManager = Mockito.mock(PolarisMetaStoreManager.class);
7574
storageCredentialCache = newStorageCredentialCache();
7675
}
7776

7877
private StorageCredentialCache newStorageCredentialCache() {
79-
return new StorageCredentialCache();
78+
return new StorageCredentialCache(storageCredentialCacheConfig);
8079
}
8180

8281
@Test

runtime/service/src/main/java/org/apache/polaris/service/quarkus/config/QuarkusProducers.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.apache.polaris.core.secrets.UserSecretsManager;
5353
import org.apache.polaris.core.secrets.UserSecretsManagerFactory;
5454
import org.apache.polaris.core.storage.cache.StorageCredentialCache;
55+
import org.apache.polaris.core.storage.cache.StorageCredentialCacheConfig;
5556
import org.apache.polaris.service.auth.ActiveRolesProvider;
5657
import org.apache.polaris.service.auth.AuthenticationType;
5758
import org.apache.polaris.service.auth.Authenticator;
@@ -100,8 +101,9 @@ public Clock clock() {
100101

101102
@Produces
102103
@ApplicationScoped
103-
public StorageCredentialCache storageCredentialCache() {
104-
return new StorageCredentialCache();
104+
public StorageCredentialCache storageCredentialCache(
105+
StorageCredentialCacheConfig storageCredentialCacheConfig) {
106+
return new StorageCredentialCache(storageCredentialCacheConfig);
105107
}
106108

107109
@Produces
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.polaris.service.quarkus.storage;
21+
22+
import io.smallrye.config.ConfigMapping;
23+
import io.smallrye.config.WithDefault;
24+
import io.smallrye.config.WithName;
25+
import jakarta.validation.constraints.Min;
26+
import org.apache.polaris.core.storage.cache.StorageCredentialCacheConfig;
27+
28+
@ConfigMapping(prefix = "polaris.storage-credential-cache")
29+
public interface QuarkusStorageCredentialCacheConfig extends StorageCredentialCacheConfig {
30+
@WithName("max-entries")
31+
@WithDefault("10000")
32+
@Min(0)
33+
@Override
34+
long maxEntries();
35+
}

runtime/service/src/test/java/org/apache/polaris/service/quarkus/catalog/IcebergCatalogHandlerAuthzTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,7 @@ public void testSendNotificationSufficientPrivileges() {
17911791

17921792
PolarisCallContextCatalogFactory factory =
17931793
new PolarisCallContextCatalogFactory(
1794-
new RealmEntityManagerFactory(null, null) {
1794+
new RealmEntityManagerFactory(null, null, null) {
17951795
@Override
17961796
public PolarisEntityManager getOrCreateEntityManager(RealmContext realmContext) {
17971797
return entityManager;

runtime/service/src/test/java/org/apache/polaris/service/quarkus/catalog/IcebergCatalogTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
import org.apache.polaris.core.storage.aws.AwsCredentialsStorageIntegration;
127127
import org.apache.polaris.core.storage.aws.AwsStorageConfigurationInfo;
128128
import org.apache.polaris.core.storage.cache.StorageCredentialCache;
129+
import org.apache.polaris.core.storage.cache.StorageCredentialCacheConfig;
129130
import org.apache.polaris.service.admin.PolarisAdminService;
130131
import org.apache.polaris.service.catalog.PolarisPassthroughResolutionView;
131132
import org.apache.polaris.service.catalog.iceberg.CatalogHandlerUtils;
@@ -237,6 +238,7 @@ public Map<String, String> getConfigOverrides() {
237238

238239
@Inject MetaStoreManagerFactory metaStoreManagerFactory;
239240
@Inject PolarisConfigurationStore configurationStore;
241+
@Inject StorageCredentialCacheConfig storageCredentialCacheConfig;
240242
@Inject PolarisStorageIntegrationProvider storageIntegrationProvider;
241243
@Inject UserSecretsManagerFactory userSecretsManagerFactory;
242244
@Inject PolarisDiagnostics diagServices;
@@ -248,6 +250,7 @@ public Map<String, String> getConfigOverrides() {
248250
private UserSecretsManager userSecretsManager;
249251
private PolarisCallContext polarisContext;
250252
private PolarisAdminService adminService;
253+
private StorageCredentialCache storageCredentialCache;
251254
private PolarisEntityManager entityManager;
252255
private FileIOFactory fileIOFactory;
253256
private InMemoryFileIO fileIO;
@@ -286,10 +289,12 @@ public void before(TestInfo testInfo) {
286289
configurationStore,
287290
Clock.systemDefaultZone());
288291

292+
storageCredentialCache = new StorageCredentialCache(storageCredentialCacheConfig);
293+
289294
entityManager =
290295
new PolarisEntityManager(
291296
metaStoreManager,
292-
new StorageCredentialCache(),
297+
storageCredentialCache,
293298
createEntityCache(polarisContext.getRealmConfig(), metaStoreManager));
294299

295300
PrincipalEntity rootEntity =
@@ -352,7 +357,8 @@ public void before(TestInfo testInfo) {
352357
.asCatalog()));
353358

354359
RealmEntityManagerFactory realmEntityManagerFactory =
355-
new RealmEntityManagerFactory(metaStoreManagerFactory, configurationStore);
360+
new RealmEntityManagerFactory(
361+
metaStoreManagerFactory, configurationStore, storageCredentialCache);
356362
this.fileIOFactory =
357363
new DefaultFileIOFactory(realmEntityManagerFactory, metaStoreManagerFactory);
358364

@@ -986,7 +992,8 @@ public void testValidateNotificationFailToCreateFileIO() {
986992
FileIOFactory fileIOFactory =
987993
spy(
988994
new DefaultFileIOFactory(
989-
new RealmEntityManagerFactory(metaStoreManagerFactory, configurationStore),
995+
new RealmEntityManagerFactory(
996+
metaStoreManagerFactory, configurationStore, storageCredentialCache),
990997
metaStoreManagerFactory));
991998
IcebergCatalog catalog =
992999
new IcebergCatalog(
@@ -1877,7 +1884,8 @@ public void testDropTableWithPurge() {
18771884
FileIO fileIO =
18781885
new TaskFileIOSupplier(
18791886
new DefaultFileIOFactory(
1880-
new RealmEntityManagerFactory(metaStoreManagerFactory, configurationStore),
1887+
new RealmEntityManagerFactory(
1888+
metaStoreManagerFactory, configurationStore, storageCredentialCache),
18811889
metaStoreManagerFactory))
18821890
.apply(taskEntity, polarisContext);
18831891
Assertions.assertThat(fileIO).isNotNull().isInstanceOf(ExceptionMappingFileIO.class);
@@ -2021,7 +2029,8 @@ public void testFileIOWrapper() {
20212029

20222030
MeasuredFileIOFactory measured =
20232031
new MeasuredFileIOFactory(
2024-
new RealmEntityManagerFactory(metaStoreManagerFactory, configurationStore),
2032+
new RealmEntityManagerFactory(
2033+
metaStoreManagerFactory, configurationStore, storageCredentialCache),
20252034
metaStoreManagerFactory);
20262035
IcebergCatalog catalog =
20272036
new IcebergCatalog(

0 commit comments

Comments
 (0)