datastax / cassandra-quarkus

An Apache Cassandra(R) extension for Quarkus
Apache License 2.0
39 stars 28 forks source link

Connecting to Aws keyspaces #141

Closed Spyna closed 3 years ago

Spyna commented 3 years ago

Hi, I just started using Quarkus, and I'm figuring out how to connect to Aws keyspaces (Cassandra) using cassandra-quarkus-client.

I created a new project using https://code.quarkus.io/ and these extensions: image

The dependency for cassandra:

 <dependency>
      <groupId>com.datastax.oss.quarkus</groupId>
      <artifactId>cassandra-quarkus-client</artifactId>
    </dependency>

With this configuration:

quarkus.cassandra.contact-points=cassandra.eu-central-1.amazonaws.com:9142
quarkus.cassandra.auth.username=<my username>
quarkus.cassandra.auth.password=<my password>

It prints this error

2020-11-10 09:24:32,669 DEBUG [com.dat.oss.dri.int.cor.met.MetadataManager] (vert.x-eventloop-thread-0) [s0] Adding initial contact points [Node(endPoint=cassandra.eu-central-1.amazonaws.com:9142, hostId=null, hashCode=cb26a18)]
2020-11-10 09:24:32,671 DEBUG [com.dat.oss.dri.int.cor.con.ControlConnection] (vert.x-eventloop-thread-1) [s0] Initializing with event types [SCHEMA_CHANGE, STATUS_CHANGE, TOPOLOGY_CHANGE]
2020-11-10 09:24:32,673 DEBUG [com.dat.oss.dri.int.cor.con.EventBus] (vert.x-eventloop-thread-1) [s0] Registering com.datastax.oss.driver.internal.core.metadata.LoadBalancingPolicyWrapper$$Lambda$614/0x00000001004f0440@6923a8f0 for class com.datastax.oss.driver.internal.core.metadata.NodeStateEvent
2020-11-10 09:24:32,675 DEBUG [com.dat.oss.dri.int.cor.con.ControlConnection] (vert.x-eventloop-thread-1) [s0] Trying to establish a connection to Node(endPoint=cassandra.eu-central-1.amazonaws.com:9142, hostId=null, hashCode=cb26a18)
2020-11-10 09:24:32,684 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.numHeapArenas: 8
2020-11-10 09:24:32,684 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.numDirectArenas: 8
2020-11-10 09:24:32,684 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.pageSize: 8192
2020-11-10 09:24:32,684 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.maxOrder: 1
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.chunkSize: 16384
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.tinyCacheSize: 512
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.smallCacheSize: 256
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.normalCacheSize: 64
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.maxCachedBufferCapacity: 32768
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.cacheTrimInterval: 8192
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.cacheTrimIntervalMillis: 0
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.useCacheForAllThreads: true
2020-11-10 09:24:32,685 DEBUG [io.net.buf.PooledByteBufAllocator] (vert.x-eventloop-thread-1) -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
2020-11-10 09:24:32,688 DEBUG [io.net.buf.ByteBufUtil] (vert.x-eventloop-thread-1) -Dio.netty.allocator.type: pooled
2020-11-10 09:24:32,688 DEBUG [io.net.buf.ByteBufUtil] (vert.x-eventloop-thread-1) -Dio.netty.threadLocalDirectBufferSize: 0
2020-11-10 09:24:32,689 DEBUG [io.net.buf.ByteBufUtil] (vert.x-eventloop-thread-1) -Dio.netty.maxThreadLocalCharBufferSize: 16384
2020-11-10 09:24:32,812 DEBUG [com.dat.oss.dri.int.cor.cha.ProtocolInitHandler] (vert.x-eventloop-thread-2) [s0|control|connecting...] Starting channel initialization
2020-11-10 09:24:32,826 DEBUG [io.net.uti.Recycler] (vert.x-eventloop-thread-2) -Dio.netty.recycler.maxCapacityPerThread: 4096
2020-11-10 09:24:32,826 DEBUG [io.net.uti.Recycler] (vert.x-eventloop-thread-2) -Dio.netty.recycler.maxSharedCapacityFactor: 2
2020-11-10 09:24:32,826 DEBUG [io.net.uti.Recycler] (vert.x-eventloop-thread-2) -Dio.netty.recycler.linkCapacity: 16
2020-11-10 09:24:32,826 DEBUG [io.net.uti.Recycler] (vert.x-eventloop-thread-2) -Dio.netty.recycler.ratio: 8
2020-11-10 09:24:32,839 DEBUG [io.net.buf.AbstractByteBuf] (vert.x-eventloop-thread-2) -Dio.netty.buffer.checkAccessible: true
2020-11-10 09:24:32,839 DEBUG [io.net.buf.AbstractByteBuf] (vert.x-eventloop-thread-2) -Dio.netty.buffer.checkBounds: true
2020-11-10 09:24:32,839 DEBUG [io.net.uti.ResourceLeakDetectorFactory] (vert.x-eventloop-thread-2) Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@4590742e
2020-11-10 09:24:33,363 WARN  [com.dat.oss.dri.int.cor.con.ControlConnection] (vert.x-eventloop-thread-1) [s0] Error connecting to Node(endPoint=cassandra.eu-central-1.amazonaws.com:9142, hostId=null, hashCode=cb26a18), trying next node: com.datastax.oss.driver.api.core.DriverTimeoutException: [s0|control|id: 0x583484e8, L:/192.168.1.9:38968 - R:cassandra.eu-central-1.amazonaws.com/3.127.48.181:9142] Protocol initialization request, step 1 (OPTIONS): timed out after 500 ms
    at com.datastax.oss.driver.internal.core.channel.ChannelHandlerRequest.onTimeout(ChannelHandlerRequest.java:108)
    at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98)
    at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:170)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)

2020-11-10 09:24:33,374 DEBUG [com.dat.oss.dri.int.cor.con.EventBus] (vert.x-eventloop-thread-1) [s0] Firing an instance of class com.datastax.oss.driver.internal.core.channel.ChannelEvent: ChannelEvent(CONTROL_CONNECTION_FAILED, Node(endPoint=cassandra.eu-central-1.amazonaws.com:9142, hostId=null, hashCode=cb26a18))
2020-11-10 09:24:33,377 DEBUG [com.dat.oss.dri.int.cor.con.EventBus] (vert.x-eventloop-thread-1) [s0] Notifying com.datastax.oss.driver.internal.core.util.concurrent.RunOrSchedule$$Lambda$535/0x000000010041bc40@5fc6399b of ChannelEvent(CONTROL_CONNECTION_FAILED, Node(endPoint=cassandra.eu-central-1.amazonaws.com:9142, hostId=null, hashCode=cb26a18))
2020-11-10 09:24:33,379 DEBUG [com.dat.oss.dri.int.cor.met.NodeStateManager] (vert.x-eventloop-thread-3) [s0] Processing ChannelEvent(CONTROL_CONNECTION_FAILED, Node(endPoint=cassandra.eu-central-1.amazonaws.com:9142, hostId=null, hashCode=cb26a18))
2020-11-10 09:24:33,381 DEBUG [com.dat.oss.dri.int.cor.met.NodeStateManager] (vert.x-eventloop-thread-3) [s0] Transitioning Node(endPoint=cassandra.eu-central-1.amazonaws.com:9142, hostId=null, hashCode=cb26a18) UNKNOWN=>DOWN (because it was tried as a contact point but failed)

┆Issue is synchronized with this Jira Task by Unito

adutra commented 3 years ago

Hi, the driver default timeouts are a bit too aggressive for cloud providers. I suggest that you do the following:

  1. Create an application.conf file in your src/main/resources directory.
  2. Insert the following content in that file:
datastax-java-driver {
  advanced {
    connection {
      connect-timeout = 10 seconds
      init-query-timeout = 10 seconds
      set-keyspace-timeout = 10 seconds
    }
    heartbeat.timeout = 10 seconds
    control-connection.timeout = 10 seconds
    control-connection.schema-agreement.timeout = 10 seconds
    metadata.schema.request-timeout = 10 seconds
}

Of course you can adjust the values above to your needs. FYI the timeout that is firing in your example is init-query-timeout.

You can also use this file to customize the driver in many other ways, see this page for details.

adutra commented 3 years ago

Alternatively, you can switch to a more recent version of the driver: starting with 4.8, the default timeouts were raised.

To include a different version of the driver, for example 4.9, include the following in your pom file:

<dependency>
  <groupId>com.datastax.oss</groupId>
  <artifactId>java-driver-core</artifactId>
  <version>4.9.0</version>
</dependency>

If you are using other driver artifacts, e.g. java-driver-query-builder or java-driver-mapper-runtime you must declare them as well with the overriding version (4.9.0 in my example).

Spyna commented 3 years ago

Thank you, the first solution worked.

The second didn't because com.datastax.oss.quarkus:cassandra-quarkus-client uses some mothods that are available only in the version 4.7.2 of com.datastax.oss:java-driver-core

Spyna commented 3 years ago

After reading this guide from AWS at this link: https://docs.aws.amazon.com/keyspaces/latest/devguide/using_java_driver.html I came up with the right configuration:

datastax-java-driver {

  basic.contact-points = [ "cassandra.eu-central-1.amazonaws.com:9142"]

  advanced.auth-provider{
        class = PlainTextAuthProvider
        username = "<my username>"
        password = "<my password>"
  }
  basic.load-balancing-policy {
    local-datacenter = "eu-central-1"
  }
  advanced.ssl-engine-factory {
    class = DefaultSslEngineFactory
    truststore-path = "<path to the keystore>"
    truststore-password = "my_password"
   }

  advanced {
    connection {
      connect-timeout = 10 seconds
      init-query-timeout = 10 seconds
      set-keyspace-timeout = 10 seconds
    }
    heartbeat.timeout = 10 seconds
    control-connection.timeout = 10 seconds
    control-connection.schema-agreement.timeout = 10 seconds
    metadata.schema.request-timeout = 10 seconds
  }
}

Note:

Instead of adding the path to the trustStore in the configuration file, you can also add the trustStore path directly in the application code or you can add the path to the trustStore to your JVM arguments.