sanpwc commented on code in PR #5134: URL: https://github.com/apache/ignite-3/pull/5134#discussion_r1938534470
########## modules/partition-replicator/src/main/java/org/apache/ignite/internal/partition/replicator/raft/ZonePartitionRaftListener.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.partition.replicator.raft; + +import static org.apache.ignite.internal.tx.TxState.ABORTED; +import static org.apache.ignite.internal.tx.TxState.COMMITTED; + +import java.io.Serializable; +import java.nio.file.Path; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.partition.replicator.network.command.FinishTxCommand; +import org.apache.ignite.internal.partition.replicator.network.command.UpdateAllCommand; +import org.apache.ignite.internal.partition.replicator.network.command.UpdateCommand; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.CommittedConfiguration; +import org.apache.ignite.internal.raft.service.RaftGroupListener; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.replicator.message.PrimaryReplicaChangeCommand; +import org.apache.ignite.internal.tx.TransactionResult; + +/** + * RAFT listener for the zone partition. + */ +public class ZonePartitionRaftListener implements RaftGroupListener { + private static final IgniteLogger LOG = Loggers.forClass(ZonePartitionRaftListener.class); + + private final Map<TablePartitionId, RaftGroupListener> tablePartitionRaftListeners = new ConcurrentHashMap<>(); + + /** + * Latest committed configuration of the zone-wide Raft group. + * + * <p>Multi-threaded access is guarded by {@link #commitedConfigurationLock}. + */ + private CommittedConfiguration currentCommitedConfiguration; + + private final Object commitedConfigurationLock = new Object(); + + @Override + public void onRead(Iterator<CommandClosure<ReadCommand>> iterator) { + iterator.forEachRemaining(clo -> { + Command command = clo.command(); + + assert false : "No read commands expected, [cmd=" + command + ']'; + }); + } + + @Override + public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) { + iterator.forEachRemaining(clo -> { + try { + processWriteCommand(clo); + } catch (Throwable t) { + LOG.error( + "Unknown error while processing command [commandIndex={}, commandTerm={}, command={}]", + t, + clo.index(), clo.index(), clo.command() + ); + + clo.result(t); + + throw t; + } + }); + } + + private void processWriteCommand(CommandClosure<WriteCommand> clo) { + Command command = clo.command(); + + if (command instanceof FinishTxCommand) { + FinishTxCommand cmd = (FinishTxCommand) command; + + clo.result(new TransactionResult(cmd.commit() ? COMMITTED : ABORTED, cmd.commitTimestamp())); + } else if (command instanceof PrimaryReplicaChangeCommand) { + // This is a hack for tests, this command is not issued in production because no zone-wide placement driver exists yet. + CommandClosure<WriteCommand> idempotentCommandClosure = idempotentCommandClosure(clo); + + tablePartitionRaftListeners.values().forEach(listener -> listener.onWrite(singletonIterator(idempotentCommandClosure))); + + clo.result(null); + } else if (command instanceof UpdateCommand) { + TablePartitionId tablePartitionId = ((UpdateCommand) command).tablePartitionId().asTablePartitionId(); + + processTableSpecificCommand(tablePartitionId, clo); + } else if (command instanceof UpdateAllCommand) { + TablePartitionId tablePartitionId = ((UpdateAllCommand) command).tablePartitionId().asTablePartitionId(); + + processTableSpecificCommand(tablePartitionId, clo); + } else { + LOG.info("Message type " + command.getClass() + " is not supported by the zone partition RAFT listener yet"); + + clo.result(null); + } + } + + private void processTableSpecificCommand(TablePartitionId tablePartitionId, CommandClosure<WriteCommand> clo) { + tablePartitionRaftListeners.get(tablePartitionId).onWrite(singletonIterator(clo)); + } + + private static <T> Iterator<T> singletonIterator(T value) { + return Collections.singleton(value).iterator(); + } + + private static CommandClosure<WriteCommand> idempotentCommandClosure(CommandClosure<WriteCommand> clo) { + return new CommandClosure<>() { + @Override + public WriteCommand command() { + return clo.command(); + } + + @Override + public long index() { + return clo.index(); + } + + @Override + public long term() { + return clo.term(); + } + + @Override + public void result(Serializable res) { + } + }; + } + + @Override + public void onConfigurationCommitted(CommittedConfiguration config) { + synchronized (commitedConfigurationLock) { + currentCommitedConfiguration = config; + + tablePartitionRaftListeners.values().forEach(listener -> listener.onConfigurationCommitted(config)); Review Comment: Curious whether it's reasonable to parallelise this. For now, it'd left it simple though. ########## modules/partition-replicator/src/main/java/org/apache/ignite/internal/partition/replicator/raft/ZonePartitionRaftListener.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.partition.replicator.raft; + +import static org.apache.ignite.internal.tx.TxState.ABORTED; +import static org.apache.ignite.internal.tx.TxState.COMMITTED; + +import java.io.Serializable; +import java.nio.file.Path; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.partition.replicator.network.command.FinishTxCommand; +import org.apache.ignite.internal.partition.replicator.network.command.UpdateAllCommand; +import org.apache.ignite.internal.partition.replicator.network.command.UpdateCommand; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.CommittedConfiguration; +import org.apache.ignite.internal.raft.service.RaftGroupListener; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.replicator.message.PrimaryReplicaChangeCommand; +import org.apache.ignite.internal.tx.TransactionResult; + +/** + * RAFT listener for the zone partition. + */ +public class ZonePartitionRaftListener implements RaftGroupListener { + private static final IgniteLogger LOG = Loggers.forClass(ZonePartitionRaftListener.class); + + private final Map<TablePartitionId, RaftGroupListener> tablePartitionRaftListeners = new ConcurrentHashMap<>(); + + /** + * Latest committed configuration of the zone-wide Raft group. + * + * <p>Multi-threaded access is guarded by {@link #commitedConfigurationLock}. + */ + private CommittedConfiguration currentCommitedConfiguration; + + private final Object commitedConfigurationLock = new Object(); + + @Override + public void onRead(Iterator<CommandClosure<ReadCommand>> iterator) { + iterator.forEachRemaining(clo -> { + Command command = clo.command(); + + assert false : "No read commands expected, [cmd=" + command + ']'; + }); + } + + @Override + public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) { + iterator.forEachRemaining(clo -> { + try { + processWriteCommand(clo); + } catch (Throwable t) { + LOG.error( + "Unknown error while processing command [commandIndex={}, commandTerm={}, command={}]", + t, + clo.index(), clo.index(), clo.command() + ); + + clo.result(t); + + throw t; + } + }); + } + + private void processWriteCommand(CommandClosure<WriteCommand> clo) { + Command command = clo.command(); + + if (command instanceof FinishTxCommand) { + FinishTxCommand cmd = (FinishTxCommand) command; + + clo.result(new TransactionResult(cmd.commit() ? COMMITTED : ABORTED, cmd.commitTimestamp())); + } else if (command instanceof PrimaryReplicaChangeCommand) { + // This is a hack for tests, this command is not issued in production because no zone-wide placement driver exists yet. + CommandClosure<WriteCommand> idempotentCommandClosure = idempotentCommandClosure(clo); + + tablePartitionRaftListeners.values().forEach(listener -> listener.onWrite(singletonIterator(idempotentCommandClosure))); + + clo.result(null); + } else if (command instanceof UpdateCommand) { + TablePartitionId tablePartitionId = ((UpdateCommand) command).tablePartitionId().asTablePartitionId(); Review Comment: I guess that we may introduce `TableAwareCommand` interface with `TablePartitionIdMessage tablePartitionId();` and thus simply instanceof UpdateCommand, UpdateAllCommand and others to be supported. ########## modules/partition-replicator/src/integrationTest/java/org/apache/ignite/internal/partition/replicator/ItReplicaLifecycleTest.java: ########## @@ -262,70 +257,6 @@ private void stopNode(int idx) { nodes.remove(idx); } - @Test - public void testZoneReplicaListener(TestInfo testInfo) throws Exception { Review Comment: Why is it removed? It's not the equivalent of testReplicationOnAllNodes. ########## modules/partition-replicator/src/main/java/org/apache/ignite/internal/partition/replicator/raft/ZonePartitionRaftListener.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.partition.replicator.raft; + +import static org.apache.ignite.internal.tx.TxState.ABORTED; +import static org.apache.ignite.internal.tx.TxState.COMMITTED; + +import java.io.Serializable; +import java.nio.file.Path; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.partition.replicator.network.command.FinishTxCommand; +import org.apache.ignite.internal.partition.replicator.network.command.UpdateAllCommand; +import org.apache.ignite.internal.partition.replicator.network.command.UpdateCommand; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.CommittedConfiguration; +import org.apache.ignite.internal.raft.service.RaftGroupListener; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.replicator.message.PrimaryReplicaChangeCommand; +import org.apache.ignite.internal.tx.TransactionResult; + +/** + * RAFT listener for the zone partition. + */ +public class ZonePartitionRaftListener implements RaftGroupListener { + private static final IgniteLogger LOG = Loggers.forClass(ZonePartitionRaftListener.class); + + private final Map<TablePartitionId, RaftGroupListener> tablePartitionRaftListeners = new ConcurrentHashMap<>(); + + /** + * Latest committed configuration of the zone-wide Raft group. + * + * <p>Multi-threaded access is guarded by {@link #commitedConfigurationLock}. + */ + private CommittedConfiguration currentCommitedConfiguration; + + private final Object commitedConfigurationLock = new Object(); + + @Override + public void onRead(Iterator<CommandClosure<ReadCommand>> iterator) { + iterator.forEachRemaining(clo -> { + Command command = clo.command(); + + assert false : "No read commands expected, [cmd=" + command + ']'; + }); + } + + @Override + public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) { + iterator.forEachRemaining(clo -> { + try { + processWriteCommand(clo); + } catch (Throwable t) { + LOG.error( + "Unknown error while processing command [commandIndex={}, commandTerm={}, command={}]", + t, + clo.index(), clo.index(), clo.command() + ); + + clo.result(t); + + throw t; + } + }); + } + + private void processWriteCommand(CommandClosure<WriteCommand> clo) { + Command command = clo.command(); + + if (command instanceof FinishTxCommand) { + FinishTxCommand cmd = (FinishTxCommand) command; + + clo.result(new TransactionResult(cmd.commit() ? COMMITTED : ABORTED, cmd.commitTimestamp())); + } else if (command instanceof PrimaryReplicaChangeCommand) { + // This is a hack for tests, this command is not issued in production because no zone-wide placement driver exists yet. + CommandClosure<WriteCommand> idempotentCommandClosure = idempotentCommandClosure(clo); + + tablePartitionRaftListeners.values().forEach(listener -> listener.onWrite(singletonIterator(idempotentCommandClosure))); + + clo.result(null); + } else if (command instanceof UpdateCommand) { + TablePartitionId tablePartitionId = ((UpdateCommand) command).tablePartitionId().asTablePartitionId(); + + processTableSpecificCommand(tablePartitionId, clo); + } else if (command instanceof UpdateAllCommand) { + TablePartitionId tablePartitionId = ((UpdateAllCommand) command).tablePartitionId().asTablePartitionId(); + + processTableSpecificCommand(tablePartitionId, clo); + } else { + LOG.info("Message type " + command.getClass() + " is not supported by the zone partition RAFT listener yet"); + + clo.result(null); + } + } + + private void processTableSpecificCommand(TablePartitionId tablePartitionId, CommandClosure<WriteCommand> clo) { + tablePartitionRaftListeners.get(tablePartitionId).onWrite(singletonIterator(clo)); Review Comment: Seems that we may batch consecutive closures if they belong to the same table in the original zone closure. ########## modules/partition-replicator/src/main/java/org/apache/ignite/internal/partition/replicator/PartitionReplicaLifecycleManager.java: ########## @@ -210,6 +212,8 @@ public class PartitionReplicaLifecycleManager extends /** Configuration of rebalance retries delay. */ private final SystemDistributedConfigurationPropertyHolder<Integer> rebalanceRetryDelayConfiguration; + private final ConcurrentMap<ZonePartitionId, ZonePartitionRaftListener> zonePartitionRaftListeners = new ConcurrentHashMap<>(); Review Comment: It's an encapsulation leek, we should have raft listeners inside replicas. Why do you need it? ########## modules/partition-replicator/src/main/java/org/apache/ignite/internal/partition/replicator/raft/ZonePartitionRaftListener.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.partition.replicator.raft; + +import static org.apache.ignite.internal.tx.TxState.ABORTED; +import static org.apache.ignite.internal.tx.TxState.COMMITTED; + +import java.io.Serializable; +import java.nio.file.Path; +import java.util.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.partition.replicator.network.command.FinishTxCommand; +import org.apache.ignite.internal.partition.replicator.network.command.UpdateAllCommand; +import org.apache.ignite.internal.partition.replicator.network.command.UpdateCommand; +import org.apache.ignite.internal.raft.Command; +import org.apache.ignite.internal.raft.ReadCommand; +import org.apache.ignite.internal.raft.WriteCommand; +import org.apache.ignite.internal.raft.service.CommandClosure; +import org.apache.ignite.internal.raft.service.CommittedConfiguration; +import org.apache.ignite.internal.raft.service.RaftGroupListener; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.replicator.message.PrimaryReplicaChangeCommand; +import org.apache.ignite.internal.tx.TransactionResult; + +/** + * RAFT listener for the zone partition. + */ +public class ZonePartitionRaftListener implements RaftGroupListener { + private static final IgniteLogger LOG = Loggers.forClass(ZonePartitionRaftListener.class); + + private final Map<TablePartitionId, RaftGroupListener> tablePartitionRaftListeners = new ConcurrentHashMap<>(); + + /** + * Latest committed configuration of the zone-wide Raft group. + * + * <p>Multi-threaded access is guarded by {@link #commitedConfigurationLock}. + */ + private CommittedConfiguration currentCommitedConfiguration; + + private final Object commitedConfigurationLock = new Object(); + + @Override + public void onRead(Iterator<CommandClosure<ReadCommand>> iterator) { + iterator.forEachRemaining(clo -> { + Command command = clo.command(); + + assert false : "No read commands expected, [cmd=" + command + ']'; + }); + } + + @Override + public void onWrite(Iterator<CommandClosure<WriteCommand>> iterator) { + iterator.forEachRemaining(clo -> { + try { + processWriteCommand(clo); + } catch (Throwable t) { + LOG.error( + "Unknown error while processing command [commandIndex={}, commandTerm={}, command={}]", + t, + clo.index(), clo.index(), clo.command() + ); + + clo.result(t); + + throw t; + } + }); + } + + private void processWriteCommand(CommandClosure<WriteCommand> clo) { + Command command = clo.command(); + + if (command instanceof FinishTxCommand) { + FinishTxCommand cmd = (FinishTxCommand) command; + + clo.result(new TransactionResult(cmd.commit() ? COMMITTED : ABORTED, cmd.commitTimestamp())); + } else if (command instanceof PrimaryReplicaChangeCommand) { + // This is a hack for tests, this command is not issued in production because no zone-wide placement driver exists yet. Review Comment: Do you need it only in order for implicit transactions to work? The ones that you've introduced in ItZoneDataReplicationTest.java. ########## modules/partition-replicator/src/integrationTest/java/org/apache/ignite/internal/partition/replicator/ItZoneDataReplicationTest.java: ########## @@ -0,0 +1,426 @@ +/* + * 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.partition.replicator; + +import static java.util.concurrent.CompletableFuture.allOf; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; +import static org.apache.ignite.internal.catalog.CatalogService.DEFAULT_STORAGE_PROFILE; +import static org.apache.ignite.internal.distributionzones.DistributionZonesTestUtil.createZoneWithStorageProfile; +import static org.apache.ignite.internal.distributionzones.DistributionZonesTestUtil.getZoneId; +import static org.apache.ignite.internal.partition.replicator.PartitionReplicaLifecycleManager.FEATURE_FLAG_NAME; +import static org.apache.ignite.internal.sql.SqlCommon.DEFAULT_SCHEMA_NAME; +import static org.apache.ignite.internal.table.TableTestUtils.getTableId; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.util.IgniteUtils.closeAll; +import static org.apache.ignite.sql.ColumnType.INT32; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ScheduledExecutorService; +import java.util.function.Function; +import java.util.stream.IntStream; +import org.apache.ignite.internal.catalog.commands.ColumnParams; +import org.apache.ignite.internal.cluster.management.configuration.NodeAttributesConfiguration; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologySnapshot; +import org.apache.ignite.internal.configuration.SystemLocalConfiguration; +import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension; +import org.apache.ignite.internal.configuration.testframework.InjectConfiguration; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.metastorage.configuration.MetaStorageConfiguration; +import org.apache.ignite.internal.network.NodeFinder; +import org.apache.ignite.internal.network.StaticNodeFinder; +import org.apache.ignite.internal.partition.replicator.fixtures.Node; +import org.apache.ignite.internal.partition.replicator.fixtures.TestPlacementDriver; +import org.apache.ignite.internal.raft.configuration.RaftConfiguration; +import org.apache.ignite.internal.replicator.Member; +import org.apache.ignite.internal.replicator.ReplicationGroupId; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.replicator.ZonePartitionId; +import org.apache.ignite.internal.replicator.configuration.ReplicationConfiguration; +import org.apache.ignite.internal.replicator.message.PrimaryReplicaChangeCommand; +import org.apache.ignite.internal.replicator.message.ReplicaMessagesFactory; +import org.apache.ignite.internal.replicator.message.ReplicaRequest; +import org.apache.ignite.internal.storage.configurations.StorageConfiguration; +import org.apache.ignite.internal.table.TableTestUtils; +import org.apache.ignite.internal.testframework.ExecutorServiceExtension; +import org.apache.ignite.internal.testframework.IgniteAbstractTest; +import org.apache.ignite.internal.testframework.InjectExecutorService; +import org.apache.ignite.internal.testframework.SystemPropertiesExtension; +import org.apache.ignite.internal.testframework.WithSystemProperty; +import org.apache.ignite.internal.tx.configuration.TransactionConfiguration; +import org.apache.ignite.internal.tx.message.WriteIntentSwitchReplicaRequest; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.NetworkAddress; +import org.apache.ignite.table.KeyValueView; +import org.jetbrains.annotations.Nullable; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * Class containing tests related to Raft-based replication for the Colocation feature. + */ +@ExtendWith(ConfigurationExtension.class) +@ExtendWith(ExecutorServiceExtension.class) +@ExtendWith(SystemPropertiesExtension.class) +// TODO: https://issues.apache.org/jira/browse/IGNITE-22522 remove this test after the switching to zone-based replication +@Disabled("https://issues.apache.org/jira/browse/IGNITE-23252") Review Comment: Is it really unstable? ########## modules/partition-replicator/src/integrationTest/java/org/apache/ignite/internal/partition/replicator/ItZoneDataReplicationTest.java: ########## @@ -0,0 +1,426 @@ +/* + * 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.partition.replicator; + +import static java.util.concurrent.CompletableFuture.allOf; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; +import static org.apache.ignite.internal.catalog.CatalogService.DEFAULT_STORAGE_PROFILE; +import static org.apache.ignite.internal.distributionzones.DistributionZonesTestUtil.createZoneWithStorageProfile; +import static org.apache.ignite.internal.distributionzones.DistributionZonesTestUtil.getZoneId; +import static org.apache.ignite.internal.partition.replicator.PartitionReplicaLifecycleManager.FEATURE_FLAG_NAME; +import static org.apache.ignite.internal.sql.SqlCommon.DEFAULT_SCHEMA_NAME; +import static org.apache.ignite.internal.table.TableTestUtils.getTableId; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.internal.util.IgniteUtils.closeAll; +import static org.apache.ignite.sql.ColumnType.INT32; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ScheduledExecutorService; +import java.util.function.Function; +import java.util.stream.IntStream; +import org.apache.ignite.internal.catalog.commands.ColumnParams; +import org.apache.ignite.internal.cluster.management.configuration.NodeAttributesConfiguration; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologySnapshot; +import org.apache.ignite.internal.configuration.SystemLocalConfiguration; +import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension; +import org.apache.ignite.internal.configuration.testframework.InjectConfiguration; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.metastorage.configuration.MetaStorageConfiguration; +import org.apache.ignite.internal.network.NodeFinder; +import org.apache.ignite.internal.network.StaticNodeFinder; +import org.apache.ignite.internal.partition.replicator.fixtures.Node; +import org.apache.ignite.internal.partition.replicator.fixtures.TestPlacementDriver; +import org.apache.ignite.internal.raft.configuration.RaftConfiguration; +import org.apache.ignite.internal.replicator.Member; +import org.apache.ignite.internal.replicator.ReplicationGroupId; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.replicator.ZonePartitionId; +import org.apache.ignite.internal.replicator.configuration.ReplicationConfiguration; +import org.apache.ignite.internal.replicator.message.PrimaryReplicaChangeCommand; +import org.apache.ignite.internal.replicator.message.ReplicaMessagesFactory; +import org.apache.ignite.internal.replicator.message.ReplicaRequest; +import org.apache.ignite.internal.storage.configurations.StorageConfiguration; +import org.apache.ignite.internal.table.TableTestUtils; +import org.apache.ignite.internal.testframework.ExecutorServiceExtension; +import org.apache.ignite.internal.testframework.IgniteAbstractTest; +import org.apache.ignite.internal.testframework.InjectExecutorService; +import org.apache.ignite.internal.testframework.SystemPropertiesExtension; +import org.apache.ignite.internal.testframework.WithSystemProperty; +import org.apache.ignite.internal.tx.configuration.TransactionConfiguration; +import org.apache.ignite.internal.tx.message.WriteIntentSwitchReplicaRequest; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.NetworkAddress; +import org.apache.ignite.table.KeyValueView; +import org.jetbrains.annotations.Nullable; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * Class containing tests related to Raft-based replication for the Colocation feature. + */ +@ExtendWith(ConfigurationExtension.class) +@ExtendWith(ExecutorServiceExtension.class) +@ExtendWith(SystemPropertiesExtension.class) +// TODO: https://issues.apache.org/jira/browse/IGNITE-22522 remove this test after the switching to zone-based replication +@Disabled("https://issues.apache.org/jira/browse/IGNITE-23252") +@WithSystemProperty(key = FEATURE_FLAG_NAME, value = "true") +public class ItZoneDataReplicationTest extends IgniteAbstractTest { + private static final int BASE_PORT = 20_000; + + private static final String TEST_ZONE_NAME = "TEST_ZONE"; + + private static final String TEST_TABLE_NAME1 = "TEST_TABLE_1"; + + private static final String TEST_TABLE_NAME2 = "TEST_TABLE_2"; + + private static final ReplicaMessagesFactory REPLICA_MESSAGES_FACTORY = new ReplicaMessagesFactory(); + + @InjectConfiguration + private static TransactionConfiguration txConfiguration; + + @InjectConfiguration + private static RaftConfiguration raftConfiguration; + + @InjectConfiguration + private static SystemLocalConfiguration systemConfiguration; + + @InjectConfiguration + private static NodeAttributesConfiguration nodeAttributesConfiguration; + + @InjectConfiguration + private static ReplicationConfiguration replicationConfiguration; + + @InjectConfiguration + private static MetaStorageConfiguration metaStorageConfiguration; + + @InjectConfiguration("mock.profiles = {" + DEFAULT_STORAGE_PROFILE + ".engine = aipersist, test.engine=test}") + private static StorageConfiguration storageConfiguration; + + @InjectExecutorService + private static ScheduledExecutorService scheduledExecutorService; + + private final List<Node> cluster = new ArrayList<>(); + + private final TestPlacementDriver placementDriver = new TestPlacementDriver(); + + private NodeFinder nodeFinder; + + @AfterEach + void tearDown() throws Exception { + closeAll(cluster.parallelStream().map(node -> node::stop)); + } + + private void startCluster(TestInfo testInfo, int size) throws Exception { + List<NetworkAddress> addresses = IntStream.range(0, size) + .mapToObj(i -> new NetworkAddress("localhost", BASE_PORT + i)) + .collect(toList()); + + nodeFinder = new StaticNodeFinder(addresses); + + IntStream.range(0, size) + .mapToObj(i -> newNode(testInfo, addresses.get(i), nodeFinder)) + .forEach(cluster::add); + + cluster.parallelStream().forEach(Node::start); + + Node node0 = cluster.get(0); + + node0.cmgManager.initCluster(List.of(node0.name), List.of(node0.name), "cluster"); + + setPrimaryReplica(node0, null); + + cluster.forEach(Node::waitWatches); + + assertThat( + allOf(cluster.stream().map(n -> n.cmgManager.onJoinReady()).toArray(CompletableFuture[]::new)), + willCompleteSuccessfully() + ); + + assertTrue(waitForCondition( + () -> { + CompletableFuture<LogicalTopologySnapshot> logicalTopologyFuture = node0.cmgManager.logicalTopology(); + + assertThat(logicalTopologyFuture, willCompleteSuccessfully()); + + return logicalTopologyFuture.join().nodes().size() == cluster.size(); + }, + 30_000 + )); + } + + private Node addNodeToCluster(TestInfo testInfo, Function<ReplicaRequest, ReplicationGroupId> requestConverter) { + Node node = newNode(testInfo, new NetworkAddress("localhost", BASE_PORT + cluster.size()), nodeFinder); + + node.setRequestConverter(requestConverter); + + cluster.add(node); + + node.start(); + + node.waitWatches(); + + assertThat(node.cmgManager.onJoinReady(), willCompleteSuccessfully()); + + return node; + } + + private Node newNode(TestInfo testInfo, NetworkAddress address, NodeFinder nodeFinder) { + return new Node( + testInfo, + address, + nodeFinder, + workDir, + placementDriver, + systemConfiguration, + raftConfiguration, + nodeAttributesConfiguration, + storageConfiguration, + metaStorageConfiguration, + replicationConfiguration, + txConfiguration, + scheduledExecutorService, + null + ); + } + + private int createZone(String zoneName, int partitions, int replicas) { + Node node = cluster.get(0); + + createZoneWithStorageProfile( + node.catalogManager, + zoneName, + partitions, + replicas, + DEFAULT_STORAGE_PROFILE + ); + + return getZoneId(node.catalogManager, zoneName, node.hybridClock.nowLong()); + } + + private int createTable(String zoneName, String tableName) { + Node node = cluster.get(0); + + TableTestUtils.createTable( + node.catalogManager, + DEFAULT_SCHEMA_NAME, + zoneName, + tableName, + List.of( + ColumnParams.builder().name("key").type(INT32).build(), + ColumnParams.builder().name("val").type(INT32).nullable(true).build() + ), + List.of("key") + ); + + return getTableId(node.catalogManager, tableName, node.hybridClock.nowLong()); + } + + /** + * Tests that inserted data is replicated to all replica nodes. + */ + @Test + void testReplicationOnAllNodes(TestInfo testInfo) throws Exception { + startCluster(testInfo, 3); + + // Create a zone with a single partition on every node. + int zoneId = createZone(TEST_ZONE_NAME, 1, cluster.size()); + + int tableId1 = createTable(TEST_ZONE_NAME, TEST_TABLE_NAME1); + int tableId2 = createTable(TEST_ZONE_NAME, TEST_TABLE_NAME2); + + var zonePartitionId = new ZonePartitionId(zoneId, 0); + + setupTableIdToZoneIdConverter(zonePartitionId, new TablePartitionId(tableId1, 0), new TablePartitionId(tableId2, 0)); + + Node node = cluster.get(0); + + setPrimaryReplica(node, zonePartitionId); + + KeyValueView<Integer, Integer> kvView1 = node.tableManager.table(TEST_TABLE_NAME1).keyValueView(Integer.class, Integer.class); + KeyValueView<Integer, Integer> kvView2 = node.tableManager.table(TEST_TABLE_NAME2).keyValueView(Integer.class, Integer.class); + + // Test single insert. + kvView1.put(null, 42, 69); Review Comment: Please use runInTransaction instead. Full transactions aren't supported yet, and the way you've mocked PrimaryReplicaChangeCommand along with preudo working single puts brings more mess. It'll be a definition of done for PrimaryReplicaChangeCommand for single puts to work properly. ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/PartitionListener.java: ########## @@ -124,7 +124,7 @@ public class PartitionListener implements RaftGroupListener { private final UUID localNodeId; - private Set<String> currentGroupTopology; + private volatile Set<String> currentGroupTopology; Review Comment: Why? Is it possible to touch it without raft synchronisation? ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/replicator/PartitionReplicaListener.java: ########## @@ -1191,7 +1192,8 @@ private CompletableFuture<List<BinaryRow>> processReadOnlyMultiEntryAction( private CompletableFuture<Void> processReplicaSafeTimeSyncRequest(Boolean isPrimary) { requireNonNull(isPrimary); - if (!isPrimary) { + // Disable safe-time sync if the Colocation feature is enabled, safe-time is managed on a different level there. + if (!isPrimary || PartitionReplicaLifecycleManager.ENABLED) { Review Comment: I'd expect `PartitionReplicaLifecycleManager.ENABLED` to be removed after adding ReplicaSafeTimeSyncRequest support to the zone level, right? If true, please add https://issues.apache.org/jira/browse/IGNITE-22620? -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
