Tuesday, 8 July 2014

Replication in cassandra using – PropertyFileSnitch (NetworkTopologyStrategy)

Replication is the process of storing copies of data on multiple nodes to ensure reliability and fault tolerance. Replication Strategy 1. SimpleStrategy -- simple single data center clusters. 2. NetworkTopologyStrategy --cluster deployed across multiple data centers.

 Here I am working on 5 node Cassandra cluster. CC1 192.168.56.138 (seed 1) CC2 192.168.56.142 CC3 192.168.56.144 CC4 192.168.56.143 (seed 2) CC5 192.168.56.145 Step 1: Edit the cassandra.yaml file in all nodes.(endpoint_snitch: SimpleSnitch -- it’s default )

1.1 endpoint_snitch: SimpleSnitch  replace with endpoint_snitch: PropertyFileSnitch.
1.2 Each DataCenter we have one seed node , that we have to update in cassandra.yaml file
seed_provider:
    -seed: “ 198.168.56.138,198.168.56.143”
Node : Do it on all nodes
Step 2: Edit this file as /conf/cassandra-topology.properties in all nodes
Note: Remove or comment the line’s b/w (# Cassandra Node IP=Data Center:Rack  ----- # default for unknown nodes)
NOTE:   Replace as you configuration  
NOTE:  3 node in Data center 1 and 2 node in Data center 2
# DATA_CENTER :1
192.168.56.138=DC1:RAC1
192.168.56.142=DC1:RAC1
192.168.56.144=DC1:RAC1

# DATA_CENTER :2
192.168.56.143=DC2:RAC1
192.168.56.145=DC2:RAC1

# default for unknown nodes
default=DC1:RAC1
Step 3: Start Cassandra in all node’s(./cassandra –f)
Check with (./nodetool status) .. it’s show as you specified on about properties file

STEP :4 Test scenario : 1 Test the setting … How the replication factor working in cluster with 2 datacenters.
4.1 create Keyspace with NetworkTopologyStrategy … here I am maintain data in Datacenter 1 all nodes(3), Datacenter 2 all nodes(2).


cqlsh> create keyspace webapp_test with replication= { 'class'    :'NetworkTopologyStrategy','DC1':3, 'DC2':2};


4.2 . This Command Show the key space structure.


cqlsh> desc keyspace webapp_test ;
CREATE KEYSPACE webapp_test  WITH replication = {
  'class': 'NetworkTopologyStrategy',
  'DC2': '2',
  'DC1': '3'
};



4.3 Create Column Family Account.


cqlsh> use webapp_test ;
cqlsh:webapp_test > create table account (accname text PRIMARY KEY , url text, username text);
cqlsh:webapp_test > desc tables;

account


4.4 Insert data


cqlsh> INSERT INTO webapp_test .account(accname,username,url) values('1','TLlive','www.tllive.com');
cqlsh> INSERT INTO webapp_test .account(accname,username,url) values('2','TLlive_2','www.tllive2.com');
cqlsh> INSERT INTO webapp_test .account(accname,username,url) values('3','ESPN','www.espn.com');

cqlsh> select * from webapp_test .account;

 accname | url             | username
---------+-----------------+----------
       3 |    www.espn.com |     ESPN
       2 | www.tllive2.com | TLlive_2
       1 |  www.tllive.com |   TLlive

(3 rows)

cqlsh> quit;


4.4 It will show the data distributed nodes in the cluster using( getendpoints)


It will show the data distributed nodes in the cluster using( getendpoints)
user@cc1:~/cassandra/bin$ ./nodetool getendpoints webapp_test  account accname
192.168.56.144
192.168.56.145
192.168.56.143
192.168.56.138
192.168.56.142 
Note: Data replicated in Dc1 3 node & DC2 2 nodes)
138,142,144---dc1
143,145 –dc2
----------------


Test scenario : 2
4.2.1 : Alter the keyspace webapp_test as


cqlsh> Alter keyspace webapp_test  with replication= { 'class' : 'NetworkTopologyStrategy','DC1':2, 'DC2':1};
cqlsh> desc keyspace webapp_test  ;

CREATE KEYSPACE webapp_test  WITH replication = {
  'class': 'NetworkTopologyStrategy',
  'DC2': '1',
  'DC1': '2'
};

cqlsh> quit;
user@cc1:~/cassandra/bin$ ./nodetool getendpoints webapp_test  account accname
192.168.56.144
192.168.56.145
192.168.56.138
Note: Data replicated in Dc1 1 node & DC2  2 nodes)
138---dc1
143,145 –dc2



Test scenario : 3


cqlsh> desc keyspace web_app1;

CREATE KEYSPACE web_app1 WITH replication = {
  'class': 'SimpleStrategy',
  'replication_factor': '2'
};
cqlsh> Alter  KEYSPACE web_app1 WITH replication = {'class' :'NetworkTopologyStrategy','DC1':2, 'DC2':2};
cqlsh> desc keyspace web_app1;

CREATE KEYSPACE web_app1 WITH replication = {

  'class': 'NetworkTopologyStrategy',
  'DC2': '2',
  'DC1': '2'

user@cc1:~/cassandra/bin$ ./nodetool repair
It will run in all nodes sequential fashion 
user@cc1:~/cassandra/bin$ ./nodetool getendpoints web_app1 users username
192.168.56.144
192.168.56.143
192.168.56.138
192.168.56.145






Acknowledgements

Thank you Rakesh Budagam and David Letchumanan for this post.

1 comment:

  1. I accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.WE are providing AngularJs training in Velachery.
    For more details:AngularJs training in Velachery

    ReplyDelete