Search in sources :

Example 6 with CdcMain

use of org.apache.ignite.internal.cdc.CdcMain in project ignite by apache.

the class CdcSelfTest method testReReadWhenStateWasNotStored.

/**
 */
@Test
public void testReReadWhenStateWasNotStored() throws Exception {
    IgniteEx ign = startGrid(getConfiguration("ignite-0"));
    ign.cluster().state(ACTIVE);
    IgniteCache<Integer, User> cache = ign.getOrCreateCache(DEFAULT_CACHE_NAME);
    addData(cache, 0, KEYS_CNT);
    for (int i = 0; i < 3; i++) {
        UserCdcConsumer cnsmr = new UserCdcConsumer() {

            @Override
            protected boolean commit() {
                return false;
            }
        };
        CdcMain cdc = createCdc(cnsmr, getConfiguration(ign.name()));
        IgniteInternalFuture<?> fut = runAsync(cdc);
        waitForSize(KEYS_CNT, DEFAULT_CACHE_NAME, UPDATE, cnsmr);
        checkMetrics(cdc, KEYS_CNT);
        fut.cancel();
        assertTrue(cnsmr.stopped());
    }
    AtomicBoolean consumeHalf = new AtomicBoolean(true);
    AtomicBoolean halfCommitted = new AtomicBoolean(false);
    int half = KEYS_CNT / 2;
    UserCdcConsumer cnsmr = new UserCdcConsumer() {

        @Override
        public boolean onEvents(Iterator<CdcEvent> evts) {
            if (consumeHalf.get() && F.size(data(UPDATE, cacheId(DEFAULT_CACHE_NAME))) == half) {
                // This means that state committed as a result of the previous call.
                halfCommitted.set(true);
                return false;
            }
            while (evts.hasNext()) {
                CdcEvent evt = evts.next();
                if (!evt.primary())
                    continue;
                data.computeIfAbsent(F.t(evt.value() == null ? DELETE : UPDATE, evt.cacheId()), k -> new ArrayList<>()).add((Integer) evt.key());
                if (consumeHalf.get())
                    return F.size(data(UPDATE, cacheId(DEFAULT_CACHE_NAME))) == half;
            }
            return true;
        }
    };
    CdcMain cdc = createCdc(cnsmr, getConfiguration(ign.name()));
    IgniteInternalFuture<?> fut = runAsync(cdc);
    waitForSize(half, DEFAULT_CACHE_NAME, UPDATE, cnsmr);
    checkMetrics(cdc, half);
    waitForCondition(halfCommitted::get, getTestTimeout());
    fut.cancel();
    assertTrue(cnsmr.stopped());
    removeData(cache, 0, KEYS_CNT);
    consumeHalf.set(false);
    cdc = createCdc(cnsmr, getConfiguration(ign.name()));
    fut = runAsync(cdc);
    waitForSize(KEYS_CNT, DEFAULT_CACHE_NAME, UPDATE, cnsmr);
    waitForSize(KEYS_CNT, DEFAULT_CACHE_NAME, DELETE, cnsmr);
    checkMetrics(cdc, KEYS_CNT * 2 - half);
    fut.cancel();
    assertTrue(cnsmr.stopped());
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) JmxMetricExporterSpi(org.apache.ignite.spi.metric.jmx.JmxMetricExporterSpi) GridCacheUtils.cacheId(org.apache.ignite.internal.processors.cache.GridCacheUtils.cacheId) CdcMain(org.apache.ignite.internal.cdc.CdcMain) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) RunWith(org.junit.runner.RunWith) DELETE(org.apache.ignite.cdc.AbstractCdcTest.ChangeEventType.DELETE) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteEx(org.apache.ignite.internal.IgniteEx) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) GridTestUtils.runAsync(org.apache.ignite.testframework.GridTestUtils.runAsync) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) MetricExporterSpi(org.apache.ignite.spi.metric.MetricExporterSpi) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) EnumSet(java.util.EnumSet) Parameterized(org.junit.runners.Parameterized) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) WALMode(org.apache.ignite.configuration.WALMode) F(org.apache.ignite.internal.util.typedef.F) Iterator(java.util.Iterator) Collection(java.util.Collection) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) UPDATE(org.apache.ignite.cdc.AbstractCdcTest.ChangeEventType.UPDATE) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Collections(java.util.Collections) IGNITE_DATA_STORAGE_FOLDER_BY_CONSISTENT_ID(org.apache.ignite.IgniteSystemProperties.IGNITE_DATA_STORAGE_FOLDER_BY_CONSISTENT_ID) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) CdcMain(org.apache.ignite.internal.cdc.CdcMain) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteEx(org.apache.ignite.internal.IgniteEx) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 7 with CdcMain

use of org.apache.ignite.internal.cdc.CdcMain in project ignite by apache.

the class CdcConfigurationTest method testLoadConfig.

/**
 */
@Test
public void testLoadConfig() throws Exception {
    assertThrows(null, () -> loadCdc("modules/spring/src/test/config/cdc/double-ignite-config.xml"), IgniteCheckedException.class, "Exact 1 IgniteConfiguration should be defined. Found 2");
    assertThrows(null, () -> loadCdc("modules/spring/src/test/config/cdc/double-cdc-config.xml"), IgniteCheckedException.class, "Exact 1 CaptureDataChangeConfiguration configuration should be defined. Found 2");
    CdcMain cdc = loadCdc("modules/spring/src/test/config/cdc/cdc-config-without-persistence.xml");
    assertNotNull(cdc);
    assertThrows(null, cdc::run, IgniteException.class, ERR_MSG);
}
Also used : CdcMain(org.apache.ignite.internal.cdc.CdcMain) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 8 with CdcMain

use of org.apache.ignite.internal.cdc.CdcMain in project ignite by apache.

the class CdcSelfTest method readAll.

/**
 */
private void readAll(UserCdcConsumer cnsmr, boolean offsetCommit) throws Exception {
    IgniteConfiguration cfg = getConfiguration("ignite-0");
    Ignite ign = startGrid(cfg);
    ign.cluster().state(ACTIVE);
    IgniteCache<Integer, User> cache = ign.getOrCreateCache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, User> txCache = ign.getOrCreateCache(TX_CACHE_NAME);
    addAndWaitForConsumption(cnsmr, cfg, cache, txCache, CdcSelfTest::addData, 0, KEYS_CNT + 3, offsetCommit);
    removeData(cache, 0, KEYS_CNT);
    CdcMain cdcMain = createCdc(cnsmr, cfg);
    IgniteInternalFuture<?> rmvFut = runAsync(cdcMain);
    waitForSize(KEYS_CNT, DEFAULT_CACHE_NAME, DELETE, cnsmr);
    checkMetrics(cdcMain, offsetCommit ? KEYS_CNT : ((KEYS_CNT + 3) * 2 + KEYS_CNT));
    rmvFut.cancel();
    assertTrue(cnsmr.stopped());
    stopAllGrids();
    cleanPersistenceDir();
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CdcMain(org.apache.ignite.internal.cdc.CdcMain) Ignite(org.apache.ignite.Ignite)

Example 9 with CdcMain

use of org.apache.ignite.internal.cdc.CdcMain in project ignite by apache.

the class AbstractCdcTest method addAndWaitForConsumption.

/**
 */
protected void addAndWaitForConsumption(UserCdcConsumer cnsmr, IgniteConfiguration cfg, IgniteCache<Integer, CdcSelfTest.User> cache, IgniteCache<Integer, CdcSelfTest.User> txCache, CI3<IgniteCache<Integer, CdcSelfTest.User>, Integer, Integer> addData, int from, int to, boolean waitForCommit) throws Exception {
    GridAbsPredicate cachePredicate = sizePredicate(to - from, cache.getName(), UPDATE, cnsmr);
    GridAbsPredicate txPredicate = txCache == null ? null : sizePredicate(to - from, txCache.getName(), UPDATE, cnsmr);
    CdcMain cdc;
    CountDownLatch latch = new CountDownLatch(1);
    if (waitForCommit) {
        cdc = txCache == null ? createCdc(cnsmr, cfg, latch, cachePredicate) : createCdc(cnsmr, cfg, latch, cachePredicate, txPredicate);
    } else
        cdc = createCdc(cnsmr, cfg);
    IgniteInternalFuture<?> fut = runAsync(cdc);
    addData.apply(cache, from, to);
    if (txCache != null)
        addData.apply(txCache, from, to);
    if (waitForCommit)
        latch.await(getTestTimeout(), MILLISECONDS);
    else {
        assertTrue(waitForCondition(cachePredicate, getTestTimeout()));
        if (txCache != null)
            assertTrue(waitForCondition(txPredicate, getTestTimeout()));
    }
    checkMetrics(cdc, txCache == null ? to : to * 2);
    fut.cancel();
    List<Integer> keys = cnsmr.data(UPDATE, cacheId(cache.getName()));
    assertEquals(to - from, keys.size());
    for (int i = from; i < to; i++) assertTrue(Integer.toString(i), keys.contains(i));
    assertTrue(cnsmr.stopped());
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CdcMain(org.apache.ignite.internal.cdc.CdcMain) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with CdcMain

use of org.apache.ignite.internal.cdc.CdcMain in project ignite by apache.

the class CdcConfigurationTest method testInjectResources.

/**
 */
@Test
public void testInjectResources() throws Exception {
    CdcMain cdc = loadCdc("modules/spring/src/test/config/cdc/correct-cdc-config.xml");
    try (IgniteEx ign = startGrid((IgniteConfiguration) getFieldValue(cdc, "igniteCfg"))) {
        TestCdcConsumer cnsmr = (TestCdcConsumer) ((WalRecordsConsumer<?, ?>) getFieldValue(cdc, "consumer")).consumer();
        assertNotNull(cnsmr);
        CountDownLatch startLatch = cnsmr.startLatch;
        IgniteInternalFuture<?> fut = runAsync(cdc::run);
        startLatch.await(getTestTimeout(), MILLISECONDS);
        assertEquals("someString", cnsmr.springString);
        assertEquals("someString2", cnsmr.springString2);
        assertNotNull(cnsmr.log);
        assertNotNull(cnsmr.ctx);
        fut.cancel();
    }
}
Also used : CdcMain(org.apache.ignite.internal.cdc.CdcMain) IgniteEx(org.apache.ignite.internal.IgniteEx) CountDownLatch(java.util.concurrent.CountDownLatch) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

CdcMain (org.apache.ignite.internal.cdc.CdcMain)10 CountDownLatch (java.util.concurrent.CountDownLatch)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)5 Test (org.junit.Test)5 Ignite (org.apache.ignite.Ignite)4 Iterator (java.util.Iterator)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)2 IgniteCache (org.apache.ignite.IgniteCache)2 DELETE (org.apache.ignite.cdc.AbstractCdcTest.ChangeEventType.DELETE)2 UPDATE (org.apache.ignite.cdc.AbstractCdcTest.ChangeEventType.UPDATE)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 CdcConsumerState (org.apache.ignite.internal.cdc.CdcConsumerState)2 GridCacheUtils.cacheId (org.apache.ignite.internal.processors.cache.GridCacheUtils.cacheId)2 WALPointer (org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)2