void handleTApplicationException(Throwable cause, Optional<StreamOp> op, SocketAddress addr, ProxyClient sc) { TApplicationException ex = (TApplicationException) cause; if (ex.getType() == TApplicationException.UNKNOWN_METHOD) { // if we encountered unknown method exception on thrift server, it means this proxy // has problem. we should remove it from routing service, clean up ownerships routingService.removeHost(addr, cause); onServerLeft(addr, sc); if (op.isPresent()) { ownershipCache.removeOwnerFromStream(op.get().stream, addr, cause.getMessage()); doSend(op.get(), addr); } } else { handleException(cause, op, addr); } }
if (t instanceof TApplicationException) { TApplicationException tae = (TApplicationException)t; switch (tae.getType()) { case TApplicationException.UNSUPPORTED_CLIENT_TYPE: case TApplicationException.UNKNOWN_METHOD:
@Test public void execute_reachedMaxAttempts() throws Exception { final HelloService.Iface client = helloClient(retryAlways, 2); when(serviceHandler.hello(anyString())).thenThrow(new IllegalArgumentException()); final Throwable thrown = catchThrowable(() -> client.hello("hello")); assertThat(thrown).isInstanceOf(TApplicationException.class); assertThat(((TApplicationException) thrown).getType()).isEqualTo(TApplicationException.INTERNAL_ERROR); verify(serviceHandler, times(2)).hello("hello"); }
@Test public void propagateLastResponseWhenNextRetryIsAfterTimeout() throws Exception { final RetryStrategyWithContent<RpcResponse> strategy = (ctx, response) -> CompletableFuture.completedFuture(Backoff.fixed(10000000)); final HelloService.Iface client = helloClient(strategy, 100); when(serviceHandler.hello(anyString())).thenThrow(new IllegalArgumentException()); final Throwable thrown = catchThrowable(() -> client.hello("hello")); assertThat(thrown).isInstanceOf(TApplicationException.class); assertThat(((TApplicationException) thrown).getType()).isEqualTo(TApplicationException.INTERNAL_ERROR); verify(serviceHandler, only()).hello("hello"); }
.isInstanceOf(ExecutionException.class) .hasCauseInstanceOf(TApplicationException.class) .satisfies(cause -> assertThat(((TApplicationException) Exceptions.peel(cause)).getType()) .isEqualTo(TApplicationException.BAD_SEQUENCE_ID));
@Test public void rpcTApplicationException() throws Exception { final String request = "{\n" + " \"method\" : \"doDebug\",\n" + " \"type\" : \"EXCEPTION\",\n" + " \"seqid\" : 101,\n" + " \"args\" : {\n" + " \"type\" : 4,\n" + " \"message\" : \"bad_seq_id\"\n" + " }\n" + " }\n" + '}'; TTextProtocol prot = new TTextProtocol( new TIOStreamTransport(new ByteArrayInputStream(request.getBytes()))); final TMessage header = prot.readMessageBegin(); final TApplicationException result = TApplicationExceptions.read(prot); prot.readMessageEnd(); assertThat(header.name).isEqualTo("doDebug"); assertThat(header.type).isEqualTo(TMessageType.EXCEPTION); assertThat(header.seqid).isEqualTo(101); assertThat(result.getType()).isEqualTo(TApplicationException.BAD_SEQUENCE_ID); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); prot = new TTextProtocol(new TIOStreamTransport(outputStream)); prot.writeMessageBegin(header); new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, "bad_seq_id").write(prot); prot.writeMessageEnd(); assertThatJson(new String(outputStream.toByteArray(), StandardCharsets.UTF_8)).isEqualTo(request); }
@Override public boolean listPartitionsByExpr(String catName, String db_name, String tbl_name, byte[] expr, String default_partition_name, int max_parts, List<Partition> result) throws TException { assert result != null; PartitionsByExprRequest req = new PartitionsByExprRequest( db_name, tbl_name, ByteBuffer.wrap(expr)); if (default_partition_name != null) { req.setDefaultPartitionName(default_partition_name); } if (max_parts >= 0) { req.setMaxParts(shrinkMaxtoShort(max_parts)); } PartitionsByExprResult r; try { r = client.get_partitions_by_expr(req); } catch (TApplicationException te) { // TODO: backward compat for Hive <= 0.12. Can be removed later. if (te.getType() != TApplicationException.UNKNOWN_METHOD && te.getType() != TApplicationException.WRONG_METHOD_NAME) { throw te; } throw new IncompatibleMetastoreException( "Metastore doesn't support listPartitionsByExpr: " + te.getMessage()); } r.setPartitions(FilterUtils.filterPartitionsIfEnabled(isClientFilterEnabled, filterHook, r.getPartitions())); // TODO: in these methods, do we really need to deepcopy? deepCopyPartitions(r.getPartitions(), result); return !r.isSetHasUnknownPartitions() || r.isHasUnknownPartitions(); // Assume the worst. }
@Test public void testSync_FileService_create_exception() throws Exception { final FileService.Client client = new FileService.Client.Factory().getClient(inProto, outProto); client.send_create(BAZ); assertThat(out.length()).isGreaterThan(0); final RuntimeException exception = Exceptions.clearTrace(new RuntimeException()); final THttpService service = THttpService.of((FileService.Iface) path -> { throw exception; }, defaultSerializationFormat); invoke(service); try { client.recv_create(); fail(TApplicationException.class.getSimpleName() + " not raised."); } catch (TApplicationException e) { assertThat(e.getType()).isEqualTo(TApplicationException.INTERNAL_ERROR); assertThat(e.getMessage()).contains(exception.toString()); } }
@Test public void testAsync_FileService_create_exception() throws Exception { final FileService.Client client = new FileService.Client.Factory().getClient(inProto, outProto); client.send_create(BAZ); assertThat(out.length()).isGreaterThan(0); final RuntimeException exception = Exceptions.clearTrace(new RuntimeException()); final THttpService service = THttpService.of( (FileService.AsyncIface) (path, resultHandler) -> resultHandler.onError(exception), defaultSerializationFormat); invoke(service); try { client.recv_create(); fail(TApplicationException.class.getSimpleName() + " not raised."); } catch (TApplicationException e) { assertThat(e.getType()).isEqualTo(TApplicationException.INTERNAL_ERROR); assertThat(e.getMessage()).contains(exception.toString()); } }
he = (HectorException) original; } else if (original instanceof TApplicationException) { he = new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage()); } else if (original instanceof TTransportException) {
/** 避免{@code null}抛出异常 */ public static <T> T returnNull(RuntimeTApplicationException e){ Throwable cause = e.getCause(); if (cause instanceof TApplicationException && ((TApplicationException) cause).getType() == TApplicationException.MISSING_RESULT){ return null; } throw e; } /** 避免{@code null}抛出异常
void handleTApplicationException(Throwable cause, Optional<StreamOp> op, SocketAddress addr, ProxyClient sc) { TApplicationException ex = (TApplicationException) cause; if (ex.getType() == TApplicationException.UNKNOWN_METHOD) { // if we encountered unknown method exception on thrift server, it means this proxy // has problem. we should remove it from routing service, clean up ownerships routingService.removeHost(addr, cause); onServerLeft(addr, sc); if (op.isPresent()) { ownershipCache.removeOwnerFromStream(op.get().stream, addr, cause.getMessage()); doSend(op.get(), addr); } } else { handleException(cause, op, addr); } }
void handleTApplicationException(Throwable cause, Optional<StreamOp> op, SocketAddress addr, ProxyClient sc) { TApplicationException ex = (TApplicationException) cause; if (ex.getType() == TApplicationException.UNKNOWN_METHOD) { // if we encountered unknown method exception on thrift server, it means this proxy // has problem. we should remove it from routing service, clean up ownerships routingService.removeHost(addr, cause); onServerLeft(addr, sc); if (op.isPresent()) { ownershipCache.removeOwnerFromStream(op.get().stream, addr, cause.getMessage()); doSend(op.get(), addr); } } else { handleException(cause, op, addr); } }
@Test public void testMissingMethod() { try { getClient().missingMethod(); fail("Expected TApplicationException of type UNKNOWN_METHOD"); } catch (TApplicationException e) { assertEquals( e.getType(), TApplicationException.UNKNOWN_METHOD, "Expected TApplicationException of type UNKNOWN_METHOD"); } }
private static Exception toDriftException(Exception e) { if (e instanceof org.apache.thrift.TApplicationException) { org.apache.thrift.TApplicationException tae = (org.apache.thrift.TApplicationException) e; return new TApplicationException(tae.getType(), tae.getMessage()); } if (e instanceof org.apache.thrift.transport.TTransportException) { return new TTransportException(e); } if (e instanceof org.apache.thrift.protocol.TProtocolException) { return new TProtocolException(e); } if (e instanceof org.apache.thrift.TException) { return new TException(e); } return e; } }
private static Exception toDriftException(Exception e) { if (e instanceof org.apache.thrift.TApplicationException) { org.apache.thrift.TApplicationException tae = (org.apache.thrift.TApplicationException) e; return new TApplicationException(tae.getType(), tae.getMessage()); } if (e instanceof org.apache.thrift.transport.TTransportException) { return new TTransportException(e); } if (e instanceof org.apache.thrift.protocol.TProtocolException) { return new TProtocolException(e); } if (e instanceof org.apache.thrift.TException) { return new TException(e); } return e; } }
@Override public boolean listPartitionsByExpr(String db_name, String tbl_name, byte[] expr, String default_partition_name, short max_parts, List<Partition> result) throws TException { assert result != null; PartitionsByExprRequest req = new PartitionsByExprRequest( db_name, tbl_name, ByteBuffer.wrap(expr)); if (default_partition_name != null) { req.setDefaultPartitionName(default_partition_name); } if (max_parts >= 0) { req.setMaxParts(max_parts); } PartitionsByExprResult r = null; try { r = client.get_partitions_by_expr(req); } catch (TApplicationException te) { // TODO: backward compat for Hive <= 0.12. Can be removed later. if (te.getType() != TApplicationException.UNKNOWN_METHOD && te.getType() != TApplicationException.WRONG_METHOD_NAME) { throw te; } throw new IncompatibleMetastoreException( "Metastore doesn't support listPartitionsByExpr: " + te.getMessage()); } r.setPartitions(filterHook.filterPartitions(r.getPartitions())); // TODO: in these methods, do we really need to deepcopy? deepCopyPartitions(r.getPartitions(), result); return !r.isSetHasUnknownPartitions() || r.isHasUnknownPartitions(); // Assume the worst. }
private static int logThrift(HostAndPort address, List<LogEntry> messages, TTransportFactory framingFactory, TProtocolFactory protocolFactory) { try { TSocket socket = new TSocket(address.getHost(), address.getPort()); socket.open(); try { TProtocol tp = protocolFactory.getProtocol(framingFactory.getTransport(socket)); Client client = new Client(tp); assertEquals(client.Log(messages), ResultCode.OK); try { client.Log(ImmutableList.of(new LogEntry("exception", "test"))); fail("Expected exception"); } catch (org.apache.thrift.TApplicationException e) { assertEquals(e.getType(), org.apache.thrift.TApplicationException.UNSUPPORTED_CLIENT_TYPE); } } finally { socket.close(); } } catch (TException e) { throw new RuntimeException(e); } return 1; }
private static int logThrift(HostAndPort address, List<LogEntry> messages, TTransportFactory framingFactory, TProtocolFactory protocolFactory) { try { TSocket socket = new TSocket(address.getHost(), address.getPort()); socket.open(); try { TProtocol tp = protocolFactory.getProtocol(framingFactory.getTransport(socket)); Client client = new Client(tp); assertEquals(client.Log(messages), ResultCode.OK); try { client.Log(ImmutableList.of(new LogEntry("exception", "test"))); fail("Expected exception"); } catch (org.apache.thrift.TApplicationException e) { assertEquals(e.getType(), org.apache.thrift.TApplicationException.UNSUPPORTED_CLIENT_TYPE); } } finally { socket.close(); } } catch (TException e) { throw new RuntimeException(e); } return 1; }