Search in sources :

Example 1 with TxnState

use of org.apache.hadoop.hive.metastore.api.TxnState in project hive by apache.

the class TxnHandler method getOpenTxnsInfo.

@Override
@RetrySemantics.ReadOnly
public GetOpenTxnsInfoResponse getOpenTxnsInfo() throws MetaException {
    try {
        // We need to figure out the current transaction number and the list of
        // open transactions.  To avoid needing a transaction on the underlying
        // database we'll look at the current transaction number first.  If it
        // subsequently shows up in the open list that's ok.
        Connection dbConn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            /**
             * This method can run at READ_COMMITTED as long as long as
             * {@link #openTxns(org.apache.hadoop.hive.metastore.api.OpenTxnRequest)} is atomic.
             * More specifically, as long as advancing TransactionID in NEXT_TXN_ID is atomic with
             * adding corresponding entries into TXNS.  The reason is that any txnid below HWM
             * is either in TXNS and thus considered open (Open/Aborted) or it's considered Committed.
             */
            dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED);
            stmt = dbConn.createStatement();
            String s = "select ntxn_next - 1 from NEXT_TXN_ID";
            LOG.debug("Going to execute query <" + s + ">");
            rs = stmt.executeQuery(s);
            if (!rs.next()) {
                throw new MetaException("Transaction tables not properly " + "initialized, no record found in next_txn_id");
            }
            long hwm = rs.getLong(1);
            if (rs.wasNull()) {
                throw new MetaException("Transaction tables not properly " + "initialized, null record found in next_txn_id");
            }
            close(rs);
            List<TxnInfo> txnInfos = new ArrayList<>();
            // need the WHERE clause below to ensure consistent results with READ_COMMITTED
            s = "select txn_id, txn_state, txn_user, txn_host, txn_started, txn_last_heartbeat from " + "TXNS where txn_id <= " + hwm;
            LOG.debug("Going to execute query<" + s + ">");
            rs = stmt.executeQuery(s);
            while (rs.next()) {
                char c = rs.getString(2).charAt(0);
                TxnState state;
                switch(c) {
                    case TXN_ABORTED:
                        state = TxnState.ABORTED;
                        break;
                    case TXN_OPEN:
                        state = TxnState.OPEN;
                        break;
                    default:
                        throw new MetaException("Unexpected transaction state " + c + " found in txns table");
                }
                TxnInfo txnInfo = new TxnInfo(rs.getLong(1), state, rs.getString(3), rs.getString(4));
                txnInfo.setStartedTime(rs.getLong(5));
                txnInfo.setLastHeartbeatTime(rs.getLong(6));
                txnInfos.add(txnInfo);
            }
            LOG.debug("Going to rollback");
            dbConn.rollback();
            return new GetOpenTxnsInfoResponse(hwm, txnInfos);
        } catch (SQLException e) {
            LOG.debug("Going to rollback");
            rollbackDBConn(dbConn);
            checkRetryable(dbConn, e, "getOpenTxnsInfo");
            throw new MetaException("Unable to select from transaction database: " + getMessage(e) + StringUtils.stringifyException(e));
        } finally {
            close(rs, stmt, dbConn);
        }
    } catch (RetryException e) {
        return getOpenTxnsInfo();
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) TxnState(org.apache.hadoop.hive.metastore.api.TxnState) ResultSet(java.sql.ResultSet) BasicTxnInfo(org.apache.hadoop.hive.metastore.api.BasicTxnInfo) TxnInfo(org.apache.hadoop.hive.metastore.api.TxnInfo) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) GetOpenTxnsInfoResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse)

Aggregations

Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 BasicTxnInfo (org.apache.hadoop.hive.metastore.api.BasicTxnInfo)1 GetOpenTxnsInfoResponse (org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse)1 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)1 TxnInfo (org.apache.hadoop.hive.metastore.api.TxnInfo)1 TxnState (org.apache.hadoop.hive.metastore.api.TxnState)1