Search in sources :

Example 11 with Client

use of com.mysql.cj.xdevapi.Client in project aws-mysql-jdbc by awslabs.

the class SessionTest method pooledSessionFailoverRandomSortAndPooling.

@Test
public void pooledSessionFailoverRandomSortAndPooling() {
    UnreliableSocketFactory.flushAllStaticData();
    final String testUriPattern = "mysqlx://%s:%s@[%s]/%s?" + PropertyKey.xdevapiConnectTimeout.getKeyName() + "=100&" + PropertyKey.socketFactory.getKeyName() + "=" + UnreliableSocketFactory.class.getName();
    String testHosts = IntStream.range(1, 6).mapToObj(i -> "host" + i).peek(h -> UnreliableSocketFactory.mapHost(h, getTestHost())).map(h -> h + ":" + getTestPort()).collect(Collectors.joining(","));
    String testUri = String.format(testUriPattern, getTestUser() == null ? "" : getTestUser(), getTestPassword() == null ? "" : getTestPassword(), testHosts, getTestDatabase());
    final ClientFactory cf = new ClientFactory();
    Client client = cf.getClient(testUri, "{\"pooling\" : {\"enabled\" : true, \"maxSize\" : 10} }");
    Set<String> downHosts = new HashSet<>();
    for (int i = 1; i <= 5; i++) {
        Session testSession = client.getSession();
        assertTrue(UnreliableSocketFactory.isConnected());
        // Pool this connection.
        testSession.close();
        String lastHost = UnreliableSocketFactory.getHostFromLastConnection();
        for (String h : downHosts) {
            assertNotEquals(h, lastHost);
        }
        downHosts.add(lastHost);
        UnreliableSocketFactory.downHost(lastHost.substring(1));
        UnreliableSocketFactory.flushConnectionAttempts();
    }
    // None of the hosts is available by now.
    assertThrows(CJCommunicationsException.class, "Unable to connect to any of the target hosts\\.", () -> {
        client.getSession();
        return null;
    });
    UnreliableSocketFactory.dontDownHost("host3");
    UnreliableSocketFactory.flushConnectionAttempts();
    Session testSession = client.getSession();
    assertTrue(UnreliableSocketFactory.isConnected());
    testSession.close();
    assertEquals(UnreliableSocketFactory.getHostConnectedStatus("host3"), UnreliableSocketFactory.getHostFromLastConnection());
    UnreliableSocketFactory.flushConnectionAttempts();
    // Pick previous connection from the pool. Doesn't count as new connections.
    Session testSession1 = client.getSession();
    assertEquals(0, UnreliableSocketFactory.getHostsFromAllConnections().size());
    // Create a new connection.
    Session testSession2 = client.getSession();
    assertTrue(UnreliableSocketFactory.isConnected());
    testSession1.close();
    testSession2.close();
    assertEquals(UnreliableSocketFactory.getHostConnectedStatus("host3"), UnreliableSocketFactory.getHostFromLastConnection());
    client.close();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) XDevAPIError(com.mysql.cj.xdevapi.XDevAPIError) RowResult(com.mysql.cj.xdevapi.RowResult) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) ServerMessages(com.mysql.cj.x.protobuf.Mysqlx.ServerMessages) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Random(java.util.Random) Collection(com.mysql.cj.xdevapi.Collection) ByteBuffer(java.nio.ByteBuffer) Constants(com.mysql.cj.Constants) Future(java.util.concurrent.Future) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) SqlStatement(com.mysql.cj.xdevapi.SqlStatement) Map(java.util.Map) ClientImpl(com.mysql.cj.xdevapi.ClientImpl) CoreSession(com.mysql.cj.CoreSession) ServerVersion(com.mysql.cj.ServerVersion) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) Warning(com.mysql.cj.xdevapi.Warning) SocketFactory(com.mysql.cj.protocol.SocketFactory) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) XdevapiSslMode(com.mysql.cj.conf.PropertyDefinitions.XdevapiSslMode) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Executors(java.util.concurrent.Executors) PropertyDefinitions(com.mysql.cj.conf.PropertyDefinitions) Test(org.junit.jupiter.api.Test) ByteOrder(java.nio.ByteOrder) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FeatureNotAvailableException(com.mysql.cj.exceptions.FeatureNotAvailableException) SqlMultiResult(com.mysql.cj.xdevapi.SqlMultiResult) InjectedSocketFactory(testsuite.InjectedSocketFactory) FindStatement(com.mysql.cj.xdevapi.FindStatement) Assertions.fail(org.junit.jupiter.api.Assertions.fail) IntStream(java.util.stream.IntStream) MysqlErrorNumbers(com.mysql.cj.exceptions.MysqlErrorNumbers) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) UnreliableSocketFactory(testsuite.UnreliableSocketFactory) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) JsonString(com.mysql.cj.xdevapi.JsonString) Schema(com.mysql.cj.xdevapi.Schema) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClientFactory(com.mysql.cj.xdevapi.ClientFactory) Client(com.mysql.cj.xdevapi.Client) ClientProperty(com.mysql.cj.xdevapi.Client.ClientProperty) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) WeakReference(java.lang.ref.WeakReference) SessionImpl(com.mysql.cj.xdevapi.SessionImpl) ExecutorService(java.util.concurrent.ExecutorService) Session(com.mysql.cj.xdevapi.Session) Properties(java.util.Properties) Frame(com.mysql.cj.x.protobuf.MysqlxNotice.Frame) Iterator(java.util.Iterator) DbDoc(com.mysql.cj.xdevapi.DbDoc) DocResult(com.mysql.cj.xdevapi.DocResult) Field(java.lang.reflect.Field) Compression(com.mysql.cj.conf.PropertyDefinitions.Compression) XProtocolError(com.mysql.cj.protocol.x.XProtocolError) SessionFactory(com.mysql.cj.xdevapi.SessionFactory) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Row(com.mysql.cj.xdevapi.Row) SqlResult(com.mysql.cj.xdevapi.SqlResult) MessageLite(com.google.protobuf.MessageLite) AfterEach(org.junit.jupiter.api.AfterEach) SelectStatement(com.mysql.cj.xdevapi.SelectStatement) PooledXProtocol(com.mysql.cj.xdevapi.ClientImpl.PooledXProtocol) PropertyKey(com.mysql.cj.conf.PropertyKey) CJPacketTooBigException(com.mysql.cj.exceptions.CJPacketTooBigException) UnreliableSocketFactory(testsuite.UnreliableSocketFactory) ClientFactory(com.mysql.cj.xdevapi.ClientFactory) JsonString(com.mysql.cj.xdevapi.JsonString) Client(com.mysql.cj.xdevapi.Client) HashSet(java.util.HashSet) CoreSession(com.mysql.cj.CoreSession) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Aggregations

Client (com.mysql.cj.xdevapi.Client)11 ClientFactory (com.mysql.cj.xdevapi.ClientFactory)11 Properties (java.util.Properties)11 CoreSession (com.mysql.cj.CoreSession)10 Session (com.mysql.cj.xdevapi.Session)10 Test (org.junit.jupiter.api.Test)10 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)7 JsonString (com.mysql.cj.xdevapi.JsonString)7 CJPacketTooBigException (com.mysql.cj.exceptions.CJPacketTooBigException)6 FeatureNotAvailableException (com.mysql.cj.exceptions.FeatureNotAvailableException)6 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)6 SessionImpl (com.mysql.cj.xdevapi.SessionImpl)6 FileNotFoundException (java.io.FileNotFoundException)6 Field (java.lang.reflect.Field)6 ExecutionException (java.util.concurrent.ExecutionException)6 PooledXProtocol (com.mysql.cj.xdevapi.ClientImpl.PooledXProtocol)5 FindStatement (com.mysql.cj.xdevapi.FindStatement)5 Row (com.mysql.cj.xdevapi.Row)5 RowResult (com.mysql.cj.xdevapi.RowResult)5 MessageLite (com.google.protobuf.MessageLite)4