Search in sources :

Example 1 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class PerformanceClientCloseTest method testClient.

@Test
public void testClient() throws Throwable {
    // 读取参数
    if (PerformanceUtils.getProperty("server", null) == null) {
        logger.warn("Please set -Dserver=127.0.0.1:9911");
        return;
    }
    final String server = System.getProperty("server", "127.0.0.1:9911");
    final String transporter = PerformanceUtils.getProperty(Constants.TRANSPORTER_KEY, Constants.DEFAULT_TRANSPORTER);
    final String serialization = PerformanceUtils.getProperty(Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
    final int timeout = PerformanceUtils.getIntProperty(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
    final int concurrent = PerformanceUtils.getIntProperty("concurrent", 1);
    final int runs = PerformanceUtils.getIntProperty("runs", Integer.MAX_VALUE);
    final String onerror = PerformanceUtils.getProperty("onerror", "continue");
    final String url = "exchange://" + server + "?transporter=" + transporter + "&serialization=" + serialization + //            + "&"+Constants.CHANNEL_HANDLER_KEY+"=connection"
    "&timeout=" + timeout;
    final AtomicInteger count = new AtomicInteger();
    final AtomicInteger error = new AtomicInteger();
    for (int n = 0; n < concurrent; n++) {
        new Thread(new Runnable() {

            public void run() {
                for (int i = 0; i < runs; i++) {
                    ExchangeClient client = null;
                    try {
                        client = Exchangers.connect(url);
                        int c = count.incrementAndGet();
                        if (c % 100 == 0) {
                            System.out.println("count: " + count.get() + ", error: " + error.get());
                        }
                    } catch (Exception e) {
                        error.incrementAndGet();
                        e.printStackTrace();
                        System.out.println("count: " + count.get() + ", error: " + error.get());
                        if ("exit".equals(onerror)) {
                            System.exit(-1);
                        } else if ("break".equals(onerror)) {
                            break;
                        } else if ("sleep".equals(onerror)) {
                            try {
                                Thread.sleep(30000);
                            } catch (InterruptedException e1) {
                            }
                        }
                    } finally {
                        if (client != null) {
                            client.close();
                        }
                    }
                }
            }
        }).start();
    }
    synchronized (PerformanceServerTest.class) {
        while (true) {
            try {
                PerformanceServerTest.class.wait();
            } catch (InterruptedException e) {
            }
        }
    }
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class PerformanceClientFixedTest method testClient.

@Test
public void testClient() throws Exception {
    // 读取参数
    if (PerformanceUtils.getProperty("server", null) == null) {
        logger.warn("Please set -Dserver=127.0.0.1:9911");
        return;
    }
    final String server = System.getProperty("server", "127.0.0.1:9911");
    final String transporter = PerformanceUtils.getProperty(Constants.TRANSPORTER_KEY, Constants.DEFAULT_TRANSPORTER);
    final String serialization = PerformanceUtils.getProperty(Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
    final int timeout = PerformanceUtils.getIntProperty(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
    //final int length = PerformanceUtils.getIntProperty("length", 1024);
    final int connectionCount = PerformanceUtils.getIntProperty(Constants.CONNECTIONS_KEY, 1);
    //final int concurrent = PerformanceUtils.getIntProperty("concurrent", 100);
    //int r = PerformanceUtils.getIntProperty("runs", 10000);
    //final int runs = r > 0 ? r : Integer.MAX_VALUE;
    //final String onerror = PerformanceUtils.getProperty("onerror", "continue");
    final String url = "exchange://" + server + "?transporter=" + transporter + "&serialization=" + serialization + "&timeout=" + timeout;
    //int idx = server.indexOf(':');
    Random rd = new Random(connectionCount);
    ArrayList<ExchangeClient> arrays = new ArrayList<ExchangeClient>();
    String oneKBlock = null;
    String messageBlock = null;
    int s = 0;
    int f = 0;
    System.out.println("initialize arrays " + url);
    while (s < connectionCount) {
        ExchangeClient client = null;
        try {
            System.out.println("open connection " + s + " " + url + arrays.size());
            client = Exchangers.connect(url);
            System.out.println("run after open");
            if (client.isConnected()) {
                arrays.add(client);
                s++;
                System.out.println("open client success " + s);
            } else {
                System.out.println("open client failed, try again.");
            }
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            if (client != null && client.isConnected() == false) {
                f++;
                System.out.println("open client failed, try again " + f);
                client.close();
            }
        }
    }
    StringBuilder sb1 = new StringBuilder();
    Random rd2 = new Random();
    char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
    int size1 = numbersAndLetters.length;
    for (int j = 0; j < 1024; j++) {
        sb1.append(numbersAndLetters[rd2.nextInt(size1)]);
    }
    oneKBlock = sb1.toString();
    for (int j = 0; j < Integer.MAX_VALUE; j++) {
        try {
            String size = "10";
            int request_size = 10;
            try {
                request_size = Integer.parseInt(size);
            } catch (Throwable t) {
                request_size = 10;
            }
            if (messageBlock == null) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < request_size; i++) {
                    sb.append(oneKBlock);
                }
                messageBlock = sb.toString();
                System.out.println("set messageBlock to " + messageBlock);
            }
            int index = rd.nextInt(connectionCount);
            ExchangeClient client = arrays.get(index);
            // ExchangeClient client = arrays.get(0);
            String output = (String) client.request(messageBlock).get();
            if (output.lastIndexOf(messageBlock) < 0) {
                System.out.println("send messageBlock;get " + output);
                throw new Throwable("return results invalid");
            } else {
                if (j % 100 == 0)
                    System.out.println("OK: " + j);
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) Random(java.util.Random) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class AbstractExchangeGroup method connect.

protected Client connect(URL url) throws RemotingException {
    if (servers.containsKey(url)) {
        return null;
    }
    ExchangeClient client = clients.get(url);
    if (client == null) {
        // TODO 有并发间隙
        client = Exchangers.connect(url, dispatcher);
        clients.put(url, client);
    }
    return client;
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient)

Example 4 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ChannelWrappedInvoker method doInvoke.

@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    //拿不到client端export 的service path.约定为interface的名称.
    inv.setAttachment(Constants.PATH_KEY, getInterface().getName());
    inv.setAttachment(Constants.CALLBACK_SERVICE_KEY, serviceKey);
    ExchangeClient currentClient = new HeaderExchangeClient(new ChannelWrapper(this.channel));
    try {
        if (getUrl().getMethodParameter(invocation.getMethodName(), Constants.ASYNC_KEY, false)) {
            // 不可靠异步
            currentClient.send(inv, getUrl().getMethodParameter(invocation.getMethodName(), Constants.SENT_KEY, false));
            return new RpcResult();
        }
        int timeout = getUrl().getMethodParameter(invocation.getMethodName(), Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
        if (timeout > 0) {
            return (Result) currentClient.request(inv, timeout).get();
        } else {
            return (Result) currentClient.request(inv).get();
        }
    } catch (RpcException e) {
        throw e;
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, e.getMessage(), e);
    } catch (Throwable e) {
        // here is non-biz exception, wrap it.
        throw new RpcException(e.getMessage(), e);
    }
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) HeaderExchangeClient(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient) RpcException(com.alibaba.dubbo.rpc.RpcException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) RpcResult(com.alibaba.dubbo.rpc.RpcResult) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult) TimeoutException(com.alibaba.dubbo.remoting.TimeoutException)

Example 5 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class DubboInvokerAvilableTest method test_NoInvokers.

@Test
public void test_NoInvokers() throws Exception {
    URL url = URL.valueOf("dubbo://127.0.0.1:20883/hi?connections=1");
    ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url);
    DubboInvoker<?> invoker = (DubboInvoker<?>) protocol.refer(IDemoService.class, url);
    ExchangeClient[] clients = getClients(invoker);
    clients[0].close();
    Assert.assertEquals(false, invoker.isAvailable());
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Aggregations

ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)22 Test (org.junit.Test)6 RemotingException (com.alibaba.dubbo.remoting.RemotingException)5 RpcException (com.alibaba.dubbo.rpc.RpcException)5 URL (com.alibaba.dubbo.common.URL)3 TimeoutException (com.alibaba.dubbo.remoting.TimeoutException)3 HeaderExchangeClient (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient)3 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)3 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3 HeaderExchangeServer (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeServer)2 Result (com.alibaba.dubbo.rpc.Result)2 RpcResult (com.alibaba.dubbo.rpc.RpcResult)2 List (java.util.List)2 Random (java.util.Random)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Transporter (com.alibaba.dubbo.remoting.Transporter)1 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)1 ExchangeServer (com.alibaba.dubbo.remoting.exchange.ExchangeServer)1 ResponseFuture (com.alibaba.dubbo.remoting.exchange.ResponseFuture)1