Java Code Examples for org.apache.ignite.cache.query.SqlFieldsQuery#setDistributedJoins()

The following examples show how to use org.apache.ignite.cache.query.SqlFieldsQuery#setDistributedJoins() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgniteSqlSplitterSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param sql SQL.
 * @param enforceJoinOrder Enforce join order flag.
 */
private void checkQueryFails(final IgniteCache<Object, Object> cache,
    String sql,
    boolean enforceJoinOrder) {
    final SqlFieldsQuery qry = new SqlFieldsQuery(sql);

    qry.setDistributedJoins(true);
    qry.setEnforceJoinOrder(enforceJoinOrder);

    GridTestUtils.assertThrows(log, new Callable<Void>() {
        @Override public Void call() throws Exception {
            cache.query(qry);

            return null;
        }
    }, CacheException.class, null);
}
 
Example 2
Source File: IgniteCacheDistributedJoinQueryConditionsTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param sql SQL.
 * @param cache Cache.
 * @param enforceJoinOrder Enforce join order flag.
 * @param expSize Expected results size.
 * @param args Arguments.
 */
private void checkQuery(String sql,
    IgniteCache<Object, Object> cache,
    boolean enforceJoinOrder,
    int expSize,
    Object... args) {
    SqlFieldsQuery qry = new SqlFieldsQuery(sql);

    qry.setDistributedJoins(true);
    qry.setEnforceJoinOrder(enforceJoinOrder);
    qry.setArgs(args);

    log.info("Plan: " + queryPlan(cache, qry));

    QueryCursor<List<?>> cur = cache.query(qry);

    List<List<?>> res = cur.getAll();

    if (expSize != res.size())
        log.info("Results: " + res);

    assertEquals(expSize, res.size());
}
 
Example 3
Source File: IgniteCacheDistributedJoinNoIndexTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param sql SQL.
 * @param cache Cache.
 * @param expSize Expected results size.
 * @param args Arguments.
 * @return Results.
 */
private List<List<?>> checkQuery(String sql,
    IgniteCache<Object, Object> cache,
    int expSize,
    Object... args) {
    SqlFieldsQuery qry = new SqlFieldsQuery(sql);

    qry.setDistributedJoins(true);
    qry.setArgs(args);

    log.info("Plan: " + queryPlan(cache, qry));

    QueryCursor<List<?>> cur = cache.query(qry);

    List<List<?>> res = cur.getAll();

    if (expSize != res.size())
        log.info("Results: " + res);

    assertEquals(expSize, res.size());

    return res;
}
 
Example 4
Source File: SqlQueriesExample.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Example for SQL queries based on all employees working
 * for a specific organization (query uses distributed join).
 */
private static void sqlQueryWithDistributedJoin() {
    IgniteCache<Long, Person> cache = Ignition.ignite().cache(PERSON_CACHE);

    // SQL clause query which joins on 2 types to select people for a specific organization.
    String joinSql =
        "select pers.* from Person as pers, \"" + ORG_CACHE + "\".Organization as org " +
        "where pers.orgId = org.id " +
        "and lower(org.name) = lower(?)";

    SqlFieldsQuery qry = new SqlFieldsQuery(joinSql).setArgs("ApacheIgnite");

    // Enable distributed joins for query.
    qry.setDistributedJoins(true);

    // Execute queries for find employees for different organizations.
    print("Following people are 'ApacheIgnite' employees (distributed join): ", cache.query(qry).getAll());

    qry.setArgs("Other");

    print("Following people are 'Other' employees (distributed join): ", cache.query(qry).getAll());
}
 
Example 5
Source File: IgniteCacheDistributedJoinCollocatedAndNotTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param sql SQL.
 * @param cache Cache.
 * @param enforceJoinOrder Enforce join order flag.
 * @param expSize Expected results size.
 */
private void checkQuery(String sql,
    IgniteCache<Object, Object> cache,
    boolean enforceJoinOrder,
    int expSize) {
    String plan = (String)cache.query(new SqlFieldsQuery("explain " + sql)
        .setDistributedJoins(true)
        .setEnforceJoinOrder(enforceJoinOrder))
        .getAll().get(0).get(0);

    log.info("Plan: " + plan);

    SqlFieldsQuery qry = new SqlFieldsQuery(sql);

    qry.setDistributedJoins(true);
    qry.setEnforceJoinOrder(enforceJoinOrder);

    QueryCursor<List<?>> cur = cache.query(qry);

    List<List<?>> res = cur.getAll();

    if (expSize != res.size())
        log.info("Results: " + res);

    assertEquals(expSize, res.size());
}
 
Example 6
Source File: IgniteCacheDistributedJoinCustomAffinityMapper.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param sql SQL.
 * @param enforceJoinOrder Enforce join order flag.
 */
private void checkQueryFails(final IgniteCache<Object, Object> cache,
    String sql,
    boolean enforceJoinOrder) {
    final SqlFieldsQuery qry = new SqlFieldsQuery(sql);

    qry.setDistributedJoins(true);
    qry.setEnforceJoinOrder(enforceJoinOrder);

    Throwable err = GridTestUtils.assertThrows(log, new Callable<Void>() {
        @Override public Void call() throws Exception {
            cache.query(qry);

            return null;
        }
    }, CacheException.class, null);

    assertTrue("Unexpected error message: " + err.getMessage(),
        err.getMessage().contains("can not use distributed joins for cache with custom AffinityKeyMapper configured."));
}
 
Example 7
Source File: IgniteSqlSplitterSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testIndexWithDifferentSegmentationLevelsFailure() throws Exception {
    CacheConfiguration ccfg1 = cacheConfig("pers", true,
        Integer.class, Person2.class).setQueryParallelism(4);
    CacheConfiguration ccfg2 = cacheConfig("org", true,
        Integer.class, Organization.class).setQueryParallelism(3);

    final IgniteCache<Object, Object> c1 = ignite(0).getOrCreateCache(ccfg1);
    final IgniteCache<Object, Object> c2 = ignite(0).getOrCreateCache(ccfg2);

    try {
        c2.put(1, new Organization("o1"));
        c2.put(2, new Organization("o2"));
        c1.put(3, new Person2(1, "p1"));
        c1.put(4, new Person2(2, "p2"));
        c1.put(5, new Person2(3, "p3"));

        String select0 = "select o.name n1, p.name n2 from \"pers\".Person2 p, \"org\".Organization o where p.orgId = o._key and o._key=1";

        final SqlFieldsQuery qry = new SqlFieldsQuery(select0);

        qry.setDistributedJoins(true);

        GridTestUtils.assertThrows(log, new Callable<Void>() {
            @Override public Void call() throws Exception {
                c1.query(qry);

                return null;
            }
        }, CacheException.class, "Using indexes with different parallelism levels in same query is forbidden.");
    }
    finally {
        c1.destroy();
        c2.destroy();
    }
}
 
Example 8
Source File: IgniteSqlSplitterSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 * @param enforceJoinOrder Enforce join order flag.
 * @param expBatchedJoins Expected batched joins count.
 * @param qry Query.
 * @param expText Expected text to find in plan.
 */
private void checkQueryPlan(IgniteCache<Object, Object> cache,
    boolean enforceJoinOrder,
    int expBatchedJoins,
    SqlFieldsQuery qry,
    String... expText) {
    qry.setEnforceJoinOrder(enforceJoinOrder);
    qry.setDistributedJoins(true);

    String plan = queryPlan(cache, qry);

    log.info("\n  Plan:\n" + plan);

    assertEquals("Unexpected number of batched joins in plan [plan=" + plan + ", qry=" + qry + ']',
        expBatchedJoins,
        StringUtils.countOccurrencesOf(plan, "batched"));

    int startIdx = 0;

    for (String exp : expText) {
        int idx = plan.indexOf(exp, startIdx);

        if (idx == -1) {
            fail("Plan does not contain expected string [startIdx=" + startIdx +
                ", plan=" + plan +
                ", exp=" + exp + ']');
        }

        startIdx = idx + 1;
    }
}
 
Example 9
Source File: JdbcStatement.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Sets parameters to query object. Logic should be consistent with {@link JdbcQueryTask#call()} and {@link
 * JdbcQueryMultipleStatementsTask#call()}. Parameters are determined from context: connection state and this
 * statement state.
 *
 * @param qry query which parameters to set up
 */
protected void setupQuery(SqlFieldsQuery qry) {
    qry.setPageSize(fetchSize);
    qry.setLocal(conn.nodeId() == null);
    qry.setCollocated(conn.isCollocatedQuery());
    qry.setDistributedJoins(conn.isDistributedJoins());
    qry.setEnforceJoinOrder(conn.isEnforceJoinOrder());
    qry.setLazy(conn.isLazy());
    qry.setSchema(conn.schemaName());
}
 
Example 10
Source File: IgniteH2Indexing.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@SuppressWarnings("deprecation")
@Override public SqlFieldsQuery generateFieldsQuery(String cacheName, SqlQuery qry) {
    String schemaName = schema(cacheName);

    String type = qry.getType();

    H2TableDescriptor tblDesc = schemaMgr.tableForType(schemaName, cacheName, type);

    if (tblDesc == null)
        throw new IgniteSQLException("Failed to find SQL table for type: " + type,
            IgniteQueryErrorCode.TABLE_NOT_FOUND);

    String sql;

    try {
        sql = generateFieldsQueryString(qry.getSql(), qry.getAlias(), tblDesc);
    }
    catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }

    SqlFieldsQuery res = new SqlFieldsQuery(sql);

    res.setArgs(qry.getArgs());
    res.setDistributedJoins(qry.isDistributedJoins());
    res.setLocal(qry.isLocal());
    res.setPageSize(qry.getPageSize());
    res.setPartitions(qry.getPartitions());
    res.setReplicatedOnly(qry.isReplicatedOnly());
    res.setSchema(schemaName);
    res.setSql(sql);

    if (qry.getTimeout() > 0)
        res.setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS);

    return res;
}
 
Example 11
Source File: IgniteCrossCachesJoinsQueryTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 */
private void checkPersonOrganizationGroupBy(IgniteCache cache) {
    if (skipQuery(cache, PERSON_CACHE_NAME, ORG_CACHE_NAME))
        return;

    qry = "checkPersonOrganizationGroupBy";

    // Max salary per organization.
    SqlFieldsQuery q = new SqlFieldsQuery("select max(p.salary) " +
        "from \"" + PERSON_CACHE_NAME + "\".Person p join \"" + ORG_CACHE_NAME + "\".Organization o " +
        "on p.orgId = o.id " +
        "group by o.name " +
        "having o.id = ?");

    q.setDistributedJoins(distributedJoins());

    for (Map.Entry<Integer, Integer> e : data.maxSalaryPerOrg.entrySet()) {
        Integer orgId = e.getKey();
        Integer maxSalary = e.getValue();

        q.setArgs(orgId);

        List<List<?>> res = cache.query(q).getAll();

        String errMsg = "Expected data [orgId=" + orgId + ", maxSalary=" + maxSalary + ", data=" + data + "]";

        // MaxSalary == -1 means that there are no persons at organization.
        if (maxSalary > 0) {
            assertEquals(errMsg, 1, res.size());
            assertEquals(errMsg, 1, res.get(0).size());
            assertEquals(errMsg, maxSalary, res.get(0).get(0));
        }
        else
            assertEquals(errMsg, 0, res.size());
    }
}
 
Example 12
Source File: IgniteCrossCachesJoinsQueryTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 */
private void checkPersonAccountGroupBy(IgniteCache cache) {
    if (skipQuery(cache, PERSON_CACHE_NAME, ACC_CACHE_NAME))
        return;

    qry = "checkPersonAccountGroupBy";

    // Count accounts per person.
    SqlFieldsQuery q = new SqlFieldsQuery("select count(a.id) " +
        "from \"" + PERSON_CACHE_NAME + "\".Person p join \"" + ACC_CACHE_NAME + "\".Account a " +
        "on p.strId = a.personStrId " +
        "group by p.name " +
        "having p.id = ?");

    q.setDistributedJoins(distributedJoins());

    List<Integer> keys = new ArrayList<>(data.accountsPerPerson.keySet());

    for (int i = 0; i < 10; i++) {
        Integer personId = keys.get(rnd.nextInt(keys.size()));
        Integer cnt = data.accountsPerPerson.get(personId);

        q.setArgs(personId);

        List<List<?>> res = cache.query(q).getAll();

        String errMsg = "Expected data [personId=" + personId + ", cnt=" + cnt + ", data=" + data + "]";

        // Cnt == 0 means that there are no accounts for the person.
        if (cnt > 0) {
            assertEquals(errMsg, 1, res.size());
            assertEquals(errMsg, 1, res.get(0).size());
            assertEquals(errMsg, (long)cnt, res.get(0).get(0));
        }
        else
            assertEquals(errMsg, 0, res.size());
    }
}
 
Example 13
Source File: CacheMvccSqlTxQueriesWithReducerAbstractTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testQueryReducerImplicitTxInsert() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT)
        .setIndexedTypes(Integer.class, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue.class);

    startGridsMultiThreaded(4);

    Random rnd = ThreadLocalRandom.current();

    Ignite checkNode = grid(rnd.nextInt(4));
    Ignite updateNode = grid(rnd.nextInt(4));

    IgniteCache<Integer, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue> cache =
        checkNode.cache(DEFAULT_CACHE_NAME);

    cache.putAll(F.asMap(
        1,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1),
        2,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2),
        3,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3)));

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), cache.get(2));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(3));

    String sqlText = "INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) " +
            "SELECT DISTINCT _key + 3, idxVal1 + 3 FROM MvccTestSqlIndexValue";

    SqlFieldsQuery qry = new SqlFieldsQuery(sqlText);

    qry.setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS);

    qry.setDistributedJoins(true);

    IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME);

    try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) {
        assertEquals(3L, cur.iterator().next().get(0));
    }

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), cache.get(2));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(3));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(4), cache.get(4));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(5), cache.get(5));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(6), cache.get(6));
}
 
Example 14
Source File: JdbcBatchUpdateTask.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Performs update.
 *
 * @param cache Cache.
 * @param sqlText SQL text.
 * @param args Parameters.
 * @return Update counter.
 * @throws SQLException If failed.
 */
private Integer doSingleUpdate(IgniteCache<?, ?> cache, String sqlText, List<Object> args) throws SQLException {
    SqlFieldsQuery qry = new SqlFieldsQueryEx(sqlText, false);

    qry.setPageSize(fetchSize);
    qry.setLocal(locQry);
    qry.setCollocated(collocatedQry);
    qry.setDistributedJoins(distributedJoins);
    qry.setSchema(schemaName);
    qry.setArgs(args == null ? null : args.toArray());

    QueryCursorImpl<List<?>> qryCursor = (QueryCursorImpl<List<?>>)cache.withKeepBinary().query(qry);

    if (qryCursor.isQuery()) {
        throw createJdbcSqlException(getError("Query produced result set", qry),
            IgniteQueryErrorCode.STMT_TYPE_MISMATCH);
    }

    List<List<?>> rows = qryCursor.getAll();

    if (F.isEmpty(rows))
        return SUCCESS_NO_INFO;

    if (rows.size() != 1)
        throw new SQLException(getError("Expected single row for update operation result", qry));

    List<?> row = rows.get(0);

    if (F.isEmpty(row) || row.size() != 1)
        throw new SQLException(getError("Expected row size of 1 for update operation", qry));

    Object objRes = row.get(0);

    if (!(objRes instanceof Long))
        throw new SQLException(getError("Unexpected update result type", qry));

    Long longRes = (Long)objRes;

    if (longRes > Integer.MAX_VALUE) {
        IgniteLogger log = ignite.log();

        if (log != null)
            log.warning(getError("Query updated row counter (" + longRes + ") exceeds integer range", qry));

        return Integer.MAX_VALUE;
    }

    return longRes.intValue();
}
 
Example 15
Source File: CacheMvccSqlTxQueriesWithReducerAbstractTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testQueryReducerRollbackInsert() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT)
        .setIndexedTypes(Integer.class, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue.class);

    startGridsMultiThreaded(4);

    Random rnd = ThreadLocalRandom.current();

    Ignite checkNode = grid(rnd.nextInt(4));
    Ignite updateNode = grid(rnd.nextInt(4));

    IgniteCache<Integer, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue> cache =
        checkNode.cache(DEFAULT_CACHE_NAME);

    cache.putAll(F.asMap(
        1,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1),
        2,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2),
        3,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3)));

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), cache.get(2));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(3));

    try (Transaction tx = updateNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        tx.timeout(TIMEOUT);

        String sqlText = "INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) " +
            "SELECT DISTINCT _key + 3, idxVal1 + 3 FROM MvccTestSqlIndexValue";

        SqlFieldsQuery qry = new SqlFieldsQuery(sqlText);

        qry.setDistributedJoins(true);

        IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME);

        try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) {
            assertEquals(3L, cur.iterator().next().get(0));
        }

        tx.rollback();
    }

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), sqlGet(1, cache).get(0).get(0));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), sqlGet(2, cache).get(0).get(0));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), sqlGet(3, cache).get(0).get(0));
    assertTrue(sqlGet(4, cache).isEmpty());
    assertTrue(sqlGet(5, cache).isEmpty());
    assertTrue(sqlGet(6, cache).isEmpty());
}
 
Example 16
Source File: CacheMvccSqlTxQueriesWithReducerAbstractTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testQueryReducerMerge() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT)
        .setIndexedTypes(Integer.class, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue.class);

    startGridsMultiThreaded(4);

    Random rnd = ThreadLocalRandom.current();

    Ignite checkNode = grid(rnd.nextInt(4));
    Ignite updateNode = grid(rnd.nextInt(4));

    IgniteCache<Integer, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue> cache =
        checkNode.cache(DEFAULT_CACHE_NAME);

    cache.putAll(F.asMap(
        1,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1),
        2,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2),
        3,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3)));

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), cache.get(2));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(3));

    try (Transaction tx = updateNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        tx.timeout(TIMEOUT);

        String sqlText = "MERGE INTO MvccTestSqlIndexValue (_key, idxVal1) " +
            "SELECT DISTINCT _key * 2, idxVal1 FROM MvccTestSqlIndexValue";

        SqlFieldsQuery qry = new SqlFieldsQuery(sqlText);

        qry.setDistributedJoins(true);

        IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME);

        try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) {
            assertEquals(3L, cur.iterator().next().get(0));
        }

        tx.commit();
    }

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(2));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(3));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), cache.get(4));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(6));
}
 
Example 17
Source File: CacheMvccSqlTxQueriesWithReducerAbstractTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testQueryReducerInsertDuplicateKey() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT)
        .setIndexedTypes(Integer.class, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue.class);

    startGridsMultiThreaded(4);

    Random rnd = ThreadLocalRandom.current();

    Ignite checkNode = grid(rnd.nextInt(4));
    Ignite updateNode = grid(rnd.nextInt(4));

    IgniteCache<Integer, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue> cache =
        checkNode.cache(DEFAULT_CACHE_NAME);

    cache.putAll(F.asMap(
        1,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1),
        2,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2),
        3,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3)));

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), cache.get(2));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(3));

    try (Transaction tx = updateNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        tx.timeout(TIMEOUT);

        String sqlText = "INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) " +
            "SELECT DISTINCT _key, idxVal1 FROM MvccTestSqlIndexValue";

        SqlFieldsQuery qry = new SqlFieldsQuery(sqlText);

        qry.setDistributedJoins(true);

        IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME);

        GridTestUtils.assertThrows(log, new Callable<Object>() {
            @Override public Object call() {
                return cache0.query(qry);
            }
        }, TransactionDuplicateKeyException.class, "Duplicate key during INSERT");

        tx.rollback();
    }
}
 
Example 18
Source File: CacheMvccSqlTxQueriesWithReducerAbstractTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testQueryReducerInsert() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT)
        .setIndexedTypes(Integer.class, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue.class);

    startGridsMultiThreaded(4);

    Random rnd = ThreadLocalRandom.current();

    Ignite checkNode = grid(rnd.nextInt(4));
    Ignite updateNode = grid(rnd.nextInt(4));

    IgniteCache<Integer, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue> cache =
        checkNode.cache(DEFAULT_CACHE_NAME);

    cache.putAll(F.asMap(
        1,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1),
        2,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2),
        3,new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3)));

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), cache.get(2));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(3));

    try (Transaction tx = updateNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        tx.timeout(TIMEOUT);

        String sqlText = "INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) " +
            "SELECT DISTINCT _key + 3, idxVal1 + 3 FROM MvccTestSqlIndexValue";

        SqlFieldsQuery qry = new SqlFieldsQuery(sqlText);

        qry.setDistributedJoins(true);

        IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME);

        try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) {
            assertEquals(3L, cur.iterator().next().get(0));
        }

        tx.commit();
    }

    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(2), cache.get(2));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3), cache.get(3));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(4), cache.get(4));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(5), cache.get(5));
    assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(6), cache.get(6));
}
 
Example 19
Source File: GridMapQueryExecutor.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param node Node.
 * @param req DML request.
 */
public void onDmlRequest(final ClusterNode node, final GridH2DmlRequest req) {
    int[] parts = req.queryPartitions();

    List<Integer> cacheIds = req.caches();

    long reqId = req.requestId();

    AffinityTopologyVersion topVer = req.topologyVersion();

    PartitionReservation reserved = null;

    MapNodeResults nodeResults = resultsForNode(node.id());

    try {
        reserved = h2.partitionReservationManager().reservePartitions(
            cacheIds,
            topVer,
            parts,
            node.id(),
            reqId
        );

        if (reserved.failed()) {
            U.error(log, "Failed to reserve partitions for DML request. [localNodeId=" + ctx.localNodeId() +
                ", nodeId=" + node.id() + ", reqId=" + req.requestId() + ", cacheIds=" + cacheIds +
                ", topVer=" + topVer + ", parts=" + Arrays.toString(parts) + ']');

            sendUpdateResponse(node, reqId, null,
                "Failed to reserve partitions for DML request. " + reserved.error());

            return;
        }

        IndexingQueryFilter filter = h2.backupFilter(topVer, parts);

        GridQueryCancel cancel = nodeResults.putUpdate(reqId);

        SqlFieldsQuery fldsQry = new SqlFieldsQuery(req.query());

        if (req.parameters() != null)
            fldsQry.setArgs(req.parameters());

        fldsQry.setEnforceJoinOrder(req.isFlagSet(GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER));
        fldsQry.setTimeout(req.timeout(), TimeUnit.MILLISECONDS);
        fldsQry.setPageSize(req.pageSize());
        fldsQry.setLocal(true);

        boolean local = true;

        final boolean replicated = req.isFlagSet(GridH2QueryRequest.FLAG_REPLICATED);

        if (!replicated && !F.isEmpty(cacheIds) &&
            CU.firstPartitioned(ctx.cache().context(), cacheIds).config().getQueryParallelism() > 1) {
            fldsQry.setDistributedJoins(true);

            local = false;
        }

        UpdateResult updRes = h2.executeUpdateOnDataNode(req.schemaName(), fldsQry, filter, cancel, local);

        GridCacheContext<?, ?> mainCctx =
            !F.isEmpty(cacheIds) ? ctx.cache().context().cacheContext(cacheIds.get(0)) : null;

        boolean evt = local && mainCctx != null && mainCctx.events().isRecordable(EVT_CACHE_QUERY_EXECUTED);

        if (evt) {
            ctx.event().record(new CacheQueryExecutedEvent<>(
                node,
                "SQL query executed.",
                EVT_CACHE_QUERY_EXECUTED,
                CacheQueryType.SQL.name(),
                mainCctx.name(),
                null,
                req.query(),
                null,
                null,
                req.parameters(),
                node.id(),
                null));
        }

        sendUpdateResponse(node, reqId, updRes, null);
    }
    catch (Exception e) {
        U.error(log, "Error processing dml request. [localNodeId=" + ctx.localNodeId() +
            ", nodeId=" + node.id() + ", req=" + req + ']', e);

        sendUpdateResponse(node, reqId, null, e.getMessage());
    }
    finally {
        if (reserved != null)
            reserved.release();

        nodeResults.removeUpdate(reqId);
    }
}
 
Example 20
Source File: CacheMvccSqlTxQueriesWithReducerAbstractTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
private void checkMultiBatchPerNode(boolean client) throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT)
        .setIndexedTypes(Integer.class, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue.class);

    Ignite checkNode;
    Ignite updateNode;

    Random rnd = ThreadLocalRandom.current();

    if (client) {
        startGridsMultiThreaded(3);

        updateNode = grid(rnd.nextInt(3));

        this.client = true;

        checkNode = startGrid(4);
    }
    else {
        startGridsMultiThreaded(4);

        checkNode = grid(rnd.nextInt(4));
        updateNode = grid(rnd.nextInt(4));
    }

    IgniteCache<Integer, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue> cache =
        checkNode.cache(DEFAULT_CACHE_NAME);

    final int count = 6;

    Map<Integer, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue> vals = new HashMap<>(count);

    for (int idx = 1; idx <= count; ++idx)
        vals.put(idx, new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(idx));

    cache.putAll(vals);

    try (Transaction tx = updateNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        tx.timeout(TIMEOUT);

        String sqlText = "INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) " +
            "SELECT DISTINCT _key + 6, idxVal1 + 6 FROM MvccTestSqlIndexValue";

        SqlFieldsQuery qry = new SqlFieldsQuery(sqlText);

        qry.setDistributedJoins(true);
        qry.setPageSize(1);

        IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME);

        try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) {
            assertEquals((long)count, cur.iterator().next().get(0));
        }

        tx.commit();
    }
}