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()));
}
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");
}
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());
}
}
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();
}
}
}
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));
}
Aggregations