Search in sources :

Example 31 with AppResponse

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

the class DubboInvoker method doInvoke.

@Override
protected Result doInvoke(final Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    final String methodName = RpcUtils.getMethodName(invocation);
    inv.setAttachment(PATH_KEY, getUrl().getPath());
    inv.setAttachment(VERSION_KEY, version);
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
        int timeout = calculateTimeout(invocation, methodName);
        invocation.put(TIMEOUT_KEY, timeout);
        if (isOneway) {
            boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
            currentClient.send(inv, isSent);
            return AsyncRpcResult.newDefaultAsyncResult(invocation);
        } else {
            ExecutorService executor = getCallbackExecutor(getUrl(), inv);
            CompletableFuture<AppResponse> appResponseFuture = currentClient.request(inv, timeout, executor).thenApply(obj -> (AppResponse) obj);
            // save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
            FutureContext.getContext().setCompatibleFuture(appResponseFuture);
            AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, inv);
            result.setExecutor(executor);
            return result;
        }
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, "Failed to invoke remote method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) RemotingException(org.apache.dubbo.remoting.RemotingException) ExecutorService(java.util.concurrent.ExecutorService) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) TimeoutException(org.apache.dubbo.remoting.TimeoutException)

Example 32 with AppResponse

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

the class DubboTelnetDecodeTest method getDefaultFuture.

private static CompletableFuture<Object> getDefaultFuture() {
    CompletableFuture<Object> future = new CompletableFuture<>();
    AppResponse result = new AppResponse();
    result.setValue("default result");
    future.complete(result);
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AppResponse(org.apache.dubbo.rpc.AppResponse)

Example 33 with AppResponse

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

the class MetricsFilterTest method testInvokeMetricsService.

public void testInvokeMetricsService() {
    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[0], new Object[0]);
    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++) {
        try {
            metricsFilter.invoke(serviceInvoker, invocation);
            metricsFilter.invoke(timeoutInvoker, invocation);
        } 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 = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), "", new Class<?>[] { String.class }, new Object[] { DUBBO_GROUP });
    try {
        Thread.sleep(5000);
    } 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, Object> metricMap = new HashMap<>();
    for (int i = 0; i < metricObjectList.size(); i++) {
        MetricObject object = metricObjectList.get(i);
        String metric = object.getMetric().substring(object.getMetric().lastIndexOf(".") + 1);
        if ((double) object.getValue() > 0.0 && object.getMetricLevel().equals(MetricLevel.MAJOR))
            metricMap.put(metric, object.getValue());
    }
    Assertions.assertEquals(50.0, metricMap.get("success_bucket_count"));
    Assertions.assertEquals(50.0, metricMap.get("timeoutError_bucket_count"));
    Assertions.assertEquals(100.0, metricMap.get("bucket_count"));
    Assertions.assertEquals(100.0 / 5, metricMap.get("qps"));
    Assertions.assertEquals(50.0 / 100.0, metricMap.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) MetricObject(com.alibaba.metrics.common.MetricObject) Protocol(org.apache.dubbo.rpc.Protocol) DubboProtocol(org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol) MetricObject(com.alibaba.metrics.common.MetricObject)

Example 34 with AppResponse

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

the class MetricsFilterTest method testConsumerTimeout.

public void testConsumerTimeout() {
    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation invocation = new RpcInvocation("timeoutException", DemoService.class.getName(), "", null, null);
    RpcContext.getContext().setRemoteAddress(host, url.getPort()).setLocalAddress(host, NetUtils.getAvailablePort());
    RpcContext.getContext().setUrl(timeoutInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE).addParameter(TIMEOUT_KEY, 300));
    AppResponse response = AppResponseBuilder.create().build();
    onInvokeReturns(response);
    for (int i = 0; i < 10; i++) {
        try {
            metricsFilter.invoke(timeoutInvoker, invocation);
        } catch (RpcException e) {
        // ignore
        }
    }
    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 timeoutException()");
        }
    }, MetricLevel.NORMAL));
    long timestamp = System.currentTimeMillis() / 5000 * 5000;
    Assertions.assertEquals(10, dubboClient.getMethodCountPerCategory(0).get("timeoutError").get(timestamp));
    timestamp = timestamp / 15000 * 15000;
    Assertions.assertEquals(10, dubboMethod.getMethodCountPerCategory(0).get("timeoutError").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) RpcException(org.apache.dubbo.rpc.RpcException) DemoService(org.apache.dubbo.monitor.dubbo.service.DemoService) IMetricManager(com.alibaba.metrics.IMetricManager)

Example 35 with AppResponse

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

the class MetricsFilterTest method testProviderSuccess.

public void testProviderSuccess() throws Exception {
    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[0], new Object[0]);
    RpcContext.getContext().setRemoteAddress(host, url.getPort()).setLocalAddress(host, NetUtils.getAvailablePort());
    RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER));
    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_PROVIDER, MetricLevel.MAJOR));
    FastCompass dubboMethod = metricManager.getFastCompass(DUBBO_GROUP, new MetricName(DUBBO_PROVIDER_METHOD, new HashMap<String, String>(4) {

        {
            put(SERVICE, "org.apache.dubbo.monitor.dubbo.service.DemoService");
            put(METHOD, "void sayName()");
        }
    }, 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)

Aggregations

AppResponse (org.apache.dubbo.rpc.AppResponse)54 Test (org.junit.jupiter.api.Test)37 URL (org.apache.dubbo.common.URL)33 Invocation (org.apache.dubbo.rpc.Invocation)27 Result (org.apache.dubbo.rpc.Result)27 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)20 Invoker (org.apache.dubbo.rpc.Invoker)20 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)20 HashMap (java.util.HashMap)13 RpcException (org.apache.dubbo.rpc.RpcException)12 Method (java.lang.reflect.Method)8 Person (org.apache.dubbo.rpc.support.Person)7 TMessage (org.apache.thrift.protocol.TMessage)6 IMetricManager (com.alibaba.metrics.IMetricManager)5 DemoService (org.apache.dubbo.monitor.dubbo.service.DemoService)5 Request (org.apache.dubbo.remoting.exchange.Request)5 Response (org.apache.dubbo.remoting.exchange.Response)5 DemoService (org.apache.dubbo.rpc.support.DemoService)5 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)5 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)5