This is an automated email from the ASF dual-hosted git repository.

gsaihemanth pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new deb68592292 HIVE-26976: Failure in direct SQL processing do not undo 
the changes made to database before fallback to JDO (#3976) (Venu Reddy, 
reviewed by Sai Hemanth)
deb68592292 is described below

commit deb68592292258c999a89334e40fad50a8581d06
Author: Venu Reddy <[email protected]>
AuthorDate: Thu Jan 26 01:19:12 2023 +0530

    HIVE-26976: Failure in direct SQL processing do not undo the changes made 
to database before fallback to JDO (#3976) (Venu Reddy, reviewed by Sai Hemanth)
---
 .../apache/hadoop/hive/metastore/ObjectStore.java  | 24 +++++++++++-
 .../hadoop/hive/metastore/TestObjectStore.java     | 43 ++++++++++++++++++++++
 2 files changed, 65 insertions(+), 2 deletions(-)

diff --git 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
index d7e938732b7..f9194e86e48 100644
--- 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
+++ 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java
@@ -255,6 +255,9 @@ import org.apache.hadoop.hive.metastore.utils.JavaUtils;
 import org.apache.hadoop.hive.metastore.utils.MetaStoreServerUtils;
 import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
 import org.apache.thrift.TException;
+import org.datanucleus.ExecutionContext;
+import org.datanucleus.api.jdo.JDOPersistenceManager;
+import org.datanucleus.api.jdo.JDOTransaction;
 import org.datanucleus.store.rdbms.exceptions.MissingTableException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -668,6 +671,20 @@ public class ObjectStore implements RawStore, Configurable 
{
     }
   }
 
+  private void setTransactionSavePoint(String savePoint) {
+    if (savePoint != null) {
+      ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
+      ec.getStoreManager().getConnectionManager().getConnection(ec);
+      ((JDOTransaction) currentTransaction).setSavepoint(savePoint);
+    }
+  }
+
+  private void rollbackTransactionToSavePoint(String savePoint) {
+    if (savePoint != null) {
+      ((JDOTransaction) currentTransaction).rollbackToSavepoint(savePoint);
+    }
+  }
+
   @Override
   public void createCatalog(Catalog cat) throws MetaException {
     LOG.debug("Creating catalog {}", cat);
@@ -4366,13 +4383,15 @@ public class ObjectStore implements RawStore, 
Configurable {
     public T run(boolean initTable) throws MetaException, 
NoSuchObjectException {
       try {
         start(initTable);
+        String savePoint = isInTxn && allowJdo ? "rollback_" + 
System.nanoTime() : null;
         if (doUseDirectSql) {
           try {
             directSql.prepareTxn();
+            setTransactionSavePoint(savePoint);
             this.results = getSqlResult(this);
             LOG.debug("Using direct SQL optimization.");
           } catch (Exception ex) {
-            handleDirectSqlError(ex);
+            handleDirectSqlError(ex, savePoint);
           }
         }
         // Note that this will be invoked in 2 cases:
@@ -4402,7 +4421,7 @@ public class ObjectStore implements RawStore, 
Configurable {
       doUseDirectSql = doUseDirectSql && canUseDirectSql(this);
     }
 
-    private void handleDirectSqlError(Exception ex) throws MetaException, 
NoSuchObjectException {
+    private void handleDirectSqlError(Exception ex, String savePoint) throws 
MetaException, NoSuchObjectException {
       String message = null;
       try {
         message = generateShorterMessage(ex);
@@ -4438,6 +4457,7 @@ public class ObjectStore implements RawStore, 
Configurable {
           table = ensureGetTable(catName, dbName, tblName);
         }
       } else {
+        rollbackTransactionToSavePoint(savePoint);
         start = doTrace ? System.nanoTime() : 0;
       }
 
diff --git 
a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java
 
b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java
index d3f29433e8f..0bcbdeb9a10 100644
--- 
a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java
+++ 
b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestObjectStore.java
@@ -1481,6 +1481,49 @@ public class TestObjectStore {
     Assert.assertEquals(1, result.size());
   }
 
+  @Test
+  public void testSavePoint() throws Exception {
+    List<String> partNames = Arrays.asList("test_part_col=a0", 
"test_part_col=a1", "test_part_col=a2");
+    createPartitionedTable(true, false);
+    Assert.assertEquals(3, objectStore.getPartitionCount());
+
+    objectStore.openTransaction();
+    objectStore.new GetHelper<Object>(DEFAULT_CATALOG_NAME, DB1, TABLE1, true, 
true) {
+      @Override
+      protected String describeResult() {
+        return "test savepoint";
+      }
+
+      @Override
+      protected Object getSqlResult(ObjectStore.GetHelper<Object> ctx) throws 
MetaException {
+        // drop the partitions with SQL alone
+        try (AutoCloseable c = deadline()) {
+          objectStore.dropPartitionsInternal(ctx.catName, ctx.dbName, 
ctx.tblName, partNames, true,
+              false);
+          Assert.assertEquals(0, objectStore.getPartitionCount());
+        } catch (Exception e) {
+          throw new MetaException(e.getMessage());
+        }
+        throw new MetaException("Throwing exception in direct SQL to test 
Savepoint");
+      }
+
+      @Override
+      protected Object getJdoResult(ObjectStore.GetHelper<Object> ctx) throws 
MetaException {
+        // drop the partitions with JDO alone
+        try (AutoCloseable c = deadline()) {
+          Assert.assertEquals(3, objectStore.getPartitionCount());
+          objectStore.dropPartitionsInternal(ctx.catName, ctx.dbName, 
ctx.tblName, partNames, false,
+              true);
+        } catch (Exception e) {
+          throw new MetaException(e.getMessage());
+        }
+        return null;
+      }
+    }.run(false);
+    objectStore.commitTransaction();
+    Assert.assertEquals(0, objectStore.getPartitionCount());
+  }
+
   /**
    * Helper method to check whether the Java system properties were set 
correctly in {@link ObjectStore#configureSSL(Configuration)}
    * @param useSSL whether or not SSL is enabled

Reply via email to