question

mysub99 avatar image
mysub99 asked smadhavan edited

What is the difference between CqlSession and Cluster object in the Java driver?

Im new to Cassandra and i have seen some projects where in Spring Boot Cassandra connection is configuration with either CqlSession or Cluster, and i would like to know the difference.

CqlSession is from com.datastax.oss.driver.api.core and Cluster com.datastax.driver.core.

I have a requirement for Retry Policy , but not getting this option in CqlSession configuration. is there any way to implement this with CqlSession ?, same Retry policy option is available with Cluster configuration as shown below.

using CqlSession

CqlSession session = CqlSession
        .builder()
        .withLocalDatacenter("Cassandra")
        .addContactPoint(new InetSocketAddress("127.0.0.1", 9252))
        .withKeyspace(keyspace)
        .build()

using Cluster

Cluster cluster = Cluster
        .builder()
        .addContactPoint("127.0.0.1")
        .withRetryPolicy(new MyCustomRetryPolicy())
        .build()

Thanks

java driver
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered smadhavan edited

It looks like you're looking at old code examples. The Java driver was completely refactored in version 4.0. It is not binary compatible with earlier versions of the driver so you will need to familiarise yourself with the Upgrade Guide.

In particular, the Cluster object does not exist anymore so you need to use CqlSession for most things.

Driver configuration has also been refactored. The recommendation in v4.0 is to configure the driver using the application.conf file. Specifically about the retry policy, you need to override the default policy using execution profiles. For example:

datastax-java-driver {
  advanced.retry-policy {
    class = DefaultRetryPolicy
  }
  profiles {
    custom-retries {
      advanced.retry-policy {
        class = CustomRetryPolicy
      }
    }
  }
}

If you're new to Cassandra, I would discourage you from implementing your own retry policy until you have a bit more experience so use the built-in retry policies instead. Cheers!

5 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

mysub99 avatar image mysub99 commented ·

How can i test retry policy effectively?

0 Likes 0 ·
smadhavan avatar image smadhavan ♦ mysub99 commented ·

@mysub99, based on the retry policy you choose, you might be able to set up a 3-node cluster, and then try to shut down nodes one by one as your client app is running to test this out. You might also be interested in this blog post (along with a source code) that is published.

1 Like 1 ·
Erick Ramirez avatar image Erick Ramirez ♦♦ smadhavan ♦ commented ·

That strategy will trigger the UNAVAILABLE error only where there are not enough live replicas to achieve the required consistency.

The retry policy gets triggered for a lot of other error cases that your strategy won't address:

  • READ_TIMEOUT or WRITE_TIMEOUT
  • ClosedConnectionException due to network failure
  • HeartbeatException
  • OverloadedException
  • ServerError where a node reports an internal failure (other than timeout)
  • ReadFailureException usually due to TombstoneOverwhelmException
  • WriteFailureException where a replica reported a write error (other than timeout) to the coordinator

None of the above retry cases will be triggered by taking nodes down. Just know that the default retry policy will handle all these cases. Cheers!

2 Likes 2 ·
Show more comments
Erick Ramirez avatar image Erick Ramirez ♦♦ mysub99 commented ·

I don't know of a way to simulate request failures that would allow you to test retries. Cheers!

0 Likes 0 ·
smadhavan avatar image
smadhavan answered smadhavan edited

@mysub99, welcome to trying Cassandra!

There are a couple options here. If you are using the latest com.datastax.oss:java-driver-core:4.12.0 version on your client application connecting to DataStax Astra or DataStax Enterprise or Apache Cassandra, you could refer to this documentation that has rich options for retries.

If you're using Spring to interact with Cassandra, you could choose to follow the browser-based free tutorial on this developer portal.

Other resources for learning are also available on here:

Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.