You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2021/07/02 05:15:40 UTC

[GitHub] [hive] pkumarsinha commented on a change in pull request #2396: HIVE-25246: Fix the clean up of open repl created transactions

pkumarsinha commented on a change in pull request #2396:
URL: https://github.com/apache/hive/pull/2396#discussion_r662736099



##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/txn/TxnHandler.java
##########
@@ -1122,12 +1154,76 @@ public void abortTxns(AbortTxnsRequest rqst) throws MetaException {
     }
   }
 
+  private long getDatabaseId(Connection dbConn, String database, String catalog) throws SQLException {
+    ResultSet rs = null;
+    PreparedStatement pst = null;
+    try {
+      String query = "select \"DB_ID\" from \"DBS\" where \"NAME\" = ?  and \"CTLG_NAME\" = ?";
+      pst = sqlGenerator.prepareStmtWithParameters(dbConn, query, Arrays.asList(database, catalog));
+      LOG.debug("Going to execute query <" + query.replaceAll("\\?", "{}") + ">",
+              quoteString(database), quoteString(catalog));
+      rs = pst.executeQuery();
+      if (!rs.next()) {
+        LOG.error("Database: " + database + " does not exist in catalog " + catalog);
+        return -1;
+      }
+      return rs.getLong(1);
+    } finally {
+      close(rs);
+      closeStmt(pst);
+    }
+  }
+
+  private void updateDatabaseProp(Connection dbConn, Statement stmt,
+                                  long dbId, String prop, String propValue) throws SQLException {
+    ResultSet rs = null;
+    PreparedStatement pst = null;
+    String query;
+    try {
+      rs = stmt.executeQuery("SELECT \"PARAM_VALUE\" FROM \"DATABASE_PARAMS\" WHERE \"PARAM_KEY\" = " +
+              "'" + prop + "' AND \"DB_ID\" = " + dbId);
+      if (!rs.next()) {
+        query = "INSERT INTO \"DATABASE_PARAMS\" VALUES ( " + dbId + " , '" + prop + "' , ? )";
+      } else {
+        query = "UPDATE \"DATABASE_PARAMS\" SET \"PARAM_VALUE\" = ? WHERE \"DB_ID\" = " + dbId +
+                " AND \"PARAM_KEY\" = '" + prop + "'";
+      }
+      close(rs);
+      pst = sqlGenerator.prepareStmtWithParameters(dbConn, query, Arrays.asList(propValue));
+      LOG.debug("Updating " + prop + " for db <" + query.replaceAll("\\?", "{}") + ">", propValue);
+      if (pst.executeUpdate() != 1) {
+        //only one row insert or update should happen
+        throw new RuntimeException("DATABASE_PARAMS is corrupted for databaseID: " + dbId);
+      }
+    } finally {
+      close(rs);
+      closeStmt(pst);
+    }
+  }
+
+  private void markDbAsReplIncompatible(Connection dbConn, String database) throws SQLException {
+    Statement stmt = dbConn.createStatement();
+    String catalog = MetaStoreUtils.getDefaultCatalog(conf);
+    String s = sqlGenerator.getDbProduct().getPrepareTxnStmt();
+    if (s != null) {
+      stmt.execute(s);
+    }
+    long dbId = getDatabaseId(dbConn, database, catalog);
+    if (dbId == -1) {
+      return;
+    }
+    updateDatabaseProp(dbConn, stmt, dbId, ReplConst.REPL_INCOMPATIBLE, ReplConst.TRUE);
+    closeStmt(stmt);

Review comment:
       There is still prone to statement object leak.

##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/txn/TxnHandler.java
##########
@@ -1122,12 +1154,76 @@ public void abortTxns(AbortTxnsRequest rqst) throws MetaException {
     }
   }
 
+  private long getDatabaseId(Connection dbConn, String database, String catalog) throws SQLException {
+    ResultSet rs = null;
+    PreparedStatement pst = null;
+    try {
+      String query = "select \"DB_ID\" from \"DBS\" where \"NAME\" = ?  and \"CTLG_NAME\" = ?";
+      pst = sqlGenerator.prepareStmtWithParameters(dbConn, query, Arrays.asList(database, catalog));
+      LOG.debug("Going to execute query <" + query.replaceAll("\\?", "{}") + ">",
+              quoteString(database), quoteString(catalog));
+      rs = pst.executeQuery();
+      if (!rs.next()) {
+        LOG.error("Database: " + database + " does not exist in catalog " + catalog);
+        return -1;
+      }
+      return rs.getLong(1);
+    } finally {
+      close(rs);
+      closeStmt(pst);
+    }
+  }
+
+  private void updateDatabaseProp(Connection dbConn, Statement stmt,
+                                  long dbId, String prop, String propValue) throws SQLException {
+    ResultSet rs = null;
+    PreparedStatement pst = null;
+    String query;
+    try {
+      rs = stmt.executeQuery("SELECT \"PARAM_VALUE\" FROM \"DATABASE_PARAMS\" WHERE \"PARAM_KEY\" = " +
+              "'" + prop + "' AND \"DB_ID\" = " + dbId);
+      if (!rs.next()) {
+        query = "INSERT INTO \"DATABASE_PARAMS\" VALUES ( " + dbId + " , '" + prop + "' , ? )";
+      } else {
+        query = "UPDATE \"DATABASE_PARAMS\" SET \"PARAM_VALUE\" = ? WHERE \"DB_ID\" = " + dbId +

Review comment:
       If the value to be updated is same as the current value, we might skip the updation

##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/txn/TxnHandler.java
##########
@@ -1122,12 +1154,76 @@ public void abortTxns(AbortTxnsRequest rqst) throws MetaException {
     }
   }
 
+  private long getDatabaseId(Connection dbConn, String database, String catalog) throws SQLException {
+    ResultSet rs = null;
+    PreparedStatement pst = null;
+    try {
+      String query = "select \"DB_ID\" from \"DBS\" where \"NAME\" = ?  and \"CTLG_NAME\" = ?";
+      pst = sqlGenerator.prepareStmtWithParameters(dbConn, query, Arrays.asList(database, catalog));
+      LOG.debug("Going to execute query <" + query.replaceAll("\\?", "{}") + ">",
+              quoteString(database), quoteString(catalog));
+      rs = pst.executeQuery();
+      if (!rs.next()) {
+        LOG.error("Database: " + database + " does not exist in catalog " + catalog);
+        return -1;
+      }
+      return rs.getLong(1);
+    } finally {
+      close(rs);
+      closeStmt(pst);
+    }
+  }
+
+  private void updateDatabaseProp(Connection dbConn, Statement stmt,
+                                  long dbId, String prop, String propValue) throws SQLException {
+    ResultSet rs = null;
+    PreparedStatement pst = null;
+    String query;
+    try {
+      rs = stmt.executeQuery("SELECT \"PARAM_VALUE\" FROM \"DATABASE_PARAMS\" WHERE \"PARAM_KEY\" = " +
+              "'" + prop + "' AND \"DB_ID\" = " + dbId);
+      if (!rs.next()) {
+        query = "INSERT INTO \"DATABASE_PARAMS\" VALUES ( " + dbId + " , '" + prop + "' , ? )";
+      } else {
+        query = "UPDATE \"DATABASE_PARAMS\" SET \"PARAM_VALUE\" = ? WHERE \"DB_ID\" = " + dbId +
+                " AND \"PARAM_KEY\" = '" + prop + "'";
+      }
+      close(rs);
+      pst = sqlGenerator.prepareStmtWithParameters(dbConn, query, Arrays.asList(propValue));
+      LOG.debug("Updating " + prop + " for db <" + query.replaceAll("\\?", "{}") + ">", propValue);
+      if (pst.executeUpdate() != 1) {
+        //only one row insert or update should happen
+        throw new RuntimeException("DATABASE_PARAMS is corrupted for databaseID: " + dbId);
+      }
+    } finally {
+      close(rs);
+      closeStmt(pst);
+    }
+  }
+
+  private void markDbAsReplIncompatible(Connection dbConn, String database) throws SQLException {
+    Statement stmt = dbConn.createStatement();
+    String catalog = MetaStoreUtils.getDefaultCatalog(conf);
+    String s = sqlGenerator.getDbProduct().getPrepareTxnStmt();
+    if (s != null) {
+      stmt.execute(s);
+    }
+    long dbId = getDatabaseId(dbConn, database, catalog);
+    if (dbId == -1) {

Review comment:
       If DB is not present, wouldn't that be an error case?




-- 
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: gitbox-unsubscribe@hive.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org