-3

added config file to take contact-point programatically

         @Bean(destroyMethod = "close")
    public CqlSession session() {
        CqlSession session = CqlSession.builder() 
            .addContactPoint(InetSocketAddress.createUnresolved("[240b:c0e0:1xx:xxx8:xxxx:x:x:x]", port))
                .withConfigLoader(
                        DriverConfigLoader.programmaticBuilder()
                                .withString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, localDatacenter)                              .withString(DefaultDriverOption.AUTH_PROVIDER_USER_NAME,username)
                                .withString(DefaultDriverOption.AUTH_PROVIDER_PASSWORD,password)
                                .withString(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT,"10s")
                                .withString(DefaultDriverOption.CONNECTION_CONNECT_TIMEOUT, "20s")
                                .withString(DefaultDriverOption.REQUEST_TIMEOUT, "20s")
                                .withString(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT, "20s")
                                .withString(DefaultDriverOption.SESSION_KEYSPACE,keyspace)
                                .build())
                //.addContactPoint(InetSocketAddress.createUnresolved(InetAddress.getByName(contactPoints).getHostName(), port))
                .build();
        }
        return session;

`Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception with message: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=/127.0.0.1:9042, hostId=0323221f-9a0f-ec92-ea4a-c1472c2a8b94, hashCode=39075b16)=datacenter1. Current DCs in this cluster are: datacenter1 at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171) ~[spring-beans-6.0.2.jar:6.0.2] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:648) ~[spring-beans-6.0.2.jar:6.0.2] ... 89 common frames omitted Caused by: java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=/127.0.0.1:9042, hostId=0323221f-9a0f-ec92-ea4a-c1472c2a8b94, hashCode=39075b16)=datacenter1. Current DCs in this cluster are: datacenter1 at com.datastax.oss.driver.internal.core.loadbalancing.helper.MandatoryLocalDcHelper.discoverLocalDc(MandatoryLocalDcHelper.java:91) ~[java-driver-core-4.11.4-yb-1-RC1.jar:na] at com.datastax.oss.driver.internal.core.loadbalancing.DefaultLoadBalancingPolicy.discoverLocalDc(DefaultLoadBalancingPolicy.java:119) ~[java-driver-core-4.11.4-yb-1-RC1.jar:na] at

this is the application.yml file

 spring:
  data:
    cassandra:
      keyspace-name: xxx
      contact-points: [xxxx:xxxx:xxxx:xxx:xxx:xxx]
      port: xxx
      local-datacenter: xxxx
      use-dc-aware: true
      username: xxxxx
      password: xxxxx
      ssl: true
      SchemaAction: CREATE_IF_NOT_EXISTS

but still the application is pointing towards the localhost , even though i've explicitly mentioned the contact-points and localDC

logs of stg evn are :

caused by: com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node (endPoint=/[240b:cOe0:102:xxxx:xxxx:x:x:x]:3xxx,hostId-null,hashCode=4e9ba6a8):[com.datastax.oss.driver.api.core.connection.ConnectionInitException:[s0|controllid:0x984419ed,L:/[240b:cOe0:102:5dd7: xxxx:x:x:xxx]:4xxx - R:/[240b:c0e0:102:xxxx:xxxx:x:x:x]:3xxx] Protocol initialization request, step 1 (OPTIONS: unexpected tarlure com.datastax.oss.driver.apt.core.connection.closedconnectiontxception: Lost connection to remote peer)]

1 Answers1

1

thanks for the question!

I'll try to provide some pointers that might help you identify the problem but it should be noted that you appear to have some non-standard elements to your application. Specifically I note that "java-driver-core-4.11.4-yb-1-RC1.jar" isn't a Java driver artifact released by DataStax (there isn't even a 4.11.4 Java driver release). This could be relevant for reasons we'll get into in a moment. I also don't recognize the configuration file you cite above. Could you provide some more detail on how your app is configured? At first blush it looked as though you might be using spring-data-cassandra but there wasn't any mention of it in your stack trace... so perhaps you're using some kind of custom configuration code?

As to your specific question: my guess is that you might have a Java driver configuration file in your staging environment which is providing a default value for "datastax-java-driver.basic.contact-points". The 4.x Java driver is configured via the Lightbend Config library. Most relevant to our case it searches for a set of configuration files with various default names on the classpath; these files are then merged together to generate the config passed to the driver. So if you have an application.conf in staging which specifies some contact points and is in the classpath the code you cite above would run fine in your local environment but fail in staging.

To validate this, create an application.conf file in your local environment in src/main/resources (or somewhere else that's explicitly included in the classpath) and give it the following contents:

datastax-java-driver {
  basic {
    contact-points: ["127.0.0.1:9042"]
  }
}

If you then re-run the app in your local environment you should see the error there as well.

Note that the core Java driver JAR already includes a reference.conf file which serves as a default configuration. Here's where the part about a custom JAR figures in; because your not using a standard DataStax Java driver JAR I don't know if you're using the standard reference.conf file defined within that JAR. It's possible that the contact points are defined in that file, although if that were the case I'd expect you to already be seeing the error in any environment where you use that JAR.

One final note: the Java driver should be fine with IPv6 addresses. The issue described above isn't related to IPv6; it's entirely a function of how you're using the Java driver's configuration mechanism.

Hopefully some of the above is helpful!

absurdfarce
  • 181
  • 2
  • 1
    but the problem is , locally it was working fine , but in my stg env it's not working as in that env the cassandra has an ipv6 address as contact-points – Vaishnav Puram Jan 31 '23 at 13:03
  • pls review again as i've updated the issue with more details – Vaishnav Puram Jan 31 '23 at 13:50
  • Hey Vaishnav, based on the information you provided it looks like your application is connecting to your cluster correctly but then sees its network connection dropped unexpectedly. This seems to indicate that the IPv6 addressing isn't the source of your problem; something else appears to be getting in the way. I'd suggest enabling debug logging for your application; we might be able to more clearly identify the issue that way. You should also check the system logs of the node you're connecting to; it's possible an exception is being reported there. – absurdfarce Jan 31 '23 at 20:38
  • hey i've added the logs which were appearing in my stg env pls review it – Vaishnav Puram Feb 01 '23 at 07:58
  • added a new question pls review it https://stackoverflow.com/questions/75307194/not-able-to-connect-to-cassandra-from-my-spring-boot-application-throwing-exce – Vaishnav Puram Feb 01 '23 at 08:13