Java Code Examples for org.apache.ignite.IgniteCache#putAll()

The following examples show how to use org.apache.ignite.IgniteCache#putAll() . 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: SqlIndexConsistencyAfterInterruptAtomicCacheOperationTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception On error.
 */
@Test
public void testPutAll() throws Exception {
    IgniteEx ign = startGrid(0);

    IgniteCache<Object, Object> cache = ign.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)
        .setAtomicityMode(atomicity())
        .setIndexedTypes(Integer.class, Integer.class));

    final Map<Integer, Integer> batch = new HashMap<>();

    for (int i = 0; i < KEYS; ++i)
        batch.put(i, i);

    Thread t = new Thread(() -> {
        cache.putAll(batch);
    });

    t.start();
    t.interrupt();
    t.join();

    assertEquals(cache.size(), cache.query(new SqlFieldsQuery("select * from Integer")).getAll().size());
}
 
Example 2
Source File: CachePutGetExample.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Execute bulk {@code putAll(...)} and {@code getAll(...)} operations.
 *
 * @throws IgniteException If failed.
 */
private static void putAllGetAll(IgniteCache<Integer, String> cache) throws IgniteException {
    System.out.println();
    System.out.println(">>> Starting putAll-getAll example.");

    final int keyCnt = 20;

    // Create batch.
    Map<Integer, String> batch = new HashMap<>();

    for (int i = 0; i < keyCnt; i++)
        batch.put(i, "bulk-" + Integer.toString(i));

    // Bulk-store entries in cache.
    cache.putAll(batch);

    System.out.println(">>> Bulk-stored values in cache.");

    // Bulk-get values from cache.
    Map<Integer, String> vals = cache.getAll(batch.keySet());

    for (Map.Entry<Integer, String> e : vals.entrySet())
        System.out.println("Got entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
}
 
Example 3
Source File: GridCacheDaemonNodeAbstractSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testImplicit() throws Exception {
    try {
        startGridsMultiThreaded(3);

        daemon = true;

        startGrid(4);

        IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

        for (int i = 0; i < 30; i++)
            cache.put(i, i);

        Map<Integer, Integer> batch = new HashMap<>();

        for (int i = 30; i < 60; i++)
            batch.put(i, i);

        cache.putAll(batch);

        for (int i = 0; i < 60; i++)
            assertEquals(i, (int)cache.get(i));
    }
    finally {
        stopAllGrids();
    }
}
 
Example 4
Source File: CacheContinuousQueryWithTransformerExample.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Populates cache with data.
 *
 * @param cache Organization cache.
 */
private static void populateCache(IgniteCache<Integer, Organization> cache) {
    Map<Integer, Organization> data = new HashMap<>();

    data.put(1, new Organization(
        "Microsoft", // Name.
        new Address("1096 Eddy Street, San Francisco, CA", 94109), // Address.
        OrganizationType.PRIVATE, // Type.
        new Timestamp(System.currentTimeMillis()))); // Last update time.

    data.put(2, new Organization(
        "Red Cross", // Name.
        new Address("184 Fidler Drive, San Antonio, TX", 78205), // Address.
        OrganizationType.NON_PROFIT, // Type.
        new Timestamp(System.currentTimeMillis()))); // Last update time.

    data.put(3, new Organization(
        "Apple", // Name.
        new Address("1 Infinite Loop, Cupertino, CA", 95014), // Address.
        OrganizationType.PRIVATE, // Type.
        new Timestamp(System.currentTimeMillis()))); // Last update time.

    data.put(4, new Organization(
        "IBM", // Name.
        new Address("1 New Orchard Road Armonk, New York", 10504), // Address.
        OrganizationType.PRIVATE, // Type.
        new Timestamp(System.currentTimeMillis()))); // Last update time.

    data.put(5, new Organization(
        "NASA Armstrong Flight Research Center", // Name.
        new Address("4800 Lilly Ave, Edwards, CA", 793523), // Address.
        OrganizationType.NON_PROFIT, // Type.
        new Timestamp(System.currentTimeMillis()))); // Last update time.

    cache.putAll(data);
}
 
Example 5
Source File: IgnitePdsWithTtlTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** */
protected void fillCache(IgniteCache<Integer, byte[]> cache) {
    cache.putAll(new TreeMap<Integer, byte[]>() {{
        for (int i = 0; i < ENTRIES; i++)
            put(i, new byte[1024]);
    }});

    //Touch entries.
    for (int i = 0; i < ENTRIES; i++)
        cache.get(i); // touch entries

    printStatistics((IgniteCacheProxy)cache, "After cache puts");
}
 
Example 6
Source File: IgniteCacheRandomOperationBenchmark.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Ignite cache.
 * @throws Exception If failed.
 */
private void doPutAll(IgniteCache<Object, Object> cache) throws Exception {
    Map<Object, Object> putMap = new TreeMap<>();

    Class keyCass = randomKeyClass(cache.getName());

    for (int cnt = 0; cnt < args.batch(); cnt++) {
        int i = nextRandom(args.range());

        putMap.put(ModelUtil.create(keyCass, i), createRandomValue(i, cache.getName()));
    }

    cache.putAll(putMap);
}
 
Example 7
Source File: GridCacheColocatedDebugTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param explicitTx Whether or not start implicit tx.
 * @param concurrency Tx concurrency.
 * @param isolation Tx isolation.
 * @throws Exception If failed.
 */
private void checkSinglePut(boolean explicitTx, TransactionConcurrency concurrency, TransactionIsolation isolation)
    throws Exception {
    startGrid();

    try {
        Transaction tx = explicitTx ? grid().transactions().txStart(concurrency, isolation) : null;

        try {
            IgniteCache<Object, Object> cache = jcache();

            cache.putAll(F.asMap(1, "Hello", 2, "World"));

            if (tx != null)
                tx.commit();

            System.out.println(cache.localMetrics());

            assertEquals("Hello", cache.get(1));
            assertEquals("World", cache.get(2));
            assertNull(cache.get(3));
        }
        finally {
            if (tx != null)
                tx.close();
        }
    }
    finally {
        stopAllGrids();
    }
}
 
Example 8
Source File: GridCacheHashMapPutAllWarningsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testHashMapPutAllExactMessage() throws Exception {
    List<String> messages = Collections.synchronizedList(new ArrayList<>());

    testLog = new ListeningTestLogger(false, log());

    testLog.registerListener((s) -> {
            if (s.contains("deadlock"))
                messages.add(s);
        });

    Ignite ignite = startGrid(0);

    IgniteCache<Integer, String> c = ignite.getOrCreateCache(new CacheConfiguration<>("exact"));

    HashMap<Integer, String> m = new HashMap<>();

    m.put(1, "foo");
    m.put(2, "bar");

    c.putAll(m);

    assertEquals(2, c.size());

    int found = 0;

    for (String message : messages) {
        if (message.contains("Unordered map java.util.HashMap is used for putAll operation on cache exact. " +
            "This can lead to a distributed deadlock. Switch to a sorted map like TreeMap instead."))
            found++;
    }

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

    startGridsMultiThreaded(4);

    awaitPartitionMapExchange();

    Random rnd = ThreadLocalRandom.current();

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

    IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME);

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

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

        SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE MvccTestSqlIndexValue AS t " +
            "SET (idxVal1) = (SELECT idxVal1*10 FROM MvccTestSqlIndexValue WHERE t._key = _key)");

        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(10, ((MvccTestSqlIndexValue)cache.get(1)).idxVal1);
    assertEquals(20, ((MvccTestSqlIndexValue)cache.get(2)).idxVal1);
    assertEquals(30, ((MvccTestSqlIndexValue)cache.get(3)).idxVal1);
}
 
Example 10
Source File: CacheTtlAbstractSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testDefaultTimeToLivePreload() throws Exception {
    if (cacheMode() == LOCAL)
        return;

    IgniteCache<Integer, Integer> cache = jcache(0);

    Map<Integer, Integer> entries = new HashMap<>();

    for (int i = 0; i < SIZE; ++i)
        entries.put(i, i);

    cache.putAll(entries);

    startGrid(gridCount());

    checkSizeBeforeLive(SIZE, gridCount() + 1);

    Thread.sleep(DEFAULT_TIME_TO_LIVE + 500);

    checkSizeAfterLive(gridCount() + 1);
}
 
Example 11
Source File: IgnitePutGetBatchBenchmark.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public boolean test(Map<Object, Object> ctx) throws Exception {
    IgniteCache<Integer, Object> cache = cacheForOperation();

    Set<Integer> keys = new TreeSet<>();

    while (keys.size() < args.batch())
        keys.add(nextRandom(args.range()));

    Map<Integer, Object> vals = cache.getAll(keys);

    Map<Integer, SampleValue> updates = new TreeMap<>();

    for (Integer key : keys) {
        Object val = vals.get(key);

        if (val != null)
            key = nextRandom(args.range());

        updates.put(key, new SampleValue(key));
    }

    cache.putAll(updates);

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

    startGridsMultiThreaded(4);

    Random rnd = ThreadLocalRandom.current();

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

    IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME);

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

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

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

        SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE MvccTestSqlIndexValue SET idxVal1 = 8 WHERE _key = 1");

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

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

        tx.commit();
    }

    assertEquals(new MvccTestSqlIndexValue(8), cache.get(1));
    assertEquals(new MvccTestSqlIndexValue(2), cache.get(2));
    assertEquals(new MvccTestSqlIndexValue(3), cache.get(3));
}
 
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 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 14
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 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 testQueryReducerUpdate() throws Exception {
    ccfgs = new CacheConfiguration[] {
        cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT)
            .setName("int")
            .setIndexedTypes(Integer.class, Integer.class),
        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, Integer> cache = checkNode.cache("int");

    cache.putAll(F.asMap(1, 5, 3, 1, 5, 3));

    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));

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

    cache0.putAll(vals);

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

        String sqlText = "UPDATE MvccTestSqlIndexValue t SET idxVal1=" +
            "(SELECT _val FROM \"int\".Integer WHERE t._key = _key)" +
            " WHERE EXISTS (SELECT 1 FROM \"int\".Integer WHERE t._key = _key)";

        SqlFieldsQuery qry = new SqlFieldsQuery(sqlText);

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

        tx.commit();
    }
}
 
Example 16
Source File: CacheMvccTransactionsTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testRebalanceSimple() throws Exception {
    Ignite srv0 = startGrid(0);

    IgniteCache<Integer, Integer> cache = (IgniteCache)srv0.createCache(
        cacheConfiguration(PARTITIONED, FULL_SYNC, 0, DFLT_PARTITION_COUNT));

    Map<Integer, Integer> map;
    Map<Integer, Integer> resMap;

    try (Transaction tx = srv0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        map = new HashMap<>();

        for (int i = 0; i < DFLT_PARTITION_COUNT * 3; i++)
            map.put(i, i);

        cache.putAll(map);

        tx.commit();
    }

    startGrid(1);

    awaitPartitionMapExchange();

    resMap = checkAndGetAll(false, cache, map.keySet(), GET, SCAN);

    assertEquals(map.size(), resMap.size());

    for (int i = 0; i < map.size(); i++)
        assertEquals(i, (Object)resMap.get(i));

    try (Transaction tx = srv0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        for (int i = 0; i < DFLT_PARTITION_COUNT * 3; i++)
            map.put(i, i + 1);

        cache.putAll(map);

        tx.commit();
    }
    try (Transaction tx = srv0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        for (int i = 0; i < DFLT_PARTITION_COUNT * 3; i++)
            map.put(i, i + 2);

        cache.putAll(map);

        tx.commit();
    }

    startGrid(2);

    awaitPartitionMapExchange();

    resMap = checkAndGetAll(false, cache, map.keySet(), GET, SCAN);

    assertEquals(map.size(), map.size());

    for (int i = 0; i < map.size(); i++)
        assertEquals(i + 2, (Object)resMap.get(i));

    // Run fake transaction
    try (Transaction tx = srv0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        Integer val = cache.get(0);

        cache.put(0, val);

        tx.commit();
    }

    resMap = checkAndGetAll(false, cache, map.keySet(), GET, SCAN);

    assertEquals(map.size(), map.size());

    for (int i = 0; i < map.size(); i++)
        assertEquals(i + 2, (Object)resMap.get(i));
}
 
Example 17
Source File: CacheMvccSqlTxQueriesAbstractTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testUpdateExplicitPartitionsWithoutReducer() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, 10)
        .setIndexedTypes(Integer.class, Integer.class);

    Ignite ignite = startGridsMultiThreaded(4);

    awaitPartitionMapExchange();

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

    Affinity<Object> affinity = internalCache0(cache).affinity();

    int keysCnt = 10, retryCnt = 0;

    Integer test = 0;

    Map<Integer, Integer> vals = new LinkedHashMap<>();

    while (vals.size() < keysCnt) {
        int partition = affinity.partition(test);

        if (partition == 1 || partition == 2)
            vals.put(test, 0);
        else
            assertTrue("Maximum retry number exceeded", ++retryCnt < 1000);

        test++;
    }

    cache.putAll(vals);

    SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE Integer set _val=2").setPartitions(1,2);

    List<List<?>> all = cache.query(qry).getAll();

    assertEquals(Long.valueOf(keysCnt), all.stream().findFirst().orElseThrow(AssertionError::new).get(0));

    List<List<?>> rows = cache.query(new SqlFieldsQuery("SELECT _val FROM Integer")).getAll();

    assertEquals(keysCnt, rows.size());
    assertTrue(rows.stream().map(r -> r.get(0)).map(Integer.class::cast).allMatch(v -> v == 2));
}
 
Example 18
Source File: IgnitePdsDataRegionMetricsTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/** */
@Test
public void testMemoryUsageSingleNode() throws Exception {
    DataRegionMetrics initMetrics = null;

    for (int iter = 0; iter < ITERATIONS; iter++) {
        final IgniteEx node = startGrid(0);

        node.cluster().active(true);

        DataRegionMetrics currMetrics = getDfltRegionMetrics(node);

        if (initMetrics == null)
            initMetrics = currMetrics;

        assertTrue(currMetrics.getTotalAllocatedPages() >= currMetrics.getPhysicalMemoryPages());

        final IgniteCache<String, String> cache = node.cache(DEFAULT_CACHE_NAME);

        Map<String, String> map = new HashMap<>();

        for (int batch = 0; batch < BATCHES; batch++) {
            int nPuts = BATCH_SIZE_LOW + ThreadLocalRandom.current().nextInt(BATCH_SIZE_HIGH - BATCH_SIZE_LOW);

            for (int i = 0; i < nPuts; i++)
                map.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());

            cache.putAll(map);

            forceCheckpoint(node);

            checkMetricsConsistency(node);
        }

        currMetrics = getDfltRegionMetrics(node);

        // Make sure metrics are rising
        assertTrue(currMetrics.getPhysicalMemoryPages() > initMetrics.getPhysicalMemoryPages());
        assertTrue(currMetrics.getTotalAllocatedPages() > initMetrics.getTotalAllocatedPages());

        stopGrid(0, true);
    }
}
 
Example 19
Source File: IgniteCacheClientNodeChangingTopologyTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testOptimisticTxMessageClientFirstFlag() throws Exception {
    ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);

    ccfg.setCacheMode(PARTITIONED);
    ccfg.setBackups(1);
    ccfg.setAtomicityMode(TRANSACTIONAL);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setRebalanceMode(SYNC);

    IgniteEx ignite0 = startGrid(0);
    IgniteEx ignite1 = startGrid(1);
    IgniteEx ignite2 = startGrid(2);

    awaitPartitionMapExchange();

    Ignite ignite3 = startClientGrid(3);

    assertTrue(ignite3.configuration().isClientMode());

    TestCommunicationSpi spi = (TestCommunicationSpi)ignite3.configuration().getCommunicationSpi();

    IgniteCache<Integer, Integer> cache = ignite3.cache(DEFAULT_CACHE_NAME);

    List<Integer> keys0 = primaryKeys(ignite0.cache(DEFAULT_CACHE_NAME), 2, 0);
    List<Integer> keys1 = primaryKeys(ignite1.cache(DEFAULT_CACHE_NAME), 2, 0);
    List<Integer> keys2 = primaryKeys(ignite2.cache(DEFAULT_CACHE_NAME), 2, 0);

    LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();

    map.put(keys0.get(0), 1);
    map.put(keys1.get(0), 2);
    map.put(keys2.get(0), 3);
    map.put(keys0.get(1), 4);
    map.put(keys1.get(1), 5);
    map.put(keys2.get(1), 6);

    spi.record(GridNearTxPrepareRequest.class);

    try (Transaction tx = ignite3.transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) {
        for (Map.Entry<Integer, Integer> e : map.entrySet())
            cache.put(e.getKey(), e.getValue());

        tx.commit();
    }

    checkClientPrepareMessages(spi.recordedMessages(), 6);

    checkData(map, null, cache, 4);

    cache.putAll(map);

    checkClientPrepareMessages(spi.recordedMessages(), 6);

    spi.record(null);

    checkData(map, null, cache, 4);

    IgniteCache<Integer, Integer> cache0 = ignite0.cache(DEFAULT_CACHE_NAME);

    TestCommunicationSpi spi0 = (TestCommunicationSpi)ignite0.configuration().getCommunicationSpi();

    spi0.record(GridNearTxPrepareRequest.class);

    cache0.putAll(map);

    spi0.record(null);

    List<Object> msgs = spi0.recordedMessages();

    assertEquals(4, msgs.size());

    for (Object msg : msgs)
        assertFalse(((GridNearTxPrepareRequest)msg).firstClientRequest());

    checkData(map, null, cache, 4);
}
 
Example 20
Source File: IgniteTxPreloadAbstractTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * Tries to execute transaction doing transform when target key is not yet preloaded.
 *
 * @param txConcurrency Transaction concurrency;
 * @throws Exception If failed.
 */
private void testLocalTxPreloading(TransactionConcurrency txConcurrency) throws Exception {
    Map<String, Integer> map = new HashMap<>();

    for (int i = 0; i < 10000; i++)
        map.put(String.valueOf(i), 0);

    IgniteCache<String, Integer> cache0 = jcache(0);

    cache0.putAll(map);

    final String TX_KEY = "9000";

    int expVal = 0;

    for (int i = 1; i < GRID_CNT; i++) {
        assertEquals((Integer)expVal, cache0.get(TX_KEY));

        startGrid(i);

        IgniteCache<String, Integer> cache = jcache(i);

        IgniteTransactions txs = ignite(i).transactions();

        try (Transaction tx = txs.txStart(txConcurrency, TransactionIsolation.REPEATABLE_READ)) {
            cache.invoke(TX_KEY, new EntryProcessor<String, Integer, Void>() {
                @Override public Void process(MutableEntry<String, Integer> e, Object... args) {
                    Integer val = e.getValue();

                    if (val == null) {
                        keyNotLoaded = true;

                        e.setValue(1);

                        return null;
                    }

                    e.setValue(val + 1);

                    return null;
                }
            });

            tx.commit();
        }

        assertFalse(keyNotLoaded);

        expVal++;

        assertEquals((Integer)expVal, cache.get(TX_KEY));
    }

    for (int i = 0; i < GRID_CNT; i++)
        assertEquals("Unexpected value for cache " + i, (Integer)expVal, jcache(i).get(TX_KEY));
}