Search in sources :

Example 36 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class IgniteTxExceptionAbstractSelfTest method checkUnlocked.

/**
 * @param key Key.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void checkUnlocked(final Integer key) throws Exception {
    TestIndexingSpi.forceFail(false);
    awaitPartitionMapExchange();
    info("Check key: " + key);
    for (int i = 0; i < gridCount(); i++) {
        final int idx = i;
        GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                IgniteKernal grid = (IgniteKernal) grid(idx);
                GridCacheAdapter cache = grid.internalCache(DEFAULT_CACHE_NAME);
                GridCacheEntryEx entry = cache.peekEx(key);
                log.info("Entry: " + entry);
                if (entry != null) {
                    try {
                        boolean locked = entry.lockedByAny();
                        if (locked) {
                            info("Unexpected entry for grid [i=" + idx + ", entry=" + entry + ']');
                            return false;
                        }
                    } catch (GridCacheEntryRemovedException ignore) {
                    // Obsolete entry cannot be locked.
                    }
                }
                if (cache.isNear()) {
                    entry = ((GridNearCacheAdapter) cache).dht().peekEx(key);
                    log.info("Dht entry: " + entry);
                    if (entry != null) {
                        try {
                            boolean locked = entry.lockedByAny();
                            if (locked) {
                                info("Unexpected entry for grid [i=" + idx + ", entry=" + entry + ']');
                                return false;
                            }
                        } catch (GridCacheEntryRemovedException ignore) {
                        // Obsolete entry cannot be locked.
                        }
                    }
                }
                return true;
            }
        }, getTestTimeout());
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteKernal(org.apache.ignite.internal.IgniteKernal)

Example 37 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class IgniteCacheLoadRebalanceEvictionSelfTest method testStartRebalancing.

/**
 * @throws Exception If failed.
 */
public void testStartRebalancing() throws Exception {
    List<IgniteInternalFuture<Object>> futs = new ArrayList<>();
    int gridCnt = 4;
    for (int i = 0; i < gridCnt; i++) {
        final IgniteEx ig = startGrid(i);
        futs.add(GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                ig.cache(DEFAULT_CACHE_NAME).localLoadCache(null);
                return null;
            }
        }));
    }
    try {
        for (IgniteInternalFuture<Object> fut : futs) fut.get();
        for (int i = 0; i < gridCnt; i++) {
            IgniteEx grid = grid(i);
            final IgniteCache<Object, Object> cache = grid.cache(DEFAULT_CACHE_NAME);
            GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return cache.localSize(CachePeekMode.ONHEAP) <= 10;
                }
            }, getTestTimeout());
        }
    } finally {
        stopAllGrids();
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) IgniteEx(org.apache.ignite.internal.IgniteEx) ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Callable(java.util.concurrent.Callable)

Example 38 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class IgniteCachePartitionedBackupNodeFailureRecoveryTest method testBackUpFail.

/**
 * Test stops and restarts backup node.
 *
 * @throws Exception If failed.
 */
public void testBackUpFail() throws Exception {
    final IgniteEx node1 = grid(0);
    final IgniteEx node2 = grid(1);
    final IgniteEx node3 = grid(2);
    awaitPartitionMapExchange();
    final IgniteCache<Integer, Integer> cache1 = node1.cache(DEFAULT_CACHE_NAME);
    Affinity<Integer> aff = node1.affinity(DEFAULT_CACHE_NAME);
    Integer key0 = null;
    for (int key = 0; key < 10_000; key++) {
        if (aff.isPrimary(node2.cluster().localNode(), key) && aff.isBackup(node3.cluster().localNode(), key)) {
            key0 = key;
            break;
        }
    }
    assertNotNull(key0);
    cache1.put(key0, 0);
    final AtomicBoolean finished = new AtomicBoolean();
    final ReentrantLock lock = new ReentrantLock();
    final AtomicInteger cntr = new AtomicInteger();
    final Integer finalKey = key0;
    IgniteInternalFuture<Void> primaryFut;
    IgniteInternalFuture<Void> backupFut;
    try {
        primaryFut = runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                while (!finished.get()) {
                    lock.lock();
                    try {
                        cache1.invoke(finalKey, new TestEntryProcessor());
                        cntr.getAndIncrement();
                    } finally {
                        lock.unlock();
                    }
                }
                return null;
            }
        });
        backupFut = runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                while (!finished.get()) {
                    stopGrid(2);
                    IgniteEx backUp = startGrid(2);
                    final IgniteCache<Integer, Integer> cache3 = backUp.cache(DEFAULT_CACHE_NAME);
                    lock.lock();
                    try {
                        boolean res = waitForCondition(new PA() {

                            @Override
                            public boolean apply() {
                                Integer actl = cache3.localPeek(finalKey);
                                Integer exp = cntr.get();
                                return exp.equals(actl);
                            }
                        }, 1000);
                        assertTrue(res);
                    } finally {
                        lock.unlock();
                    }
                }
                return null;
            }
        });
        Thread.sleep(30_000);
    } finally {
        finished.set(true);
    }
    primaryFut.get();
    backupFut.get();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PA(org.apache.ignite.internal.util.typedef.PA) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx)

Example 39 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class GridCacheRebalancingSyncSelfTest method checkSupplyContextMapIsEmpty.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
protected void checkSupplyContextMapIsEmpty() throws Exception {
    for (Ignite g : G.allGrids()) {
        for (GridCacheAdapter c : ((IgniteEx) g).context().cache().internalCaches()) {
            Object supplier = U.field(c.preloader(), "supplier");
            final Map map = U.field(supplier, "scMap");
            GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    synchronized (map) {
                        return map.isEmpty();
                    }
                }
            }, 15_000);
            synchronized (map) {
                assertTrue("Map is not empty [cache=" + c.name() + ", node=" + g.name() + ", map=" + map + ']', map.isEmpty());
            }
        }
    }
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap)

Example 40 with PA

use of org.apache.ignite.internal.util.typedef.PA in project ignite by apache.

the class CacheContinuousQueryOrderingEventTest method doOrderingTest.

/**
 * @param ccfg Cache configuration.
 * @param async Async filter.
 * @throws Exception If failed.
 */
protected void doOrderingTest(final CacheConfiguration ccfg, final boolean async) throws Exception {
    ignite(0).createCache(ccfg);
    List<QueryCursor<?>> qries = new ArrayList<>();
    try {
        List<BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(LISTENER_CNT * NODES);
        final AtomicInteger qryCntr = new AtomicInteger(0);
        final int threadCnt = 20;
        for (int idx = 0; idx < NODES; idx++) {
            for (int i = 0; i < LISTENER_CNT; i++) {
                BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue = new ArrayBlockingQueue<>(ITERATION_CNT * threadCnt);
                ContinuousQuery qry = new ContinuousQuery();
                if (async) {
                    qry.setLocalListener(new TestCacheAsyncEventListener(queue, qryCntr));
                    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
                } else {
                    qry.setLocalListener(new TestCacheEventListener(queue, qryCntr));
                    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilter(ccfg.getName())));
                }
                rcvdEvts.add(queue);
                IgniteCache<Object, Object> cache = grid(idx).cache(ccfg.getName());
                QueryCursor qryCursor = cache.query(qry);
                qries.add(qryCursor);
            }
        }
        IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < ITERATION_CNT; i++) {
                    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
                    QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
                    boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean();
                    Transaction tx = null;
                    if (startTx)
                        tx = cache.unwrap(Ignite.class).transactions().txStart();
                    try {
                        if ((cache.get(key) == null) || rnd.nextBoolean()) {
                            cache.invoke(key, new CacheEntryProcessor<QueryTestKey, QueryTestValue, Object>() {

                                @Override
                                public Object process(MutableEntry<QueryTestKey, QueryTestValue> entry, Object... arguments) throws EntryProcessorException {
                                    if (entry.exists())
                                        entry.setValue(new QueryTestValue(entry.getValue().val1 + 1));
                                    else
                                        entry.setValue(new QueryTestValue(0));
                                    return null;
                                }
                            });
                        } else {
                            QueryTestValue val;
                            QueryTestValue newVal;
                            do {
                                val = cache.get(key);
                                newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
                            } while (!cache.replace(key, val, newVal));
                        }
                    } finally {
                        if (tx != null)
                            tx.commit();
                    }
                }
            }
        }, threadCnt, "put-thread");
        f.get(15, TimeUnit.SECONDS);
        GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return qryCntr.get() >= ITERATION_CNT * threadCnt * LISTENER_CNT * NODES;
            }
        }, 1000L);
        for (BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue : rcvdEvts) checkEvents(queue, ITERATION_CNT * threadCnt);
        assertFalse("Ordering invocations of filter broken.", fail);
    } finally {
        for (QueryCursor<?> qry : qries) qry.close();
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) IgniteCache(org.apache.ignite.IgniteCache) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor)

Aggregations

PA (org.apache.ignite.internal.util.typedef.PA)55 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 Ignite (org.apache.ignite.Ignite)20 ArrayList (java.util.ArrayList)14 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)13 UUID (java.util.UUID)9 CacheEntryEvent (javax.cache.event.CacheEntryEvent)9 IgniteException (org.apache.ignite.IgniteException)7 QueryCursor (org.apache.ignite.cache.query.QueryCursor)7 List (java.util.List)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 IgniteCache (org.apache.ignite.IgniteCache)5 IgniteEx (org.apache.ignite.internal.IgniteEx)5 T2 (org.apache.ignite.internal.util.typedef.T2)5 Transaction (org.apache.ignite.transactions.Transaction)5 Callable (java.util.concurrent.Callable)4