15

For operations monitoring of my application, I am looking for something similar to the commonly used "SQL connection validation" query

SELECT 1;

in Cassandra, using the Hector driver. I have tried things like looking at Cluster.getKnownPoolHosts() and .getConnectionManager().getActivePools(). But it seems that their status is not continuously updated, only when I actually try to access Cassandra with a query.

I'd like my health check to be independent of any keyspaces or user CFs that need to exist, so just running a "dummy" query seems difficult (against what?). And of course it shouldn't take a lot of memory or generate any significant load.

Can I force Hector somehow to update its connection pool status without running a real query?

(BTW: CQL doesn't even accept "SELECT 1" as a valid query.)

HansMari
  • 347
  • 1
  • 2
  • 16

4 Answers4

20

With CQL3, I'm using the following query:

SELECT now() FROM system.local;

It would be nice to get rid of the FROM clause altogther to make this generic, in case the user does not have access to the system keyspace or local column family for some reason. But as with the other answers, at least this should not give false positives.

Eemeli Kantola
  • 5,437
  • 6
  • 35
  • 43
  • How actually you do this check? Do you catch some exception or examine QuerryResult object? – CAMOBAP Dec 17 '13 at 11:51
  • Yes, just checking that this one does not throw an exception. One could also verify that the query actually returns something, but I found that unnecessary. – Eemeli Kantola Jan 13 '14 at 13:39
  • do we have any better solution for this because it's very old comments – Vimit Dhawan Aug 07 '20 at 07:08
  • 2
    This query returns a timestamp, which I couldn't get converted to any Java type. The alternative query that works for me and returns a string is `SELECT release_version FROM system.local;` – Abhijit Sarkar Sep 01 '20 at 04:22
4

The solution I'm currently using, and which seems to be the most robust so far (tested with Cassandra 1.1 and 1.2) is a simple query on "system":

Query<String> query = Query.selectQuery("*", "system", null, 1, consistencyLevel, StringSerializer.get());

It's not exactly what I wanted since it's a "real" query, but on the other hand it doesn't give any false positives.

HansMari
  • 347
  • 1
  • 2
  • 16
  • I could be wrong but I think this approach will always call the same node(s) -- depending on consistencyLevel you will call always 1, Q or All nodes of your RF. If you want monitor if the connection is active you can do it, if you want monitor the ring status I'd use nodetool inside your java application – Carlo Bertuccini Apr 11 '13 at 14:53
  • 1
    This is intended as a connection check, not as an operations check of the Cassandra cluster. – HansMari Apr 12 '13 at 07:26
  • @HansMari, How actually you do this check? Do you check some query properties? – CAMOBAP Dec 17 '13 at 11:50
  • If the query works OK, the connection is up. The actual query results don't matter. – HansMari Dec 17 '13 at 13:04
  • So if there is no exception, we think that connection is up. correct? – CAMOBAP Jan 10 '14 at 10:20
  • @HansMari what does this look like in real cql query? – samxiao Jul 12 '14 at 16:56
0

Eemeli Kantola's query returns a timestamp, which I couldn't get converted to any Java type. The alternative query that works for me and returns a string is SELECT release_version FROM system.local;

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0

You can try to check cassandra status next way:

CqlSession session = ...;
Collection<Node> nodes = session.getMetadata().getNodes().values();
Optional<Node> nodeUp = nodes.stream().filter((node) -> node.getState() == NodeState.UP).findAny();
bool isNodeUp = nodeUp.isPresent();

Source: CassandraDriverHealthIndicator.