Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

mall.aayush99_177036 avatar image
mall.aayush99_177036 asked ·

Cassandra TCP connections through PHP/Java

I am having an application in code igniter which makes the connection to cassandra using the cassa DB PHP driver. The code for connection is as follows which is autoloaded.


<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CassaDb {
    private $CI;
    public $cassa_db;
    private $batch;
    private $schema;

    private $keyspace_name = "dev";
    private $username = "";
    private $password = "";
    private $hostname = "127.0.0.1";

    public function __construct() {
        if(!$this->cassa_db) {
            try {
                $cluster = Cassandra::cluster()
                    ->withContactPoints($this->hostname)
                    ->withDefaultConsistency(Cassandra::CONSISTENCY_LOCAL_QUORUM)
                    ->withCredentials($this->username, $this->password)
                    ->withRequestTimeout(30)
                    ->build();
                $this->cassa_db = $cluster->connect($this->keyspace_name);
                $this->batch = new Cassandra\BatchStatement();
                $this->schema = $this->cassa_db->schema();
            } catch(Exception $e) {
                log_message('error', $e->getMessage()); 
            }
        }
    }
}


The cassandra is in my local machine and uses only a single node.

In the idle case there are no connections to port 9042 which is used by Cassandra.

:~$ sudo lsof -i -P -n | grep '9042 (ESTAB' | wc -l
0

Once I hit the application URL I see some TCP connections made to the port 9042 by apache.

:~$ sudo lsof -i -P -n | grep '9042 (ESTAB' 
apache2   17394        www-data   21u  IPv4 2685247      0t0  TCP 127.0.0.1:47144->127.0.0.1:9042 (ESTABLISHED)
apache2   17394        www-data   27u  IPv4 2685248      0t0  TCP 127.0.0.1:47146->127.0.0.1:9042 (ESTABLISHED)

Again I go on refreshing the application or opening it in new browsers then some more TCP connections are made which goes on increasing to a limit of 20-22. After this limit no more connections are made.

:~$ sudo lsof -i -P -n | grep '9042 (ESTAB' | wc -l
16
:~$ sudo lsof -i -P -n | grep '9042 (ESTAB' 
apache2   17395        www-data   21u  IPv4 2685782      0t0  TCP 127.0.0.1:47236->127.0.0.1:9042 (ESTABLISHED)
apache2   17395        www-data   27u  IPv4 2687278      0t0  TCP 127.0.0.1:47238->127.0.0.1:9042 (ESTABLISHED)
apache2   17398        www-data   21u  IPv4 2685511      0t0  TCP 127.0.0.1:47170->127.0.0.1:9042 (ESTABLISHED)
apache2   17398        www-data   27u  IPv4 2682226      0t0  TCP 127.0.0.1:47172->127.0.0.1:9042 (ESTABLISHED)
apache2   17400        www-data   21u  IPv4 2689101      0t0  TCP 127.0.0.1:47274->127.0.0.1:9042 (ESTABLISHED)
apache2   17400        www-data   27u  IPv4 2685815      0t0  TCP 127.0.0.1:47276->127.0.0.1:9042 (ESTABLISHED)
apache2   17552        www-data   21u  IPv4 2684269      0t0  TCP 127.0.0.1:47206->127.0.0.1:9042 (ESTABLISHED)
apache2   17552        www-data   27u  IPv4 2688683      0t0  TCP 127.0.0.1:47208->127.0.0.1:9042 (ESTABLISHED)
apache2   17553        www-data   21u  IPv4 2686408      0t0  TCP 127.0.0.1:47214->127.0.0.1:9042 (ESTABLISHED)
apache2   17553        www-data   27u  IPv4 2683890      0t0  TCP 127.0.0.1:47216->127.0.0.1:9042 (ESTABLISHED)
apache2   17851        www-data   21u  IPv4 2688980      0t0  TCP 127.0.0.1:47248->127.0.0.1:9042 (ESTABLISHED)
apache2   17851        www-data   27u  IPv4 2687319      0t0  TCP 127.0.0.1:47250->127.0.0.1:9042 (ESTABLISHED)
apache2   17853        www-data   21u  IPv4 2687432      0t0  TCP 127.0.0.1:47292->127.0.0.1:9042 (ESTABLISHED)
apache2   17853        www-data   27u  IPv4 2686531      0t0  TCP 127.0.0.1:47294->127.0.0.1:9042 (ESTABLISHED)
apache2   17948        www-data   21u  IPv4 2682435      0t0  TCP 127.0.0.1:47288->127.0.0.1:9042 (ESTABLISHED)
apache2   17948        www-data   27u  IPv4 2685831      0t0  TCP 127.0.0.1:47290->127.0.0.1:9042 (ESTABLISHED)

When I look in to the phpinfo() there i see only 1 persistent connection and cluster in cassandra section.

The main point of the question is where did the Cassandra maintain the connection pool. There are multiple tcp connections established and only 1 persistent is being shown in phpinfo.

Also, I connect Cassandra with Java code where I produce Cassandra sessions in a singleton design pattern, where the old session is returned if it is present. Then also there are multiple TCP connections established by Java to port 9042 by Java.

:~$ sudo lsof -i -P -n | grep '9042 (ESTAB' 
apache2   17395        www-data   21u  IPv4 2685782      0t0  TCP 127.0.0.1:47236->127.0.0.1:9042 (ESTABLISHED)
apache2   17395        www-data   27u  IPv4 2687278      0t0  TCP 127.0.0.1:47238->127.0.0.1:9042 (ESTABLISHED)
apache2   17398        www-data   21u  IPv4 2685511      0t0  TCP 127.0.0.1:47170->127.0.0.1:9042 (ESTABLISHED)
apache2   17398        www-data   27u  IPv4 2682226      0t0  TCP 127.0.0.1:47172->127.0.0.1:9042 (ESTABLISHED)
apache2   17400        www-data   21u  IPv4 2689101      0t0  TCP 127.0.0.1:47274->127.0.0.1:9042 (ESTABLISHED)
apache2   17400        www-data   27u  IPv4 2685815      0t0  TCP 127.0.0.1:47276->127.0.0.1:9042 (ESTABLISHED)
apache2   17407        www-data   21u  IPv4 2687454      0t0  TCP 127.0.0.1:47302->127.0.0.1:9042 (ESTABLISHED)
apache2   17407        www-data   27u  IPv4 2689170      0t0  TCP 127.0.0.1:47304->127.0.0.1:9042 (ESTABLISHED)
apache2   17552        www-data   21u  IPv4 2684269      0t0  TCP 127.0.0.1:47206->127.0.0.1:9042 (ESTABLISHED)
apache2   17552        www-data   27u  IPv4 2688683      0t0  TCP 127.0.0.1:47208->127.0.0.1:9042 (ESTABLISHED)
apache2   17553        www-data   21u  IPv4 2686408      0t0  TCP 127.0.0.1:47214->127.0.0.1:9042 (ESTABLISHED)
apache2   17553        www-data   27u  IPv4 2683890      0t0  TCP 127.0.0.1:47216->127.0.0.1:9042 (ESTABLISHED)
apache2   17851        www-data   21u  IPv4 2688980      0t0  TCP 127.0.0.1:47248->127.0.0.1:9042 (ESTABLISHED)
apache2   17851        www-data   27u  IPv4 2687319      0t0  TCP 127.0.0.1:47250->127.0.0.1:9042 (ESTABLISHED)
apache2   17853        www-data   21u  IPv4 2687432      0t0  TCP 127.0.0.1:47292->127.0.0.1:9042 (ESTABLISHED)
apache2   17853        www-data   27u  IPv4 2686531      0t0  TCP 127.0.0.1:47294->127.0.0.1:9042 (ESTABLISHED)
apache2   17948        www-data   21u  IPv4 2682435      0t0  TCP 127.0.0.1:47288->127.0.0.1:9042 (ESTABLISHED)
apache2   17948        www-data   27u  IPv4 2685831      0t0  TCP 127.0.0.1:47290->127.0.0.1:9042 (ESTABLISHED)
apache2   17993        www-data   21u  IPv4 2687491      0t0  TCP 127.0.0.1:47316->127.0.0.1:9042 (ESTABLISHED)
apache2   17993        www-data   27u  IPv4 2690176      0t0  TCP 127.0.0.1:47318->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3413u  IPv4 2700469      0t0  TCP 127.0.0.1:47376->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3414u  IPv4 2700470      0t0  TCP 127.0.0.1:47378->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3450u  IPv4 2696024      0t0  TCP 127.0.0.1:47382->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3501u  IPv4 2694086      0t0  TCP 127.0.0.1:47384->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3525u  IPv4 2694087      0t0  TCP 127.0.0.1:47386->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3575u  IPv4 2692561      0t0  TCP 127.0.0.1:47388->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3576u  IPv4 2699860      0t0  TCP 127.0.0.1:47390->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3577u  IPv4 2694999      0t0  TCP 127.0.0.1:47392->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3580u  IPv4 2697303      0t0  TCP 127.0.0.1:47396->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3581u  IPv4 2694088      0t0  TCP 127.0.0.1:47398->127.0.0.1:9042 (ESTABLISHED)
java      18315           juser 3682u  IPv4 2694089      0t0  TCP 127.0.0.1:47400->127.0.0.1:9042 (ESTABLISHED)

The several TCP connections created above are the multiple Cassandra sessions or connections on a particular session.


cassandradriverphpcassatcp
stack1.png (31.1 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered ·

@mall.aayush99_177036 I'm not familiar with your configuration -- is it PHPCassa? If so, it may not be the best choice to develop a new application using a driver that's no longer maintained or doesn't appear to have activity for a while.

In any case, the number of connections is configured on the driver (client) side so you will need to work out the defaults used by your driver if you didn't override it. It isn't maintained on the server-side (C* cluster). Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.