Search in sources :

Example 11 with ExchangeClient

use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ReferenceCountExchangeClientTest method getClient.

private ExchangeClient getClient(Invoker<?> invoker) {
    if (invoker.getUrl().getParameter(CONNECTIONS_KEY, 1) == 1) {
        return getInvokerClient(invoker);
    } else {
        ReferenceCountExchangeClient client = getReferenceClient(invoker);
        try {
            Field clientField = ReferenceCountExchangeClient.class.getDeclaredField("client");
            ReflectUtils.makeAccessible(clientField);
            return (ExchangeClient) clientField.get(client);
        } catch (Exception e) {
            e.printStackTrace();
            Assertions.fail(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient)

Example 12 with ExchangeClient

use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ReferenceCountExchangeClientTest method getInvokerClientList.

private List<ExchangeClient> getInvokerClientList(Invoker<?> invoker) {
    @SuppressWarnings("rawtypes") DubboInvoker dInvoker = (DubboInvoker) ((AsyncToSyncInvoker) invoker).getInvoker();
    try {
        Field clientField = DubboInvoker.class.getDeclaredField("clients");
        ReflectUtils.makeAccessible(clientField);
        ExchangeClient[] clients = (ExchangeClient[]) clientField.get(dInvoker);
        List<ExchangeClient> clientList = new ArrayList<ExchangeClient>(clients.length);
        for (ExchangeClient client : clients) {
            clientList.add(client);
        }
        // sorting makes it easy to compare between lists
        Collections.sort(clientList, Comparator.comparing(c -> Integer.valueOf(Objects.hashCode(c))));
        return clientList;
    } catch (Exception e) {
        e.printStackTrace();
        Assertions.fail(e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ExtensionLoader(org.apache.dubbo.common.extension.ExtensionLoader) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) SHARE_CONNECTIONS_KEY(org.apache.dubbo.rpc.protocol.dubbo.Constants.SHARE_CONNECTIONS_KEY) DubboAppender(org.apache.dubbo.common.utils.DubboAppender) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) Invoker(org.apache.dubbo.rpc.Invoker) NetUtils(org.apache.dubbo.common.utils.NetUtils) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) URL(org.apache.dubbo.common.URL) ProtocolUtils(org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) ReflectUtils(org.apache.dubbo.common.utils.ReflectUtils) Exporter(org.apache.dubbo.rpc.Exporter) CONNECTIONS_KEY(org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY) LogUtil(org.apache.dubbo.common.utils.LogUtil) Field(java.lang.reflect.Field) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) LAZY_REQUEST_WITH_WARNING_KEY(org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_REQUEST_WITH_WARNING_KEY) List(java.util.List) Assertions(org.junit.jupiter.api.Assertions) Comparator(java.util.Comparator) Collections(java.util.Collections) Field(java.lang.reflect.Field) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) ArrayList(java.util.ArrayList)

Example 13 with ExchangeClient

use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class DubboInvokerAvailableTest method test_NoInvokers.

@Test
public void test_NoInvokers() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf("dubbo://127.0.0.1:" + port + "/org.apache.dubbo.rpc.protocol.dubbo.IDemoService?connections=1");
    ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url);
    DubboInvoker<?> invoker = (DubboInvoker<?>) protocol.protocolBindingRefer(IDemoService.class, url);
    ExchangeClient[] clients = getClients(invoker);
    clients[0].close();
    Assertions.assertFalse(invoker.isAvailable());
}
Also used : ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 14 with ExchangeClient

use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class DubboInvokerAvailableTest method test_Lazy_ChannelReadOnly.

@Test
public void test_Lazy_ChannelReadOnly() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf("dubbo://127.0.0.1:" + port + "/org.apache.dubbo.rpc.protocol.dubbo.IDemoService?lazy=true&connections=1&timeout=10000");
    ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url);
    AsyncToSyncInvoker<?> invoker = (AsyncToSyncInvoker) protocol.refer(IDemoService.class, url);
    Assertions.assertTrue(invoker.isAvailable());
    ExchangeClient exchangeClient = getClients((DubboInvoker<?>) invoker.getInvoker())[0];
    Assertions.assertFalse(exchangeClient.isClosed());
    try {
        exchangeClient.setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE);
        fail();
    } catch (IllegalStateException e) {
    }
    // invoke method --> init client
    IDemoService service = (IDemoService) proxy.getProxy(invoker);
    Assertions.assertEquals("ok", service.get());
    Assertions.assertTrue(invoker.isAvailable());
    exchangeClient.setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE);
    Assertions.assertFalse(invoker.isAvailable());
}
Also used : ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 15 with ExchangeClient

use of org.apache.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class PerformanceClientFixedTest method testClient.

@Test
public void testClient() throws Exception {
    // read the parameters
    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(TIMEOUT_KEY, DEFAULT_TIMEOUT);
    // final int length = PerformanceUtils.getIntProperty("length", 1024);
    final int connectionCount = PerformanceUtils.getIntProperty(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()) {
                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(org.apache.dubbo.remoting.exchange.ExchangeClient) Random(java.util.Random) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

ExchangeClient (org.apache.dubbo.remoting.exchange.ExchangeClient)17 Test (org.junit.jupiter.api.Test)8 URL (org.apache.dubbo.common.URL)4 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3 RemotingException (org.apache.dubbo.remoting.RemotingException)3 RpcException (org.apache.dubbo.rpc.RpcException)3 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TimeoutException (org.apache.dubbo.remoting.TimeoutException)2 AppResponse (org.apache.dubbo.rpc.AppResponse)2 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)2 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)2 AsyncToSyncInvoker (org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 Map (java.util.Map)1