From 1a592b9e45f95d694fbf649f41304f148e3c40fe Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Mon, 17 Jun 2019 10:51:08 -0400 Subject: Changes related to eviction of connections These are changes related to ASDC controller for evicting database connection pool when ip address changes Included other appllications such as requestDB, CatalogDB, API Handler, BPMN, OpenstackAdapter etc Change-Id: I9a96ea12fb0c10643a204a58d55360bebba326fe Issue-ID: SO-2018 Signed-off-by: Benjamin, Max (mb388a) --- .../adapters/catalogdb/CatalogDBApplication.java | 5 +- .../so/adapters/catalogdb/CatalogDBConfig.java | 83 +++++++++++++++++++++ .../src/main/resources/application.yaml | 13 ++-- .../so/adapters/openstack/CatalogDBConfig.java | 77 ++++++++++++++++++++ .../src/main/resources/application.yaml | 12 ++-- .../application/MSORequestDBApplication.java | 7 +- .../requestsdb/application/RequestDBConfig.java | 22 +++++- .../src/main/resources/application.yaml | 13 ++-- .../java/org/onap/so/asdc/CatalogDBConfig.java | 14 ++-- .../java/org/onap/so/asdc/RequestDBConfig.java | 16 +++-- .../src/main/resources/application.yaml | 28 ++++---- .../so/bpmn/infrastructure/CamundaDBConfig.java | 84 ++++++++++++++++++++++ .../src/main/resources/application.yaml | 11 +-- common/pom.xml | 4 ++ .../org/onap/so/db/connections/DbDnsIpAddress.java | 20 ++++++ .../onap/so/db/connections/ScheduledDnsLookup.java | 78 ++++++++++++++++++++ .../onap/so/db/connections/DbDnsIpAddressTest.java | 19 +++++ .../so/apihandlerinfra/ApiHandlerApplication.java | 7 +- .../configuration/CatalogDBConfig.java | 21 +++++- .../configuration/RequestDBConfig.java | 22 ++++-- .../src/main/resources/application.yaml | 23 +++--- pom.xml | 15 ++-- 22 files changed, 525 insertions(+), 69 deletions(-) create mode 100644 adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/CatalogDBConfig.java create mode 100644 adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/CatalogDBConfig.java create mode 100644 bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/infrastructure/CamundaDBConfig.java create mode 100644 common/src/main/java/org/onap/so/db/connections/DbDnsIpAddress.java create mode 100644 common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java create mode 100644 common/src/test/java/org/onap/so/db/connections/DbDnsIpAddressTest.java diff --git a/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/CatalogDBApplication.java b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/CatalogDBApplication.java index a0a0756d3d..aa19aed515 100644 --- a/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/CatalogDBApplication.java +++ b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/CatalogDBApplication.java @@ -24,12 +24,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication(scanBasePackages = {"org.onap.so.adapters.catalogdb", "org.onap.so.db.catalog.client", "org.onap.so.logging.jaxrs.filter", "org.onap.so.logging.spring.interceptor", "org.onap.so.client", - "org.onap.so.configuration"}) + "org.onap.so.configuration", "org.onap.so.db"}) @EnableJpaRepositories("org.onap.so.db.catalog.data.repository") @EntityScan("org.onap.so.db.catalog.beans") +@EnableScheduling public class CatalogDBApplication { private static final String LOGS_DIR = "logs_dir"; @@ -42,6 +44,7 @@ public class CatalogDBApplication { public static void main(String[] args) { SpringApplication.run(CatalogDBApplication.class, args); + java.security.Security.setProperty("networkaddress.cache.ttl", "10"); System.getProperties().setProperty("mso.db", "MARIADB"); System.getProperties().setProperty("server.name", "Springboot"); setLogsDir(); diff --git a/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/CatalogDBConfig.java b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/CatalogDBConfig.java new file mode 100644 index 0000000000..f7faa1f709 --- /dev/null +++ b/adapters/mso-catalog-db-adapter/src/main/java/org/onap/so/adapters/catalogdb/CatalogDBConfig.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.catalogdb; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jmx.export.MBeanExporter; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", + basePackages = {"org.onap.so.db.catalog.data.repository"}) +@Profile({"!test"}) +public class CatalogDBConfig { + + @Autowired(required = false) + private MBeanExporter mBeanExporter; + + @Bean + @ConfigurationProperties(prefix = "spring.datasource.hikari") + public HikariConfig catalogDbConfig() { + return new HikariConfig(); + } + + @Primary + @Bean(name = "dataSource") + public DataSource dataSource() { + if (mBeanExporter != null) { + mBeanExporter.addExcludedBean("dataSource"); + } + HikariConfig hikariConfig = this.catalogDbConfig(); + return new HikariDataSource(hikariConfig); + } + + @Primary + @Bean(name = "entityManagerFactory") + public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, + @Qualifier("dataSource") DataSource dataSource) { + return builder.dataSource(dataSource).packages("org.onap.so.db.catalog.beans").persistenceUnit("catalogDB") + .build(); + } + + @Primary + @Bean(name = "transactionManager") + public PlatformTransactionManager transactionManager( + @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) { + return new JpaTransactionManager(entityManagerFactory); + } + +} diff --git a/adapters/mso-catalog-db-adapter/src/main/resources/application.yaml b/adapters/mso-catalog-db-adapter/src/main/resources/application.yaml index b1528a0897..bcf5429789 100644 --- a/adapters/mso-catalog-db-adapter/src/main/resources/application.yaml +++ b/adapters/mso-catalog-db-adapter/src/main/resources/application.yaml @@ -16,11 +16,14 @@ mso: spring: datasource: - url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - driver-class-name: org.mariadb.jdbc.Driver - initialization-mode: never + hikari: + jdbcUrl: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driver-class-name: org.mariadb.jdbc.Driver + pool-name: catdb-pool + registerMbeans: true + flyway: baseline-on-migrate: false url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb diff --git a/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/CatalogDBConfig.java b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/CatalogDBConfig.java new file mode 100644 index 0000000000..299e695275 --- /dev/null +++ b/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/CatalogDBConfig.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.openstack; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", + basePackages = {"org.onap.so.db.catalog.data.repository"}) +@EnableScheduling +@Profile({"!test"}) +public class CatalogDBConfig { + + @Bean + @ConfigurationProperties(prefix = "spring.datasource.hikari") + public HikariConfig catalogDbConfig() { + return new HikariConfig(); + } + + @Primary + @Bean(name = "dataSource") + public DataSource dataSource() { + HikariConfig hikariConfig = this.catalogDbConfig(); + return new HikariDataSource(hikariConfig); + } + + @Primary + @Bean(name = "entityManagerFactory") + public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, + @Qualifier("dataSource") DataSource dataSource) { + return builder.dataSource(dataSource).packages("org.onap.so.db.catalog.beans").persistenceUnit("catalogDB") + .build(); + } + + @Primary + @Bean(name = "transactionManager") + public PlatformTransactionManager transactionManager( + @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) { + return new JpaTransactionManager(entityManagerFactory); + } + +} diff --git a/adapters/mso-openstack-adapters/src/main/resources/application.yaml b/adapters/mso-openstack-adapters/src/main/resources/application.yaml index 470bb31b19..1c4de2daa1 100644 --- a/adapters/mso-openstack-adapters/src/main/resources/application.yaml +++ b/adapters/mso-openstack-adapters/src/main/resources/application.yaml @@ -19,11 +19,13 @@ mso: retrySequence: 1, 1, 2, 3, 5, 8, 13, 20 spring: datasource: - url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - driver-class-name: org.mariadb.jdbc.Driver - initialization-mode: never + hikari: + jdbc-url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driver-class-name: org.mariadb.jdbc.Driver + pool-name: catdb-pool + registerMbeans: true jpa: show-sql: false hibernate: diff --git a/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/application/MSORequestDBApplication.java b/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/application/MSORequestDBApplication.java index 6dab6c188c..85c05decb1 100644 --- a/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/application/MSORequestDBApplication.java +++ b/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/application/MSORequestDBApplication.java @@ -27,6 +27,9 @@ import javax.sql.DataSource; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.EnableMBeanExport; +import org.springframework.jmx.support.RegistrationPolicy; +import org.springframework.scheduling.annotation.EnableScheduling; import net.javacrumbs.shedlock.core.LockProvider; import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider; import net.javacrumbs.shedlock.spring.ScheduledLockConfiguration; @@ -38,6 +41,8 @@ import net.javacrumbs.shedlock.spring.ScheduledLockConfigurationBuilder; */ @SpringBootApplication(scanBasePackages = {"org.onap.so"}) +@EnableScheduling +@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class MSORequestDBApplication { private static final String LOGS_DIR = "logs_dir"; @@ -50,6 +55,7 @@ public class MSORequestDBApplication { public static void main(String... args) { SpringApplication.run(MSORequestDBApplication.class, args); + java.security.Security.setProperty("networkaddress.cache.ttl", "10"); setLogsDir(); } @@ -64,5 +70,4 @@ public class MSORequestDBApplication { .withDefaultLockAtMostFor(Duration.ofMinutes(10)).build(); } - } diff --git a/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/application/RequestDBConfig.java b/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/application/RequestDBConfig.java index 651cce8543..b14e3ce4fb 100644 --- a/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/application/RequestDBConfig.java +++ b/adapters/mso-requests-db-adapter/src/main/java/org/onap/so/adapters/requestsdb/application/RequestDBConfig.java @@ -23,6 +23,7 @@ package org.onap.so.adapters.requestsdb.application; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -32,10 +33,13 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Profile; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jmx.export.MBeanExporter; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; @Profile({"!test"}) @Configuration @@ -44,13 +48,25 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; transactionManagerRef = "requestTransactionManager", basePackages = {"org.onap.so.db.request.data.repository"}) public class RequestDBConfig { - @Primary + @Autowired(required = false) + private MBeanExporter mBeanExporter; + + @Bean + @ConfigurationProperties(prefix = "spring.datasource.hikari") + public HikariConfig requestDbConfig() { + return new HikariConfig(); + } + @Bean(name = "requestDataSource") - @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { - return DataSourceBuilder.create().build(); + if (mBeanExporter != null) { + mBeanExporter.addExcludedBean("requestDataSource"); + } + HikariConfig hikariConfig = this.requestDbConfig(); + return new HikariDataSource(hikariConfig); } + @Primary @Bean(name = "requestEntityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, diff --git a/adapters/mso-requests-db-adapter/src/main/resources/application.yaml b/adapters/mso-requests-db-adapter/src/main/resources/application.yaml index 645a6e9e38..7234733b95 100644 --- a/adapters/mso-requests-db-adapter/src/main/resources/application.yaml +++ b/adapters/mso-requests-db-adapter/src/main/resources/application.yaml @@ -18,10 +18,13 @@ mso: # H2 spring: datasource: - jdbcUrl: jdbc:mariadb://${DB_HOST}:${DB_PORT}/requestdb - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - driver-class-name: org.mariadb.jdbc.Driver + hikari: + jdbc-url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/requestdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driver-class-name: org.mariadb.jdbc.Driver + pool-name: reqdb-pool + registerMbeans: true flyway: baseline-on-migrate: false url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/requestdb @@ -57,4 +60,4 @@ management: export: prometheus: enabled: true # Whether exporting of metrics to Prometheus is enabled. - step: 1m # Step size (i.e. reporting frequency) to use. \ No newline at end of file + step: 1m # Step size (i.e. reporting frequency) to use. diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/CatalogDBConfig.java b/asdc-controller/src/main/java/org/onap/so/asdc/CatalogDBConfig.java index 3494945020..39bb836ff8 100644 --- a/asdc-controller/src/main/java/org/onap/so/asdc/CatalogDBConfig.java +++ b/asdc-controller/src/main/java/org/onap/so/asdc/CatalogDBConfig.java @@ -20,11 +20,9 @@ package org.onap.so.asdc; - import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; @@ -36,6 +34,8 @@ import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; @Configuration @EnableTransactionManagement @@ -44,11 +44,17 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @Profile({"!test"}) public class CatalogDBConfig { + @Bean + @ConfigurationProperties(prefix = "spring.datasource.hikari") + public HikariConfig catalogDbConfig() { + return new HikariConfig(); + } + @Primary @Bean(name = "dataSource") - @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { - return DataSourceBuilder.create().build(); + HikariConfig hikariConfig = this.catalogDbConfig(); + return new HikariDataSource(hikariConfig); } @Primary diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/RequestDBConfig.java b/asdc-controller/src/main/java/org/onap/so/asdc/RequestDBConfig.java index 8320da01cf..821b2dacff 100644 --- a/asdc-controller/src/main/java/org/onap/so/asdc/RequestDBConfig.java +++ b/asdc-controller/src/main/java/org/onap/so/asdc/RequestDBConfig.java @@ -20,11 +20,9 @@ package org.onap.so.asdc; - import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; @@ -35,6 +33,8 @@ import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; @Configuration @EnableTransactionManagement @@ -43,13 +43,18 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @Profile({"!test"}) public class RequestDBConfig { + @Bean + @ConfigurationProperties(prefix = "request.datasource.hikari") + public HikariConfig requestDbConfig() { + return new HikariConfig(); + } + @Bean(name = "requestDataSource") - @ConfigurationProperties(prefix = "request.datasource") public DataSource dataSource() { - return DataSourceBuilder.create().build(); + HikariConfig hikariConfig = this.requestDbConfig(); + return new HikariDataSource(hikariConfig); } - @Bean(name = "requestEntityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("requestDataSource") DataSource dataSource) { @@ -57,7 +62,6 @@ public class RequestDBConfig { .build(); } - @Bean(name = "requestTransactionManager") public PlatformTransactionManager transactionManager( @Qualifier("requestEntityManagerFactory") EntityManagerFactory entityManagerFactory) { diff --git a/asdc-controller/src/main/resources/application.yaml b/asdc-controller/src/main/resources/application.yaml index 2d0a2acf94..beb40e5e65 100644 --- a/asdc-controller/src/main/resources/application.yaml +++ b/asdc-controller/src/main/resources/application.yaml @@ -4,15 +4,13 @@ server: spring: datasource: - jdbc-url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - driver-class-name: org.mariadb.jdbc.Driver - dbcp2: - initial-size: 5 - max-total: 20 - validation-query: select 1 - test-on-borrow: true + hikari: + jdbc-url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driver-class-name: org.mariadb.jdbc.Driver + pool-name: catdb-pool + registerMbeans: true jpa: show-sql: true hibernate: @@ -23,10 +21,14 @@ spring: request: datasource: - jdbc-url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/requestdb - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - driver-class-name: org.mariadb.jdbc.Driver + hikari: + jdbc-url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/requestdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driver-class-name: org.mariadb.jdbc.Driver + pool-name: reqdb-pool + registerMbeans: true + #Actuator management: endpoints: diff --git a/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/infrastructure/CamundaDBConfig.java b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/infrastructure/CamundaDBConfig.java new file mode 100644 index 0000000000..686b377727 --- /dev/null +++ b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/infrastructure/CamundaDBConfig.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.bpmn.infrastructure; + + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jmx.export.MBeanExporter; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", + basePackages = {"org.onap.so.db.catalog.data.repository"}) +@Profile({"!test"}) +public class CamundaDBConfig { + + @Autowired(required = false) + private MBeanExporter mBeanExporter; + + @Bean + @ConfigurationProperties(prefix = "spring.datasource.hikari") + public HikariConfig camundaDbConfig() { + return new HikariConfig(); + } + + @Primary + @Bean(name = "dataSource") + public DataSource dataSource() { + if (mBeanExporter != null) { + mBeanExporter.addExcludedBean("dataSource"); + } + HikariConfig hikariConfig = this.camundaDbConfig(); + return new HikariDataSource(hikariConfig); + } + + @Primary + @Bean(name = "entityManagerFactory") + public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, + @Qualifier("dataSource") DataSource dataSource) { + return builder.dataSource(dataSource).packages("org.onap.so.db.catalog.beans").persistenceUnit("catalogDB") + .build(); + } + + @Primary + @Bean(name = "transactionManager") + public PlatformTransactionManager transactionManager( + @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) { + return new JpaTransactionManager(entityManagerFactory); + } + +} diff --git a/bpmn/mso-infrastructure-bpmn/src/main/resources/application.yaml b/bpmn/mso-infrastructure-bpmn/src/main/resources/application.yaml index e364981a66..185db168fe 100644 --- a/bpmn/mso-infrastructure-bpmn/src/main/resources/application.yaml +++ b/bpmn/mso-infrastructure-bpmn/src/main/resources/application.yaml @@ -7,10 +7,13 @@ mso: auditInventory: false spring: datasource: - driver-class-name: org.mariadb.jdbc.Driver - url: jdbc:mariadb://${DB_HOST}:${DB_PORT}/camundabpmn - username: ${DB_ADMIN_USERNAME} - password: ${DB_ADMIN_PASSWORD} + hikari: + jdbcUrl: jdbc:mariadb://${DB_HOST}:${DB_PORT}/camundabpmn + username: ${DB_ADMIN_USERNAME} + password: ${DB_ADMIN_PASSWORD} + driver-class-name: org.mariadb.jdbc.Driver + pool-name: bpmn-pool + registerMbeans: true http: multipart: enabled: false diff --git a/common/pom.xml b/common/pom.xml index 2742afee1a..316cad1da7 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -19,6 +19,10 @@ + + hikari-cp + hikari-cp + org.apache.httpcomponents httpclient diff --git a/common/src/main/java/org/onap/so/db/connections/DbDnsIpAddress.java b/common/src/main/java/org/onap/so/db/connections/DbDnsIpAddress.java new file mode 100644 index 0000000000..8ee63a487b --- /dev/null +++ b/common/src/main/java/org/onap/so/db/connections/DbDnsIpAddress.java @@ -0,0 +1,20 @@ +package org.onap.so.db.connections; + +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Component +@Scope("singleton") +public class DbDnsIpAddress { + + private String ipAddress; + + public String getIpAddress() { + return ipAddress; + } + + public void setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + } + +} diff --git a/common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java b/common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java new file mode 100644 index 0000000000..14f2f5e9b7 --- /dev/null +++ b/common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java @@ -0,0 +1,78 @@ +package org.onap.so.db.connections; + +import java.lang.management.ManagementFactory; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Set; +import javax.management.JMX; +import javax.management.MBeanServer; +import javax.management.ObjectInstance; +import javax.management.ObjectName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import com.zaxxer.hikari.HikariPoolMXBean; + +@Component +@Profile("!test") +public class ScheduledDnsLookup { + + private static final String JMX_HIKARI_DB_POOL_LOOKUP = "com.zaxxer.hikari:type=Pool (*,*"; + + private static final String DB_HOST = "DB_HOST"; + + @Autowired + private DbDnsIpAddress dnsIpAddress; + + private static Logger logger = LoggerFactory.getLogger(ScheduledDnsLookup.class); + + @Scheduled(fixedRate = 15000) + public void performDnsLookup() { + + String dnsUrl = System.getenv(DB_HOST); + + try { + if (dnsUrl == null) { + logger.error("Database DNS is not provided. Please verify the configuration"); + return; + } + + InetAddress inetAddress = java.net.InetAddress.getByName(dnsUrl); + String ipAddress = inetAddress.getHostAddress(); + String currentIpAddress = dnsIpAddress.getIpAddress(); + /* This is in initial state */ + if (currentIpAddress == null) { + dnsIpAddress.setIpAddress(ipAddress); + return; + } + + if ((ipAddress != null) && (!ipAddress.equalsIgnoreCase(currentIpAddress))) { + logger.debug("Switched Database IP Address from {} to {}", currentIpAddress, ipAddress); + softEvictConnectionPool(); + dnsIpAddress.setIpAddress(ipAddress); + } + } catch (UnknownHostException e) { + logger.warn("Database DNS %s is not resolvable to an IP Address", dnsUrl); + } + + } + + private void softEvictConnectionPool() { + try { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + ObjectName queryObj = new ObjectName(JMX_HIKARI_DB_POOL_LOOKUP); + Set objects = mBeanServer.queryMBeans(queryObj, null); + for (ObjectInstance objectInstance : objects) { + ObjectName poolObject = objectInstance.getObjectName(); + HikariPoolMXBean poolProxy = JMX.newMXBeanProxy(mBeanServer, poolObject, HikariPoolMXBean.class); + logger.debug("database connection pool is soft evicted for connections"); + poolProxy.softEvictConnections(); + } + } catch (Exception e) { + logger.warn("Error encountered in evicting DB connection pool", e); + } + } +} diff --git a/common/src/test/java/org/onap/so/db/connections/DbDnsIpAddressTest.java b/common/src/test/java/org/onap/so/db/connections/DbDnsIpAddressTest.java new file mode 100644 index 0000000000..0dc35a054c --- /dev/null +++ b/common/src/test/java/org/onap/so/db/connections/DbDnsIpAddressTest.java @@ -0,0 +1,19 @@ +package org.onap.so.db.connections; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class DbDnsIpAddressTest { + + @Test + public void test() { + final String expectedIpAddress = "10.0.75.1"; + + DbDnsIpAddress dbDnsIpAddress = new DbDnsIpAddress(); + dbDnsIpAddress.setIpAddress(expectedIpAddress); + + assertEquals(expectedIpAddress, dbDnsIpAddress.getIpAddress()); + + } + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ApiHandlerApplication.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ApiHandlerApplication.java index bc1972a593..5beb4810ec 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ApiHandlerApplication.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ApiHandlerApplication.java @@ -21,17 +21,18 @@ package org.onap.so.apihandlerinfra; import java.util.concurrent.Executor; -import org.springframework.beans.factory.annotation.Autowired; +import org.onap.so.logging.jaxrs.filter.MDCTaskDecorator; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import org.onap.so.logging.jaxrs.filter.MDCTaskDecorator; @SpringBootApplication(scanBasePackages = {"org.onap"}) @EnableAsync +@EnableScheduling public class ApiHandlerApplication { @Value("${mso.async.core-pool-size}") @@ -53,6 +54,7 @@ public class ApiHandlerApplication { public static void main(String[] args) { SpringApplication.run(ApiHandlerApplication.class, args); + java.security.Security.setProperty("networkaddress.cache.ttl", "10"); System.getProperties().setProperty("mso.db", "MARIADB"); System.getProperties().setProperty("server.name", "Springboot"); setLogsDir(); @@ -69,4 +71,5 @@ public class ApiHandlerApplication { executor.initialize(); return executor; } + } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/configuration/CatalogDBConfig.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/configuration/CatalogDBConfig.java index 4d81695e55..0db63e7493 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/configuration/CatalogDBConfig.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/configuration/CatalogDBConfig.java @@ -23,8 +23,8 @@ package org.onap.so.apihandlerinfra.configuration; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; @@ -32,10 +32,13 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Profile; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jmx.export.MBeanExporter; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; @Configuration @EnableTransactionManagement @@ -44,11 +47,23 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @Profile({"!test"}) public class CatalogDBConfig { + @Autowired(required = false) + private MBeanExporter mBeanExporter; + + @Bean + @ConfigurationProperties(prefix = "spring.datasource.hikari") + public HikariConfig catalogDbConfig() { + return new HikariConfig(); + } + @Primary @Bean(name = "dataSource") - @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { - return DataSourceBuilder.create().build(); + if (mBeanExporter != null) { + mBeanExporter.addExcludedBean("dataSource"); + } + HikariConfig hikariConfig = this.catalogDbConfig(); + return new HikariDataSource(hikariConfig); } @Primary diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/configuration/RequestDBConfig.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/configuration/RequestDBConfig.java index 1bc54ffe7b..02cbf2f3bf 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/configuration/RequestDBConfig.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/configuration/RequestDBConfig.java @@ -23,18 +23,21 @@ package org.onap.so.apihandlerinfra.configuration; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jmx.export.MBeanExporter; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; @Configuration @EnableTransactionManagement @@ -43,13 +46,24 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @Profile({"!test"}) public class RequestDBConfig { + @Autowired(required = false) + private MBeanExporter mBeanExporter; + + @Bean + @ConfigurationProperties(prefix = "request.datasource.hikari") + public HikariConfig requestDbConfig() { + return new HikariConfig(); + } + @Bean(name = "requestDataSource") - @ConfigurationProperties(prefix = "request.datasource") public DataSource dataSource() { - return DataSourceBuilder.create().build(); + if (mBeanExporter != null) { + mBeanExporter.addExcludedBean("requestDataSource"); + } + HikariConfig hikariConfig = this.requestDbConfig(); + return new HikariDataSource(hikariConfig); } - @Bean(name = "requestEntityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("requestDataSource") DataSource dataSource) { diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/resources/application.yaml b/mso-api-handlers/mso-api-handler-infra/src/main/resources/application.yaml index 03934edf20..136acfb9e1 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/resources/application.yaml +++ b/mso-api-handlers/mso-api-handler-infra/src/main/resources/application.yaml @@ -29,10 +29,13 @@ mso: spring: datasource: - jdbcUrl: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - driver-class-name: org.mariadb.jdbc.Driver + hikari: + jdbcUrl: jdbc:mariadb://${DB_HOST}:${DB_PORT}/catalogdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driver-class-name: org.mariadb.jdbc.Driver + pool-name: catdb-pool + registerMbeans: true jpa: show-sql: true hibernate: @@ -45,11 +48,13 @@ spring: request: datasource: - jdbcUrl: jdbc:mariadb://${DB_HOST}:${DB_PORT}/requestdb - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - driver-class-name: org.mariadb.jdbc.Driver - + hikari: + jdbcUrl: jdbc:mariadb://${DB_HOST}:${DB_PORT}/requestdb + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driver-class-name: org.mariadb.jdbc.Driver + pool-name: reqdb-pool + registerMbeans: true #Actuator management: endpoints: diff --git a/pom.xml b/pom.xml index 1abbeaec80..7384c5b104 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,8 @@ 3.2.0 false true + 2.7.1 + 4.1.0 @@ -502,7 +504,7 @@ io.fabric8 fabric8-maven-plugin - 4.1.0 + ${io.fabric8.version} ${docker.skip} ${docker.skip.build} @@ -510,12 +512,12 @@ ${docker.newHost} ${docker.host.cert.path} - + ${dockerPushRepo} ${dockerPullRepo} - + service @@ -858,6 +860,11 @@ snakeyaml 1.19 + + hikari-cp + hikari-cp + ${hikari.cp.version} + -- cgit 1.2.3-korg