nizhikov commented on a change in pull request #8909: URL: https://github.com/apache/ignite/pull/8909#discussion_r646446191
########## File path: modules/core/src/test/java/org/apache/ignite/cdc/ChangeDataCaptureSelfTest.java ########## @@ -0,0 +1,416 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.cdc; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.DataRegionConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.configuration.WALMode; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.cdc.ChangeDataCapture; +import org.apache.ignite.internal.util.typedef.F; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static org.apache.ignite.cdc.AbstractChangeDataCaptureTest.ChangeEventType.DELETE; +import static org.apache.ignite.cdc.AbstractChangeDataCaptureTest.ChangeEventType.UPDATE; +import static org.apache.ignite.cluster.ClusterState.ACTIVE; +import static org.apache.ignite.internal.processors.cache.GridCacheUtils.cacheId; +import static org.apache.ignite.testframework.GridTestUtils.runAsync; +import static org.apache.ignite.testframework.GridTestUtils.waitForCondition; + +/** */ +@RunWith(Parameterized.class) +public class ChangeDataCaptureSelfTest extends AbstractChangeDataCaptureTest { + /** */ + public static final String TX_CACHE_NAME = "tx-cache"; + + /** */ + public static final int WAL_ARCHIVE_TIMEOUT = 5_000; + + /** Keys count. */ + public static final int KEYS_CNT = 50; + + /** */ + @Parameterized.Parameter + public boolean specificConsistentId; + + /** */ + @Parameterized.Parameter(1) + public WALMode walMode; + + /** */ + @Parameterized.Parameters(name = "specificConsistentId={0}, walMode={1}") + public static Collection<?> parameters() { + return Arrays.asList(new Object[][] { + {true, WALMode.FSYNC}, + {false, WALMode.FSYNC}, + {true, WALMode.LOG_ONLY}, + {false, WALMode.LOG_ONLY}, + {true, WALMode.BACKGROUND}, + {false, WALMode.BACKGROUND} + }); + } + + /** Consistent id. */ + private UUID consistentId = UUID.randomUUID(); + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + + if (specificConsistentId) + cfg.setConsistentId(consistentId); + + cfg.setDataStorageConfiguration(new DataStorageConfiguration() + .setChangeDataCaptureEnabled(true) + .setWalMode(walMode) + .setWalForceArchiveTimeout(WAL_ARCHIVE_TIMEOUT) + .setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(true))); + + cfg.setCacheConfiguration( + new CacheConfiguration<>(TX_CACHE_NAME).setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL) + ); + + return cfg; + } + + /** Simplest CDC test. */ + @Test + public void testReadAllKeys() throws Exception { + // Read all records from iterator. + readAll(new UserCDCConsumer()); + + // Read one record per call. + readAll(new UserCDCConsumer() { + @Override public boolean onEvents(Iterator<ChangeDataCaptureEvent> evts) { + super.onEvents(Collections.singleton(evts.next()).iterator()); + + return false; + } + }); + + // Read one record per call and commit. + readAll(new UserCDCConsumer() { + @Override public boolean onEvents(Iterator<ChangeDataCaptureEvent> evts) { + super.onEvents(Collections.singleton(evts.next()).iterator()); + + return true; + } + }); + } + + /** */ + private void readAll(UserCDCConsumer cnsmr) throws Exception { + IgniteConfiguration cfg = getConfiguration("ignite-0"); + + Ignite ign = startGrid(cfg); + + ign.cluster().state(ACTIVE); + + ChangeDataCapture cdc = new ChangeDataCapture(cfg, null, cdcConfig(cnsmr)); + + IgniteCache<Integer, User> cache = ign.getOrCreateCache(DEFAULT_CACHE_NAME); + IgniteCache<Integer, User> txCache = ign.getOrCreateCache(TX_CACHE_NAME); + + addAndWaitForConsumption( + cnsmr, + cdc, + cache, + txCache, + ChangeDataCaptureSelfTest::addData, + 0, + KEYS_CNT * 2, + getTestTimeout() + ); + + removeData(cache, 0, KEYS_CNT); + + IgniteInternalFuture<?> rmvFut = runAsync(cdc); + + assertTrue(waitForSize(KEYS_CNT, DEFAULT_CACHE_NAME, DELETE, getTestTimeout(), cnsmr)); + + rmvFut.cancel(); + + assertTrue(cnsmr.stopped()); + + stopAllGrids(); + + cleanPersistenceDir(); + } + + /** */ + @Test + public void testReadBeforeGracefulShutdown() throws Exception { + IgniteConfiguration cfg = getConfiguration("ignite-0"); + + Ignite ign = startGrid(cfg); + + ign.cluster().state(ACTIVE); + + CountDownLatch cnsmrStarted = new CountDownLatch(1); + CountDownLatch startProcEvts = new CountDownLatch(1); + + UserCDCConsumer cnsmr = new UserCDCConsumer() { + @Override public boolean onEvents(Iterator<ChangeDataCaptureEvent> evts) { + cnsmrStarted.countDown(); + + try { + startProcEvts.await(getTestTimeout(), TimeUnit.MILLISECONDS); + } + catch (InterruptedException e) { + throw new RuntimeException(e); + } + + return super.onEvents(evts); + } + }; + + ChangeDataCapture cdc = new ChangeDataCapture(cfg, null, cdcConfig(cnsmr)); + + 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(), TimeUnit.MILLISECONDS); + + // Initiate graceful shutdown. + cdc.stop(); + + startProcEvts.countDown(); + + assertTrue(waitForSize(KEYS_CNT, DEFAULT_CACHE_NAME, UPDATE, getTestTimeout(), 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)); + } + + /** */ + @Test + public void testMultiNodeConsumption() throws Exception { + IgniteEx ign1 = startGrid(0); + + if (specificConsistentId) + consistentId = UUID.randomUUID(); + + IgniteEx ign2 = startGrid(1); + + ign1.cluster().state(ACTIVE); + + IgniteCache<Integer, User> cache = ign1.getOrCreateCache(DEFAULT_CACHE_NAME); + + IgniteInternalFuture<?> addDataFut = runAsync(() -> addData(cache, 0, KEYS_CNT)); Review comment: Right here, data added concurrently with CDC start. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
