veghlaci05 commented on code in PR #4384:
URL: https://github.com/apache/hive/pull/4384#discussion_r1309918630


##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/txn/CompactionTxnHandler.java:
##########
@@ -444,136 +158,15 @@ public void markCompacted(CompactionInfo info) throws 
MetaException {
   @Override
   @RetrySemantics.ReadOnly
   public List<CompactionInfo> findReadyToClean(long minOpenTxnWaterMark, long 
retentionTime) throws MetaException {
-    try {
-      List<CompactionInfo> rc = new ArrayList<>();
-
-      try (Connection dbConn = 
getDbConn(Connection.TRANSACTION_READ_COMMITTED, connPoolCompaction);
-          Statement stmt = dbConn.createStatement()) {
-        /*
-         * By filtering on minOpenTxnWaterMark, we will only cleanup after 
every transaction is committed, that could see
-         * the uncompacted deltas. This way the cleaner can clean up 
everything that was made obsolete by this compaction.
-         */
-        String whereClause = " WHERE \"CQ_STATE\" = " + 
quoteChar(READY_FOR_CLEANING) +
-          " AND \"CQ_TYPE\" != " + quoteChar(TxnStore.ABORT_TXN_CLEANUP_TYPE) +
-          " AND (\"CQ_COMMIT_TIME\" < (" + getEpochFn(dbProduct) + " - 
\"CQ_RETRY_RETENTION\" - " + retentionTime + ") OR \"CQ_COMMIT_TIME\" IS NULL)";
-        
-        String queryStr = 
-          "  \"CQ_ID\", \"cq1\".\"CQ_DATABASE\", \"cq1\".\"CQ_TABLE\", 
\"cq1\".\"CQ_PARTITION\"," +
-          "  \"CQ_TYPE\", \"CQ_RUN_AS\", \"CQ_HIGHEST_WRITE_ID\", 
\"CQ_TBLPROPERTIES\", \"CQ_RETRY_RETENTION\", " +
-          "  \"CQ_NEXT_TXN_ID\"";
-        if (useMinHistoryWriteId) {
-          queryStr += ", \"MIN_OPEN_WRITE_ID\"";
-        }
-        queryStr +=
-          "  FROM \"COMPACTION_QUEUE\" \"cq1\" " +
-          "INNER JOIN (" +
-          "  SELECT MIN(\"CQ_HIGHEST_WRITE_ID\") \"MIN_WRITE_ID_HWM\", 
\"CQ_DATABASE\", \"CQ_TABLE\", \"CQ_PARTITION\"" +
-          "  FROM \"COMPACTION_QUEUE\"" 
-          + whereClause +
-          "  GROUP BY \"CQ_DATABASE\", \"CQ_TABLE\", \"CQ_PARTITION\") \"cq2\" 
" +
-          "ON \"cq1\".\"CQ_DATABASE\" = \"cq2\".\"CQ_DATABASE\""+
-          "  AND \"cq1\".\"CQ_TABLE\" = \"cq2\".\"CQ_TABLE\""+
-          "  AND (\"cq1\".\"CQ_PARTITION\" = \"cq2\".\"CQ_PARTITION\"" +
-          "    OR \"cq1\".\"CQ_PARTITION\" IS NULL AND 
\"cq2\".\"CQ_PARTITION\" IS NULL)" +
-          "  AND \"CQ_HIGHEST_WRITE_ID\" = \"MIN_WRITE_ID_HWM\" ";
-        
-        if (useMinHistoryWriteId) {
-          queryStr += 
-            "LEFT JOIN (" +
-            "  SELECT MIN(\"MH_WRITEID\") \"MIN_OPEN_WRITE_ID\", 
\"MH_DATABASE\", \"MH_TABLE\"" +
-            "  FROM \"MIN_HISTORY_WRITE_ID\"" +
-            "  GROUP BY \"MH_DATABASE\", \"MH_TABLE\") \"hwm\" " +
-            "ON \"cq1\".\"CQ_DATABASE\" = \"hwm\".\"MH_DATABASE\"" +
-            "  AND \"cq1\".\"CQ_TABLE\" = \"hwm\".\"MH_TABLE\"";
-          
-          whereClause += " AND (\"CQ_HIGHEST_WRITE_ID\" < 
\"MIN_OPEN_WRITE_ID\" OR \"MIN_OPEN_WRITE_ID\" IS NULL)";
-          
-        } else if (minOpenTxnWaterMark > 0) {
-          whereClause += " AND (\"CQ_NEXT_TXN_ID\" <= " + minOpenTxnWaterMark 
+ " OR \"CQ_NEXT_TXN_ID\" IS NULL)";
-        }
-
-        queryStr += whereClause + " ORDER BY \"CQ_ID\"";
-
-        queryStr = dbProduct.addLimitClause(MetastoreConf.getIntVar(conf, 
ConfVars.COMPACTOR_FETCH_SIZE), queryStr);
-        LOG.debug("Going to execute query <{}>", queryStr);
-
-        try (ResultSet rs = stmt.executeQuery(queryStr)) {
-          while (rs.next()) {
-            CompactionInfo info = new CompactionInfo();
-            info.id = rs.getLong(1);
-            info.dbname = rs.getString(2);
-            info.tableName = rs.getString(3);
-            info.partName = rs.getString(4);
-            info.type = 
TxnUtils.dbCompactionType2ThriftType(rs.getString(5).charAt(0));
-            info.runAs = rs.getString(6);
-            info.highestWriteId = rs.getLong(7);
-            info.properties = rs.getString(8);
-            info.retryRetention = rs.getInt(9);
-            info.nextTxnId = rs.getLong(10);
-            if (useMinHistoryWriteId) {
-              info.minOpenWriteId = rs.getLong(11);
-            }
-            LOG.debug("Found ready to clean: {}", info);
-            rc.add(info);
-          }
-        }
-        return rc;
-      } catch (SQLException e) {
-        LOG.error("Unable to select next element for cleaning, " + 
e.getMessage());
-        checkRetryable(e, "findReadyToClean");
-        throw new MetaException(DB_FAILED_TO_CONNECT + e.getMessage());
-      }
-    } catch (RetryException e) {
-      return findReadyToClean(minOpenTxnWaterMark, retentionTime);
-    }
+    return jdbcTemplate.execute(new ReadyToCleanHandler(useMinHistoryWriteId, 
minOpenTxnWaterMark, retentionTime, 
+        MetastoreConf.getIntVar(conf, ConfVars.COMPACTOR_FETCH_SIZE)));
   }
 
   @Override
   @RetrySemantics.ReadOnly
   public List<CompactionInfo> findReadyToCleanAborts(long 
abortedTimeThreshold, int abortedThreshold) throws MetaException {
-    try {
-      List<CompactionInfo> readyToCleanAborts = new ArrayList<>();
-      try (Connection dbConn = 
getDbConn(Connection.TRANSACTION_READ_COMMITTED, connPoolCompaction);
-           Statement stmt = dbConn.createStatement()) {
-        boolean checkAbortedTimeThreshold = abortedTimeThreshold >= 0;
-
-        String sCheckAborted = dbProduct.addLimitClause(
-                MetastoreConf.getIntVar(conf, ConfVars.COMPACTOR_FETCH_SIZE),
-                String.format(SELECT_ABORTS_WITH_MIN_OPEN_WRITETXN_QUERY,
-                checkAbortedTimeThreshold ? "" : " HAVING COUNT(*) > " + 
abortedThreshold, getEpochFn(dbProduct)));
-        LOG.debug("Going to execute query <{}>", sCheckAborted);
-
-        try (ResultSet rs = stmt.executeQuery(sCheckAborted)) {
-          long systemTime = System.currentTimeMillis();
-          while (rs.next()) {
-            boolean pastTimeThreshold =
-                    checkAbortedTimeThreshold && rs.getLong(4) + 
abortedTimeThreshold < systemTime;
-            int numAbortedTxns = rs.getInt(5);
-            if (numAbortedTxns > abortedThreshold || pastTimeThreshold) {
-              CompactionInfo info = new CompactionInfo();
-              info.dbname = rs.getString(1);
-              info.tableName = rs.getString(2);
-              info.partName = rs.getString(3);
-              // In this case, this field contains min open write txn ID.
-              info.minOpenWriteTxnId = rs.getLong(6) > 0 ? rs.getLong(6) : 
Long.MAX_VALUE;
-              // The specific type, state assigned to abort cleanup.
-              info.type = CompactionType.ABORT_TXN_CLEANUP;
-              info.state = READY_FOR_CLEANING;
-              info.retryRetention = rs.getLong(7);
-              info.id = rs.getLong(8);
-              readyToCleanAborts.add(info);
-            }
-          }
-        }
-        return readyToCleanAborts;
-      } catch (SQLException e) {
-        LOG.error("Unable to select next element for cleaning, " + 
e.getMessage());
-        checkRetryable(e, "findReadyToCleanAborts");
-        throw new MetaException(DB_FAILED_TO_CONNECT + e.getMessage());
-      }
-    } catch (RetryException e) {
-      return findReadyToCleanAborts(abortedTimeThreshold, abortedThreshold);
-    }
+    return jdbcTemplate.execute(new AbortTxnInfoHandler(abortedTimeThreshold, 
abortedThreshold,

Review Comment:
   done



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to