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


##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/MetaStorageRaftGroupEventsListener.java:
##########
@@ -0,0 +1,230 @@
+/*
+ * 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.toSet;
+
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import 
org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyEventListener;
+import 
org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologyService;
+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.PeersAndLearners;
+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;
+
+/**
+ * Raft Group Events listener that registers Logical Topology listener for 
updating the list of Meta Storage Raft group listeners.
+ */
+public class MetaStorageRaftGroupEventsListener implements 
RaftGroupEventsListener {
+    private static final IgniteLogger LOG = 
Loggers.forClass(MetaStorageManagerImpl.class);
+
+    private final IgniteSpinBusyLock busyLock;
+
+    private final String nodeName;
+
+    private final LogicalTopologyService logicalTopologyService;
+
+    private final CompletableFuture<MetaStorageServiceImpl> metaStorageSvcFut;
+
+    /**
+     * Future required to enable serialized processing of topology events.
+     *
+     * <p>While Logical Topology events are linearized, we usually start a 
bunch of async operations inside the event listener and need
+     * them to finish in the same order.
+     *
+     * <p>Multi-threaded access is guarded by {@code serializationFutureMux}.
+     */
+    private CompletableFuture<Void> serializationFuture = null;
+
+    private final Object serializationFutureMux = new Object();
+
+    MetaStorageRaftGroupEventsListener(
+            IgniteSpinBusyLock busyLock,
+            ClusterService clusterService,
+            LogicalTopologyService logicalTopologyService,
+            CompletableFuture<MetaStorageServiceImpl> metaStorageSvcFut
+    ) {
+        this.busyLock = busyLock;
+        this.nodeName = clusterService.localConfiguration().getName();
+        this.logicalTopologyService = logicalTopologyService;
+        this.metaStorageSvcFut = metaStorageSvcFut;
+    }
+
+    @Override
+    public void onLeaderElected(long term) {
+        synchronized (serializationFutureMux) {
+            registerTopologyEventListeners();
+
+            // Update learner configuration (in case we missed some topology 
updates) and initialize the serialization future.
+            serializationFuture = executeIfLeaderImpl(this::resetLearners);
+        }
+    }
+
+    private void registerTopologyEventListeners() {
+        logicalTopologyService.addEventListener(new 
LogicalTopologyEventListener() {
+            @Override
+            public void onNodeValidated(ClusterNode validatedNode) {
+                executeIfLeader((service, term) -> 
addLearner(service.raftGroupService(), validatedNode));

Review Comment:
   1. We do call onNodeValidated, ..., onNodeLeft are called within raft 
thread, is that correct?
   2. If true
       2.a You didn't introduce any synchronous long running logic, e.g. 
network communication as a reaction to such events, did you? 
serializationFuture?
       2.b You didn't introduce any logic that may prevent CMG state-machine 
idempotentancy, did you? Meaning, that if we'll have an exception within onSmt 
on Node A and won't have such an exception on Node B, CMG listener won't be 
effected, is that true?
   3. Who is responsible for exception handling for serializationFuture? E.g. 
logging.
   



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