Search in sources :

Example 1 with OpenTxnEvent

use of org.apache.hadoop.hive.metastore.events.OpenTxnEvent in project hive by apache.

the class TxnHandler method openTxns.

private List<Long> openTxns(Connection dbConn, OpenTxnRequest rqst) throws SQLException, MetaException {
    int numTxns = rqst.getNum_txns();
    // Make sure the user has not requested an insane amount of txns.
    int maxTxns = MetastoreConf.getIntVar(conf, ConfVars.TXN_MAX_OPEN_BATCH);
    if (numTxns > maxTxns) {
        numTxns = maxTxns;
    }
    List<PreparedStatement> insertPreparedStmts = null;
    TxnType txnType = rqst.isSetTxn_type() ? rqst.getTxn_type() : TxnType.DEFAULT;
    boolean isReplayedReplTxn = txnType == TxnType.REPL_CREATED;
    boolean isHiveReplTxn = rqst.isSetReplPolicy() && txnType == TxnType.DEFAULT;
    try {
        if (isReplayedReplTxn) {
            assert rqst.isSetReplPolicy();
            List<Long> targetTxnIdList = getTargetTxnIdList(rqst.getReplPolicy(), rqst.getReplSrcTxnIds(), dbConn);
            if (!targetTxnIdList.isEmpty()) {
                if (targetTxnIdList.size() != rqst.getReplSrcTxnIds().size()) {
                    LOG.warn("target txn id number " + targetTxnIdList.toString() + " is not matching with source txn id number " + rqst.getReplSrcTxnIds().toString());
                }
                LOG.info("Target transactions " + targetTxnIdList.toString() + " are present for repl policy :" + rqst.getReplPolicy() + " and Source transaction id : " + rqst.getReplSrcTxnIds().toString());
                return targetTxnIdList;
            }
        }
        long minOpenTxnId = 0;
        if (useMinHistoryLevel) {
            minOpenTxnId = getMinOpenTxnIdWaterMark(dbConn);
        }
        List<Long> txnIds = new ArrayList<>(numTxns);
        /*
       * The getGeneratedKeys are not supported in every dbms, after executing a multi line insert.
       * But it is supported in every used dbms for single line insert, even if the metadata says otherwise.
       * If the getGeneratedKeys are not supported first we insert a random batchId in the TXN_META_INFO field,
       * then the keys are selected beck with that batchid.
       */
        boolean genKeySupport = dbProduct.supportsGetGeneratedKeys();
        genKeySupport = genKeySupport || (numTxns == 1);
        String insertQuery = String.format(TXNS_INSERT_QRY, getEpochFn(dbProduct), getEpochFn(dbProduct));
        LOG.debug("Going to execute insert <" + insertQuery + ">");
        try (PreparedStatement ps = dbConn.prepareStatement(insertQuery, new String[] { "TXN_ID" })) {
            String state = genKeySupport ? TxnStatus.OPEN.getSqlConst() : TXN_TMP_STATE;
            if (numTxns == 1) {
                ps.setString(1, state);
                ps.setString(2, rqst.getUser());
                ps.setString(3, rqst.getHostname());
                ps.setInt(4, txnType.getValue());
                txnIds.addAll(executeTxnInsertBatchAndExtractGeneratedKeys(dbConn, genKeySupport, ps, false));
            } else {
                for (int i = 0; i < numTxns; ++i) {
                    ps.setString(1, state);
                    ps.setString(2, rqst.getUser());
                    ps.setString(3, rqst.getHostname());
                    ps.setInt(4, txnType.getValue());
                    ps.addBatch();
                    if ((i + 1) % maxBatchSize == 0) {
                        txnIds.addAll(executeTxnInsertBatchAndExtractGeneratedKeys(dbConn, genKeySupport, ps, true));
                    }
                }
                if (numTxns % maxBatchSize != 0) {
                    txnIds.addAll(executeTxnInsertBatchAndExtractGeneratedKeys(dbConn, genKeySupport, ps, true));
                }
            }
        }
        assert txnIds.size() == numTxns;
        addTxnToMinHistoryLevel(dbConn, txnIds, minOpenTxnId);
        if (isReplayedReplTxn) {
            List<String> rowsRepl = new ArrayList<>(numTxns);
            List<String> params = Collections.singletonList(rqst.getReplPolicy());
            List<List<String>> paramsList = new ArrayList<>(numTxns);
            for (int i = 0; i < numTxns; i++) {
                rowsRepl.add("?," + rqst.getReplSrcTxnIds().get(i) + "," + txnIds.get(i));
                paramsList.add(params);
            }
            insertPreparedStmts = sqlGenerator.createInsertValuesPreparedStmt(dbConn, "\"REPL_TXN_MAP\" (\"RTM_REPL_POLICY\", \"RTM_SRC_TXN_ID\", \"RTM_TARGET_TXN_ID\")", rowsRepl, paramsList);
            for (PreparedStatement pst : insertPreparedStmts) {
                pst.execute();
            }
        }
        if (transactionalListeners != null && !isHiveReplTxn) {
            MetaStoreListenerNotifier.notifyEventWithDirectSql(transactionalListeners, EventMessage.EventType.OPEN_TXN, new OpenTxnEvent(txnIds, txnType), dbConn, sqlGenerator);
        }
        return txnIds;
    } finally {
        if (insertPreparedStmts != null) {
            for (PreparedStatement pst : insertPreparedStmts) {
                pst.close();
            }
        }
    }
}
Also used : TxnType(org.apache.hadoop.hive.metastore.api.TxnType) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Savepoint(java.sql.Savepoint) OpenTxnEvent(org.apache.hadoop.hive.metastore.events.OpenTxnEvent) ValidReadTxnList(org.apache.hadoop.hive.common.ValidReadTxnList) ArrayList(java.util.ArrayList) ValidTxnWriteIdList(org.apache.hadoop.hive.common.ValidTxnWriteIdList) ValidTxnList(org.apache.hadoop.hive.common.ValidTxnList) ValidWriteIdList(org.apache.hadoop.hive.common.ValidWriteIdList) ValidReaderWriteIdList(org.apache.hadoop.hive.common.ValidReaderWriteIdList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 PreparedStatement (java.sql.PreparedStatement)1 Savepoint (java.sql.Savepoint)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ValidReadTxnList (org.apache.hadoop.hive.common.ValidReadTxnList)1 ValidReaderWriteIdList (org.apache.hadoop.hive.common.ValidReaderWriteIdList)1 ValidTxnList (org.apache.hadoop.hive.common.ValidTxnList)1 ValidTxnWriteIdList (org.apache.hadoop.hive.common.ValidTxnWriteIdList)1 ValidWriteIdList (org.apache.hadoop.hive.common.ValidWriteIdList)1 TxnType (org.apache.hadoop.hive.metastore.api.TxnType)1 OpenTxnEvent (org.apache.hadoop.hive.metastore.events.OpenTxnEvent)1