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

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

Reply via email to