From 0dffd2366b78e0ded36bed936064702450e1f9b2 Mon Sep 17 00:00:00 2001 From: Raymond Huffman Date: Wed, 7 Aug 2024 19:10:50 -0400 Subject: [PATCH] move SeedProvider and AbstractNetworkTopologySnitch to InetAddressAndPort for forward compatibility --- .../cassandra/config/DatabaseDescriptor.java | 12 +++++++++-- .../AbstractNetworkTopologySnitch.java | 20 +++++++++++++++++-- .../cassandra/locator/CloudstackSnitch.java | 6 ++++-- .../locator/DynamicEndpointSnitch.java | 4 ++-- .../apache/cassandra/locator/Ec2Snitch.java | 6 ++++-- .../cassandra/locator/GoogleCloudSnitch.java | 6 ++++-- .../locator/GossipingPropertyFileSnitch.java | 10 ++++++---- .../cassandra/locator/InetAddressAndPort.java | 12 +++++++++++ .../cassandra/locator/PropertyFileSnitch.java | 10 ++++++---- .../locator/RackInferringSnitch.java | 8 ++++---- .../cassandra/locator/SeedProvider.java | 2 +- .../cassandra/locator/SimpleSeedProvider.java | 8 ++++---- .../apache/cassandra/service/MoveTest.java | 9 +++++---- 13 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/java/org/apache/cassandra/config/DatabaseDescriptor.java b/src/java/org/apache/cassandra/config/DatabaseDescriptor.java index 4ee23fe03c..1d4a6563d3 100644 --- a/src/java/org/apache/cassandra/config/DatabaseDescriptor.java +++ b/src/java/org/apache/cassandra/config/DatabaseDescriptor.java @@ -28,10 +28,12 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.*; +import java.util.stream.Collectors; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; @@ -731,7 +733,8 @@ else if (conf.num_tokens > MAX_NUM_TOKENS) if (Boolean.getBoolean("palantir_cassandra.single_node_cluster")) { try { - if (!seedProvider.getSeeds().equals(ImmutableList.of(InetAddress.getLocalHost()))) + List seeds = seedProvider.getSeeds(); + if (seeds.size() != 1 || !Iterables.getOnlyElement(seeds).address.equals(InetAddress.getLocalHost())) throw new ConfigurationException("Unexpected seed list when single_node_cluster flag is set to true." + " For a single node Cassandra cluster, the only seed should be localhost", false); } catch (UnknownHostException e) { @@ -1355,7 +1358,12 @@ public static String getPersistentSettingsLocation() { public static Set getSeeds() { - return ImmutableSet.builder().addAll(seedProvider.getSeeds()).build(); + ImmutableSet.Builder builder = ImmutableSet.builder(); + for (InetAddressAndPort seed : seedProvider.getSeeds()) + { + builder.add(seed.address); + } + return builder.build(); } public static Set getAllHosts() diff --git a/src/java/org/apache/cassandra/locator/AbstractNetworkTopologySnitch.java b/src/java/org/apache/cassandra/locator/AbstractNetworkTopologySnitch.java index b5606d6dc4..38686ad547 100644 --- a/src/java/org/apache/cassandra/locator/AbstractNetworkTopologySnitch.java +++ b/src/java/org/apache/cassandra/locator/AbstractNetworkTopologySnitch.java @@ -30,14 +30,30 @@ public abstract class AbstractNetworkTopologySnitch extends AbstractEndpointSnit * @param endpoint a specified endpoint * @return string of rack */ - abstract public String getRack(InetAddress endpoint); + abstract public String getRack(InetAddressAndPort endpoint); + + /** + * @apiNote Only override in tests! + */ + public String getRack(InetAddress endpoint) + { + return getRack(InetAddressAndPort.wrap(endpoint)); + } /** * Return the data center for which an endpoint resides in * @param endpoint a specified endpoint * @return string of data center */ - abstract public String getDatacenter(InetAddress endpoint); + abstract public String getDatacenter(InetAddressAndPort endpoint); + + /** + * @apiNote Only override in tests! + */ + public String getDatacenter(InetAddress endpoint) + { + return getDatacenter(InetAddressAndPort.wrap(endpoint)); + } public int compareEndpoints(InetAddress address, InetAddress a1, InetAddress a2) { diff --git a/src/java/org/apache/cassandra/locator/CloudstackSnitch.java b/src/java/org/apache/cassandra/locator/CloudstackSnitch.java index 88c62e9710..7ece625b40 100644 --- a/src/java/org/apache/cassandra/locator/CloudstackSnitch.java +++ b/src/java/org/apache/cassandra/locator/CloudstackSnitch.java @@ -83,8 +83,9 @@ public CloudstackSnitch() throws IOException, ConfigurationException csZoneRack = zone_parts[2]; } - public String getRack(InetAddress endpoint) + public String getRack(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; if (endpoint.equals(FBUtilities.getBroadcastAddress())) return csZoneRack; EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint); @@ -99,8 +100,9 @@ public String getRack(InetAddress endpoint) return state.getApplicationState(ApplicationState.RACK).value; } - public String getDatacenter(InetAddress endpoint) + public String getDatacenter(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; if (endpoint.equals(FBUtilities.getBroadcastAddress())) return csZoneDc; EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint); diff --git a/src/java/org/apache/cassandra/locator/DynamicEndpointSnitch.java b/src/java/org/apache/cassandra/locator/DynamicEndpointSnitch.java index 9ca9c465aa..25caa365f1 100644 --- a/src/java/org/apache/cassandra/locator/DynamicEndpointSnitch.java +++ b/src/java/org/apache/cassandra/locator/DynamicEndpointSnitch.java @@ -125,12 +125,12 @@ public void gossiperStarting() subsnitch.gossiperStarting(); } - public String getRack(InetAddress endpoint) + public String getRack(InetAddressAndPort endpoint) { return subsnitch.getRack(endpoint); } - public String getDatacenter(InetAddress endpoint) + public String getDatacenter(InetAddressAndPort endpoint) { return subsnitch.getDatacenter(endpoint); } diff --git a/src/java/org/apache/cassandra/locator/Ec2Snitch.java b/src/java/org/apache/cassandra/locator/Ec2Snitch.java index 59eb27bb2b..512664498e 100644 --- a/src/java/org/apache/cassandra/locator/Ec2Snitch.java +++ b/src/java/org/apache/cassandra/locator/Ec2Snitch.java @@ -92,8 +92,9 @@ String awsApiCall(String url) throws IOException, ConfigurationException } } - public String getRack(InetAddress endpoint) + public String getRack(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; if (endpoint.equals(FBUtilities.getBroadcastAddress())) return ec2zone; EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint); @@ -108,8 +109,9 @@ public String getRack(InetAddress endpoint) return state.getApplicationState(ApplicationState.RACK).value; } - public String getDatacenter(InetAddress endpoint) + public String getDatacenter(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; if (endpoint.equals(FBUtilities.getBroadcastAddress())) return ec2region; EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint); diff --git a/src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java b/src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java index b4d3b19931..ab04948eaa 100644 --- a/src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java +++ b/src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java @@ -94,8 +94,9 @@ String gceApiCall(String url) throws IOException, ConfigurationException } } - public String getRack(InetAddress endpoint) + public String getRack(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; if (endpoint.equals(FBUtilities.getBroadcastAddress())) return gceZone; EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint); @@ -110,8 +111,9 @@ public String getRack(InetAddress endpoint) return state.getApplicationState(ApplicationState.RACK).value; } - public String getDatacenter(InetAddress endpoint) + public String getDatacenter(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; if (endpoint.equals(FBUtilities.getBroadcastAddress())) return gceRegion; EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint); diff --git a/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java b/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java index e2449ae346..35a19c3697 100644 --- a/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java +++ b/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java @@ -81,11 +81,12 @@ private static SnitchProperties loadConfiguration() throws ConfigurationExceptio /** * Return the data center for which an endpoint resides in * - * @param endpoint the endpoint to process + * @param endpointAndPort the endpoint to process * @return string of data center */ - public String getDatacenter(InetAddress endpoint) + public String getDatacenter(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; if (endpoint.equals(FBUtilities.getBroadcastAddress())) return myDC; @@ -109,11 +110,12 @@ public String getDatacenter(InetAddress endpoint) /** * Return the rack for which an endpoint resides in * - * @param endpoint the endpoint to process + * @param endpointAndPort the endpoint to process * @return string of rack */ - public String getRack(InetAddress endpoint) + public String getRack(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; if (endpoint.equals(FBUtilities.getBroadcastAddress())) return myRack; diff --git a/src/java/org/apache/cassandra/locator/InetAddressAndPort.java b/src/java/org/apache/cassandra/locator/InetAddressAndPort.java index 584e13b424..6d7143b575 100644 --- a/src/java/org/apache/cassandra/locator/InetAddressAndPort.java +++ b/src/java/org/apache/cassandra/locator/InetAddressAndPort.java @@ -65,6 +65,13 @@ private InetAddressAndPort(InetAddress address, byte[] addressBytes, int port) this.addressBytes = addressBytes; } + private InetAddressAndPort(InetAddress address) + { + this.address = address; + this.port = -1; + this.addressBytes = null; + } + private static void validatePortRange(int port) { if (port < 0 | port > 65535) @@ -191,4 +198,9 @@ public static void initializeDefaultPort(int port) { defaultPort = port; } + + static InetAddressAndPort wrap(InetAddress address) + { + return new InetAddressAndPort(address); + } } diff --git a/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java b/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java index 8cc6549dbf..86dfe571f1 100644 --- a/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java +++ b/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java @@ -115,11 +115,12 @@ private static String[] getRawEndpointInfo(InetAddress endpoint) /** * Return the data center for which an endpoint resides in * - * @param endpoint the endpoint to process + * @param endpointAndPort the endpoint to process * @return string of data center */ - public String getDatacenter(InetAddress endpoint) + public String getDatacenter(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; String[] info = getEndpointInfo(endpoint); assert info != null : "No location defined for endpoint " + endpoint; return info[0]; @@ -128,11 +129,12 @@ public String getDatacenter(InetAddress endpoint) /** * Return the rack for which an endpoint resides in * - * @param endpoint the endpoint to process + * @param endpointAndPort the endpoint to process * @return string of rack */ - public String getRack(InetAddress endpoint) + public String getRack(InetAddressAndPort endpointAndPort) { + InetAddress endpoint = endpointAndPort.address; String[] info = getEndpointInfo(endpoint); assert info != null : "No location defined for endpoint " + endpoint; return info[1]; diff --git a/src/java/org/apache/cassandra/locator/RackInferringSnitch.java b/src/java/org/apache/cassandra/locator/RackInferringSnitch.java index a6ea1abd53..2172008ffd 100644 --- a/src/java/org/apache/cassandra/locator/RackInferringSnitch.java +++ b/src/java/org/apache/cassandra/locator/RackInferringSnitch.java @@ -25,13 +25,13 @@ */ public class RackInferringSnitch extends AbstractNetworkTopologySnitch { - public String getRack(InetAddress endpoint) + public String getRack(InetAddressAndPort endpoint) { - return Integer.toString(endpoint.getAddress()[2] & 0xFF, 10); + return Integer.toString(endpoint.address.getAddress()[2] & 0xFF, 10); } - public String getDatacenter(InetAddress endpoint) + public String getDatacenter(InetAddressAndPort endpoint) { - return Integer.toString(endpoint.getAddress()[1] & 0xFF, 10); + return Integer.toString(endpoint.address.getAddress()[1] & 0xFF, 10); } } diff --git a/src/java/org/apache/cassandra/locator/SeedProvider.java b/src/java/org/apache/cassandra/locator/SeedProvider.java index a013fbbb31..029b2ea84c 100644 --- a/src/java/org/apache/cassandra/locator/SeedProvider.java +++ b/src/java/org/apache/cassandra/locator/SeedProvider.java @@ -22,5 +22,5 @@ public interface SeedProvider { - List getSeeds(); + List getSeeds(); } diff --git a/src/java/org/apache/cassandra/locator/SimpleSeedProvider.java b/src/java/org/apache/cassandra/locator/SimpleSeedProvider.java index 665261d47a..782cf28ede 100644 --- a/src/java/org/apache/cassandra/locator/SimpleSeedProvider.java +++ b/src/java/org/apache/cassandra/locator/SimpleSeedProvider.java @@ -35,7 +35,7 @@ public class SimpleSeedProvider implements SeedProvider public SimpleSeedProvider(Map args) {} - public List getSeeds() + public List getSeeds() { Config conf; try @@ -47,12 +47,12 @@ public List getSeeds() throw new AssertionError(e); } String[] hosts = conf.seed_provider.parameters.get("seeds").split(",", -1); - List seeds = new ArrayList(hosts.length); + List seeds = new ArrayList<>(hosts.length); for (String host : hosts) { try { - seeds.add(InetAddress.getByName(host.trim())); + seeds.add(InetAddressAndPort.wrap(InetAddress.getByName(host.trim()))); } catch (UnknownHostException ex) { @@ -60,6 +60,6 @@ public List getSeeds() logger.warn("Seed provider couldn't lookup host {}", host); } } - return Collections.unmodifiableList(seeds); + return seeds; } } diff --git a/test/unit/org/apache/cassandra/service/MoveTest.java b/test/unit/org/apache/cassandra/service/MoveTest.java index bd4317db55..d3cc610796 100644 --- a/test/unit/org/apache/cassandra/service/MoveTest.java +++ b/test/unit/org/apache/cassandra/service/MoveTest.java @@ -32,6 +32,7 @@ import org.apache.cassandra.db.marshal.BytesType; import org.apache.cassandra.gms.Gossiper; import org.apache.cassandra.locator.AbstractNetworkTopologySnitch; +import org.apache.cassandra.locator.InetAddressAndPort; import org.apache.cassandra.locator.NetworkTopologyStrategy; import org.apache.cassandra.locator.PendingRangeMaps; import org.junit.AfterClass; @@ -106,9 +107,9 @@ private static void addNetworkTopologyKeyspace(String keyspaceName, Integer... r //Odd IPs are in DC1 and Even are in DC2. Endpoints upto .14 will have unique racks and // then will be same for a set of three. @Override - public String getRack(InetAddress endpoint) + public String getRack(InetAddressAndPort endpoint) { - int ipLastPart = getIPLastPart(endpoint); + int ipLastPart = getIPLastPart(endpoint.address); if (ipLastPart <= 14) return UUID.randomUUID().toString(); else @@ -116,9 +117,9 @@ public String getRack(InetAddress endpoint) } @Override - public String getDatacenter(InetAddress endpoint) + public String getDatacenter(InetAddressAndPort endpoint) { - if (getIPLastPart(endpoint) % 2 == 0) + if (getIPLastPart(endpoint.address) % 2 == 0) return "DC2"; else return "DC1";