Search in sources :

Example 11 with TApplicationException

use of org.apache.thrift.TApplicationException in project dubbo by alibaba.

the class ThriftCodecTest method testDecodeExceptionResponse.

@Test
public void testDecodeExceptionResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.class.getName());
    Channel channel = new MockedChannel(url);
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
    Request request = createRequest();
    DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null);
    TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId());
    TTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    TApplicationException exception = new TApplicationException();
    int messageLength, headerLength;
    // prepare
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(Demo.class.getName());
    // path
    protocol.writeString(Demo.class.getName());
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();
    protocol.writeMessageBegin(message);
    exception.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();
    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    // prepare
    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));
    Object obj = codec.decode((Channel) null, bis);
    Assertions.assertNotNull(obj);
    Assertions.assertTrue(obj instanceof Response);
    Response response = (Response) obj;
    Assertions.assertTrue(response.getResult() instanceof AppResponse);
    AppResponse result = (AppResponse) response.getResult();
    Assertions.assertTrue(result.hasException());
    Assertions.assertTrue(result.getException() instanceof RpcException);
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture) TApplicationException(org.apache.thrift.TApplicationException) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.jupiter.api.Test)

Example 12 with TApplicationException

use of org.apache.thrift.TApplicationException in project hive by apache.

the class HiveStatement method closeStatementIfNeeded.

/**
 * Closes the statement if there is one running. Do not change the the flags.
 * @throws SQLException If there is an error closing the statement
 */
private void closeStatementIfNeeded() throws SQLException {
    try {
        if (stmtHandle.isPresent()) {
            TCloseOperationReq closeReq = new TCloseOperationReq(stmtHandle.get());
            TCloseOperationResp closeResp = client.CloseOperation(closeReq);
            if (!checkInvalidOperationHandle(closeResp)) {
                Utils.verifySuccessWithInfo(closeResp.getStatus());
            }
        }
    } catch (SQLException e) {
        throw e;
    } catch (TApplicationException tae) {
        String errorMsg = "Failed to close statement";
        if (tae.getType() == TApplicationException.BAD_SEQUENCE_ID) {
            errorMsg = "Failed to close statement. Mismatch thrift sequence id. A previous call to the Thrift library" + " failed and now position within the input stream is lost. Please enable verbose error logging and" + " check the status of previous calls.";
        }
        throw new SQLException(errorMsg, "08S01", tae);
    } catch (Exception e) {
        throw new SQLException("Failed to close statement", "08S01", e);
    } finally {
        stmtHandle = Optional.empty();
    }
}
Also used : SQLException(java.sql.SQLException) TCloseOperationResp(org.apache.hive.service.rpc.thrift.TCloseOperationResp) TCloseOperationReq(org.apache.hive.service.rpc.thrift.TCloseOperationReq) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) TApplicationException(org.apache.thrift.TApplicationException) TException(org.apache.thrift.TException) TApplicationException(org.apache.thrift.TApplicationException)

Example 13 with TApplicationException

use of org.apache.thrift.TApplicationException in project hive by apache.

the class Hive method addWriteNotificationLogInBatch.

private boolean addWriteNotificationLogInBatch(Table tbl, List<WriteNotificationLogRequest> requestList) throws HiveException, MetaException, TException {
    long start = System.currentTimeMillis();
    boolean supported = true;
    WriteNotificationLogBatchRequest rqst = new WriteNotificationLogBatchRequest(tbl.getCatName(), tbl.getDbName(), tbl.getTableName(), requestList);
    try {
        get(conf).getSynchronizedMSC().addWriteNotificationLogInBatch(rqst);
    } catch (TApplicationException e) {
        int type = e.getType();
        if (type == TApplicationException.UNKNOWN_METHOD || type == TApplicationException.WRONG_METHOD_NAME) {
            // For older HMS, if the batch API is not supported, fall back to older API.
            LOG.info("addWriteNotificationLogInBatch failed with ", e);
            for (WriteNotificationLogRequest request : requestList) {
                get(conf).getSynchronizedMSC().addWriteNotificationLog(request);
            }
            supported = false;
        }
    }
    long end = System.currentTimeMillis();
    LOG.info("Time taken to add " + requestList.size() + " write notifications: " + ((end - start) / 1000F) + " seconds");
    return supported;
}
Also used : WriteNotificationLogBatchRequest(org.apache.hadoop.hive.metastore.api.WriteNotificationLogBatchRequest) WriteNotificationLogRequest(org.apache.hadoop.hive.metastore.api.WriteNotificationLogRequest) TApplicationException(org.apache.thrift.TApplicationException)

Example 14 with TApplicationException

use of org.apache.thrift.TApplicationException in project hive by apache.

the class TUGIBasedProcessor method process.

@SuppressWarnings("unchecked")
@Override
public void process(final TProtocol in, final TProtocol out) throws TException {
    setIpAddress(in);
    final TMessage msg = in.readMessageBegin();
    final ProcessFunction<Iface, ? extends TBase> fn = functions.get(msg.name);
    if (fn == null) {
        TProtocolUtil.skip(in, TType.STRUCT);
        in.readMessageEnd();
        TApplicationException x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, "Invalid method name: '" + msg.name + "'");
        out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));
        x.write(out);
        out.writeMessageEnd();
        out.getTransport().flush();
        return;
    }
    TUGIContainingTransport ugiTrans = (TUGIContainingTransport) in.getTransport();
    // Store ugi in transport if the rpc is set_ugi
    if (msg.name.equalsIgnoreCase("set_ugi")) {
        try {
            handleSetUGI(ugiTrans, (ThriftHiveMetastore.Processor.set_ugi<Iface>) fn, msg, in, out);
        } catch (TException e) {
            throw e;
        } catch (Exception e) {
            throw new TException(e.getCause());
        }
        return;
    }
    UserGroupInformation clientUgi = ugiTrans.getClientUGI();
    if (null == clientUgi) {
        // At this point, transport must contain client ugi, if it doesn't then its an old client.
        fn.process(msg.seqid, in, out, iface);
        return;
    } else {
        // Found ugi, perform doAs().
        PrivilegedExceptionAction<Void> pvea = new PrivilegedExceptionAction<Void>() {

            @Override
            public Void run() {
                try {
                    fn.process(msg.seqid, in, out, iface);
                    return null;
                } catch (TException te) {
                    throw new RuntimeException(te);
                }
            }
        };
        try {
            clientUgi.doAs(pvea);
            return;
        } catch (RuntimeException rte) {
            if (rte.getCause() instanceof TException) {
                throw (TException) rte.getCause();
            }
            throw rte;
        } catch (InterruptedException ie) {
            // unexpected!
            throw new RuntimeException(ie);
        } catch (IOException ioe) {
            // unexpected!
            throw new RuntimeException(ioe);
        } finally {
            try {
                FileSystem.closeAllForUGI(clientUgi);
            } catch (IOException e) {
                LOG.error("Could not clean up file-system handles for UGI: " + clientUgi, e);
            }
        }
    }
}
Also used : TException(org.apache.thrift.TException) TUGIContainingTransport(org.apache.hadoop.hive.metastore.security.TUGIContainingTransport) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) TProtocolException(org.apache.thrift.protocol.TProtocolException) TApplicationException(org.apache.thrift.TApplicationException) TException(org.apache.thrift.TException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TApplicationException(org.apache.thrift.TApplicationException) Iface(org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface) TMessage(org.apache.thrift.protocol.TMessage) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 15 with TApplicationException

use of org.apache.thrift.TApplicationException in project hive by apache.

the class TestForeignKey method foreignKeyAcrossCatalogs.

@Test
public void foreignKeyAcrossCatalogs() throws TException {
    Table parentTable = testTables[2];
    Table table = testTables[0];
    // Single column unnamed primary key in default catalog and database
    List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").build(metaStore.getConf());
    client.addPrimaryKey(pk);
    try {
        List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("col1").build(metaStore.getConf());
        client.addForeignKey(fk);
        Assert.fail();
    } catch (InvalidObjectException | TApplicationException e) {
    // NOP
    }
}
Also used : SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) SQLForeignKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) Table(org.apache.hadoop.hive.metastore.api.Table) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) TApplicationException(org.apache.thrift.TApplicationException) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Aggregations

TApplicationException (org.apache.thrift.TApplicationException)38 TException (org.apache.thrift.TException)16 Test (org.junit.Test)14 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)12 TMessage (org.apache.thrift.protocol.TMessage)12 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)11 Table (org.apache.hadoop.hive.metastore.api.Table)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)6 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)6 Method (java.lang.reflect.Method)5 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)5 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)5 SQLPrimaryKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder)5 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)4 SQLForeignKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder)4 TBase (org.apache.thrift.TBase)4 TFieldIdEnum (org.apache.thrift.TFieldIdEnum)4