Search in sources :

Example 1 with TableValidWriteIds

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

the class TxnHandler method getValidWriteIds.

@Override
@RetrySemantics.ReadOnly
public GetValidWriteIdsResponse getValidWriteIds(GetValidWriteIdsRequest rqst) throws NoSuchTxnException, MetaException {
    try {
        Connection dbConn = null;
        Statement stmt = null;
        ValidTxnList validTxnList;
        // required to get the current state of txns to make validTxnList
        if (rqst.isSetValidTxnList()) {
            validTxnList = new ValidReadTxnList(rqst.getValidTxnList());
        } else {
            // Passing 0 for currentTxn means, this validTxnList is not wrt to any txn
            validTxnList = TxnUtils.createValidReadTxnList(getOpenTxns(), 0);
        }
        try {
            /**
             * This runs at READ_COMMITTED for exactly the same reason as {@link #getOpenTxnsInfo()}
             */
            dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED);
            stmt = dbConn.createStatement();
            // Get the valid write id list for all the tables read by the current txn
            List<TableValidWriteIds> tblValidWriteIdsList = new ArrayList<>();
            for (String fullTableName : rqst.getFullTableNames()) {
                tblValidWriteIdsList.add(getValidWriteIdsForTable(stmt, fullTableName, validTxnList));
            }
            LOG.debug("Going to rollback");
            dbConn.rollback();
            GetValidWriteIdsResponse owr = new GetValidWriteIdsResponse(tblValidWriteIdsList);
            return owr;
        } catch (SQLException e) {
            LOG.debug("Going to rollback");
            rollbackDBConn(dbConn);
            checkRetryable(dbConn, e, "getValidWriteIds");
            throw new MetaException("Unable to select from transaction database, " + StringUtils.stringifyException(e));
        } finally {
            close(null, stmt, dbConn);
        }
    } catch (RetryException e) {
        return getValidWriteIds(rqst);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ValidTxnList(org.apache.hadoop.hive.common.ValidTxnList) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) GetValidWriteIdsResponse(org.apache.hadoop.hive.metastore.api.GetValidWriteIdsResponse) ValidReadTxnList(org.apache.hadoop.hive.common.ValidReadTxnList) TableValidWriteIds(org.apache.hadoop.hive.metastore.api.TableValidWriteIds) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 2 with TableValidWriteIds

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

the class TxnHandler method getValidWriteIdsForTable.

// Method to get the Valid write ids list for the given table
// Input fullTableName is expected to be of format <db_name>.<table_name>
private TableValidWriteIds getValidWriteIdsForTable(Statement stmt, String fullTableName, ValidTxnList validTxnList) throws SQLException {
    ResultSet rs = null;
    String[] names = TxnUtils.getDbTableName(fullTableName);
    try {
        // Need to initialize to 0 to make sure if nobody modified this table, then current txn
        // shouldn't read any data.
        // If there is a conversion from non-acid to acid table, then by default 0 would be assigned as
        // writeId for data from non-acid table and so writeIdHwm=0 would ensure those data are readable by any txns.
        long writeIdHwm = 0;
        List<Long> invalidWriteIdList = new ArrayList<>();
        long minOpenWriteId = Long.MAX_VALUE;
        BitSet abortedBits = new BitSet();
        long txnHwm = validTxnList.getHighWatermark();
        // Find the writeId high water mark based upon txnId high water mark. If found, then, need to
        // traverse through all write Ids less than writeId HWM to make exceptions list.
        String s = "select max(t2w_writeid) from TXN_TO_WRITE_ID where t2w_txnid <= " + txnHwm + " and t2w_database = " + quoteString(names[0]) + " and t2w_table = " + quoteString(names[1]);
        LOG.debug("Going to execute query<" + s + ">");
        rs = stmt.executeQuery(s);
        if (rs.next()) {
            writeIdHwm = rs.getLong(1);
            // As writeIdHwm is known, query all writeIds under the writeId HWM.
            // If any writeId under HWM is allocated by txn > txnId HWM, then will be added to invalid list.
            // The output of this query includes all the txns which are under the high water mark. It includes
            // the committed transactions as well. The results should be sorted in ascending order based
            // on write id. The sorting is needed as exceptions list in ValidWriteIdList would be looked-up
            // using binary search.
            s = "select t2w_txnid, t2w_writeid from TXN_TO_WRITE_ID where t2w_writeid <= " + writeIdHwm + " and t2w_database = " + quoteString(names[0]) + " and t2w_table = " + quoteString(names[1]) + " order by t2w_writeid asc";
            LOG.debug("Going to execute query<" + s + ">");
            rs = stmt.executeQuery(s);
            while (rs.next()) {
                long txnId = rs.getLong(1);
                long writeId = rs.getLong(2);
                if (validTxnList.isTxnValid(txnId)) {
                    // Skip if the transaction under evaluation is already committed.
                    continue;
                }
                // The current txn is either in open or aborted state.
                // Mark the write ids state as per the txn state.
                invalidWriteIdList.add(writeId);
                if (validTxnList.isTxnAborted(txnId)) {
                    abortedBits.set(invalidWriteIdList.size() - 1);
                } else {
                    minOpenWriteId = Math.min(minOpenWriteId, writeId);
                }
            }
        }
        ByteBuffer byteBuffer = ByteBuffer.wrap(abortedBits.toByteArray());
        TableValidWriteIds owi = new TableValidWriteIds(fullTableName, writeIdHwm, invalidWriteIdList, byteBuffer);
        if (minOpenWriteId < Long.MAX_VALUE) {
            owi.setMinOpenWriteId(minOpenWriteId);
        }
        return owi;
    } finally {
        close(rs);
    }
}
Also used : ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) ByteBuffer(java.nio.ByteBuffer) TableValidWriteIds(org.apache.hadoop.hive.metastore.api.TableValidWriteIds)

Aggregations

ArrayList (java.util.ArrayList)2 TableValidWriteIds (org.apache.hadoop.hive.metastore.api.TableValidWriteIds)2 ByteBuffer (java.nio.ByteBuffer)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 BitSet (java.util.BitSet)1 ValidReadTxnList (org.apache.hadoop.hive.common.ValidReadTxnList)1 ValidTxnList (org.apache.hadoop.hive.common.ValidTxnList)1 GetValidWriteIdsResponse (org.apache.hadoop.hive.metastore.api.GetValidWriteIdsResponse)1 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)1