Client connection monitoring

Hi ,
I am using java SDK to connect to Couchbase cluster and wanted to know different options for getting connection status w.r.t cluster nodes before trying out detailed hands on.

I can figure out two options

1 - Handling couchbase connection exception
This will not be real time, as and when I try to operate something I may get this exception

2- Subscribing to SDK event bus
Will the SDK will publish me NodeConnectedEvent and NodeDisconnectedEvent in real time. Will subscribing to event bus will have some performance penalty ?

Is there any other way to monitor the connection status so that I can pass the information to my monitoring server?

Thanks & Regards
Paresh

Hi Panda,

The SDKs provide diagnostics about the current state of the connections, as described in RFC-0034 (Health Check). Here’s a code snippet for Java:

void printDiagnostics(Bucket bucket) {
    bucket.ping();
    DiagnosticsReport report = cluster.diagnostics();
    System.out.println(report.exportToJson(true));
}

For a single-node cluster the output is something like:

{
  "services" : {
    "view" : [ {
      "last_activity_us" : 2863,
      "state" : "connected",
      "id" : "0x46ee8659",
      "remote" : "localhost:8092",
      "local" : "localhost:53309"
    } ],
    "fts" : [ {
      "last_activity_us" : 2625,
      "state" : "connected",
      "id" : "0x1c5de614",
      "remote" : "localhost:8094",
      "local" : "localhost:53308"
    } ],
    "kv" : [ {
      "last_activity_us" : 19243,
      "state" : "connected",
      "id" : "0x7d942ebb",
      "remote" : "localhost:11210",
      "local" : "localhost:53307"
    } ],
    "n1ql" : [ {
      "last_activity_us" : 1963,
      "state" : "connected",
      "id" : "0x8de927d",
      "remote" : "localhost:8093",
      "local" : "localhost:53310"
    } ]
  },
  "sdk" : "couchbase-java-client/2.5.8 (git: 2.5.8, core: 1.5.8) (Mac OS X/10.13.4 x86_64; Java HotSpot(TM) 64-Bit Server VM 1.8.0_171-b11)",
  "id" : "9c7e2ac7-9a41-4e67-8cdf-bf16abaa35a4",
  "version" : 1
}

You can also inspect the report programmatically and decide what “healthy” means for your particular application. If all you care about is whether there’s connection to each node, you could do something like:

boolean allEndpointsConnected(DiagnosticsReport report) {
    for (EndpointHealth endpoint : report.endpoints()) {
        if (endpoint.state() != LifecycleState.CONNECTED) {
            return false;
        }
    }
    return true;
}

Thanks,
David

2 Likes

Oh, and the Bucket.ping() method returns a PingReport you can inspect to find out if the nodes are connected and how quickly they respond to the ping request.

1 Like

Thank you so much!
I will try out your suggestions.

Regards
Paresh