snazy commented on code in PR #1517:
URL: https://github.com/apache/polaris/pull/1517#discussion_r2079103744


##########
quarkus/common/build.gradle.kts:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+
+plugins {
+    alias(libs.plugins.jandex)
+}
+
+configurations.all {
+    exclude(group = "org.antlr", module = "antlr4-runtime")
+    exclude(group = "org.scala-lang", module = "scala-library")
+    exclude(group = "org.scala-lang", module = "scala-reflect")
+}
+
+java {
+    sourceCompatibility = JavaVersion.VERSION_21
+    targetCompatibility = JavaVersion.VERSION_21

Review Comment:
   There is 
   ```
     id("polaris-quarkus")
   ```
   for this?



##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/DatasourceOperations.java:
##########
@@ -173,23 +190,82 @@ public int executeUpdate(String query) throws 
SQLException {
    * @throws SQLException : Exception caught during transaction execution.
    */
   public void runWithinTransaction(TransactionCallback callback) throws 
SQLException {
-    try (Connection connection = borrowConnection()) {
-      boolean autoCommit = connection.getAutoCommit();
-      connection.setAutoCommit(false);
-      boolean success = false;
+    withRetries(
+        () -> {
+          try (Connection connection = borrowConnection()) {
+            boolean autoCommit = connection.getAutoCommit();
+            boolean success = false;
+            connection.setAutoCommit(false);
+            try {
+              try (Statement statement = connection.createStatement()) {
+                success = callback.execute(statement);
+              }
+            } finally {
+              if (success) {
+                connection.commit();
+              } else {
+                connection.rollback();
+              }
+              connection.setAutoCommit(autoCommit);
+            }
+          }
+          return null;
+        });
+  }
+
+  private boolean isRetryable(SQLException e) {
+    String sqlState = e.getSQLState();
+
+    if (sqlState != null) {
+      return sqlState.equals(DEADLOCK_SQL_CODE)
+          || // Deadlock detected
+          sqlState.equals(SERIALIZATION_FAILURE_SQL_CODE); // Serialization 
failure
+    }
+
+    // Additionally, one might check for specific error messages or other 
conditions
+    return e.getMessage().contains("connection refused")
+        || e.getMessage().contains("connection reset");
+  }
+
+  public <T> T withRetries(Operation<T> operation) throws SQLException {
+    int attempts = 0;
+    // maximum number of retries.
+    int maxAttempts = relationalJdbcConfiguration.maxRetries().orElse(1);

Review Comment:
   The last iteration looks much better



##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/DatasourceOperations.java:
##########
@@ -173,23 +189,105 @@ public int executeUpdate(String query) throws 
SQLException {
    * @throws SQLException : Exception caught during transaction execution.
    */
   public void runWithinTransaction(TransactionCallback callback) throws 
SQLException {
-    try (Connection connection = borrowConnection()) {
-      boolean autoCommit = connection.getAutoCommit();
-      connection.setAutoCommit(false);
-      boolean success = false;
+    withRetries(
+        () -> {
+          try (Connection connection = borrowConnection()) {
+            boolean autoCommit = connection.getAutoCommit();
+            boolean success = false;
+            connection.setAutoCommit(false);
+            try {
+              try {
+                try (Statement statement = connection.createStatement()) {
+                  success = callback.execute(statement);
+                }
+              } finally {
+                if (success) {
+                  connection.commit();
+                } else {
+                  connection.rollback();
+                }
+              }
+            } finally {
+              connection.setAutoCommit(autoCommit);
+            }
+          }
+          return null;
+        });
+  }
+
+  private boolean isRetryable(SQLException e) {
+    String sqlState = e.getSQLState();
+
+    if (sqlState != null) {
+      return sqlState.equals(SERIALIZATION_FAILURE_SQL_CODE); // Serialization 
failure
+    }
+
+    // Additionally, one might check for specific error messages or other 
conditions
+    return e.getMessage().toLowerCase(Locale.ROOT).contains("connection 
refused")
+        || e.getMessage().toLowerCase(Locale.ROOT).contains("connection 
reset");
+  }
+
+  // TODO: consider refactoring to use a retry library, inorder to have fair 
retries
+  // and more knobs for tuning retry pattern.
+  @VisibleForTesting
+  <T> T withRetries(Operation<T> operation) throws SQLException {
+    int attempts = 0;
+    // maximum number of retries.
+    int maxAttempts = relationalJdbcConfiguration.maxRetries().orElse(1);
+    // How long we should try, since the first attempt.
+    long maxDuration = 
relationalJdbcConfiguration.maxDurationInMs().orElse(5000L);
+    // How long to wait before first failure.
+    long delay = relationalJdbcConfiguration.initialDelayInMs().orElse(100L);
+
+    // maximum time we will retry till.
+    long maxRetryTime = clock.millis() + maxDuration;
+
+    while (attempts < maxAttempts) {
       try {
-        try (Statement statement = connection.createStatement()) {
-          success = callback.execute(statement);
-        }
-      } finally {
-        if (success) {
-          connection.commit();
+        return operation.execute();
+      } catch (SQLException | RuntimeException e) {
+        SQLException sqlException;
+        if (e instanceof RuntimeException) {
+          // Handle Exceptions from ResultSet Iterator consumer, as it throws 
a RTE, ignore RTE from
+          // the transactions.
+          if (e.getCause() instanceof SQLException
+              && !(e instanceof EntityAlreadyExistsException)) {
+            sqlException = (SQLException) e.getCause();
+          } else {
+            throw e;
+          }
         } else {
-          connection.rollback();
+          sqlException = (SQLException) e;
         }
-        connection.setAutoCommit(autoCommit);
+
+        attempts++;
+        long timeLeft = Math.max((maxRetryTime - clock.millis()), 0L);

Review Comment:
   There's a rare but still existent problem here:
   The code is using the _wall clock_, which can go backwards and forwards at 
any time. Not so much of an issue in _proper_ server setups (most NTP default 
setups), but even proper NTP setups can jump.
   What you really want here is a strictly monotonically increasing clock (and 
no, System.nanoTime() alone is not a good candidate).



-- 
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-unsubscr...@polaris.apache.org

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

Reply via email to