Search in sources :

Example 56 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgniteClientReconnectApiExceptionTest method igniteOperationsTest.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
public void igniteOperationsTest() throws Exception {
    clientMode = true;
    final Ignite client = startGrid(serverCount());
    final IgniteCache<Object, Object> dfltCache = client.cache(DEFAULT_CACHE_NAME);
    final CountDownLatch recvLatch = new CountDownLatch(1);
    assertNotNull(dfltCache);
    doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check compute.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.compute();
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.compute();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCompute comp = (IgniteCompute) o;
            Collection<UUID> uuids = comp.broadcast(new IgniteCallable<UUID>() {

                @IgniteInstanceResource
                private Ignite ignite;

                @Override
                public UUID call() throws Exception {
                    return ignite.cluster().localNode().id();
                }
            });
            assertFalse(uuids.isEmpty());
            for (UUID uuid : uuids) assertNotNull(uuid);
            return true;
        }
    }), // Check ping node.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.cluster().pingNode(new UUID(0, 0));
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.cluster().pingNode(new UUID(0, 0));
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            Boolean pingNode = (Boolean) o;
            assertFalse(pingNode);
            return true;
        }
    }), // Check register remote listener.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.events().remoteListen(null, new IgnitePredicate<Event>() {

                    @Override
                    public boolean apply(Event event) {
                        return true;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.events().remoteListen(null, new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event event) {
                    return true;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            UUID remoteId = (UUID) o;
            assertNotNull(remoteId);
            client.events().stopRemoteListen(remoteId);
            return true;
        }
    }), // Check message operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.message().remoteListen(null, new IgniteBiPredicate<UUID, Object>() {

                    @Override
                    public boolean apply(UUID uuid, Object o) {
                        if (o.equals("Test message."))
                            recvLatch.countDown();
                        return true;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.message().remoteListen(null, new IgniteBiPredicate<UUID, Object>() {

                @Override
                public boolean apply(UUID uuid, Object o) {
                    if (o.equals("Test message."))
                        recvLatch.countDown();
                    return true;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteMessaging msg = client.message();
            msg.send(null, "Test message.");
            try {
                assertTrue(recvLatch.await(2, SECONDS));
            } catch (InterruptedException ignored) {
                fail("Message wasn't received.");
            }
            return true;
        }
    }), // Check executor.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.executorService().submit(new Callable<Integer>() {

                    @Override
                    public Integer call() throws Exception {
                        return 42;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.executorService().submit(new Callable<Integer>() {

                @Override
                public Integer call() throws Exception {
                    return 42;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            Future<Integer> fut = (Future<Integer>) o;
            try {
                assertEquals(42, (int) fut.get());
            } catch (Exception ignored) {
                fail("Failed submit task.");
            }
            return true;
        }
    })));
    clientMode = false;
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteMessaging(org.apache.ignite.IgniteMessaging) IgniteCallable(org.apache.ignite.lang.IgniteCallable) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) Future(java.util.concurrent.Future) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) IgniteCompute(org.apache.ignite.IgniteCompute)

Example 57 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgniteClientReconnectContinuousProcessorTest method continuousQueryReconnect.

/**
 * @param client Client.
 * @param clientCache Client cache.
 * @param lsnr Continuous query listener.
 * @throws Exception If failed.
 */
private void continuousQueryReconnect(Ignite client, IgniteCache<Object, Object> clientCache, CacheEventListener lsnr) throws Exception {
    Ignite srv = clientRouter(client);
    TestTcpDiscoverySpi srvSpi = spi(srv);
    final CountDownLatch reconnectLatch = new CountDownLatch(1);
    IgnitePredicate<Event> p = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    };
    client.events().localListen(p, EVT_CLIENT_NODE_RECONNECTED);
    srvSpi.failNode(client.cluster().localNode().id(), null);
    waitReconnectEvent(reconnectLatch);
    client.events().stopLocalListen(p);
    lsnr.latch = new CountDownLatch(1);
    clientCache.put(1, 1);
    assertTrue(lsnr.latch.await(5000, MILLISECONDS));
    lsnr.latch = new CountDownLatch(1);
    srv.cache(DEFAULT_CACHE_NAME).put(2, 2);
    assertTrue(lsnr.latch.await(5000, MILLISECONDS));
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 58 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class GridDiscoverySelfTest method testDiscoveryListener.

/**
 * @throws Exception If failed.
 */
public void testDiscoveryListener() throws Exception {
    ClusterNode node = ignite.cluster().localNode();
    assert node != null;
    final AtomicInteger cnt = new AtomicInteger();
    /**
     * Joined nodes counter.
     */
    final CountDownLatch joinedCnt = new CountDownLatch(NODES_CNT);
    /**
     * Left nodes counter.
     */
    final CountDownLatch leftCnt = new CountDownLatch(NODES_CNT);
    IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (EVT_NODE_JOINED == evt.type()) {
                cnt.incrementAndGet();
                joinedCnt.countDown();
            } else if (EVT_NODE_LEFT == evt.type()) {
                int i = cnt.decrementAndGet();
                assert i >= 0;
                leftCnt.countDown();
            } else
                assert false;
            return true;
        }
    };
    ignite.events().localListen(lsnr, EVT_NODE_LEFT, EVT_NODE_JOINED);
    try {
        for (int i = 0; i < NODES_CNT; i++) startGrid(i);
        joinedCnt.await(MAX_TIMEOUT_IN_MINS, MINUTES);
        assert cnt.get() == NODES_CNT;
        for (int i = 0; i < NODES_CNT; i++) stopGrid(i);
        leftCnt.await(MAX_TIMEOUT_IN_MINS, MINUTES);
        assert cnt.get() == 0;
        ignite.events().stopLocalListen(lsnr);
        assert cnt.get() == 0;
    } finally {
        for (int i = 0; i < NODES_CNT; i++) stopAndCancelGrid(i);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 59 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgniteCacheConfigVariationsFullApiTest method checkGetOutTx.

/**
 * @param async Use async API.
 * @param oldAsync Uase old style async API.
 * @throws Exception If failed.
 */
private void checkGetOutTx(boolean async, boolean oldAsync) throws Exception {
    final AtomicInteger lockEvtCnt = new AtomicInteger();
    IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            lockEvtCnt.incrementAndGet();
            return true;
        }
    };
    try {
        IgniteCache<String, Integer> cache = grid(0).cache(cacheName());
        List<String> keys = primaryKeysForCache(0, 2, 1);
        assertEquals(2, keys.size());
        cache.put(keys.get(0), 0);
        cache.put(keys.get(1), 1);
        grid(0).events().localListen(lsnr, EVT_CACHE_OBJECT_LOCKED, EVT_CACHE_OBJECT_UNLOCKED);
        if (async && oldAsync)
            cache = cache.withAsync();
        try (Transaction tx = transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            Integer val0;
            if (async) {
                if (oldAsync) {
                    cache.get(keys.get(0));
                    val0 = cache.<Integer>future().get();
                } else
                    val0 = cache.getAsync(keys.get(0)).get();
            } else
                val0 = cache.get(keys.get(0));
            assertEquals(0, val0.intValue());
            Map<String, Integer> allOutTx;
            if (async) {
                if (oldAsync) {
                    cache.getAllOutTx(F.asSet(keys.get(1)));
                    allOutTx = cache.<Map<String, Integer>>future().get();
                } else
                    allOutTx = cache.getAllOutTxAsync(F.asSet(keys.get(1))).get();
            } else
                allOutTx = cache.getAllOutTx(F.asSet(keys.get(1)));
            assertEquals(1, allOutTx.size());
            assertTrue(allOutTx.containsKey(keys.get(1)));
            assertEquals(1, allOutTx.get(keys.get(1)).intValue());
        }
        assertTrue(GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                info("Lock event count: " + lockEvtCnt.get());
                if (atomicityMode() == ATOMIC)
                    return lockEvtCnt.get() == 0;
                if (cacheMode() == PARTITIONED && nearEnabled()) {
                    if (!grid(0).configuration().isClientMode())
                        return lockEvtCnt.get() == 4;
                }
                return lockEvtCnt.get() == 2;
            }
        }, 15000));
    } finally {
        grid(0).events().stopLocalListen(lsnr, EVT_CACHE_OBJECT_LOCKED, EVT_CACHE_OBJECT_UNLOCKED);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 60 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgniteDynamicCacheStartSelfTest method testEvents.

/**
 * @throws Exception If failed.
 */
public void testEvents() throws Exception {
    CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    cfg.setName(DYNAMIC_CACHE_NAME);
    cfg.setCacheMode(CacheMode.REPLICATED);
    // This cache will not fire any cache events.
    CacheConfiguration cfgEvtsDisabled = new CacheConfiguration(cfg);
    cfgEvtsDisabled.setName("DynamicCacheEvtsDisabled");
    cfgEvtsDisabled.setEventsDisabled(true);
    final CountDownLatch[] starts = new CountDownLatch[nodeCount()];
    final CountDownLatch[] stops = new CountDownLatch[nodeCount()];
    IgnitePredicate[] lsnrs = new IgnitePredicate[nodeCount()];
    for (int i = 0; i < nodeCount(); i++) {
        final int idx = i;
        starts[i] = new CountDownLatch(1);
        stops[i] = new CountDownLatch(1);
        lsnrs[i] = new IgnitePredicate<CacheEvent>() {

            @Override
            public boolean apply(CacheEvent e) {
                switch(e.type()) {
                    case EventType.EVT_CACHE_STARTED:
                        starts[idx].countDown();
                        break;
                    case EventType.EVT_CACHE_STOPPED:
                        stops[idx].countDown();
                        break;
                    default:
                        assert false;
                }
                assertEquals(DYNAMIC_CACHE_NAME, e.cacheName());
                return true;
            }
        };
        ignite(i).events().localListen(lsnrs[i], EventType.EVTS_CACHE_LIFECYCLE);
    }
    IgniteCache<Object, Object> cacheEvtsDisabled = ignite(0).createCache(cfgEvtsDisabled);
    IgniteCache<Object, Object> cache = ignite(0).createCache(cfg);
    try {
        for (CountDownLatch start : starts) start.await();
    } finally {
        cacheEvtsDisabled.destroy();
        cache.destroy();
    }
    for (CountDownLatch stop : stops) stop.await();
    for (int i = 0; i < nodeCount(); i++) ignite(i).events().stopLocalListen(lsnrs[i]);
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) CountDownLatch(java.util.concurrent.CountDownLatch) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)81 Event (org.apache.ignite.events.Event)44 CountDownLatch (java.util.concurrent.CountDownLatch)35 Ignite (org.apache.ignite.Ignite)32 UUID (java.util.UUID)20 ClusterNode (org.apache.ignite.cluster.ClusterNode)18 ArrayList (java.util.ArrayList)15 Ignition (org.apache.ignite.Ignition)10 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)10 Collection (java.util.Collection)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 IgfsEvent (org.apache.ignite.events.IgfsEvent)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)8 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)8 CacheEvent (org.apache.ignite.events.CacheEvent)7 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)7 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7