nizhikov commented on a change in pull request #8909: URL: https://github.com/apache/ignite/pull/8909#discussion_r645677091
########## File path: modules/core/src/test/java/org/apache/ignite/cdc/ChangeDataCaptureSelfTest.java ########## @@ -0,0 +1,494 @@ +/* + * 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 java.util.concurrent.atomic.AtomicInteger; +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.Ignore; +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 TestCDCConsumer()); + + // Read one record per call. + readAll(new TestCDCConsumer() { + @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 TestCDCConsumer() { + @Override public boolean onEvents(Iterator<ChangeDataCaptureEvent> evts) { + super.onEvents(Collections.singleton(evts.next()).iterator()); + + return true; + } + }); + } + + /** */ + private void readAll(TestCDCConsumer 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); + + stopGrid("ignite-0"); + + cleanPersistenceDir(); + } + + /** */ + @Test + public void testReadBeforeStop() throws Exception { + IgniteConfiguration cfg = getConfiguration("ignite-0"); + + Ignite ign = startGrid(cfg); + + ign.cluster().state(ACTIVE); + + CountDownLatch startLatch = new CountDownLatch(1); + CountDownLatch onChangeLatch1 = new CountDownLatch(1); + CountDownLatch onChangeLatch2 = new CountDownLatch(1); + + TestCDCConsumer cnsmr = new TestCDCConsumer() { + @Override public void start() { + try { + startLatch.await(getTestTimeout(), TimeUnit.MILLISECONDS); + } + catch (InterruptedException e) { + throw new RuntimeException(e); + } + + super.start(); + } + + @Override public boolean onEvents(Iterator<ChangeDataCaptureEvent> evts) { + onChangeLatch1.countDown(); + + try { + onChangeLatch2.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); + + Thread.sleep(2 * WAL_ARCHIVE_TIMEOUT); + + startLatch.countDown(); + + onChangeLatch1.await(getTestTimeout(), TimeUnit.MILLISECONDS); + + cdc.stop(); + + onChangeLatch1.await(getTestTimeout(), TimeUnit.MILLISECONDS); + onChangeLatch2.countDown(); + + assertTrue(waitForSize(KEYS_CNT, DEFAULT_CACHE_NAME, UPDATE, getTestTimeout(), cnsmr)); + assertTrue(cnsmr.stopped); + + List<Integer> keys = cnsmr.keys(UPDATE, cacheId(DEFAULT_CACHE_NAME)); + + assertEquals(KEYS_CNT, keys.size()); + + for (int i = 0; i < KEYS_CNT; i++) + assertTrue(keys.contains(i)); + } + + /** */ + @Test + @Ignore("Not implemented yet") + public void testReadAfterNodeStop() throws Exception { + cleanPersistenceDir(); + + AtomicInteger cnt = new AtomicInteger(); + + TestCDCConsumer cnsmr = new TestCDCConsumer(); + + // Restart node several time to make sure we can continue after gracefull shutdown. + for (int restarts = 0; restarts < 2; restarts++) { + IgniteConfiguration cfg = getConfiguration("ignite-0"); + + Ignite ign = startGrid(cfg); + + ign.cluster().state(ACTIVE); + + long startCnt = cnt.get(); + + runAsync(() -> { + IgniteCache<Integer, User> cache = ign.getOrCreateCache(DEFAULT_CACHE_NAME); + + while (true) { + int key = cnt.getAndIncrement(); + + try { + cache.put(key, createUser(key)); + } + catch (Exception e) { + cnt.decrementAndGet(); + + throw e; + } + } + }); + + waitForCondition(() -> cnt.get() - startCnt > KEYS_CNT, getTestTimeout()); + + ign.close(); + + ChangeDataCapture cdc = new ChangeDataCapture(cfg, null, cdcConfig(cnsmr)); + + IgniteInternalFuture<?> fut = runAsync(cdc); + + assertTrue(waitForSize(cnt.get(), DEFAULT_CACHE_NAME, UPDATE, getTestTimeout(), cnsmr)); + + fut.cancel(); + + List<Integer> keys = cnsmr.keys(UPDATE, cacheId(DEFAULT_CACHE_NAME)); + + assertTrue(cnt.get() <= keys.size()); Review comment: Moved to https://issues.apache.org/jira/browse/IGNITE-14825 -- 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]
