Search in sources :

Example 16 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method testReplicationConnectWithMultipleSources.

@Test
public void testReplicationConnectWithMultipleSources() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.retriesAllDown.getKeyName(), "3");
    Set<MockConnectionConfiguration> configs = new HashSet<>();
    MockConnectionConfiguration first = new MockConnectionConfiguration("first", "replica", null, false);
    MockConnectionConfiguration second = new MockConnectionConfiguration("second", "source", null, false);
    MockConnectionConfiguration third = new MockConnectionConfiguration("third", "source", null, false);
    configs.add(first);
    configs.add(second);
    configs.add(third);
    ReplicationConnection conn2 = this.getUnreliableReplicationConnection(configs, props);
    assertFalse(conn2.isReadOnly());
    assertTrue(conn2.isSourceConnection());
    assertTrue(conn2.isHostReplica(first.getHostPortPair()));
    assertTrue(conn2.isHostSource(second.getHostPortPair()));
    assertTrue(conn2.isHostSource(third.getHostPortPair()));
}
Also used : Properties(java.util.Properties) HashSet(java.util.HashSet) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Test(org.junit.jupiter.api.Test)

Example 17 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method testBug68763.

/**
 * Tests fix for BUG#68763, ReplicationConnection.isSourceConnection() returns false always
 *
 * @throws Exception
 *             if the test fails.
 */
@Test
public void testBug68763() throws Exception {
    ReplicationConnection replConn = null;
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    replConn = (ReplicationConnection) getSourceReplicaReplicationConnection(props);
    replConn.setReadOnly(true);
    assertFalse(replConn.isSourceConnection(), "isSourceConnection() should be false for replica connection");
    replConn.setReadOnly(false);
    assertTrue(replConn.isSourceConnection(), "isSourceConnection() should be true for source connection");
}
Also used : Properties(java.util.Properties) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Test(org.junit.jupiter.api.Test)

Example 18 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method testReplicationConnectionHostManagement.

@Test
public void testReplicationConnectionHostManagement() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.retriesAllDown.getKeyName(), "3");
    props.setProperty(PropertyKey.loadBalanceHostRemovalGracePeriod.getKeyName(), "1");
    ReplicationConnection conn2 = this.getUnreliableReplicationConnection(new String[] { "first", "second", "third" }, props);
    conn2.setAutoCommit(false);
    String port = getPort(props);
    String firstHost = "first:" + port;
    String secondHost = "second:" + port;
    String thirdHost = "third:" + port;
    // "first" should be source, "second" and "third" should be replicas.
    assertTrue(conn2.isHostSource(firstHost));
    assertTrue(conn2.isHostReplica(secondHost));
    assertTrue(conn2.isHostReplica(thirdHost));
    assertFalse(conn2.isHostReplica(firstHost));
    assertFalse(conn2.isHostSource(secondHost));
    assertFalse(conn2.isHostSource(thirdHost));
    // remove "third" from replica pool:
    conn2.removeReplica(thirdHost);
    assertFalse(conn2.isHostReplica(thirdHost));
    assertFalse(conn2.isHostSource(thirdHost));
    // add "third" back into replica pool:
    conn2.addReplicaHost(thirdHost);
    assertTrue(conn2.isHostReplica(thirdHost));
    assertFalse(conn2.isHostSource(thirdHost));
    conn2.setReadOnly(false);
    // failover to "second" as source, "first"
    // can still be used:
    conn2.promoteReplicaToSource(secondHost);
    assertTrue(conn2.isHostSource(firstHost));
    assertFalse(conn2.isHostReplica(firstHost));
    assertFalse(conn2.isHostReplica(secondHost));
    assertTrue(conn2.isHostSource(secondHost));
    assertTrue(conn2.isHostReplica(thirdHost));
    assertFalse(conn2.isHostSource(thirdHost));
    conn2.removeSourceHost(firstHost);
    // "first" should no longer be used:
    conn2.promoteReplicaToSource(secondHost);
    assertFalse(conn2.isHostSource(firstHost));
    assertFalse(conn2.isHostReplica(firstHost));
    assertFalse(conn2.isHostReplica(secondHost));
    assertTrue(conn2.isHostSource(secondHost));
    assertTrue(conn2.isHostReplica(thirdHost));
    assertFalse(conn2.isHostSource(thirdHost));
    conn2.createStatement().execute("SELECT 1");
    assertFalse(conn2.isClosed());
    // check that we're waiting until transaction boundary to fail over.
    // assertTrue(conn2.hasPendingNewSource());
    assertFalse(conn2.isClosed());
    conn2.commit();
    assertFalse(conn2.isClosed());
    assertTrue(conn2.isHostSource(secondHost));
    assertFalse(conn2.isClosed());
    assertTrue(conn2.isSourceConnection());
    assertFalse(conn2.isClosed());
    // validate that queries are successful:
    conn2.createStatement().execute("SELECT 1");
    assertTrue(conn2.isHostSource(secondHost));
    // source is now offline
    UnreliableSocketFactory.downHost("second");
    try {
        Statement lstmt = conn2.createStatement();
        lstmt.execute("SELECT 1");
        fail("Should fail here due to closed connection");
    } catch (SQLException sqlEx) {
        assertEquals("08S01", sqlEx.getSQLState());
    }
    UnreliableSocketFactory.dontDownHost("second");
    try {
        // won't work now even though source is back up connection has already been implicitly closed when a new source host cannot be found:
        Statement lstmt = conn2.createStatement();
        lstmt.execute("SELECT 1");
        fail("Will fail because inability to find new source host implicitly closes connection.");
    } catch (SQLException e) {
        assertEquals("08003", e.getSQLState());
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) Properties(java.util.Properties) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Test(org.junit.jupiter.api.Test)

Example 19 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method testBug22643.

/**
 * Test of a new feature to fix BUG 22643, specifying a "validation query" in your connection pool that starts with "slash-star ping slash-star" _exactly_
 * will cause the driver to " + instead send a ping to the server (much lighter weight), and when using a ReplicationConnection or a LoadBalancedConnection,
 * will send the ping across all active connections.
 *
 * @throws Exception
 */
@Test
public void testBug22643() throws Exception {
    checkPingQuery(this.conn);
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    Connection replConnection = getSourceReplicaReplicationConnection(props);
    try {
        checkPingQuery(replConnection);
    } finally {
        if (replConnection != null) {
            replConnection.close();
        }
    }
    Connection lbConn = getLoadBalancedConnection(props);
    try {
        checkPingQuery(lbConn);
    } finally {
        if (lbConn != null) {
            lbConn.close();
        }
    }
}
Also used : ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 20 with ReplicationConnection

use of com.mysql.cj.jdbc.ha.ReplicationConnection in project aws-mysql-jdbc by awslabs.

the class ConnectionRegressionTest method testReplicationJMXInterfaces.

@Test
public void testReplicationJMXInterfaces() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.retriesAllDown.getKeyName(), "3");
    String replicationGroup = "testReplicationJMXInterfaces";
    props.setProperty(PropertyKey.replicationConnectionGroup.getKeyName(), replicationGroup);
    props.setProperty(PropertyKey.ha_enableJMX.getKeyName(), "true");
    Set<MockConnectionConfiguration> configs = new HashSet<>();
    MockConnectionConfiguration first = new MockConnectionConfiguration("first", "replica", null, false);
    MockConnectionConfiguration second = new MockConnectionConfiguration("second", "source", null, false);
    MockConnectionConfiguration third = new MockConnectionConfiguration("third", "replica", null, false);
    configs.add(first);
    configs.add(second);
    configs.add(third);
    ReplicationConnection conn2 = this.getUnreliableReplicationConnection(configs, props);
    ReplicationGroupManagerMBean bean = getReplicationMBean();
    assertEquals(1, bean.getActiveLogicalConnectionCount(replicationGroup));
    assertEquals(1, bean.getTotalLogicalConnectionCount(replicationGroup));
    assertEquals(0, bean.getReplicaPromotionCount(replicationGroup));
    assertEquals(1, bean.getActiveSourceHostCount(replicationGroup));
    assertEquals(2, bean.getActiveReplicaHostCount(replicationGroup));
    bean.removeReplicaHost(replicationGroup, first.getHostPortPair());
    assertFalse(bean.getReplicaHostsList(replicationGroup).contains(first.getHostPortPair()));
    assertEquals(1, bean.getActiveReplicaHostCount(replicationGroup));
    conn2.close();
    assertEquals(0, bean.getActiveLogicalConnectionCount(replicationGroup));
    conn2 = this.getUnreliableReplicationConnection(configs, props);
    assertEquals(1, bean.getActiveLogicalConnectionCount(replicationGroup));
    assertEquals(2, bean.getTotalLogicalConnectionCount(replicationGroup));
    assertEquals(1, bean.getActiveReplicaHostCount(replicationGroup));
    assertEquals(1, bean.getActiveSourceHostCount(replicationGroup));
    bean.promoteReplicaToSource(replicationGroup, third.getHostPortPair());
    assertEquals(2, bean.getActiveSourceHostCount(replicationGroup));
    assertEquals(0, bean.getActiveReplicaHostCount(replicationGroup));
    // confirm this works when no group filter is specified:
    bean.addReplicaHost(null, first.getHostPortPair());
    assertEquals(1, bean.getActiveReplicaHostCount(replicationGroup));
    assertEquals(2, bean.getActiveSourceHostCount(replicationGroup));
    bean.removeSourceHost(replicationGroup, second.getHostPortPair());
    assertEquals(1, bean.getActiveReplicaHostCount(replicationGroup));
    assertEquals(1, bean.getActiveSourceHostCount(replicationGroup));
    ReplicationConnection conn3 = this.getUnreliableReplicationConnection(configs, props);
    assertEquals(2, bean.getActiveLogicalConnectionCount(replicationGroup));
    assertEquals(3, bean.getTotalLogicalConnectionCount(replicationGroup));
    assertTrue(bean.getSourceHostsList(replicationGroup).contains(third.getHostPortPair()));
    assertFalse(bean.getSourceHostsList(replicationGroup).contains(first.getHostPortPair()));
    assertFalse(bean.getSourceHostsList(replicationGroup).contains(second.getHostPortPair()));
    assertFalse(bean.getReplicaHostsList(replicationGroup).contains(third.getHostPortPair()));
    assertTrue(bean.getReplicaHostsList(replicationGroup).contains(first.getHostPortPair()));
    assertFalse(bean.getReplicaHostsList(replicationGroup).contains(second.getHostPortPair()));
    assertTrue(bean.getSourceHostsList(replicationGroup).contains(conn3.getSourceConnection().getHost()));
    assertTrue(bean.getReplicaHostsList(replicationGroup).contains(conn3.getReplicaConnection().getHost()));
    assertTrue(bean.getRegisteredConnectionGroups().contains(replicationGroup));
}
Also used : Properties(java.util.Properties) ReplicationGroupManagerMBean(com.mysql.cj.jdbc.jmx.ReplicationGroupManagerMBean) HashSet(java.util.HashSet) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Test(org.junit.jupiter.api.Test)

Aggregations

ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)23 Properties (java.util.Properties)21 Test (org.junit.jupiter.api.Test)20 Connection (java.sql.Connection)9 MysqlConnection (com.mysql.cj.MysqlConnection)8 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)8 SQLException (java.sql.SQLException)8 XAConnection (javax.sql.XAConnection)8 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)6 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)6 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)6 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)6 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)6 PreparedStatement (java.sql.PreparedStatement)6 Statement (java.sql.Statement)6 PooledConnection (javax.sql.PooledConnection)6 HostInfo (com.mysql.cj.conf.HostInfo)4 HashSet (java.util.HashSet)4 ClosedOnExpiredPasswordException (com.mysql.cj.exceptions.ClosedOnExpiredPasswordException)3 PasswordExpiredException (com.mysql.cj.exceptions.PasswordExpiredException)3