Question

Is there any direct way to check if a Cluster/Session is connected/valid/ok?

I mean, I have a com.datastax.driver.core.Session created into a neverending thread and I'd like to assure the session is ok every time is needed. I use the next cluster initialization, but I'm not sure this is enough...

Cluster.builder().addContactPoint(url)
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.withReconnectionPolicy(new ConstantReconnectionPolicy(1000L)).build());
Était-ce utile?

La solution

In fact when using the DataStax Java Driver, you have a hidden/magic capability embedded:

The driver is aware of the full network topology (nodes topology across datacenters and nodes availabilities).

Thus, the only thing you have to do is to initialise your cluster with a few nodes(1) and then you can be sure at every moment that if there is at least one available node your request will be performed correctly. Because the driver is topology aware, if one node (even initialisation nodes) goes out of availability, the driver will automagically route your request to another available node.

In summary, your code is good(1).

(1): You should provide a few nodes in order to be fault tolerant in the cluster initialisation phase. Indeed, if one initialisation node is down, the driver has then the possibility to contact another one to discover the full topology.

Autres conseils

I have a local development environment setup where I am starting up my java application and Cassandra (Docker) container at the same time, so Cassandra will normally not be in a ready state when the java application first attempts to connect.

When this is starting up the application will throw a NoHostAvailableException when the Cluster instance attempts to create a Session. Subsequent attempts to create a Session from the Cluster will then throw an IllegalStateException because the cluster instance was closed after the first exception.

What I did to rememdy this was to create a check method that attempts to create a Cluster and Session and then immediately closes these. See this:

private void waitForCassandraToBeReady(String keyspace, Cluster.Builder builder) {
    RuntimeException exception = null;
    int retries = 0;

    while (retries++ < 40) {
        Session session = null;
        Cluster cluster = null;
        try {
            cluster = builder.build();
            session = cluster.connect(keyspace);
            log.info("Cassandra is available");
            return;
        } catch (RuntimeException e) {
            log.warn("Cassandra not available, try {}", retries);
            exception = e;
        } finally {
            if (session != null && !session.isClosed()) session.close();
            if (cluster != null && !cluster.isClosed()) cluster.close();
        }
        sleep();
    }
    log.error("Retries exceeded waiting for Cassandra to be available");

    if (exception != null) throw exception;
    else throw new RuntimeException("Cassandra not available");
}

After this method returns, I then create a create a Cluster and Session independent of this check method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top