Skip to content

Instantly share code, notes, and snippets.

@olim7t
Created January 7, 2016 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olim7t/471d3eb7b95f1a0c0ae0 to your computer and use it in GitHub Desktop.
Save olim7t/471d3eb7b95f1a0c0ae0 to your computer and use it in GitHub Desktop.
Use custom LBP to query system.local on each host
import com.datastax.driver.core.*;
import com.datastax.driver.core.policies.Policies;
import java.net.InetAddress;
public class GatherAllListenAddresses {
public static void main(String[] args) {
Cluster cluster = null;
try {
cluster = Cluster.builder()
.addContactPoint("127.0.0.1")
.withLoadBalancingPolicy(new HostTargetingPolicy(Policies.defaultLoadBalancingPolicy()))
.build();
Session session = cluster.connect();
for (Host host : cluster.getMetadata().getAllHosts()) {
Statement statement = new HostTargetingStatement(
new SimpleStatement("select listen_address from system.local"),
host);
try {
Row row = session.execute(statement).one();
InetAddress listenAddress = row.getInet(0);
System.out.printf("Host %s uses listen address %s%n", host, listenAddress);
} catch (Exception e) {
// continue to next host
}
}
} finally {
if (cluster != null) cluster.close();
}
}
}
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.HostDistance;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.policies.LoadBalancingPolicy;
import com.google.common.collect.Iterators;
import java.util.Collection;
import java.util.Iterator;
/**
* A load balancing policy that wraps another policy to allow sending some specially marked statements to a specific
* node in the cluster.
*/
public class HostTargetingPolicy implements LoadBalancingPolicy {
private final LoadBalancingPolicy childPolicy;
public HostTargetingPolicy(LoadBalancingPolicy childPolicy) {
this.childPolicy = childPolicy;
}
@Override
public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) {
if (statement instanceof HostTargetingStatement) {
return Iterators.singletonIterator(((HostTargetingStatement) statement).host);
} else {
return childPolicy.newQueryPlan(loggedKeyspace, statement);
}
}
@Override
public void init(Cluster cluster, Collection<Host> hosts) {
childPolicy.init(cluster, hosts);
}
@Override
public HostDistance distance(Host host) {
return childPolicy.distance(host);
}
@Override
public void onAdd(Host host) {
childPolicy.onAdd(host);
}
@Override
public void onUp(Host host) {
childPolicy.onUp(host);
}
@Override
public void onDown(Host host) {
childPolicy.onDown(host);
}
@Override
public void onRemove(Host host) {
childPolicy.onRemove(host);
}
@Override
public void close() {
childPolicy.close();
}
}
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.StatementWrapper;
/**
* Wraps a statement to target it to a specific node in the cluster.
*
* @see HostTargetingPolicy
*/
public class HostTargetingStatement extends StatementWrapper {
final Host host;
public HostTargetingStatement(Statement wrapped, Host host) {
super(wrapped);
// Note that we completely trust the caller to pass a Host that is part of the current cluster.
// You might want to be more defensive and perform additional checks, either here or in HostTargetingPolicy#newQueryPlan.
this.host = host;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment