Coder
Coder

Reputation: 3262

Loadbalancing policy in cassandra

What is wrong with the following code, why the load balancing in Cassandra is not working?

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['172.30.56.63','172.30.36.129'], keyspace: 'qnapstat',
                                      policies : { loadBalancing : new cassandra.policies.loadBalancing.RoundRobinPolicy } });

const query = 'SELECT * FROM nodedata WHERE partitionkey = ?';

async function read() {
    console.log(client.options.policies.loadBalancing);
    const params = ['testdata'];
    var response = await client.execute(query, params, { prepare: true, consistency: cassandra.types.consistencies.quorum });
    console.log(response.info);
    process.exit();
}

read();

I printed the tried hosts, I am able to see It always tries to fetch the 172.30.36.129 as coordinator, Not the other one.

Upvotes: 1

Views: 205

Answers (1)

Coder
Coder

Reputation: 3262

I misunderstood, Only with the same client connection it will work with that policy, To make it clear the following code works as expected,

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['172.30.56.60','172.30.56.61,172.30.56.62'], keyspace: 'qnapstat', 
                                      policies : { loadBalancing : new cassandra.policies.loadBalancing.RoundRobinPolicy
                         } });

var index = 0;
var totalcount = 10;

const query = 'SELECT readiops FROM nodedata where nodeip = ?';

async function read() {
    console.log(client.options.policies.loadBalancing);
    console.log(client.options.policies.retry);

    while(index < totalcount) {
       const params = ['testdata'];
       var response = await client.execute(query, params, { prepare: true, consistency: cassandra.types.consistencies.quorum });
       console.log(response.info);
       index = index + 1;
    }
       process.exit();
}

read();

Thanks,

Harry

Upvotes: 2

Related Questions