Search in sources :

Example 76 with GridAbsPredicate

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

the class IgniteCacheStoreValueAbstractTest method testValueNotStored.

/**
 * @throws Exception If failed.
 */
public void testValueNotStored() throws Exception {
    cpyOnRead = true;
    startGrids();
    IgniteCache<TestKey, TestValue> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    Affinity<Object> aff = grid(0).affinity(DEFAULT_CACHE_NAME);
    final List<WeakReference<Object>> refs = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        TestKey key = new TestKey(i);
        TestValue val = new TestValue(i);
        refs.add(new WeakReference<Object>(val));
        cache.put(key, val);
        checkNoValue(aff, key);
        for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
        checkNoValue(aff, key);
        cache.invoke(key, new CacheEntryProcessor<TestKey, TestValue, Object>() {

            @Override
            public Object process(MutableEntry<TestKey, TestValue> entry, Object... args) {
                assertNotNull(entry.getValue());
                entry.setValue(new TestValue(10_000));
                return new TestValue(20_000);
            }
        });
        checkNoValue(aff, key);
        for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
        checkNoValue(aff, key);
        cache.remove(key);
        for (int g = 0; g < gridCount(); g++) assertNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
        try (IgniteDataStreamer<TestKey, TestValue> streamer = grid(0).dataStreamer(DEFAULT_CACHE_NAME)) {
            streamer.addData(key, val);
        }
        checkNoValue(aff, key);
        cache.remove(key);
        try (IgniteDataStreamer<TestKey, TestValue> streamer = grid(0).dataStreamer(DEFAULT_CACHE_NAME)) {
            streamer.allowOverwrite(true);
            streamer.addData(key, val);
        }
        checkNoValue(aff, key);
        if (aff.isPrimaryOrBackup(grid(0).localNode(), key)) {
            cache.localEvict(Collections.singleton(key));
            assertNull(cache.localPeek(key, CachePeekMode.ONHEAP));
            assertNotNull(cache.localPeek(key, CachePeekMode.ONHEAP));
            checkNoValue(aff, key);
        }
    }
    // Should load TestKey(100_000).
    cache.loadCache(null);
    TestKey key = new TestKey(100_000);
    checkNoValue(aff, key);
    for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
    checkNoValue(aff, key);
    boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            System.gc();
            boolean pass = true;
            for (Iterator<WeakReference<Object>> it = refs.iterator(); it.hasNext(); ) {
                WeakReference<Object> ref = it.next();
                if (ref.get() == null)
                    it.remove();
                else {
                    pass = false;
                    log.info("Not collected value: " + ref.get());
                }
            }
            return pass;
        }
    }, 60_000);
    assertTrue("Failed to wait for when values are collected", wait);
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ArrayList(java.util.ArrayList) WeakReference(java.lang.ref.WeakReference) Iterator(java.util.Iterator)

Example 77 with GridAbsPredicate

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

the class GridTcpCommunicationSpiMultithreadedSelfTest method testFlowSend.

/**
 * @throws Exception If failed.
 */
public void testFlowSend() throws Exception {
    reject = true;
    final CyclicBarrier barrier = new CyclicBarrier(THREAD_CNT);
    final Random rnd = new Random();
    final ClusterNode from = randomNode(rnd);
    ClusterNode tmp;
    do {
        tmp = randomNode(rnd);
    } while (tmp.id().equals(from.id()));
    final ClusterNode to = tmp;
    final int iterationCnt = 1000;
    final AtomicInteger threadId = new AtomicInteger();
    final int interval = 50;
    IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void run() {
            try {
                // Only first thread will print messages.
                int id = threadId.getAndIncrement();
                for (int i = 0; i < iterationCnt; i++) {
                    if (id == 0 && (i % 50) == 0)
                        info(">>> Running iteration " + i);
                    try {
                        for (ClusterNode node : nodes) {
                            Message msg = new GridTestMessage(from.id(), msgId.getAndIncrement(), 0);
                            spis.get(from.id()).sendMessage(node, msg);
                        }
                    } catch (IgniteException e) {
                        log.warning(">>> Oops, unable to send message (safe to ignore).", e);
                    }
                    barrier.await();
                }
            } catch (InterruptedException ignored) {
                Thread.currentThread().interrupt();
            } catch (BrokenBarrierException e) {
                info("Wait on barrier failed: " + e);
                Thread.currentThread().interrupt();
            }
        }
    }, THREAD_CNT, "message-sender");
    final AtomicBoolean run = new AtomicBoolean(true);
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            try {
                while (run.get() && !Thread.currentThread().isInterrupted()) {
                    U.sleep(interval * 3 / 2);
                    ((TcpCommunicationSpi) spis.get(from.id())).onNodeLeft(to.id());
                }
            } catch (IgniteInterruptedCheckedException ignored) {
                Thread.currentThread().interrupt();
            }
        }
    }, 1);
    fut.get();
    run.set(false);
    fut2.get();
    // Wait when all messages are acknowledged to do not break next tests' logic.
    for (CommunicationSpi<Message> spi : spis.values()) {
        GridNioServer srv = U.field(spi, "nioSrvr");
        Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
        for (GridNioSession ses : sessions) {
            final GridNioRecoveryDescriptor snd = ses.outRecoveryDescriptor();
            if (snd != null) {
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return snd.messagesRequests().isEmpty();
                    }
                }, 10_000);
                assertEquals("Unexpected messages: " + snd.messagesRequests(), 0, snd.messagesRequests().size());
            }
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)

Example 78 with GridAbsPredicate

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

the class GridTcpCommunicationSpiRecoverySelfTest method checkBlockListener.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("BusyWait")
private void checkBlockListener() throws Exception {
    TcpCommunicationSpi spi0 = spis.get(0);
    TcpCommunicationSpi spi1 = spis.get(1);
    final TestListener lsnr0 = (TestListener) spi0.getListener();
    final TestListener lsnr1 = (TestListener) spi1.getListener();
    ClusterNode node0 = nodes.get(0);
    ClusterNode node1 = nodes.get(1);
    lsnr1.block();
    int msgId = 0;
    for (int j = 0; j < 10; j++) {
        spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0));
        spi1.sendMessage(node0, new GridTestMessage(node1.id(), ++msgId, 0));
    }
    lsnr1.blockLatch.await();
    lsnr1.unblock();
    Thread.sleep(500);
    int errCnt = 0;
    int msgs = 0;
    while (true) {
        try {
            int id = msgId + 1;
            spi0.sendMessage(node1, new GridTestMessage(node0.id(), id, 0));
            msgId++;
            msgs++;
            if (msgs == 10)
                break;
        } catch (IgniteSpiException e) {
            errCnt++;
            if (errCnt > 10)
                fail("Failed to send message: " + e);
        }
    }
    for (int j = 0; j < 10; j++) spi1.sendMessage(node0, new GridTestMessage(node1.id(), ++msgId, 0));
    final int expMsgs = 20;
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return lsnr0.rcvCnt.get() >= expMsgs && lsnr1.rcvCnt.get() >= expMsgs;
        }
    }, awaitForSocketWriteTimeout());
    assertEquals(expMsgs, lsnr0.rcvCnt.get());
    assertEquals(expMsgs, lsnr1.rcvCnt.get());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 79 with GridAbsPredicate

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

the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead2.

/**
 * @throws Exception If failed.
 */
public void testBlockRead2() throws Exception {
    createSpis();
    try {
        final TcpCommunicationSpi spi0 = spis.get(0);
        final TcpCommunicationSpi spi1 = spis.get(1);
        final TestListener lsnr0 = (TestListener) spi0.getListener();
        final TestListener lsnr1 = (TestListener) spi1.getListener();
        final ClusterNode node0 = nodes.get(0);
        final ClusterNode node1 = nodes.get(1);
        final AtomicInteger msgId = new AtomicInteger();
        final AtomicInteger expCnt0 = new AtomicInteger();
        final AtomicInteger expCnt1 = new AtomicInteger();
        // Send message to establish connection.
        spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return lsnr1.rcvCnt.get() >= 1;
            }
        }, 1000);
        expCnt1.incrementAndGet();
        int errCnt = 0;
        for (int i = 0; i < ITERS; i++) {
            log.info("Iteration: " + i);
            try {
                final GridNioSession ses0 = communicationSession(spi0, false);
                final GridNioSession ses1 = communicationSession(spi1, true);
                ses1.pauseReads().get();
                IgniteInternalFuture<?> sndFut = GridTestUtils.runAsync(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        for (int i = 0; i < 6000; i++) {
                            spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
                            expCnt1.incrementAndGet();
                        }
                        return null;
                    }
                });
                // Wait when session is closed because of write timeout.
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return ses0.closeTime() != 0;
                    }
                }, awaitForSocketWriteTimeout());
                assertTrue("Failed to wait for session close", ses0.closeTime() != 0);
                try {
                    ses1.resumeReads().get();
                } catch (IgniteCheckedException ignore) {
                // Can fail if ses1 was closed.
                }
                // Wait when session is closed, then try to open new connection from node1.
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return ses1.closeTime() != 0;
                    }
                }, awaitForSocketWriteTimeout());
                assertTrue("Failed to wait for session close", ses1.closeTime() != 0);
                for (int j = 0; j < 100; j++) {
                    spi1.sendMessage(node0, new GridTestMessage(node1.id(), msgId.incrementAndGet(), 0));
                    expCnt0.incrementAndGet();
                }
                sndFut.get();
                final int expMsgs0 = expCnt0.get();
                final int expMsgs1 = expCnt1.get();
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return lsnr0.rcvCnt.get() >= expMsgs0 && lsnr1.rcvCnt.get() >= expMsgs1;
                    }
                }, 60_000);
                assertEquals(expMsgs0, lsnr0.rcvCnt.get());
                assertEquals(expMsgs1, lsnr1.rcvCnt.get());
            } catch (IgniteCheckedException e) {
                if (e.hasCause(BindException.class)) {
                    errCnt++;
                    if (errCnt > 3) {
                        log.warning("Got exception > 3 times, test fails.");
                        throw e;
                    }
                    if (i < ITERS - 1) {
                        info("Got exception caused by BindException, will retry after delay: " + e);
                        U.sleep(10_000);
                    } else
                        info("Got exception caused by BindException, will ignore: " + e);
                } else {
                    log.warning("Unexpected exception: " + e, e);
                    throw e;
                }
            }
        }
    } finally {
        stopSpis();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) BindException(java.net.BindException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) BindException(java.net.BindException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 80 with GridAbsPredicate

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

the class IgniteTxCachePrimarySyncTest method waitKeyRemoved.

/**
 * @param cacheName Cache name.
 * @param key Cache key.
 * @throws Exception If failed.
 */
private void waitKeyRemoved(final String cacheName, final Object key) throws Exception {
    boolean waitRmv = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (Ignite ignite : G.allGrids()) {
                if (ignite.cache(cacheName).get(key) != null)
                    return false;
            }
            return true;
        }
    }, 5000);
    assertTrue(waitRmv);
}
Also used : 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