Search in sources :

Example 41 with IgniteBiPredicate

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

the class GridP2PComputeWithNestedEntryProcessorTest method scanByCopositeFirstPredicate.

/**
 * @param cache Ignite cache.
 * @throws Exception If failed.
 */
private void scanByCopositeFirstPredicate(IgniteCache cache) throws Exception {
    IgniteBiPredicate firstFilter = (IgniteBiPredicate) TEST_CLASS_LOADER.loadClass(FIRST_FILTER_NAME).getConstructor().newInstance();
    IgniteBiPredicate compositeFilter = (IgniteBiPredicate) TEST_CLASS_LOADER.loadClass(COMPOSITE_FILTER_NAME).getConstructor().newInstance();
    U.invoke(TEST_CLASS_LOADER.loadClass(COMPOSITE_FILTER_NAME), compositeFilter, "addPredicate", firstFilter);
    List list = cache.query(new ScanQuery().setFilter(compositeFilter)).getAll();
    assertEquals(list.size(), ENTRIES / 2);
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ScanQuery(org.apache.ignite.cache.query.ScanQuery) List(java.util.List)

Example 42 with IgniteBiPredicate

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

the class RebalanceAfterResettingLostPartitionTest method testRebalanceAfterPartitionsWereLost.

/**
 * Test to restore lost partitions and rebalance data on working grid with two nodes.
 *
 * @throws Exception if fail.
 */
@SuppressWarnings("unchecked")
@Test
public void testRebalanceAfterPartitionsWereLost() throws Exception {
    startGrids(2);
    grid(0).cluster().active(true);
    for (int j = 0; j < CACHE_SIZE; j++) grid(0).cache(CACHE_NAME).put(j, "Value" + j);
    String g1Name = grid(1).name();
    // Stopping the the second node.
    stopGrid(1);
    // Cleaning the persistence for second node.
    cleanPersistenceDir(g1Name);
    AtomicInteger msgCntr = new AtomicInteger();
    TestRecordingCommunicationSpi.spi(ignite(0)).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

        @Override
        public boolean apply(ClusterNode clusterNode, Message msg) {
            if (msg instanceof GridDhtPartitionSupplyMessage && ((GridCacheGroupIdMessage) msg).groupId() == CU.cacheId(CACHE_NAME)) {
                if (msgCntr.get() > 3)
                    return true;
                else
                    msgCntr.incrementAndGet();
            }
            return false;
        }
    });
    // Starting second node again(with the same consistent id).
    startGrid(1);
    // Waitting for rebalance.
    TestRecordingCommunicationSpi.spi(ignite(0)).waitForBlocked();
    // Killing the first node at the moment of rebalancing.
    stopGrid(0);
    // Returning first node to the cluster.
    IgniteEx g0 = startGrid(0);
    assertTrue(Objects.requireNonNull(grid(0).cachex(CACHE_NAME)).context().topology().localPartitions().stream().allMatch(p -> p.state() == GridDhtPartitionState.LOST));
    // Verify that partition loss is detected.
    assertTrue(Objects.requireNonNull(grid(1).cachex(CACHE_NAME)).context().topology().localPartitions().stream().allMatch(p -> p.state() == GridDhtPartitionState.LOST));
    // Reset lost partitions and wait for PME.
    grid(1).resetLostPartitions(Arrays.asList(CACHE_NAME, "ignite-sys-cache"));
    awaitPartitionMapExchange();
    assertTrue(Objects.requireNonNull(grid(0).cachex(CACHE_NAME)).context().topology().localPartitions().stream().allMatch(p -> p.state() == GridDhtPartitionState.OWNING));
    // Verify that partitions are in owning state.
    assertTrue(Objects.requireNonNull(grid(1).cachex(CACHE_NAME)).context().topology().localPartitions().stream().allMatch(p -> p.state() == GridDhtPartitionState.OWNING));
    // Verify that data was successfully rebalanced.
    for (int i = 0; i < CACHE_SIZE; i++) assertEquals("Value" + i, grid(0).cache(CACHE_NAME).get(i));
    for (int i = 0; i < CACHE_SIZE; i++) assertEquals("Value" + i, grid(1).cache(CACHE_NAME).get(i));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Arrays(java.util.Arrays) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) UUID(java.util.UUID) GridDhtPartitionSupplyMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) Objects(java.util.Objects) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) CU(org.apache.ignite.internal.util.typedef.internal.CU) After(org.junit.After) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) GridCacheGroupIdMessage(org.apache.ignite.internal.processors.cache.GridCacheGroupIdMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) Before(org.junit.Before) GridDhtPartitionSupplyMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage) GridCacheGroupIdMessage(org.apache.ignite.internal.processors.cache.GridCacheGroupIdMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) GridDhtPartitionSupplyMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 43 with IgniteBiPredicate

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

the class GridCacheQueryTransformerSelfTest method testLocalKeepBinaryFiltered.

/**
 * @throws Exception If failed.
 */
@Test
public void testLocalKeepBinaryFiltered() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteBiPredicate<Integer, BinaryObject> filter = new IgniteBiPredicate<Integer, BinaryObject>() {

                    @Override
                    public boolean apply(Integer k, BinaryObject v) {
                        return v.<Integer>field("idx") % 1000 == 0;
                    }
                };
                IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
                        return e.getValue().field("idx");
                    }
                };
                return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<>(filter).setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(5, res.size());
        Collections.sort(res);
        for (int i = 0; i < 5; i++) assertEquals(i * 1000, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) BinaryObject(org.apache.ignite.binary.BinaryObject) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 44 with IgniteBiPredicate

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

the class GridClosureProcessorRemoteTest method testAnonymousUnicastRequest.

/**
 * @throws Exception Thrown in case of failure.
 */
@Test
public void testAnonymousUnicastRequest() throws Exception {
    Ignite g = grid(0);
    assert g.cluster().nodes().size() == NODES_CNT;
    execCntr.set(0);
    ClusterNode rmt = F.first(g.cluster().forRemotes().nodes());
    final ClusterNode loc = g.cluster().localNode();
    compute(g.cluster().forNode(rmt)).run(new CARemote() {

        @Override
        public void apply() {
            message(grid(1).cluster().forNode(loc)).localListen(null, new IgniteBiPredicate<UUID, String>() {

                @Override
                public boolean apply(UUID uuid, String s) {
                    log.info("Received test message [nodeId: " + uuid + ", s=" + s + ']');
                    ignite.countDownLatch("messagesPending", 1, false, true).countDown();
                    execCntr.incrementAndGet();
                    return false;
                }
            });
        }
    });
    message(g.cluster().forNode(rmt)).send(null, "TESTING...");
    assertTrue(g.countDownLatch("messagesPending", 1, false, true).await(2000));
    assertEquals(0, execCntr.get());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest)

Example 45 with IgniteBiPredicate

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

the class IgniteClientReconnectApiExceptionTest method igniteOperationsTest.

/**
 * @throws Exception If failed.
 */
private void igniteOperationsTest() throws Exception {
    final Ignite client = startClientGrid(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;
        }
    })));
}
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)

Aggregations

IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)57 Ignite (org.apache.ignite.Ignite)36 UUID (java.util.UUID)28 IgniteCache (org.apache.ignite.IgniteCache)22 Test (org.junit.Test)22 List (java.util.List)21 Collection (java.util.Collection)18 Map (java.util.Map)18 HashMap (java.util.HashMap)17 CountDownLatch (java.util.concurrent.CountDownLatch)17 ClusterNode (org.apache.ignite.cluster.ClusterNode)17 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)17 ArrayList (java.util.ArrayList)16 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)16 ScanQuery (org.apache.ignite.cache.query.ScanQuery)15 IgniteEx (org.apache.ignite.internal.IgniteEx)15 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 IgniteException (org.apache.ignite.IgniteException)13 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)13