Search in sources :

Example 6 with IgniteBiPredicate

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

the class IgniteClientReconnectCacheQueriesFailoverTest method testReconnectScanQuery.

/**
 * @throws Exception If failed.
 */
public void testReconnectScanQuery() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Person> cache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(cache);
    final Affinity<Integer> aff = client.affinity(DEFAULT_CACHE_NAME);
    final Map<Integer, Integer> partMap = new HashMap<>();
    for (int i = 0; i < aff.partitions(); i++) partMap.put(i, 0);
    for (int i = 0; i <= 10_000; i++) {
        Integer part = aff.partition(i);
        Integer size = partMap.get(part);
        partMap.put(part, size + 1);
    }
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ScanQuery<Integer, Person> qry = new ScanQuery<>(new IgniteBiPredicate<Integer, Person>() {

                @Override
                public boolean apply(Integer key, Person val) {
                    return val.getId() % 2 == 1;
                }
            });
            assertEquals(5000, cache.query(qry).getAll().size());
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            Integer part = rnd.nextInt(0, aff.partitions());
            qry = new ScanQuery<>(part);
            assertEquals((int) partMap.get(part), cache.query(qry).getAll().size());
            return null;
        }
    });
}
Also used : HashMap(java.util.HashMap) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CacheException(javax.cache.CacheException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 7 with IgniteBiPredicate

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

the class IgniteMqttStreamerTest method subscribeToPutEvents.

/**
 * @param expect Expected count.
 * @return Latch to be counted down in listener.
 */
private CountDownLatch subscribeToPutEvents(int expect) {
    Ignite ignite = grid();
    // Listen to cache PUT events and expect as many as messages as test data items
    final CountDownLatch latch = new CountDownLatch(expect);
    IgniteBiPredicate<UUID, CacheEvent> cb = new IgniteBiPredicate<UUID, CacheEvent>() {

        @Override
        public boolean apply(UUID uuid, CacheEvent evt) {
            latch.countDown();
            return true;
        }
    };
    remoteLsnr = ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(cb, null, EVT_CACHE_OBJECT_PUT);
    return latch;
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 8 with IgniteBiPredicate

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

the class IgniteH2Indexing method backupFilter.

/** {@inheritDoc} */
@Override
public IndexingQueryFilter backupFilter(@Nullable final AffinityTopologyVersion topVer, @Nullable final int[] parts) {
    final AffinityTopologyVersion topVer0 = topVer != null ? topVer : AffinityTopologyVersion.NONE;
    return new IndexingQueryFilter() {

        @Nullable
        @Override
        public <K, V> IgniteBiPredicate<K, V> forCache(String cacheName) {
            final GridCacheAdapter<Object, Object> cache = ctx.cache().internalCache(cacheName);
            if (cache.context().isReplicated())
                return null;
            final GridCacheAffinityManager aff = cache.context().affinity();
            if (parts != null) {
                if (parts.length < 64) {
                    // Fast scan for small arrays.
                    return new IgniteBiPredicate<K, V>() {

                        @Override
                        public boolean apply(K k, V v) {
                            int p = aff.partition(k);
                            for (int p0 : parts) {
                                if (p0 == p)
                                    return true;
                                if (// Array is sorted.
                                p0 > p)
                                    return false;
                            }
                            return false;
                        }
                    };
                }
                return new IgniteBiPredicate<K, V>() {

                    @Override
                    public boolean apply(K k, V v) {
                        int p = aff.partition(k);
                        return Arrays.binarySearch(parts, p) >= 0;
                    }
                };
            }
            final ClusterNode locNode = ctx.discovery().localNode();
            return new IgniteBiPredicate<K, V>() {

                @Override
                public boolean apply(K k, V v) {
                    return aff.primaryByKey(locNode, k, topVer0);
                }
            };
        }

        @Override
        public boolean isValueRequired() {
            return false;
        }

        @Override
        public String toString() {
            return "IndexingQueryFilter [ver=" + topVer + ']';
        }
    };
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheAffinityManager(org.apache.ignite.internal.processors.cache.GridCacheAffinityManager) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString)

Example 9 with IgniteBiPredicate

use of org.apache.ignite.lang.IgniteBiPredicate 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 10 with IgniteBiPredicate

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

the class CacheLateAffinityAssignmentTest method testNoForceKeysRequests.

/**
 * @throws Exception If failed.
 */
public void testNoForceKeysRequests() throws Exception {
    fail("https://issues.apache.org/jira/browse/IGNITE-5510");
    cacheC = new IgniteClosure<String, CacheConfiguration[]>() {

        @Override
        public CacheConfiguration[] apply(String s) {
            return null;
        }
    };
    final AtomicBoolean fail = new AtomicBoolean();
    spiC = new IgniteClosure<String, TestRecordingCommunicationSpi>() {

        @Override
        public TestRecordingCommunicationSpi apply(String s) {
            TestRecordingCommunicationSpi spi = new TestRecordingCommunicationSpi();
            spi.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

                @Override
                public boolean apply(ClusterNode node, Message msg) {
                    if (msg instanceof GridDhtForceKeysRequest || msg instanceof GridDhtForceKeysResponse) {
                        fail.set(true);
                        U.dumpStack(log, "Unexpected message: " + msg);
                    }
                    return false;
                }
            });
            return spi;
        }
    };
    final int SRVS = 3;
    for (int i = 0; i < SRVS; i++) startGrid(i);
    client = true;
    startGrid(SRVS);
    client = false;
    final List<CacheConfiguration> ccfgs = new ArrayList<>();
    ccfgs.add(cacheConfiguration("tc1", TRANSACTIONAL, 0));
    ccfgs.add(cacheConfiguration("tc2", TRANSACTIONAL, 1));
    ccfgs.add(cacheConfiguration("tc3", TRANSACTIONAL, 2));
    for (CacheConfiguration ccfg : ccfgs) ignite(0).createCache(ccfg);
    final int NODES = SRVS + 1;
    final AtomicInteger nodeIdx = new AtomicInteger();
    final long stopTime = System.currentTimeMillis() + 60_000;
    IgniteInternalFuture<?> updateFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            int idx = nodeIdx.getAndIncrement();
            Ignite node = grid(idx);
            List<IgniteCache<Object, Object>> caches = new ArrayList<>();
            for (CacheConfiguration ccfg : ccfgs) caches.add(node.cache(ccfg.getName()));
            while (!fail.get() && System.currentTimeMillis() < stopTime) {
                for (IgniteCache<Object, Object> cache : caches) cacheOperations(cache);
            }
            return null;
        }
    }, NODES, "update-thread");
    IgniteInternalFuture<?> srvRestartFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!fail.get() && System.currentTimeMillis() < stopTime) {
                Ignite node = startGrid(NODES);
                List<IgniteCache<Object, Object>> caches = new ArrayList<>();
                for (CacheConfiguration ccfg : ccfgs) caches.add(node.cache(ccfg.getName()));
                for (int i = 0; i < 2; i++) {
                    for (IgniteCache<Object, Object> cache : caches) cacheOperations(cache);
                }
                U.sleep(500);
                stopGrid(NODES);
                U.sleep(500);
            }
            return null;
        }
    }, "srv-restart");
    srvRestartFut.get();
    updateFut.get();
    assertFalse("Unexpected messages.", fail.get());
}
Also used : GridDhtForceKeysResponse(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtForceKeysResponse) GridDhtPartitionSupplyMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) GridDhtPartitionsSingleMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsSingleMessage) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) CacheAffinityChangeMessage(org.apache.ignite.internal.processors.cache.CacheAffinityChangeMessage) ArrayList(java.util.ArrayList) GridDhtForceKeysRequest(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtForceKeysRequest) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteCache(org.apache.ignite.IgniteCache) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)23 Ignite (org.apache.ignite.Ignite)18 UUID (java.util.UUID)12 CountDownLatch (java.util.concurrent.CountDownLatch)9 CacheEvent (org.apache.ignite.events.CacheEvent)8 IgniteCache (org.apache.ignite.IgniteCache)6 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)5 Cache (javax.cache.Cache)4 IgniteException (org.apache.ignite.IgniteException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 ScanQuery (org.apache.ignite.cache.query.ScanQuery)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 Map (java.util.Map)2 CacheException (javax.cache.CacheException)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 Ignition (org.apache.ignite.Ignition)2