Search in sources :

Example 1 with RpcStatus

use of com.alibaba.dubbo.rpc.RpcStatus in project dubbo by alibaba.

the class ActiveLimitFilter method invoke.

public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    URL url = invoker.getUrl();
    String methodName = invocation.getMethodName();
    int max = invoker.getUrl().getMethodParameter(methodName, Constants.ACTIVES_KEY, 0);
    RpcStatus count = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName());
    if (max > 0) {
        long timeout = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.TIMEOUT_KEY, 0);
        long start = System.currentTimeMillis();
        long remain = timeout;
        int active = count.getActive();
        if (active >= max) {
            synchronized (count) {
                while ((active = count.getActive()) >= max) {
                    try {
                        count.wait(remain);
                    } catch (InterruptedException e) {
                    }
                    long elapsed = System.currentTimeMillis() - start;
                    remain = timeout - elapsed;
                    if (remain <= 0) {
                        throw new RpcException("Waiting concurrent invoke timeout in client-side for service:  " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", elapsed: " + elapsed + ", timeout: " + timeout + ". concurrent invokes: " + active + ". max concurrent invoke limit: " + max);
                    }
                }
            }
        }
    }
    try {
        long begin = System.currentTimeMillis();
        RpcStatus.beginCount(url, methodName);
        try {
            Result result = invoker.invoke(invocation);
            RpcStatus.endCount(url, methodName, System.currentTimeMillis() - begin, true);
            return result;
        } catch (RuntimeException t) {
            RpcStatus.endCount(url, methodName, System.currentTimeMillis() - begin, false);
            throw t;
        }
    } finally {
        if (max > 0) {
            synchronized (count) {
                count.notify();
            }
        }
    }
}
Also used : RpcException(com.alibaba.dubbo.rpc.RpcException) RpcStatus(com.alibaba.dubbo.rpc.RpcStatus) URL(com.alibaba.dubbo.common.URL) Result(com.alibaba.dubbo.rpc.Result)

Example 2 with RpcStatus

use of com.alibaba.dubbo.rpc.RpcStatus in project dubbo by alibaba.

the class ExecuteLimitFilter method invoke.

public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    URL url = invoker.getUrl();
    String methodName = invocation.getMethodName();
    Semaphore executesLimit = null;
    boolean acquireResult = false;
    int max = url.getMethodParameter(methodName, Constants.EXECUTES_KEY, 0);
    if (max > 0) {
        RpcStatus count = RpcStatus.getStatus(url, invocation.getMethodName());
        // if (count.getActive() >= max) {
        /**
         * http://manzhizhen.iteye.com/blog/2386408
         * use semaphore for concurrency control (to limit thread number)
         */
        executesLimit = count.getSemaphore(max);
        if (executesLimit != null && !(acquireResult = executesLimit.tryAcquire())) {
            throw new RpcException("Failed to invoke method " + invocation.getMethodName() + " in provider " + url + ", cause: The service using threads greater than <dubbo:service executes=\"" + max + "\" /> limited.");
        }
    }
    long begin = System.currentTimeMillis();
    boolean isSuccess = true;
    RpcStatus.beginCount(url, methodName);
    try {
        Result result = invoker.invoke(invocation);
        return result;
    } catch (Throwable t) {
        isSuccess = false;
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else {
            throw new RpcException("unexpected exception when ExecuteLimitFilter", t);
        }
    } finally {
        RpcStatus.endCount(url, methodName, System.currentTimeMillis() - begin, isSuccess);
        if (acquireResult) {
            executesLimit.release();
        }
    }
}
Also used : RpcException(com.alibaba.dubbo.rpc.RpcException) Semaphore(java.util.concurrent.Semaphore) RpcStatus(com.alibaba.dubbo.rpc.RpcStatus) URL(com.alibaba.dubbo.common.URL) Result(com.alibaba.dubbo.rpc.Result)

Example 3 with RpcStatus

use of com.alibaba.dubbo.rpc.RpcStatus in project dubbo by alibaba.

the class CountTelnetHandler method count.

private String count(Invoker<?> invoker, String method) {
    URL url = invoker.getUrl();
    List<List<String>> table = new ArrayList<List<String>>();
    List<String> header = new ArrayList<String>();
    header.add("method");
    header.add("total");
    header.add("failed");
    header.add("active");
    header.add("average");
    header.add("max");
    if (method == null || method.length() == 0) {
        for (Method m : invoker.getInterface().getMethods()) {
            RpcStatus count = RpcStatus.getStatus(url, m.getName());
            List<String> row = new ArrayList<String>();
            row.add(m.getName());
            row.add(String.valueOf(count.getTotal()));
            row.add(String.valueOf(count.getFailed()));
            row.add(String.valueOf(count.getActive()));
            row.add(String.valueOf(count.getSucceededAverageElapsed()) + "ms");
            row.add(String.valueOf(count.getSucceededMaxElapsed()) + "ms");
            table.add(row);
        }
    } else {
        boolean found = false;
        for (Method m : invoker.getInterface().getMethods()) {
            if (m.getName().equals(method)) {
                found = true;
                break;
            }
        }
        if (found) {
            RpcStatus count = RpcStatus.getStatus(url, method);
            List<String> row = new ArrayList<String>();
            row.add(method);
            row.add(String.valueOf(count.getTotal()));
            row.add(String.valueOf(count.getFailed()));
            row.add(String.valueOf(count.getActive()));
            row.add(String.valueOf(count.getSucceededAverageElapsed()) + "ms");
            row.add(String.valueOf(count.getSucceededMaxElapsed()) + "ms");
            table.add(row);
        } else {
            return "No such method " + method + " in class " + invoker.getInterface().getName();
        }
    }
    return TelnetUtils.toTable(header, table);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method) RpcStatus(com.alibaba.dubbo.rpc.RpcStatus) URL(com.alibaba.dubbo.common.URL)

Aggregations

URL (com.alibaba.dubbo.common.URL)3 RpcStatus (com.alibaba.dubbo.rpc.RpcStatus)3 Result (com.alibaba.dubbo.rpc.Result)2 RpcException (com.alibaba.dubbo.rpc.RpcException)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Semaphore (java.util.concurrent.Semaphore)1