Search in sources :

Example 1 with TxnInfo

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

the class TestTxnHandler method testRecoverManyTimeouts.

@Test
public void testRecoverManyTimeouts() throws Exception {
    long timeout = txnHandler.setTimeout(1);
    try {
        txnHandler.openTxns(new OpenTxnRequest(503, "me", "localhost"));
        Thread.sleep(10);
        txnHandler.performTimeOuts();
        GetOpenTxnsInfoResponse rsp = txnHandler.getOpenTxnsInfo();
        int numAborted = 0;
        for (TxnInfo txnInfo : rsp.getOpen_txns()) {
            assertEquals(TxnState.ABORTED, txnInfo.getState());
            numAborted++;
        }
        assertEquals(503, numAborted);
    } finally {
        txnHandler.setTimeout(timeout);
    }
}
Also used : TxnInfo(org.apache.hadoop.hive.metastore.api.TxnInfo) OpenTxnRequest(org.apache.hadoop.hive.metastore.api.OpenTxnRequest) GetOpenTxnsInfoResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse) Test(org.junit.Test)

Example 2 with TxnInfo

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

the class TestTxnCommands method testTimeOutReaper.

@Test
public void testTimeOutReaper() throws Exception {
    runStatementOnDriver("set autocommit false");
    runStatementOnDriver("start transaction");
    runStatementOnDriver("delete from " + Table.ACIDTBL + " where a = 5");
    //make sure currently running txn is considered aborted by housekeeper
    hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TIMEDOUT_TXN_REAPER_START, 0, TimeUnit.SECONDS);
    hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TXN_TIMEOUT, 2, TimeUnit.MILLISECONDS);
    AcidHouseKeeperService houseKeeperService = new AcidHouseKeeperService();
    //this will abort the txn
    TestTxnCommands2.runHouseKeeperService(houseKeeperService, hiveConf);
    //this should fail because txn aborted due to timeout
    CommandProcessorResponse cpr = runStatementOnDriverNegative("delete from " + Table.ACIDTBL + " where a = 5");
    Assert.assertTrue("Actual: " + cpr.getErrorMessage(), cpr.getErrorMessage().contains("Transaction manager has aborted the transaction txnid:1"));
    //now test that we don't timeout locks we should not
    //heartbeater should be running in the background every 1/2 second
    hiveConf.setTimeVar(HiveConf.ConfVars.HIVE_TXN_TIMEOUT, 1, TimeUnit.SECONDS);
    //hiveConf.setBoolVar(HiveConf.ConfVars.HIVETESTMODEFAILHEARTBEATER, true);
    runStatementOnDriver("start transaction");
    runStatementOnDriver("select count(*) from " + Table.ACIDTBL + " where a = 17");
    pause(750);
    TxnStore txnHandler = TxnUtils.getTxnStore(hiveConf);
    //since there is txn open, we are heartbeating the txn not individual locks
    GetOpenTxnsInfoResponse txnsInfoResponse = txnHandler.getOpenTxnsInfo();
    Assert.assertEquals(2, txnsInfoResponse.getOpen_txns().size());
    TxnInfo txnInfo = null;
    for (TxnInfo ti : txnsInfoResponse.getOpen_txns()) {
        if (ti.getState() == TxnState.OPEN) {
            txnInfo = ti;
            break;
        }
    }
    Assert.assertNotNull(txnInfo);
    Assert.assertEquals(2, txnInfo.getId());
    Assert.assertEquals(TxnState.OPEN, txnInfo.getState());
    String s = TxnDbUtil.queryToString("select TXN_STARTED, TXN_LAST_HEARTBEAT from TXNS where TXN_ID = " + txnInfo.getId(), false);
    String[] vals = s.split("\\s+");
    Assert.assertEquals("Didn't get expected timestamps", 2, vals.length);
    long lastHeartbeat = Long.parseLong(vals[1]);
    //these 2 values are equal when TXN entry is made.  Should never be equal after 1st heartbeat, which we
    //expect to have happened by now since HIVE_TXN_TIMEOUT=1sec
    Assert.assertNotEquals("Didn't see heartbeat happen", Long.parseLong(vals[0]), lastHeartbeat);
    ShowLocksResponse slr = txnHandler.showLocks(new ShowLocksRequest());
    TestDbTxnManager2.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "default", Table.ACIDTBL.name, null, slr.getLocks());
    pause(750);
    TestTxnCommands2.runHouseKeeperService(houseKeeperService, hiveConf);
    pause(750);
    slr = txnHandler.showLocks(new ShowLocksRequest());
    Assert.assertEquals("Unexpected lock count: " + slr, 1, slr.getLocks().size());
    TestDbTxnManager2.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "default", Table.ACIDTBL.name, null, slr.getLocks());
    pause(750);
    TestTxnCommands2.runHouseKeeperService(houseKeeperService, hiveConf);
    slr = txnHandler.showLocks(new ShowLocksRequest());
    Assert.assertEquals("Unexpected lock count: " + slr, 1, slr.getLocks().size());
    TestDbTxnManager2.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "default", Table.ACIDTBL.name, null, slr.getLocks());
    //should've done several heartbeats
    s = TxnDbUtil.queryToString("select TXN_STARTED, TXN_LAST_HEARTBEAT from TXNS where TXN_ID = " + txnInfo.getId(), false);
    vals = s.split("\\s+");
    Assert.assertEquals("Didn't get expected timestamps", 2, vals.length);
    Assert.assertTrue("Heartbeat didn't progress: (old,new) (" + lastHeartbeat + "," + vals[1] + ")", lastHeartbeat < Long.parseLong(vals[1]));
    runStatementOnDriver("rollback");
    slr = txnHandler.showLocks(new ShowLocksRequest());
    Assert.assertEquals("Unexpected lock count", 0, slr.getLocks().size());
}
Also used : ShowLocksRequest(org.apache.hadoop.hive.metastore.api.ShowLocksRequest) CommandProcessorResponse(org.apache.hadoop.hive.ql.processors.CommandProcessorResponse) TxnInfo(org.apache.hadoop.hive.metastore.api.TxnInfo) AcidHouseKeeperService(org.apache.hadoop.hive.ql.txn.AcidHouseKeeperService) TxnStore(org.apache.hadoop.hive.metastore.txn.TxnStore) ShowLocksResponse(org.apache.hadoop.hive.metastore.api.ShowLocksResponse) GetOpenTxnsInfoResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse) Test(org.junit.Test)

Example 3 with TxnInfo

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

the class TxnUtils method createValidCompactTxnList.

/**
   * Transform a {@link org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse} to a
   * {@link org.apache.hadoop.hive.common.ValidTxnList}.  This assumes that the caller intends to
   * compact the files, and thus treats only open transactions as invalid.  Additionally any
   * txnId > highestOpenTxnId is also invalid.  This is to avoid creating something like
   * delta_17_120 where txnId 80, for example, is still open.
   * @param txns txn list from the metastore
   * @return a valid txn list.
   */
public static ValidTxnList createValidCompactTxnList(GetOpenTxnsInfoResponse txns) {
    long highWater = txns.getTxn_high_water_mark();
    long minOpenTxn = Long.MAX_VALUE;
    long[] exceptions = new long[txns.getOpen_txnsSize()];
    int i = 0;
    for (TxnInfo txn : txns.getOpen_txns()) {
        if (txn.getState() == TxnState.OPEN) {
            minOpenTxn = Math.min(minOpenTxn, txn.getId());
        } else {
            //only need aborted since we don't consider anything above minOpenTxn
            exceptions[i++] = txn.getId();
        }
    }
    if (i < exceptions.length) {
        exceptions = Arrays.copyOf(exceptions, i);
    }
    highWater = minOpenTxn == Long.MAX_VALUE ? highWater : minOpenTxn - 1;
    return new ValidCompactorTxnList(exceptions, highWater);
}
Also used : TxnInfo(org.apache.hadoop.hive.metastore.api.TxnInfo) ValidCompactorTxnList(org.apache.hadoop.hive.common.ValidCompactorTxnList)

Example 4 with TxnInfo

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

the class DDLTask method showTxns.

private int showTxns(Hive db, ShowTxnsDesc desc) throws HiveException {
    // Call the metastore to get the currently queued and running compactions.
    GetOpenTxnsInfoResponse rsp = db.showTransactions();
    // Write the results into the file
    DataOutputStream os = getOutputStream(desc.getResFile());
    try {
        // Write a header
        os.writeBytes("Transaction ID");
        os.write(separator);
        os.writeBytes("Transaction State");
        os.write(separator);
        os.writeBytes("Started Time");
        os.write(separator);
        os.writeBytes("Last Heartbeat Time");
        os.write(separator);
        os.writeBytes("User");
        os.write(separator);
        os.writeBytes("Hostname");
        os.write(terminator);
        for (TxnInfo txn : rsp.getOpen_txns()) {
            os.writeBytes(Long.toString(txn.getId()));
            os.write(separator);
            os.writeBytes(txn.getState().toString());
            os.write(separator);
            os.writeBytes(Long.toString(txn.getStartedTime()));
            os.write(separator);
            os.writeBytes(Long.toString(txn.getLastHeartbeatTime()));
            os.write(separator);
            os.writeBytes(txn.getUser());
            os.write(separator);
            os.writeBytes(txn.getHostname());
            os.write(terminator);
        }
    } catch (IOException e) {
        LOG.warn("show transactions: " + stringifyException(e));
        return 1;
    } finally {
        IOUtils.closeStream(os);
    }
    return 0;
}
Also used : DataOutputStream(java.io.DataOutputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) TxnInfo(org.apache.hadoop.hive.metastore.api.TxnInfo) IOException(java.io.IOException) GetOpenTxnsInfoResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse)

Example 5 with TxnInfo

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

the class TestStreaming method testErrorHandling.

@Test
public void testErrorHandling() throws Exception {
    String agentInfo = "UT_" + Thread.currentThread().getName();
    runCmdOnDriver("create database testErrors");
    runCmdOnDriver("use testErrors");
    runCmdOnDriver("create table T(a int, b int) clustered by (b) into 2 buckets stored as orc TBLPROPERTIES ('transactional'='true')");
    HiveEndPoint endPt = new HiveEndPoint(metaStoreURI, "testErrors", "T", null);
    StreamingConnection connection = endPt.newConnection(false, agentInfo);
    DelimitedInputWriter innerWriter = new DelimitedInputWriter("a,b".split(","), ",", endPt, connection);
    FaultyWriter writer = new FaultyWriter(innerWriter);
    TransactionBatch txnBatch = connection.fetchTransactionBatch(2, writer);
    txnBatch.close();
    //this is no-op on closed batch
    txnBatch.heartbeat();
    //ditto
    txnBatch.abort();
    GetOpenTxnsInfoResponse r = msClient.showTxns();
    Assert.assertEquals("HWM didn't match", 2, r.getTxn_high_water_mark());
    List<TxnInfo> ti = r.getOpen_txns();
    Assert.assertEquals("wrong status ti(0)", TxnState.ABORTED, ti.get(0).getState());
    Assert.assertEquals("wrong status ti(1)", TxnState.ABORTED, ti.get(1).getState());
    Exception expectedEx = null;
    try {
        txnBatch.beginNextTransaction();
    } catch (IllegalStateException ex) {
        expectedEx = ex;
    }
    Assert.assertTrue("beginNextTransaction() should have failed", expectedEx != null && expectedEx.getMessage().contains("has been closed()"));
    expectedEx = null;
    try {
        txnBatch.write("name0,1,Hello streaming".getBytes());
    } catch (IllegalStateException ex) {
        expectedEx = ex;
    }
    Assert.assertTrue("write()  should have failed", expectedEx != null && expectedEx.getMessage().contains("has been closed()"));
    expectedEx = null;
    try {
        txnBatch.commit();
    } catch (IllegalStateException ex) {
        expectedEx = ex;
    }
    Assert.assertTrue("commit() should have failed", expectedEx != null && expectedEx.getMessage().contains("has been closed()"));
    txnBatch = connection.fetchTransactionBatch(2, writer);
    txnBatch.beginNextTransaction();
    txnBatch.write("name2,2,Welcome to streaming".getBytes());
    txnBatch.write("name4,2,more Streaming unlimited".getBytes());
    txnBatch.write("name5,2,even more Streaming unlimited".getBytes());
    txnBatch.commit();
    expectedEx = null;
    txnBatch.beginNextTransaction();
    writer.enableErrors();
    try {
        txnBatch.write("name6,2,Doh!".getBytes());
    } catch (StreamingIOFailure ex) {
        expectedEx = ex;
        txnBatch.getCurrentTransactionState();
        //test it doesn't throw ArrayIndexOutOfBounds...
        txnBatch.getCurrentTxnId();
    }
    Assert.assertTrue("Wrong exception: " + (expectedEx != null ? expectedEx.getMessage() : "?"), expectedEx != null && expectedEx.getMessage().contains("Simulated fault occurred"));
    expectedEx = null;
    try {
        txnBatch.commit();
    } catch (IllegalStateException ex) {
        expectedEx = ex;
    }
    Assert.assertTrue("commit() should have failed", expectedEx != null && expectedEx.getMessage().contains("has been closed()"));
    r = msClient.showTxns();
    Assert.assertEquals("HWM didn't match", 4, r.getTxn_high_water_mark());
    ti = r.getOpen_txns();
    Assert.assertEquals("wrong status ti(0)", TxnState.ABORTED, ti.get(0).getState());
    Assert.assertEquals("wrong status ti(1)", TxnState.ABORTED, ti.get(1).getState());
    //txnid 3 was committed and thus not open
    Assert.assertEquals("wrong status ti(2)", TxnState.ABORTED, ti.get(2).getState());
    writer.disableErrors();
    txnBatch = connection.fetchTransactionBatch(2, writer);
    txnBatch.beginNextTransaction();
    txnBatch.write("name2,2,Welcome to streaming".getBytes());
    writer.enableErrors();
    expectedEx = null;
    try {
        txnBatch.commit();
    } catch (StreamingIOFailure ex) {
        expectedEx = ex;
    }
    Assert.assertTrue("Wrong exception: " + (expectedEx != null ? expectedEx.getMessage() : "?"), expectedEx != null && expectedEx.getMessage().contains("Simulated fault occurred"));
    r = msClient.showTxns();
    Assert.assertEquals("HWM didn't match", 6, r.getTxn_high_water_mark());
    ti = r.getOpen_txns();
    Assert.assertEquals("wrong status ti(3)", TxnState.ABORTED, ti.get(3).getState());
    Assert.assertEquals("wrong status ti(4)", TxnState.ABORTED, ti.get(4).getState());
    txnBatch.abort();
}
Also used : TxnInfo(org.apache.hadoop.hive.metastore.api.TxnInfo) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) TxnAbortedException(org.apache.hadoop.hive.metastore.api.TxnAbortedException) CommandNeedRetryException(org.apache.hadoop.hive.ql.CommandNeedRetryException) TException(org.apache.thrift.TException) IOException(java.io.IOException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) GetOpenTxnsInfoResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse) Test(org.junit.Test)

Aggregations

TxnInfo (org.apache.hadoop.hive.metastore.api.TxnInfo)5 GetOpenTxnsInfoResponse (org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse)4 Test (org.junit.Test)3 IOException (java.io.IOException)2 DataOutputStream (java.io.DataOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 ValidCompactorTxnList (org.apache.hadoop.hive.common.ValidCompactorTxnList)1 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)1 OpenTxnRequest (org.apache.hadoop.hive.metastore.api.OpenTxnRequest)1 ShowLocksRequest (org.apache.hadoop.hive.metastore.api.ShowLocksRequest)1 ShowLocksResponse (org.apache.hadoop.hive.metastore.api.ShowLocksResponse)1 TxnAbortedException (org.apache.hadoop.hive.metastore.api.TxnAbortedException)1 TxnStore (org.apache.hadoop.hive.metastore.txn.TxnStore)1 CommandNeedRetryException (org.apache.hadoop.hive.ql.CommandNeedRetryException)1 CommandProcessorResponse (org.apache.hadoop.hive.ql.processors.CommandProcessorResponse)1 AcidHouseKeeperService (org.apache.hadoop.hive.ql.txn.AcidHouseKeeperService)1 TException (org.apache.thrift.TException)1