sanpwc commented on code in PR #1542: URL: https://github.com/apache/ignite-3/pull/1542#discussion_r1091697689
########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/MetaStorageRaftGroupEventsListener.java: ########## @@ -0,0 +1,247 @@ +/* + * 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.metastorage.impl; + +import static java.util.concurrent.CompletableFuture.allOf; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; + +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; +import java.util.stream.Stream; +import org.apache.ignite.internal.cluster.management.topology.LogicalTopology; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyEventListener; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologySnapshot; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.raft.Peer; +import org.apache.ignite.internal.raft.RaftGroupEventsListener; +import org.apache.ignite.internal.raft.service.RaftGroupService; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.TopologyEventHandler; +import org.apache.ignite.network.TopologyService; + +/** + * Raft Group Events listener that registers Logical Topology listeners for removing learners that have left the topology. + */ +public class MetaStorageRaftGroupEventsListener implements RaftGroupEventsListener { + private static final IgniteLogger LOG = Loggers.forClass(MetaStorageManagerImpl.class); + + private final IgniteSpinBusyLock busyLock; + + private final TopologyService topologyService; + + private final LogicalTopology logicalTopology; + + private final CompletableFuture<MetaStorageServiceImpl> metaStorageSvcFut; + + /** + * Future required to enable serialized processing of topology events. + * + * <p>While Logical Topology events are linearized, we also concurrently react to Physical Topology changes and/or update Raft + * configuration after a new leader has been elected. + * + * <p>Multi-threaded access is guarded by {@code serializationFutureMux}. + */ + private CompletableFuture<Void> serializationFuture = null; + + private final Object serializationFutureMux = new Object(); + + /** + * The most recent version of the Logical Topology that this listener has observed. + */ + private volatile long lastProcessedLogicalTopologyVersion = -1; + + MetaStorageRaftGroupEventsListener( + IgniteSpinBusyLock busyLock, + ClusterService clusterService, + LogicalTopology logicalTopology, + CompletableFuture<MetaStorageServiceImpl> metaStorageSvcFut + ) { + this.busyLock = busyLock; + this.topologyService = clusterService.topologyService(); + this.logicalTopology = logicalTopology; + this.metaStorageSvcFut = metaStorageSvcFut; + } + + @Override + public void onLeaderElected(long term) { + registerTopologyEventListeners(); + + // Update learner configuration in case we missed some topology updates. + executeIfLeader(service -> removeLearners(service.raftGroupService(), logicalTopology.getLogicalTopology())); + } + + private void registerTopologyEventListeners() { + // Try to remove a Meta Storage Learner if a node leaves the Physical Topology. This handles the case when a node joins, but leaves + // before sending the Join Ready request (i.e. before it gets added to the Logical Topology). + topologyService.addEventHandler(new TopologyEventHandler() { + @Override + public void onDisappeared(ClusterNode member) { + executeIfLeader(service -> removeLearners(service.raftGroupService(), logicalTopology.getLogicalTopology())); Review Comment: I mean following: 0. There's node A up and running that is also CMG and MS Leader, meaning that both CMG and MS are build on top of single node A. 1. Node B tries to join the cluster. At some point it sends `service.addLearners(List.of(localPeer))` message to the A. 2. addLearner(B) processing hangs a bit. 3. Node A sees onDisappeared(B) and tries to remove B from learners, despite the fact that there's no B in the learners set. 4. addLearner(B) wakes up and successfully adds B as a Learner. Thus we have a reordering between adding and removing Learner B. So, basically, my question is about possibility of such reordering. -- 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]
