sashapolo commented on code in PR #1542:
URL: https://github.com/apache/ignite-3/pull/1542#discussion_r1091571518


##########
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()));
+            }
+        });
+
+        logicalTopology.addEventListener(new LogicalTopologyEventListener() {
+            // Try to add a node as a Learner when it appears in the Logical 
Topology. Even though usually remote nodes add themselves as

Review Comment:
   Normal flow:
   1. A node gets discovered by the CMG leader using the Physical Topology.
   2. The node gets validated on the CMG leader.
   3. The CMG leader sends MS node IDs to the joining node.
   4. The joining node starts a local Raft server and executes `addLearners` to 
add itself as a learner.
   5. The joining node executes `onJoinReady` and gets added to the Logical 
Topology.
   
   Now imagine that after that a network partition happens for a long enough 
time for the node to get kicked out of the Logical Topology. As soon as that 
happens, it gets removed from the MS learners as well. When the network 
partition is fixed, the node will be added back to the Logical Topology, but it 
will not be added as MS learner (because the node was not restarted and the 
code that starts the Raft server will not be executed).



-- 
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]

Reply via email to