The frame induction strategy of Cassandra Learning notes

Source: Internet
Author: User
Tags assert cassandra
snitches Overview

Cassandra provides snitches functionality to know which data centers and racks each node in the cluster belongs to. All rack-sensing policies implement the same interface Iendpointsnitch. Let's take a look at Snitches's class diagram:


A more practical approach is provided in the Iendpointsnitch interface:

Gets the rack public
String getrack (inetaddress endpoint) through an IP address
; Gets the data center public
String Getdatacenter (inetaddress endpoint) by IP address;
Sort public list<inetaddress> getsortedlistbyproximity by node distance
(inetaddress address, collection< Inetaddress> unsortedaddress);
Compare nodes to the specified node near and far public
int compareendpoints (inetaddress target, inetaddress A1, InetAddress A2);
Open as gossip protocol public
Void Gossiperstarting ();
The snitches is divided into three types according to the realization:

(1) Simplesnitch: This strategy does not identify data center and rack information, suitable for use in a single data center;

(2) Networktopologysnitch: This strategy provides a network topology for more efficient message routing;

(3) Dynamicendpointsnitch: This strategy can record the communication time between nodes, record the communication speed between nodes, so as to achieve the dynamic selection of the most appropriate node.
Simplesnitch simpler to introduce, this class is just a default implementation. The following describes the two strategies of Networktopologysnitch and Dynamicendpointsnitch.


Networktopologysnitch This strategy provides a network topology, so you can know the distance between nodes, the abstract class implements the compareEndPoints method, the code is as follows:

    public int compareendpoints (inetaddress address, inetaddress A1, inetaddress A2) {if (address.equals)
        &&!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, the first comparison of IP address, if one of the nodes of the IP address and a given node the same, the other is not the same, then return; 2, the 3 node IP is not the same, as 1, compared to the data center, and then compare the rack; 3, to be represented here with the data center, the same rack, return 0. Here are two methods: Getdatacenter and Getrack, different subclass implementations. Abstractnetworktopologysnitch has four subclass implementations:
PropertyfilesnitchUse the property file to configure the topology, which is located in Conf/cassandra-topology.properties and is configured like
# Data Center One
175.56.12.105=dc1:rac1
175.50.13.200=dc1:rac1
175.54.35.197=dc1:rac1
The file records the location of the data center and rack, the data center name can be defined arbitrarily, and all nodes in the cluster should have the same configuration. Get the data center and rack read the configuration file directly:
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 defines the data center and racks of the current node through the properties file (conf/cassandra-rackdc.properties) and propagates to other nodes using the gossip protocol. When the Cassandra-topology.properties file exists, cassandra-rackdc.properties just as a standby. Configuration is similar:

DC =DC1
Rack =RAC1
This strategy facilitates the rapid propagation of a node's changes and is generally used in conjunction with Propertyfilesnitch. The code to get the data center is as follows:
 public String Getdatacenter (inetaddress endpoint) {if endpoint.equals (FBUTILITIES.GETB

        Roadcastaddress ()) return MYDC;
        Endpointstate epstate = Gossiper.instance.getEndpointStateForEndpoint (endpoint); if (epstate = null | | epstate.getapplicationstate (APPLICATIONSTATE.DC) = = null) {if (Psnitch = = NULL {if (savedendpoints = null) savedendpoints = Systemkeyspace.loaddcrack
                Info ();
                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, if it is native, directly return to the local configuration file in the data center, 2, is not the case of the machine: if received gossip message, directly return to the data center in gossip message; otherwise return to native conf/ The data center in the cassandra-topology.properties file; rack capture is similar to data center acquisition.
RackinferringsnitchThe implementation is similar to Propertyfilesnitch, which determines the data center and rack according to the IP address, as shown in the figure:
The second 8-bit determines the data center, and the third 8-bit determines the rack.
Ec2snitchUse a private IP address primarily in a single zone data center.
DynamicendpointsnitchThis strategy determines the best choice by monitoring the communication time of two nodes, which is used by default and is recommended by Cassandra. This strategy is closely related to failure detection, and the Cassandra failure detection principle is based on a Hayashibara paper (http://ddg.jaist.ac.jp/pub/HDY+04.pdf).

References: http://www.datastax.com/documentation/cassandra/2.0/cassandra/architecture/architectureSnitchesAbout_c.html
SOURCE Download Address Git://git.apache.org/cassandra.git

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.