Pradeep Charan
Pradeep Charan

Reputation: 683

DataStax API Cassandra execute method returns empty result set some times

I am using DataStax API to read records from Cassandra database,Some times execute method in Session is showing strange behavior. Sometimes it results empty result set, sometimes it returns correct result set.

Here is my code

    //Create session instance, using Singleton pattern
          public synchronized static Static getSession(){
          if(session !=null){
   //Not sending all the clusters

             Cluster cluster = Cluster.builder().withPort(myPort).addClusterPoints(clusters).withCredentials("username","password").
                withSocketOptions(new SocketOptions().setReadTimeoutMillis(30000).setConnectTimeoutMillis(30000)).build();
           session = cluster.connect("database");
           }
           else{
             return session;
           }
        }

        //get the session and execute query and return resultset
        public void executeQuery(){

           Session session = getSession();
           BoundStatement boundStatement = ..
           ResultSet result = session.execute(boundStatement);
           System.out.println(result.isExhausted); // true
           System.out.println(result.isFullyFetched); // true

           System.out.println(result.all().size()); /// **0 sometimes, correct count sometimes**

        }

I am not sending all clusters in addClusterPoints due to some reasons. Does it create any problem? But i am getting data some times.

Upvotes: 0

Views: 1136

Answers (1)

Alex Ott
Alex Ott

Reputation: 87174

This may depend on your replication factor (RF) & the state of your cluster. If you have RF > 1, then default TokenAware/DCAware policy will try to fetch data from any of nodes that contain replica. By default, query is executed with consistency level LOCAL_ONE - this means that answer from one replica is enough, and in that case it could be possible that replica that is contacted, doesn't have data that you need.

This happens if one of the replicas was offline for time longer than the size of time window for hints (3 hours by default), and that repair wasn't performed afterwards. To mitigate the problem, run the nodetool repair (precise recommendation depends on your version of DSE - if you're using it).

Upvotes: 1

Related Questions