Cassandra源码学习:机架感应策略

来源:互联网 发布:淘宝晒单福利图 编辑:程序博客网 时间:2024/04/28 14:52

Snitches概述

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


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

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //通过ip地址获取机架  
  2. public String getRack(InetAddress endpoint);  
  3. //通过ip地址获取数据中心  
  4. public String getDatacenter(InetAddress endpoint);  
  5. //按节点距离排序  
  6. public List<InetAddress> getSortedListByProximity(InetAddress address, Collection<InetAddress> unsortedAddress);  
  7. //比较节点与指定节点远近  
  8. public int compareEndpoints(InetAddress target, InetAddress a1, InetAddress a2);  
  9. //开如gossip协议  
  10. public void gossiperStarting();  
Snitches按实现分为三种:

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

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

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


NetworkTopologySnitch

此策略提供了网络拓扑结构,因此可以知道节点之间的远近关系,该抽象类实现了compareEndpoints方法,代码如下:
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public int compareEndpoints(InetAddress address, InetAddress a1, InetAddress a2)  
  2. {  
  3.     if (address.equals(a1) && !address.equals(a2))  
  4.         return -1;  
  5.     if (address.equals(a2) && !address.equals(a1))  
  6.         return 1;  
  7.   
  8.     String addressDatacenter = getDatacenter(address);  
  9.     String a1Datacenter = getDatacenter(a1);  
  10.     String a2Datacenter = getDatacenter(a2);  
  11.     if (addressDatacenter.equals(a1Datacenter) && !addressDatacenter.equals(a2Datacenter))  
  12.         return -1;  
  13.     if (addressDatacenter.equals(a2Datacenter) && !addressDatacenter.equals(a1Datacenter))  
  14.         return 1;  
  15.   
  16.     String addressRack = getRack(address);  
  17.     String a1Rack = getRack(a1);  
  18.     String a2Rack = getRack(a2);  
  19.     if (addressRack.equals(a1Rack) && !addressRack.equals(a2Rack))  
  20.         return -1;  
  21.     if (addressRack.equals(a2Rack) && !addressRack.equals(a1Rack))  
  22.         return 1;  
  23.     return 0;  
  24. }  
1、先比较ip地址,如果其中一个节点的ip地址与给定节点相同,另一个不相同,则返回;
2、到这里表示3个节点ip均不相同,与1一样,比较数据中心,然后比较机架;
3、到这里表示同数据中心,同机架,返回0.
这里用到了两个方法:getDatacenter和getRack,不同的子类实现不同。AbstractNetworkTopologySnitch有四个子类实现:

PropertyFileSnitch

使用属性文件配置拓扑结构,该文件位于conf/cassandra-topology.properties中,配置类似
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. # Data Center One  
  2. 175.56.12.105=DC1:RAC1  
  3. 175.50.13.200=DC1:RAC1  
  4. 175.54.35.197=DC1:RAC1  
该文件记录数据中心和机架的位置,数据中心名称可以随意定义,集群中的所有节点都应该有相同的配置。获取数据中心和机架直接读取配置文件即可:
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public String getDatacenter(InetAddress endpoint)  
  2. {  
  3.     String[] info = getEndpointInfo(endpoint);  
  4.     assert info != null : "No location defined for endpoint " + endpoint;  
  5.     return info[0];  
  6. }  
  7. public String getRack(InetAddress endpoint)  
  8. {  
  9.     String[] info = getEndpointInfo(endpoint);  
  10.     assert info != null : "No location defined for endpoint " + endpoint;  
  11.     return info[1];  
  12. }  


GossipingPropertyFileSnitch

通过属性文件(conf/cassandra-rackdc.properties)定义当前节点的数据中心和机架,并使用Gossip协议传播到其他节点,当cassandra-topology.properties文件存在的时候,cassandra-rackdc.properties只是做为一个备用。配置类似:
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. dc =DC1  
  2. rack =RAC1  
该策略便于快速传播一个节点的变化,一般和PropertyFileSnitch配合使用。其获取数据中心的代码如下:
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public String getDatacenter(InetAddress endpoint)  
  2. {  
  3.     if (endpoint.equals(FBUtilities.getBroadcastAddress()))  
  4.         return myDC;  
  5.   
  6.     EndpointState epState = Gossiper.instance.getEndpointStateForEndpoint(endpoint);  
  7.     if (epState == null || epState.getApplicationState(ApplicationState.DC) == null)  
  8.     {  
  9.         if (psnitch == null)  
  10.         {  
  11.             if (savedEndpoints == null)  
  12.                 savedEndpoints = SystemKeyspace.loadDcRackInfo();  
  13.             if (savedEndpoints.containsKey(endpoint))  
  14.                 return savedEndpoints.get(endpoint).get("data_center");  
  15.             return DEFAULT_DC;  
  16.         }  
  17.         else  
  18.             return psnitch.getDatacenter(endpoint);  
  19.     }  
  20.     return epState.getApplicationState(ApplicationState.DC).value;  
  21. }  
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
原创粉丝点击