vldpyatkov commented on code in PR #1726:
URL: https://github.com/apache/ignite-3/pull/1726#discussion_r1130736891


##########
modules/placement-driver/src/main/java/org/apache/ignite/internal/placementdriver/TopologyTracker.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.placementdriver;
+
+import java.util.concurrent.atomic.AtomicReference;
+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.util.CollectionUtils;
+import org.apache.ignite.network.ClusterNode;
+
+/**
+ * The class tracks a logical topology.
+ */
+public class TopologyTracker {
+    /** Ignite logger. */
+    private static final IgniteLogger LOG = 
Loggers.forClass(TopologyTracker.class);
+
+    /** Topology service. */
+    private final LogicalTopologyService topologyService;
+
+    /** Logical topology snapshot. */
+    private AtomicReference<LogicalTopologySnapshot> topologySnapRef;
+
+    /** Listener to track topology updates. */
+    private final TopologyListener topologyListener;
+
+    /**
+     * The constructor.
+     *
+     * @param topologyService Topology service.
+     */
+    public TopologyTracker(LogicalTopologyService topologyService) {
+        this.topologyService = topologyService;
+
+        this.topologySnapRef = new AtomicReference<>();
+        this.topologyListener = new TopologyListener();
+    }
+
+    /**
+     * Initializes topology on start.
+     */
+    public void startTrack() {
+        topologyService.addEventListener(topologyListener);
+
+        topologyService.logicalTopologyOnLeader().thenAccept(topologySnap -> {
+            LogicalTopologySnapshot logicalTopologySnap0;
+
+            do {
+                logicalTopologySnap0 = topologySnapRef.get();
+
+                if (logicalTopologySnap0 != null && 
logicalTopologySnap0.version() >= topologySnap.version()) {
+                    break;
+                }
+            } while (!topologySnapRef.compareAndSet(logicalTopologySnap0, 
topologySnap));
+
+            LOG.info("Logical topology initialized for placement driver 
[topologySnap={}]", topologySnap);
+        });
+    }
+
+    /**
+     * Stops the tracker.
+     */
+    public void stopTrack() {
+        topologyService.removeEventListener(topologyListener);
+    }
+
+    /**
+     * Gets a node by consistent id from the logical topology.
+     *
+     * @param consistentId Node consistent id.
+     * @return Cluster node or {@code null} if topology has no a node with the 
consistent id.
+     */
+    public ClusterNode localTopologyNodeByConsistentId(String consistentId) {

Review Comment:
   I simplify the name to `nodeByConsistentId`, because the class already has a 
reference to topology in its title.



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