Search in sources :

Example 1 with JdbcThinTcpIo

use of org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo in project ignite by apache.

the class JdbcThinTcpIoTest method createTcpIo.

/**
 * Create JdbcThinTcpIo instance.
 *
 * @param addrs IP-addresses.
 * @param port Server socket port.
 * @return JdbcThinTcpIo instance.
 * @throws SQLException On connection error or reject.
 */
private JdbcThinTcpIo createTcpIo(String[] addrs, int port) throws SQLException {
    ConnectionPropertiesImpl connProps = new ConnectionPropertiesImpl();
    connProps.setAddresses(new HostAndPortRange[] { new HostAndPortRange("test.domain.name", port, port) });
    return new JdbcThinTcpIo(connProps) {

        @Override
        protected InetAddress[] getAllAddressesByHost(String host) throws UnknownHostException {
            InetAddress[] addresses = new InetAddress[addrs.length];
            for (int i = 0; i < addrs.length; i++) addresses[i] = InetAddress.getByName(addrs[i]);
            return addresses;
        }

        @Override
        public void handshake(ClientListenerProtocolVersion ver) {
        // Skip handshake.
        }
    };
}
Also used : HostAndPortRange(org.apache.ignite.internal.util.HostAndPortRange) ClientListenerProtocolVersion(org.apache.ignite.internal.processors.odbc.ClientListenerProtocolVersion) ConnectionPropertiesImpl(org.apache.ignite.internal.jdbc.thin.ConnectionPropertiesImpl) JdbcThinTcpIo(org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo) InetAddress(java.net.InetAddress)

Example 2 with JdbcThinTcpIo

use of org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo in project ignite by apache.

the class JdbcThinPartitionAwarenessReconnectionAndFailoverSelfTest method testFailoverLimit.

/**
 * Check that there won't be more than 5 retry attempts.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings("ThrowableNotThrown")
@Test
public void testFailoverLimit() throws Exception {
    startGrid(3);
    startGrid(4);
    startGrid(5);
    try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800..10805?partitionAwareness=true")) {
        Map<UUID, JdbcThinTcpIo> ios = GridTestUtils.getFieldValue(conn, "ios");
        assertConnectionsCount(ios, 6);
        stopAllGrids();
        logHnd.records.clear();
        GridTestUtils.assertThrows(null, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                conn.createStatement().execute("select 1");
                return null;
            }
        }, SQLException.class, "Failed to communicate with Ignite cluster.");
        assertEquals("Unexpected connections count.", 1, ios.keySet().size());
    }
    assertEquals("Unexpected log records count.", 5, logHnd.records.size());
    for (LogRecord record : logHnd.records) {
        assertEquals("Unexpected log record text.", "Exception during sending an sql request.", record.getMessage());
        assertEquals("Unexpected log level", Level.FINE, record.getLevel());
    }
    startGridsMultiThreaded(INITIAL_NODES_CNT);
}
Also used : LogRecord(java.util.logging.LogRecord) Connection(java.sql.Connection) JdbcThinConnection(org.apache.ignite.internal.jdbc.thin.JdbcThinConnection) JdbcThinTcpIo(org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo) UUID(java.util.UUID) SQLException(java.sql.SQLException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) Test(org.junit.Test)

Example 3 with JdbcThinTcpIo

use of org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo in project ignite by apache.

the class JdbcThinPartitionAwarenessReconnectionAndFailoverSelfTest method testReconnectionDelaySelectiveIncreasing.

/**
 * Check that reconnection thread selectively increases delay between unsuccessful connection attempts:
 * <ol>
 * <li>Create {@code JdbcThinConnection} with two valid inet addresses.</li>
 * <li>Stop one node and invalidate corresponding connection. Ensure that only one connection left.</li>
 * <li>Wait for specific amount of time. The reconnection logic suppose to increase delays between reconnection
 * attempts. The basic idea is very simple: delay is doubled on evey connection failure until connection succeeds or
 * until delay exceeds predefined maximum value {@code JdbcThinConnection.RECONNECTION_MAX_DELAY}
 * <pre>
 *   |_|_ _|_ _ _ _|_ _ _ _ _ _ _ _|
 *   where: '|' is connection attempt;
 *          '_' is an amount of time that reconnection tread waits, equal to JdbcThinConnection.RECONNECTION_DELAY;
 *
 *   so if we wait for 9 * RECONNECTION_DELAY, we expect to see exact four connection attempts:
 *   |_|_ _|_ _ _ _|_ _^_ _ _ _ _ _|
 *   </pre>
 * </li>
 * <li>Check that there were exact four reconnection attempts. In order to do this, we check logs, expecting to see
 * four warning messages there.</li>
 * <li>Start previously stopped node.</li>
 * <li>Wait until next reconnection attempt.</li>
 * <li>Check that both connections are established and that there are no warning messages within logs.</li>
 * <li>One more time: stop one node and invalidate corresponding connection.
 * Ensure that only one connection left.</li>
 * <li>Wait for some time.</li>
 * <li>Ensure that delay between reconnection was reset to initial value.
 * In other words, we again expect four warning messages within logs.</li>
 * </ol>
 *
 * @throws Exception If failed.
 */
@Test
public void testReconnectionDelaySelectiveIncreasing() throws Exception {
    try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800..10801?partitionAwareness=true")) {
        // Stop one node and invalidate corresponding connection. Ensure that only one connection left.
        stopGrid(0);
        invalidateConnectionToStoppedNode(conn);
        Map<UUID, JdbcThinTcpIo> ios = GridTestUtils.getFieldValue(conn, "ios");
        assertEquals("Unexpected connections count.", 1, ios.size());
        logHnd.records.clear();
        // Wait for some specific amount of time and ensure that there were exact four reconnection attempts.
        doSleep(9 * JdbcThinConnection.RECONNECTION_DELAY);
        assertEquals("Unexpected log records count.", 4, logHnd.records.size());
        String expRecordMsg = "Failed to connect to Ignite node [url=jdbc:ignite:thin://127.0.0.1:10800..10801]." + " address = [localhost/127.0.0.1:10800].";
        for (LogRecord record : logHnd.records) {
            assertEquals("Unexpected log record text.", expRecordMsg, record.getMessage());
            assertEquals("Unexpected log level", Level.WARNING, record.getLevel());
        }
        // Start previously stopped node.
        startGrid(0);
        logHnd.records.clear();
        // Waiting until next reconnection attempt.
        doSleep(9 * JdbcThinConnection.RECONNECTION_DELAY);
        // Checking that both connections are established and that there are no warning messages within logs.
        assertEquals("Unexpected log records count.", 0, logHnd.records.size());
        assertEquals("Unexpected connections count.", 2, ios.size());
        // One more time: stop one node, invalidate corresponding connection and ensure that only one connection
        // left.
        stopGrid(0);
        invalidateConnectionToStoppedNode(conn);
        assertEquals("Unexpected connections count.", 1, ios.size());
        logHnd.records.clear();
        // Wait for some time and ensure that delay between reconnection was reset to initial value.
        doSleep(9 * JdbcThinConnection.RECONNECTION_DELAY);
        assertEquals("Unexpected log records count.", 4, logHnd.records.size());
        for (LogRecord record : logHnd.records) {
            assertEquals("Unexpected log record text.", expRecordMsg, record.getMessage());
            assertEquals("Unexpected log level", Level.WARNING, record.getLevel());
        }
        startGrid(0);
    }
}
Also used : LogRecord(java.util.logging.LogRecord) Connection(java.sql.Connection) JdbcThinConnection(org.apache.ignite.internal.jdbc.thin.JdbcThinConnection) JdbcThinTcpIo(org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with JdbcThinTcpIo

use of org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo in project ignite by apache.

the class JdbcThinPartitionAwarenessReconnectionAndFailoverSelfTest method testBackgroundConnectionEstablishment.

/**
 * Check that background connection establishment works as expected.
 * <p>
 * Within new reconnection logic in partition awareness mode when {@code JdbcThinConnection} is created it eagerly
 * establishes a connection to one and only one ignite node. All other connections to nodes specified in connection
 * properties are established by background thread.
 * <p>
 * So in given test we specify url with 3 different ports and verify that 3 connections will be created: one in
 * eager mode and two within background thread. It takes some time for background thread to create a connection, and
 * cause, in addition to that it runs periodically with some delay, {@code GridTestUtils.waitForCondition} is used
 * in order to check that all expected connections are established.
 *
 * @throws Exception If failed.
 */
@Test
public void testBackgroundConnectionEstablishment() throws Exception {
    try (Connection conn = DriverManager.getConnection(URL)) {
        Map<UUID, JdbcThinTcpIo> ios = GridTestUtils.getFieldValue(conn, "ios");
        assertConnectionsCount(ios, 3);
    }
}
Also used : Connection(java.sql.Connection) JdbcThinConnection(org.apache.ignite.internal.jdbc.thin.JdbcThinConnection) JdbcThinTcpIo(org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with JdbcThinTcpIo

use of org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo in project ignite by apache.

the class JdbcThinPartitionAwarenessReconnectionAndFailoverSelfTest method testTotalConnectionFailover.

/**
 * Test total connection failover:
 * <ol>
 * <li>Check that at the beginning of test {@code INITIAL_NODES_CNT} connections are established.</li>
 * <li>Stop all nodes, invalidate dead connections (jdbc thin, won't detect that node has gone,
 * until it tries to touch it) and verify, that connections count equals to zero. </li>
 * <li>Start, previously stopped nodes, and check that connections count also restored to initial value.</li>
 * </ol>
 *
 * @throws Exception If failed.
 */
@Test
public void testTotalConnectionFailover() throws Exception {
    try (Connection conn = DriverManager.getConnection(URL)) {
        Map<UUID, JdbcThinTcpIo> ios = GridTestUtils.getFieldValue(conn, "ios");
        assertConnectionsCount(ios, INITIAL_NODES_CNT);
        for (int i = 0; i < INITIAL_NODES_CNT; i++) {
            stopGrid(i);
            invalidateConnectionToStoppedNode(conn);
        }
        assertConnectionsCount(ios, 0);
        for (int i = 0; i < INITIAL_NODES_CNT; i++) startGrid(i);
        assertConnectionsCount(ios, INITIAL_NODES_CNT);
    }
}
Also used : Connection(java.sql.Connection) JdbcThinConnection(org.apache.ignite.internal.jdbc.thin.JdbcThinConnection) JdbcThinTcpIo(org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

JdbcThinTcpIo (org.apache.ignite.internal.jdbc.thin.JdbcThinTcpIo)25 Test (org.junit.Test)19 JdbcThinConnection (org.apache.ignite.internal.jdbc.thin.JdbcThinConnection)18 Connection (java.sql.Connection)16 UUID (java.util.UUID)9 SQLException (java.sql.SQLException)5 ConnectionPropertiesImpl (org.apache.ignite.internal.jdbc.thin.ConnectionPropertiesImpl)4 InetSocketAddress (java.net.InetSocketAddress)3 LogRecord (java.util.logging.LogRecord)3 IgniteJdbcThinDataSource (org.apache.ignite.IgniteJdbcThinDataSource)3 Savepoint (java.sql.Savepoint)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 HostAndPortRange (org.apache.ignite.internal.util.HostAndPortRange)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 InetAddress (java.net.InetAddress)1 ServerSocket (java.net.ServerSocket)1 SQLClientInfoException (java.sql.SQLClientInfoException)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 NamingException (javax.naming.NamingException)1