Search in sources :

Example 1 with CdcMain

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

the class CdcLoader method loadCdc.

/**
 * Loads {@link CdcMain} from XML configuration file.
 *
 * @param springXmlPath Path to XML configuration file.
 * @return {@code ChangeDataCapture} instance.
 * @throws IgniteCheckedException If failed.
 */
public static CdcMain loadCdc(String springXmlPath) throws IgniteCheckedException {
    URL cfgUrl = U.resolveSpringUrl(springXmlPath);
    IgniteSpringHelper spring = SPRING.create(false);
    IgniteBiTuple<Map<Class<?>, Collection>, ? extends GridSpringResourceContext> cfgs = spring.loadBeans(cfgUrl, IgniteConfiguration.class, CdcConfiguration.class);
    Collection<IgniteConfiguration> igniteCfgs = cfgs.get1().get(IgniteConfiguration.class);
    if (F.size(igniteCfgs) != 1) {
        throw new IgniteCheckedException("Exact 1 IgniteConfiguration should be defined. Found " + F.size(igniteCfgs));
    }
    Collection<CdcConfiguration> cdcCfgs = cfgs.get1().get(CdcConfiguration.class);
    if (F.size(cdcCfgs) != 1) {
        throw new IgniteCheckedException("Exact 1 CaptureDataChangeConfiguration configuration should be defined. Found " + F.size(cdcCfgs));
    }
    return new CdcMain(F.first(igniteCfgs), cfgs.get2(), F.first(cdcCfgs));
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CdcMain(org.apache.ignite.internal.cdc.CdcMain) IgniteSpringHelper(org.apache.ignite.internal.util.spring.IgniteSpringHelper) Map(java.util.Map) URL(java.net.URL)

Example 2 with CdcMain

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

the class AbstractCdcTest method createCdc.

/**
 */
protected CdcMain createCdc(CdcConsumer cnsmr, IgniteConfiguration cfg, CountDownLatch latch, GridAbsPredicate... conditions) {
    CdcConfiguration cdcCfg = new CdcConfiguration();
    cdcCfg.setConsumer(cnsmr);
    cdcCfg.setKeepBinary(keepBinary());
    cdcCfg.setMetricExporterSpi(metricExporters());
    return new CdcMain(cfg, null, cdcCfg) {

        @Override
        protected CdcConsumerState createState(Path stateDir) {
            return new CdcConsumerState(stateDir) {

                @Override
                public void save(WALPointer ptr) throws IOException {
                    super.save(ptr);
                    if (!F.isEmpty(conditions)) {
                        for (GridAbsPredicate p : conditions) {
                            if (!p.apply())
                                return;
                        }
                        latch.countDown();
                    }
                }
            };
        }
    };
}
Also used : Path(java.nio.file.Path) CdcConsumerState(org.apache.ignite.internal.cdc.CdcConsumerState) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CdcMain(org.apache.ignite.internal.cdc.CdcMain) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)

Example 3 with CdcMain

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

the class AbstractCdcTest method checkMetrics.

/**
 */
protected void checkMetrics(CdcMain cdc, int expCnt) throws Exception {
    if (metricExporters() != null) {
        IgniteConfiguration cfg = getFieldValue(cdc, "igniteCfg");
        DynamicMBean jmxCdcReg = metricRegistry(cdcInstanceName(cfg.getIgniteInstanceName()), null, "cdc");
        Function<String, ?> jmxVal = m -> {
            try {
                return jmxCdcReg.getAttribute(m);
            } catch (Exception e) {
                throw new IgniteException(e);
            }
        };
        checkMetrics(expCnt, (Function<String, Long>) jmxVal, (Function<String, String>) jmxVal);
    }
    MetricRegistry mreg = getFieldValue(cdc, "mreg");
    assertNotNull(mreg);
    checkMetrics(expCnt, m -> mreg.<LongMetric>findMetric(m).value(), m -> mreg.<ObjectMetric<String>>findMetric(m).value());
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Arrays(java.util.Arrays) 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) DELETE(org.apache.ignite.cdc.AbstractCdcTest.ChangeEventType.DELETE) CdcConsumerState(org.apache.ignite.internal.cdc.CdcConsumerState) Function(java.util.function.Function) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) ObjectMetric(org.apache.ignite.spi.metric.ObjectMetric) COMMITTED_SEG_IDX(org.apache.ignite.internal.cdc.CdcMain.COMMITTED_SEG_IDX) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) LAST_SEG_CONSUMPTION_TIME(org.apache.ignite.internal.cdc.CdcMain.LAST_SEG_CONSUMPTION_TIME) GridTestUtils.runAsync(org.apache.ignite.testframework.GridTestUtils.runAsync) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) MetricExporterSpi(org.apache.ignite.spi.metric.MetricExporterSpi) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) COMMITTED_SEG_OFFSET(org.apache.ignite.internal.cdc.CdcMain.COMMITTED_SEG_OFFSET) MARSHALLER_DIR(org.apache.ignite.internal.cdc.CdcMain.MARSHALLER_DIR) CdcMain.cdcInstanceName(org.apache.ignite.internal.cdc.CdcMain.cdcInstanceName) Path(java.nio.file.Path) F(org.apache.ignite.internal.util.typedef.F) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Iterator(java.util.Iterator) IgniteException(org.apache.ignite.IgniteException) BINARY_META_DIR(org.apache.ignite.internal.cdc.CdcMain.BINARY_META_DIR) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) CI3(org.apache.ignite.internal.util.typedef.CI3) LAST_EVT_TIME(org.apache.ignite.internal.cdc.WalRecordsConsumer.LAST_EVT_TIME) File(java.io.File) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CUR_SEG_IDX(org.apache.ignite.internal.cdc.CdcMain.CUR_SEG_IDX) UPDATE(org.apache.ignite.cdc.AbstractCdcTest.ChangeEventType.UPDATE) EVTS_CNT(org.apache.ignite.internal.cdc.WalRecordsConsumer.EVTS_CNT) GridTestUtils.getFieldValue(org.apache.ignite.testframework.GridTestUtils.getFieldValue) DynamicMBean(javax.management.DynamicMBean) LongMetric(org.apache.ignite.spi.metric.LongMetric) CDC_DIR(org.apache.ignite.internal.cdc.CdcMain.CDC_DIR) DynamicMBean(javax.management.DynamicMBean) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteException(org.apache.ignite.IgniteException) ObjectMetric(org.apache.ignite.spi.metric.ObjectMetric) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) LongMetric(org.apache.ignite.spi.metric.LongMetric) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Example 4 with CdcMain

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

the class CdcSelfTest method testMultiNodeConsumption.

/**
 */
@Test
@WithSystemProperty(key = IGNITE_DATA_STORAGE_FOLDER_BY_CONSISTENT_ID, value = "true")
public void testMultiNodeConsumption() throws Exception {
    IgniteEx ign1 = startGrid(0);
    IgniteEx ign2 = startGrid(1);
    ign1.cluster().state(ACTIVE);
    IgniteCache<Integer, User> cache = ign1.getOrCreateCache(DEFAULT_CACHE_NAME);
    // Calculate expected count of key for each node.
    int[] keysCnt = new int[2];
    for (int i = 0; i < KEYS_CNT * 2; i++) {
        Ignite primary = primaryNode(i, DEFAULT_CACHE_NAME);
        assertTrue(primary == ign1 || primary == ign2);
        keysCnt[primary == ign1 ? 0 : 1]++;
    }
    // Adds data concurrently with CDC start.
    IgniteInternalFuture<?> addDataFut = runAsync(() -> addData(cache, 0, KEYS_CNT));
    UserCdcConsumer cnsmr1 = new UserCdcConsumer();
    UserCdcConsumer cnsmr2 = new UserCdcConsumer();
    IgniteConfiguration cfg1 = getConfiguration(ign1.name());
    IgniteConfiguration cfg2 = getConfiguration(ign2.name());
    // Always run CDC with consistent id to ensure instance read data for specific node.
    if (!specificConsistentId) {
        cfg1.setConsistentId((Serializable) ign1.localNode().consistentId());
        cfg2.setConsistentId((Serializable) ign2.localNode().consistentId());
    }
    CountDownLatch latch = new CountDownLatch(2);
    GridAbsPredicate sizePredicate1 = sizePredicate(keysCnt[0], DEFAULT_CACHE_NAME, UPDATE, cnsmr1);
    GridAbsPredicate sizePredicate2 = sizePredicate(keysCnt[1], DEFAULT_CACHE_NAME, UPDATE, cnsmr2);
    CdcMain cdc1 = createCdc(cnsmr1, cfg1, latch, sizePredicate1);
    CdcMain cdc2 = createCdc(cnsmr2, cfg2, latch, sizePredicate2);
    IgniteInternalFuture<?> fut1 = runAsync(cdc1);
    IgniteInternalFuture<?> fut2 = runAsync(cdc2);
    addDataFut.get(getTestTimeout());
    runAsync(() -> addData(cache, KEYS_CNT, KEYS_CNT * 2)).get(getTestTimeout());
    // Wait while predicate will become true and state saved on the disk for both cdc.
    assertTrue(latch.await(getTestTimeout(), MILLISECONDS));
    checkMetrics(cdc1, keysCnt[0]);
    checkMetrics(cdc2, keysCnt[1]);
    assertFalse(cnsmr1.stopped());
    assertFalse(cnsmr2.stopped());
    fut1.cancel();
    fut2.cancel();
    assertTrue(cnsmr1.stopped());
    assertTrue(cnsmr2.stopped());
    removeData(cache, 0, KEYS_CNT * 2);
    cdc1 = createCdc(cnsmr1, cfg1);
    cdc2 = createCdc(cnsmr2, cfg2);
    IgniteInternalFuture<?> rmvFut1 = runAsync(cdc1);
    IgniteInternalFuture<?> rmvFut2 = runAsync(cdc2);
    waitForSize(KEYS_CNT * 2, DEFAULT_CACHE_NAME, DELETE, cnsmr1, cnsmr2);
    checkMetrics(cdc1, keysCnt[0]);
    checkMetrics(cdc2, keysCnt[1]);
    rmvFut1.cancel();
    rmvFut2.cancel();
    assertTrue(cnsmr1.stopped());
    assertTrue(cnsmr2.stopped());
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CdcMain(org.apache.ignite.internal.cdc.CdcMain) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) Test(org.junit.Test) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty)

Example 5 with CdcMain

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

the class CdcSelfTest method testReadBeforeGracefulShutdown.

/**
 */
@Test
public void testReadBeforeGracefulShutdown() throws Exception {
    Ignite ign = startGrid(getConfiguration("ignite-0"));
    ign.cluster().state(ACTIVE);
    CountDownLatch cnsmrStarted = new CountDownLatch(1);
    CountDownLatch startProcEvts = new CountDownLatch(1);
    UserCdcConsumer cnsmr = new UserCdcConsumer() {

        @Override
        public boolean onEvents(Iterator<CdcEvent> evts) {
            cnsmrStarted.countDown();
            try {
                startProcEvts.await(getTestTimeout(), MILLISECONDS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return super.onEvents(evts);
        }
    };
    CdcMain cdc = createCdc(cnsmr, getConfiguration(ign.name()));
    runAsync(cdc);
    IgniteCache<Integer, User> cache = ign.getOrCreateCache(DEFAULT_CACHE_NAME);
    addData(cache, 0, KEYS_CNT);
    // Make sure all streamed data will become available for consumption.
    Thread.sleep(2 * WAL_ARCHIVE_TIMEOUT);
    cnsmrStarted.await(getTestTimeout(), MILLISECONDS);
    // Initiate graceful shutdown.
    cdc.stop();
    startProcEvts.countDown();
    waitForSize(KEYS_CNT, DEFAULT_CACHE_NAME, UPDATE, cnsmr);
    assertTrue(waitForCondition(cnsmr::stopped, getTestTimeout()));
    List<Integer> keys = cnsmr.data(UPDATE, cacheId(DEFAULT_CACHE_NAME));
    assertEquals(KEYS_CNT, keys.size());
    for (int i = 0; i < KEYS_CNT; i++) assertTrue(keys.contains(i));
}
Also used : CdcMain(org.apache.ignite.internal.cdc.CdcMain) Iterator(java.util.Iterator) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) 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