Search in sources :

Example 1 with ClientSyncReceiveEvent

use of com.alipay.sofa.rpc.event.ClientSyncReceiveEvent in project sofa-rpc by sofastack.

the class FaultToleranceSubscriberTest method onProviderEvent.

@Test
public void onProviderEvent() {
    FaultToleranceConfig config = new FaultToleranceConfig();
    config.setRegulationEffective(true);
    FaultToleranceConfigManager.putAppConfig(APP_NAME1, config);
    ProviderInfo providerInfo1 = ProviderHelper.toProviderInfo("127.0.0.1");
    ProviderInfo providerInfo2 = ProviderHelper.toProviderInfo("127.0.0.2");
    ProviderInfo providerInfo3 = ProviderHelper.toProviderInfo("127.0.0.3");
    ProviderInfo providerInfo4 = ProviderHelper.toProviderInfo("127.0.0.4");
    ProviderInfo providerInfo5 = ProviderHelper.toProviderInfo("127.0.0.5");
    FaultToleranceSubscriber subscriber = new FaultToleranceSubscriber();
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo1, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo2, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo3, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo4, new SofaRequest(), new SofaResponse(), null));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 4);
    subscriber.onEvent(new ProviderInfoRemoveEvent(consumerConfig, new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.1")))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 3);
    subscriber.onEvent(new ProviderInfoUpdateEvent(consumerConfig, new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.3"), ProviderHelper.toProviderInfo("127.0.0.4"))), new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.4"), ProviderHelper.toProviderInfo("127.0.0.5")))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 2);
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo5, new SofaRequest(), new SofaResponse(), null));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 3);
    subscriber.onEvent(new ProviderInfoUpdateAllEvent(consumerConfig, Arrays.asList(new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.4"), ProviderHelper.toProviderInfo("127.0.0.5")))), Arrays.asList(new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.1"), ProviderHelper.toProviderInfo("127.0.0.4"))))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 1);
}
Also used : ProviderInfoRemoveEvent(com.alipay.sofa.rpc.event.ProviderInfoRemoveEvent) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ProviderInfoUpdateEvent(com.alipay.sofa.rpc.event.ProviderInfoUpdateEvent) ProviderInfoUpdateAllEvent(com.alipay.sofa.rpc.event.ProviderInfoUpdateAllEvent) FaultToleranceSubscriber(com.alipay.sofa.rpc.event.FaultToleranceSubscriber) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) Test(org.junit.Test)

Example 2 with ClientSyncReceiveEvent

use of com.alipay.sofa.rpc.event.ClientSyncReceiveEvent in project sofa-rpc by sofastack.

the class BoltClientTransport method syncSend.

@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext boltInvokeContext = createInvokeContext(request);
    SofaResponse response = null;
    SofaRpcException throwable = null;
    try {
        beforeSend(context, request);
        response = doInvokeSync(request, boltInvokeContext, timeout);
        return response;
    } catch (Exception e) {
        // 其它异常
        throwable = convertToRpcException(e);
        throw throwable;
    } finally {
        afterSend(context, boltInvokeContext, request);
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, response, throwable));
        }
    }
}
Also used : InvokeContext(com.alipay.remoting.InvokeContext) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) DeserializationException(com.alipay.remoting.exception.DeserializationException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) RemotingException(com.alipay.remoting.exception.RemotingException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 3 with ClientSyncReceiveEvent

use of com.alipay.sofa.rpc.event.ClientSyncReceiveEvent in project sofa-rpc by sofastack.

the class TripleClientTransport method syncSend.

@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
    SofaResponse sofaResponse = null;
    SofaRpcException throwable = null;
    try {
        RpcInternalContext context = RpcInternalContext.getContext();
        beforeSend(context, request);
        RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
        invokeContext.put(TripleContants.SOFA_REQUEST_KEY, request);
        invokeContext.put(TripleContants.SOFA_CONSUMER_CONFIG_KEY, transportConfig.getConsumerConfig());
        sofaResponse = tripleClientInvoker.invoke(request, timeout);
        return sofaResponse;
    } catch (Exception e) {
        throwable = convertToRpcException(e);
        throw throwable;
    } finally {
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, sofaResponse, throwable));
        }
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) StatusException(io.grpc.StatusException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 4 with ClientSyncReceiveEvent

use of com.alipay.sofa.rpc.event.ClientSyncReceiveEvent in project sofa-rpc by sofastack.

the class AbstractProxyClientTransport method syncSend.

@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
    RpcInternalContext context = RpcInternalContext.getContext();
    SofaResponse response = null;
    SofaRpcException throwable = null;
    try {
        beforeSend(context, request);
        if (EventBus.isEnable(ClientBeforeSendEvent.class)) {
            EventBus.post(new ClientBeforeSendEvent(request));
        }
        response = doInvokeSync(request, timeout);
        return response;
    } catch (InvocationTargetException e) {
        throwable = convertToRpcException(e);
        throw throwable;
    } catch (SofaRpcException e) {
        throwable = e;
        throw e;
    } catch (Exception e) {
        throwable = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Failed to send message to remote", e);
        throw throwable;
    } finally {
        afterSend(context, request);
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(), transportConfig.getProviderInfo(), request, response, throwable));
        }
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ClientBeforeSendEvent(com.alipay.sofa.rpc.event.ClientBeforeSendEvent) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SocketTimeoutException(java.net.SocketTimeoutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ConnectException(java.net.ConnectException)

Example 5 with ClientSyncReceiveEvent

use of com.alipay.sofa.rpc.event.ClientSyncReceiveEvent 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

ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)6 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)5 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)4 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)4 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)4 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)3 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)3 InvokeContext (com.alipay.remoting.InvokeContext)2 ConnectionClosedException (com.alipay.remoting.exception.ConnectionClosedException)2 DeserializationException (com.alipay.remoting.exception.DeserializationException)2 RemotingException (com.alipay.remoting.exception.RemotingException)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 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)2 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)2 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)2 FaultToleranceSubscriber (com.alipay.sofa.rpc.event.FaultToleranceSubscriber)2 Test (org.junit.Test)2