You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@polaris.apache.org by "snicholasbarton (via GitHub)" <gi...@apache.org> on 2026/05/27 16:23:46 UTC

[PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

snicholasbarton opened a new pull request, #4554:
URL: https://github.com/apache/polaris/pull/4554

   Fixes #4553
   
   Currently granting catalog roles with JDBC-backed persistence ends up throwing an HTTP 500 due to a PK constraint violation: all fields in grant records are in the PK and the service endpoint is a PUT (implying idempotency). This change catches the violation and silently succeeds. Later if additional non-PK fields are added to the grant_records table, this implementation might need to change to allow updating those attributes.
   
   I think the alternative here is to propagate conflicts and ultimately throw a 409 - but this contradicts the original Javadoc in BasePersistence, which I think means that the aim is that this does not throw on conflict. 
   
   ## Checklist
   - [x] ๐Ÿ›ก๏ธ Don't disclose security issues! (contact security@apache.org)
   - [x] ๐Ÿ”— Clearly explained why the changes are needed, or linked related issues: Fixes #
   - [x] ๐Ÿงช Added/updated tests with good coverage, or manually tested (and explained how)
   - [x] ๐Ÿ’ก Added comments for complex logic
   - [ ] ๐Ÿงพ Updated `CHANGELOG.md` (if needed)
   - [ ] ๐Ÿ“š Updated documentation in `site/content/in-dev/unreleased` (if needed)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain merged PR #4554:
URL: https://github.com/apache/polaris/pull/4554


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4554:
URL: https://github.com/apache/polaris/pull/4554#discussion_r3337258201


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -265,13 +258,17 @@ public void writeToGrantRecords(
           QueryGenerator.generateInsertQuery(
               ModelGrantRecord.ALL_COLUMNS, ModelGrantRecord.TABLE_NAME, values, realmId));
     } catch (SQLException e) {
+      if (datasourceOperations.isConstraintViolation(e)) {

Review Comment:
   Fair enough :+1: Thanks for narrowing the method name to "unique constraint" violations. Given that we currently have only the PK constrain this change looks pretty solid to me.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on PR #4554:
URL: https://github.com/apache/polaris/pull/4554#issuecomment-4605291505

   Thanks @snicholasbarton for the fix. Thanks @dimas-b for the review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #4554:
URL: https://github.com/apache/polaris/pull/4554#discussion_r3312440954


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -258,6 +258,10 @@ public void writeToGrantRecords(
           QueryGenerator.generateInsertQuery(
               ModelGrantRecord.ALL_COLUMNS, ModelGrantRecord.TABLE_NAME, values, realmId));
     } catch (SQLException e) {
+      if (datasourceOperations.isConstraintViolation(e)) {

Review Comment:
   Narrow swallow looks right, only SQLSTATE `23505` (unique_violation) is suppressed; FK/NOT NULL/CHECK still propagate via the existing throw. Can we double check cockroachDb and H2 to ensure the state is consistent across them?



##########
persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/JdbcGrantRecordsIdempotencyTest.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.persistence.relational.jdbc;
+
+import static org.apache.polaris.core.persistence.PrincipalSecretsGenerator.RANDOM_SECRETS;
+import static org.assertj.core.api.Assertions.assertThatCode;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.SQLException;
+import java.util.Optional;
+import org.apache.polaris.core.PolarisCallContext;
+import org.apache.polaris.core.PolarisDefaultDiagServiceImpl;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.entity.PolarisGrantRecord;
+import org.apache.polaris.core.storage.PolarisStorageIntegrationProvider;
+import org.h2.jdbcx.JdbcConnectionPool;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.mockito.Mockito;
+
+class JdbcGrantRecordsIdempotencyTest {
+
+  private static final long SECURABLE_CATALOG_ID = 1L;
+  private static final long SECURABLE_ID = 2L;
+  private static final long GRANTEE_CATALOG_ID = 3L;
+  private static final long GRANTEE_ID = 4L;
+  private static final int PRIVILEGE_CODE = 21;
+
+  @ParameterizedTest
+  @ValueSource(ints = {1, 2, 3, 4})
+  void writeToGrantRecordsIsIdempotent(int schemaVersion) throws SQLException {
+    JdbcConnectionPool dataSource =
+        JdbcConnectionPool.create(
+            "jdbc:h2:mem:grant_idempotency_v"
+                + schemaVersion
+                + "_"
+                + System.nanoTime()
+                + ";DB_CLOSE_DELAY=-1",
+            "sa",
+            "");
+    DatasourceOperations datasourceOperations =
+        new DatasourceOperations(dataSource, new TestJdbcConfiguration());
+    try (InputStream scriptStream = DatabaseType.H2.openInitScriptResource(schemaVersion)) {
+      datasourceOperations.executeScript(scriptStream);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+
+    RealmContext realmContext = () -> "REALM";
+    JdbcBasePersistenceImpl basePersistence =
+        new JdbcBasePersistenceImpl(
+            new PolarisDefaultDiagServiceImpl(),
+            datasourceOperations,
+            RANDOM_SECRETS,
+            Mockito.mock(PolarisStorageIntegrationProvider.class),
+            realmContext.getRealmIdentifier(),
+            schemaVersion);
+    PolarisCallContext callCtx = new PolarisCallContext(realmContext, basePersistence);
+
+    PolarisGrantRecord grant =
+        new PolarisGrantRecord(
+            SECURABLE_CATALOG_ID, SECURABLE_ID, GRANTEE_CATALOG_ID, GRANTEE_ID, PRIVILEGE_CODE);
+
+    assertThatCode(() -> basePersistence.writeToGrantRecords(callCtx, grant))

Review Comment:
   Optional: also add a negative case asserting non-23505 `SQLException`s still propagate (e.g. via a stub `DatasourceOperations`), so the swallow stays narrow if someone widens `isConstraintViolation` later.



##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/BasePersistence.java:
##########
@@ -133,7 +133,8 @@ void writeEntities(
 
   /**
    * Write the specified grantRecord to the grant_records table. If there is a conflict (existing
-   * record with the same PK), all attributes of the new record will replace the existing one.
+   * record with the same PK), this is a no-op, because currently all fields of the grantRecord are
+   * part of the PK. If additional non-PK attributes are added this might change.
    *
    * @param callCtx call context
    * @param grantRec entity record to write, potentially replacing an existing entity record with

Review Comment:
   The body now says "no-op" but `@param grantRec` still says "potentially replacing an existing entity record with the same key", can we reconcile to one wording? Also worth noting that a duplicate write still triggers grant-records-version bumps in the calling path.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "snicholasbarton (via GitHub)" <gi...@apache.org>.
snicholasbarton commented on code in PR #4554:
URL: https://github.com/apache/polaris/pull/4554#discussion_r3330611395


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -265,13 +258,17 @@ public void writeToGrantRecords(
           QueryGenerator.generateInsertQuery(
               ModelGrantRecord.ALL_COLUMNS, ModelGrantRecord.TABLE_NAME, values, realmId));
     } catch (SQLException e) {
+      if (datasourceOperations.isConstraintViolation(e)) {

Review Comment:
   I'm not opposed in theory; if non-PK fields are added later then we probably do want this. Chose not to here for consistency (didn't see any existing direct upsert query patterns), portability (I think the query might be different per PG/Cockroach/H2), and YAGNI (all the fields are part of the PK, so currently we don't actually have anything to update). Happy to revisit if you think it's worth it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "snicholasbarton (via GitHub)" <gi...@apache.org>.
snicholasbarton commented on code in PR #4554:
URL: https://github.com/apache/polaris/pull/4554#discussion_r3330610564


##########
persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/JdbcGrantRecordsIdempotencyTest.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.persistence.relational.jdbc;
+
+import static org.apache.polaris.core.persistence.PrincipalSecretsGenerator.RANDOM_SECRETS;
+import static org.assertj.core.api.Assertions.assertThatCode;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.SQLException;
+import java.util.Optional;
+import org.apache.polaris.core.PolarisCallContext;
+import org.apache.polaris.core.PolarisDefaultDiagServiceImpl;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.entity.PolarisGrantRecord;
+import org.apache.polaris.core.storage.PolarisStorageIntegrationProvider;
+import org.h2.jdbcx.JdbcConnectionPool;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.mockito.Mockito;
+
+class JdbcGrantRecordsIdempotencyTest {
+
+  private static final long SECURABLE_CATALOG_ID = 1L;
+  private static final long SECURABLE_ID = 2L;
+  private static final long GRANTEE_CATALOG_ID = 3L;
+  private static final long GRANTEE_ID = 4L;
+  private static final int PRIVILEGE_CODE = 21;
+
+  @ParameterizedTest
+  @ValueSource(ints = {1, 2, 3, 4})
+  void writeToGrantRecordsIsIdempotent(int schemaVersion) throws SQLException {
+    JdbcConnectionPool dataSource =
+        JdbcConnectionPool.create(
+            "jdbc:h2:mem:grant_idempotency_v"
+                + schemaVersion
+                + "_"
+                + System.nanoTime()
+                + ";DB_CLOSE_DELAY=-1",
+            "sa",
+            "");
+    DatasourceOperations datasourceOperations =
+        new DatasourceOperations(dataSource, new TestJdbcConfiguration());
+    try (InputStream scriptStream = DatabaseType.H2.openInitScriptResource(schemaVersion)) {
+      datasourceOperations.executeScript(scriptStream);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+
+    RealmContext realmContext = () -> "REALM";
+    JdbcBasePersistenceImpl basePersistence =
+        new JdbcBasePersistenceImpl(
+            new PolarisDefaultDiagServiceImpl(),
+            datasourceOperations,
+            RANDOM_SECRETS,
+            Mockito.mock(PolarisStorageIntegrationProvider.class),
+            realmContext.getRealmIdentifier(),
+            schemaVersion);
+    PolarisCallContext callCtx = new PolarisCallContext(realmContext, basePersistence);
+
+    PolarisGrantRecord grant =
+        new PolarisGrantRecord(
+            SECURABLE_CATALOG_ID, SECURABLE_ID, GRANTEE_CATALOG_ID, GRANTEE_ID, PRIVILEGE_CODE);
+
+    assertThatCode(() -> basePersistence.writeToGrantRecords(callCtx, grant))

Review Comment:
   Added



##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/BasePersistence.java:
##########
@@ -133,7 +133,8 @@ void writeEntities(
 
   /**
    * Write the specified grantRecord to the grant_records table. If there is a conflict (existing
-   * record with the same PK), all attributes of the new record will replace the existing one.
+   * record with the same PK), this is a no-op, because currently all fields of the grantRecord are
+   * part of the PK. If additional non-PK attributes are added this might change.
    *
    * @param callCtx call context
    * @param grantRec entity record to write, potentially replacing an existing entity record with

Review Comment:
   Updated the param wording.
   
   For the grant-records-version - that's used for caching, right? So after this change - where dupe writes don't throw - we pay an extra write (to the grant records version table) + later an extra read (after the invalidated cache lookup). I would anticipate that in normal use there probably isn't a huge number of these dupes, so maybe that's an acceptable trade. 
   
   If we do care about it we could potentially return a flag indicating whether a write was /actually/ made and only increment the version if so? Would potentially want to do that as a separate change.



##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -258,6 +258,10 @@ public void writeToGrantRecords(
           QueryGenerator.generateInsertQuery(
               ModelGrantRecord.ALL_COLUMNS, ModelGrantRecord.TABLE_NAME, values, realmId));
     } catch (SQLException e) {
+      if (datasourceOperations.isConstraintViolation(e)) {

Review Comment:
   Yes - both Cockroach ([source](https://github.com/cockroachdb/cockroach/blob/2fffba777c3a77451a6480b09316d0ae985f0b48/pkg/sql/pgwire/pgcode/codes.go) / [docs](https://www.cockroachlabs.com/docs/stable/transactions#error-handling)) and H2 ([source](https://github.com/h2database/h2database/blob/c39970fff5fbda13cf3d16df88426c36d98e5c39/h2/src/main/org/h2/api/ErrorCode.java#L265-L274)) also use 23505 specifically for uniqueness constraint. Added a comment indicating as such.
   
   Renamed the constant and method to make it clear it's checking for uniqueness constraint violations.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4554:
URL: https://github.com/apache/polaris/pull/4554#discussion_r3315100382


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -265,13 +258,17 @@ public void writeToGrantRecords(
           QueryGenerator.generateInsertQuery(
               ModelGrantRecord.ALL_COLUMNS, ModelGrantRecord.TABLE_NAME, values, realmId));
     } catch (SQLException e) {
+      if (datasourceOperations.isConstraintViolation(e)) {

Review Comment:
   Would it be more appropriate to use `MERGE INTO` SQL instead?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4554:
URL: https://github.com/apache/polaris/pull/4554#discussion_r3337258201


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -265,13 +258,17 @@ public void writeToGrantRecords(
           QueryGenerator.generateInsertQuery(
               ModelGrantRecord.ALL_COLUMNS, ModelGrantRecord.TABLE_NAME, values, realmId));
     } catch (SQLException e) {
+      if (datasourceOperations.isConstraintViolation(e)) {

Review Comment:
   Fair enough :+1: Thanks for narrowing the method name to "unique constraint" violations. Given that we currently have only the PK constraint on this table this change looks pretty solid to me.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Make granting catalog roles idempotent for JDBC persistence [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #4554:
URL: https://github.com/apache/polaris/pull/4554#discussion_r3337247005


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/idempotency/RelationalJdbcIdempotencyStore.java:
##########
@@ -83,7 +83,7 @@ public ReserveResult reserve(
       datasourceOperations.executeUpdate(insert);
       return new ReserveResult(ReserveResultType.OWNED, Optional.empty());
     } catch (SQLException e) {
-      if (datasourceOperations.isConstraintViolation(e)) {
+      if (datasourceOperations.isUniquenessConstraintViolation(e)) {

Review Comment:
   nice ๐Ÿ‘ 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org