Search in sources :

Example 1 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project Sentinel by alibaba.

the class SentinelSofaRpcProviderFilterTest method testInvokeSentinelWorks.

@Test
public void testInvokeSentinelWorks() {
    SentinelSofaRpcProviderFilter filter = new SentinelSofaRpcProviderFilter();
    final String applicationName = "demo-provider";
    final String interfaceResourceName = "com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService";
    final String methodResourceName = "com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService#sayHello(java.lang.String,int)";
    SofaRequest request = mock(SofaRequest.class);
    when(request.getRequestProp("app")).thenReturn(applicationName);
    when(request.getInvokeType()).thenReturn(RpcConstants.INVOKER_TYPE_SYNC);
    when(request.getInterfaceName()).thenReturn(interfaceResourceName);
    when(request.getMethodName()).thenReturn("sayHello");
    when(request.getMethodArgSigs()).thenReturn(new String[] { "java.lang.String", "int" });
    when(request.getMethodArgs()).thenReturn(new Object[] { "Sentinel", 2020 });
    FilterInvoker filterInvoker = mock(FilterInvoker.class);
    when(filterInvoker.invoke(request)).thenAnswer(new Answer<SofaResponse>() {

        @Override
        public SofaResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
            verifyInvocationStructure(applicationName, interfaceResourceName, methodResourceName);
            SofaResponse response = new SofaResponse();
            response.setAppResponse("Hello Sentinel 2020");
            return response;
        }
    });
    // Before invoke
    assertNull(ContextUtil.getContext());
    // Do invoke
    SofaResponse response = filter.invoke(filterInvoker, request);
    assertEquals("Hello Sentinel 2020", response.getAppResponse());
    verify(filterInvoker).invoke(request);
    // After invoke, make sure exit context
    assertNull(ContextUtil.getContext());
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 2 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project Sentinel by alibaba.

the class SentinelSofaRpcConsumerFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    // Now only support sync invoke.
    if (request.getInvokeType() != null && !RpcConstants.INVOKER_TYPE_SYNC.equals(request.getInvokeType())) {
        return invoker.invoke(request);
    }
    String interfaceResourceName = getInterfaceResourceName(request);
    String methodResourceName = getMethodResourceName(request);
    Entry interfaceEntry = null;
    Entry methodEntry = null;
    try {
        interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT);
        methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, getMethodArguments(request));
        SofaResponse response = invoker.invoke(request);
        traceResponseException(response, interfaceEntry, methodEntry);
        return response;
    } catch (BlockException e) {
        return SofaRpcFallbackRegistry.getConsumerFallback().handle(invoker, request, e);
    } catch (Throwable t) {
        throw traceOtherException(t, interfaceEntry, methodEntry);
    } finally {
        if (methodEntry != null) {
            methodEntry.exit(1, getMethodArguments(request));
        }
        if (interfaceEntry != null) {
            interfaceEntry.exit();
        }
    }
}
Also used : BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 3 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project Sentinel by alibaba.

the class SofaRpcFallbackRegistryTest method testCustomFallback.

@Test
public void testCustomFallback() {
    // Test invoke custom provider fallback
    SofaRpcFallbackRegistry.setProviderFallback(new SofaRpcFallback() {

        @Override
        public SofaResponse handle(FilterInvoker invoker, SofaRequest request, BlockException ex) {
            SofaResponse response = new SofaResponse();
            response.setAppResponse("test provider response");
            return response;
        }
    });
    SofaResponse providerResponse = SofaRpcFallbackRegistry.getProviderFallback().handle(null, null, null);
    assertNotNull(providerResponse);
    assertEquals("test provider response", providerResponse.getAppResponse());
    // Test invoke custom consumer fallback
    SofaRpcFallbackRegistry.setConsumerFallback(new SofaRpcFallback() {

        @Override
        public SofaResponse handle(FilterInvoker invoker, SofaRequest request, BlockException ex) {
            SofaResponse response = new SofaResponse();
            response.setAppResponse("test consumer response");
            return response;
        }
    });
    SofaResponse consumerResponse = SofaRpcFallbackRegistry.getConsumerFallback().handle(null, null, null);
    assertNotNull(consumerResponse);
    assertEquals("test consumer response", consumerResponse.getAppResponse());
}
Also used : BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 4 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.

the class SofaRpcMetricsTest method buildResponse.

private SofaResponse buildResponse() {
    SofaResponse response = new SofaResponse();
    response.setAppResponse("123");
    return response;
}
Also used : SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 5 with SofaResponse

use of com.alipay.sofa.rpc.core.response.SofaResponse 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)

Aggregations

SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)85 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)25 Test (org.junit.Test)21 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)19 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)12 Method (java.lang.reflect.Method)10 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)9 HashMap (java.util.HashMap)9 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)6 ClientEndInvokeEvent (com.alipay.sofa.rpc.event.ClientEndInvokeEvent)6 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 ClientAsyncReceiveEvent (com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent)5 ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)5 Serializer (com.alipay.sofa.rpc.codec.Serializer)4 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 FilterChain (com.alipay.sofa.rpc.filter.FilterChain)4 Hessian2Input (com.caucho.hessian.io.Hessian2Input)4 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)3 DeserializationException (com.alipay.remoting.exception.DeserializationException)3