uaa icon indicating copy to clipboard operation
uaa copied to clipboard

org.apache.tomcat.jdbc.pool.PoolExhaustedException when ramping up load on UAA

Open sivabalans opened this issue 11 years ago • 21 comments

We are doing some performance tests against UAA where our app is using UAA for authentication on Cloudfoundry. As we ramp up the threads, the number of requests to UAA increases correspondingly. The UAA is setup to store data in a postgres DB. As we start cranking up the requests to UAA, we start seeing the following exception in the UAA logs:

2015-06-10T11:01:36.84-0700 [App/0]      OUT org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.tomcat.jdbc.pool.PoolExhaustedException: [http-nio-61126-exec-95] Timeout: Pool empty. Unable to fetch a connection in 30 seconds, none available[size:4; busy:4; idle:0; lastwait:30000].
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:630)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:737)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:811)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.cloudfoundry.identity.uaa.zone.JdbcIdentityZoneProvisioning.retrieveBySubdomain(JdbcIdentityZoneProvisioning.java:71)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.cloudfoundry.identity.uaa.zone.JdbcIdentityZoneProvisioning$$FastClassBySpringCGLIB$$926ef4fb.invoke(<generated>)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.cloudfoundry.identity.uaa.zone.JdbcIdentityZoneProvisioning$$EnhancerBySpringCGLIB$$19cab146.retrieveBySubdomain(<generated>)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.cloudfoundry.identity.uaa.zone.IdentityZoneResolvingFilter.doFilterInternal(IdentityZoneResolvingFilter.java:46)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.cloudfoundry.identity.uaa.web.CorsFilter.doFilterInternal(CorsFilter.java:37)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:676)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at java.lang.Thread.run(Thread.java:745)
2015-06-10T11:01:36.84-0700 [App/0]      OUT Caused by: org.apache.tomcat.jdbc.pool.PoolExhaustedException: [http-nio-61126-exec-95] Timeout: Pool empty. Unable to fetch a connection in 30 seconds, none available[size:4; busy:4; idle:0; lastwait:30000].
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:672)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.tomcat.jdbc.pool.ConnectionPool.getConnection(ConnectionPool.java:186)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
2015-06-10T11:01:36.84-0700 [App/0]      OUT    ... 42 more

I am not able to figure out where it gets the pool size of 4 from. We need to be able to adjust this value based on the number of requests UAA will be handling at any given time. Where can this connection pool size value be configured? Any ideas would be appreciated. Thanks.

sivabalans avatar Jun 10 '15 18:06 sivabalans

We have created an issue in Pivotal Tracker to manage this. You can view the current status of your issue at: https://www.pivotaltracker.com/story/show/96692414.

cf-gitbot avatar Jun 10 '15 18:06 cf-gitbot

The defaults are higher than that.

https://github.com/cloudfoundry/uaa/blob/develop/common/src/main/resources/spring/data-source.xml#L30-L31

Have you defined these properties elsewhere?

fhanik avatar Jun 10 '15 18:06 fhanik

That was my understanding as well. I even tried hardcoding the values in the data-source.xml but it still does not take anything more than 4. I also verified it with MySQL DB and I am hitting the same limit with 4 connections in the pool. To my knowledge, there is no other place where we explicitly or implicitly modify the connection pool value.

sivabalans avatar Jun 10 '15 18:06 sivabalans

I will investigate this! Thanks for the report

fhanik avatar Jun 10 '15 18:06 fhanik

I changed maxactive to 25 and maxidle to 25, in a very simple test, I can get the idle number of connections to go up to 11.

image

Can you launch jconsole, and you should be able to find max active under spring.application

image

fhanik avatar Jun 10 '15 19:06 fhanik

Thanks for checking this. My UAA is deployed on Cloudfoundry and there is no way that I know of to attach a Jconsole to it. Any other way you can think of that I can verify this on my end?

sivabalans avatar Jun 10 '15 20:06 sivabalans

How is it deployed, as an app, so is it the UAA job as part of cf-release?

fhanik avatar Jun 10 '15 22:06 fhanik

Yes, it is deployed as an app. I dont understand what you mean by "is it the UAA job as part of cf-release?" This UAA app is used for authentication(oauth) by downstream applications(also deployed on CF) and also by our custom authorization module(another app in CF).

sivabalans avatar Jun 10 '15 23:06 sivabalans

Ok, then I would examine

  1. Check the version of the UAA you are using
  2. extract the WAR file - take a look at uaa.yml and make sure DB properties are not set there
  3. do a 'cf env ' and see if any properties are set

Somewhere, it may be that the DB pool size is being restricted. We just have to found where.

fhanik avatar Jun 11 '15 08:06 fhanik

  1. The version of UAA is the latest one that is currently on github.
  2. Confirmed that uaa.yml file in the WAR file does not have any DB properties set.
  3. Below is the output of 'cf env':
OK

System-Provided:
{
 "VCAP_SERVICES": {
  "logstash14": [
   {
    "credentials": {
     "hostname": "10.xxx.xx.xx",
     "ports": {
      "514/tcp": "49282",
      "9200/tcp": "49283",
      "9300/tcp": "49284"
     }
    },
    "label": "logstash14",
    "name": "perf-uaa-capacity-logstash14",
    "plan": "free",
    "syslog_drain_url": "http://10.xxx.xxx.xxx:49282",
    "tags": [
     "logstash14",
     "logstash",
     "syslog"
    ]
   }
  ],
  "newrelic-predix": [
   {
    "credentials": {
     "licenseKey": "xxxxxxxxxxxxxxxxxxx"
    },
    "label": "newrelic-predix",
    "name": "perf-uaa-capacity-newrelic",
    "plan": "standard",
    "tags": [
     "newrelic",
     "management",
     "monitoring",
     "apm"
    ]
   }
  ],
  "postgresql93": [
   {
    "credentials": {
     "dbname": "jcgqlwvd82pc2tf0",
     "hostname": "xx.xx.xx.xx",
     "password": "xxxxxxxxxxxxx",
     "port": "33255",
     "ports": {
      "5432/tcp": "33255"
     },
     "uri": "postgres://xxxxxxx",
     "username": "xxxxxxx"
    },
    "label": "postgresql93",
    "name": "perf-uaa-capacity-db",
    "plan": "free",
    "tags": [
     "postgresql93",
     "postgresql",
     "relational"
    ]
   }
  ]
 }
}

{
 "VCAP_APPLICATION": {
  "application_name": "perf-uaa",
  "application_uris": [
   "perf-uaa.grc-apps.abc.com"
  ],
  "application_version": "393cb592-245d-4a1b-8832-569da6ab1ad7",
  "limits": {
   "disk": 1024,
   "fds": 16384,
   "mem": 1024
  },
  "name": "perf-uaa",
  "space_id": "1f203c4d-2719-4e12-97ab-5508fde6fc31",
  "space_name": "security",
  "uris": [
   "perf-uaa.grc-apps.abc.com"
  ],
  "users": null,
  "version": "393cb592-245d-4a1b-8832-569da6ab1ad7"
 }
}

User-Provided:
SPRING_PROFILES_ACTIVE: postgresql
UAA_DB: perf-uaa-capacity-db

No running env variables have been set

No staging env variables have been set

When I checked the cf logs while deploying UAA to CF, I found the following:

2015-06-11T10:14:58.15-0700 [App/0]      OUT [CONTAINER] ing.DataSourceCloudServiceBeanFactoryPostProcessor INFO    Auto-reconfiguring beans of type javax.sql.DataSource
2015-06-11T10:14:58.19-0700 [App/0]      OUT [CONTAINER] n.org.springframework.cloud.AbstractCloudConnector WARNING No suitable service info creator found for service perf-uaa-capacity-logstash14 Did you forget to add a ServiceInfoCreator?
2015-06-11T10:14:58.20-0700 [App/0]      OUT [CONTAINER] n.org.springframework.cloud.AbstractCloudConnector WARNING No suitable service info creator found for service perf-uaa-capacity-logstash14 Did you forget to add a ServiceInfoCreator?
2015-06-11T10:14:58.24-0700 [App/0]      OUT [CONTAINER] k.cloud.service.relational.PooledDataSourceCreator INFO    Found Tomcat high-performance connection pool on the classpath. Using it for DataSource connection pooling.
2015-06-11T10:14:58.32-0700 [App/0]      OUT [CONTAINER] ing.DataSourceCloudServiceBeanFactoryPostProcessor INFO    Reconfigured bean dataSource into singleton service connector org.apache.tomcat.jdbc.pool.DataSource@6f04fe92{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=org.postgresql.Driver; maxActive=4; maxIdle=100; minIdle=0; initialSize=0; maxWait=30000; testOnBorrow=true; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=false; password=********; url=jdbc:postgresql://xx.xx.xx.xx:33255/jcgqlwvd82pc2tf0?user=erm3ldjddl3e80zo&password=xxxxxxx; username=null; validationQuery=SELECT 1; validationQueryTimeout=-1; validatorClassName=null; validationInterval=30000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=null; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; }

A little after the above snippet, I see the following, which makes sense:

OUT [CONTAINER] org.apache.tomcat.jdbc.pool.ConnectionPool         WARNING maxIdle is larger than maxActive, setting maxIdle to: 4

What baffles me is, what in the UAA code is setting the maxActive explicitly to 4 and maxIdle to 100? I could not find anywhere in the code or property files where this value is being set to 4. Any ideas where this could come from?

sivabalans avatar Jun 11 '15 18:06 sivabalans

Can it be the pool size limit for the 'free' postgres plan?

sparameswaran avatar Jun 11 '15 20:06 sparameswaran

possibly-look at this statement - it's not a UAA log file?

DataSourceCloudServiceBeanFactoryPostProcessor INFO Reconfigured bean dataSource into singleton service connector org.apache.tomcat.jdbc.pool.DataSource@6f04fe92{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=org.postgresql.Driver; maxActive=4; maxIdle=100; minIdle=0; initialSize=0; maxWait=30000; testOnBorrow=true; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false;

fhanik avatar Jun 11 '15 21:06 fhanik

Yes, that is the statement I was referring to in my comment earlier. That was from the UAA logs when deploying UAA to CF. I am not sure where it is getting those values in that log statement from. Also, I am currently checking to see if the free postgres plan is limiting the connection pool in any way. Will keep you posted.

sivabalans avatar Jun 11 '15 21:06 sivabalans

Do you think the below has anything to do with this issue? https://github.com/spring-cloud/spring-cloud-cloudfoundry/issues/4 But the above comes into picture only when we use spring boot and UAA does not use spring boot. Correct?

sivabalans avatar Jun 12 '15 21:06 sivabalans

@fhanik Hi Filip, any progress or new ideas on this? I think one key element of this problem is that it only happens when we push UAA to Cloud Foundry. It doesn't seem like we can reproduce this issue locally. We could modify UAA to skip Spring Cloud auto-reconfiguration as documented here: http://spring.io/blog/2011/11/04/using-cloud-foundry-services-with-spring-part-2-auto-reconfiguration . However, it's unclear to us whether the UAA community would accept a pull request for a fix along those lines. We would very much appreciate some direction in how to fix this.

DarioAmiri avatar Jun 16 '15 15:06 DarioAmiri

@fhanik I have an update to this issue. Depending on what grant_type I use to get the oauth token from UAA, I am getting the above mentioned connection pool exception. If I use client_credentials as grant_type, I don't see the JDBC exception. But if I use "password" grant_type I run into the connection pool exception. But in both cases, the maxActive is still set to 4 by the auto-reconfiguration.

sivabalans avatar Jun 19 '15 17:06 sivabalans

hi, I wasn't aware you expected an update from me. there is nothing I can do in the UAA to make this work from you. It appears as the plan you are on, will limit your connections...and is out of my hands.

we can investigate if some calls are inefficiently using connections, but I don't think there are any connection leaks at all.

Filip

On Fri, Jun 19, 2015 at 7:58 PM, sivabalans [email protected] wrote:

@fhanik https://github.com/fhanik I have an update to this issue. Depending on what grant_type I use to get the oauth token from UAA, I am getting the above mentioned connection pool exception. If I use client_credentials as grant_type, I don't see the JDBC exception. But if I use "password" grant_type I run into the connection pool exception. But in both cases, the maxActive is still set to 4 by the auto-reconfiguration.

— Reply to this email directly or view it on GitHub https://github.com/cloudfoundry/uaa/issues/165#issuecomment-113590953.

cf-identity avatar Jun 22 '15 13:06 cf-identity

This is the culprit: https://github.com/spring-cloud/spring-cloud-connectors/blob/master/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/relational/DbcpLikePooledDataSourceCreator.java#L40

This line hard codes a pool size of 4 and a timeout of 30 seconds, which matches the log output that @fhanik dug up. The issue has nothing to do with UAA and so a fix shouldn't be made against it. I'll file an issue in that tracker, in the mean time I'd try disabling auto-reconfiguration.

william-tran avatar Jul 09 '15 15:07 william-tran

@sivabalans this may help as well: https://spring.io/blog/2015/04/27/binding-to-data-services-with-spring-boot-in-cloud-foundry

william-tran avatar Jul 09 '15 15:07 william-tran

This line hard codes a pool size of 4 and a timeout of 30 seconds

The line @willtran- references sets defaults for the pool size and timeout. These defaults can be overridden with manual configuration of the DataSource provided by Spring Cloud Connectors (i.e. in Java config as shown in the blog post linked above), but there isn't a way to override the defaults with auto-reconfiguration.

scottfrederick avatar Jul 09 '15 16:07 scottfrederick