sashapolo commented on code in PR #1542: URL: https://github.com/apache/ignite-3/pull/1542#discussion_r1091588972
########## 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: > Are there any order guarantees between messaging and network topology events? I don't think so, at least there's no explicit guarantee. Technically these can be separate subsystems. > In other words is it possible that we will have a race between processing nodeJoinRequest and handling onDissapeared, meaning the race between adding and removing the learner in this particular case? Can you please clarify the situation? -- 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]
