dimas-b commented on code in PR #1517:
URL: https://github.com/apache/polaris/pull/1517#discussion_r2078572988


##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/DatasourceOperations.java:
##########
@@ -173,23 +188,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 (Statement statement = connection.createStatement()) {
+                success = callback.execute(statement);
+              }
+            } finally {
+              if (success) {
+                connection.commit();
+              } else {
+                connection.rollback();
+              }
+              connection.setAutoCommit(autoCommit);

Review Comment:
   This should probably move above the commit/rollback block because those 
methods may throw... or better still use a separate try/finally for the 
autoCommit setting.



##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/DatasourceOperations.java:
##########
@@ -173,23 +188,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 (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(SERIALIZATION_FAILURE_SQL_CODE); // Serialization 
failure
+    }
+
+    // Additionally, one might check for specific error messages or other 
conditions
+    return e.getMessage().contains("connection refused")

Review Comment:
   Did you mean `.toLowerCase(Locale.ROOT)`?



##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/DatasourceOperations.java:
##########
@@ -173,23 +188,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 (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(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");
+  }
+
+  // TODO: consider refactoring to use a retry library, inorder to have fair 
retries
+  // and more knobs for tuning retry pattern.
+  @VisibleForTesting
+  public <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);
+        if (attempts >= maxAttempts || !isRetryable(sqlException) || timeLeft 
== 0) {
+          String exceptionMessage =
+              String.format(
+                  "Failed due to %s, after , %s attempts and %s milliseconds",
+                  sqlException.getMessage(), attempts, maxDuration);
+          throw new SQLException(
+              exceptionMessage,
+              sqlException.getSQLState(),
+              sqlException.getErrorCode(),
+              sqlException);

Review Comment:
   Please use `e` (the most complete exception) as the "root cause".



##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/DatasourceOperations.java:
##########
@@ -173,23 +188,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 (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(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");

Review Comment:
   Please add tests for these cases.



-- 
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