Search in sources :

Example 41 with IgniteDataStreamer

use of org.apache.ignite.IgniteDataStreamer in project ignite by apache.

the class IgniteClusterActivateDeactivateTestWithPersistence method testDeactivateDuringEvictionAndRebalance.

/**
 * Test that after deactivation during eviction and rebalance and activation again after
 * all data in cache is consistent.
 *
 * @throws Exception If failed.
 */
@Test
public void testDeactivateDuringEvictionAndRebalance() throws Exception {
    Assume.assumeFalse("https://issues.apache.org/jira/browse/IGNITE-7384", MvccFeatureChecker.forcedMvcc());
    IgniteEx srv = startGrids(3);
    srv.cluster().state(ACTIVE);
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME).setBackups(1).setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC).setIndexedTypes(Integer.class, Integer.class).setAffinity(new RendezvousAffinityFunction(false, 64)).setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    IgniteCache cache = srv.createCache(ccfg);
    // High number of keys triggers long partition eviction.
    final int keysCount = 100_000;
    try (IgniteDataStreamer ds = srv.dataStreamer(DEFAULT_CACHE_NAME)) {
        log.info("Writing initial data...");
        ds.allowOverwrite(true);
        for (int k = 1; k <= keysCount; k++) {
            ds.addData(k, k);
            if (k % 50_000 == 0)
                log.info("Written " + k + " entities.");
        }
        log.info("Writing initial data finished.");
    }
    AtomicInteger keyCounter = new AtomicInteger(keysCount);
    AtomicBoolean stop = new AtomicBoolean(false);
    Set<Integer> addedKeys = new GridConcurrentHashSet<>();
    IgniteInternalFuture cacheLoadFuture = GridTestUtils.runMultiThreadedAsync(() -> {
        while (!stop.get()) {
            int key = keyCounter.incrementAndGet();
            try {
                cache.put(key, key);
                addedKeys.add(key);
                Thread.sleep(10);
            } catch (Exception ignored) {
            }
        }
    }, 2, "cache-load");
    stopGrid(2);
    // Wait for some data.
    Thread.sleep(3000);
    startGrid(2);
    log.info("Stop load...");
    stop.set(true);
    cacheLoadFuture.get();
    // Deactivate and activate again.
    srv.cluster().state(INACTIVE);
    srv.cluster().state(ACTIVE);
    awaitPartitionMapExchange();
    log.info("Checking data...");
    for (Ignite ignite : G.allGrids()) {
        IgniteCache cache1 = ignite.getOrCreateCache(DEFAULT_CACHE_NAME);
        for (int k = 1; k <= keysCount; k++) {
            Object val = cache1.get(k);
            Assert.assertNotNull("node=" + ignite.name() + ", key=" + k, val);
            Assert.assertTrue("node=" + ignite.name() + ", key=" + k + ", val=" + val, (int) val == k);
        }
        for (int k : addedKeys) {
            Object val = cache1.get(k);
            Assert.assertNotNull("node=" + ignite.name() + ", key=" + k, val);
            Assert.assertTrue("node=" + ignite.name() + ", key=" + k + ", val=" + val, (int) val == k);
        }
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteClusterReadOnlyException(org.apache.ignite.internal.processors.cache.distributed.dht.IgniteClusterReadOnlyException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test)

Example 42 with IgniteDataStreamer

use of org.apache.ignite.IgniteDataStreamer in project ignite by apache.

the class HistoricalRebalanceTwoPartsInDifferentCheckpointsTest method rebalanceTwoPartitions.

/**
 * Checks a historical rebalance for partitions 0 and 2.
 *
 * @param descending When true first updates is for younger partition, false is older one.
 * @throws Exception If failed.
 */
public void rebalanceTwoPartitions(boolean descending) throws Exception {
    IgniteEx ignite0 = startGrids(2);
    ignite0.cluster().state(ClusterState.ACTIVE);
    awaitPartitionMapExchange();
    LinkedHashSet<Integer> partKeys = new LinkedHashSet<>();
    if (descending) {
        partKeys.addAll(partitionKeys(ignite0.cache(DEFAULT_CACHE_NAME), 0, 10, 0));
        partKeys.addAll(partitionKeys(ignite0.cache(DEFAULT_CACHE_NAME), 2, 10, 0));
    } else {
        partKeys.addAll(partitionKeys(ignite0.cache(DEFAULT_CACHE_NAME), 2, 10, 0));
        partKeys.addAll(partitionKeys(ignite0.cache(DEFAULT_CACHE_NAME), 0, 10, 0));
    }
    // It can be achieved by using full_sync write sync mode or by using a data streamer with allowOverwrite == false.
    try (IgniteDataStreamer streamer = ignite0.dataStreamer(DEFAULT_CACHE_NAME)) {
        partKeys.forEach(key -> streamer.addData(key, key));
    }
    info("Data preload completed.");
    stopGrid(1);
    awaitPartitionMapExchange();
    info("Node stopped.");
    IgniteCache cache = ignite0.cache(DEFAULT_CACHE_NAME);
    for (Integer key : partKeys) {
        cache.put(key, key + 1);
        forceCheckpoint();
    }
    IgniteConfiguration cfg = getConfiguration(getTestIgniteInstanceName(1));
    AtomicBoolean hasFullRebalance = new AtomicBoolean();
    AtomicBoolean hasHistoricalRebalance = new AtomicBoolean();
    ((TestRecordingCommunicationSpi) cfg.getCommunicationSpi()).record((node, msg) -> {
        if (msg instanceof GridDhtPartitionDemandMessage) {
            GridDhtPartitionDemandMessage demandMsg = (GridDhtPartitionDemandMessage) msg;
            hasFullRebalance.set(demandMsg.partitions().hasFull());
            hasHistoricalRebalance.set(demandMsg.partitions().hasHistorical());
        }
        return false;
    });
    startGrid(cfg);
    awaitPartitionMapExchange();
    info("Node started and rebalance completed.");
    assertFalse(hasFullRebalance.get());
    assertTrue(hasHistoricalRebalance.get());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteCache(org.apache.ignite.IgniteCache)

Example 43 with IgniteDataStreamer

use of org.apache.ignite.IgniteDataStreamer in project ignite by apache.

the class CacheIndexStreamerTest method checkStreamer.

/**
 * @param atomicityMode Cache atomicity mode.
 * @throws Exception If failed.
 */
public void checkStreamer(CacheAtomicityMode atomicityMode) throws Exception {
    final Ignite ignite = startGrid(0);
    final IgniteCache<Integer, String> cache = ignite.createCache(cacheConfiguration(atomicityMode));
    final AtomicBoolean stop = new AtomicBoolean();
    final int KEYS = 10_000;
    try {
        IgniteInternalFuture streamerFut = GridTestUtils.runAsync(new Callable() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get()) {
                    try (IgniteDataStreamer<Integer, String> streamer = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
                        // TODO FIXME https://issues.apache.org/jira/browse/IGNITE-11793
                        streamer.allowOverwrite(atomicityMode == TRANSACTIONAL);
                        for (int i = 0; i < 1; i++) streamer.addData(rnd.nextInt(KEYS), String.valueOf(i));
                    }
                }
                return null;
            }
        }, "streamer-thread");
        IgniteInternalFuture updateFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!stop.get()) {
                    for (int i = 0; i < 100; i++) {
                        Integer key = rnd.nextInt(KEYS);
                        cache.put(key, String.valueOf(key));
                        cache.remove(key);
                    }
                }
                return null;
            }
        }, 1, "update-thread");
        U.sleep(30_000);
        stop.set(true);
        streamerFut.get();
        updateFut.get();
    } finally {
        stop.set(true);
        stopAllGrids();
    }
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Callable(java.util.concurrent.Callable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 44 with IgniteDataStreamer

use of org.apache.ignite.IgniteDataStreamer in project ignite by apache.

the class CacheSizeTtlTest method testCacheSizeWorksCorrectlyWithTtl.

/**
 * Tests that cache.size() works correctly for massive amount of puts and ttl.
 */
@Test
public void testCacheSizeWorksCorrectlyWithTtl() throws IgniteInterruptedCheckedException {
    startIgniteServer();
    Ignite client = startIgniteClient();
    try (IgniteDataStreamer dataStreamer = client.dataStreamer(CACHE_NAME)) {
        IntStream.range(0, 100_000).forEach(i -> dataStreamer.addData(1, LocalDateTime.now()));
    }
    assertTrue(GridTestUtils.waitForCondition(() -> client.cache(CACHE_NAME).size(CachePeekMode.PRIMARY) == 0, getTestTimeout()));
}
Also used : IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 45 with IgniteDataStreamer

use of org.apache.ignite.IgniteDataStreamer in project ignite by apache.

the class DynamicIndexAbstractSelfTest method put.

/**
 * Put key range.
 *
 * @param node Node.
 * @param from From key.
 * @param to To key.
 */
protected static void put(Ignite node, int from, int to) {
    try (IgniteDataStreamer streamer = node.dataStreamer(CACHE_NAME)) {
        streamer.allowOverwrite(true);
        streamer.keepBinary(true);
        for (int i = from; i < to; i++) {
            BinaryObject key = key(node, i);
            BinaryObject val = value(node, i);
            streamer.addData(key, val);
        }
        streamer.flush();
    }
}
Also used : IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) BinaryObject(org.apache.ignite.binary.BinaryObject)

Aggregations

IgniteDataStreamer (org.apache.ignite.IgniteDataStreamer)47 Ignite (org.apache.ignite.Ignite)25 Test (org.junit.Test)21 IgniteCache (org.apache.ignite.IgniteCache)19 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)18 IgniteEx (org.apache.ignite.internal.IgniteEx)16 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)14 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)10 List (java.util.List)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)8 Ignition (org.apache.ignite.Ignition)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Random (java.util.Random)6 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)6 UUID (java.util.UUID)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5