Sunday, January 20, 2013

OpenSIPS and Cassandra Integrated

Finally back to the blog after a long busy schedule, well its still the same scheule but I've managed some time to take a look at setting up OpenSIPS with Cassandra as it's cache store. In one of my blog post I showed how to setup opensips and redis (which is also now updated for new Hiredis installation github path).

The advanatges can be numerious while we are using Cassandra and Redis individually or together from one opensips which are outlined in a very detailed article by Vlad Paiu - One of the developers and contributors of OpenSIPS project.
Possible use cases from the opensips documentation page are; Password caching for DB authentication
or Simple but distributed billing. With recent new developments going on specially on cacheDB are its now possible to have a distirubuted OpenSIPS cluster with a shared CacheDB backbone and all iternal data is available across all the opensips node for example dialog profiles of callers, loadbalanced resources statuses.
Over all these CacheDB modules enable multiple OpenSIPS instances to share internal data like number of calls, registration, call counters, billing data, IP balcklists etc. Refer to this presentation at Cluecon-2012.
So lets get started to integrate OpenSIPS 1.8 with Cassandra. Installation process for OpenSIPS 1.8 has been changed a little bit. Install all the libraries and packages as mentioned in this post.

OPENSIPS 1.8 INSTALLATION

Once after the repositiores are installed we need to install thrift-0.6.1 which is the Cassandra's interfcing client used by OpenSIPS.
root@OpenSIPS-CAS:~# wget http://archive.apache.org/dist/thrift/0.6.1/thrift-0.6.1.tar.gz
root@OpenSIPS-CAS:~# tar zxvf thrift-0.6.1.tar.gz
root@OpenSIPS-CAS:~# cd thrift-0.6.1/
root@OpenSIPS-CAS:~# ./configure
root@OpenSIPS-CAS:~# make
root@OpenSIPS-CAS:~# make install

UPDATE[28-July-2013]: As commented by a respected viewer; we need to install C++ boost libraries for OpenSIPS 1.9 on Debian 6. See comment by RQ in the comments section for further details.

I tried installing the newest version of thrift but OpenSIPS won't compile and give errors. So next, download and extract the 1.8 version of OpenSIPS and execute the 'make menuconfig' command.
root@OpenSIPS-CAS:~#wget http://opensips.org/pub/opensips/1.8.2/src/opensips-1.8.2-svn9601_src.tar.gz
root@OpenSIPS-CAS:~#tar zxvf opensips-1.8.2-svn9601_src.tar.gz
root@OpenSIPS-CAS:~#cd opensips-1.8.2-tls/
root@OpenSIPS-CAS:~#make menuconfig
A menu will open up like this.
Press Right arrow key on the first option and goto the following sub menu.
Now pressing right arrow key will take us to the module selection menu where we have to select which modules we need to install. Make sure to select the Cassandra module.
Once the modules' selection is made press left arrow key to go back to the figure 2. Select "Save Changes" and press enter. A list of suggested libraries and packages will show up at the bottom of the screen for help.
Do change installation paths and other flags according to your requirements.
Press left arrow key again to go back to main menu as in figure 1. Select "Compile And Install OpenSIPS" and press Enter. If everything goes well our opensips will be installed successfully. Follow the steps as mentioned in this post to setup the opensips LSB script (/etc/init.d/opensips); default opensips file; and creating the opensips DB. 
CASSANDRA INSTALLATION
The OpenSIPS installation process is complete till here, now take a look at the cassandra installation.
root@CAS-N1:~# vim /etc/apt/sources.list
Add the follwoing lines at the bottom of the apt/sources.list file.
deb http://www.apache.org/dist/cassandra/debian 10x main 

deb-src http://www.apache.org/dist/cassandra/debian 10x main
Save and exit the file. and run apt-get update.
root@CAS-N1:~# apt-get update
If any keys error is encountered at the update then we need to add the keys using following command. (Make sure to change the '4BD736A82B5C1B00' string with the one you're seeing on your screen)
root@CAS-N1:~#gpg --keyserver wwwkeys.pgp.net --recv-keys 4BD736A82B5C1B00
root@CAS-N1:~#gpg --export --armor 4BD736A82B5C1B00 | sudo apt-key add -
Once the key is successfully added execute the apt-get update once again and after that install cassandra.
root@CAS-N1:~#apt-get install cassandra
I highly recommend to go through the following links to get quick know how about cassandra working and its files.
Planning a Cassandra Cluster Deployment

Cassandra’s data model cheat sheet

Node and Cluster Configuration (cassandra.yaml)

Configuring and Starting a Cassandra Cluster
Edit the /etc/init.d/cassandra.yaml file and change the cluster name and any other property as needed. Start cassandra and verify that it is running in processes.

I edited the following:
listen_address: 192.168.56.3

broadcast_address: 192.168.56.255

rpc_address: 0.0.0.0

rpc_port: 9160
root@CAS-N1:~#vim /etc/cassandra/cassandra.yaml
root@CAS-N1:~#/etc/init.d/cassandra start
root@CAS-N1:~#ps -ef | grep cass
If cassandra has started successfully then connect to cassandra-cli and creake a keyspace for opensips to use.
root@CAS-N1:~# cassandra-cli --host 192.168.56.3 --port 9160
Connected to: "Test Cluster" on 192.168.56.3/9160
Welcome to Cassandra CLI version 1.0.12

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.
[default@unknown]CREATE KEYSPACE opensips WITH placement_strategy = 'SimpleStrategy' AND strategy_options = {replication_factor:2}; 
a43901e0-6263-11e2-0000-af8ddedd2ed7
Waiting for schema agreement...
... schemas agree across the cluster
[default@unknown]quit;

OPENSIPS CONFIGURATIONS FOR CASSANDRA
Now, everything is ready. Time to open up the opensips configurations file and load the module for cassandra.
root@OpenSIPS-CAS:~# vim /etc/opensips/opensips.cfg
loadmodule "cachedb_cassandra.so"
modparam("cachedb_cassandra", "cachedb_url","cassandra:group1://192.168.56.3:9160/opensips_cdrs")
modparam("cachedb_cassandra", "connection_timeout",1000)
modparam("cachedb_cassandra", "send_timeout",1000)
modparam("cachedb_cassandra", "receive_timeout",1000)
modparam("cachedb_cassandra", "wr_consistency_level",1)
modparam("cachedb_cassandra", "rd_consistency_level",1)
Go throught the OpenSIPS Cassandra module documentation; we can start using Cassandra as our KVP backend by using the following commands in our opensips.cfg main route;
cache_store("cassandra:group1","key","$ru value")
cache_fetch("cassandra:cluster1","key",$avp(10))
cache_remove("cassandra:cluster1","key");
That is all , now its upto users to take advantage from this according to their needs.
Thanks for reading.

Reference URLS:
http://wiki.apache.org/cassandra/GettingStarted
http://www.opensips.org/html/docs/modules/1.8.x/cachedb_cassandra.html
http://wiki.apache.org/cassandra/Operations
http://www.sinbadsoft.com/blog/cassandra-data-model-cheat-sheet/
http://news.ycombinator.com/item?id=1244914
http://www.datastax.com/docs/0.8/configuration/node_configuration#init-properties
http://www.datastax.com/docs/0.8/install/cluster_init
http://www.datastax.com/docs/0.8/cluster_architecture/cluster_planning#node-init-config
http://www.datastax.com/docs/1.0/ddl/keyspaces
http://wiki.apache.org/cassandra/FAQ#clustername_mismatch
http://lzyeval.blogspot.com/2012/02/install-cassandra-in-debian-ubuntu.html

6 comments:

  1. Hello,

    This is a nice tutorial, and I see you have quite a few other on this blog that are good as well.

    Would you mind if we linked your OpenSIPS tutorials from the OpenSIPS.org website, in the Tutorials section ?

    Best Regards,
    Vlad

    ReplyDelete
    Replies
    1. This is definitely an honour for me. Yes sure afterall thats why I write these things.

      Btw Vlad your recent MongoDB and Couchbase modules for cacheDB backend are great. Excellent work :)

      Delete
  2. Hello Gohar, You need to install C++ boost libraries before installing thrift, IF you want to install opensips-1.9 with cassandra.

    http://wiki.apache.org/thrift/ThriftRequirements
    cd thrift-*
    ./configure --enable-gen-cpp --enable-gen-c_glib --with-c_glib --with-cpp
    make all
    make install

    and then opensips.

    ReplyDelete
    Replies
    1. Thats great Hint RQ: I should put an update over eher for user using newer 1.9 OpenSIPS as this tutorial is for ver. 1.8 and I used Ubuntu.

      Please share your OS name,version and anything special to make this integration work on your setup.

      Thanks alot.
      Gohar

      Delete
    2. I used debian 6 and opensips 1.9.1 (latest stable as of now).

      Delete
  3. Dear Gohar,
    I have to make a record of current active calls in opensips in am using $DLG_COUNT variable to get the number of active calls. but when opensips get calls with higher cps rate or in other words for simultaneous calls it shows wrong call count e.g if I send 10 calls simultaneously then it shows only one active call. Please guide me what to do to handle the situation.

    Thanks in advance.

    ReplyDelete