15

I am not using Elasticssearch. I am trying to perform some database operations in cassandra using CQL. I am using threads. While running the code I am always getting the exception in thread after a while : com.datastax.oss.driver.api.core.NoNodeAvailableException: No node was available to execute the query.

I have tested with even one thread. The error is still there. Here is my code :

InetAddress addrOne = InetAddress.getByName("52.15.195.41");
InetSocketAddress addrSocOne = new InetSocketAddress(addrOne,9042);
CqlSession sessionOne = CqlSession.builder().addContactPoint(addrSocOne).withLocalDatacenter("us-east-2").withKeyspace("test").build();

while(counter <= 100)
{
    String query = "select max(id) FROM samplequeue";
    ResultSet rs = session.execute(query);
    for (Row row : rs) 
    {
        int exS = row.getInt("system.max(id)");
    }
    counter++;
    Thread.sleep(50);
}

This is a very simple, modified example just to demonstrate the problem. I am unable to resolve it. All the threads are exiting giving the same exception. I am running cassandra 3.11.4 on AWS. All my nodes are up and running and I can perform operations finely in the backend.

SDt
  • 432
  • 1
  • 4
  • 10

3 Answers3

17

Change .withLocalDatacenter("us-east-2") to .withLocalDatacenter("datacenter1") and retry.

JoSSte
  • 2,953
  • 6
  • 34
  • 54
William
  • 194
  • 1
  • 3
  • 7
    could you please explain why this works? is it some hard coded value? – Leonid Bor Nov 17 '19 at 16:15
  • I'm surprised that this worked, but it did, even though my datacenter's name is different than "datacenter1" (as confirmed by `nodetool status`). Setting to "datacenter1" works though. For me, before I did this I would get inconsistent results, sometimes working sometimes not, but after I did this, it worked great. – RyanQuey Oct 14 '20 at 03:52
  • Update: it seems like it might relate to whether or not there is an instance of C* running on the machine using the Java driver. I was connecting to C* running on an external AWS, but had C* (specifically DSE) running on my local box, and had to use .withLocalDatacenter("datacenter1"). But when I stopped the local C* instance (sudo service dse stop), then tried again, now it couldn't connect, and complained giving the error as shown in next comment – RyanQuey Oct 14 '20 at 04:44
  • `You specified datacenter1 as the local DC, but some contact points ar e from a different DC: Node(endPoint=/:9042, hostId=, hashCode=4a395efc)=SearchGraph; please provide the correct local DC, or check your contact points` – RyanQuey Oct 14 '20 at 04:44
15

You can execute nodetool status in cassandra and you will obtain the name of the Datacenter. That is the value that you must put in withLocalDatacenter method:

/opt/apache-cassandra-3.11.2/bin$ ./nodetool status
Datacenter: **dc1**
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address         Load       Tokens       Owns (effective)  Host ID    Rack
B--rian
  • 5,578
  • 10
  • 38
  • 89
Ceci G J
  • 151
  • 1
  • 2
  • This solution worked for me. In my case, I was using wrong data centre name DC1 in my application. But it was datacentre1 which I found with this "nodetools status" command. – Gunjan Shah Jul 27 '22 at 14:33
0

When running in local, better to run nodetool status command get the dc name and use in application while connecting to Cassandra instance.