Cassandra学习笔记之机架感应策略

来源:互联网 发布:javascript 注释规范 编辑:程序博客网 时间:2024/04/29 22:14

Snitches概述

Cassandra提供了Snitches功能,可以知道集群中的每个节点所属数据中心和机架。所有机架感应策略都实现了相同的接口IEndpointSnitch。先来看看Snitches的类图:


IEndpointSnitch接口中提供了比较实用的方法:

//通过ip地址获取机架public String getRack(InetAddress endpoint);//通过ip地址获取数据中心public String getDatacenter(InetAddress endpoint);//按节点距离排序public List<InetAddress> getSortedListByProximity(InetAddress address, Collection<InetAddress> unsortedAddress);//比较节点与指定节点远近public int compareEndpoints(InetAddress target, InetAddress a1, InetAddress a2);//开如gossip协议public void gossiperStarting();
Snitches按实现分为三种:

(1)SimpleSnitch:这种策略不能识别数据中心和机架信息,适合在单数据中心使用;

(2)NetworkTopologySnitch:这种策略提供了网络拓扑结构,以便更高效地消息路由;

(3)DynamicEndpointSnitch:这种策略可以记录节点之间通信时间间隔,记录节点之间通信速度,从而达到动态选择最合适节点的目的。
SimpleSnitch比较简单就不用介绍了,此类只是一个默认实现。下面主要介绍NetworkTopologySnitch和DynamicEndpointSnitch两种策略。


NetworkTopologySnitch

此策略提供了网络拓扑结构,因此可以知道节点之间的远近关系,该抽象类实现了compareEndpoints方法,代码如下:
    public int compareEndpoints(InetAddress address, InetAddress a1, InetAddress a2)    {        if (address.equals(a1) && !address.equals(a2))            return -1;        if (address.equals(a2) && !address.equals(a1))            return 1;        String addressDatacenter = getDatacenter(address);        String a1Datacenter = getDatacenter(a1);        String a2Datacenter = getDatacenter(a2);        if (addressDatacenter.equals(a1Datacenter) && !addressDatacenter.equals(a2Datacenter))            return -1;        if (addressDatacenter.equals(a2Datacenter) && !addressDatacenter.equals(a1Datacenter))            return 1;        String addressRack = getRack(address);        String a1Rack = getRack(a1);        String a2Rack = getRack(a2);        if (addressRack.equals(a1Rack) && !addressRack.equals(a2Rack))            return -1;        if (addressRack.equals(a2Rack) && !addressRack.equals(a1Rack))            return 1;        return 0;    }
1、先比较ip地址,如果其中一个节点的ip地址与给定节点相同,另一个不相同,则返回;
2、到这里表示3个节点ip均不相同,与1一样,比较数据中心,然后比较机架;
3、到这里表示同数据中心,同机架,返回0.
这里用到了两个方法:getDatacenter和getRack,不同的子类实现不同。AbstractNetworkTopologySnitch有四个子类实现:

PropertyFileSnitch

使用属性文件配置拓扑结构,该文件位于conf/cassandra-topology.properties中,配置类似
# Data Center One175.56.12.105=DC1:RAC1175.50.13.200=DC1:RAC1175.54.35.197=DC1:RAC1
该文件记录数据中心和机架的位置,数据中心名称可以随意定义,集群中的所有节点都应该有相同的配置。获取数据中心和机架直接读取配置文件即可:
public String getDatacenter(InetAddress endpoint){    String[] info = getEndpointInfo(endpoint);    assert info != null : "No location defined for endpoint " + endpoint;    return info[0];}public String getRack(InetAddress endpoint){    String[] info = getEndpointInfo(endpoint);    assert info != null : "No location defined for endpoint " + endpoint;    return info[1];}


GossipingPropertyFileSnitch

通过属性文件(conf/cassandra-rackdc.properties)定义当前节点的数据中心和机架,并使用Gossip协议传播到其他节点,当cassandra-topology.properties文件存在的时候,cassandra-rackdc.properties只是做为一个备用。配置类似:
dc =DC1rack =RAC1
该策略便于快速传播一个节点的变化,一般和PropertyFileSnitch配合使用。其获取数据中心的代码如下:
    public String getDatacenter(InetAddress endpoint)    {        if (endpoint.equals(FBUtilities.getBroadcastAddress()))            return myDC;        EndpointState epState = Gossiper.instance.getEndpointStateForEndpoint(endpoint);        if (epState == null || epState.getApplicationState(ApplicationState.DC) == null)        {            if (psnitch == null)            {                if (savedEndpoints == null)                    savedEndpoints = SystemKeyspace.loadDcRackInfo();                if (savedEndpoints.containsKey(endpoint))                    return savedEndpoints.get(endpoint).get("data_center");                return DEFAULT_DC;            }            else                return psnitch.getDatacenter(endpoint);        }        return epState.getApplicationState(ApplicationState.DC).value;    }
1、如果是本机,直接返回本地配置文件中的数据中心;
2、不是本机的情况:如果接收到了Gossip消息,直接返回Gossip消息中的数据中心;否则返回本机conf/cassandra-topology.properties文件中的数据中心;
机架的获取与数据中心获取类似。

RackInferringSnitch

其实现与PropertyFileSnitch类似,根据ip地址确定数据中心和机架,如图所示:

第二个8位决定数据中心,第三个8位决定机架。

Ec2Snitch

主要使用在单区域数据中心,使用私有ip地址的情况。

DynamicEndpointSnitch

该策略通过监控两个节点的通信时间,来决定最佳选择方案,该策略是默认使用的,也是Cassandra推荐的。该策略和失败检测密切相关,Cassandra失败检测原理基于Hayashibara的一篇论文(http://ddg.jaist.ac.jp/pub/HDY+04.pdf).


参考资料:
http://www.datastax.com/documentation/cassandra/2.0/cassandra/architecture/architectureSnitchesAbout_c.html
源码下载地址git://git.apache.org/cassandra.git

0 0
原创粉丝点击