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) --- 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 ++++++ 4 files changed, 121 insertions(+) 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 (limited to 'common') 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()); + + } + +} -- cgit 1.2.3-korg