anton-vinogradov commented on a change in pull request #8909: URL: https://github.com/apache/ignite/pull/8909#discussion_r638581945
########## File path: modules/core/src/main/java/org/apache/ignite/cdc/ChangeEventOrder.java ########## @@ -0,0 +1,44 @@ +/* + * 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.io.Serializable; +import org.apache.ignite.lang.IgniteExperimental; + +/** + * Entry event order. + * Two concurrent updates of the same entry can be ordered based on {@link ChangeEventOrder} comparsion. + * Greater value means that event occurs later. + */ +@IgniteExperimental +public interface ChangeEventOrder extends Comparable<ChangeEventOrder>, Serializable { + /** @return topVer Topology version plus number of seconds from the start time of the first grid node. */ + public int topologyVersion(); + + /** @return Node order on which this version was assigned. */ + public int nodeOrder(); + + /** @return Data center id. */ + public byte dataCenterId(); + + /** @return order Version order. */ Review comment: proper javadoc required ########## File path: modules/core/src/main/java/org/apache/ignite/cdc/ChangeDataCaptureEvent.java ########## @@ -0,0 +1,67 @@ +/* + * 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.io.Serializable; +import org.apache.ignite.internal.cdc.ChangeDataCapture; +import org.apache.ignite.lang.IgniteExperimental; +import org.jetbrains.annotations.Nullable; + +/** + * Event of single entry change. + * Instance presents new value of modified entry. + * + * @see ChangeDataCapture + * @see ChangeDataCaptureConsumer + */ +@IgniteExperimental +public interface ChangeDataCaptureEvent extends Serializable { + /** + * @return Key for the changed entry. + */ + public Object key(); + + /** + * @return Value for the changed entry or {@code null} in case of entry removal. + */ + @Nullable public Object value(); + + /** + * @return {@code True} if event fired on primary node for partition containing this entry. + * @see <a href=" + * https://ignite.apache.org/docs/latest/configuring-caches/configuring-backups#configuring-partition-backups"> + * Configuring partition backups.</a> + */ + public boolean primary(); + + /** + * @return Partition number. + */ + public int partition(); Review comment: Partition is an Ignite internal feature. You may use `key.hashCode` to spread your load or just use a round-robin. If the partition is just one more convenient way, please explain this at Javadoc. ########## File path: modules/core/src/main/java/org/apache/ignite/cdc/ChangeEventOrder.java ########## @@ -0,0 +1,44 @@ +/* + * 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; Review comment: not sure that *.cdc is a proper package. See no special CDC features here. ########## File path: modules/core/src/main/java/org/apache/ignite/internal/cdc/WALRecordsConsumer.java ########## @@ -0,0 +1,136 @@ +/* + * 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.internal.cdc; + +import java.util.EnumSet; +import java.util.Iterator; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.cdc.ChangeDataCaptureConsumer; +import org.apache.ignite.cdc.ChangeDataCaptureEvent; +import org.apache.ignite.internal.pagemem.wal.record.DataEntry; +import org.apache.ignite.internal.pagemem.wal.record.DataRecord; +import org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry; +import org.apache.ignite.internal.pagemem.wal.record.WALRecord; +import org.apache.ignite.internal.processors.cache.GridCacheOperation; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; + +import static org.apache.ignite.internal.processors.cache.GridCacheOperation.CREATE; +import static org.apache.ignite.internal.processors.cache.GridCacheOperation.DELETE; +import static org.apache.ignite.internal.processors.cache.GridCacheOperation.TRANSFORM; +import static org.apache.ignite.internal.processors.cache.GridCacheOperation.UPDATE; + +/** + * Transform {@link DataEntry} to {@link ChangeDataCaptureEvent} and sends it to {@link ChangeDataCaptureConsumer}. + * + * @see ChangeDataCapture + * @see ChangeDataCaptureConsumer + */ +public class WALRecordsConsumer<K, V> { + /** Ignite logger. */ + private IgniteLogger log; + + /** Data change events consumer. */ + private final ChangeDataCaptureConsumer cdcConsumer; + + /** Operations types we interested in. */ + private static final EnumSet<GridCacheOperation> OPS_TYPES = EnumSet.of(CREATE, UPDATE, DELETE, TRANSFORM); + + /** Operations filter. */ + private static final IgnitePredicate<? super DataEntry> OPS_FILTER = e -> { Review comment: OPERATIONS_FILTER? ########## File path: modules/core/src/main/java/org/apache/ignite/cdc/ChangeEventOrder.java ########## @@ -0,0 +1,44 @@ +/* + * 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.io.Serializable; +import org.apache.ignite.lang.IgniteExperimental; + +/** + * Entry event order. + * Two concurrent updates of the same entry can be ordered based on {@link ChangeEventOrder} comparsion. + * Greater value means that event occurs later. + */ +@IgniteExperimental +public interface ChangeEventOrder extends Comparable<ChangeEventOrder>, Serializable { + /** @return topVer Topology version plus number of seconds from the start time of the first grid node. */ + public int topologyVersion(); + + /** @return Node order on which this version was assigned. */ + public int nodeOrder(); + + /** @return Data center id. */ + public byte dataCenterId(); + + /** @return order Version order. */ + public long order(); + + /** @return Replication version. */ + public ChangeEventOrder otherDcOrder(); Review comment: What is Dc? We should not use abbreviations at method names. Also, I see no usages of this method and implementation is not clear for me too. ########## File path: modules/indexing/src/test/java/org/apache/ignite/internal/cdc/SQLChangeDataCaptureTest.java ########## @@ -0,0 +1,250 @@ +/* + * 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.internal.cdc; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.cache.query.SqlFieldsQuery; +import org.apache.ignite.cdc.ChangeDataCaptureConfiguration; +import org.apache.ignite.cdc.ChangeDataCaptureConsumer; +import org.apache.ignite.cdc.ChangeDataCaptureEvent; +import org.apache.ignite.cdc.ChangeDataCaptureSelfTest.ChangeEventType; +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.IgniteInterruptedCheckedException; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.lang.IgniteBiTuple; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.junit.Test; + +import static org.apache.ignite.cdc.ChangeDataCaptureSelfTest.ChangeEventType.DELETE; +import static org.apache.ignite.cdc.ChangeDataCaptureSelfTest.ChangeEventType.UPDATE; +import static org.apache.ignite.cdc.ChangeDataCaptureSelfTest.WAL_ARCHIVE_TIMEOUT; +import static org.apache.ignite.cluster.ClusterState.ACTIVE; +import static org.apache.ignite.internal.processors.cache.GridCacheUtils.cacheId; +import static org.apache.ignite.internal.processors.cache.distributed.dht.GridCacheAbstractTransformWriteThroughSelfTest.KEYS_CNT; +import static org.apache.ignite.testframework.GridTestUtils.runAsync; +import static org.apache.ignite.testframework.GridTestUtils.waitForCondition; + +/** */ +public class SQLChangeDataCaptureTest extends GridCommonAbstractTest { + /** */ + private static final String JOHN = "John Connor"; + + /** */ + private static final String SARAH = "Sarah Connor"; + + /** */ + public static final String USER = "user"; + + /** */ + public static final String CITY = "city"; + + /** */ + public static final String SPB = "Saint-Petersburg"; + + /** */ + public static final String MSK = "Moscow"; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + + int segmentSz = 10 * 1024 * 1024; + + cfg.setDataStorageConfiguration(new DataStorageConfiguration() + .setCdcEnabled(true) + .setWalMode(WALMode.FSYNC) + .setMaxWalArchiveSize(10 * segmentSz) + .setWalSegmentSize(segmentSz) + .setWalForceArchiveTimeout(WAL_ARCHIVE_TIMEOUT) + .setDefaultDataRegionConfiguration(new DataRegionConfiguration() + .setPersistenceEnabled(true))); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + cleanPersistenceDir(); + } + + /** + * Simplest CDC test. + */ + @Test + public void testReadAllSQLRows() throws Exception { + IgniteConfiguration cfg = getConfiguration("ignite-0"); + + IgniteEx ign = startGrid(cfg); + + ign.cluster().state(ACTIVE); + + BinaryCDCConsumer cnsmr = new BinaryCDCConsumer(); + + ChangeDataCaptureConfiguration cdcCfg = new ChangeDataCaptureConfiguration(); + + cdcCfg.setConsumer(cnsmr); + cdcCfg.setKeepBinary(true); + + ChangeDataCapture cdc = new ChangeDataCapture(cfg, null, cdcCfg); + + IgniteInternalFuture<?> fut = runAsync(cdc); + + executeSql( + ign, + "CREATE TABLE USER(id int, city_id int, name varchar, PRIMARY KEY (id, city_id)) WITH \"CACHE_NAME=user\"" + ); + + executeSql( + ign, + "CREATE TABLE CITY(id int, name varchar, zip_code varchar(6), PRIMARY KEY (id)) WITH \"CACHE_NAME=city\"" + ); + + for (int i = 0; i < KEYS_CNT * 2; i++) { + executeSql( + ign, + "INSERT INTO USER VALUES(?, ?, ?)", + i, + 42 * i, + (i % 2 == 0 ? JOHN : SARAH) + i); + + executeSql( + ign, + "INSERT INTO CITY VALUES(?, ?, ?)", + i, + (i % 2 == 0 ? MSK : SPB) + i, + Integer.toString(127000 + i)); + } + + assertTrue(waitForSize(KEYS_CNT * 2, USER, UPDATE, cnsmr)); + assertTrue(waitForSize(KEYS_CNT * 2, CITY, UPDATE, cnsmr)); + + fut.cancel(); + + assertEquals(KEYS_CNT * 2, cnsmr.data(UPDATE, cacheId(USER)).size()); + assertEquals(KEYS_CNT * 2, cnsmr.data(UPDATE, cacheId(CITY)).size()); + + assertTrue(cnsmr.stopped); + + for (int i = 0; i < KEYS_CNT; i++) + executeSql(ign, "DELETE FROM USER WHERE id = ?", i); + + IgniteInternalFuture<?> rmvFut = runAsync(cdc); + + assertTrue(waitForSize(KEYS_CNT, USER, DELETE, cnsmr)); + + rmvFut.cancel(); + + assertTrue(cnsmr.stopped); + } + + /** */ + private boolean waitForSize( + int expSz, + String cacheName, + ChangeEventType evtType, + BinaryCDCConsumer cnsmr + ) throws IgniteInterruptedCheckedException { + return waitForCondition( + () -> F.size(cnsmr.data(evtType, cacheId(cacheName))) >= expSz, Review comment: could we have an equality check instead >=? ########## File path: modules/core/src/main/java/org/apache/ignite/cdc/ChangeEventOrder.java ########## @@ -0,0 +1,44 @@ +/* + * 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.io.Serializable; +import org.apache.ignite.lang.IgniteExperimental; + +/** + * Entry event order. + * Two concurrent updates of the same entry can be ordered based on {@link ChangeEventOrder} comparsion. + * Greater value means that event occurs later. + */ +@IgniteExperimental +public interface ChangeEventOrder extends Comparable<ChangeEventOrder>, Serializable { Review comment: Not sure that *Order is a good name. It's just a CacheEntryVersion -- 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]
