Search code examples
scalacassandradatastax-java-driver

Datastax Cassandra Driver always attempts to connect to localhost, even though it's not configured to do so


So I have the following Client code:

def getCluster:Session = {
    import collection.JavaConversions._
    val endpoints = config.getStringList("cassandra.server")
    val keyspace = config.getString("cassandra.keyspace")

    val clusterBuilder = Cluster.builder

    endpoints.toTraversable.map { x =>
      clusterBuilder.addContactPoint(x)
    }
    val cluster = clusterBuilder.build
    cluster
      .getConfiguration
      .getProtocolOptions
      .setCompression(ProtocolOptions.Compression.LZ4)
    cluster.connect(keyspace)}

which is shamelessly borrowed from the examples in datastax's driver documentation.

When I attempt to execute code with it, it always tries to connect to localhost, even though it's not configured for that...

In some cases, it will connect (basic reads) but for writes I get the following log message:

 2016-07-07 11:34:31 DEBUG Connection:157 - Connection[/127.0.0.1:9042-10, inFlight=0, closed=false] Error connecting to /127.0.0.1:9042 (Connection refused: /127.0.0.1:9042)
2016-07-07 11:34:31 DEBUG STATES:404 - Defuncting Connection[/127.0.0.1:9042-10, inFlight=0, closed=false] because: [/127.0.0.1] Cannot connect
2016-07-07 11:34:31 DEBUG STATES:108 - [/127.0.0.1:9042] Connection[/127.0.0.1:9042-10, inFlight=0, closed=false] failed, remaining = 0
2016-07-07 11:34:31 DEBUG Connection:629 - Connection[/127.0.0.1:9042-10, inFlight=0, closed=true] closing connection
2016-07-07 11:34:31 DEBUG Cluster:1802 - Aborting onDown because a reconnection is running on DOWN host /127.0.0.1:9042
2016-07-07 11:34:31 DEBUG Cluster:1872 - Failed reconnection to /127.0.0.1:9042 ([/127.0.0.1] Cannot connect), scheduling retry in 512000 milliseconds
2016-07-07 11:34:31 DEBUG STATES:196 - [/127.0.0.1:9042] next reconnection attempt in 512000 ms

I can't figure out where/what I need to configure on the driver side (no local client, it's just the driver) to correct this issue


Solution

  • My guess is that this is caused by configuration of the cassandra.yaml file on your cassandra node(s). The two main settings that would impact this are broadcast_rpc_address and rpc_address, from The cassandra.yaml configuration reference:

    broadcast_rpc_address

    (Default: unset) RPC address to broadcast to drivers and other Cassandra nodes. This cannot be set to 0.0.0.0. If blank, it is set to the value of the rpc_address or rpc_interface. If rpc_address or rpc_interfaceis set to 0.0.0.0, this property must be set.

    rpc_address

    (Default: localhost) The listen address for client connections (Thrift RPC service and native transport).

    If you leave both of these to the defaults, localhost will be the default address cassandra will communicate to connect on.

    After the driver is able to connect to a contact point, it queries the system.local and system.peers table of the contact point to determine which hosts to connect to, the addresses those tables communicate are from rpc_address/broadcast_rpc_address