Search in sources :

Example 36 with IgniteBiPredicate

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

the class CacheMvccTransactionsTest method getAllByReadMode.

/**
 * Reads value from cache for the given key using given read mode.
 *
 * // TODO IGNITE-6938 remove inTx flag
 * // TODO IGNITE-6739 add SQL-get support "select _key, _val from cache where _key in ... keySet"
 * @param inTx Flag whether current read is inside transaction.
 * This is because reads can't see writes made in current transaction.
 * @param cache Cache.
 * @param keys Key.
 * @param readMode Read mode.
 * @return Value.
 */
private Map getAllByReadMode(boolean inTx, IgniteCache cache, Set keys, ReadMode readMode) {
    // TODO Remove in IGNITE-6938
    if (inTx)
        readMode = GET;
    switch(readMode) {
        case GET:
            return cache.getAll(keys);
        case SCAN:
            Map res = (Map) cache.query(new ScanQuery(new IgniteBiPredicate() {

                @Override
                public boolean apply(Object k, Object v) {
                    return keys.contains(k);
                }
            })).getAll().stream().collect(Collectors.toMap(v -> ((IgniteBiTuple) v).getKey(), v -> ((IgniteBiTuple) v).getValue()));
            assertTrue(res.size() <= keys.size());
            return res;
        default:
            throw new IgniteException("Unsupported read mode: " + readMode);
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteException(org.apache.ignite.IgniteException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 37 with IgniteBiPredicate

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

the class CacheMvccTransactionsTest method getByReadMode.

/**
 * Reads value from cache for the given key using given read mode.
 *
 * // TODO IGNITE-6938 remove inTx flag
 * // TODO IGNITE-6739 add SQL-get support "select _key, _val from cache where _key = key"
 * @param inTx Flag whether current read is inside transaction.
 * This is because reads can't see writes made in current transaction.
 * @param cache Cache.
 * @param key Key.
 * @param readMode Read mode.
 * @return Value.
 */
private Object getByReadMode(boolean inTx, IgniteCache cache, final Object key, ReadMode readMode) {
    // TODO Remove in IGNITE-6938
    if (inTx)
        readMode = GET;
    switch(readMode) {
        case GET:
            return cache.get(key);
        case SCAN:
            List res = cache.query(new ScanQuery(new IgniteBiPredicate() {

                @Override
                public boolean apply(Object k, Object v) {
                    return k.equals(key);
                }
            })).getAll();
            assertTrue(res.size() <= 1);
            return res.isEmpty() ? null : ((IgniteBiTuple) res.get(0)).getValue();
        default:
            throw new IgniteException("Unsupported read mode: " + readMode);
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteException(org.apache.ignite.IgniteException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) List(java.util.List) ArrayList(java.util.ArrayList) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 38 with IgniteBiPredicate

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

the class IgnitePdsConsistencyOnDelayedPartitionOwning method checkConsistencyNodeLeft.

/**
 */
@Test
public void checkConsistencyNodeLeft() throws Exception {
    IgniteEx crd = (IgniteEx) startGridsMultiThreaded(4);
    crd.cluster().active(true);
    for (int i = 0; i < PARTS; i++) crd.cache(DEFAULT_CACHE_NAME).put(i, i);
    forceCheckpoint();
    stopGrid(1);
    for (int i = 0; i < PARTS; i++) crd.cache(DEFAULT_CACHE_NAME).put(i, i + 1);
    // Block supply messages from all owners.
    TestRecordingCommunicationSpi spi0 = TestRecordingCommunicationSpi.spi(grid(0));
    TestRecordingCommunicationSpi spi2 = TestRecordingCommunicationSpi.spi(grid(2));
    TestRecordingCommunicationSpi spi3 = TestRecordingCommunicationSpi.spi(grid(3));
    IgniteBiPredicate<ClusterNode, Message> pred = new IgniteBiPredicate<ClusterNode, Message>() {

        @Override
        public boolean apply(ClusterNode clusterNode, Message msg) {
            return msg instanceof GridDhtPartitionSupplyMessage;
        }
    };
    spi0.blockMessages(pred);
    spi2.blockMessages(pred);
    spi3.blockMessages(pred);
    GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            startGrid(1);
            return null;
        }
    });
    spi0.waitForBlocked();
    spi2.waitForBlocked();
    spi3.waitForBlocked();
    spi0.stopBlock();
    spi2.stopBlock();
    CountDownLatch topInitLatch = new CountDownLatch(1);
    CountDownLatch enableDurabilityCPStartLatch = new CountDownLatch(1);
    CountDownLatch delayedOnwningLatch = new CountDownLatch(1);
    GridCacheDatabaseSharedManager dbMgr = (GridCacheDatabaseSharedManager) grid(1).context().cache().context().database();
    dbMgr.addCheckpointListener(new CheckpointListener() {

        @Override
        public void onMarkCheckpointBegin(Context ctx) throws IgniteCheckedException {
        // No-op.
        }

        @Override
        public void onCheckpointBegin(Context ctx) throws IgniteCheckedException {
        // No-op.
        }

        @Override
        public void beforeCheckpointBegin(Context ctx) throws IgniteCheckedException {
            String reason = ctx.progress().reason();
            String reason0 = WalStateManager.reason(cacheId(DEFAULT_CACHE_NAME), new AffinityTopologyVersion(6, 0));
            if (reason != null && reason.equals(reason0)) {
                enableDurabilityCPStartLatch.countDown();
                try {
                    assertTrue(U.await(delayedOnwningLatch, 10_000, TimeUnit.MILLISECONDS));
                } catch (IgniteInterruptedCheckedException e) {
                    fail(X.getFullStackTrace(e));
                }
            }
        }
    });
    TestRecordingCommunicationSpi.spi(grid(1)).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

        @Override
        public boolean apply(ClusterNode clusterNode, Message msg) {
            if (msg instanceof GridDhtPartitionDemandMessage) {
                GridDhtPartitionDemandMessage msg0 = (GridDhtPartitionDemandMessage) msg;
                return msg0.topologyVersion().equals(new AffinityTopologyVersion(7, 0));
            }
            return false;
        }
    });
    grid(1).context().cache().context().exchange().registerExchangeAwareComponent(new PartitionsExchangeAware() {

        @Override
        public void onDoneBeforeTopologyUnlock(GridDhtPartitionsExchangeFuture fut) {
            if (fut.initialVersion().equals(new AffinityTopologyVersion(7, 0))) {
                topInitLatch.countDown();
                try {
                    assertTrue(U.await(enableDurabilityCPStartLatch, 20_000, TimeUnit.MILLISECONDS));
                } catch (IgniteInterruptedCheckedException e) {
                    fail(X.getFullStackTrace(e));
                }
                System.out.println();
            }
        }
    });
    // Trigger rebalancing remap because owner has left.
    IgniteInternalFuture stopFut = GridTestUtils.runAsync(new Runnable() {

        @Override
        public void run() {
            // TODO start cache.
            stopGrid(2);
        }
    });
    // Wait for topology (7,0) init on grid1 before finishing rebalancing on (6,0).
    assertTrue(U.await(topInitLatch, 20_000, TimeUnit.MILLISECONDS));
    // Release last supply message, causing triggering a cp for enabling durability.
    spi3.stopBlock();
    // Wait for new rebalancing assignments ready on grid1.
    TestRecordingCommunicationSpi.spi(grid(1)).waitForBlocked();
    // Triggers spurious ideal switching before rebalancing has finished for (7,0).
    delayedOnwningLatch.countDown();
    stopFut.get();
    TestRecordingCommunicationSpi.spi(grid(1)).stopBlock();
    awaitPartitionMapExchange();
    assertPartitionsSame(idleVerify(grid(0), DEFAULT_CACHE_NAME));
    CacheGroupContext grpCtx = grid(1).context().cache().cacheGroup(cacheId(DEFAULT_CACHE_NAME));
    if (grpCtx != null)
        assertTrue(grpCtx.localWalEnabled());
}
Also used : GridDhtPartitionSupplyMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage) GridDhtPartitionDemandMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionDemandMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) PartitionsExchangeAware(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.PartitionsExchangeAware) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CheckpointListener(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) CountDownLatch(java.util.concurrent.CountDownLatch) GridDhtPartitionSupplyMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteEx(org.apache.ignite.internal.IgniteEx) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) GridDhtPartitionDemandMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionDemandMessage) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 39 with IgniteBiPredicate

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

the class ClientFastReplyCoordinatorFailureTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    cfg.setFailureHandler(new StopNodeOrHaltFailureHandler());
    cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(IP_FINDER));
    TestRecordingCommunicationSpi commSpi = new TestRecordingCommunicationSpi();
    // Block messages to old coordinator right before killing it.
    if (igniteInstanceName.contains("client")) {
        commSpi.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

            @Override
            public boolean apply(ClusterNode node, Message msg) {
                if (msg instanceof GridDhtPartitionsSingleMessage && (node.id().getLeastSignificantBits() & OLD_CRD_BITS) == 0) {
                    info("Going to block message [node=" + node + ", msg=" + msg + ']');
                    clientSingleMesssageLatch.countDown();
                    return true;
                }
                return false;
            }
        });
        if (delayNodeFailedMsg) {
            TcpDiscoverySpi spi = new TestDiscoverySpi();
            spi.setIpFinder(IP_FINDER);
            cfg.setDiscoverySpi(spi);
        }
    } else if (getTestIgniteInstanceName(3).equals(igniteInstanceName)) {
        commSpi.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

            @Override
            public boolean apply(ClusterNode node, Message msg) {
                if (msg instanceof GridDhtPartitionsSingleMessage && (node.id().getLeastSignificantBits() & OLD_CRD_BITS) == 0L) {
                    info("Going to block message [node=" + node + ", msg=" + msg + ']');
                    newSrvSingleMesssageLatch.countDown();
                    return true;
                }
                return false;
            }
        });
    } else if (delayNodeFailedMsg) {
        commSpi.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

            @Override
            public boolean apply(ClusterNode node, Message msg) {
                if (msg instanceof GridDhtPartitionsSingleRequest && node.isClient()) {
                    GridTestUtils.runAsync(() -> {
                        try {
                            Thread.sleep(1_000);
                        } catch (InterruptedException ignore) {
                        // No-op.
                        }
                        PART_SINGLE_REQ_MSG_LATCH.countDown();
                    });
                }
                return false;
            }
        });
    }
    cfg.setCommunicationSpi(commSpi);
    return cfg;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) TcpDiscoveryNodeFailedMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryNodeFailedMessage) GridDhtPartitionsSingleMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsSingleMessage) TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) TcpDiscoveryNodeLeftMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryNodeLeftMessage) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) StopNodeOrHaltFailureHandler(org.apache.ignite.failure.StopNodeOrHaltFailureHandler) GridDhtPartitionsSingleMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsSingleMessage) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridDhtPartitionsSingleRequest(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsSingleRequest) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)

Example 40 with IgniteBiPredicate

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

the class CacheContinuousQueryBufferLimitTest method doTestContinuousQueryPendingBufferLimit.

/**
 * @param locBlockPred Block predicate on local node to emulate message delivery issues.
 * @param pendingLimit Test limit of pending entries.
 * @throws Exception If fails.
 */
private void doTestContinuousQueryPendingBufferLimit(IgniteBiPredicate<ClusterNode, Message> locBlockPred, int pendingLimit) throws Exception {
    AtomicInteger keys = new AtomicInteger();
    IgniteEx locIgnite = startGrid(0);
    IgniteEx rmtIgnite = startGrid(1);
    IgniteCache<Integer, Integer> cache = locIgnite.cache(DEFAULT_CACHE_NAME);
    CacheConfiguration<Integer, Integer> ccfg = cache.getConfiguration(CacheConfiguration.class);
    for (int i = 0; i < TOTAL_KEYS; i++) cache.put(i, i);
    assertEquals(PARTS, ccfg.getAffinity().partitions());
    GridAtomicLong lastAcked = new GridAtomicLong();
    ContinuousQuery<Integer, Integer> cq = new ContinuousQuery<>();
    cq.setRemoteFilterFactory(FactoryBuilder.factoryOf(RMT_FILTER));
    cq.setLocalListener((events) -> events.forEach(e -> lastAcked.setIfGreater(((CacheQueryEntryEvent<?, ?>) e).getPartitionUpdateCounter())));
    cq.setLocal(false);
    IgniteInternalFuture<?> updFut = null;
    try (QueryCursor<?> qry = locIgnite.cache(DEFAULT_CACHE_NAME).query(cq)) {
        awaitPartitionMapExchange();
        // Partition Id, Update Counter, Continuous Entry.
        ConcurrentMap<Long, CacheContinuousQueryEntry> pending = getContinuousQueryPendingBuffer(rmtIgnite, CU.cacheId(DEFAULT_CACHE_NAME), 0);
        spi(locIgnite).blockMessages(locBlockPred);
        updFut = GridTestUtils.runMultiThreadedAsync(() -> {
            while (keys.get() <= OVERFLOW_KEYS_COUNT) cache.put(keys.incrementAndGet(), 0);
        }, 3, "cq-put-");
        assertNotNull("Partition remote buffers must be inited", pending);
        log.warning("Waiting for pending buffer being overflowed within " + OVERFLOW_KEYS_COUNT + " number of keys.");
        boolean await = waitForCondition(() -> pending.size() > pendingLimit, () -> keys.get() <= OVERFLOW_KEYS_COUNT);
        assertFalse("Pending buffer exceeded the limit despite entries have been acked " + "[lastAcked=" + lastAcked + ", pending=" + S.compact(pending.keySet(), i -> i + 1) + ']', await);
    } finally {
        spi(locIgnite).stopBlock();
        if (updFut != null)
            updFut.cancel();
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) TestRecordingCommunicationSpi.spi(org.apache.ignite.internal.TestRecordingCommunicationSpi.spi) Arrays(java.util.Arrays) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) GridDhtAtomicUpdateRequest(org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicUpdateRequest) IgniteEx(org.apache.ignite.internal.IgniteEx) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) Parameterized(org.junit.runners.Parameterized) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) FactoryBuilder(javax.cache.configuration.FactoryBuilder) SystemView(org.apache.ignite.spi.systemview.view.SystemView) Collection(java.util.Collection) IgniteException(org.apache.ignite.IgniteException) UUID(java.util.UUID) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CU(org.apache.ignite.internal.util.typedef.internal.CU) GridTestUtils.getFieldValue(org.apache.ignite.testframework.GridTestUtils.getFieldValue) Message(org.apache.ignite.plugin.extensions.communication.Message) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong) ContinuousQueryView(org.apache.ignite.spi.systemview.view.ContinuousQueryView) CacheQueryEntryEvent(org.apache.ignite.cache.query.CacheQueryEntryEvent) GridDhtTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareRequest) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) RunWith(org.junit.runner.RunWith) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConcurrentMap(java.util.concurrent.ConcurrentMap) ClusterNode(org.apache.ignite.cluster.ClusterNode) S(org.apache.ignite.internal.util.typedef.internal.S) Before(org.junit.Before) Test(org.junit.Test) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) REPLICATED(org.apache.ignite.cache.CacheMode.REPLICATED) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) MAX_PENDING_BUFF_SIZE(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryEventBuffer.MAX_PENDING_BUFF_SIZE) ATOMIC(org.apache.ignite.cache.CacheAtomicityMode.ATOMIC) CQ_SYS_VIEW(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor.CQ_SYS_VIEW) CacheMode(org.apache.ignite.cache.CacheMode) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong)

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