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


##########
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
+            // learners, this call allows us to handle the case when a network 
partition occurs and a node gets kicked out of the Logical
+            // Topology. Since the remote node simply gets disconnected and 
not restarted, it should be able to re-join the cluster even
+            // though it will skip the step when it adds itself as a Learner.
+            @Override
+            public void onAppeared(ClusterNode appearedNode, 
LogicalTopologySnapshot newTopology) {
+                executeIfLeader(service -> 
addLearner(service.raftGroupService(), appearedNode));
+            }
+
+            // Try to remove a Meta Storage Learner when a node leaves the 
Logical Topology. This approach protects from network glitches
+            // when a node leaves and re-joins the Physical Topology.
+            @Override
+            public void onDisappeared(ClusterNode disappearedNode, 
LogicalTopologySnapshot newTopology) {

Review Comment:
   Again, can you please clarify the situation a little bit?



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