Skip to content

Commit af69d9f

Browse files
authored
JdbcMetaStoreManagerFactory determines schemaVersion once per realm (#2217)
otherwise every call to `MetaStoreManagerFactory.getOrCreateSession` was running the query.
1 parent 20febda commit af69d9f

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class JdbcBasePersistenceImpl implements BasePersistence, IntegrationPers
8080
private final PrincipalSecretsGenerator secretsGenerator;
8181
private final PolarisStorageIntegrationProvider storageIntegrationProvider;
8282
private final String realmId;
83-
private final int version;
83+
private final int schemaVersion;
8484

8585
// The max number of components a location can have before the optimized sibling check is not used
8686
private static final int MAX_LOCATION_COMPONENTS = 40;
@@ -89,12 +89,13 @@ public JdbcBasePersistenceImpl(
8989
DatasourceOperations databaseOperations,
9090
PrincipalSecretsGenerator secretsGenerator,
9191
PolarisStorageIntegrationProvider storageIntegrationProvider,
92-
String realmId) {
92+
String realmId,
93+
int schemaVersion) {
9394
this.datasourceOperations = databaseOperations;
9495
this.secretsGenerator = secretsGenerator;
9596
this.storageIntegrationProvider = storageIntegrationProvider;
9697
this.realmId = realmId;
97-
this.version = loadVersion();
98+
this.schemaVersion = schemaVersion;
9899
}
99100

100101
@Override
@@ -651,7 +652,7 @@ public boolean hasChildren(
651652
}
652653
}
653654

654-
private int loadVersion() {
655+
static int loadSchemaVersion(DatasourceOperations datasourceOperations) {
655656
PreparedQuery query = QueryGenerator.generateVersionQuery();
656657
try {
657658
List<SchemaVersion> schemaVersion =
@@ -671,7 +672,7 @@ private int loadVersion() {
671672
public <T extends PolarisEntity & LocationBasedEntity>
672673
Optional<Optional<String>> hasOverlappingSiblings(
673674
@Nonnull PolarisCallContext callContext, T entity) {
674-
if (this.version < 2) {
675+
if (this.schemaVersion < 2) {
675676
return Optional.empty();
676677
}
677678
if (entity.getBaseLocation().chars().filter(ch -> ch == '/').count()

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,17 @@ private void initializeForRealm(
9696
// Materialize realmId so that background tasks that don't have an active
9797
// RealmContext (request-scoped bean) can still create a JdbcBasePersistenceImpl
9898
String realmId = realmContext.getRealmIdentifier();
99+
// determine schemaVersion once per realm
100+
final int schemaVersion = JdbcBasePersistenceImpl.loadSchemaVersion(datasourceOperations);
99101
sessionSupplierMap.put(
100102
realmId,
101103
() ->
102104
new JdbcBasePersistenceImpl(
103105
datasourceOperations,
104106
secretsGenerator(realmId, rootCredentialsSet),
105107
storageIntegrationProvider,
106-
realmId));
108+
realmId,
109+
schemaVersion));
107110

108111
PolarisMetaStoreManager metaStoreManager = createNewMetaStoreManager();
109112
metaStoreManagerMap.put(realmId, metaStoreManager);

persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/AtomicMetastoreManagerWithJdbcBasePersistenceImplTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static DataSource createH2DataSource() {
4545

4646
@Override
4747
protected PolarisTestMetaStoreManager createPolarisTestMetaStoreManager() {
48+
int schemaVersion = 2;
4849
PolarisDiagnostics diagServices = new PolarisDefaultDiagServiceImpl();
4950
DatasourceOperations datasourceOperations;
5051
try {
@@ -53,7 +54,7 @@ protected PolarisTestMetaStoreManager createPolarisTestMetaStoreManager() {
5354
ClassLoader classLoader = DatasourceOperations.class.getClassLoader();
5455
InputStream scriptStream =
5556
classLoader.getResourceAsStream(
56-
String.format("%s/schema-v2.sql", DatabaseType.H2.getDisplayName()));
57+
String.format("%s/schema-v%s.sql", DatabaseType.H2.getDisplayName(), schemaVersion));
5758
datasourceOperations.executeScript(scriptStream);
5859
} catch (SQLException e) {
5960
throw new RuntimeException(
@@ -68,7 +69,8 @@ protected PolarisTestMetaStoreManager createPolarisTestMetaStoreManager() {
6869
datasourceOperations,
6970
RANDOM_SECRETS,
7071
Mockito.mock(),
71-
realmContext.getRealmIdentifier());
72+
realmContext.getRealmIdentifier(),
73+
schemaVersion);
7274
return new PolarisTestMetaStoreManager(
7375
new AtomicOperationMetaStoreManager(),
7476
new PolarisCallContext(

0 commit comments

Comments
 (0)