gravitee-io / issues

Gravitee.io - API Platform - Issues
64 stars 26 forks source link

[gravitee-repository-mongodb] Failed to start component with mongo+srv #2760

Closed snahelou closed 4 years ago

snahelou commented 5 years ago

Hello

Since 1.25.12, when we use mongo backend, all components (gw, mgmt api) which use URI like mongodb+srv://user:password@xxxxx.xx.mongodb.net/gravitee?retryWrites=true fail to start.

( ie: using seed list: https://www.mongodb.com/blog/post/mongodb-3-6-here-to-SRV-you-with-easier-replica-set-connections)

Expected Behavior

In 1.25.11:

09:38:41.136 [graviteeio-node] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[xxxxx.xx.mongodb.net:27017, xxxxx.xx.mongodb.net:27017, xxxxx.xx.mongodb.net:27017], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='1000 ms', maxWaitQueueSize=500, requiredReplicaSetName='xxxxx-shard-0', description='gravitee.io'}
09:38:41.136 [graviteeio-node] INFO  org.mongodb.driver.cluster - Adding discovered server xxxxx.00.mongodb.net:27017 to client view of cluster
09:38:41.316 [graviteeio-node] INFO  org.mongodb.driver.cluster - Adding discovered server xxxxx.01.mongodb.net:27017 to client view of cluster
09:38:41.320 [graviteeio-node] INFO  org.mongodb.driver.cluster - Adding discovered server xxxxx.02.mongodb.net:27017 to client view of cluster

Current Behavior

In 1.25.12 / 1.25.13 / 1.29.4 :

12:11:44.732 [graviteeio-node] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[xxxxx.xx.mongodb.net:27017], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='1000 ms', maxWaitQueueSize=500, requiredReplicaSetName='xxxx-0', description='gravitee.io'}
12:11:44.732 [graviteeio-node] INFO  org.mongodb.driver.cluster - Adding discovered server xxxxx.xx.mongodb.net:27017 to client view of cluster
12:11:44.735 [cluster-ClusterId{value='5dada080c5fa043d62fa7a26', description='gravitee.io'}-xxxxx.xx.mongodb.net:27017] INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server xxxxx.xx.mongodb.net:27017
com.mongodb.MongoSocketException: xxxxx.xx.mongodb.net
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.UnknownHostException: xxxxx.xx.mongodb.net
    at java.net.InetAddress.getAllByName0(InetAddress.java:1280)
    at java.net.InetAddress.getAllByName(InetAddress.java:1192)
    at java.net.InetAddress.getAllByName(InetAddress.java:1126)
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
    ... 5 common frames omitted

Possible Solution

I'm not sure but it seems that this issue was introduced with the commit https://github.com/gravitee-io/gravitee-repository-mongodb/commit/87ff9e1735d2ce31f46e461f5be71c85d48afa7c (between 1.25.11 and 1.25.12 )

Steps to Reproduce (for bugs)

  1. Start a mgmt api with
    gravitee_management_mongodb_uri=mongodb+srv://user:password@xxxxx.xx.mongodb.net/gravitee?retryWrites=true

Context

MongoDB database hosted on mongoAtlas.

Your Environment

Feel free if you have any question.

NicolasGeraud commented 5 years ago

hi @snahelou , I've tried with 1.25.12 and everything works fine. my uri is mongodb+srv://xxx:xxx@xxx.xxx.mongodb.net/gravitee?retryWrites=true&w=majority

snahelou commented 5 years ago

It's strange, everything works find with previous version.

In addition my DNS server works well

/opt/graviteeio-management-api $ dig SRV _mongodb._tcp.xx.xx.mongodb.net

; <<>> DiG 9.10.2 <<>> SRV _mongodb._tcp.xx.xx.mongodb.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2935
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;_mongodb._tcp.xx.xx.mongodb.net. IN    SRV

;; ANSWER SECTION:
_mongodb._tcp.xx.xx.mongodb.net. 59 IN SRV 0 0 27017 xx-shard-00-00-xx.xx.mongodb.net.
_mongodb._tcp.xx.xx.mongodb.net. 59 IN SRV 0 0 27017 xx-shard-00-01-xx.xx.mongodb.net.
_mongodb._tcp.xx.xx.mongodb.net. 59 IN SRV 0 0 27017 xx-shard-00-02-xx.xx.mongodb.net.

@NicolasGeraud Did you tried on a cluster using multiple replicas?

I keep you in touch when I find something...

BR

EDIT:

Just tried at home:

docker run --env GRAVITEE_MANAGEMENT_MONGODB_URI="mongodb+srv://xx:xx@xx.xx.mongodb.net/gravitee?retryWrites=true&w=majority" graviteeio/management-api:latest

=========================================================================

  Gravitee.IO Management Standalone Runtime Bootstrap Environment

  GRAVITEE_HOME: /opt/graviteeio-management-api

  GRAVITEE_OPTS:

  JAVA: /usr/lib/jvm/java-1.8-openjdk/jre/bin/java

  JAVA_OPTS:  -Djava.net.preferIPv4Stack=true -Xms1024m -Xmx2048m -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8

  CLASSPATH: /opt/graviteeio-management-api/lib/gravitee-management-api-standalone-bootstrap-1.29.5.jar

=========================================================================
16:58:08.241 [graviteeio-node] INFO  i.g.m.r.p.RepositoryPluginHandler - Register a new repository: repository-mongodb [io.gravitee.repository.mongodb.MongoRepository]
16:58:08.242 [graviteeio-node] INFO  i.g.m.r.p.RepositoryPluginHandler - Repository [MANAGEMENT] loaded by mongodb
.... 
16:58:12.824 [graviteeio-node] WARN  o.s.c.a.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoCommandRepository': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 1000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@27b490de. Client view of cluster state is {type=REPLICA_SET, servers=[{address=xx.xx.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: xx.xx.mongodb.net}, caused by {java.net.UnknownHostException: xx.xx.mongodb.net}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 1000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@27b490de. Client view of cluster state is {type=REPLICA_SET, servers=[{address=xx.xx.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: xx.xx.mongodb.net}, caused by {java.net.UnknownHostException: xx.xx.mongodb.net}}]
16:58:12.832 [graviteeio-node] ERROR i.g.p.c.i.PluginContextFactoryImpl - Unable to refresh plugin context

It works fine with

docker run --env GRAVITEE_MANAGEMENT_MONGODB_URI="mongodb+srv://xx:xx@xx.xx.mongodb.net/gravitee?retryWrites=true&w=majority" graviteeio/management-api:1.25.11
snahelou commented 5 years ago

Of course, a workaround is to start image (all version) with URI like:

GRAVITEE_MANAGEMENT_MONGODB_URI="mongodb://xx:xx@xx-shard-00-00-xx.xx.mongodb.net:27017,xx-shard-00-01-xx.xx.mongodb.net:27017,xx-shard-00-02-xx.xx.mongodb.net:27017/gravitee?authSource=admin&replicaSet=xx-shard-0&ssl=true&retryWrites=true&w=majority"

( URI mongo <= 3.4 )

brasseld commented 4 years ago

It'll be fixed thanks to https://github.com/gravitee-io/issues/issues/3235