0

I'm having one CentOS machine over network. In which I tried to connect Cassandra using NodeJS through express-cassandra node_module. And when I start my application it gives me following error log.

0|app      | 2018-03-07T14:49:21.329 ERROR cassandra 4456 { 
NoHostAvailableError: All host(s) tried for query failed. First host tried, 
127.0.0.1:9160: OperationTimedOutError: The host 127.0.0.1:9160 did not 
reply before timeout 12000 ms. See innerErrors.
0|app      |     at whilstEnded 
(/opt/cisco/sprint5_node/node_modules/cassandra-driver/lib/control-
connection.js:231:25)
0|app      |     at next (/opt/cisco/sprint5_node/node_modules/cassandra-
driver/lib/utils.js:842:14)
0|app      |     at borrowConnectionCallback 
(/opt/cisco/sprint5_node/node_modules/cassandra-driver/lib/control-
connection.js:226:9)
0|app      |     at HostConnectionPool.create.err 
(/opt/cisco/sprint5_node/node_modules/cassandra-driver/lib/host-connection-
pool.js:71:16)
0|app      |     at Object.onceWrapper (events.js:315:30)
0|app      |     at emitOne (events.js:116:13)
0|app      |     at HostConnectionPool.emit (events.js:211:7)
0|app      |     at whilstEnded 
(/opt/cisco/sprint5_node/node_modules/cassandra-driver/lib/host-connection-
pool.js:173:23)
0|app      |     at next (/opt/cisco/sprint5_node/node_modules/cassandra-
driver/lib/utils.js:839:14)
0|app      |     at HostConnectionPool.<anonymous> 
(/opt/cisco/sprint5_node/node_modules/cassandra-driver/lib/utils.js:851:9)
0|app      |     at Object.onceWrapper (events.js:315:30)
0|app      |     at emitOne (events.js:121:20)
0|app      |     at HostConnectionPool.emit (events.js:211:7)
0|app      |     at attemptOpenCallback 
(/opt/cisco/sprint5_node/node_modules/cassandra-driver/lib/host-connection-
pool.js:223:21)
0|app      |     at Connection.errorConnecting 
(/opt/cisco/sprint5_node/node_modules/cassandra-
driver/lib/connection.js:225:3)
0|app      |     at Socket.socketError 
(/opt/cisco/sprint5_node/node_modules/cassandra-
driver/lib/connection.js:146:10)
0|app      |   name: 'NoHostAvailableError',
0|app      |   info: 'Represents an error when a query cannot be performed 
because no host is available or could be reached by the driver.',

I tried to change rpc_address, listen_address with help of other links, but still facing issue.

This is code to bind models

cassandra_model.setDirectory(path.resolve() + '/app' + '/models')
.bind({
    clientOptions: {
        contactPoints: [config.cassandra.ip],
        protocolOptions: { port: config.cassandra.port },
        keyspace: config.cassandra.keyspace,
        queryOptions: { consistency: cassandra_model.consistencies.one },
        socketOptions: { readTimeout: 0 }
    },
    ormOptions: {
        defaultReplicationStrategy: {
            class: 'SimpleStrategy',
            replication_factor: 1
        },
        migration: 'alter',
        createKeyspace: true
    }
}, function onError(err) {
    cassandra_model.timeuuid()
    if (err) {
        logger.error(err);
    }
    else {
        logger.info('Cassandra_model initialized!');
    }
});

above code works fine. Now I want to connect to keyspace without binding of models. so tried following code.

    var ExpressCassandra = require('express-cassandra');
var models = ExpressCassandra.createClient({
    clientOptions: {
        contactPoints: [config.cassandra.ip],
        protocolOptions: { port: 9042 },
        keyspace: config.cassandra.keyspace,
        queryOptions: { consistency: ExpressCassandra.consistencies.one }
    },
    ormOptions: {
        defaultReplicationStrategy: {
            class: 'SimpleStrategy',
            replication_factor: 1
        },
        migration: 'safe',
    }
}, function onError(err) {
    cassandra_model.timeuuid()
    if (err) {
        logger.error(err);
    }
    else {
        logger.info('Cassandra initialized!');
    }
});

but I'm not able to connect to cassandra. Anything I'm doing wrong over here?

netfreak30
  • 306
  • 1
  • 6
  • 19
  • 1. try telnet on the port. 2. check firewall settings iptables -L and see that port 9160 is not blocked. 3. is nodetool and cqlsh working? 4. also some more info/settings on/from your environment might help – Horia Mar 07 '18 at 16:01
  • so, sometimes you can connect to the database and sometimes not? It shows no host available and on the next run it just connect as usual? – MD Ruhul Amin Mar 07 '18 at 16:17
  • if I'm creating database for first time, getting error till database successful creation. after that can connect easily. – netfreak30 Mar 07 '18 at 17:22
  • looks like your operation is timing out, you may have a look at `request_timeout_in_ms` in your cassandra.yml file. – Masum Mar 08 '18 at 11:28
  • You can enable checkout the driver logs for more information: https://github.com/datastax/nodejs-driver#logging – jorgebg Mar 08 '18 at 11:42
  • @Masum, I tried to change request_timeout_in_ms in Cassandra.yml file but it didn't worked so changed the read timeout in nodejs cassandra driver file and made it disable. – netfreak30 Mar 10 '18 at 04:29
  • @DarshanaAhalpara you may use any of the https://docs.datastax.com/en/developer/nodejs-driver/3.4/api/type.ClientOptions/ mentioned in the nodejs-driver docs in express-cassandra clientOptions parameter. – Masum Mar 10 '18 at 06:43
  • also it looks like the setting you want to overwrite is `socketOptions.readTimeout` since it has a 12000ms default timeout – Masum Mar 10 '18 at 06:45
  • so I added socketOptions.readTimeout while binding models, it worked fine. – netfreak30 Mar 12 '18 at 12:02
  • now I want to connect to keyspace only without binding model. – netfreak30 Mar 12 '18 at 12:02

0 Answers0