sanpwc commented on code in PR #5134: URL: https://github.com/apache/ignite-3/pull/5134#discussion_r1939006743
########## 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: In that case, please remove it for now. It'll be covered with another ticket. -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org