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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to