You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Shaheen Afroz <sh...@gmail.com> on 2014/06/19 05:22:17 UTC

Re: Exception with java driver

+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();
}
}

Re: Exception with java driver

Posted by Sylvain Lebresne <sy...@datastax.com>.
Please don't post on two mailing lists at once, it makes it impossible for
people that are not subscribed to the 2 mailing list to follow the thread
(and is bad form in general). If unsure which one is the most appropriate,
it's fine, pick your best guest (in this case it's clearly a java driver
question).

--
Sylvain


On Thu, Jun 19, 2014 at 5:22 AM, Shaheen Afroz <sh...@gmail.com>
wrote:

> +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();
> }
>  }
>
> To unsubscribe from this group and stop receiving emails from it, send an
> email to java-driver-user+unsubscribe@lists.datastax.com.
>