Skip to content

Commit

Permalink
move SeedProvider and AbstractNetworkTopologySnitch to InetAddressAnd…
Browse files Browse the repository at this point in the history
…Port for forward compatibility
  • Loading branch information
rhuffy committed Aug 7, 2024
1 parent 69e57b6 commit 0dffd23
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 33 deletions.
12 changes: 10 additions & 2 deletions src/java/org/apache/cassandra/config/DatabaseDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<InetAddressAndPort> 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) {
Expand Down Expand Up @@ -1355,7 +1358,12 @@ public static String getPersistentSettingsLocation() {

public static Set<InetAddress> getSeeds()
{
return ImmutableSet.<InetAddress>builder().addAll(seedProvider.getSeeds()).build();
ImmutableSet.Builder<InetAddress> builder = ImmutableSet.builder();
for (InetAddressAndPort seed : seedProvider.getSeeds())
{
builder.add(seed.address);
}
return builder.build();
}

public static Set<InetAddress> getAllHosts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
6 changes: 4 additions & 2 deletions src/java/org/apache/cassandra/locator/CloudstackSnitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions src/java/org/apache/cassandra/locator/Ec2Snitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions src/java/org/apache/cassandra/locator/InetAddressAndPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -191,4 +198,9 @@ public static void initializeDefaultPort(int port)
{
defaultPort = port;
}

static InetAddressAndPort wrap(InetAddress address)
{
return new InetAddressAndPort(address);
}
}
10 changes: 6 additions & 4 deletions src/java/org/apache/cassandra/locator/PropertyFileSnitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/java/org/apache/cassandra/locator/SeedProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

public interface SeedProvider
{
List<InetAddress> getSeeds();
List<InetAddressAndPort> getSeeds();
}
8 changes: 4 additions & 4 deletions src/java/org/apache/cassandra/locator/SimpleSeedProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SimpleSeedProvider implements SeedProvider

public SimpleSeedProvider(Map<String, String> args) {}

public List<InetAddress> getSeeds()
public List<InetAddressAndPort> getSeeds()
{
Config conf;
try
Expand All @@ -47,19 +47,19 @@ public List<InetAddress> getSeeds()
throw new AssertionError(e);
}
String[] hosts = conf.seed_provider.parameters.get("seeds").split(",", -1);
List<InetAddress> seeds = new ArrayList<InetAddress>(hosts.length);
List<InetAddressAndPort> 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)
{
// not fatal... DD will bark if there end up being zero seeds.
logger.warn("Seed provider couldn't lookup host {}", host);
}
}
return Collections.unmodifiableList(seeds);
return seeds;
}
}
9 changes: 5 additions & 4 deletions test/unit/org/apache/cassandra/service/MoveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,19 +107,19 @@ 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
return "RAC" + (ipLastPart % 3);
}

@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";
Expand Down

0 comments on commit 0dffd23

Please sign in to comment.