Search in sources :

Example 56 with URL

use of com.alibaba.dubbo.common.URL in project dubbo by alibaba.

the class ServerPeer method getChannel.

@Override
public Channel getChannel(InetSocketAddress remoteAddress) {
    String host = remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName();
    int port = remoteAddress.getPort();
    Channel channel = super.getChannel(remoteAddress);
    if (channel == null) {
        for (Map.Entry<URL, Client> entry : clients.entrySet()) {
            URL url = entry.getKey();
            if (url.getIp().equals(host) && url.getPort() == port) {
                return entry.getValue();
            }
        }
    }
    return channel;
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) Client(com.alibaba.dubbo.remoting.Client) Map(java.util.Map) URL(com.alibaba.dubbo.common.URL)

Example 57 with URL

use of com.alibaba.dubbo.common.URL in project dubbo by alibaba.

the class RpcContext method isConsumerSide.

/**
 * is consumer side.
 *
 * @return consumer side.
 */
public boolean isConsumerSide() {
    URL url = getUrl();
    if (url == null) {
        return false;
    }
    InetSocketAddress address = getRemoteAddress();
    if (address == null) {
        return false;
    }
    String host;
    if (address.getAddress() == null) {
        host = address.getHostName();
    } else {
        host = address.getAddress().getHostAddress();
    }
    return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()).equals(NetUtils.filterLocalHost(host));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) URL(com.alibaba.dubbo.common.URL)

Example 58 with URL

use of com.alibaba.dubbo.common.URL 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 59 with URL

use of com.alibaba.dubbo.common.URL 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 60 with URL

use of com.alibaba.dubbo.common.URL in project dubbo by alibaba.

the class EnumBak method testEnumCompat.

// verify compatibility when 2.0.5 invokes 2.0.3
@Ignore
@Test
public void testEnumCompat() {
    int port = 20880;
    URL consumerurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE);
    Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
    DemoService demoProxy = (DemoService) proxy.getProxy(reference);
    Type type = demoProxy.enumlength(Type.High);
    System.out.println(type);
    Assert.assertEquals(Type.High, type);
    reference.destroy();
}
Also used : URL(com.alibaba.dubbo.common.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

URL (com.alibaba.dubbo.common.URL)297 Test (org.junit.Test)169 ArrayList (java.util.ArrayList)73 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)64 HashMap (java.util.HashMap)45 Result (com.alibaba.dubbo.rpc.Result)37 Invoker (com.alibaba.dubbo.rpc.Invoker)36 Map (java.util.Map)36 List (java.util.List)30 Invocation (com.alibaba.dubbo.rpc.Invocation)29 ConcurrentMap (java.util.concurrent.ConcurrentMap)29 RegistryDirectory (com.alibaba.dubbo.registry.integration.RegistryDirectory)28 RpcException (com.alibaba.dubbo.rpc.RpcException)22 NotifyListener (com.alibaba.dubbo.registry.NotifyListener)20 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)18 RpcResult (com.alibaba.dubbo.rpc.RpcResult)17 DemoService (com.alibaba.dubbo.rpc.support.DemoService)12 Set (java.util.Set)12 ConcurrentHashSet (com.alibaba.dubbo.common.utils.ConcurrentHashSet)11 Protocol (com.alibaba.dubbo.rpc.Protocol)11