Search in sources :

Example 1 with RpcException

use of org.apache.dubbo.rpc.RpcException 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 2 with RpcException

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

the class RedisMetadataReportTest method testStoreConsumer.

private void testStoreConsumer(RedisMetadataReport redisMetadataReport, String version, long moreTime) throws ClassNotFoundException {
    String interfaceName = "org.apache.dubbo.metadata.store.redis.RedisMetadata4TstService";
    String group = null;
    String application = "vic.redis.md";
    MetadataIdentifier consumerMetadataIdentifier = storeConsumer(redisMetadataReport, interfaceName, version, group, application);
    Jedis jedis = null;
    try {
        jedis = redisMetadataReport.pool.getResource();
        String keyTmp = consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY);
        String value = jedis.get(keyTmp);
        if (value == null) {
            Thread.sleep(moreTime);
            value = jedis.get(keyTmp);
        }
        Assertions.assertEquals(value, "{\"paramConsumerTest\":\"redisCm\"}");
    } catch (Throwable e) {
        throw new RpcException("Failed to put to redis . cause: " + e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.del(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
        }
        redisMetadataReport.pool.close();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) MetadataIdentifier(org.apache.dubbo.metadata.report.identifier.MetadataIdentifier) RpcException(org.apache.dubbo.rpc.RpcException)

Example 3 with RpcException

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

the class RedisMetadataReportTest method testStoreProvider.

private void testStoreProvider(RedisMetadataReport redisMetadataReport, String version, long moreTime) throws ClassNotFoundException {
    String interfaceName = "org.apache.dubbo.metadata.store.redis.RedisMetadata4TstService";
    String group = null;
    String application = "vic.redis.md";
    MetadataIdentifier providerMetadataIdentifier = storePrivider(redisMetadataReport, interfaceName, version, group, application);
    Jedis jedis = null;
    try {
        jedis = redisMetadataReport.pool.getResource();
        String keyTmp = providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY);
        String value = jedis.get(keyTmp);
        if (value == null) {
            Thread.sleep(moreTime);
            value = jedis.get(keyTmp);
        }
        Assertions.assertNotNull(value);
        Gson gson = new Gson();
        FullServiceDefinition fullServiceDefinition = gson.fromJson(value, FullServiceDefinition.class);
        Assertions.assertEquals(fullServiceDefinition.getParameters().get("paramTest"), "redisTest");
    } catch (Throwable e) {
        throw new RpcException("Failed to put to redis . cause: " + e.getMessage(), e);
    } finally {
        if (jedis != null) {
            jedis.del(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
        }
        redisMetadataReport.pool.close();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) MetadataIdentifier(org.apache.dubbo.metadata.report.identifier.MetadataIdentifier) FullServiceDefinition(org.apache.dubbo.metadata.definition.model.FullServiceDefinition) RpcException(org.apache.dubbo.rpc.RpcException) Gson(com.google.gson.Gson)

Example 4 with RpcException

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

the class ProviderAuthFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    URL url = invoker.getUrl();
    boolean shouldAuth = url.getParameter(Constants.SERVICE_AUTH, false);
    if (shouldAuth) {
        Authenticator authenticator = ExtensionLoader.getExtensionLoader(Authenticator.class).getExtension(url.getParameter(Constants.AUTHENTICATOR, Constants.DEFAULT_AUTHENTICATOR));
        try {
            authenticator.authenticate(invocation, url);
        } catch (Exception e) {
            return AsyncRpcResult.newDefaultAsyncResult(e, invocation);
        }
    }
    return invoker.invoke(invocation);
}
Also used : URL(org.apache.dubbo.common.URL) Authenticator(org.apache.dubbo.auth.spi.Authenticator) RpcException(org.apache.dubbo.rpc.RpcException)

Example 5 with RpcException

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

the class DubboProtocol method initClient.

/**
 * Create new connection
 *
 * @param url
 */
private ExchangeClient initClient(URL url) {
    // client type setting.
    String str = url.getParameter(CLIENT_KEY, url.getParameter(SERVER_KEY, DEFAULT_REMOTING_CLIENT));
    url = url.addParameter(CODEC_KEY, DubboCodec.NAME);
    // enable heartbeat by default
    url = url.addParameterIfAbsent(HEARTBEAT_KEY, String.valueOf(DEFAULT_HEARTBEAT));
    // BIO is not allowed since it has severe performance issue.
    if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
        throw new RpcException("Unsupported client type: " + str + "," + " supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
    }
    ExchangeClient client;
    try {
        // connection should be lazy
        if (url.getParameter(LAZY_CONNECT_KEY, false)) {
            client = new LazyConnectExchangeClient(url, requestHandler);
        } else {
            client = Exchangers.connect(url, requestHandler);
        }
    } catch (RemotingException e) {
        throw new RpcException("Fail to create remoting client for service(" + url + "): " + e.getMessage(), e);
    }
    return client;
}
Also used : ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) RpcException(org.apache.dubbo.rpc.RpcException) RemotingException(org.apache.dubbo.remoting.RemotingException) Transporter(org.apache.dubbo.remoting.Transporter)

Aggregations

RpcException (org.apache.dubbo.rpc.RpcException)102 URL (org.apache.dubbo.common.URL)37 Test (org.junit.jupiter.api.Test)29 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)28 Result (org.apache.dubbo.rpc.Result)21 Invocation (org.apache.dubbo.rpc.Invocation)17 ArrayList (java.util.ArrayList)15 AppResponse (org.apache.dubbo.rpc.AppResponse)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 Invoker (org.apache.dubbo.rpc.Invoker)13 IOException (java.io.IOException)9 List (java.util.List)9 Method (java.lang.reflect.Method)8 Gson (com.google.gson.Gson)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 HashMap (java.util.HashMap)5 RemotingException (org.apache.dubbo.remoting.RemotingException)5 TException (org.apache.thrift.TException)5 SocketTimeoutException (java.net.SocketTimeoutException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4