Search in sources :

Example 6 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class CacheTest method testCacheProvider.

@Test
public void testCacheProvider() throws Exception {
    CacheFactory cacheFactory = ExtensionLoader.getExtensionLoader(CacheFactory.class).getAdaptiveExtension();
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("findCache.cache", "threadlocal");
    URL url = new URL("dubbo", "127.0.0.1", 29582, "org.apache.dubbo.config.cache.CacheService", parameters);
    Invocation invocation = new RpcInvocation("findCache", CacheService.class.getName(), "", new Class[] { String.class }, new String[] { "0" }, null, null, null);
    Cache cache = cacheFactory.getCache(url, invocation);
    assertTrue(cache instanceof ThreadLocalCache);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) HashMap(java.util.HashMap) CacheFactory(org.apache.dubbo.cache.CacheFactory) URL(org.apache.dubbo.common.URL) Cache(org.apache.dubbo.cache.Cache) ThreadLocalCache(org.apache.dubbo.cache.support.threadlocal.ThreadLocalCache) ThreadLocalCache(org.apache.dubbo.cache.support.threadlocal.ThreadLocalCache) Test(org.junit.jupiter.api.Test)

Example 7 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class MetricsFilterTest method testConsumerSuccess.

public void testConsumerSuccess() throws Exception {
    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[] { Integer.class }, new Object[0]);
    RpcContext.getContext().setRemoteAddress(host, url.getPort()).setLocalAddress(host, NetUtils.getAvailablePort());
    RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE));
    AppResponse response = AppResponseBuilder.create().build();
    onInvokeReturns(response);
    for (int i = 0; i < 100; i++) {
        metricsFilter.invoke(serviceInvoker, invocation);
    }
    FastCompass dubboClient = metricManager.getFastCompass(DUBBO_GROUP, new MetricName(DUBBO_CONSUMER, MetricLevel.MAJOR));
    FastCompass dubboMethod = metricManager.getFastCompass(DUBBO_GROUP, new MetricName(DUBBO_CONSUMER_METHOD, new HashMap<String, String>(4) {

        {
            put(SERVICE, "org.apache.dubbo.monitor.dubbo.service.DemoService");
            put(METHOD, "void sayName(Integer)");
        }
    }, MetricLevel.NORMAL));
    long timestamp = System.currentTimeMillis() / 5000 * 5000;
    Assertions.assertEquals(100, dubboClient.getMethodCountPerCategory(0).get("success").get(timestamp));
    timestamp = timestamp / 15000 * 15000;
    Assertions.assertEquals(100, dubboMethod.getMethodCountPerCategory(0).get("success").get(timestamp));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MetricName(com.alibaba.metrics.MetricName) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) FastCompass(com.alibaba.metrics.FastCompass) HashMap(java.util.HashMap) AppResponse(org.apache.dubbo.rpc.AppResponse) DemoService(org.apache.dubbo.monitor.dubbo.service.DemoService) IMetricManager(com.alibaba.metrics.IMetricManager)

Example 8 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class MetricsFilterTest method testInvokeMetricsMethodService.

public void testInvokeMetricsMethodService() {
    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation sayNameInvocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[0], new Object[0]);
    Invocation echoInvocation = new RpcInvocation("echo", DemoService.class.getName(), "", new Class<?>[] { Integer.class }, new Integer[] { 1 });
    RpcContext.getContext().setRemoteAddress(host, url.getPort()).setLocalAddress(host, NetUtils.getAvailablePort());
    RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE).addParameter(TIMEOUT_KEY, 300));
    AppResponse response = AppResponseBuilder.create().build();
    onInvokeReturns(response);
    for (int i = 0; i < 50; i++) {
        metricsFilter.invoke(serviceInvoker, sayNameInvocation);
        metricsFilter.invoke(serviceInvoker, echoInvocation);
        try {
            metricsFilter.invoke(timeoutInvoker, sayNameInvocation);
        } catch (RpcException e) {
        // ignore
        }
        try {
            metricsFilter.invoke(timeoutInvoker, echoInvocation);
        } catch (RpcException e) {
        // ignore
        }
    }
    Protocol protocol = DubboProtocol.getDubboProtocol();
    URL metricUrl = URL.valueOf("dubbo://" + url.getHost() + ":" + url.getPort() + "/" + MetricsService.class.getName() + "?" + METRICS_PORT + "=" + port);
    Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, metricUrl);
    Invocation invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), "", new Class<?>[] { String.class }, new Object[] { DUBBO_GROUP });
    try {
        Thread.sleep(15000);
    } catch (Exception e) {
    // ignore
    }
    String resStr = invoker.invoke(invocation).getValue().toString();
    List<MetricObject> metricObjectList = new Gson().fromJson(resStr, new TypeToken<List<MetricObject>>() {
    }.getType());
    Map<String, Map<String, Object>> methodMetricMap = new HashMap<>();
    for (int i = 0; i < metricObjectList.size(); i++) {
        MetricObject object = metricObjectList.get(i);
        String service = object.getTags().get("service");
        String method = service + "." + object.getTags().get("method");
        String metric = object.getMetric().substring(object.getMetric().lastIndexOf(".") + 1);
        Map map = methodMetricMap.get(method);
        if (map == null) {
            map = new HashMap();
            methodMetricMap.put(method, map);
        }
        map.put(metric, object.getValue());
    }
    Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_bucket_count"));
    Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_bucket_count"));
    Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("timeoutError_bucket_count"));
    Assertions.assertEquals(50.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("timeoutError_bucket_count"));
    Assertions.assertEquals(100.0 / 15, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("qps"));
    Assertions.assertEquals(100.0 / 15, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("qps"));
    Assertions.assertEquals(50.0 / 100.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_rate"));
    Assertions.assertEquals(50.0 / 100.0, methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_rate"));
    invoker.destroy();
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MetricsService(org.apache.dubbo.monitor.MetricsService) HashMap(java.util.HashMap) DemoService(org.apache.dubbo.monitor.dubbo.service.DemoService) Gson(com.google.gson.Gson) IMetricManager(com.alibaba.metrics.IMetricManager) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) AppResponse(org.apache.dubbo.rpc.AppResponse) TypeToken(com.google.gson.reflect.TypeToken) RpcException(org.apache.dubbo.rpc.RpcException) Protocol(org.apache.dubbo.rpc.Protocol) DubboProtocol(org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol) MetricObject(com.alibaba.metrics.common.MetricObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class MonitorFilterTest method testSkipMonitorIfNotHasKey.

@Test
public void testSkipMonitorIfNotHasKey() {
    MonitorFilter monitorFilter = new MonitorFilter();
    MonitorFactory mockMonitorFactory = mock(MonitorFactory.class);
    monitorFilter.setMonitorFactory(mockMonitorFactory);
    Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), "", new Class<?>[0], new Object[0]);
    Invoker invoker = mock(Invoker.class);
    given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880?" + APPLICATION_KEY + "=abc&" + SIDE_KEY + "=" + CONSUMER_SIDE));
    monitorFilter.invoke(invoker, invocation);
    verify(mockMonitorFactory, never()).getMonitor(any(URL.class));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MonitorFactory(org.apache.dubbo.monitor.MonitorFactory) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MonitorService(org.apache.dubbo.monitor.MonitorService) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 10 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class MonitorFilterTest method testGenericFilter.

@Test
public void testGenericFilter() throws Exception {
    MonitorFilter monitorFilter = new MonitorFilter();
    monitorFilter.setMonitorFactory(monitorFactory);
    Invocation invocation = new RpcInvocation("$invoke", MonitorService.class.getName(), "", new Class<?>[] { String.class, String[].class, Object[].class }, new Object[] { "xxx", new String[] {}, new Object[] {} });
    RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
    Result result = monitorFilter.invoke(serviceInvoker, invocation);
    result.whenCompleteWithContext((r, t) -> {
        if (t == null) {
            monitorFilter.onResponse(r, serviceInvoker, invocation);
        } else {
            monitorFilter.onError(t, serviceInvoker, invocation);
        }
    });
    while (lastStatistics == null) {
        Thread.sleep(10);
    }
    Assertions.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION));
    Assertions.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE));
    Assertions.assertEquals("xxx", lastStatistics.getParameter(MonitorService.METHOD));
    Assertions.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER));
    Assertions.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress());
    Assertions.assertNull(lastStatistics.getParameter(MonitorService.CONSUMER));
    Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0));
    Assertions.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0));
    Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0));
    Assertions.assertEquals(invocation, lastInvocation);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MonitorService(org.apache.dubbo.monitor.MonitorService) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Aggregations

RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)172 Test (org.junit.jupiter.api.Test)122 URL (org.apache.dubbo.common.URL)101 Invoker (org.apache.dubbo.rpc.Invoker)65 ArrayList (java.util.ArrayList)51 Result (org.apache.dubbo.rpc.Result)38 Invocation (org.apache.dubbo.rpc.Invocation)36 RpcException (org.apache.dubbo.rpc.RpcException)26 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)23 AppResponse (org.apache.dubbo.rpc.AppResponse)20 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)20 Router (org.apache.dubbo.rpc.cluster.Router)19 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)16 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Method (java.lang.reflect.Method)9 List (java.util.List)8 Person (org.apache.dubbo.rpc.support.Person)7 Protocol (org.apache.dubbo.rpc.Protocol)6