@Override public List<Long> replOpenTxn(String replPolicy, List<Long> srcTxnIds, String user) throws LockException { try { return getMS().replOpenTxn(replPolicy, srcTxnIds, user); } catch (TException e) { throw new LockException(e, ErrorMsg.METASTORE_COMMUNICATION_FAILED); } }
@Override public void replRollbackTxn(String replPolicy, long srcTxnId) throws LockException { try { getMS().replRollbackTxn(srcTxnId, replPolicy); } catch (NoSuchTxnException e) { LOG.error("Metastore could not find " + JavaUtils.txnIdToString(srcTxnId)); throw new LockException(e, ErrorMsg.TXN_NO_SUCH_TRANSACTION, JavaUtils.txnIdToString(srcTxnId)); } catch (TxnAbortedException e) { LockException le = new LockException(e, ErrorMsg.TXN_ABORTED, JavaUtils.txnIdToString(srcTxnId), e.getMessage()); LOG.error(le.getMessage()); throw le; } catch (TException e) { throw new LockException(ErrorMsg.METASTORE_COMMUNICATION_FAILED.getMsg(), e); } }
@Override public void acquireLocks(QueryPlan plan, Context ctx, String username) throws LockException { try { acquireLocksWithHeartbeatDelay(plan, ctx, username, 0); } catch(LockException e) { if(e.getCause() instanceof TxnAbortedException) { txnId = 0; stmtId = -1; tableWriteIds.clear(); } throw e; } }
@Override public void run() { try { releaseLocksAndCommitOrRollback(false, txnManager); } catch (LockException e) { LOG.warn("Exception when releasing locks in ShutdownHook for Driver: " + e.getMessage()); } } };
Assert.assertEquals("Got wrong message code", ErrorMsg.TXN_ABORTED, expectedException.getCanonicalErrorMsg()); Assert.assertEquals("Exception msg didn't match", "Aborting [txnid:"+txnId2+","+txnId2+"] due to a write conflict on default/tab_part/p=blah committed by [txnid:"+txnId+","+txnId2+"] u/u", expectedException.getCause().getMessage());
@Test public void testExceptions() throws Exception { addPartitionOutput(newTable(true), WriteEntity.WriteType.INSERT); QueryPlan qp = new MockQueryPlan(this, HiveOperation.QUERY); ((DbTxnManager) txnMgr).openTxn(ctx, "NicholasII", HiveConf.getTimeVar(conf, HiveConf.ConfVars.HIVE_TXN_TIMEOUT, TimeUnit.MILLISECONDS) * 2); Thread.sleep(HiveConf.getTimeVar(conf, HiveConf.ConfVars.HIVE_TXN_TIMEOUT, TimeUnit.MILLISECONDS)); runReaper(); LockException exception = null; try { txnMgr.commitTxn(); } catch(LockException ex) { exception = ex; } Assert.assertNotNull("Expected exception1", exception); Assert.assertEquals("Wrong Exception1", ErrorMsg.TXN_ABORTED, exception.getCanonicalErrorMsg()); exception = null; ((DbTxnManager) txnMgr).openTxn(ctx, "AlexanderIII", HiveConf.getTimeVar(conf, HiveConf.ConfVars.HIVE_TXN_TIMEOUT, TimeUnit.MILLISECONDS) * 2); Thread.sleep(HiveConf.getTimeVar(conf, HiveConf.ConfVars.HIVE_TXN_TIMEOUT, TimeUnit.MILLISECONDS)); runReaper();//this will abort the txn TxnStore txnHandler = TxnUtils.getTxnStore(conf); GetOpenTxnsInfoResponse txnsInfo = txnHandler.getOpenTxnsInfo(); assertEquals(2, txnsInfo.getTxn_high_water_mark()); assertEquals(2, txnsInfo.getOpen_txns().size()); Assert.assertEquals(TxnState.ABORTED, txnsInfo.getOpen_txns().get(1).getState()); txnMgr.rollbackTxn();//this is idempotent }
numRetries, sleepTime, true); } catch (LockException e) { console.printError("Error in acquireLocks: "+ e.getLocalizedMessage()); lock = null;
@Override protected void destruct() { if (lockMgr != null) { try { lockMgr.close(); } catch (LockException e) { // Not much I can do about it. LOG.warn("Got exception when closing lock manager " + e.getMessage()); } } }
Assert.assertEquals("Got wrong message code", ErrorMsg.TXN_ABORTED, expectedException.getCanonicalErrorMsg()); Assert.assertEquals("Exception msg didn't match", "Aborting [txnid:7,7] due to a write conflict on default/target committed by [txnid:6,7] d/u", expectedException.getCause().getMessage());
@Test public void testLockRetryLimit() throws Exception { dropTable(new String[] {"T9"}); conf.setIntVar(HiveConf.ConfVars.HIVE_LOCK_NUMRETRIES, 2); conf.setBoolVar(HiveConf.ConfVars.TXN_MGR_DUMP_LOCK_STATE_ON_ACQUIRE_TIMEOUT, true); CommandProcessorResponse cpr = driver.run("create table T9(a int)"); checkCmdOnDriver(cpr); cpr = driver.compileAndRespond("select * from T9", true); checkCmdOnDriver(cpr); txnMgr.acquireLocks(driver.getPlan(), ctx, "Vincent Vega"); List<ShowLocksResponseElement> locks = getLocks(); Assert.assertEquals("Unexpected lock count", 1, locks.size()); checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "default", "T9", null, locks); HiveTxnManager txnMgr2 = TxnManagerFactory.getTxnManagerFactory().getTxnManager(conf); swapTxnManager(txnMgr2); cpr = driver.compileAndRespond("drop table T9", true); checkCmdOnDriver(cpr); try { txnMgr2.acquireLocks(driver.getPlan(), ctx, "Winston Winnfield"); } catch(LockException ex) { Assert.assertEquals("Got wrong lock exception", ErrorMsg.LOCK_ACQUIRE_TIMEDOUT, ex.getCanonicalErrorMsg()); } locks = getLocks(txnMgr); Assert.assertEquals("Unexpected lock count", 1, locks.size()); checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "default", "T9", null, locks); txnMgr2.closeTxnManager(); }
public static long getHeartbeatInterval(Configuration conf) throws LockException { // Retrieve HIVE_TXN_TIMEOUT in MILLISECONDS (it's defined as SECONDS), // then divide it by 2 to give us a safety factor. long interval = HiveConf.getTimeVar(conf, HiveConf.ConfVars.HIVE_TXN_TIMEOUT, TimeUnit.MILLISECONDS) / 2; if (interval == 0) { throw new LockException(HiveConf.ConfVars.HIVE_TXN_TIMEOUT.toString() + " not set," + " heartbeats won't be sent"); } return interval; }
/** * Send a heartbeat to the metastore for locks and transactions. */ @Override public void run() { try { // For negative testing purpose.. if(conf.getBoolVar(HiveConf.ConfVars.HIVE_IN_TEST) && conf.getBoolVar(HiveConf.ConfVars.HIVETESTMODEFAILHEARTBEATER)) { throw new LockException(HiveConf.ConfVars.HIVETESTMODEFAILHEARTBEATER.name() + "=true"); } LOG.debug("Heartbeating...for currentUser: " + currentUser); currentUser.doAs((PrivilegedExceptionAction<Object>) () -> { txnMgr.heartbeat(); return null; }); } catch (LockException e) { LOG.error("Failed trying to heartbeat queryId=" + queryId + ", currentUser: " + currentUser + ": " + e.getMessage()); lockException = e; } catch (Throwable t) { String errorMsg = "Failed trying to heartbeat queryId=" + queryId + ", currentUser: " + currentUser + ": " + t.getMessage(); LOG.error(errorMsg, t); lockException = new LockException(errorMsg, t); } } }
@Override public void run() { try { releaseLocksAndCommitOrRollback(false, txnMgr); } catch (LockException e) { LOG.warn("Exception when releasing locks in ShutdownHook for Driver: " + e.getMessage()); } } };
Assert.assertEquals("Got wrong message code", ErrorMsg.TXN_ABORTED, expectedException.getCanonicalErrorMsg()); Assert.assertEquals("Exception msg didn't match", "Aborting [txnid:7,7] due to a write conflict on default/target/p=1/q=2 committed by [txnid:6,7] u/u", expectedException.getCause().getMessage());
@Override public void acquireLocks(QueryPlan plan, Context ctx, String username) throws LockException { try { acquireLocksWithHeartbeatDelay(plan, ctx, username, 0); } catch(LockException e) { if(e.getCause() instanceof TxnAbortedException) { txnId = 0; statementId = -1; } throw e; } }
Assert.assertEquals(ErrorMsg.TXN_ABORTED, exception.getCanonicalErrorMsg());
private static long getHeartbeatInterval(Configuration conf) throws LockException { // Retrieve HIVE_TXN_TIMEOUT in MILLISECONDS (it's defined as SECONDS), // then divide it by 2 to give us a safety factor. long interval = HiveConf.getTimeVar(conf, HiveConf.ConfVars.HIVE_TXN_TIMEOUT, TimeUnit.MILLISECONDS) / 2; if (interval == 0) { throw new LockException(HiveConf.ConfVars.HIVE_TXN_MANAGER.toString() + " not set," + " heartbeats won't be sent"); } return interval; }
/** * Send a heartbeat to the metastore for locks and transactions. */ @Override public void run() { try { // For negative testing purpose.. if(conf.getBoolVar(HiveConf.ConfVars.HIVE_IN_TEST) && conf.getBoolVar(HiveConf.ConfVars.HIVETESTMODEFAILHEARTBEATER)) { throw new LockException(HiveConf.ConfVars.HIVETESTMODEFAILHEARTBEATER.name() + "=true"); } LOG.debug("Heartbeating..."); txnMgr.heartbeat(); } catch (LockException e) { LOG.error("Failed trying to heartbeat queryId=" + queryId + ": " + e.getMessage()); lockException = e; } catch (Throwable t) { LOG.error("Failed trying to heartbeat queryId=" + queryId + ": " + t.getMessage(), t); lockException = new LockException("Failed trying to heartbeat queryId=" + queryId + ": " + t.getMessage(), t); } } }
protected void destruct() { if (lockMgr != null) { try { lockMgr.close(); } catch (LockException e) { // Not much I can do about it. LOG.warn("Got exception when closing lock manager " + e.getMessage()); } } }
Assert.assertEquals("Exception msg doesn't match", "Aborting [txnid:5,5] due to a write conflict on default/tab1/p=two committed by [txnid:4,5] d/u", exception.getCause().getMessage());