minal-kyada commented on code in PR #4630:
URL: https://github.com/apache/cassandra/pull/4630#discussion_r2897648505


##########
src/java/org/apache/cassandra/db/SystemPeersValidator.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.db;
+
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.cql3.UntypedResultSet;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.db.virtual.PeersTable;
+import org.apache.cassandra.locator.InetAddressAndPort;
+import org.apache.cassandra.schema.SchemaConstants;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.tcm.membership.Location;
+import org.apache.cassandra.tcm.membership.NodeAddresses;
+import org.apache.cassandra.tcm.membership.NodeId;
+import org.apache.cassandra.utils.FBUtilities;
+
+import static org.apache.cassandra.cql3.QueryProcessor.executeInternal;
+import static org.apache.cassandra.db.SystemKeyspace.LEGACY_PEERS;
+import static org.apache.cassandra.db.SystemKeyspace.PEERS_V2;
+
+/**
+ * Validator to ensure system.peers and system.peers_v2 tables match 
ClusterMetadata on startup.
+ * This is critical for backward compatibility as older clients and tools read 
from these
+ * legacy tables while TCM uses ClusterMetadata as the source of truth.
+ *
+ * The validator detects inconsistencies and automatically repairs them by 
synchronizing
+ * the peers tables with the current ClusterMetadata.
+ */
+public class SystemPeersValidator
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(SystemPeersValidator.class);
+
+    public static void validateAndRepair(ClusterMetadata metadata)
+    {
+        Map<InetAddressAndPort, UntypedResultSet.Row> peersV2Rows = 
getPeersV2Rows();
+        Map<InetAddress, UntypedResultSet.Row> legacyPeersRows = 
getLegacyPeersRows();
+
+        Map<InetAddressAndPort, NodeId> expectedEndpoints = new HashMap<>();
+        Map<InetAddress, NodeId> expectedAddresses = new HashMap<>();
+        for (NodeId nodeId : getExpectedPeerNodes(metadata))

Review Comment:
   I had this because I wanted to eliminate localEndpoints, but there's a 
better way to do it by just having allJoinedEndpoints and skipping the local 
endpoint. Will change it! Thanks Sam!



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to