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