Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
const P = require('bluebird');
const cass = require('cassandra-driver');
const fs = require('fs');
const loadBalancing = cass.policies.loadBalancing;
const reconnection = cass.policies.reconnection;
const DB = require('./db');
function validateAndNormalizeDcConf(conf) {
// Default to 'datacenter1'
conf.localDc = conf.localDc || 'datacenter1';
conf.datacenters = conf.datacenters || ['datacenter1'];
if (!(conf.datacenters instanceof Array)) {
throw new Error('invalid datacenters configuration (not an array)');
}
if (conf.datacenters.indexOf(conf.localDc) < 0) {
throw new Error('localDc not in configured datacenters');
}
}
// sync
driver.connect('myKeyspace', () => {
const dsOptions = cql.Client.lastCall.args[0];
expect(dsOptions.policies).to.be.an('object');
expect(dsOptions.policies.loadBalancing)
.to.be.instanceOf(cql.policies.loadBalancing.TokenAwarePolicy);
expect(dsOptions.policies.loadBalancing.childPolicy)
.to.be.instanceOf(cql.policies.loadBalancing.DCAwareRoundRobinPolicy);
expect(dsOptions.policies.loadBalancing.childPolicy.localDc)
.to.equal('some_dc');
done();
});
});
driver.connect('myKeyspace', () => {
const dsOptions = cql.Client.lastCall.args[0];
expect(dsOptions.policies).to.be.an('object');
expect(dsOptions.policies.loadBalancing)
.to.be.instanceOf(cql.policies.loadBalancing.TokenAwarePolicy);
expect(dsOptions.policies.loadBalancing.childPolicy)
.to.be.instanceOf(cql.policies.loadBalancing.DCAwareRoundRobinPolicy);
expect(dsOptions.policies.loadBalancing.childPolicy.localDc)
.to.equal('some_dc');
done();
});
});
const _createNewClient = function(hosts, keyspace) {
const loadBalancingPolicy = new cassandra.policies.loadBalancing.RoundRobinPolicy();
const reconnectionPolicy = new cassandra.policies.reconnection.ConstantReconnectionPolicy(CONFIG.timeout);
return new cassandra.Client({
contactPoints: hosts,
policies: {
timestampGeneration: null,
loadBalancing: loadBalancingPolicy,
reconnection: reconnectionPolicy
},
keyspace,
protocolOptions: { maxVersion: 3 },
socketOptions: {
connectTimeout: CONFIG.timeout,
readTimeout: CONFIG.timeout
},
consistency: cassandra.types.consistencies.quorum
});
const _createNewClient = function(hosts, keyspace) {
const loadBalancingPolicy = new cassandra.policies.loadBalancing.RoundRobinPolicy();
const reconnectionPolicy = new cassandra.policies.reconnection.ConstantReconnectionPolicy(
CONFIG.timeout
);
return new cassandra.Client({
contactPoints: hosts,
policies: {
timestampGeneration: null,
loadBalancing: loadBalancingPolicy,
reconnection: reconnectionPolicy
},
keyspace,
protocolOptions: { maxVersion: 3 },
socketOptions: {
connectTimeout: CONFIG.timeout
},
consistency: cassandra.types.consistencies.quorum
};
if (typeof this.config.consistency === 'string') {
const level = this.config.consistency;
if (typeof this.consistencyLevel[level] !== 'undefined') {
this.poolConfig.consistencyLevel = this.consistencyLevel[level];
} else {
throw new Error(`Error: "${level}" is not a valid consistency level`);
}
}
if (typeof this.config.localDataCenter === 'string') {
this.poolConfig.policies = this.poolConfig.policies || {};
if (!this.poolConfig.policies.loadBalancing) {
this.poolConfig.policies.loadBalancing = new cqlDriver.policies.loadBalancing.TokenAwarePolicy(
new cqlDriver.policies.loadBalancing.DCAwareRoundRobinPolicy(this.config.localDataCenter)
);
}
}
}
const _createNewClient = function(hosts, keyspace) {
const loadBalancingPolicy = new cassandra.policies.loadBalancing.RoundRobinPolicy();
const reconnectionPolicy = new cassandra.policies.reconnection.ConstantReconnectionPolicy(CONFIG.timeout);
return new cassandra.Client({
contactPoints: hosts,
policies: {
timestampGeneration: null,
loadBalancing: loadBalancingPolicy,
reconnection: reconnectionPolicy
},
keyspace,
protocolOptions: { maxVersion: 3 },
socketOptions: {
connectTimeout: CONFIG.timeout,
readTimeout: CONFIG.timeout
},
consistency: cassandra.types.consistencies.quorum
});
};
if (typeof this.config.consistency === 'string') {
const level = this.config.consistency;
if (typeof this.consistencyLevel[level] !== 'undefined') {
this.poolConfig.consistencyLevel = this.consistencyLevel[level];
} else {
throw new Error(`Error: "${level}" is not a valid consistency level`);
}
}
if (typeof this.config.localDataCenter === 'string') {
this.poolConfig.policies = this.poolConfig.policies || {};
if (!this.poolConfig.policies.loadBalancing) {
this.poolConfig.policies.loadBalancing = new cqlDriver.policies.loadBalancing.TokenAwarePolicy(
new cqlDriver.policies.loadBalancing.DCAwareRoundRobinPolicy(this.config.localDataCenter)
);
}
}
}