+Cassandra DL

We have Cassandra nodes in three datacenters - dc1, dc2 and dc3 and the
cluster name is DataCluster. In the same way, our application code is also
in same three datacenters. Our application code is accessing cassandra.

Now I want to make sure if application call is coming from `dc1` then it
should go to cassandra `dc1` always. Same with `dc2` and `dc3`.

So I decided to use DCAwareRoundRobinPolicy of datastax java driver.
Cassandra version we have is DSE 4.0 and datastax java driver version we
are using is 2.0.2.

But somehow with the below code it always gives me an excpetion as
NoHostAvailableException -

com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s)
tried for query failed (no host was tried)
But in the same code, if I comment out below line and run it again. It
works fine without any problem. That is pretty strange. What could be wrong
with DCAwareRoundRobinPolicy or Cassandra setup?

.withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc1"))
Below is my code -

public static Cluster cluster;
public static Session session;
public static Builder builder;

public static void main(String[] args) {

try {
builder = Cluster.builder();
 builder.addContactPoint("some1_dc1_machine");
builder.addContactPoint("some2_dc1_machine");
builder.addContactPoint("some1_dc2_machine");
builder.addContactPoint("some2_dc2_machine");
builder.addContactPoint("some1_dc3_machine");
builder.addContactPoint("some2_dc3_machine");
 PoolingOptions opts = new PoolingOptions();
opts.setCoreConnectionsPerHost(HostDistance.LOCAL,
opts.getCoreConnectionsPerHost(HostDistance.LOCAL));

SocketOptions socketOpts = new SocketOptions();
socketOpts.setReceiveBufferSize(1048576);
socketOpts.setSendBufferSize(1048576);
socketOpts.setTcpNoDelay(false);

cluster = builder
.withSocketOptions(socketOpts)
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.withPoolingOptions(opts)
.withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
.withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc1"))
.withCredentials("username", "password")
.build();


session = cluster.connect("testingkeyspace");
 Metadata metadata = cluster.getMetadata();
            System.out.println(String.format("Connected to cluster '%s' on
%s.", metadata.getClusterName(), metadata.getAllHosts()));

} catch (NoHostAvailableException e) {
System.out.println("NoHostAvailableException");
e.printStackTrace();
System.out.println(e.getErrors());
} catch (Exception e) {
System.out.println("Exception");
e.printStackTrace();
}
}

Reply via email to