Search in sources :

Example 1 with ClientTransportConfig

use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.

the class ReuseBoltClientConnectionManagerTest method buildConfig.

private ClientTransportConfig buildConfig(int port) {
    ClientTransportConfig config = new ClientTransportConfig();
    ProviderInfo providerInfo2 = new ProviderInfo().setHost("127.0.0.1").setPort(port);
    config.setProviderInfo(providerInfo2).setContainer("bolt");
    return config;
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig)

Example 2 with ClientTransportConfig

use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.

the class ReuseBoltClientConnectionManagerTest method testAll.

@Test
public void testAll() throws Exception {
    ReuseBoltClientConnectionManager manager = new ReuseBoltClientConnectionManager(false);
    Connection connection = manager.getConnection(null, null, null);
    Assert.assertNull(connection);
    connection = manager.getConnection(rpcClient, null, null);
    Assert.assertNull(connection);
    ClientTransportConfig wrongConfig = buildConfig(12224);
    connection = manager.getConnection(rpcClient, wrongConfig, null);
    Assert.assertNull(connection);
    // 连不上的端口
    Connection result = manager.getConnection(rpcClient, wrongConfig, buildUrl(wrongConfig));
    Assert.assertNull(result);
    // ok
    final ClientTransportConfig config = buildConfig(12222);
    connection = manager.getConnection(rpcClient, config, buildUrl(config));
    Assert.assertNotNull(connection);
    Assert.assertTrue(manager.urlConnectionMap.size() == 1);
    Assert.assertTrue(manager.connectionRefCounter.size() == 1);
    Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 1);
    // 同一个config去get,计数器不增加
    Connection connection1 = manager.getConnection(rpcClient, config, buildUrl(config));
    Assert.assertNotNull(connection1);
    Assert.assertTrue(manager.urlConnectionMap.size() == 1);
    Assert.assertTrue(manager.connectionRefCounter.size() == 1);
    Assert.assertTrue(connection == connection1);
    Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 1);
    Assert.assertTrue(manager.connectionRefCounter.get(connection1).get() == 1);
    // 相同地址的config去get,计数器加一
    final ClientTransportConfig config2 = buildConfig(12222);
    Connection connection2 = manager.getConnection(rpcClient, config2, buildUrl(config2));
    Assert.assertNotNull(connection2);
    Assert.assertTrue(manager.urlConnectionMap.size() == 2);
    Assert.assertTrue(manager.connectionRefCounter.size() == 1);
    Assert.assertTrue(connection1 == connection2);
    Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 2);
    Assert.assertTrue(manager.connectionRefCounter.get(connection2).get() == 2);
    // 不同地址的config去get,地址和计数器加1
    ClientTransportConfig config3 = buildConfig(12223);
    Connection connection3 = manager.getConnection(rpcClient, config3, buildUrl(config3));
    Assert.assertNotNull(connection3);
    Assert.assertFalse(connection == connection3);
    Assert.assertTrue(manager.urlConnectionMap.size() == 3);
    Assert.assertTrue(manager.connectionRefCounter.size() == 2);
    Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 2);
    Assert.assertTrue(manager.connectionRefCounter.get(connection3).get() == 1);
    // 非法关闭
    manager.closeConnection(null, null, null);
    Assert.assertTrue(manager.urlConnectionMap.size() == 3);
    manager.closeConnection(rpcClient, null, null);
    Assert.assertTrue(manager.urlConnectionMap.size() == 3);
    manager.closeConnection(rpcClient, config, null);
    Assert.assertTrue(manager.urlConnectionMap.size() == 3);
    // 正常关闭1
    manager.closeConnection(rpcClient, config, buildUrl(config));
    Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 1);
    // 重复关闭1
    manager.closeConnection(rpcClient, config, buildUrl(config));
    Assert.assertTrue(manager.urlConnectionMap.size() == 2);
    Assert.assertTrue(manager.connectionRefCounter.size() == 2);
    Assert.assertTrue(manager.connectionRefCounter.get(connection).get() == 1);
    // 正常关闭2
    manager.closeConnection(rpcClient, config2, buildUrl(config2));
    Assert.assertTrue(manager.urlConnectionMap.size() == 1);
    Assert.assertTrue(manager.connectionRefCounter.size() == 1);
    // 正常关闭3
    manager.closeConnection(rpcClient, config3, buildUrl(config3));
    Assert.assertTrue(manager.urlConnectionMap.size() == 0);
    Assert.assertTrue(manager.connectionRefCounter.size() == 0);
    // 检查泄漏
    manager.checkLeak();
    Assert.assertTrue(CommonUtils.isEmpty(manager.urlConnectionMap));
    Assert.assertTrue(CommonUtils.isEmpty(manager.connectionRefCounter));
}
Also used : Connection(com.alipay.remoting.Connection) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 3 with ClientTransportConfig

use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.

the class ReuseBoltClientConnectionManagerTest method testConcurrentCreate.

@Test
public void testConcurrentCreate() throws Exception {
    final ReuseBoltClientConnectionManager manager = new ReuseBoltClientConnectionManager(false);
    final ClientTransportConfig config = buildConfig(12222);
    // 并发创建
    final CountDownLatch latch = new CountDownLatch(5);
    List<Thread> threads = new ArrayList<Thread>(5);
    final Url url = buildUrl(config);
    for (int i = 0; i < 5; i++) {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Connection innerConnection = manager.getConnection(rpcClient, config, url);
                    System.out.println("url=" + url + ",connection=" + innerConnection);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    latch.countDown();
                }
            }
        }, "thread" + i);
        threads.add(thread);
    }
    for (Thread thread : threads) {
        thread.start();
    }
    latch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(1, manager.urlConnectionMap.size());
    Assert.assertEquals(1, manager.connectionRefCounter.size());
    Connection connection = manager.getConnection(rpcClient, config, url);
    Assert.assertNotNull(connection);
    final AtomicInteger atomicInteger = manager.connectionRefCounter.get(connection);
    Assert.assertEquals(1, atomicInteger.get());
    // 检查泄漏
    manager.checkLeak();
    Assert.assertTrue(CommonUtils.isEmpty(manager.urlConnectionMap));
    Assert.assertTrue(CommonUtils.isEmpty(manager.connectionRefCounter));
}
Also used : ArrayList(java.util.ArrayList) Connection(com.alipay.remoting.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Url(com.alipay.remoting.Url) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 4 with ClientTransportConfig

use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.

the class BoltClientTransportTest method testConvertToRpcException.

@Test
public void testConvertToRpcException() {
    ClientTransportConfig config1 = new ClientTransportConfig();
    config1.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
    BoltClientTransport transport = new BoltClientTransport(config1);
    Assert.assertTrue(transport.convertToRpcException(new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "")) instanceof SofaRpcException);
    Assert.assertTrue(transport.convertToRpcException(new InvokeTimeoutException()) instanceof SofaTimeOutException);
    Assert.assertTrue(transport.convertToRpcException(new InvokeServerBusyException()).getErrorType() == RpcErrorType.SERVER_BUSY);
    Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_SERIALIZE);
    Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_SERIALIZE);
    Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_DESERIALIZE);
    Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_DESERIALIZE);
    Assert.assertTrue(transport.convertToRpcException(new ConnectionClosedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
    Assert.assertTrue(transport.convertToRpcException(new InvokeSendFailedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
    Assert.assertTrue(transport.convertToRpcException(new InvokeServerException()).getErrorType() == RpcErrorType.SERVER_UNDECLARED_ERROR);
    Assert.assertTrue(transport.convertToRpcException(new UnsupportedOperationException()).getErrorType() == RpcErrorType.CLIENT_UNDECLARED_ERROR);
}
Also used : InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) DeserializationException(com.alipay.remoting.exception.DeserializationException) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 5 with ClientTransportConfig

use of com.alipay.sofa.rpc.transport.ClientTransportConfig in project sofa-rpc by sofastack.

the class AllConnectConnectionHolder method initClientRunnable.

/**
 * 线程池建立长连接
 */
protected void initClientRunnable(ThreadPoolExecutor initPool, final CountDownLatch latch, final ProviderInfo providerInfo) {
    final ClientTransportConfig config = providerToClientConfig(providerInfo);
    initPool.execute(new Runnable() {

        @Override
        public void run() {
            ClientTransport transport = ClientTransportFactory.getClientTransport(config);
            if (consumerConfig.isLazy()) {
                uninitializedConnections.put(providerInfo, transport);
                latch.countDown();
            } else {
                try {
                    initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport);
                } finally {
                    // 连上或者抛异常
                    latch.countDown();
                }
            }
        }
    });
}
Also used : ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) ClientTransport(com.alipay.sofa.rpc.transport.ClientTransport)

Aggregations

ClientTransportConfig (com.alipay.sofa.rpc.transport.ClientTransportConfig)10 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)6 Test (org.junit.Test)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)4 ClientTransport (com.alipay.sofa.rpc.transport.ClientTransport)3 Connection (com.alipay.remoting.Connection)2 ConnectionClosedException (com.alipay.remoting.exception.ConnectionClosedException)2 DeserializationException (com.alipay.remoting.exception.DeserializationException)2 SerializationException (com.alipay.remoting.exception.SerializationException)2 InvokeSendFailedException (com.alipay.remoting.rpc.exception.InvokeSendFailedException)2 InvokeServerBusyException (com.alipay.remoting.rpc.exception.InvokeServerBusyException)2 InvokeServerException (com.alipay.remoting.rpc.exception.InvokeServerException)2 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Url (com.alipay.remoting.Url)1 NamedThreadFactory (com.alipay.sofa.rpc.common.struct.NamedThreadFactory)1 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1