use of org.apache.hadoop.hive.metastore.events.AbortTxnEvent in project hive by apache.
the class TxnHandler method abortTxns.
@Override
@RetrySemantics.Idempotent
public void abortTxns(AbortTxnsRequest rqst) throws MetaException {
List<Long> txnIds = rqst.getTxn_ids();
try {
Connection dbConn = null;
Statement stmt = null;
try {
dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED);
stmt = dbConn.createStatement();
List<String> queries = new ArrayList<>();
StringBuilder prefix = new StringBuilder("SELECT \"TXN_ID\", \"TXN_TYPE\" from \"TXNS\" where \"TXN_STATE\" = ").append(TxnStatus.OPEN).append(" and \"TXN_TYPE\" != ").append(TxnType.READ_ONLY.getValue()).append(" and ");
TxnUtils.buildQueryWithINClause(conf, queries, prefix, new StringBuilder(), txnIds, "\"TXN_ID\"", false, false);
Map<Long, TxnType> nonReadOnlyTxns = new HashMap<>();
for (String query : queries) {
LOG.debug("Going to execute query<" + query + ">");
try (ResultSet rs = stmt.executeQuery(sqlGenerator.addForUpdateClause(query))) {
while (rs.next()) {
TxnType txnType = TxnType.findByValue(rs.getInt(2));
nonReadOnlyTxns.put(rs.getLong(1), txnType);
}
}
}
int numAborted = abortTxns(dbConn, txnIds, false, false);
if (numAborted != txnIds.size()) {
LOG.warn("Abort Transactions command only aborted " + numAborted + " out of " + txnIds.size() + " transactions. It's possible that the other " + (txnIds.size() - numAborted) + " transactions have been aborted or committed, or the transaction ids are invalid.");
}
if (transactionalListeners != null) {
for (Long txnId : txnIds) {
MetaStoreListenerNotifier.notifyEventWithDirectSql(transactionalListeners, EventMessage.EventType.ABORT_TXN, new AbortTxnEvent(txnId, nonReadOnlyTxns.getOrDefault(txnId, TxnType.READ_ONLY)), dbConn, sqlGenerator);
}
}
LOG.debug("Going to commit");
dbConn.commit();
} catch (SQLException e) {
LOG.debug("Going to rollback: ", e);
rollbackDBConn(dbConn);
checkRetryable(e, "abortTxns(" + rqst + ")");
throw new MetaException("Unable to update transaction database " + StringUtils.stringifyException(e));
} finally {
closeStmt(stmt);
closeDbConn(dbConn);
}
} catch (RetryException e) {
abortTxns(rqst);
}
}
use of org.apache.hadoop.hive.metastore.events.AbortTxnEvent in project hive by apache.
the class TxnHandler method abortTxn.
@Override
@RetrySemantics.Idempotent
public void abortTxn(AbortTxnRequest rqst) throws NoSuchTxnException, MetaException, TxnAbortedException {
long txnid = rqst.getTxnid();
long sourceTxnId = -1;
boolean isReplayedReplTxn = TxnType.REPL_CREATED.equals(rqst.getTxn_type());
boolean isHiveReplTxn = rqst.isSetReplPolicy() && TxnType.DEFAULT.equals(rqst.getTxn_type());
try {
Connection dbConn = null;
Statement stmt = null;
try {
lockInternal();
dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED);
stmt = dbConn.createStatement();
if (isReplayedReplTxn) {
assert (rqst.isSetReplPolicy());
sourceTxnId = rqst.getTxnid();
List<Long> targetTxnIds = getTargetTxnIdList(rqst.getReplPolicy(), Collections.singletonList(sourceTxnId), dbConn);
if (targetTxnIds.isEmpty()) {
// Idempotent case where txn was already closed or abort txn event received without
// corresponding open txn event.
LOG.info("Target txn id is missing for source txn id : " + sourceTxnId + " and repl policy " + rqst.getReplPolicy());
return;
}
assert targetTxnIds.size() == 1;
txnid = targetTxnIds.get(0);
}
TxnType txnType = getOpenTxnTypeAndLock(stmt, txnid);
if (txnType == null) {
TxnStatus status = findTxnState(txnid, stmt);
if (status == TxnStatus.ABORTED) {
if (isReplayedReplTxn) {
// in case of replication, idempotent is taken care by getTargetTxnId
LOG.warn("Invalid state ABORTED for transactions started using replication replay task");
deleteReplTxnMapEntry(dbConn, sourceTxnId, rqst.getReplPolicy());
}
LOG.info("abortTxn(" + JavaUtils.txnIdToString(txnid) + ") requested by it is already " + TxnStatus.ABORTED);
return;
}
raiseTxnUnexpectedState(status, txnid);
}
abortTxns(dbConn, Collections.singletonList(txnid), true, isReplayedReplTxn);
if (isReplayedReplTxn) {
deleteReplTxnMapEntry(dbConn, sourceTxnId, rqst.getReplPolicy());
}
if (transactionalListeners != null && !isHiveReplTxn) {
MetaStoreListenerNotifier.notifyEventWithDirectSql(transactionalListeners, EventMessage.EventType.ABORT_TXN, new AbortTxnEvent(txnid, txnType), dbConn, sqlGenerator);
}
LOG.debug("Going to commit");
dbConn.commit();
} catch (SQLException e) {
LOG.debug("Going to rollback: ", e);
rollbackDBConn(dbConn);
checkRetryable(e, "abortTxn(" + rqst + ")");
throw new MetaException("Unable to update transaction database " + StringUtils.stringifyException(e));
} finally {
close(null, stmt, dbConn);
unlockInternal();
}
} catch (RetryException e) {
abortTxn(rqst);
}
}
Aggregations