Search in sources :

Example 1 with AffinityCache

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

the class JdbcThinPartitionAwarenessReconnectionAndFailoverSelfTest method testQueryFailover.

/**
 * Check that failover occurs if the result of sending first iteration of sql request is an Exception but not an
 * SQLException.
 *
 * <ol>
 * <li>Create {@code JdbcThinConnection} to all existing nodes.</li>
 * <li>Create a cache and populate it with some data.</li>
 * <li>Submit some failover-applicable sql query with specific condition within where clause,
 * that assumes partition awareness. Submit same query one more time. It's necessary in order to warm up affinity
 * awareness cache.</li>
 * <li>From within affinity cache calculate node that was used to process query. Stop it.</li>
 * <li>Submit sql query, that is equal to initial one, one more time.
 * Because of partition awareness, given query will be routed to previously stopped node, so an Exception will be
 * received. Here query failover goes and resents query to an alive node using another {@code JdbcThinTcpIo}</li>
 * <li>Because of failover, no exceptions are expected on Jdbc thin client side.
 * However within the {@code JdbcThinConnection}, in case of {@code Level.FINE} log level, corresponding log record
 * is expected.</li>
 * </ol>
 *
 * @throws Exception If failed.
 */
@Test
public void testQueryFailover() throws Exception {
    try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800..10802?partitionAwareness=true")) {
        final String cacheName = UUID.randomUUID().toString().substring(0, 6);
        final String sql = "select * from \"" + cacheName + "\".Person where _key = 1";
        CacheConfiguration<Object, Object> cache = prepareCacheConfig(cacheName);
        ignite(0).createCache(cache);
        fillCache(cacheName);
        Statement stmt = conn.createStatement();
        stmt.execute(sql);
        stmt.execute(sql);
        AffinityCache affinityCache = GridTestUtils.getFieldValue(conn, "affinityCache");
        Integer part = ((PartitionSingleNode) affinityCache.partitionResult(new QualifiedSQLQuery("PUBLIC", sql)).partitionResult().tree()).value();
        UUID nodeId = affinityCache.cacheDistribution(GridCacheUtils.cacheId(cacheName))[part];
        int gridIdx = new Integer(Ignition.ignite(nodeId).name().substring(getTestIgniteInstanceName().length()));
        stopGrid(gridIdx);
        logHnd.records.clear();
        conn.createStatement().execute(sql);
        startGrid(gridIdx);
    }
    assertEquals("Unexpected log records count.", 1, logHnd.records.size());
    LogRecord record = logHnd.records.get(0);
    assertEquals("Unexpected log record text.", "Exception during sending an sql request.", record.getMessage());
    assertEquals("Unexpected log level", Level.FINE, record.getLevel());
}
Also used : AffinityCache(org.apache.ignite.internal.jdbc.thin.AffinityCache) LogRecord(java.util.logging.LogRecord) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcThinConnection(org.apache.ignite.internal.jdbc.thin.JdbcThinConnection) PartitionSingleNode(org.apache.ignite.internal.sql.optimizer.affinity.PartitionSingleNode) QualifiedSQLQuery(org.apache.ignite.internal.jdbc.thin.QualifiedSQLQuery) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with AffinityCache

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

the class JdbcThinPartitionAwarenessSelfTest method testPartitionAwarenessIsSkippedByDefault.

/**
 * Check that client side partition awareness optimizations are skipped by default.
 *
 * @throws Exception If failed.
 */
@Test
public void testPartitionAwarenessIsSkippedByDefault() throws Exception {
    try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800..10802");
        Statement stmt = conn.createStatement()) {
        final String cacheName = "yacccc";
        CacheConfiguration<Object, Object> cache = prepareCacheConfig(cacheName);
        ignite(0).createCache(cache);
        stmt.executeQuery("select * from \"" + cacheName + "\".Person where _key = 1");
        AffinityCache affinityCache = GridTestUtils.getFieldValue(conn, "affinityCache");
        assertNull("Affinity cache is not null.", affinityCache);
    }
}
Also used : AffinityCache(org.apache.ignite.internal.jdbc.thin.AffinityCache) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 3 with AffinityCache

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

the class JdbcThinPartitionAwarenessSelfTest method verifyPartitionResultIsNull.

/**
 * Utility method that executes given query and verifies that expected number of records was returned.
 * Besides that given method verified that partition result for corresponding query is null.
 *
 * @param sqlQry Sql query.
 * @param expRowsCnt Expected rows count.
 * @throws SQLException If failed.
 */
protected void verifyPartitionResultIsNull(String sqlQry, int expRowsCnt) throws SQLException {
    ResultSet rs = stmt.executeQuery(sqlQry);
    assert rs != null;
    int rowCntr = 0;
    while (rs.next()) rowCntr++;
    assertEquals("Rows counter doesn't match expected value.", expRowsCnt, rowCntr);
    AffinityCache affinityCache = GridTestUtils.getFieldValue(conn, "affinityCache");
    PartitionResult gotPartRes = affinityCache.partitionResult(new QualifiedSQLQuery("default", sqlQry)).partitionResult();
    assertNull("Partition result descriptor is not null.", gotPartRes);
}
Also used : AffinityCache(org.apache.ignite.internal.jdbc.thin.AffinityCache) ResultSet(java.sql.ResultSet) QualifiedSQLQuery(org.apache.ignite.internal.jdbc.thin.QualifiedSQLQuery) PartitionResult(org.apache.ignite.internal.sql.optimizer.affinity.PartitionResult)

Example 4 with AffinityCache

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

the class JdbcThinPartitionAwarenessSelfTest method testPartitionAwarenessLimitedCacheSize.

/**
 * Check that partitionAwarenessSQLCacheSize and partitionAwarenessPartitionDistributionsCacheSize
 * actually limit corresponding caches within partition awareness cache.
 *
 * @throws Exception If failed.
 */
@Test
public void testPartitionAwarenessLimitedCacheSize() throws Exception {
    try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800..10802?partitionAwareness=true" + "&partitionAwarenessSQLCacheSize=1&partitionAwarenessPartitionDistributionsCacheSize=1");
        Statement stmt = conn.createStatement()) {
        final String cacheName1 = UUID.randomUUID().toString().substring(0, 6);
        CacheConfiguration<Object, Object> cache1 = prepareCacheConfig(cacheName1);
        ignite(0).createCache(cache1);
        fillCache(cacheName1);
        final String cacheName2 = UUID.randomUUID().toString().substring(0, 6);
        CacheConfiguration<Object, Object> cache2 = prepareCacheConfig(cacheName2);
        ignite(0).createCache(cache2);
        fillCache(cacheName2);
        stmt.executeQuery("select * from \"" + cacheName1 + "\".Person where _key = 1");
        stmt.executeQuery("select * from \"" + cacheName1 + "\".Person where _key = 1");
        stmt.executeQuery("select * from \"" + cacheName2 + "\".Person where _key = 1");
        stmt.executeQuery("select * from \"" + cacheName2 + "\".Person where _key = 1");
        AffinityCache affinityCache = GridTestUtils.getFieldValue(conn, "affinityCache");
        GridBoundedLinkedHashMap<Integer, UUID[]> partitionsDistributionCache = GridTestUtils.getFieldValue(affinityCache, "cachePartitionsDistribution");
        GridBoundedLinkedHashMap<QualifiedSQLQuery, JdbcThinPartitionResultDescriptor> sqlCache = GridTestUtils.getFieldValue(affinityCache, "sqlCache");
        assertEquals("Unexpected count of partitions distributions.", 1, partitionsDistributionCache.size());
        assertEquals("Unexpected count of sql queries.", 1, sqlCache.size());
        assertTrue("Unexpected distribution is found.", partitionsDistributionCache.containsKey(GridCacheUtils.cacheId(cacheName2)));
        assertTrue("Unexpected sql query is found.", sqlCache.containsKey(new QualifiedSQLQuery("PUBLIC", "select * from \"" + cacheName2 + "\".Person where _key = 1")));
    }
}
Also used : JdbcThinPartitionResultDescriptor(org.apache.ignite.internal.jdbc.thin.JdbcThinPartitionResultDescriptor) AffinityCache(org.apache.ignite.internal.jdbc.thin.AffinityCache) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) QualifiedSQLQuery(org.apache.ignite.internal.jdbc.thin.QualifiedSQLQuery) Test(org.junit.Test)

Example 5 with AffinityCache

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

the class JdbcThinPartitionAwarenessSelfTest method testChangeTopologyDetectionWithinPartitionDistributionResponse.

/**
 * Check that affinity cache is invalidated in case of changing topology,
 * detected during partitions distribution retrieval.
 *
 * @throws Exception If failed.
 */
@Test
public void testChangeTopologyDetectionWithinPartitionDistributionResponse() throws Exception {
    final String sqlQry = "select * from Person where _key = 1";
    stmt.executeQuery(sqlQry);
    AffinityCache affinityCache = GridTestUtils.getFieldValue(conn, "affinityCache");
    startGrid(3);
    stmt.executeQuery(sqlQry);
    AffinityCache recreatedAffinityCache = GridTestUtils.getFieldValue(conn, "affinityCache");
    assertTrue(recreatedAffinityCache.version().compareTo(affinityCache.version()) > 0);
}
Also used : AffinityCache(org.apache.ignite.internal.jdbc.thin.AffinityCache) Test(org.junit.Test)

Aggregations

AffinityCache (org.apache.ignite.internal.jdbc.thin.AffinityCache)10 Test (org.junit.Test)9 QualifiedSQLQuery (org.apache.ignite.internal.jdbc.thin.QualifiedSQLQuery)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 Statement (java.sql.Statement)4 JdbcThinPartitionResultDescriptor (org.apache.ignite.internal.jdbc.thin.JdbcThinPartitionResultDescriptor)3 ResultSet (java.sql.ResultSet)2 UUID (java.util.UUID)1 LogRecord (java.util.logging.LogRecord)1 JdbcThinConnection (org.apache.ignite.internal.jdbc.thin.JdbcThinConnection)1 PartitionResult (org.apache.ignite.internal.sql.optimizer.affinity.PartitionResult)1 PartitionSingleNode (org.apache.ignite.internal.sql.optimizer.affinity.PartitionSingleNode)1