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();
}
}
}
}
Aggregations