Skip to content

Commit 5ea215a

Browse files
authored
Add fallback in case the VERSION table is not present (#2653)
* initial commit * wire up * pastefix * change to postgres specific code
1 parent d8d0f81 commit 5ea215a

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class DatasourceOperations {
5353
private static final Logger LOGGER = LoggerFactory.getLogger(DatasourceOperations.class);
5454

5555
private static final String CONSTRAINT_VIOLATION_SQL_CODE = "23505";
56+
private static final String RELATION_DOES_NOT_EXIST = "42P01";
5657

5758
// POSTGRES RETRYABLE EXCEPTIONS
5859
private static final String SERIALIZATION_FAILURE_SQL_CODE = "40001";
@@ -394,6 +395,10 @@ public boolean isConstraintViolation(SQLException e) {
394395
return CONSTRAINT_VIOLATION_SQL_CODE.equals(e.getSQLState());
395396
}
396397

398+
public boolean isRelationDoesNotExist(SQLException e) {
399+
return RELATION_DOES_NOT_EXIST.equals(e.getSQLState());
400+
}
401+
397402
private Connection borrowConnection() throws SQLException {
398403
return datasource.getConnection();
399404
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ public boolean hasChildren(
725725
}
726726
}
727727

728-
static int loadSchemaVersion(DatasourceOperations datasourceOperations) {
728+
static int loadSchemaVersion(
729+
DatasourceOperations datasourceOperations, boolean fallbackOnDoesNotExist) {
729730
PreparedQuery query = QueryGenerator.generateVersionQuery();
730731
try {
731732
List<SchemaVersion> schemaVersion =
@@ -736,6 +737,9 @@ static int loadSchemaVersion(DatasourceOperations datasourceOperations) {
736737
return schemaVersion.getFirst().getValue();
737738
} catch (SQLException e) {
738739
LOGGER.error("Failed to load schema version due to {}", e.getMessage(), e);
740+
if (fallbackOnDoesNotExist && datasourceOperations.isRelationDoesNotExist(e)) {
741+
return SchemaVersion.MINIMUM.getValue();
742+
}
739743
throw new IllegalStateException("Failed to retrieve schema version", e);
740744
}
741745
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import javax.sql.DataSource;
3333
import org.apache.polaris.core.PolarisCallContext;
3434
import org.apache.polaris.core.PolarisDiagnostics;
35+
import org.apache.polaris.core.config.BehaviorChangeConfiguration;
36+
import org.apache.polaris.core.config.PolarisConfigurationStore;
3537
import org.apache.polaris.core.config.RealmConfig;
3638
import org.apache.polaris.core.context.RealmContext;
3739
import org.apache.polaris.core.entity.PolarisEntityConstants;
@@ -74,6 +76,7 @@ public class JdbcMetaStoreManagerFactory implements MetaStoreManagerFactory {
7476
@Inject PolarisStorageIntegrationProvider storageIntegrationProvider;
7577
@Inject Instance<DataSource> dataSource;
7678
@Inject RelationalJdbcConfiguration relationalJdbcConfiguration;
79+
@Inject PolarisConfigurationStore configurationStore;
7780

7881
protected JdbcMetaStoreManagerFactory() {}
7982

@@ -98,7 +101,11 @@ private void initializeForRealm(
98101
// RealmContext (request-scoped bean) can still create a JdbcBasePersistenceImpl
99102
String realmId = realmContext.getRealmIdentifier();
100103
// determine schemaVersion once per realm
101-
final int schemaVersion = JdbcBasePersistenceImpl.loadSchemaVersion(datasourceOperations);
104+
final int schemaVersion =
105+
JdbcBasePersistenceImpl.loadSchemaVersion(
106+
datasourceOperations,
107+
configurationStore.getConfiguration(
108+
realmContext, BehaviorChangeConfiguration.SCHEMA_VERSION_FALL_BACK_ON_DNE));
102109
sessionSupplierMap.put(
103110
realmId,
104111
() ->

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.apache.polaris.persistence.relational.jdbc.DatabaseType;
2626

2727
public class SchemaVersion implements Converter<SchemaVersion> {
28+
public static final SchemaVersion MINIMUM = new SchemaVersion(0);
29+
2830
private final Integer value;
2931

3032
public SchemaVersion() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,14 @@ protected BehaviorChangeConfiguration(
8484
+ " credential vending.")
8585
.defaultValue(false)
8686
.buildBehaviorChangeConfiguration();
87+
88+
public static final BehaviorChangeConfiguration<Boolean> SCHEMA_VERSION_FALL_BACK_ON_DNE =
89+
PolarisConfiguration.<Boolean>builder()
90+
.key("SCHEMA_VERSION_FALL_BACK_ON_DNE")
91+
.description(
92+
"If set to true, exceptions encountered while loading the VERSION table which appear to be"
93+
+ " caused by the VERSION table not existing will be interpreted as meaning that the"
94+
+ " schema version is currently 0.")
95+
.defaultValue(true)
96+
.buildBehaviorChangeConfiguration();
8797
}

0 commit comments

Comments
 (0)