Search in sources :

Example 1 with GetOpenTxnsInfoResponse

use of org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse 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: ", 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 2 with GetOpenTxnsInfoResponse

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

the class ShowTransactionsOperation method execute.

@Override
public int execute() throws HiveException {
    SessionState sessionState = SessionState.get();
    // Call the metastore to get the currently queued and running compactions.
    GetOpenTxnsInfoResponse rsp = context.getDb().showTransactions();
    // Write the results into the file
    try (DataOutputStream os = ShowUtils.getOutputStream(new Path(desc.getResFile()), context)) {
        if (!sessionState.isHiveServerQuery()) {
            writeHeader(os);
        }
        for (TxnInfo txn : rsp.getOpen_txns()) {
            writeRow(os, txn);
        }
    } catch (IOException e) {
        LOG.warn("show transactions: ", e);
        return 1;
    }
    return 0;
}
Also used : Path(org.apache.hadoop.fs.Path) SessionState(org.apache.hadoop.hive.ql.session.SessionState) DataOutputStream(java.io.DataOutputStream) TxnInfo(org.apache.hadoop.hive.metastore.api.TxnInfo) IOException(java.io.IOException) GetOpenTxnsInfoResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse)

Example 3 with GetOpenTxnsInfoResponse

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

the class TestTxnHandler method testValidTxnsEmpty.

@Test
public void testValidTxnsEmpty() throws Exception {
    GetOpenTxnsInfoResponse txnsInfo = txnHandler.getOpenTxnsInfo();
    assertEquals(0L, txnsInfo.getTxn_high_water_mark());
    assertTrue(txnsInfo.getOpen_txns().isEmpty());
    GetOpenTxnsResponse txns = txnHandler.getOpenTxns();
    assertEquals(0L, txns.getTxn_high_water_mark());
    assertTrue(txns.getOpen_txns().isEmpty());
}
Also used : GetOpenTxnsResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsResponse) GetOpenTxnsInfoResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse) Test(org.junit.Test)

Example 4 with GetOpenTxnsInfoResponse

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

the class TestTxnHandler method testAbortTxn.

@Test
public void testAbortTxn() throws Exception {
    OpenTxnsResponse openedTxns = txnHandler.openTxns(new OpenTxnRequest(3, "me", "localhost"));
    List<Long> txnList = openedTxns.getTxn_ids();
    long first = txnList.get(0);
    assertEquals(1L, first);
    long second = txnList.get(1);
    assertEquals(2L, second);
    txnHandler.abortTxn(new AbortTxnRequest(1));
    List<String> parts = new ArrayList<String>();
    parts.add("p=1");
    AllocateTableWriteIdsRequest rqst = new AllocateTableWriteIdsRequest("default", "T");
    rqst.setTxnIds(Collections.singletonList(3L));
    AllocateTableWriteIdsResponse writeIds = txnHandler.allocateTableWriteIds(rqst);
    long writeId = writeIds.getTxnToWriteIds().get(0).getWriteId();
    assertEquals(3, writeIds.getTxnToWriteIds().get(0).getTxnId());
    assertEquals(1, writeId);
    AddDynamicPartitions adp = new AddDynamicPartitions(3, writeId, "default", "T", parts);
    adp.setOperationType(DataOperationType.INSERT);
    txnHandler.addDynamicPartitions(adp);
    GetOpenTxnsInfoResponse txnsInfo = txnHandler.getOpenTxnsInfo();
    assertEquals(3, txnsInfo.getTxn_high_water_mark());
    assertEquals(3, txnsInfo.getOpen_txns().size());
    assertEquals(1L, txnsInfo.getOpen_txns().get(0).getId());
    assertEquals(TxnState.ABORTED, txnsInfo.getOpen_txns().get(0).getState());
    assertEquals(2L, txnsInfo.getOpen_txns().get(1).getId());
    assertEquals(TxnState.OPEN, txnsInfo.getOpen_txns().get(1).getState());
    assertEquals(3, txnsInfo.getOpen_txns().get(2).getId());
    assertEquals(TxnState.OPEN, txnsInfo.getOpen_txns().get(2).getState());
    GetOpenTxnsResponse txns = txnHandler.getOpenTxns();
    assertEquals(3, txns.getTxn_high_water_mark());
    assertEquals(3, txns.getOpen_txns().size());
    boolean[] saw = new boolean[4];
    for (int i = 0; i < saw.length; i++) saw[i] = false;
    for (Long tid : txns.getOpen_txns()) {
        saw[tid.intValue()] = true;
    }
    for (int i = 1; i < saw.length; i++) assertTrue(saw[i]);
    txnHandler.commitTxn(new CommitTxnRequest(2));
    // this succeeds as abortTxn is idempotent
    txnHandler.abortTxn(new AbortTxnRequest(1));
    boolean gotException = false;
    try {
        txnHandler.abortTxn(new AbortTxnRequest(2));
    } catch (NoSuchTxnException ex) {
        gotException = true;
        // this is the last committed, so it is still in the txns table
        Assert.assertEquals("Transaction " + JavaUtils.txnIdToString(2) + " is already committed.", ex.getMessage());
    }
    Assert.assertTrue(gotException);
    gotException = false;
    txnHandler.commitTxn(new CommitTxnRequest(3));
    try {
        txnHandler.abortTxn(new AbortTxnRequest(3));
    } catch (NoSuchTxnException ex) {
        gotException = true;
        // txn 3 is not empty txn, so we get a better msg
        Assert.assertEquals("Transaction " + JavaUtils.txnIdToString(3) + " is already committed.", ex.getMessage());
    }
    Assert.assertTrue(gotException);
    txnHandler.setOpenTxnTimeOutMillis(1);
    txnHandler.cleanEmptyAbortedAndCommittedTxns();
    txnHandler.setOpenTxnTimeOutMillis(1000);
    gotException = false;
    try {
        txnHandler.abortTxn(new AbortTxnRequest(2));
    } catch (NoSuchTxnException ex) {
        gotException = true;
        // now the second transaction is cleared and since it was empty, we do not recognize it anymore
        Assert.assertEquals("No such transaction " + JavaUtils.txnIdToString(2), ex.getMessage());
    }
    Assert.assertTrue(gotException);
    gotException = false;
    try {
        txnHandler.abortTxn(new AbortTxnRequest(4));
    } catch (NoSuchTxnException ex) {
        gotException = true;
        Assert.assertEquals("No such transaction " + JavaUtils.txnIdToString(4), ex.getMessage());
    }
    Assert.assertTrue(gotException);
}
Also used : CommitTxnRequest(org.apache.hadoop.hive.metastore.api.CommitTxnRequest) ArrayList(java.util.ArrayList) AbortTxnRequest(org.apache.hadoop.hive.metastore.api.AbortTxnRequest) AddDynamicPartitions(org.apache.hadoop.hive.metastore.api.AddDynamicPartitions) AllocateTableWriteIdsResponse(org.apache.hadoop.hive.metastore.api.AllocateTableWriteIdsResponse) NoSuchTxnException(org.apache.hadoop.hive.metastore.api.NoSuchTxnException) AllocateTableWriteIdsRequest(org.apache.hadoop.hive.metastore.api.AllocateTableWriteIdsRequest) GetOpenTxnsResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsResponse) OpenTxnRequest(org.apache.hadoop.hive.metastore.api.OpenTxnRequest) OpenTxnsResponse(org.apache.hadoop.hive.metastore.api.OpenTxnsResponse) GetOpenTxnsResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsResponse) GetOpenTxnsInfoResponse(org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse) Test(org.junit.Test)

Example 5 with GetOpenTxnsInfoResponse

use of org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse 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(1000);
        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)

Aggregations

GetOpenTxnsInfoResponse (org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse)16 Test (org.junit.Test)13 GetOpenTxnsResponse (org.apache.hadoop.hive.metastore.api.GetOpenTxnsResponse)8 TxnInfo (org.apache.hadoop.hive.metastore.api.TxnInfo)8 OpenTxnRequest (org.apache.hadoop.hive.metastore.api.OpenTxnRequest)6 IOException (java.io.IOException)4 CommitTxnRequest (org.apache.hadoop.hive.metastore.api.CommitTxnRequest)3 OpenTxnsResponse (org.apache.hadoop.hive.metastore.api.OpenTxnsResponse)3 DataOutputStream (java.io.DataOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 AbortTxnRequest (org.apache.hadoop.hive.metastore.api.AbortTxnRequest)2 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)2 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)2 TxnAbortedException (org.apache.hadoop.hive.metastore.api.TxnAbortedException)2 TxnStore (org.apache.hadoop.hive.metastore.txn.TxnStore)2 CommandProcessorException (org.apache.hadoop.hive.ql.processors.CommandProcessorException)2 TException (org.apache.thrift.TException)2 Connection (java.sql.Connection)1