Search in sources :

Example 6 with ClientEndInvokeEvent

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

the class FutureInvokeClientHandler method doOnException.

@Override
public void doOnException(Throwable e) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, request, null, e));
        }
        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, null, e);
        }
        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, null, e));
        }
        rpcFuture.setFailure(e);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
Also used : ClientEndInvokeEvent(com.alipay.sofa.rpc.event.ClientEndInvokeEvent) FilterChain(com.alipay.sofa.rpc.filter.FilterChain) ClientAsyncReceiveEvent(com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent)

Example 7 with ClientEndInvokeEvent

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

the class BoltFutureInvokeCallback method onResponse.

@Override
public void onResponse(Object result) {
    if (rpcFuture == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    Throwable throwable = null;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, request, response, null));
        }
        pickupBaggage(response);
        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }
        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }
        Object appResp = response.getAppResponse();
        if (response.isError()) {
            // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            rpcFuture.setFailure(sofaRpcException);
        } else if (appResp instanceof Throwable) {
            // 业务层异常
            throwable = (Throwable) appResp;
            rpcFuture.setFailure(throwable);
        } else {
            rpcFuture.setSuccess(appResp);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
Also used : ClientEndInvokeEvent(com.alipay.sofa.rpc.event.ClientEndInvokeEvent) FilterChain(com.alipay.sofa.rpc.filter.FilterChain) ClientAsyncReceiveEvent(com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 8 with ClientEndInvokeEvent

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

the class BoltInvokerCallback method onException.

@Override
public void onException(Throwable e) {
    if (callback == null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, request, null, e));
        }
        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, null, e);
        }
        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, null, e));
        }
        // judge is timeout or others
        SofaRpcException sofaRpcException = null;
        if (e instanceof InvokeTimeoutException) {
            sofaRpcException = new SofaTimeOutException(e);
        } else {
            sofaRpcException = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, e.getMessage(), e);
        }
        callback.onSofaException(sofaRpcException, request.getMethodName(), request);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
Also used : ClientEndInvokeEvent(com.alipay.sofa.rpc.event.ClientEndInvokeEvent) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) FilterChain(com.alipay.sofa.rpc.filter.FilterChain) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ClientAsyncReceiveEvent(com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 9 with ClientEndInvokeEvent

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

the class BoltInvokerCallback method onResponse.

@Override
public void onResponse(Object result) {
    if (callback == null) {
        return;
    }
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    SofaResponse response = (SofaResponse) result;
    Throwable throwable = null;
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, request, response, null));
        }
        pickupBaggage(response);
        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, response, null);
        }
        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, response, null));
        }
        Object appResp = response.getAppResponse();
        if (response.isError()) {
            // rpc层异常
            SofaRpcException sofaRpcException = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
            callback.onSofaException(sofaRpcException, request.getMethodName(), request);
        } else if (appResp instanceof Throwable) {
            // 业务层异常
            throwable = (Throwable) appResp;
            callback.onAppException(throwable, request.getMethodName(), request);
        } else {
            callback.onAppResponse(appResp, request.getMethodName(), request);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
Also used : ClientEndInvokeEvent(com.alipay.sofa.rpc.event.ClientEndInvokeEvent) FilterChain(com.alipay.sofa.rpc.filter.FilterChain) ClientAsyncReceiveEvent(com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 10 with ClientEndInvokeEvent

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

the class SofaRpcMetricsTest method testMicrometerMetrics.

@Test
public void testMicrometerMetrics() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    try (SofaRpcMetrics metrics = new SofaRpcMetrics()) {
        metrics.bindTo(registry);
        Method handleEvent = EventBus.class.getDeclaredMethod("handleEvent", Subscriber.class, Event.class);
        handleEvent.setAccessible(true);
        SofaRequest request = buildRequest();
        SofaResponse response = buildResponse();
        RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE, 100).setAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE, 10).setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, 3).setAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE, 4);
        handleEvent.invoke(EventBus.class, metrics, new ClientEndInvokeEvent(request, response, null));
        handleEvent.invoke(EventBus.class, metrics, new ServerSendEvent(request, response, null));
        ServerConfig serverConfig = new ServerConfig();
        handleEvent.invoke(EventBus.class, metrics, new ServerStartedEvent(serverConfig, new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>())));
        handleEvent.invoke(EventBus.class, metrics, new ServerStoppedEvent(serverConfig));
        handleEvent.invoke(EventBus.class, metrics, new ProviderPubEvent(new ProviderConfig<>()));
        handleEvent.invoke(EventBus.class, metrics, new ConsumerSubEvent(new ConsumerConfig<>()));
        Assert.assertEquals(12, registry.getMeters().size());
    }
}
Also used : ClientEndInvokeEvent(com.alipay.sofa.rpc.event.ClientEndInvokeEvent) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ServerSendEvent(com.alipay.sofa.rpc.event.ServerSendEvent) ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Method(java.lang.reflect.Method) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ServerStartedEvent(com.alipay.sofa.rpc.event.ServerStartedEvent) ServerStoppedEvent(com.alipay.sofa.rpc.event.ServerStoppedEvent) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ProviderPubEvent(com.alipay.sofa.rpc.event.ProviderPubEvent) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Aggregations

ClientEndInvokeEvent (com.alipay.sofa.rpc.event.ClientEndInvokeEvent)10 ClientAsyncReceiveEvent (com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent)8 FilterChain (com.alipay.sofa.rpc.filter.FilterChain)8 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)6 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)6 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)1 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)1 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)1 ClientStartInvokeEvent (com.alipay.sofa.rpc.event.ClientStartInvokeEvent)1 ConsumerSubEvent (com.alipay.sofa.rpc.event.ConsumerSubEvent)1 ProviderPubEvent (com.alipay.sofa.rpc.event.ProviderPubEvent)1 ServerSendEvent (com.alipay.sofa.rpc.event.ServerSendEvent)1 ServerStartedEvent (com.alipay.sofa.rpc.event.ServerStartedEvent)1 ServerStoppedEvent (com.alipay.sofa.rpc.event.ServerStoppedEvent)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 Method (java.lang.reflect.Method)1