How do I check if the Session object in GoCQL is connected similar to PING in Redis?

huangapple go评论91阅读模式
英文:

How do I check if the Session object in GoCQL is connected similar to PING in Redis?

问题

创建了一个使用gocql库的Cassandra集群。

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
}

我的目标是创建一个函数,该函数以*gocql.Session作为参数,用于检查传递的任何Cassandra集群的会话是否已连接或未连接。
对于Redis,我们有PING查询来确定Redis是否已连接,那么对于使用gocql的Cassandra,我们有类似的功能吗?

英文:

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?

答案1

得分: 1

这看起来像是你昨天问过的同样的问题,尽管我现在找不到它,所以我想知道你是否删除了它。

对于我来说,你试图实现什么并不明显。当涉及到Cassandra时,检查会话是否连接或断开并不有用。

当驱动程序首次与集群建立连接时,它使用你在NewCluster()中传递的主机来进行初始连接,以发现其他节点和集群拓扑(机架配置、数据中心),然后建立与节点的连接。

如果连接失败,驱动程序中的默认策略足够智能,可以自动重新连接,因此复制此功能是没有意义的。

正如我在你的另一个问题中所说,如果你的最终目标是监控Cassandra集群的健康状况,你最好使用免费的开源工具,比如Apache Cassandra的度量收集器(MCAC)。祝好!

英文:

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!

答案2

得分: 0

尝试了这种方法,我们只是检查会话是否打开或关闭。

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
}

不确定这是否是正确的方法。

英文:

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

huangapple
  • 本文由 发表于 2022年9月2日 14:05:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/73578402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定