gpt4 book ai didi

java - Cassandra如何选择发送请求的节点?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:17:44 26 4
gpt4 key购买 nike

假设一个 Cassandra 集群需要由客户端应用程序访问。在 Java api 中,我们创建一个集群实例并通过 Session 发送读取或写入请求。如果我们使用读/写一致性 ONE,api 如何选择实际节点(协调器节点)以转发请求。是随机选择吗?请帮忙解决这个问题。

最佳答案

Cassandra 驱动程序使用“八卦”协议(protocol)(以及称为节点发现的过程)来获取有关集群的信息。如果一个节点变得不可用,客户端驱动程序会自动尝试其他节点并安排与死节点的重新连接时间。根据to the DataStax docs :

Gossip is a peer-to-peer communication protocol in which nodes periodically exchange state information about themselves and about other nodes they know about. The gossip process runs every second and exchanges state messages with up to three other nodes in the cluster. The nodes exchange information about themselves and about the other nodes that they have gossiped about, so all nodes quickly learn about all other nodes in the cluster.

本质上,您提供给客户端连接的节点列表是获取整个集群信息的初始联系点。这就是为什么您的客户端可以与集群中的所有节点通信(如果需要),即使您可能只在连接字符串中提供一小部分节点。

一旦你的驱动程序有了关于集群的八卦信息,它就可以做出关于在哪个节点上运行查询的智能决策。节点选择不是投票或随机选择的过程。根据返回的八卦信息,客户端驱动程序应用其 Load Balancing Policy .虽然它确实考虑了几个因素,但基本上它会尝试选择与客户端的网络“距离”最短的节点。

编辑20200322

让我稍微扩展一下负载平衡策略。我鼓励高性能应用程序的开发人员使用 TokenAwarePolicy。此策略将分区键值散列为“ token ”,并使用此散列来确定哪个节点负责生成的 token 范围。这具有跳过选择“协调器”节点的中间步骤的效果,并将查询直接发送到包含所请求数据的节点。

但是,如果您使用的是非 token 感知型负载平衡策略,或者运行的查询未对分区键进行过滤,则上述原始过程适用。

关于java - Cassandra如何选择发送请求的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27077701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com