Search in sources :

Example 1 with SofaTimeOutException

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);
}
Also used : SofaGreeterTriple(io.grpc.examples.helloworld.SofaGreeterTriple) HelloRequest(io.grpc.examples.helloworld.HelloRequest) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 2 with SofaTimeOutException

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);
        }
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with SofaTimeOutException

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);
}
Also used : InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) DeserializationException(com.alipay.remoting.exception.DeserializationException) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 4 with SofaTimeOutException

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;
}
Also used : Status(io.grpc.Status) StatusException(io.grpc.StatusException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 5 with SofaTimeOutException

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);
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ClientAsyncReceiveEvent(com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent) FaultToleranceSubscriber(com.alipay.sofa.rpc.event.FaultToleranceSubscriber) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) Test(org.junit.Test)

Aggregations

SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)14 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)10 Test (org.junit.Test)10 ServiceExceptionInvocationStat (com.alipay.sofa.rpc.client.aft.impl.ServiceExceptionInvocationStat)4 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)4 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)4 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)3 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)3 ServiceHorizontalMeasureStrategy (com.alipay.sofa.rpc.client.aft.impl.ServiceHorizontalMeasureStrategy)3 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)3 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ConnectionClosedException (com.alipay.remoting.exception.ConnectionClosedException)2 DeserializationException (com.alipay.remoting.exception.DeserializationException)2 SerializationException (com.alipay.remoting.exception.SerializationException)2 InvokeSendFailedException (com.alipay.remoting.rpc.exception.InvokeSendFailedException)2 InvokeServerBusyException (com.alipay.remoting.rpc.exception.InvokeServerBusyException)2 InvokeServerException (com.alipay.remoting.rpc.exception.InvokeServerException)2 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)2