use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class TripleServerTest method testSyncTimeout.
@Test
public // 同步调用,直连
void testSyncTimeout() {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server");
int port = 50052;
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
ProviderConfig<SofaGreeterTriple.IGreeter> providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>().setApplication(applicationConfig).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setRef(new GreeterImpl()).setServer(serverConfig);
providerConfig.export();
ConsumerConfig<SofaGreeterTriple.IGreeter> consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("tri://127.0.0.1:" + port).setTimeout(1);
SofaGreeterTriple.IGreeter greeterBlockingStub = consumerConfig.refer();
HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
HelloReply reply = null;
HelloRequest request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
boolean exp = false;
try {
reply = greeterBlockingStub.sayHello(request);
} catch (SofaTimeOutException e) {
exp = true;
}
Assert.assertTrue(exp);
}
use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class SofaResponseFuture method getResponse.
/**
* get response
* <p>
* If remoting get exception, framework will wrapped it to SofaRpcException
*
* @param timeout get timeout
* @param clear true: framework will clear the ThreadLocal when return
* @return The response
* @throws SofaRpcException When throw SofaRpcException
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public static Object getResponse(long timeout, boolean clear) throws SofaRpcException, InterruptedException {
RpcInvokeContext context = RpcInvokeContext.getContext();
Future future = context.getFuture();
if (null == future) {
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, LogCodes.getLog(LogCodes.ERROR_RESPONSE_FUTURE_NULL, Thread.currentThread()));
}
try {
if (clear) {
context.setFuture(null);
}
return future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
// Future设置为超时
if (!future.isDone()) {
throw new SofaTimeOutException("Future is not done when timeout.", ex);
} else {
throw new SofaTimeOutException(ex.getMessage(), ex);
}
} catch (ExecutionException ex) {
Throwable cause = ex.getCause();
if (cause instanceof SofaRpcException) {
throw (SofaRpcException) cause;
} else {
throw new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, cause.getMessage(), cause);
}
}
}
use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class BoltClientTransportTest method testConvertToRpcException.
@Test
public void testConvertToRpcException() {
ClientTransportConfig config1 = new ClientTransportConfig();
config1.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
BoltClientTransport transport = new BoltClientTransport(config1);
Assert.assertTrue(transport.convertToRpcException(new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "")) instanceof SofaRpcException);
Assert.assertTrue(transport.convertToRpcException(new InvokeTimeoutException()) instanceof SofaTimeOutException);
Assert.assertTrue(transport.convertToRpcException(new InvokeServerBusyException()).getErrorType() == RpcErrorType.SERVER_BUSY);
Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_SERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_SERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_DESERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_DESERIALIZE);
Assert.assertTrue(transport.convertToRpcException(new ConnectionClosedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
Assert.assertTrue(transport.convertToRpcException(new InvokeSendFailedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
Assert.assertTrue(transport.convertToRpcException(new InvokeServerException()).getErrorType() == RpcErrorType.SERVER_UNDECLARED_ERROR);
Assert.assertTrue(transport.convertToRpcException(new UnsupportedOperationException()).getErrorType() == RpcErrorType.CLIENT_UNDECLARED_ERROR);
}
use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class TripleClientTransport method convertToRpcException.
/**
* 转换调用出现的异常为RPC异常
*
* @param e 异常
* @return RPC异常
*/
protected SofaRpcException convertToRpcException(Exception e) {
SofaRpcException exception;
if (e instanceof SofaRpcException) {
exception = (SofaRpcException) e;
return exception;
}
Status status = Status.fromThrowable(e);
StatusException grpcException = status.asException();
if (status.getCode() == Status.DEADLINE_EXCEEDED.getCode()) {
exception = new SofaTimeOutException(grpcException);
} else if (status.getCode() == Status.NOT_FOUND.getCode()) {
exception = new SofaRpcException(RpcErrorType.SERVER_NOT_FOUND_INVOKER, grpcException);
} else if (status.getCode() == Status.UNAVAILABLE.getCode()) {
exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, grpcException);
} else if (status.getCode() == Status.RESOURCE_EXHAUSTED.getCode()) {
exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, grpcException);
} else {
exception = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, grpcException);
}
return exception;
}
use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.
the class FaultToleranceSubscriberTest method onEvent.
@Test
public void onEvent() throws Exception {
ProviderInfo providerInfo = ProviderHelper.toProviderInfo("127.0.0.1");
FaultToleranceSubscriber subscriber = new FaultToleranceSubscriber();
subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo, new SofaRequest(), new SofaResponse(), null));
InvocationStat stat = InvocationStatFactory.getInvocationStat(consumerConfig, providerInfo);
Assert.assertNull(stat);
FaultToleranceConfig config = new FaultToleranceConfig();
config.setRegulationEffective(true);
FaultToleranceConfigManager.putAppConfig(APP_NAME1, config);
subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo, new SofaRequest(), new SofaResponse(), null));
stat = InvocationStatFactory.getInvocationStat(consumerConfig, providerInfo);
Assert.assertTrue(stat.getInvokeCount() == 1);
subscriber.onEvent(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, new SofaRequest(), new SofaResponse(), null));
Assert.assertTrue(stat.getInvokeCount() == 2);
subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo, new SofaRequest(), null, new SofaTimeOutException("")));
Assert.assertTrue(stat.getExceptionCount() == 1);
subscriber.onEvent(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, new SofaRequest(), null, new SofaTimeOutException("")));
Assert.assertTrue(stat.getExceptionCount() == 2);
Assert.assertTrue(stat.getExceptionRate() == 0.5d);
}
Aggregations