Search in sources :

Example 61 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class IgniteCacheTxRecoveryRollbackTest method nearTx2.

/**
 * Stop both tx near node (client2) and primary node, near cache tx on client1 is invalidated.
 *
 * @param concurrency Tx concurrency or {@code null} for implicit transaction.
 * @throws Exception If failed.
 */
private void nearTx2(final TransactionConcurrency concurrency) throws Exception {
    startGrids(4);
    Ignite srv0 = grid(0);
    srv0.createCache(cacheConfiguration(2, false, false));
    awaitPartitionMapExchange();
    client = true;
    Ignite client1 = startGrid(4);
    final Ignite client2 = startGrid(5);
    final Integer key = primaryKey(srv0.cache(DEFAULT_CACHE_NAME));
    final IgniteCache<Integer, Integer> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<Integer, Integer>());
    final IgniteCache<Integer, Integer> cache2 = client2.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<Integer, Integer>());
    cache1.put(key, 1);
    final Integer newVal = 2;
    testSpi(client2).blockMessages(GridNearTxFinishRequest.class, srv0.name());
    testSpi(srv0).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

        @Override
        public boolean apply(ClusterNode node, Message msg) {
            return msg instanceof GridDhtTxFinishRequest;
        }
    });
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            log.info("Start put, concurrency: " + concurrency);
            if (concurrency != null) {
                try (Transaction tx = client2.transactions().txStart(concurrency, REPEATABLE_READ)) {
                    cache2.put(key, newVal);
                    tx.commit();
                }
            } else
                cache2.put(key, newVal);
            return null;
        }
    });
    U.sleep(500);
    assertFalse(fut.isDone());
    testSpi(client2).waitForBlocked(GridNearTxFinishRequest.class, srv0.name());
    stopGrid(client2.name());
    stopGrid(srv0.name());
    try {
        fut.get();
    } catch (IgniteCheckedException ignore) {
    // No-op.
    }
    final IgniteCache<Integer, Integer> srvCache = grid(1).cache(DEFAULT_CACHE_NAME);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return newVal.equals(srvCache.get(key)) && newVal.equals(cache1.get(key));
        }
    }, 5000);
    checkData(F.asMap(key, newVal));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Message(org.apache.ignite.plugin.extensions.communication.Message) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheLoaderException(javax.cache.integration.CacheLoaderException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheWriterException(javax.cache.integration.CacheWriterException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 62 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class IgniteCacheTxRecoveryRollbackTest method nearTx1.

/**
 * Stop tx near node (client2), near cache tx on client1 is either committed
 * by primary or invalidated.
 *
 * @param concurrency Tx concurrency or {@code null} for implicit transaction.
 * @throws Exception If failed.
 */
private void nearTx1(final TransactionConcurrency concurrency) throws Exception {
    startGrids(4);
    Ignite srv0 = grid(0);
    final IgniteCache<Integer, Integer> srvCache = srv0.createCache(cacheConfiguration(2, false, false));
    awaitPartitionMapExchange();
    client = true;
    Ignite client1 = startGrid(4);
    final Ignite client2 = startGrid(5);
    final Integer key = primaryKey(srv0.cache(DEFAULT_CACHE_NAME));
    final IgniteCache<Integer, Integer> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<Integer, Integer>());
    final IgniteCache<Integer, Integer> cache2 = client2.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<Integer, Integer>());
    cache1.put(key, 1);
    final Integer newVal = 2;
    testSpi(client2).blockMessages(GridNearTxFinishRequest.class, srv0.name());
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            log.info("Start put, concurrency: " + concurrency);
            if (concurrency != null) {
                try (Transaction tx = client2.transactions().txStart(concurrency, REPEATABLE_READ)) {
                    cache2.put(key, newVal);
                    tx.commit();
                }
            } else
                cache2.put(key, newVal);
            return null;
        }
    });
    U.sleep(500);
    assertFalse(fut.isDone());
    testSpi(client2).waitForBlocked(GridNearTxFinishRequest.class, srv0.name());
    stopGrid(client2.name());
    try {
        fut.get();
    } catch (IgniteCheckedException ignore) {
    // No-op.
    }
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return newVal.equals(srvCache.get(key)) && newVal.equals(cache1.get(key));
        }
    }, 5000);
    checkData(F.asMap(key, newVal));
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Ignite(org.apache.ignite.Ignite) CacheLoaderException(javax.cache.integration.CacheLoaderException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 63 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class GridCacheAtomicInvalidPartitionHandlingSelfTest method checkRestarts.

/**
 * @param writeSync Write synchronization mode to check.
 * @throws Exception If failed.
 */
private void checkRestarts(CacheWriteSynchronizationMode writeSync) throws Exception {
    this.writeSync = writeSync;
    final int gridCnt = 6;
    startGrids(gridCnt);
    awaitPartitionMapExchange();
    try {
        assertEquals(testClientNode(), (boolean) grid(0).configuration().isClientMode());
        final IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME);
        final int range = 100_000;
        final Set<Integer> keys = new LinkedHashSet<>();
        try (IgniteDataStreamer<Integer, Integer> streamer = grid(0).dataStreamer(DEFAULT_CACHE_NAME)) {
            streamer.allowOverwrite(true);
            for (int i = 0; i < range; i++) {
                streamer.addData(i, 0);
                keys.add(i);
                if (i > 0 && i % 10_000 == 0)
                    System.err.println("Put: " + i);
            }
        }
        final Affinity<Integer> aff = grid(0).affinity(DEFAULT_CACHE_NAME);
        boolean putDone = GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                Iterator<Integer> it = keys.iterator();
                while (it.hasNext()) {
                    Integer key = it.next();
                    Collection<ClusterNode> affNodes = aff.mapKeyToPrimaryAndBackups(key);
                    for (int i = 0; i < gridCnt; i++) {
                        ClusterNode locNode = grid(i).localNode();
                        IgniteCache<Object, Object> cache = grid(i).cache(DEFAULT_CACHE_NAME);
                        Object val = cache.localPeek(key);
                        if (affNodes.contains(locNode)) {
                            if (val == null)
                                return false;
                        } else
                            assertNull(val);
                    }
                    it.remove();
                }
                return true;
            }
        }, 30_000);
        assertTrue(putDone);
        assertTrue(keys.isEmpty());
        final AtomicBoolean done = new AtomicBoolean();
        delay = true;
        System.err.println("FINISHED PUTS");
        // Start put threads.
        IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new Random();
                while (!done.get()) {
                    try {
                        int cnt = rnd.nextInt(5);
                        if (cnt < 2) {
                            int key = rnd.nextInt(range);
                            int val = rnd.nextInt();
                            cache.put(key, val);
                        } else {
                            Map<Integer, Integer> upd = new TreeMap<>();
                            for (int i = 0; i < cnt; i++) upd.put(rnd.nextInt(range), rnd.nextInt());
                            cache.putAll(upd);
                        }
                    } catch (CachePartialUpdateException ignored) {
                    // No-op.
                    }
                }
                return null;
            }
        }, 4, "putAll-thread");
        Random rnd = new Random();
        // Restart random nodes.
        for (int r = 0; r < 20; r++) {
            int idx0 = rnd.nextInt(gridCnt - 1) + 1;
            stopGrid(idx0);
            U.sleep(200);
            startGrid(idx0);
        }
        done.set(true);
        awaitPartitionMapExchange();
        fut.get();
        for (int k = 0; k < range; k++) {
            Collection<ClusterNode> affNodes = affinity(cache).mapKeyToPrimaryAndBackups(k);
            // Test is valid with at least one backup.
            assert affNodes.size() >= 2;
            Object val = null;
            GridCacheVersion ver = null;
            UUID nodeId = null;
            for (int i = 0; i < gridCnt; i++) {
                ClusterNode locNode = grid(i).localNode();
                GridCacheAdapter<Object, Object> c = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
                GridCacheEntryEx entry = null;
                try {
                    entry = c.entryEx(k);
                    entry.unswap();
                } catch (GridDhtInvalidPartitionException ignored) {
                // Skip key.
                }
                for (int r = 0; r < 10; r++) {
                    try {
                        if (affNodes.contains(locNode)) {
                            assert c.affinity().isPrimaryOrBackup(locNode, k);
                            boolean primary = c.affinity().isPrimary(locNode, k);
                            assertNotNull("Failed to find entry on node for key [locNode=" + locNode.id() + ", key=" + k + ']', entry);
                            if (val == null) {
                                assertNull(ver);
                                val = CU.value(entry.rawGet(), entry.context(), false);
                                ver = entry.version();
                                nodeId = locNode.id();
                            } else {
                                assertNotNull(ver);
                                assertEquals("Failed to check value for key [key=" + k + ", node=" + locNode.id() + ", primary=" + primary + ", recNodeId=" + nodeId + ']', val, CU.value(entry.rawGet(), entry.context(), false));
                                assertEquals("Failed to check version for key [key=" + k + ", node=" + locNode.id() + ", primary=" + primary + ", recNodeId=" + nodeId + ']', ver, entry.version());
                            }
                        } else
                            assertTrue("Invalid entry: " + entry, entry == null || !entry.partitionValid());
                    } catch (AssertionError e) {
                        if (r == 9) {
                            info("Failed to verify cache contents: " + e.getMessage());
                            throw e;
                        }
                        info("Failed to verify cache contents, will retry: " + e.getMessage());
                        // Give some time to finish async updates.
                        U.sleep(1000);
                    }
                }
            }
        }
    } finally {
        stopAllGrids();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) Random(java.util.Random) Iterator(java.util.Iterator) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) CachePartialUpdateException(org.apache.ignite.cache.CachePartialUpdateException) CachePartialUpdateException(org.apache.ignite.cache.CachePartialUpdateException) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) Collection(java.util.Collection) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 64 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class IgniteCacheAtomicProtocolTest method testPutPrimarySync.

/**
 * @throws Exception If failed.
 */
public void testPutPrimarySync() throws Exception {
    startGrids(2);
    client = true;
    Ignite clientNode = startGrid(2);
    client = false;
    final IgniteCache<Integer, Integer> nearCache = clientNode.createCache(cacheConfiguration(1, PRIMARY_SYNC));
    awaitPartitionMapExchange();
    Ignite srv0 = grid(0);
    final Ignite srv1 = grid(1);
    final Integer key = primaryKey(srv0.cache(TEST_CACHE));
    testSpi(srv0).blockMessages(GridDhtAtomicSingleUpdateRequest.class, srv1.name());
    IgniteFuture<?> fut = nearCache.putAsync(key, key);
    fut.get(5, TimeUnit.SECONDS);
    assertEquals(key, srv0.cache(TEST_CACHE).get(key));
    assertNull(srv1.cache(TEST_CACHE).localPeek(key));
    testSpi(srv0).stopBlock(true);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return srv1.cache(TEST_CACHE).localPeek(key) != null;
        }
    }, 5000);
    checkData(F.asMap(key, key));
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Ignite(org.apache.ignite.Ignite)

Example 65 with GridAbsPredicate

use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.

the class IgniteCacheAtomicProtocolTest method fullAsyncRemap.

/**
 * @param putAll Test putAll flag.
 * @throws Exception If failed.
 */
private void fullAsyncRemap(boolean putAll) throws Exception {
    Ignite srv0 = startGrid(0);
    client = true;
    Ignite clientNode = startGrid(1);
    client = false;
    final IgniteCache<Integer, Integer> nearCache = clientNode.createCache(cacheConfiguration(1, FULL_ASYNC));
    List<Integer> keys = movingKeysAfterJoin(srv0, TEST_CACHE, putAll ? 10 : 1);
    testSpi(clientNode).blockMessages(GridNearAtomicSingleUpdateRequest.class, srv0.name());
    testSpi(clientNode).blockMessages(GridNearAtomicFullUpdateRequest.class, srv0.name());
    final Map<Integer, Integer> map = new HashMap<>();
    for (Integer key : keys) map.put(key, -key);
    if (putAll)
        nearCache.putAll(map);
    else
        nearCache.put(keys.get(0), map.get(keys.get(0)));
    Affinity<Object> aff = clientNode.affinity(TEST_CACHE);
    startGrid(2);
    awaitPartitionMapExchange();
    int keysMoved = 0;
    for (Integer key : keys) {
        if (!aff.isPrimary(srv0.cluster().localNode(), key))
            keysMoved++;
    }
    assertEquals(keys.size(), keysMoved);
    testSpi(clientNode).stopBlock(true);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (Integer key : map.keySet()) {
                if (nearCache.get(key) == null)
                    return false;
            }
            return true;
        }
    }, 5000);
    checkData(map);
}
Also used : HashMap(java.util.HashMap) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Ignite(org.apache.ignite.Ignite)

Aggregations

GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)175 Ignite (org.apache.ignite.Ignite)81 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)42 ClusterNode (org.apache.ignite.cluster.ClusterNode)32 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)27 IgniteException (org.apache.ignite.IgniteException)22 CountDownLatch (java.util.concurrent.CountDownLatch)20 IgniteKernal (org.apache.ignite.internal.IgniteKernal)19 ArrayList (java.util.ArrayList)18 IgniteCache (org.apache.ignite.IgniteCache)16 Map (java.util.Map)15 Transaction (org.apache.ignite.transactions.Transaction)15 IgniteEx (org.apache.ignite.internal.IgniteEx)14 Duration (javax.cache.expiry.Duration)13 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)13 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)12 HashMap (java.util.HashMap)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)11 Event (org.apache.ignite.events.Event)11