skip to Main Content

Created a cassandra cluster using gocql library.

func CreateCassandraCluster(host string) (*gocql.Session, error) {
cluster := gocql.NewCluster(host)
cluster.ConnectTimeout = time.Second * 10
cluster.DisableInitialHostLookup = true
session, err := cluster.CreateSession()
if err != nil {
return session, err
}
return session, nil
}

my objective is to make a function with *gocql.Session as a argument that will check when passing any cassandra cluster’s session is it connected or not connected?
For redis we have PING query to determine redis is connected or not do we have similar thing for cassandra using gocql?

2

Answers


  1. Chosen as BEST ANSWER

    Tried this approach where we are going to just check session open or closed

    func CheckCassandraHealthCheck(Pool interface{}, Host string, Port int) map[string]interface{} {
    response := make(map[string]interface{})
    response1 := make(map[string]interface{})
    cassandraCluster := Pool.(*gocql.Session)
    response["type"] = 4
    response1["host"] = Host
    response1["PORT"] = Port
    
    if cassandraCluster != nil && !cassandraCluster.Closed() {
        response["STATUS"] = constant.UP
        response["DETAILS"] = response1
        
        return response
    }
    response["DETAILS"] = response1
    response["STATUS"] = constant.DOWN
    return response
    

    Not sure if it is the correct approach or not


  2. This looks like an identical question you asked yesterday although I can’t seem to locate it now so I’m wondering if you deleted it.

    It’s not obvious to me what you are trying to achieve. Checking if the session is connected or not is not helpful when it comes to Cassandra.

    When the driver establishes a connection to the cluster for the first time, it uses the hosts you passed in NewCluster() to do an initial connection to discover the rest of the nodes and the cluster topology (rack configuration, data centres), then establishes connections to the nodes.

    If a connection fails, the default policy in the driver is smart enough to automatically reconnect so duplicating this functionality is pointless.

    As I stated in your other question, you are better off using free open-source tools like Metric Collector for Apache Cassandra (MCAC) if your ultimate goal is to monitor the health of your Cassandra cluster. Cheers!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search