Search in sources :

Example 26 with CI1

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

the class IgniteClientReconnectCacheTest method testReconnectOperationInProgress.

/**
 * @throws Exception If failed.
 */
@Test
public void testReconnectOperationInProgress() throws Exception {
    IgniteEx client = startClientGrid(SRV_CNT);
    client.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED)
                info("Client disconnected: " + evt);
            else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED)
                info("Client reconnected: " + evt);
            return true;
        }
    }, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
    IgniteInClosure<IgniteCache<Object, Object>> putOp = new CI1<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            while (true) {
                try {
                    cache.put(1, 1);
                    break;
                } catch (Exception e) {
                    if (e.getCause() instanceof IgniteClientDisconnectedException)
                        throw e;
                    else
                        MvccFeatureChecker.assertMvccWriteConflict(e);
                }
            }
        }
    };
    IgniteInClosure<IgniteCache<Object, Object>> getOp = new CI1<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            cache.get(1);
        }
    };
    IgniteInClosure<IgniteCache<Object, Object>> getAllOp = new CI1<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            cache.getAll(F.asSet(1, 2));
        }
    };
    int cnt = 0;
    for (CacheAtomicityMode atomicityMode : CacheAtomicityMode.values()) {
        for (CacheWriteSynchronizationMode syncMode : CacheWriteSynchronizationMode.values()) {
            CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
            ccfg.setAtomicityMode(atomicityMode);
            ccfg.setName("cache-" + cnt++);
            ccfg.setWriteSynchronizationMode(syncMode);
            if (syncMode != FULL_ASYNC) {
                Class<?> cls = (ccfg.getAtomicityMode() == ATOMIC) ? GridNearAtomicUpdateResponse.class : GridNearTxPrepareResponse.class;
                log.info("Test cache put [atomicity=" + atomicityMode + ", syncMode=" + syncMode + ']');
                checkOperationInProgressFails(client, ccfg, cls, putOp);
                client.destroyCache(ccfg.getName());
            }
            log.info("Test cache get [atomicity=" + atomicityMode + ", syncMode=" + syncMode + ']');
            checkOperationInProgressFails(client, ccfg, GridNearSingleGetResponse.class, getOp);
            checkOperationInProgressFails(client, ccfg, GridNearGetResponse.class, getAllOp);
            client.destroyCache(ccfg.getName());
        }
    }
}
Also used : CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCache(org.apache.ignite.IgniteCache) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) CacheException(javax.cache.CacheException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test)

Example 27 with CI1

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

the class GridTaskListenerSelfTest method testGridTaskListener.

/**
 * Checks that GridTaskListener is only called once per task.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "BusyWait" })
@Test
public void testGridTaskListener() throws Exception {
    final AtomicInteger cnt = new AtomicInteger(0);
    IgniteInClosure<IgniteFuture<?>> lsnr = new CI1<IgniteFuture<?>>() {

        @Override
        public void apply(IgniteFuture<?> fut) {
            assert fut != null;
            cnt.incrementAndGet();
        }
    };
    Ignite ignite = G.ignite(getTestIgniteInstanceName());
    assert ignite != null;
    ignite.compute().localDeployTask(TestTask.class, TestTask.class.getClassLoader());
    ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), TestTask.class.getName(), null);
    fut.listen(lsnr);
    fut.get();
    while (cnt.get() == 0) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            error("Got interrupted while sleep.", e);
            break;
        }
    }
    assert cnt.get() == 1 : "Unexpected GridTaskListener apply count [count=" + cnt.get() + ", expected=1]";
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CI1(org.apache.ignite.internal.util.typedef.CI1) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest)

Example 28 with CI1

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

the class IgniteAbstractTxSuspendResumeTest method testMultiTxSuspendResume.

/**
 * Test for starting and suspending transactions, and then resuming and committing in another thread.
 *
 * @throws Exception If failed.
 */
@Test
public void testMultiTxSuspendResume() throws Exception {
    executeTestForAllCaches(new CI2Exc<Ignite, IgniteCache<Integer, Integer>>() {

        @Override
        public void applyx(Ignite ignite, final IgniteCache<Integer, Integer> cache) throws Exception {
            for (TransactionIsolation isolation : TransactionIsolation.values()) {
                final List<Transaction> clientTxs = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    Transaction tx = ignite.transactions().txStart(transactionConcurrency(), isolation);
                    cache.put(i, i);
                    tx.suspend();
                    clientTxs.add(tx);
                }
                GridTestUtils.runMultiThreaded(new CI1<Integer>() {

                    @Override
                    public void apply(Integer idx) {
                        Transaction tx = clientTxs.get(idx);
                        assertEquals(SUSPENDED, tx.state());
                        tx.resume();
                        assertEquals(ACTIVE, tx.state());
                        tx.commit();
                    }
                }, 10, "th-suspend");
                for (int i = 0; i < 10; i++) assertEquals(i, (int) cache.get(i));
                cache.removeAll();
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) ArrayList(java.util.ArrayList) List(java.util.List) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteException(org.apache.ignite.IgniteException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 29 with CI1

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

the class IgniteTcpCommunicationRecoveryAckClosureSelfTest method checkOverflow.

/**
 * @throws Exception If failed.
 */
private void checkOverflow() throws Exception {
    TcpCommunicationSpi spi0 = spis.get(0);
    TcpCommunicationSpi spi1 = spis.get(1);
    ClusterNode node0 = nodes.get(0);
    ClusterNode node1 = nodes.get(1);
    // Await time to close the session by queue overflow.
    final int awaitTime = 5_000;
    // Check that session will not be closed by idle timeout because expected close by queue overflow.
    assertTrue(spi0.getIdleConnectionTimeout() > awaitTime);
    final GridNioServer<?> srv1 = ((GridNioServerWrapper) U.field(spi1, "nioSrvWrapper")).nio();
    // For prevent session close by write timeout.
    srv1.writeTimeout(60_000);
    final AtomicInteger ackMsgs = new AtomicInteger(0);
    IgniteInClosure<IgniteException> ackC = new CI1<IgniteException>() {

        @Override
        public void apply(IgniteException o) {
            assert o == null;
            ackMsgs.incrementAndGet();
        }
    };
    int msgId = 0;
    // Send message to establish connection.
    spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
    int sentMsgs = 1;
    // Prevent node1 from send
    GridTestUtils.setFieldValue(srv1, "skipWrite", true);
    final GridNioSession ses0 = communicationSession(spi0);
    int queueLimit = ses0.outRecoveryDescriptor().queueLimit();
    for (int i = sentMsgs; i < queueLimit; i++) {
        try {
            spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
            sentMsgs++;
        } catch (IgniteSpiException e) {
            log.info("Send error [err=" + e + ", sentMsgs=" + sentMsgs + ']');
            break;
        }
    }
    // Wait when session is closed because of queue overflow.
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return ses0.closeTime() != 0;
        }
    }, awaitTime);
    assertTrue("Failed to wait for session close", ses0.closeTime() != 0);
    GridTestUtils.setFieldValue(srv1, "skipWrite", false);
    // It to gain all acks since acks have batch nature.
    int cnt = 100 - sentMsgs % spi0.getAckSendThreshold();
    for (int i = 0; i < cnt; i++) spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
    final int expMsgs = sentMsgs + cnt;
    final TestListener lsnr = (TestListener) spi1.getListener();
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return lsnr.rcvCnt.get() >= expMsgs;
        }
    }, 5000);
    assertEquals(expMsgs, lsnr.rcvCnt.get());
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return expMsgs == ackMsgs.get();
        }
    }, 5000);
    assertEquals(expMsgs, ackMsgs.get());
}
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) GridNioServerWrapper(org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper) CI1(org.apache.ignite.internal.util.typedef.CI1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 30 with CI1

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

the class IgniteTcpCommunicationRecoveryAckClosureSelfTest method checkAck.

/**
 * @param ackCnt Recovery acknowledgement count.
 * @param idleTimeout Idle connection timeout.
 * @param msgPerIter Messages per iteration.
 * @throws Exception If failed.
 */
private void checkAck(int ackCnt, int idleTimeout, int msgPerIter) throws Exception {
    createSpis(ackCnt, idleTimeout, TcpCommunicationSpi.DFLT_MSG_QUEUE_LIMIT);
    try {
        TcpCommunicationSpi spi0 = spis.get(0);
        TcpCommunicationSpi spi1 = spis.get(1);
        ClusterNode node0 = nodes.get(0);
        ClusterNode node1 = nodes.get(1);
        int msgId = 0;
        int expMsgs = 0;
        long totAcked = 0;
        for (int i = 0; i < 5; i++) {
            info("Iteration: " + i);
            final AtomicInteger ackMsgs = new AtomicInteger(0);
            IgniteInClosure<IgniteException> ackC = new CI1<IgniteException>() {

                @Override
                public void apply(IgniteException o) {
                    assert o == null;
                    ackMsgs.incrementAndGet();
                }
            };
            for (int j = 0; j < msgPerIter; j++) {
                spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
                spi1.sendMessage(node0, new GridTestMessage(node1.id(), ++msgId, 0), ackC);
                if (j == 0) {
                    final TestListener lsnr0 = (TestListener) spi0.getListener();
                    final TestListener lsnr1 = (TestListener) spi1.getListener();
                    GridTestUtils.waitForCondition(new GridAbsPredicate() {

                        @Override
                        public boolean apply() {
                            return lsnr0.rcvCnt.get() >= 1 && lsnr1.rcvCnt.get() >= 1;
                        }
                    }, 1000);
                }
            }
            expMsgs += msgPerIter;
            final long totAcked0 = totAcked;
            for (TcpCommunicationSpi spi : spis) {
                GridNioServer<?> srv = ((GridNioServerWrapper) U.field(spi, "nioSrvWrapper")).nio();
                Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
                assertFalse(sessions.isEmpty());
                boolean found = false;
                for (GridNioSession ses : sessions) {
                    final GridNioRecoveryDescriptor recoveryDesc = ses.outRecoveryDescriptor();
                    if (recoveryDesc != null) {
                        found = true;
                        GridTestUtils.waitForCondition(new GridAbsPredicate() {

                            @Override
                            public boolean apply() {
                                long acked = GridTestUtils.getFieldValue(recoveryDesc, "acked");
                                return acked > totAcked0;
                            }
                        }, 5000);
                        GridTestUtils.waitForCondition(new GridAbsPredicate() {

                            @Override
                            public boolean apply() {
                                return recoveryDesc.messagesRequests().isEmpty();
                            }
                        }, 10_000);
                        assertEquals("Unexpected messages: " + recoveryDesc.messagesRequests(), 0, recoveryDesc.messagesRequests().size());
                        break;
                    }
                }
                assertTrue(found);
            }
            final int expMsgs0 = expMsgs;
            for (TcpCommunicationSpi spi : spis) {
                final TestListener lsnr = (TestListener) spi.getListener();
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return lsnr.rcvCnt.get() >= expMsgs0;
                    }
                }, 5000);
                assertEquals(expMsgs, lsnr.rcvCnt.get());
            }
            assertEquals(msgPerIter * 2, ackMsgs.get());
            totAcked += msgPerIter;
        }
    } 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) CI1(org.apache.ignite.internal.util.typedef.CI1) GridNioServerWrapper(org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)

Aggregations

CI1 (org.apache.ignite.internal.util.typedef.CI1)32 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)11 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 Test (org.junit.Test)11 IgniteFuture (org.apache.ignite.lang.IgniteFuture)9 ArrayList (java.util.ArrayList)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IgniteException (org.apache.ignite.IgniteException)7 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)7 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 Collection (java.util.Collection)6 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)6 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)6 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)6 Nullable (org.jetbrains.annotations.Nullable)6 List (java.util.List)5 UUID (java.util.UUID)5 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)5 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)5